// 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. import 'dart:async'; import 'package:flutter_tools/src/application_package.dart'; 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 fakeDevices = [ FakeDeviceJsonData( FakeDevice('ephemeral', 'ephemeral', type: PlatformType.android), { 'name': 'ephemeral', 'id': 'ephemeral', 'isSupported': true, 'targetPlatform': 'android-arm', 'emulator': true, 'sdk': 'Test SDK (1.2.3)', 'capabilities': { 'hotReload': true, 'hotRestart': true, 'screenshot': false, 'fastStart': false, 'flutterExit': true, 'hardwareRendering': true, 'startPaused': true } } ), FakeDeviceJsonData( FakeDevice('webby', 'webby') ..targetPlatform = Future.value(TargetPlatform.web_javascript) ..sdkNameAndVersion = Future.value('Web SDK (1.2.4)'), { 'name': 'webby', 'id': 'webby', 'isSupported': true, 'targetPlatform': 'web-javascript', 'emulator': true, 'sdk': 'Web SDK (1.2.4)', 'capabilities': { 'hotReload': true, 'hotRestart': true, 'screenshot': false, 'fastStart': false, 'flutterExit': true, 'hardwareRendering': true, 'startPaused': true } } ), ]; /// Fake device to test `devices` command. class FakeDevice extends Device { FakeDevice(this.name, String id, { bool ephemeral = true, bool isSupported = true, bool isSupportedForProject = true, PlatformType type = PlatformType.web, LaunchResult? launchResult, }) : _isSupported = isSupported, _isSupportedForProject = isSupportedForProject, _launchResult = launchResult ?? LaunchResult.succeeded(), super( id, platformType: type, category: Category.mobile, ephemeral: ephemeral, ); final bool _isSupported; final bool _isSupportedForProject; final LaunchResult _launchResult; @override final String name; @override Future startApp(covariant ApplicationPackage package, { String? mainPath, String? route, DebuggingOptions? debuggingOptions, Map? platformArgs, bool prebuiltApplication = false, bool ipv6 = false, String? userIdentifier, }) async => _launchResult; @override Future stopApp(covariant ApplicationPackage app, { String? userIdentifier, }) async => true; @override Future uninstallApp( covariant ApplicationPackage app, { String? userIdentifier, }) async => true; @override Future dispose() async {} @override Future targetPlatform = Future.value(TargetPlatform.android_arm); @override void noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); @override bool isSupportedForProject(FlutterProject flutterProject) => _isSupportedForProject; @override bool isSupported() => _isSupported; @override Future isLocalEmulator = Future.value(true); @override Future sdkNameAndVersion = Future.value('Test SDK (1.2.3)'); } /// Combines fake device with its canonical JSON representation. class FakeDeviceJsonData { FakeDeviceJsonData(this.dev, this.json); final FakeDevice dev; final Map json; } class FakePollingDeviceDiscovery extends PollingDeviceDiscovery { FakePollingDeviceDiscovery() : super('mock'); final List _devices = []; final StreamController _onAddedController = StreamController.broadcast(); final StreamController _onRemovedController = StreamController.broadcast(); @override Future> pollingGetDevices({ Duration? timeout }) async { lastPollingTimeout = timeout; return _devices; } Duration? lastPollingTimeout; @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 devices) { while(_devices.isNotEmpty) { _removeDevice(_devices.first); } devices.forEach(addDevice); } bool discoverDevicesCalled = false; @override Future> discoverDevices({Duration? timeout}) { discoverDevicesCalled = true; return super.discoverDevices(timeout: timeout); } @override Stream get onAdded => _onAddedController.stream; @override Stream get onRemoved => _onRemovedController.stream; @override List wellKnownIds = []; } /// A fake implementation of the [DeviceLogReader]. class FakeDeviceLogReader extends DeviceLogReader { @override String get name => 'FakeLogReader'; bool disposed = false; final List _lineQueue = []; late final StreamController _linesController = StreamController .broadcast(onListen: () { _lineQueue.forEach(_linesController.add); _lineQueue.clear(); }); @override Stream get logLines => _linesController.stream; void addLine(String line) { if (_linesController.hasListener) { _linesController.add(line); } else { _lineQueue.add(line); } } @override Future dispose() async { _lineQueue.clear(); await _linesController.close(); disposed = true; } }