vmservice_integration_test.dart 5.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:file/file.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:vm_service/vm_service.dart';
import 'package:vm_service/vm_service_io.dart';

import '../src/common.dart';
import 'test_data/basic_project.dart';
import 'test_driver.dart';
import 'test_utils.dart';

void main() {
16
  group('Flutter Tool VMService method', () {
17 18 19
    late Directory tempDir;
    late FlutterRunTestDriver flutter;
    late VmService vmService;
20

21
    setUp(() async {
22 23
      tempDir = createResolvedTempDirectorySync('vmservice_integration_test.');

24 25
      final BasicProject project = BasicProject();
      await project.setUpIn(tempDir);
26 27 28

      flutter = FlutterRunTestDriver(tempDir);
      await flutter.run(withDebugger: true);
29 30
      final int? port = flutter.vmServicePort;
      expect(port != null, true);
31 32 33
      vmService = await vmServiceConnectUri('ws://localhost:$port/ws');
    });

34
    tearDown(() async {
35
      await flutter.stop();
36 37 38
      tryToDelete(tempDir);
    });

39
    testWithoutContext('getSupportedProtocols includes DDS', () async {
40 41 42
      final ProtocolList protocolList =
          await vmService.getSupportedProtocols();
      expect(protocolList.protocols, hasLength(2));
43
      for (final Protocol protocol in protocolList.protocols!) {
44 45
        expect(protocol.protocolName, anyOf('VM Service', 'DDS'));
      }
46
    });
47

48
    testWithoutContext('flutterVersion can be called', () async {
49 50 51 52 53 54 55
      final Response response =
          await vmService.callServiceExtension('s0.flutterVersion');
      expect(response.type, 'Success');
      expect(response.json, containsPair('frameworkRevisionShort', isNotNull));
      expect(response.json, containsPair('engineRevisionShort', isNotNull));
    });

56
    testWithoutContext('flutterMemoryInfo can be called', () async {
57 58 59 60 61
      final Response response =
          await vmService.callServiceExtension('s0.flutterMemoryInfo');
      expect(response.type, 'Success');
    });

62
    testWithoutContext('reloadSources can be called', () async {
63
      final VM vm = await vmService.getVM();
64 65
      final IsolateRef? isolateRef = vm.isolates?.first;
      expect(isolateRef != null, true);
66
      final Response response = await vmService.callMethod('s0.reloadSources',
67
          isolateId: isolateRef!.id);
68 69
      expect(response.type, 'Success');
    });
70

71
    testWithoutContext('reloadSources fails on bad params', () async {
72 73 74 75
      final Future<Response> response =
          vmService.callMethod('s0.reloadSources', isolateId: '');
      expect(response, throwsA(const TypeMatcher<RPCError>()));
    });
76

77
    testWithoutContext('hotRestart can be called', () async {
78
      final VM vm = await vmService.getVM();
79 80
      final IsolateRef? isolateRef = vm.isolates?.first;
      expect(isolateRef != null, true);
81
      final Response response =
82
          await vmService.callMethod('s0.hotRestart', isolateId: isolateRef!.id);
83 84
      expect(response.type, 'Success');
    });
85

86
    testWithoutContext('hotRestart fails on bad params', () async {
87 88 89 90
      final Future<Response> response = vmService.callMethod('s0.hotRestart',
          args: <String, dynamic>{'pause': 'not_a_bool'});
      expect(response, throwsA(const TypeMatcher<RPCError>()));
    });
91

92
    testWithoutContext('flutterGetSkSL can be called', () async {
93 94 95 96 97
      final Response response = await vmService.callMethod('s0.flutterGetSkSL');

      expect(response.type, 'Success');
    });

98
    testWithoutContext('ext.flutter.brightnessOverride can toggle window brightness', () async {
99
      final Isolate isolate = await waitForExtension(vmService, 'ext.flutter.brightnessOverride');
100 101 102 103
      final Response response = await vmService.callServiceExtension(
        'ext.flutter.brightnessOverride',
        isolateId: isolate.id,
      );
104
      expect(response.json?['value'], 'Brightness.light');
105 106 107 108 109 110 111 112

      final Response updateResponse = await vmService.callServiceExtension(
        'ext.flutter.brightnessOverride',
        isolateId: isolate.id,
        args: <String, String>{
          'value': 'Brightness.dark',
        }
      );
113
      expect(updateResponse.json?['value'], 'Brightness.dark');
114 115 116 117 118 119 120 121 122

      // Change the brightness back to light
      final Response verifyResponse = await vmService.callServiceExtension(
        'ext.flutter.brightnessOverride',
        isolateId: isolate.id,
        args: <String, String>{
          'value': 'Brightness.light',
        }
      );
123
      expect(verifyResponse.json?['value'], 'Brightness.light');
124 125 126 127 128 129 130 131 132

      // Change with a bogus value
      final Response bogusResponse = await vmService.callServiceExtension(
        'ext.flutter.brightnessOverride',
        isolateId: isolate.id,
        args: <String, String>{
          'value': 'dark', // Intentionally invalid value.
        }
      );
133
      expect(bogusResponse.json?['value'], 'Brightness.light');
134 135
    });

136 137 138 139 140 141
    testWithoutContext('ext.flutter.debugPaint can toggle debug painting', () async {
      final Isolate isolate = await waitForExtension(vmService, 'ext.flutter.debugPaint');
      final Response response = await vmService.callServiceExtension(
        'ext.flutter.debugPaint',
        isolateId: isolate.id,
      );
142
      expect(response.json?['enabled'], 'false');
143 144 145 146 147 148 149 150

      final Response updateResponse = await vmService.callServiceExtension(
        'ext.flutter.debugPaint',
        isolateId: isolate.id,
        args: <String, String>{
          'enabled': 'true',
        }
      );
151
      expect(updateResponse.json?['enabled'], 'true');
152 153
    });
  });
154
}