device_test.dart 9.17 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:flutter_tools/src/build_info.dart';
8
import 'package:flutter_tools/src/device.dart';
9
import 'package:flutter_tools/src/base/io.dart';
10
import 'package:flutter_tools/src/project.dart';
11
import 'package:mockito/mockito.dart';
12
import 'package:quiver/testing/async.dart';
13

14 15
import '../src/common.dart';
import '../src/context.dart';
16
import '../src/fake_devices.dart';
17
import '../src/mocks.dart';
18

19
void main() {
20
  group('DeviceManager', () {
21
    testUsingContext('getDevices', () async {
22 23 24 25 26 27
      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];
      final DeviceManager deviceManager = TestDeviceManager(devices);
      expect(await deviceManager.getDevices(), devices);
28
    });
29 30

    testUsingContext('getDeviceById', () async {
31 32 33
      final FakeDevice device1 = FakeDevice('Nexus 5', '0553790d0a4e726f');
      final FakeDevice device2 = FakeDevice('Nexus 5X', '01abfc49119c410e');
      final FakeDevice device3 = FakeDevice('iPod touch', '82564b38861a9a5');
34
      final List<Device> devices = <Device>[device1, device2, device3];
35
      final DeviceManager deviceManager = TestDeviceManager(devices);
36

37
      Future<void> expectDevice(String id, List<Device> expected) async {
38
        expect(await deviceManager.getDevicesById(id), expected);
39
      }
40 41 42 43 44 45
      await expectDevice('01abfc49119c410e', <Device>[device2]);
      await expectDevice('Nexus 5X', <Device>[device2]);
      await expectDevice('0553790d0a4e726f', <Device>[device1]);
      await expectDevice('Nexus 5', <Device>[device1]);
      await expectDevice('0553790', <Device>[device1]);
      await expectDevice('Nexus', <Device>[device1, device2]);
46
    });
47 48

    testUsingContext('getAllConnectedDevices caches', () async {
49
      final FakeDevice device1 = FakeDevice('Nexus 5', '0553790d0a4e726f');
50 51 52
      final TestDeviceManager deviceManager = TestDeviceManager(<Device>[device1]);
      expect(await deviceManager.getAllConnectedDevices(), <Device>[device1]);

53
      final FakeDevice device2 = FakeDevice('Nexus 5X', '01abfc49119c410e');
54 55 56 57 58
      deviceManager.resetDevices(<Device>[device2]);
      expect(await deviceManager.getAllConnectedDevices(), <Device>[device1]);
    });

    testUsingContext('refreshAllConnectedDevices does not cache', () async {
59
      final FakeDevice device1 = FakeDevice('Nexus 5', '0553790d0a4e726f');
60 61 62
      final TestDeviceManager deviceManager = TestDeviceManager(<Device>[device1]);
      expect(await deviceManager.refreshAllConnectedDevices(), <Device>[device1]);

63
      final FakeDevice device2 = FakeDevice('Nexus 5X', '01abfc49119c410e');
64 65 66
      deviceManager.resetDevices(<Device>[device2]);
      expect(await deviceManager.refreshAllConnectedDevices(), <Device>[device2]);
    });
67
  });
68

69 70
  group('PollingDeviceDiscovery', () {
    testUsingContext('startPolling', () async {
71
      FakeAsync().run((FakeAsync time) async {
72
        final FakePollingDeviceDiscovery pollingDeviceDiscovery = FakePollingDeviceDiscovery();
73
        await pollingDeviceDiscovery.startPolling();
74 75 76 77 78 79 80 81 82 83
        time.elapse(const Duration(milliseconds: 4001));
        time.flushMicrotasks();
        // First check should use the default polling timeout
        // to quickly populate the list.
        expect(pollingDeviceDiscovery.lastPollingTimeout, isNull);

        time.elapse(const Duration(milliseconds: 4001));
        time.flushMicrotasks();
        // Subsequent polling should be much longer.
        expect(pollingDeviceDiscovery.lastPollingTimeout, const Duration(seconds: 30));
84
        await pollingDeviceDiscovery.stopPolling();
85 86 87 88
      });
    });
  });

89
  group('Filter devices', () {
90 91 92 93 94 95
    FakeDevice ephemeral;
    FakeDevice nonEphemeralOne;
    FakeDevice nonEphemeralTwo;
    FakeDevice unsupported;
    FakeDevice webDevice;
    FakeDevice fuchsiaDevice;
96 97

    setUp(() {
98 99 100 101 102
      ephemeral = FakeDevice('ephemeral', 'ephemeral', true);
      nonEphemeralOne = FakeDevice('nonEphemeralOne', 'nonEphemeralOne', false);
      nonEphemeralTwo = FakeDevice('nonEphemeralTwo', 'nonEphemeralTwo', false);
      unsupported = FakeDevice('unsupported', 'unsupported', true, false);
      webDevice = FakeDevice('webby', 'webby')
103
        ..targetPlatform = Future<TargetPlatform>.value(TargetPlatform.web_javascript);
104
      fuchsiaDevice = FakeDevice('fuchsiay', 'fuchsiay')
105
        ..targetPlatform = Future<TargetPlatform>.value(TargetPlatform.fuchsia_x64);
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    });

    testUsingContext('chooses ephemeral device', () async {
      final List<Device> devices = <Device>[
        ephemeral,
        nonEphemeralOne,
        nonEphemeralTwo,
        unsupported,
      ];

      final DeviceManager deviceManager = TestDeviceManager(devices);
      final List<Device> filtered = await deviceManager.findTargetDevices(FlutterProject.current());

      expect(filtered.single, ephemeral);
    });

    testUsingContext('does not remove all non-ephemeral', () async {
      final List<Device> devices = <Device>[
        nonEphemeralOne,
        nonEphemeralTwo,
      ];

      final DeviceManager deviceManager = TestDeviceManager(devices);
      final List<Device> filtered = await deviceManager.findTargetDevices(FlutterProject.current());

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

137 138 139 140 141 142 143 144 145 146 147
    testUsingContext('Removes a single unsupported device', () async {
      final List<Device> devices = <Device>[
        unsupported,
      ];

      final DeviceManager deviceManager = TestDeviceManager(devices);
      final List<Device> filtered = await deviceManager.findTargetDevices(FlutterProject.current());

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

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 175 176
    testUsingContext('Removes web and fuchsia from --all', () async {
      final List<Device> devices = <Device>[
        webDevice,
        fuchsiaDevice,
      ];
      final DeviceManager deviceManager = TestDeviceManager(devices);
      deviceManager.specifiedDeviceId = 'all';

      final List<Device> filtered = await deviceManager.findTargetDevices(FlutterProject.current());

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

    testUsingContext('Removes unsupported devices from --all', () async {
      final List<Device> devices = <Device>[
        nonEphemeralOne,
        nonEphemeralTwo,
        unsupported,
      ];
      final DeviceManager deviceManager = TestDeviceManager(devices);
      deviceManager.specifiedDeviceId = 'all';

      final List<Device> filtered = await deviceManager.findTargetDevices(FlutterProject.current());

      expect(filtered, <Device>[
        nonEphemeralOne,
        nonEphemeralTwo,
      ]);
    });
177 178 179 180 181 182 183 184 185 186 187 188 189 190

    testUsingContext('uses DeviceManager.isDeviceSupportedForProject instead of device.isSupportedForProject', () async {
      final List<Device> devices = <Device>[
        unsupported,
      ];
      final TestDeviceManager deviceManager = TestDeviceManager(devices);
      deviceManager.isAlwaysSupportedOverride = true;

      final List<Device> filtered = await deviceManager.findTargetDevices(FlutterProject.current());

      expect(filtered, <Device>[
        unsupported,
      ]);
    });
191
  });
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  group('ForwardedPort', () {
    group('dispose()', () {
      testUsingContext('does not throw exception if no process is present', () {
        final ForwardedPort forwardedPort = ForwardedPort(123, 456);
        expect(forwardedPort.context, isNull);
        forwardedPort.dispose();
      });

      testUsingContext('kills process if process was available', () {
        final MockProcess mockProcess = MockProcess();
        final ForwardedPort forwardedPort = ForwardedPort.withContext(123, 456, mockProcess);
        forwardedPort.dispose();
        expect(forwardedPort.context, isNotNull);
        verify(mockProcess.kill());
      });
    });
  });
209 210 211 212 213 214 215 216 217 218 219

  group('JSON encode devices', () {
    testUsingContext('Consistency of JSON representation', () async {
      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)
      );
    });
  });
220
}
221

222
class TestDeviceManager extends DeviceManager {
223
  TestDeviceManager(List<Device> allDevices) {
224
    _deviceDiscoverer = FakePollingDeviceDiscovery();
225 226 227 228
    resetDevices(allDevices);
  }
  @override
  List<DeviceDiscovery> get deviceDiscoverers => <DeviceDiscovery>[_deviceDiscoverer];
229
  FakePollingDeviceDiscovery _deviceDiscoverer;
230

231 232 233
  void resetDevices(List<Device> allDevices) {
    _deviceDiscoverer.setDevices(allDevices);
  }
234

235
  bool isAlwaysSupportedOverride;
236 237 238 239 240 241 242 243

  @override
  bool isDeviceSupportedForProject(Device device, FlutterProject flutterProject) {
    if (isAlwaysSupportedOverride != null) {
      return isAlwaysSupportedOverride;
    }
    return super.isDeviceSupportedForProject(device, flutterProject);
  }
244 245
}

246
class MockProcess extends Mock implements Process {}