daemon_mode_test.dart 2.67 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';
import 'dart:convert';
7
import 'dart:io' hide Directory;
8

9
import 'package:file/file.dart';
10
import 'package:process/process.dart';
11 12 13 14 15 16 17

import '../src/common.dart';
import 'test_data/basic_project.dart';
import 'test_driver.dart';
import 'test_utils.dart';

void main() {
18 19
  late Directory tempDir;
  late Process daemonProcess;
20 21 22 23

  setUp(() async {
    tempDir = createResolvedTempDirectorySync('daemon_mode_test.');
  });
24

25 26
  tearDown(() async {
    tryToDelete(tempDir);
27
    daemonProcess.kill();
28 29 30
  });

  testWithoutContext('device.getDevices', () async {
31 32
    final BasicProject project = BasicProject();
    await project.setUpIn(tempDir);
33

34
    final String flutterBin = fileSystem.path.join(getFlutterRoot(), 'bin', 'flutter');
35

36
    const ProcessManager processManager = LocalProcessManager();
37
    daemonProcess = await processManager.start(
38
      <String>[flutterBin, ...getLocalEngineArguments(), '--show-test-device', 'daemon'],
39 40 41 42
      workingDirectory: tempDir.path,
    );

    final StreamController<String> stdout = StreamController<String>.broadcast();
43
    transformToLines(daemonProcess.stdout).listen((String line) => stdout.add(line));
44
    final Stream<Map<String, Object?>?> stream = stdout
45
      .stream
46 47
      .map<Map<String, Object?>?>(parseFlutterResponse)
      .where((Map<String, Object?>? value) => value != null);
48

49
    Map<String, Object?> response = (await stream.first)!;
50 51 52
    expect(response['event'], 'daemon.connected');

    // start listening for devices
53
    daemonProcess.stdin.writeln('[${jsonEncode(<String, dynamic>{
54 55 56
      'id': 1,
      'method': 'device.enable',
    })}]');
57
    response = (await stream.firstWhere((Map<String, Object?>? json) => json!['id'] == 1))!;
58 59 60 61 62
    expect(response['id'], 1);
    expect(response['error'], isNull);

    // [{"event":"device.added","params":{"id":"flutter-tester","name":
    //   "Flutter test device","platform":"flutter-tester","emulator":false}}]
63
    response = (await stream.first)!;
64 65 66
    expect(response['event'], 'device.added');

    // get the list of all devices
67
    daemonProcess.stdin.writeln('[${jsonEncode(<String, dynamic>{
68 69 70
      'id': 2,
      'method': 'device.getDevices',
    })}]');
71
    // Skip other device.added events that may fire (desktop/web devices).
72
    response = (await stream.firstWhere((Map<String, Object?>? response) => response!['event'] != 'device.added'))!;
73 74 75 76 77 78 79
    expect(response['id'], 2);
    expect(response['error'], isNull);

    final dynamic result = response['result'];
    expect(result, isList);
    expect(result, isNotEmpty);
  });
80
}