fake_devices.dart 6.74 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
/// Fake device to test `devices` command.
85
class FakeDevice extends Device {
86 87 88
  FakeDevice(this.name, String id, {
    bool ephemeral = true,
    bool isSupported = true,
89
    bool isSupportedForProject = true,
90 91
    this.isConnected = true,
    this.connectionInterface = DeviceConnectionInterface.attached,
92
    PlatformType type = PlatformType.web,
93
    LaunchResult? launchResult,
94
  }) : _isSupported = isSupported,
95
      _isSupportedForProject = isSupportedForProject,
96 97 98 99 100 101 102
      _launchResult = launchResult ?? LaunchResult.succeeded(),
      super(
        id,
        platformType: type,
        category: Category.mobile,
        ephemeral: ephemeral,
      );
103 104

  final bool _isSupported;
105
  final bool _isSupportedForProject;
106
  final LaunchResult _launchResult;
107 108 109 110

  @override
  final String name;

111
  @override
112
  Future<LaunchResult> startApp(ApplicationPackage? package, {
113 114 115 116
    String? mainPath,
    String? route,
    DebuggingOptions? debuggingOptions,
    Map<String, dynamic>? platformArgs,
117 118
    bool prebuiltApplication = false,
    bool ipv6 = false,
119
    String? userIdentifier,
120 121 122
  }) async => _launchResult;

  @override
123
  Future<bool> stopApp(ApplicationPackage? app, {
124
    String? userIdentifier,
125 126 127 128
  }) async => true;

  @override
  Future<bool> uninstallApp(
129
    ApplicationPackage app, {
130
    String? userIdentifier,
131 132 133 134 135
  }) async => true;

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

136 137 138 139 140 141 142
  @override
  Future<TargetPlatform> targetPlatform = Future<TargetPlatform>.value(TargetPlatform.android_arm);

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

  @override
143
  bool isSupportedForProject(FlutterProject flutterProject) => _isSupportedForProject;
144 145 146 147

  @override
  bool isSupported() => _isSupported;

148 149 150 151 152 153
  @override
  bool isConnected;

  @override
  DeviceConnectionInterface connectionInterface;

154 155 156 157 158 159 160
  @override
  Future<bool> isLocalEmulator = Future<bool>.value(true);

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

161
/// Combines fake device with its canonical JSON representation.
162 163 164 165 166 167
class FakeDeviceJsonData {
  FakeDeviceJsonData(this.dev, this.json);

  final FakeDevice dev;
  final Map<String, Object> json;
}
168 169 170 171 172 173 174 175 176

class FakePollingDeviceDiscovery extends PollingDeviceDiscovery {
  FakePollingDeviceDiscovery() : super('mock');

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

  @override
177
  Future<List<Device>> pollingGetDevices({ Duration? timeout }) async {
178 179 180 181
    lastPollingTimeout = timeout;
    return _devices;
  }

182
  Duration? lastPollingTimeout;
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

  @override
  bool get supportsPlatform => true;

  @override
  bool get canListAnything => true;

  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) {
    while(_devices.isNotEmpty) {
      _removeDevice(_devices.first);
    }
    devices.forEach(addDevice);
  }

207 208 209
  bool discoverDevicesCalled = false;

  @override
210 211 212 213
  Future<List<Device>> discoverDevices({
    Duration? timeout,
    DeviceDiscoveryFilter? filter,
  }) {
214 215 216 217
    discoverDevicesCalled = true;
    return super.discoverDevices(timeout: timeout);
  }

218 219 220 221 222
  @override
  Stream<Device> get onAdded => _onAddedController.stream;

  @override
  Stream<Device> get onRemoved => _onRemovedController.stream;
223 224 225

  @override
  List<String> wellKnownIds = <String>[];
226 227 228 229 230 231 232
}

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

233 234
  bool disposed = false;

235
  final List<String> _lineQueue = <String>[];
236 237
  late final StreamController<String> _linesController =
    StreamController<String>
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
        .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();
258
    disposed = true;
259 260
  }
}