fake_devices.dart 5.88 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
/// Fake device to test `devices` command.
60
class FakeDevice extends Device {
61 62 63
  FakeDevice(this.name, String id, {
    bool ephemeral = true,
    bool isSupported = true,
64
    bool isSupportedForProject = true,
65
    PlatformType type = PlatformType.web,
66
    LaunchResult? launchResult,
67
  }) : _isSupported = isSupported,
68
      _isSupportedForProject = isSupportedForProject,
69 70 71 72 73 74 75
      _launchResult = launchResult ?? LaunchResult.succeeded(),
      super(
        id,
        platformType: type,
        category: Category.mobile,
        ephemeral: ephemeral,
      );
76 77

  final bool _isSupported;
78
  final bool _isSupportedForProject;
79
  final LaunchResult _launchResult;
80 81 82 83

  @override
  final String name;

84 85
  @override
  Future<LaunchResult> startApp(covariant ApplicationPackage package, {
86 87 88 89
    String? mainPath,
    String? route,
    DebuggingOptions? debuggingOptions,
    Map<String, dynamic>? platformArgs,
90 91
    bool prebuiltApplication = false,
    bool ipv6 = false,
92
    String? userIdentifier,
93 94 95 96
  }) async => _launchResult;

  @override
  Future<bool> stopApp(covariant ApplicationPackage app, {
97
    String? userIdentifier,
98 99 100 101 102
  }) async => true;

  @override
  Future<bool> uninstallApp(
  covariant ApplicationPackage app, {
103
    String? userIdentifier,
104 105 106 107 108
  }) async => true;

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

109 110 111 112 113 114 115
  @override
  Future<TargetPlatform> targetPlatform = Future<TargetPlatform>.value(TargetPlatform.android_arm);

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

  @override
116
  bool isSupportedForProject(FlutterProject flutterProject) => _isSupportedForProject;
117 118 119 120 121 122 123 124 125 126 127

  @override
  bool isSupported() => _isSupported;

  @override
  Future<bool> isLocalEmulator = Future<bool>.value(true);

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

128
/// Combines fake device with its canonical JSON representation.
129 130 131 132 133 134
class FakeDeviceJsonData {
  FakeDeviceJsonData(this.dev, this.json);

  final FakeDevice dev;
  final Map<String, Object> json;
}
135 136 137 138 139 140 141 142 143

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
144
  Future<List<Device>> pollingGetDevices({ Duration? timeout }) async {
145 146 147 148
    lastPollingTimeout = timeout;
    return _devices;
  }

149
  Duration? lastPollingTimeout;
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

  @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);
  }

174 175 176 177 178 179 180 181
  bool discoverDevicesCalled = false;

  @override
  Future<List<Device>> discoverDevices({Duration? timeout}) {
    discoverDevicesCalled = true;
    return super.discoverDevices(timeout: timeout);
  }

182 183 184 185 186
  @override
  Stream<Device> get onAdded => _onAddedController.stream;

  @override
  Stream<Device> get onRemoved => _onRemovedController.stream;
187 188 189

  @override
  List<String> wellKnownIds = <String>[];
190 191 192 193 194 195 196
}

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

197 198
  bool disposed = false;

199
  final List<String> _lineQueue = <String>[];
200 201
  late final StreamController<String> _linesController =
    StreamController<String>
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        .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();
222
    disposed = true;
223 224
  }
}