devices_test.dart 14.9 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'dart:async';
6

7
import 'package:file/memory.dart';
8
import 'package:flutter_tools/src/base/file_system.dart';
9
import 'package:flutter_tools/src/base/logger.dart';
10
import 'package:flutter_tools/src/base/platform.dart';
11
import 'package:flutter_tools/src/build_info.dart';
12
import 'package:flutter_tools/src/device.dart';
13
import 'package:flutter_tools/src/web/chrome.dart';
14 15
import 'package:flutter_tools/src/web/web_device.dart';

16
import '../../src/common.dart';
17
import '../../src/fake_process_manager.dart';
18
import '../../src/fakes.dart';
19 20

void main() {
21 22
  testWithoutContext('No web devices listed if feature is disabled', () async {
    final WebDevices webDevices = WebDevices(
23
      featureFlags: TestFeatureFlags(),
24 25 26 27 28 29 30 31 32
      fileSystem: MemoryFileSystem.test(),
      logger: BufferLogger.test(),
      platform: FakePlatform(
        environment: <String, String>{}
      ),
      processManager:  FakeProcessManager.any(),
    );

    expect(await webDevices.pollingGetDevices(), isEmpty);
33
  });
34

35
  testWithoutContext('GoogleChromeDevice defaults', () async {
36 37
    final TestChromiumLauncher launcher = TestChromiumLauncher();

38
    final GoogleChromeDevice chromeDevice = GoogleChromeDevice(
39
      chromiumLauncher: launcher,
40 41
      fileSystem: MemoryFileSystem.test(),
      logger: BufferLogger.test(),
42
      platform: FakePlatform(),
43 44
      processManager: FakeProcessManager.any(),
    );
45

46 47 48 49 50
    expect(chromeDevice.name, 'Chrome');
    expect(chromeDevice.id, 'chrome');
    expect(chromeDevice.supportsHotReload, true);
    expect(chromeDevice.supportsHotRestart, true);
    expect(chromeDevice.supportsStartPaused, true);
51
    expect(chromeDevice.supportsFlutterExit, false);
52 53
    expect(chromeDevice.supportsScreenshot, false);
    expect(await chromeDevice.isLocalEmulator, false);
54 55
    expect(chromeDevice.getLogReader(), isA<NoOpDeviceLogReader>());
    expect(chromeDevice.getLogReader(), isA<NoOpDeviceLogReader>());
56
    expect(await chromeDevice.portForwarder!.forward(1), 1);
57 58 59 60 61

    expect(chromeDevice.supportsRuntimeMode(BuildMode.debug), true);
    expect(chromeDevice.supportsRuntimeMode(BuildMode.profile), true);
    expect(chromeDevice.supportsRuntimeMode(BuildMode.release), true);
    expect(chromeDevice.supportsRuntimeMode(BuildMode.jitRelease), false);
62 63 64
  });

  testWithoutContext('MicrosoftEdge defaults', () async {
65 66
    final TestChromiumLauncher launcher = TestChromiumLauncher();

67
    final MicrosoftEdgeDevice chromeDevice = MicrosoftEdgeDevice(
68
      chromiumLauncher: launcher,
69 70
      fileSystem: MemoryFileSystem.test(),
      logger: BufferLogger.test(),
71
      processManager: FakeProcessManager.any(),
72 73 74 75 76 77 78
    );

    expect(chromeDevice.name, 'Edge');
    expect(chromeDevice.id, 'edge');
    expect(chromeDevice.supportsHotReload, true);
    expect(chromeDevice.supportsHotRestart, true);
    expect(chromeDevice.supportsStartPaused, true);
79
    expect(chromeDevice.supportsFlutterExit, false);
80 81 82
    expect(chromeDevice.supportsScreenshot, false);
    expect(await chromeDevice.isLocalEmulator, false);
    expect(chromeDevice.getLogReader(), isA<NoOpDeviceLogReader>());
Dan Field's avatar
Dan Field committed
83
    expect(chromeDevice.getLogReader(), isA<NoOpDeviceLogReader>());
84
    expect(await chromeDevice.portForwarder!.forward(1), 1);
85 86 87 88 89

    expect(chromeDevice.supportsRuntimeMode(BuildMode.debug), true);
    expect(chromeDevice.supportsRuntimeMode(BuildMode.profile), true);
    expect(chromeDevice.supportsRuntimeMode(BuildMode.release), true);
    expect(chromeDevice.supportsRuntimeMode(BuildMode.jitRelease), false);
90
  });
91

92 93 94 95
  testWithoutContext('Server defaults', () async {
    final WebServerDevice device = WebServerDevice(
      logger: BufferLogger.test(),
    );
96

97 98
    expect(device.name, 'Web Server');
    expect(device.id, 'web-server');
99 100 101
    expect(device.supportsHotReload, true);
    expect(device.supportsHotRestart, true);
    expect(device.supportsStartPaused, true);
102
    expect(device.supportsFlutterExit, false);
103 104
    expect(device.supportsScreenshot, false);
    expect(await device.isLocalEmulator, false);
105
    expect(device.getLogReader(), isA<NoOpDeviceLogReader>());
Dan Field's avatar
Dan Field committed
106
    expect(device.getLogReader(), isA<NoOpDeviceLogReader>());
107
    expect(await device.portForwarder!.forward(1), 1);
108 109 110 111 112 113

    expect(device.supportsRuntimeMode(BuildMode.debug), true);
    expect(device.supportsRuntimeMode(BuildMode.profile), true);
    expect(device.supportsRuntimeMode(BuildMode.release), true);
    expect(device.supportsRuntimeMode(BuildMode.jitRelease), false);
});
114

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
  testWithoutContext('ChromiumDevice accepts null package', () async {
    final MemoryFileSystem fs = MemoryFileSystem.test();
    final FakePlatform platform = FakePlatform();
    final FakeProcessManager pm = FakeProcessManager.any();
    final BufferLogger logger = BufferLogger.test();
    final GoogleChromeDevice device = GoogleChromeDevice(
      fileSystem: fs,
      processManager: pm,
      platform: platform,
      chromiumLauncher: ChromiumLauncher(
        fileSystem: fs,
        platform: platform,
        processManager: pm,
        operatingSystemUtils: FakeOperatingSystemUtils(),
        browserFinder: findChromeExecutable,
        logger: logger,
      ),
      logger: logger,
    );
    await expectLater(
      () => device.startApp(
        null,
        debuggingOptions: DebuggingOptions.disabled(BuildInfo.debug),
        platformArgs: <String, Object?>{'uri': 'localhost:1234'},
      ),
      // The tool exit here is irrelevant, this test simply ensures ChromiumDevice.startApp
      // will accept a null value for a package.
      throwsToolExit(message: 'Failed to launch browser'),
    );
  });

146 147 148 149 150 151 152 153 154 155 156 157 158
  testWithoutContext('Chrome device is listed when Chrome can be run', () async {
    final WebDevices webDevices = WebDevices(
      featureFlags: TestFeatureFlags(isWebEnabled: true),
      fileSystem: MemoryFileSystem.test(),
      logger: BufferLogger.test(),
      platform: FakePlatform(
        environment: <String, String>{}
      ),
      processManager:  FakeProcessManager.any(),
    );

    expect(await webDevices.pollingGetDevices(),
      contains(isA<GoogleChromeDevice>()));
159 160
  });

161 162 163 164 165 166 167 168 169 170 171 172 173 174
  testWithoutContext('Has well known device ids chrome, edge, and web-server', () async {
    final WebDevices webDevices = WebDevices(
      featureFlags: TestFeatureFlags(isWebEnabled: true),
      fileSystem: MemoryFileSystem.test(),
      logger: BufferLogger.test(),
      platform: FakePlatform(
        environment: <String, String>{}
      ),
      processManager:  FakeProcessManager.any(),
    );

    expect(webDevices.wellKnownIds, <String>['chrome', 'web-server', 'edge']);
  });

175
  testWithoutContext('Chrome device is not listed when Chrome cannot be run', () async {
176
    final FakeProcessManager processManager = FakeProcessManager.empty();
177
    processManager.excludedExecutables = <String>{kLinuxExecutable};
178 179 180 181 182 183 184 185 186 187 188 189
    final WebDevices webDevices = WebDevices(
      featureFlags: TestFeatureFlags(isWebEnabled: true),
      fileSystem: MemoryFileSystem.test(),
      logger: BufferLogger.test(),
      platform: FakePlatform(
        environment: <String, String>{}
      ),
      processManager: processManager,
    );

    expect(await webDevices.pollingGetDevices(),
      isNot(contains(isA<GoogleChromeDevice>())));
190 191
  });

192 193
  testWithoutContext('Web Server device is listed if enabled via showWebServerDevice', () async {
    WebServerDevice.showWebServerDevice = true;
194 195 196 197 198 199 200 201 202 203 204 205
    final WebDevices webDevices = WebDevices(
      featureFlags: TestFeatureFlags(isWebEnabled: true),
      fileSystem: MemoryFileSystem.test(),
      logger: BufferLogger.test(),
      platform: FakePlatform(
        environment: <String, String>{}
      ),
      processManager: FakeProcessManager.any(),
    );

    expect(await webDevices.pollingGetDevices(),
      contains(isA<WebServerDevice>()));
206 207
  });

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
  testWithoutContext('Web Server device is not listed if disabled via showWebServerDevice', () async {
    WebServerDevice.showWebServerDevice = false;
    final WebDevices webDevices = WebDevices(
      featureFlags: TestFeatureFlags(isWebEnabled: true),
      fileSystem: MemoryFileSystem.test(),
      logger: BufferLogger.test(),
      platform: FakePlatform(
        environment: <String, String>{}
      ),
      processManager: FakeProcessManager.any(),
    );

    expect(await webDevices.pollingGetDevices(),
      isNot(contains(isA<WebServerDevice>())));
  });

224 225 226 227 228 229 230
  testWithoutContext('Chrome invokes version command on non-Windows platforms', () async {
    final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
      const FakeCommand(
        command: <String>[
          kLinuxExecutable,
          '--version',
        ],
231 232
        stdout: 'ABC',
      ),
233 234 235 236 237 238 239 240 241 242 243 244 245 246
    ]);
    final WebDevices webDevices = WebDevices(
      featureFlags: TestFeatureFlags(isWebEnabled: true),
      fileSystem: MemoryFileSystem.test(),
      logger: BufferLogger.test(),
      platform: FakePlatform(
        environment: <String, String>{}
      ),
      processManager: processManager,
    );


    final GoogleChromeDevice chromeDevice = (await webDevices.pollingGetDevices())
      .whereType<GoogleChromeDevice>().first;
247

248 249
    expect(chromeDevice.isSupported(), true);
    expect(await chromeDevice.sdkNameAndVersion, 'ABC');
250 251 252

    // Verify caching works correctly.
    expect(await chromeDevice.sdkNameAndVersion, 'ABC');
253
    expect(processManager, hasNoRemainingExpectations);
254
  });
255

256
  testWithoutContext('Chrome and Edge version check invokes registry query on windows.', () async {
257
    final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
258 259 260 261 262 263 264 265 266 267
      const FakeCommand(
        command: <String>[
          'reg',
          'query',
          r'HKEY_CURRENT_USER\Software\Microsoft\Edge\BLBeacon',
          '/v',
          'version',
        ],
        stdout: r'HKEY_CURRENT_USER\Software\Microsoft\Edge\BLBeacon\ version REG_SZ 83.0.478.44 ',
      ),
268 269 270 271 272 273 274 275 276
      const FakeCommand(
        command: <String>[
          'reg',
          'query',
          r'HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon',
          '/v',
          'version',
        ],
        stdout: r'HKEY_CURRENT_USER\Software\Google\Chrome\BLBeacon\ version REG_SZ 74.0.0 A',
277
      ),
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
    ]);
    final WebDevices webDevices = WebDevices(
      featureFlags: TestFeatureFlags(isWebEnabled: true),
      fileSystem: MemoryFileSystem.test(),
      logger: BufferLogger.test(),
      platform: FakePlatform(
        operatingSystem: 'windows',
        environment: <String, String>{}
      ),
      processManager: processManager,
    );


    final GoogleChromeDevice chromeDevice = (await webDevices.pollingGetDevices())
      .whereType<GoogleChromeDevice>().first;
293 294 295

    expect(chromeDevice.isSupported(), true);
    expect(await chromeDevice.sdkNameAndVersion, 'Google Chrome 74.0.0');
296 297 298

    // Verify caching works correctly.
    expect(await chromeDevice.sdkNameAndVersion, 'Google Chrome 74.0.0');
299
    expect(processManager, hasNoRemainingExpectations);
300
  });
301

302
  testWithoutContext('Chrome and Edge version check handles missing registry on Windows', () async {
303
    final FakeProcessManager processManager = FakeProcessManager.empty();
304 305 306 307 308 309 310 311
    processManager.excludedExecutables.add('reg');

    final Platform platform = FakePlatform(
        operatingSystem: 'windows', environment: <String, String>{});
    final ChromiumLauncher chromeLauncher = ChromiumLauncher(
      fileSystem: MemoryFileSystem.test(),
      platform: platform,
      processManager: processManager,
312
      operatingSystemUtils: FakeOperatingSystemUtils(),
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
      browserFinder: findChromeExecutable,
      logger: BufferLogger.test(),
    );
    final MicrosoftEdgeDevice edgeDevice = MicrosoftEdgeDevice(
      chromiumLauncher: chromeLauncher,
      fileSystem: MemoryFileSystem.test(),
      logger: BufferLogger.test(),
      processManager: processManager,
    );

    expect(edgeDevice.isSupported(), true);
    expect(await edgeDevice.sdkNameAndVersion, '');

    final GoogleChromeDevice chromeDevice = GoogleChromeDevice(
      chromiumLauncher: chromeLauncher,
      fileSystem: MemoryFileSystem.test(),
      logger: BufferLogger.test(),
      processManager: processManager,
      platform: platform,
    );

    expect(chromeDevice.isSupported(), true);
    expect(await chromeDevice.sdkNameAndVersion, 'unknown');
  });

338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
  testWithoutContext('Edge is not supported on versions less than 73', () async {
    final FakeProcessManager processManager = FakeProcessManager.list(<FakeCommand>[
      const FakeCommand(
        command: <String>[
          'reg',
          'query',
          r'HKEY_CURRENT_USER\Software\Microsoft\Edge\BLBeacon',
          '/v',
          'version',
        ],
        stdout: r'HKEY_CURRENT_USER\Software\Microsoft\Edge\BLBeacon\ version REG_SZ 72.0.478.44 ',
      ),
    ]);
    final WebDevices webDevices = WebDevices(
      featureFlags: TestFeatureFlags(isWebEnabled: true),
      fileSystem: MemoryFileSystem.test(),
      logger: BufferLogger.test(),
      platform: FakePlatform(
        operatingSystem: 'windows',
        environment: <String, String>{}
      ),
      processManager: processManager,
    );

    expect((await webDevices.pollingGetDevices()).whereType<MicrosoftEdgeDevice>(), isEmpty);
  });

  testWithoutContext('Edge is not support on non-windows platform', () async {
    final WebDevices webDevices = WebDevices(
      featureFlags: TestFeatureFlags(isWebEnabled: true),
      fileSystem: MemoryFileSystem.test(),
      logger: BufferLogger.test(),
      platform: FakePlatform(
        environment: <String, String>{}
      ),
373
      processManager: FakeProcessManager.empty(),
374 375 376 377 378 379 380 381 382 383 384 385
    );

    expect((await webDevices.pollingGetDevices()).whereType<MicrosoftEdgeDevice>(), isEmpty);

    final WebDevices macosWebDevices = WebDevices(
      featureFlags: TestFeatureFlags(isWebEnabled: true),
      fileSystem: MemoryFileSystem.test(),
      logger: BufferLogger.test(),
      platform: FakePlatform(
        operatingSystem: 'macos',
        environment: <String, String>{}
      ),
386
      processManager: FakeProcessManager.empty(),
387 388 389 390
    );

    expect((await macosWebDevices.pollingGetDevices()).whereType<MicrosoftEdgeDevice>(), isEmpty);
  });
391
}
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413

/// A test implementation of the [ChromiumLauncher] that launches a fixed instance.
class TestChromiumLauncher implements ChromiumLauncher {
  TestChromiumLauncher();

  @override
  Completer<Chromium> currentCompleter = Completer<Chromium>();

  @override
  bool canFindExecutable() {
    return true;
  }

  @override
  Future<Chromium> get connectedInstance => currentCompleter.future;

  @override
  String findExecutable() {
    return 'chrome';
  }

  @override
414
  bool get hasChromeInstance => false;
415 416

  @override
417 418 419 420 421 422 423 424
  Future<Chromium> launch(
    String url, {
    bool headless = false,
    int? debugPort,
    bool skipCheck = false,
    Directory? cacheDir,
    List<String> webBrowserFlags = const <String>[],
  }) async {
425 426
    return currentCompleter.future;
  }
427 428 429 430 431

  @override
  Future<Chromium> connect(Chromium chrome, bool skipCheck) {
    return currentCompleter.future;
  }
432
}