fake_devices.dart 7.61 KB
Newer Older
1 2 3 4
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:async';

7
import 'package:flutter_tools/src/application_package.dart';
8 9 10 11 12 13 14 15
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/project.dart';

/// A list of fake devices to test JSON serialization
/// (`Device.toJson()` and `--machine` flag for `devices` command)
List<FakeDeviceJsonData> fakeDevices = <FakeDeviceJsonData>[
  FakeDeviceJsonData(
16
    FakeDevice('ephemeral', 'ephemeral', type: PlatformType.android),
17 18 19 20 21 22 23 24 25 26 27 28 29 30
    <String, Object>{
      'name': 'ephemeral',
      'id': 'ephemeral',
      'isSupported': true,
      'targetPlatform': 'android-arm',
      'emulator': true,
      'sdk': 'Test SDK (1.2.3)',
      'capabilities': <String, Object>{
        'hotReload': true,
        'hotRestart': true,
        'screenshot': false,
        'fastStart': false,
        'flutterExit': true,
        'hardwareRendering': true,
31 32
        'startPaused': true,
      },
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    }
  ),
  FakeDeviceJsonData(
    FakeDevice('webby', 'webby')
      ..targetPlatform = Future<TargetPlatform>.value(TargetPlatform.web_javascript)
      ..sdkNameAndVersion = Future<String>.value('Web SDK (1.2.4)'),
    <String,Object>{
      'name': 'webby',
      'id': 'webby',
      'isSupported': true,
      'targetPlatform': 'web-javascript',
      'emulator': true,
      'sdk': 'Web SDK (1.2.4)',
      'capabilities': <String, Object>{
        'hotReload': true,
        'hotRestart': true,
        'screenshot': false,
        'fastStart': false,
        'flutterExit': true,
52
        'hardwareRendering': true,
53 54 55
        'startPaused': true,
      },
    },
56
  ),
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  FakeDeviceJsonData(
    FakeDevice(
      'wireless android',
      'wireless-android',
      type: PlatformType.android,
      connectionInterface: DeviceConnectionInterface.wireless,
    ),
    <String, Object>{
      'name': 'wireless android',
      'id': 'wireless-android',
      'isSupported': true,
      'targetPlatform': 'android-arm',
      'emulator': true,
      'sdk': 'Test SDK (1.2.3)',
      'capabilities': <String, Object>{
        'hotReload': true,
        'hotRestart': true,
        'screenshot': false,
        'fastStart': false,
        'flutterExit': true,
        'hardwareRendering': true,
        'startPaused': true,
      },
    }
  ),
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  FakeDeviceJsonData(
    FakeDevice(
      'wireless ios',
      'wireless-ios',
      type:PlatformType.ios,
      connectionInterface: DeviceConnectionInterface.wireless,
    )
      ..targetPlatform = Future<TargetPlatform>.value(TargetPlatform.ios)
      ..sdkNameAndVersion = Future<String>.value('iOS 16'),
    <String,Object>{
      'name': 'wireless ios',
      'id': 'wireless-ios',
      'isSupported': true,
      'targetPlatform': 'ios',
      'emulator': true,
      'sdk': 'iOS 16',
      'capabilities': <String, Object>{
        'hotReload': true,
        'hotRestart': true,
        'screenshot': false,
        'fastStart': false,
        'flutterExit': true,
        'hardwareRendering': true,
        'startPaused': true,
      },
    },
  ),
109 110
];

111
/// Fake device to test `devices` command.
112
class FakeDevice extends Device {
113 114 115
  FakeDevice(this.name, String id, {
    bool ephemeral = true,
    bool isSupported = true,
116
    bool isSupportedForProject = true,
117 118
    this.isConnected = true,
    this.connectionInterface = DeviceConnectionInterface.attached,
119
    PlatformType type = PlatformType.web,
120
    LaunchResult? launchResult,
121
  }) : _isSupported = isSupported,
122
      _isSupportedForProject = isSupportedForProject,
123 124 125 126 127 128 129
      _launchResult = launchResult ?? LaunchResult.succeeded(),
      super(
        id,
        platformType: type,
        category: Category.mobile,
        ephemeral: ephemeral,
      );
130 131

  final bool _isSupported;
132
  final bool _isSupportedForProject;
133
  final LaunchResult _launchResult;
134 135 136 137

  @override
  final String name;

138
  @override
139
  Future<LaunchResult> startApp(ApplicationPackage? package, {
140 141 142 143
    String? mainPath,
    String? route,
    DebuggingOptions? debuggingOptions,
    Map<String, dynamic>? platformArgs,
144 145
    bool prebuiltApplication = false,
    bool ipv6 = false,
146
    String? userIdentifier,
147 148 149
  }) async => _launchResult;

  @override
150
  Future<bool> stopApp(ApplicationPackage? app, {
151
    String? userIdentifier,
152 153 154 155
  }) async => true;

  @override
  Future<bool> uninstallApp(
156
    ApplicationPackage app, {
157
    String? userIdentifier,
158 159 160 161 162
  }) async => true;

  @override
  Future<void> dispose() async {}

163 164 165 166 167 168 169
  @override
  Future<TargetPlatform> targetPlatform = Future<TargetPlatform>.value(TargetPlatform.android_arm);

  @override
  void noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);

  @override
170
  bool isSupportedForProject(FlutterProject flutterProject) => _isSupportedForProject;
171 172 173 174

  @override
  bool isSupported() => _isSupported;

175 176 177 178 179 180
  @override
  bool isConnected;

  @override
  DeviceConnectionInterface connectionInterface;

181 182 183 184 185 186 187
  @override
  Future<bool> isLocalEmulator = Future<bool>.value(true);

  @override
  Future<String> sdkNameAndVersion = Future<String>.value('Test SDK (1.2.3)');
}

188
/// Combines fake device with its canonical JSON representation.
189 190 191 192 193 194
class FakeDeviceJsonData {
  FakeDeviceJsonData(this.dev, this.json);

  final FakeDevice dev;
  final Map<String, Object> json;
}
195 196

class FakePollingDeviceDiscovery extends PollingDeviceDiscovery {
197 198 199
  FakePollingDeviceDiscovery({
    this.requiresExtendedWirelessDeviceDiscovery = false,
  })  : super('mock');
200 201 202 203 204 205

  final List<Device> _devices = <Device>[];
  final StreamController<Device> _onAddedController = StreamController<Device>.broadcast();
  final StreamController<Device> _onRemovedController = StreamController<Device>.broadcast();

  @override
206
  Future<List<Device>> pollingGetDevices({ Duration? timeout }) async {
207 208 209 210
    lastPollingTimeout = timeout;
    return _devices;
  }

211
  Duration? lastPollingTimeout;
212 213 214 215 216 217 218

  @override
  bool get supportsPlatform => true;

  @override
  bool get canListAnything => true;

219 220 221
  @override
  bool requiresExtendedWirelessDeviceDiscovery;

222 223 224 225 226 227 228 229 230 231 232
  void addDevice(Device device) {
    _devices.add(device);
    _onAddedController.add(device);
  }

  void _removeDevice(Device device) {
    _devices.remove(device);
    _onRemovedController.add(device);
  }

  void setDevices(List<Device> devices) {
233
    while (_devices.isNotEmpty) {
234 235 236 237 238
      _removeDevice(_devices.first);
    }
    devices.forEach(addDevice);
  }

239 240 241
  bool discoverDevicesCalled = false;

  @override
242 243 244 245
  Future<List<Device>> discoverDevices({
    Duration? timeout,
    DeviceDiscoveryFilter? filter,
  }) {
246 247 248 249
    discoverDevicesCalled = true;
    return super.discoverDevices(timeout: timeout);
  }

250 251 252 253 254
  @override
  Stream<Device> get onAdded => _onAddedController.stream;

  @override
  Stream<Device> get onRemoved => _onRemovedController.stream;
255 256 257

  @override
  List<String> wellKnownIds = <String>[];
258 259 260 261 262 263 264
}

/// A fake implementation of the [DeviceLogReader].
class FakeDeviceLogReader extends DeviceLogReader {
  @override
  String get name => 'FakeLogReader';

265 266
  bool disposed = false;

267
  final List<String> _lineQueue = <String>[];
268 269
  late final StreamController<String> _linesController =
    StreamController<String>
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
        .broadcast(onListen: () {
      _lineQueue.forEach(_linesController.add);
      _lineQueue.clear();
    });

  @override
  Stream<String> get logLines => _linesController.stream;

  void addLine(String line) {
    if (_linesController.hasListener) {
      _linesController.add(line);
    } else {
      _lineQueue.add(line);
    }
  }

  @override
  Future<void> dispose() async {
    _lineQueue.clear();
    await _linesController.close();
290
    disposed = true;
291 292
  }
}