device_test.dart 23.1 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 6
import 'dart:async';

7
import 'package:fake_async/fake_async.dart';
8
import 'package:flutter_tools/src/base/io.dart';
9
import 'package:flutter_tools/src/base/logger.dart';
10
import 'package:flutter_tools/src/base/terminal.dart';
11
import 'package:flutter_tools/src/base/user_messages.dart';
12
import 'package:flutter_tools/src/base/utils.dart';
13
import 'package:flutter_tools/src/build_info.dart';
14
import 'package:flutter_tools/src/convert.dart';
15
import 'package:flutter_tools/src/device.dart';
16
import 'package:flutter_tools/src/project.dart';
17
import 'package:test/fake.dart';
18

19
import '../src/common.dart';
20
import '../src/fake_devices.dart';
21

22
void main() {
23
  group('DeviceManager', () {
24
    testWithoutContext('getDevices', () async {
25 26 27 28
      final FakeDevice device1 = FakeDevice('Nexus 5', '0553790d0a4e726f');
      final FakeDevice device2 = FakeDevice('Nexus 5X', '01abfc49119c410e');
      final FakeDevice device3 = FakeDevice('iPod touch', '82564b38861a9a5');
      final List<Device> devices = <Device>[device1, device2, device3];
29 30 31 32 33 34 35

      final DeviceManager deviceManager = TestDeviceManager(
        devices,
        logger: BufferLogger.test(),
        terminal: Terminal.test(),
      );

36
      expect(await deviceManager.getDevices(), devices);
37
    });
38

39
    testWithoutContext('getDeviceById exact matcher', () async {
40 41 42
      final FakeDevice device1 = FakeDevice('Nexus 5', '0553790d0a4e726f');
      final FakeDevice device2 = FakeDevice('Nexus 5X', '01abfc49119c410e');
      final FakeDevice device3 = FakeDevice('iPod touch', '82564b38861a9a5');
43
      final List<Device> devices = <Device>[device1, device2, device3];
44
      final BufferLogger logger = BufferLogger.test();
45 46 47 48 49 50 51 52 53

      // Include different device discoveries:
      // 1. One that never completes to prove the first exact match is
      // returned quickly.
      // 2. One that throws, to prove matches can return when some succeed
      // and others fail.
      // 3. A device discoverer that succeeds.
      final DeviceManager deviceManager = TestDeviceManager(
        devices,
54 55 56 57
        deviceDiscoveryOverrides: <DeviceDiscovery>[
          ThrowingPollingDeviceDiscovery(),
          LongPollingDeviceDiscovery(),
        ],
58 59
        logger: logger,
        terminal: Terminal.test(),
60
      );
61

62
      Future<void> expectDevice(String id, List<Device> expected) async {
63
        expect(await deviceManager.getDevicesById(id), expected);
64
      }
65
      await expectDevice('01abfc49119c410e', <Device>[device2]);
66
      expect(logger.traceText, contains('Ignored error discovering 01abfc49119c410e'));
67
      await expectDevice('Nexus 5X', <Device>[device2]);
68
      expect(logger.traceText, contains('Ignored error discovering Nexus 5X'));
69
      await expectDevice('0553790d0a4e726f', <Device>[device1]);
70 71 72
      expect(logger.traceText, contains('Ignored error discovering 0553790d0a4e726f'));
    });

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    testWithoutContext('getDeviceById exact matcher with well known ID', () async {
      final FakeDevice device1 = FakeDevice('Windows', 'windows');
      final FakeDevice device2 = FakeDevice('Nexus 5X', '01abfc49119c410e');
      final FakeDevice device3 = FakeDevice('iPod touch', '82564b38861a9a5');
      final List<Device> devices = <Device>[device1, device2, device3];
      final BufferLogger logger = BufferLogger.test();

      // Because the well known ID will match, no other device discovery will run.
      final DeviceManager deviceManager = TestDeviceManager(
        devices,
        deviceDiscoveryOverrides: <DeviceDiscovery>[
          ThrowingPollingDeviceDiscovery(),
          LongPollingDeviceDiscovery(),
        ],
        logger: logger,
        terminal: Terminal.test(),
        wellKnownId: 'windows',
      );

      Future<void> expectDevice(String id, List<Device> expected) async {
        deviceManager.specifiedDeviceId = id;
        expect(await deviceManager.getDevicesById(id), expected);
      }
      await expectDevice('windows', <Device>[device1]);
      expect(logger.traceText, isEmpty);
    });

100
    testWithoutContext('getDeviceById prefix matcher', () async {
101 102 103 104
      final FakeDevice device1 = FakeDevice('Nexus 5', '0553790d0a4e726f');
      final FakeDevice device2 = FakeDevice('Nexus 5X', '01abfc49119c410e');
      final FakeDevice device3 = FakeDevice('iPod touch', '82564b38861a9a5');
      final List<Device> devices = <Device>[device1, device2, device3];
105
      final BufferLogger logger = BufferLogger.test();
106 107 108 109 110 111 112

      // Include different device discoveries:
      // 1. One that throws, to prove matches can return when some succeed
      // and others fail.
      // 2. A device discoverer that succeeds.
      final DeviceManager deviceManager = TestDeviceManager(
        devices,
113 114 115
        deviceDiscoveryOverrides: <DeviceDiscovery>[
          ThrowingPollingDeviceDiscovery(),
        ],
116 117
        logger: logger,
        terminal: Terminal.test(),
118 119 120 121 122
      );

      Future<void> expectDevice(String id, List<Device> expected) async {
        expect(await deviceManager.getDevicesById(id), expected);
      }
123
      await expectDevice('Nexus 5', <Device>[device1]);
124
      expect(logger.traceText, contains('Ignored error discovering Nexus 5'));
125
      await expectDevice('0553790', <Device>[device1]);
126
      expect(logger.traceText, contains('Ignored error discovering 0553790'));
127
      await expectDevice('Nexus', <Device>[device1, device2]);
128
      expect(logger.traceText, contains('Ignored error discovering Nexus'));
129
    });
130

131
    testWithoutContext('getAllConnectedDevices caches', () async {
132
      final FakeDevice device1 = FakeDevice('Nexus 5', '0553790d0a4e726f');
133 134 135 136 137
      final TestDeviceManager deviceManager = TestDeviceManager(
        <Device>[device1],
        logger: BufferLogger.test(),
        terminal: Terminal.test(),
      );
138 139
      expect(await deviceManager.getAllConnectedDevices(), <Device>[device1]);

140
      final FakeDevice device2 = FakeDevice('Nexus 5X', '01abfc49119c410e');
141 142 143 144
      deviceManager.resetDevices(<Device>[device2]);
      expect(await deviceManager.getAllConnectedDevices(), <Device>[device1]);
    });

145
    testWithoutContext('refreshAllConnectedDevices does not cache', () async {
146
      final FakeDevice device1 = FakeDevice('Nexus 5', '0553790d0a4e726f');
147 148 149 150 151
      final TestDeviceManager deviceManager = TestDeviceManager(
        <Device>[device1],
        logger: BufferLogger.test(),
        terminal: Terminal.test(),
      );
152 153
      expect(await deviceManager.refreshAllConnectedDevices(), <Device>[device1]);

154
      final FakeDevice device2 = FakeDevice('Nexus 5X', '01abfc49119c410e');
155 156 157
      deviceManager.resetDevices(<Device>[device2]);
      expect(await deviceManager.refreshAllConnectedDevices(), <Device>[device2]);
    });
158
  });
159

160 161 162 163 164
  testWithoutContext('PollingDeviceDiscovery startPolling', () {
    FakeAsync().run((FakeAsync time) {
      final FakePollingDeviceDiscovery pollingDeviceDiscovery = FakePollingDeviceDiscovery();
      pollingDeviceDiscovery.startPolling();
      time.elapse(const Duration(milliseconds: 4001));
165

166 167 168
      // First check should use the default polling timeout
      // to quickly populate the list.
      expect(pollingDeviceDiscovery.lastPollingTimeout, isNull);
169

170
      time.elapse(const Duration(milliseconds: 4001));
171

172 173 174
      // Subsequent polling should be much longer.
      expect(pollingDeviceDiscovery.lastPollingTimeout, const Duration(seconds: 30));
      pollingDeviceDiscovery.stopPolling();
175
    });
176 177
  });

178
  group('Filter devices', () {
179 180 181 182 183
    final FakeDevice ephemeralOne = FakeDevice('ephemeralOne', 'ephemeralOne');
    final FakeDevice ephemeralTwo = FakeDevice('ephemeralTwo', 'ephemeralTwo');
    final FakeDevice nonEphemeralOne = FakeDevice('nonEphemeralOne', 'nonEphemeralOne', ephemeral: false);
    final FakeDevice nonEphemeralTwo = FakeDevice('nonEphemeralTwo', 'nonEphemeralTwo', ephemeral: false);
    final FakeDevice unsupported = FakeDevice('unsupported', 'unsupported', isSupported: false);
184
    final FakeDevice unsupportedForProject = FakeDevice('unsupportedForProject', 'unsupportedForProject', isSupportedForProject: false);
185 186 187 188 189 190
    final FakeDevice webDevice = FakeDevice('webby', 'webby')
      ..targetPlatform = Future<TargetPlatform>.value(TargetPlatform.web_javascript);
    final FakeDevice fuchsiaDevice = FakeDevice('fuchsiay', 'fuchsiay')
      ..targetPlatform = Future<TargetPlatform>.value(TargetPlatform.fuchsia_x64);

    testWithoutContext('chooses ephemeral device', () async {
191
      final List<Device> devices = <Device>[
192
        ephemeralOne,
193 194 195
        nonEphemeralOne,
        nonEphemeralTwo,
        unsupported,
196
        unsupportedForProject,
197 198
      ];

199 200 201 202 203 204
      final DeviceManager deviceManager = TestDeviceManager(
        devices,
        logger: BufferLogger.test(),
        terminal: Terminal.test(),
      );
      final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
205

206
      expect(filtered.single, ephemeralOne);
207 208
    });

209
    testWithoutContext('choose first non-ephemeral device', () async {
210 211 212 213
      final List<Device> devices = <Device>[
        nonEphemeralOne,
        nonEphemeralTwo,
      ];
214 215
      final FakeTerminal terminal = FakeTerminal()
        ..setPrompt(<String>['1', '2', 'q', 'Q'], '1');
216

217 218 219
      final DeviceManager deviceManager = TestDeviceManager(
        devices,
        logger: BufferLogger.test(),
220
        terminal: terminal,
221 222
      );
      final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
223 224 225 226 227 228

      expect(filtered, <Device>[
        nonEphemeralOne
      ]);
    });

229
    testWithoutContext('choose second non-ephemeral device', () async {
230 231 232 233
      final List<Device> devices = <Device>[
        nonEphemeralOne,
        nonEphemeralTwo,
      ];
234 235
      final FakeTerminal terminal = FakeTerminal()
        ..setPrompt(<String>['1', '2', 'q', 'Q'], '2');
236

237 238 239
      final DeviceManager deviceManager = TestDeviceManager(
        devices,
        logger: BufferLogger.test(),
240
        terminal: terminal,
241 242
      );
      final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
243 244 245 246 247 248

      expect(filtered, <Device>[
        nonEphemeralTwo
      ]);
    });

249
    testWithoutContext('choose first ephemeral device', () async {
250 251 252 253 254
      final List<Device> devices = <Device>[
        ephemeralOne,
        ephemeralTwo,
      ];

255 256
      final FakeTerminal terminal = FakeTerminal()
        ..setPrompt(<String>['1', '2', 'q', 'Q'], '1');
257

258 259 260
      final DeviceManager deviceManager = TestDeviceManager(
        devices,
        logger: BufferLogger.test(),
261
        terminal: terminal,
262 263
      );
      final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
264 265 266 267 268 269

      expect(filtered, <Device>[
        ephemeralOne
      ]);
    });

270
    testWithoutContext('choose second ephemeral device', () async {
271 272 273 274
      final List<Device> devices = <Device>[
        ephemeralOne,
        ephemeralTwo,
      ];
275 276
      final FakeTerminal terminal = FakeTerminal()
        ..setPrompt(<String>['1', '2', 'q', 'Q'], '2');
277

278 279 280
      final DeviceManager deviceManager = TestDeviceManager(
        devices,
        logger: BufferLogger.test(),
281
        terminal: terminal,
282 283
      );
      final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
284 285

      expect(filtered, <Device>[
286 287 288 289
        ephemeralTwo
      ]);
    });

290
    testWithoutContext('choose non-ephemeral device', () async {
291 292 293
      final List<Device> devices = <Device>[
        ephemeralOne,
        ephemeralTwo,
294 295
        nonEphemeralOne,
        nonEphemeralTwo,
296 297
      ];

298 299
      final FakeTerminal terminal = FakeTerminal()
        ..setPrompt(<String>['1', '2', '3', '4', 'q', 'Q'], '3');
300

301 302 303
      final DeviceManager deviceManager = TestDeviceManager(
        devices,
        logger: BufferLogger.test(),
304
        terminal: terminal,
305 306 307
      );

      final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
308 309 310

      expect(filtered, <Device>[
        nonEphemeralOne
311 312
      ]);
    });
313

314
    testWithoutContext('exit from choose one of available devices', () async {
315 316 317 318 319
      final List<Device> devices = <Device>[
        ephemeralOne,
        ephemeralTwo,
      ];

320 321
      final FakeTerminal terminal = FakeTerminal()
        ..setPrompt(<String>['1', '2', 'q', 'Q'], 'q');
322 323 324 325

      final DeviceManager deviceManager = TestDeviceManager(
        devices,
        logger: BufferLogger.test(),
326
        terminal: terminal,
327 328
      );
      await expectLater(
329
        () async => deviceManager.findTargetDevices(FakeFlutterProject()),
330
        throwsToolExit(),
331
      );
332 333
    });

334
    testWithoutContext('Unsupported devices listed in all connected devices', () async {
335 336
      final List<Device> devices = <Device>[
        unsupported,
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
        unsupportedForProject,
      ];

      final DeviceManager deviceManager = TestDeviceManager(
        devices,
        logger: BufferLogger.test(),
        terminal: Terminal.test(),
      );
      final List<Device> filtered = await deviceManager.getAllConnectedDevices();

      expect(filtered, <Device>[
        unsupported,
        unsupportedForProject,
      ]);
    });

    testWithoutContext('Removes a unsupported devices', () async {
      final List<Device> devices = <Device>[
        unsupported,
        unsupportedForProject,
357 358
      ];

359 360 361 362 363 364
      final DeviceManager deviceManager = TestDeviceManager(
        devices,
        logger: BufferLogger.test(),
        terminal: Terminal.test(),
      );
      final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
365 366 367 368

      expect(filtered, <Device>[]);
    });

369
    testWithoutContext('Retains devices unsupported by the project if FlutterProject is null', () async {
370 371
      final List<Device> devices = <Device>[
        unsupported,
372
        unsupportedForProject,
373 374
      ];

375 376 377 378 379
      final DeviceManager deviceManager = TestDeviceManager(
        devices,
        logger: BufferLogger.test(),
        terminal: Terminal.test(),
      );
380 381
      final List<Device> filtered = await deviceManager.findTargetDevices(null);

382
      expect(filtered, <Device>[unsupportedForProject]);
383 384
    });

385
    testWithoutContext('Removes web and fuchsia from --all', () async {
386 387 388 389
      final List<Device> devices = <Device>[
        webDevice,
        fuchsiaDevice,
      ];
390 391 392 393 394
      final DeviceManager deviceManager = TestDeviceManager(
        devices,
        logger: BufferLogger.test(),
        terminal: Terminal.test(),
      );
395 396
      deviceManager.specifiedDeviceId = 'all';

397
      final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
398 399 400 401

      expect(filtered, <Device>[]);
    });

402
    testWithoutContext('Removes devices unsupported by the project from --all', () async {
403 404 405 406
      final List<Device> devices = <Device>[
        nonEphemeralOne,
        nonEphemeralTwo,
        unsupported,
407
        unsupportedForProject,
408
      ];
409 410 411 412 413
      final DeviceManager deviceManager = TestDeviceManager(
        devices,
        logger: BufferLogger.test(),
        terminal: Terminal.test(),
      );
414 415
      deviceManager.specifiedDeviceId = 'all';

416
      final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
417 418 419 420 421 422

      expect(filtered, <Device>[
        nonEphemeralOne,
        nonEphemeralTwo,
      ]);
    });
423

424
    testWithoutContext('uses DeviceManager.isDeviceSupportedForProject instead of device.isSupportedForProject', () async {
425 426
      final List<Device> devices = <Device>[
        unsupported,
427
        unsupportedForProject,
428
      ];
429 430 431 432 433
      final TestDeviceManager deviceManager = TestDeviceManager(
        devices,
        logger: BufferLogger.test(),
        terminal: Terminal.test(),
      );
434
      deviceManager.isAlwaysSupportedForProjectOverride = true;
435

436
      final List<Device> filtered = await deviceManager.findTargetDevices(FakeFlutterProject());
437 438

      expect(filtered, <Device>[
439
        unsupportedForProject,
440 441
      ]);
    });
442

443
    testWithoutContext('does not refresh device cache without a timeout', () async {
444 445 446
      final List<Device> devices = <Device>[
        ephemeralOne,
      ];
447 448
      final MockDeviceDiscovery deviceDiscovery = MockDeviceDiscovery()
        ..deviceValues = devices;
449

450 451 452
      final DeviceManager deviceManager = TestDeviceManager(
        <Device>[],
        deviceDiscoveryOverrides: <DeviceDiscovery>[
453
          deviceDiscovery
454 455 456 457
        ],
        logger: BufferLogger.test(),
        terminal: Terminal.test(),
      );
458 459
      deviceManager.specifiedDeviceId = ephemeralOne.id;
      final List<Device> filtered = await deviceManager.findTargetDevices(
460
        FakeFlutterProject(),
461 462 463
      );

      expect(filtered.single, ephemeralOne);
464 465
      expect(deviceDiscovery.devicesCalled, 1);
      expect(deviceDiscovery.discoverDevicesCalled, 0);
466 467
    });

468
    testWithoutContext('refreshes device cache with a timeout', () async {
469 470 471 472
      final List<Device> devices = <Device>[
        ephemeralOne,
      ];
      const Duration timeout = Duration(seconds: 2);
473 474
      final MockDeviceDiscovery deviceDiscovery = MockDeviceDiscovery()
        ..deviceValues = devices;
475

476 477 478
      final DeviceManager deviceManager = TestDeviceManager(
        <Device>[],
        deviceDiscoveryOverrides: <DeviceDiscovery>[
479
          deviceDiscovery
480 481 482 483
        ],
        logger: BufferLogger.test(),
        terminal: Terminal.test(),
      );
484 485
      deviceManager.specifiedDeviceId = ephemeralOne.id;
      final List<Device> filtered = await deviceManager.findTargetDevices(
486
        FakeFlutterProject(),
487 488 489 490
        timeout: timeout,
      );

      expect(filtered.single, ephemeralOne);
491 492
      expect(deviceDiscovery.devicesCalled, 1);
      expect(deviceDiscovery.discoverDevicesCalled, 1);
493
    });
494
  });
495

496
  group('JSON encode devices', () {
497
    testWithoutContext('Consistency of JSON representation', () async {
498 499 500 501 502 503 504 505
      expect(
        // This tests that fakeDevices is a list of tuples where "second" is the
        // correct JSON representation of the "first". Actual values are irrelevant
        await Future.wait(fakeDevices.map((FakeDeviceJsonData d) => d.dev.toJson())),
        fakeDevices.map((FakeDeviceJsonData d) => d.json)
      );
    });
  });
506 507

  testWithoutContext('computeDartVmFlags handles various combinations of Dart VM flags and null_assertions', () {
508
    expect(computeDartVmFlags(DebuggingOptions.enabled(BuildInfo.debug)), '');
509
    expect(computeDartVmFlags(DebuggingOptions.enabled(BuildInfo.debug, dartFlags: '--foo')), '--foo');
510
    expect(computeDartVmFlags(DebuggingOptions.enabled(BuildInfo.debug, nullAssertions: true)), '--null_assertions');
511 512
    expect(computeDartVmFlags(DebuggingOptions.enabled(BuildInfo.debug, dartFlags: '--foo', nullAssertions: true)), '--foo,--null_assertions');
  });
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535

  group('JSON encode DebuggingOptions', () {
    testWithoutContext('can preserve the original options', () {
      final DebuggingOptions original = DebuggingOptions.enabled(
        BuildInfo.debug,
        startPaused: true,
        disableServiceAuthCodes: true,
        enableDds: false,
        dartEntrypointArgs: <String>['a', 'b'],
        dartFlags: 'c',
        deviceVmServicePort: 1234,
      );
      final String jsonString = json.encode(original.toJson());
      final Map<String, dynamic> decoded = castStringKeyedMap(json.decode(jsonString))!;
      final DebuggingOptions deserialized = DebuggingOptions.fromJson(decoded, BuildInfo.debug);
      expect(deserialized.startPaused, original.startPaused);
      expect(deserialized.disableServiceAuthCodes, original.disableServiceAuthCodes);
      expect(deserialized.enableDds, original.enableDds);
      expect(deserialized.dartEntrypointArgs, original.dartEntrypointArgs);
      expect(deserialized.dartFlags, original.dartFlags);
      expect(deserialized.deviceVmServicePort, original.deviceVmServicePort);
    });
  });
536
}
537

538
class TestDeviceManager extends DeviceManager {
539 540
  TestDeviceManager(
    List<Device> allDevices, {
541 542 543 544 545 546 547
    List<DeviceDiscovery>? deviceDiscoveryOverrides,
    required Logger logger,
    required Terminal terminal,
    String? wellKnownId,
  }) : _fakeDeviceDiscoverer = FakePollingDeviceDiscovery(),
       _deviceDiscoverers = <DeviceDiscovery>[],
       super(logger: logger, terminal: terminal, userMessages: UserMessages()) {
548 549 550
    if (wellKnownId != null) {
      _fakeDeviceDiscoverer.wellKnownIds.add(wellKnownId);
    }
551 552 553 554
    _deviceDiscoverers.add(_fakeDeviceDiscoverer);
    if (deviceDiscoveryOverrides != null) {
      _deviceDiscoverers.addAll(deviceDiscoveryOverrides);
    }
555 556 557
    resetDevices(allDevices);
  }
  @override
558
  List<DeviceDiscovery> get deviceDiscoverers => _deviceDiscoverers;
559 560
  final List<DeviceDiscovery> _deviceDiscoverers;
  final FakePollingDeviceDiscovery _fakeDeviceDiscoverer;
561

562
  void resetDevices(List<Device> allDevices) {
563
    _fakeDeviceDiscoverer.setDevices(allDevices);
564
  }
565

566
  bool? isAlwaysSupportedForProjectOverride;
567 568

  @override
569
  bool isDeviceSupportedForProject(Device device, FlutterProject? flutterProject) {
570 571
    if (isAlwaysSupportedForProjectOverride != null) {
      return isAlwaysSupportedForProjectOverride!;
572 573 574
    }
    return super.isDeviceSupportedForProject(device, flutterProject);
  }
575 576
}

577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
class MockDeviceDiscovery extends Fake implements DeviceDiscovery {
  int devicesCalled = 0;
  int discoverDevicesCalled = 0;

  @override
  bool supportsPlatform = true;

  List<Device> deviceValues = <Device>[];

  @override
  Future<List<Device>> get devices async {
    devicesCalled += 1;
    return deviceValues;
  }

  @override
593
  Future<List<Device>> discoverDevices({Duration? timeout}) async {
594 595 596
    discoverDevicesCalled += 1;
    return deviceValues;
  }
597 598 599

  @override
  List<String> get wellKnownIds => <String>[];
600 601
}

602
class FakeFlutterProject extends Fake implements FlutterProject { }
603 604 605 606 607 608 609

class LongPollingDeviceDiscovery extends PollingDeviceDiscovery {
  LongPollingDeviceDiscovery() : super('forever');

  final Completer<List<Device>> _completer = Completer<List<Device>>();

  @override
610
  Future<List<Device>> pollingGetDevices({ Duration? timeout }) async {
611 612 613 614 615
    return _completer.future;
  }

  @override
  Future<void> stopPolling() async {
616
    _completer.complete(<Device>[]);
617 618 619 620
  }

  @override
  Future<void> dispose() async {
621
    _completer.complete(<Device>[]);
622 623 624 625 626 627 628
  }

  @override
  bool get supportsPlatform => true;

  @override
  bool get canListAnything => true;
629 630 631

  @override
  final List<String> wellKnownIds = <String>[];
632 633 634 635 636 637
}

class ThrowingPollingDeviceDiscovery extends PollingDeviceDiscovery {
  ThrowingPollingDeviceDiscovery() : super('throw');

  @override
638
  Future<List<Device>> pollingGetDevices({ Duration? timeout }) async {
639 640 641 642 643 644 645 646
    throw const ProcessException('fake-discovery', <String>[]);
  }

  @override
  bool get supportsPlatform => true;

  @override
  bool get canListAnything => true;
647 648 649

  @override
  List<String> get wellKnownIds => <String>[];
650
}
651 652 653 654 655 656 657 658 659 660 661 662 663

class FakeTerminal extends Fake implements Terminal {
  @override
  bool stdinHasTerminal = true;

  @override
  bool usesTerminalUi = true;

  void setPrompt(List<String> characters, String result) {
    _nextPrompt = characters;
    _nextResult = result;
  }

664 665
  List<String>? _nextPrompt;
  late String _nextResult;
666 667 668 669

  @override
  Future<String> promptForCharInput(
    List<String> acceptedCharacters, {
670 671 672
    Logger? logger,
    String? prompt,
    int? defaultChoiceIndex,
673 674 675 676 677 678
    bool displayAcceptedCharacters = true,
  }) async {
    expect(acceptedCharacters, _nextPrompt);
    return _nextResult;
  }
}