devices_test.dart 37.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:convert';

7
import 'package:flutter_tools/src/android/android_sdk.dart';
8
import 'package:flutter_tools/src/artifacts.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/base/terminal.dart';
12
import 'package:flutter_tools/src/cache.dart';
13
import 'package:flutter_tools/src/commands/devices.dart';
14
import 'package:flutter_tools/src/device.dart';
15
import 'package:flutter_tools/src/globals.dart' as globals;
16
import 'package:test/fake.dart';
17

18
import '../../src/common.dart';
19
import '../../src/context.dart';
20
import '../../src/fake_devices.dart';
21
import '../../src/test_flutter_command_runner.dart';
22

23
void main() {
24
  group('devices', () {
25 26 27 28
    setUpAll(() {
      Cache.disableLocking();
    });

29
    late Cache cache;
30
    late Platform platform;
31

32 33
    group('ensure factory', () {
      late FakeBufferLogger fakeLogger;
34

35 36 37
      setUpAll(() {
        fakeLogger = FakeBufferLogger();
      });
38

39 40 41 42 43 44
      testWithoutContext('returns DevicesCommandOutputWithExtendedWirelessDeviceDiscovery on MacOS', () async {
        final Platform platform = FakePlatform(operatingSystem: 'macos');
        final DevicesCommandOutput devicesCommandOutput = DevicesCommandOutput(
          platform: platform,
          logger: fakeLogger,
        );
45

46 47
        expect(devicesCommandOutput is DevicesCommandOutputWithExtendedWirelessDeviceDiscovery, true);
      });
48

49 50 51 52 53
      testWithoutContext('returns default when not on MacOS', () async {
        final Platform platform = FakePlatform();
        final DevicesCommandOutput devicesCommandOutput = DevicesCommandOutput(
          platform: platform,
          logger: fakeLogger,
54
        );
55 56 57

        expect(devicesCommandOutput is DevicesCommandOutputWithExtendedWirelessDeviceDiscovery, false);
      });
58
    });
59

60
    group('when Platform is not MacOS', () {
61
      setUp(() {
62 63
        cache = Cache.test(processManager: FakeProcessManager.any());
        platform = FakePlatform();
64 65
      });

66 67 68
      testUsingContext('returns 0 when called', () async {
        final DevicesCommand command = DevicesCommand();
        await createTestCommandRunner(command).run(<String>['devices']);
69 70 71 72 73
      }, overrides: <Type, Generator>{
        Cache: () => cache,
        Artifacts: () => Artifacts.test(),
      });

74
      testUsingContext('no error when no connected devices', () async {
75
        final DevicesCommand command = DevicesCommand();
76
        await createTestCommandRunner(command).run(<String>['devices']);
77
        expect(
78 79
            testLogger.statusText,
            equals('''
80
No authorized devices detected.
81 82 83

Run "flutter emulators" to list and start any available device emulators.

84
If you expected a device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
85
'''),
86 87
        );
      }, overrides: <Type, Generator>{
88 89
        AndroidSdk: () => null,
        DeviceManager: () => NoDevicesManager(),
90 91 92 93 94 95
        ProcessManager: () => FakeProcessManager.any(),
        Cache: () => cache,
        Artifacts: () => Artifacts.test(),
        Platform: () => platform,
      });

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
      group('when includes both attached and wireless devices', () {
        List<FakeDeviceJsonData>? deviceList;
        setUp(() {
          deviceList = <FakeDeviceJsonData>[
            fakeDevices[0],
            fakeDevices[1],
            fakeDevices[2],
          ];
        });

        testUsingContext("get devices' platform types", () async {
          final List<String> platformTypes = Device.devicesPlatformTypes(
            await globals.deviceManager!.getAllDevices(),
          );
          expect(platformTypes, <String>['android', 'web']);
        }, overrides: <Type, Generator>{
          DeviceManager: () => _FakeDeviceManager(devices: deviceList),
          ProcessManager: () => FakeProcessManager.any(),
          Cache: () => cache,
          Artifacts: () => Artifacts.test(),
          Platform: () => platform,
        });

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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
        group('with --machine flag', () {
          testUsingContext('Outputs parsable JSON', () async {
            final DevicesCommand command = DevicesCommand();
            await createTestCommandRunner(command).run(<String>['devices', '--machine']);
            expect(
              json.decode(testLogger.statusText),
              <Map<String, Object>>[
                fakeDevices[0].json,
                fakeDevices[1].json,
                fakeDevices[2].json,
              ],
            );
          }, overrides: <Type, Generator>{
            DeviceManager: () => _FakeDeviceManager(devices: deviceList),
            ProcessManager: () => FakeProcessManager.any(),
            Cache: () => cache,
            Artifacts: () => Artifacts.test(),
            Platform: () => platform,
          });

          group('when deviceConnectionInterface', () {
            testUsingContext('filtered to attached', () async {
              final DevicesCommand command = DevicesCommand();
              await createTestCommandRunner(command).run(<String>['devices', '--machine', '--device-connection', 'attached']);
              expect(
                json.decode(testLogger.statusText),
                <Map<String, Object>>[
                  fakeDevices[0].json,
                  fakeDevices[1].json,
                ],
              );
            }, overrides: <Type, Generator>{
              DeviceManager: () => _FakeDeviceManager(devices: deviceList),
              ProcessManager: () => FakeProcessManager.any(),
              Cache: () => cache,
              Artifacts: () => Artifacts.test(),
              Platform: () => platform,
            });

            testUsingContext('filtered to wireless', () async {
            final DevicesCommand command = DevicesCommand();
              await createTestCommandRunner(command).run(<String>['devices', '--machine', '--device-connection', 'wireless']);
              expect(
                json.decode(testLogger.statusText),
                <Map<String, Object>>[
                  fakeDevices[2].json,
                ],
              );
            }, overrides: <Type, Generator>{
              DeviceManager: () => _FakeDeviceManager(devices: deviceList),
              ProcessManager: () => FakeProcessManager.any(),
              Cache: () => cache,
              Artifacts: () => Artifacts.test(),
              Platform: () => platform,
            });
          });
175 176 177 178 179 180
        });

        testUsingContext('available devices and diagnostics', () async {
          final DevicesCommand command = DevicesCommand();
          await createTestCommandRunner(command).run(<String>['devices']);
          expect(testLogger.statusText, '''
181 182 183
Found 2 connected devices:
  ephemeral (mobile) • ephemeral • android-arm    • Test SDK (1.2.3) (emulator)
  webby (mobile)     • webby     • web-javascript • Web SDK (1.2.4) (emulator)
184

185 186
Found 1 wirelessly connected device:
  wireless android (mobile) • wireless-android • android-arm • Test SDK (1.2.3) (emulator)
187

188
Cannot connect to device ABC
189

190
Run "flutter emulators" to list and start any available device emulators.
191

192
If you expected another device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
193
''');
194 195 196 197 198
        }, overrides: <Type, Generator>{
          DeviceManager: () => _FakeDeviceManager(devices: deviceList),
          ProcessManager: () => FakeProcessManager.any(),
          Platform: () => platform,
        });
199 200 201 202 203 204

        group('when deviceConnectionInterface', () {
          testUsingContext('filtered to attached', () async {
            final DevicesCommand command = DevicesCommand();
            await createTestCommandRunner(command).run(<String>['devices', '--device-connection', 'attached']);
            expect(testLogger.statusText, '''
205 206 207 208 209
Found 2 connected devices:
  ephemeral (mobile) • ephemeral • android-arm    • Test SDK (1.2.3) (emulator)
  webby (mobile)     • webby     • web-javascript • Web SDK (1.2.4) (emulator)

Cannot connect to device ABC
210

211
Run "flutter emulators" to list and start any available device emulators.
212

213
If you expected another device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
214 215 216 217 218 219 220 221 222 223 224
''');
          }, overrides: <Type, Generator>{
            DeviceManager: () => _FakeDeviceManager(devices: deviceList),
            ProcessManager: () => FakeProcessManager.any(),
            Platform: () => platform,
          });

          testUsingContext('filtered to wireless', () async {
            final DevicesCommand command = DevicesCommand();
            await createTestCommandRunner(command).run(<String>['devices', '--device-connection', 'wireless']);
            expect(testLogger.statusText, '''
225 226
Found 1 wirelessly connected device:
  wireless android (mobile) • wireless-android • android-arm • Test SDK (1.2.3) (emulator)
227

228 229 230
Cannot connect to device ABC

Run "flutter emulators" to list and start any available device emulators.
231

232
If you expected another device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
233 234 235 236 237 238 239
''');
          }, overrides: <Type, Generator>{
            DeviceManager: () => _FakeDeviceManager(devices: deviceList),
            ProcessManager: () => FakeProcessManager.any(),
            Platform: () => platform,
          });
        });
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
      });

      group('when includes only attached devices', () {
        List<FakeDeviceJsonData>? deviceList;
        setUp(() {
          deviceList = <FakeDeviceJsonData>[
            fakeDevices[0],
            fakeDevices[1],
          ];
        });

        testUsingContext('available devices and diagnostics', () async {
          final DevicesCommand command = DevicesCommand();
          await createTestCommandRunner(command).run(<String>['devices']);
          expect(testLogger.statusText, '''
255 256 257
Found 2 connected devices:
  ephemeral (mobile) • ephemeral • android-arm    • Test SDK (1.2.3) (emulator)
  webby (mobile)     • webby     • web-javascript • Web SDK (1.2.4) (emulator)
258

259
Cannot connect to device ABC
260

261 262 263
Run "flutter emulators" to list and start any available device emulators.

If you expected another device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
''');
        }, overrides: <Type, Generator>{
          DeviceManager: () => _FakeDeviceManager(devices: deviceList),
          ProcessManager: () => FakeProcessManager.any(),
          Platform: () => platform,
        });
      });

      group('when includes only wireless devices', () {
        List<FakeDeviceJsonData>? deviceList;
        setUp(() {
          deviceList = <FakeDeviceJsonData>[
            fakeDevices[2],
          ];
        });

        testUsingContext('available devices and diagnostics', () async {
          final DevicesCommand command = DevicesCommand();
          await createTestCommandRunner(command).run(<String>['devices']);
          expect(testLogger.statusText, '''
284 285
Found 1 wirelessly connected device:
  wireless android (mobile) • wireless-android • android-arm • Test SDK (1.2.3) (emulator)
286

287
Cannot connect to device ABC
288

289 290 291
Run "flutter emulators" to list and start any available device emulators.

If you expected another device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
292 293 294 295 296 297
''');
        }, overrides: <Type, Generator>{
          DeviceManager: () => _FakeDeviceManager(devices: deviceList),
          ProcessManager: () => FakeProcessManager.any(),
          Platform: () => platform,
        });
298
      });
299
    });
300

301
    group('when Platform is MacOS', () {
302
      setUp(() {
303 304 305 306 307 308 309 310 311 312 313
        cache = Cache.test(processManager: FakeProcessManager.any());
        platform = FakePlatform(operatingSystem: 'macos');
      });

      testUsingContext('returns 0 when called', () async {
        final DevicesCommand command = DevicesCommand();
        await createTestCommandRunner(command).run(<String>['devices']);
      }, overrides: <Type, Generator>{
        Cache: () => cache,
        Artifacts: () => Artifacts.test(),
        Platform: () => platform,
314 315
      });

316 317 318 319 320 321 322
      group('when no connected devices', () {
        testUsingContext('no error', () async {
          final DevicesCommand command = DevicesCommand();
          await createTestCommandRunner(command).run(<String>['devices']);
          expect(
            testLogger.statusText,
            equals('''
323 324
No devices found yet. Checking for wireless devices...

325
No authorized devices detected.
326 327 328

Run "flutter emulators" to list and start any available device emulators.

329
If you expected a device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
330
'''),
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
          );
        }, overrides: <Type, Generator>{
          AndroidSdk: () => null,
          DeviceManager: () => NoDevicesManager(),
          ProcessManager: () => FakeProcessManager.any(),
          Cache: () => cache,
          Artifacts: () => Artifacts.test(),
          Platform: () => platform,
        });

        group('when deviceConnectionInterface', () {
          testUsingContext('filtered to attached', () async {
            final DevicesCommand command = DevicesCommand();
            await createTestCommandRunner(command).run(<String>['devices', '--device-connection', 'attached']);
            expect(testLogger.statusText, '''
346
No authorized devices detected.
347 348 349

Run "flutter emulators" to list and start any available device emulators.

350
If you expected a device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
351 352 353 354 355 356 357 358 359 360 361 362 363
''');
          }, overrides: <Type, Generator>{
            DeviceManager: () => NoDevicesManager(),
            ProcessManager: () => FakeProcessManager.any(),
            Platform: () => platform,
          });

          testUsingContext('filtered to wireless', () async {
            final DevicesCommand command = DevicesCommand();
            await createTestCommandRunner(command).run(<String>['devices', '--device-connection', 'wireless']);
            expect(testLogger.statusText, '''
Checking for wireless devices...

364
No authorized devices detected.
365 366 367

Run "flutter emulators" to list and start any available device emulators.

368
If you expected a device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
369 370 371 372 373 374 375
''');
          }, overrides: <Type, Generator>{
            DeviceManager: () => NoDevicesManager(),
            ProcessManager: () => FakeProcessManager.any(),
            Platform: () => platform,
          });
        });
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
      });

      group('when includes both attached and wireless devices', () {
        List<FakeDeviceJsonData>? deviceList;
        setUp(() {
          deviceList = <FakeDeviceJsonData>[
            fakeDevices[0],
            fakeDevices[1],
            fakeDevices[2],
            fakeDevices[3],
          ];
        });

        testUsingContext("get devices' platform types", () async {
          final List<String> platformTypes = Device.devicesPlatformTypes(
            await globals.deviceManager!.getAllDevices(),
          );
          expect(platformTypes, <String>['android', 'ios', 'web']);
        }, overrides: <Type, Generator>{
          DeviceManager: () => _FakeDeviceManager(devices: deviceList),
          ProcessManager: () => FakeProcessManager.any(),
          Cache: () => cache,
          Artifacts: () => Artifacts.test(),
          Platform: () => platform,
        });

402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
        group('with --machine flag', () {
          testUsingContext('Outputs parsable JSON', () async {
            final DevicesCommand command = DevicesCommand();
            await createTestCommandRunner(command).run(<String>['devices', '--machine']);
            expect(
              json.decode(testLogger.statusText),
              <Map<String, Object>>[
                fakeDevices[0].json,
                fakeDevices[1].json,
                fakeDevices[2].json,
                fakeDevices[3].json,
              ],
            );
          }, overrides: <Type, Generator>{
            DeviceManager: () => _FakeDeviceManager(devices: deviceList),
            ProcessManager: () => FakeProcessManager.any(),
            Cache: () => cache,
            Artifacts: () => Artifacts.test(),
            Platform: () => platform,
          });

          group('when deviceConnectionInterface', () {
            testUsingContext('filtered to attached', () async {
              final DevicesCommand command = DevicesCommand();
              await createTestCommandRunner(command).run(<String>['devices', '--machine', '--device-connection', 'attached']);
              expect(
                json.decode(testLogger.statusText),
                <Map<String, Object>>[
                  fakeDevices[0].json,
                  fakeDevices[1].json,
                ],
              );
            }, overrides: <Type, Generator>{
              DeviceManager: () => _FakeDeviceManager(devices: deviceList),
              ProcessManager: () => FakeProcessManager.any(),
              Cache: () => cache,
              Artifacts: () => Artifacts.test(),
              Platform: () => platform,
            });

            testUsingContext('filtered to wireless', () async {
            final DevicesCommand command = DevicesCommand();
              await createTestCommandRunner(command).run(<String>['devices', '--machine', '--device-connection', 'wireless']);
              expect(
                json.decode(testLogger.statusText),
                <Map<String, Object>>[
                  fakeDevices[2].json,
                  fakeDevices[3].json,
                ],
              );
            }, overrides: <Type, Generator>{
              DeviceManager: () => _FakeDeviceManager(devices: deviceList),
              ProcessManager: () => FakeProcessManager.any(),
              Cache: () => cache,
              Artifacts: () => Artifacts.test(),
              Platform: () => platform,
            });
          });
460 461 462 463 464 465
        });

        testUsingContext('available devices and diagnostics', () async {
          final DevicesCommand command = DevicesCommand();
          await createTestCommandRunner(command).run(<String>['devices']);
          expect(testLogger.statusText, '''
466 467 468
Found 2 connected devices:
  ephemeral (mobile) • ephemeral • android-arm    • Test SDK (1.2.3) (emulator)
  webby (mobile)     • webby     • web-javascript • Web SDK (1.2.4) (emulator)
469

470 471
Checking for wireless devices...

472 473 474
Found 2 wirelessly connected devices:
  wireless android (mobile) • wireless-android • android-arm • Test SDK (1.2.3) (emulator)
  wireless ios (mobile)     • wireless-ios     • ios         • iOS 16 (simulator)
475

476
Cannot connect to device ABC
477

478 479 480
Run "flutter emulators" to list and start any available device emulators.

If you expected another device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
481
''');
482 483 484 485 486 487 488 489 490 491 492 493 494 495
        }, overrides: <Type, Generator>{
          DeviceManager: () => _FakeDeviceManager(devices: deviceList),
          ProcessManager: () => FakeProcessManager.any(),
          Platform: () => platform,
        });

        group('with ansi terminal', () {
          late FakeTerminal terminal;
          late FakeBufferLogger fakeLogger;

          setUp(() {
            terminal = FakeTerminal(supportsColor: true);
            fakeLogger = FakeBufferLogger(terminal: terminal);
            fakeLogger.originalStatusText = '''
496 497 498
Found 2 connected devices:
  ephemeral (mobile) • ephemeral • android-arm    • Test SDK (1.2.3) (emulator)
  webby (mobile)     • webby     • web-javascript • Web SDK (1.2.4) (emulator)
499 500 501 502 503 504 505 506 507 508

Checking for wireless devices...
''';
          });

          testUsingContext('available devices and diagnostics', () async {
            final DevicesCommand command = DevicesCommand();
            await createTestCommandRunner(command).run(<String>['devices']);

            expect(fakeLogger.statusText, '''
509 510 511
Found 2 connected devices:
  ephemeral (mobile) • ephemeral • android-arm    • Test SDK (1.2.3) (emulator)
  webby (mobile)     • webby     • web-javascript • Web SDK (1.2.4) (emulator)
512

513 514 515
Found 2 wirelessly connected devices:
  wireless android (mobile) • wireless-android • android-arm • Test SDK (1.2.3) (emulator)
  wireless ios (mobile)     • wireless-ios     • ios         • iOS 16 (simulator)
516

517
Cannot connect to device ABC
518

519
Run "flutter emulators" to list and start any available device emulators.
520

521
If you expected another device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
''');
          }, overrides: <Type, Generator>{
            DeviceManager: () =>
                _FakeDeviceManager(devices: deviceList, logger: fakeLogger),
            ProcessManager: () => FakeProcessManager.any(),
            Platform: () => platform,
            AnsiTerminal: () => terminal,
            Logger: () => fakeLogger,
          });
        });

        group('with verbose logging', () {
          late FakeBufferLogger fakeLogger;

          setUp(() {
            fakeLogger = FakeBufferLogger(verbose: true);
          });

          testUsingContext('available devices and diagnostics', () async {
            final DevicesCommand command = DevicesCommand();
            await createTestCommandRunner(command).run(<String>['devices']);

            expect(fakeLogger.statusText, '''
545 546 547
Found 2 connected devices:
  ephemeral (mobile) • ephemeral • android-arm    • Test SDK (1.2.3) (emulator)
  webby (mobile)     • webby     • web-javascript • Web SDK (1.2.4) (emulator)
548 549 550

Checking for wireless devices...

551 552 553
Found 2 connected devices:
  ephemeral (mobile) • ephemeral • android-arm    • Test SDK (1.2.3) (emulator)
  webby (mobile)     • webby     • web-javascript • Web SDK (1.2.4) (emulator)
554

555 556 557
Found 2 wirelessly connected devices:
  wireless android (mobile) • wireless-android • android-arm • Test SDK (1.2.3) (emulator)
  wireless ios (mobile)     • wireless-ios     • ios         • iOS 16 (simulator)
558

559
Cannot connect to device ABC
560

561
Run "flutter emulators" to list and start any available device emulators.
562

563
If you expected another device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
564 565 566 567 568 569 570 571 572 573
''');
          }, overrides: <Type, Generator>{
            DeviceManager: () => _FakeDeviceManager(
              devices: deviceList,
              logger: fakeLogger,
            ),
            ProcessManager: () => FakeProcessManager.any(),
            Platform: () => platform,
            Logger: () => fakeLogger,
          });
574 575 576 577 578 579 580

          testUsingContext('when deviceConnectionInterface filtered to wireless', () async {
            final DevicesCommand command = DevicesCommand();
            await createTestCommandRunner(command).run(<String>['devices', '--device-connection', 'wireless']);
            expect(testLogger.statusText, '''
Checking for wireless devices...

581 582 583 584 585
Found 2 wirelessly connected devices:
  wireless android (mobile) • wireless-android • android-arm • Test SDK (1.2.3) (emulator)
  wireless ios (mobile)     • wireless-ios     • ios         • iOS 16 (simulator)

Cannot connect to device ABC
586

587
Run "flutter emulators" to list and start any available device emulators.
588

589
If you expected another device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
590 591 592 593 594 595
''');
          }, overrides: <Type, Generator>{
            DeviceManager: () => _FakeDeviceManager(devices: deviceList),
            ProcessManager: () => FakeProcessManager.any(),
            Platform: () => platform,
          });
596
        });
597 598
      });

599 600 601 602 603 604 605 606 607 608 609 610 611
      group('when includes only attached devices', () {
        List<FakeDeviceJsonData>? deviceList;
        setUp(() {
          deviceList = <FakeDeviceJsonData>[
            fakeDevices[0],
            fakeDevices[1],
          ];
        });

        testUsingContext('available devices and diagnostics', () async {
          final DevicesCommand command = DevicesCommand();
          await createTestCommandRunner(command).run(<String>['devices']);
          expect(testLogger.statusText, '''
612 613 614
Found 2 connected devices:
  ephemeral (mobile) • ephemeral • android-arm    • Test SDK (1.2.3) (emulator)
  webby (mobile)     • webby     • web-javascript • Web SDK (1.2.4) (emulator)
615 616 617 618 619

Checking for wireless devices...

No wireless devices were found.

620 621 622 623 624
Cannot connect to device ABC

Run "flutter emulators" to list and start any available device emulators.

If you expected another device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
''');
        }, overrides: <Type, Generator>{
          DeviceManager: () => _FakeDeviceManager(devices: deviceList),
          ProcessManager: () => FakeProcessManager.any(),
          Platform: () => platform,
        });

        group('with ansi terminal', () {
          late FakeTerminal terminal;
          late FakeBufferLogger fakeLogger;

          setUp(() {
            terminal = FakeTerminal(supportsColor: true);
            fakeLogger = FakeBufferLogger(terminal: terminal);
            fakeLogger.originalStatusText = '''
640 641 642
Found 2 connected devices:
  ephemeral (mobile) • ephemeral • android-arm    • Test SDK (1.2.3) (emulator)
  webby (mobile)     • webby     • web-javascript • Web SDK (1.2.4) (emulator)
643 644 645 646 647 648 649 650 651 652

Checking for wireless devices...
''';
          });

          testUsingContext('available devices and diagnostics', () async {
            final DevicesCommand command = DevicesCommand();
            await createTestCommandRunner(command).run(<String>['devices']);

            expect(fakeLogger.statusText, '''
653 654 655
Found 2 connected devices:
  ephemeral (mobile) • ephemeral • android-arm    • Test SDK (1.2.3) (emulator)
  webby (mobile)     • webby     • web-javascript • Web SDK (1.2.4) (emulator)
656 657 658

No wireless devices were found.

659 660 661 662 663
Cannot connect to device ABC

Run "flutter emulators" to list and start any available device emulators.

If you expected another device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
''');
          }, overrides: <Type, Generator>{
            DeviceManager: () => _FakeDeviceManager(
              devices: deviceList,
              logger: fakeLogger,
            ),
            ProcessManager: () => FakeProcessManager.any(),
            Platform: () => platform,
            AnsiTerminal: () => terminal,
            Logger: () => fakeLogger,
          });
        });

        group('with verbose logging', () {
          late FakeBufferLogger fakeLogger;

          setUp(() {
            fakeLogger = FakeBufferLogger(verbose: true);
          });

          testUsingContext('available devices and diagnostics', () async {
            final DevicesCommand command = DevicesCommand();
            await createTestCommandRunner(command).run(<String>['devices']);

            expect(fakeLogger.statusText, '''
689 690 691
Found 2 connected devices:
  ephemeral (mobile) • ephemeral • android-arm    • Test SDK (1.2.3) (emulator)
  webby (mobile)     • webby     • web-javascript • Web SDK (1.2.4) (emulator)
692 693 694

Checking for wireless devices...

695 696 697
Found 2 connected devices:
  ephemeral (mobile) • ephemeral • android-arm    • Test SDK (1.2.3) (emulator)
  webby (mobile)     • webby     • web-javascript • Web SDK (1.2.4) (emulator)
698 699 700

No wireless devices were found.

701 702 703 704 705
Cannot connect to device ABC

Run "flutter emulators" to list and start any available device emulators.

If you expected another device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
706 707 708 709 710 711 712 713 714 715 716
''');
          }, overrides: <Type, Generator>{
            DeviceManager: () => _FakeDeviceManager(
              devices: deviceList,
              logger: fakeLogger,
            ),
            ProcessManager: () => FakeProcessManager.any(),
            Platform: () => platform,
            Logger: () => fakeLogger,
          });
        });
717 718
      });

719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
      group('when includes only wireless devices', () {
        List<FakeDeviceJsonData>? deviceList;
        setUp(() {
          deviceList = <FakeDeviceJsonData>[
            fakeDevices[2],
            fakeDevices[3],
          ];
        });

        testUsingContext('available devices and diagnostics', () async {
          final DevicesCommand command = DevicesCommand();
          await createTestCommandRunner(command).run(<String>['devices']);
          expect(testLogger.statusText, '''
No devices found yet. Checking for wireless devices...

734 735 736
Found 2 wirelessly connected devices:
  wireless android (mobile) • wireless-android • android-arm • Test SDK (1.2.3) (emulator)
  wireless ios (mobile)     • wireless-ios     • ios         • iOS 16 (simulator)
737

738 739 740
Cannot connect to device ABC

Run "flutter emulators" to list and start any available device emulators.
741

742
If you expected another device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
743
''');
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
        }, overrides: <Type, Generator>{
          DeviceManager: () => _FakeDeviceManager(devices: deviceList),
          ProcessManager: () => FakeProcessManager.any(),
          Platform: () => platform,
        });

        group('with ansi terminal', () {
          late FakeTerminal terminal;
          late FakeBufferLogger fakeLogger;

          setUp(() {
            terminal = FakeTerminal(supportsColor: true);
            fakeLogger = FakeBufferLogger(terminal: terminal);
            fakeLogger.originalStatusText = '''
No devices found yet. Checking for wireless devices...
''';
          });

          testUsingContext('available devices and diagnostics', () async {
            final DevicesCommand command = DevicesCommand();
            await createTestCommandRunner(command).run(<String>['devices']);

            expect(fakeLogger.statusText, '''
767 768 769
Found 2 wirelessly connected devices:
  wireless android (mobile) • wireless-android • android-arm • Test SDK (1.2.3) (emulator)
  wireless ios (mobile)     • wireless-ios     • ios         • iOS 16 (simulator)
770

771
Cannot connect to device ABC
772

773 774 775
Run "flutter emulators" to list and start any available device emulators.

If you expected another device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
''');
          }, overrides: <Type, Generator>{
            DeviceManager: () => _FakeDeviceManager(
              devices: deviceList,
              logger: fakeLogger,
            ),
            ProcessManager: () => FakeProcessManager.any(),
            Platform: () => platform,
            AnsiTerminal: () => terminal,
            Logger: () => fakeLogger,
          });
        });

        group('with verbose logging', () {
          late FakeBufferLogger fakeLogger;

          setUp(() {
            fakeLogger = FakeBufferLogger(verbose: true);
          });

          testUsingContext('available devices and diagnostics', () async {
            final DevicesCommand command = DevicesCommand();
            await createTestCommandRunner(command).run(<String>['devices']);

            expect(fakeLogger.statusText, '''
No devices found yet. Checking for wireless devices...

803 804 805
Found 2 wirelessly connected devices:
  wireless android (mobile) • wireless-android • android-arm • Test SDK (1.2.3) (emulator)
  wireless ios (mobile)     • wireless-ios     • ios         • iOS 16 (simulator)
806

807 808 809
Cannot connect to device ABC

Run "flutter emulators" to list and start any available device emulators.
810

811
If you expected another device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
812 813 814 815 816 817 818 819 820 821 822
''');
          }, overrides: <Type, Generator>{
            DeviceManager: () => _FakeDeviceManager(
              devices: deviceList,
              logger: fakeLogger,
            ),
            ProcessManager: () => FakeProcessManager.any(),
            Platform: () => platform,
            Logger: () => fakeLogger,
          });
        });
823
      });
824
    });
825 826
  });
}
827

828
class _FakeDeviceManager extends DeviceManager {
829 830
  _FakeDeviceManager({
    List<FakeDeviceJsonData>? devices,
831
    FakeBufferLogger? logger,
832
  })  : fakeDevices = devices ?? <FakeDeviceJsonData>[],
833
        super(logger: logger ?? testLogger);
834 835

  List<FakeDeviceJsonData> fakeDevices = <FakeDeviceJsonData>[];
836 837

  @override
838 839 840 841 842 843 844 845 846 847
  Future<List<Device>> getAllDevices({DeviceDiscoveryFilter? filter}) async {
    final List<Device> devices = <Device>[];
    for (final FakeDeviceJsonData deviceJson in fakeDevices) {
      if (filter?.deviceConnectionInterface == null ||
          deviceJson.dev.connectionInterface == filter?.deviceConnectionInterface) {
        devices.add(deviceJson.dev);
      }
    }
    return devices;
  }
848 849

  @override
850 851 852 853
  Future<List<Device>> refreshAllDevices({
    Duration? timeout,
    DeviceDiscoveryFilter? filter,
  }) => getAllDevices(filter: filter);
854

855 856 857 858 859 860
  @override
  Future<List<Device>> refreshExtendedWirelessDeviceDiscoverers({
    Duration? timeout,
    DeviceDiscoveryFilter? filter,
  }) => getAllDevices(filter: filter);

861 862 863 864
  @override
  Future<List<String>> getDeviceDiagnostics() => Future<List<String>>.value(
    <String>['Cannot connect to device ABC']
  );
865 866 867

  @override
  List<DeviceDiscovery> get deviceDiscoverers => <DeviceDiscovery>[];
868
}
869 870

class NoDevicesManager extends DeviceManager {
871
  NoDevicesManager() : super(logger: testLogger);
872

873
  @override
874 875 876 877 878 879 880
  List<DeviceDiscovery> get deviceDiscoverers => <DeviceDiscovery>[];
}

class FakeTerminal extends Fake implements AnsiTerminal {
  FakeTerminal({
    this.supportsColor = false,
  });
881 882

  @override
883
  final bool supportsColor;
884

885 886 887
  @override
  bool get isCliAnimationEnabled => supportsColor;

888
  @override
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
  bool singleCharMode = false;

  @override
  String clearLines(int numberOfLines) {
    return 'CLEAR_LINES_$numberOfLines';
  }
}

class FakeBufferLogger extends BufferLogger {
  FakeBufferLogger({
    super.terminal,
    super.outputPreferences,
    super.verbose,
  }) : super.test();

  String originalStatusText = '';

  @override
  void printStatus(
    String message, {
    bool? emphasis,
    TerminalColor? color,
    bool? newline,
    int? indent,
    int? hangingIndent,
    bool? wrap,
  }) {
    if (message.startsWith('CLEAR_LINES_')) {
      expect(statusText, equals(originalStatusText));
      final int numberOfLinesToRemove =
          int.parse(message.split('CLEAR_LINES_')[1]) - 1;
      final List<String> lines = LineSplitter.split(statusText).toList();
      // Clear string buffer and re-add lines not removed
      clear();
      for (int lineNumber = 0; lineNumber < lines.length - numberOfLinesToRemove; lineNumber++) {
        super.printStatus(lines[lineNumber]);
      }
    } else {
      super.printStatus(
        message,
        emphasis: emphasis,
        color: color,
        newline: newline,
        indent: indent,
        hangingIndent: hangingIndent,
        wrap: wrap,
      );
    }
  }
938
}