flutter_adapter_test.dart 6.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// 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.

// @dart = 2.8

import 'dart:async';

import 'package:dds/src/dap/protocol_generated.dart';
import 'package:file/file.dart';
import 'package:flutter_tools/src/cache.dart';

import '../../src/common.dart';
import '../test_data/basic_project.dart';
import '../test_data/compile_error_project.dart';
import '../test_utils.dart';
import 'test_client.dart';
import 'test_support.dart';

void main() {
  Directory tempDir;
  /*late*/ DapTestSession dap;
  final String relativeMainPath = 'lib${fileSystem.path.separator}main.dart';

  setUpAll(() {
    Cache.flutterRoot = getFlutterRoot();
  });

  setUp(() async {
30
    tempDir = createResolvedTempDirectorySync('flutter_adapter_test.');
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 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 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    dap = await DapTestSession.setUp();
  });

  tearDown(() async {
    await dap.tearDown();
    tryToDelete(tempDir);
  });

  testWithoutContext('can run and terminate a Flutter app in debug mode', () async {
    final BasicProject _project = BasicProject();
    await _project.setUpIn(tempDir);

    // Once the "topLevelFunction" output arrives, we can terminate the app.
    unawaited(
      dap.client.outputEvents
          .firstWhere((OutputEventBody output) => output.output.startsWith('topLevelFunction'))
          .whenComplete(() => dap.client.terminate()),
    );

    final List<OutputEventBody> outputEvents = await dap.client.collectAllOutput(
      launch: () => dap.client
          .launch(
            cwd: _project.dir.path,
            toolArgs: <String>['-d', 'flutter-tester'],
          ),
    );

    final String output = _uniqueOutputLines(outputEvents);

    expectLines(output, <Object>[
      'Launching $relativeMainPath on Flutter test device in debug mode...',
      startsWith('Connecting to VM Service at'),
      'topLevelFunction',
      '',
      startsWith('Exited'),
    ]);
  });

  testWithoutContext('can run and terminate a Flutter app in noDebug mode', () async {
    final BasicProject _project = BasicProject();
    await _project.setUpIn(tempDir);

    // Once the "topLevelFunction" output arrives, we can terminate the app.
    unawaited(
      dap.client.outputEvents
          .firstWhere((OutputEventBody output) => output.output.startsWith('topLevelFunction'))
          .whenComplete(() => dap.client.terminate()),
    );

    final List<OutputEventBody> outputEvents = await dap.client.collectAllOutput(
      launch: () => dap.client
          .launch(
            cwd: _project.dir.path,
            noDebug: true,
            toolArgs: <String>['-d', 'flutter-tester'],
          ),
    );

    final String output = _uniqueOutputLines(outputEvents);

    expectLines(output, <Object>[
      'Launching $relativeMainPath on Flutter test device in debug mode...',
      'topLevelFunction',
      '',
      startsWith('Exited'),
    ]);
  });

  testWithoutContext('correctly outputs launch errors and terminates', () async {
    final CompileErrorProject _project = CompileErrorProject();
    await _project.setUpIn(tempDir);

    final List<OutputEventBody> outputEvents = await dap.client.collectAllOutput(
      launch: () => dap.client
          .launch(
            cwd: _project.dir.path,
            toolArgs: <String>['-d', 'flutter-tester'],
          ),
    );

    final String output = _uniqueOutputLines(outputEvents);
    expect(output, contains('this code does not compile'));
    expect(output, contains('Exception: Failed to build'));
    expect(output, contains('Exited (1)'));
  });

  testWithoutContext('can hot reload', () async {
    final BasicProject _project = BasicProject();
    await _project.setUpIn(tempDir);

    // Launch the app and wait for it to print "topLevelFunction".
    await Future.wait(<Future<Object>>[
      dap.client.outputEvents.firstWhere((OutputEventBody output) => output.output.startsWith('topLevelFunction')),
      dap.client.start(
        launch: () => dap.client.launch(
          cwd: _project.dir.path,
          noDebug: true,
          toolArgs: <String>['-d', 'flutter-tester'],
        ),
      ),
    ], eagerError: true);

    // Capture the next two output events that we expect to be the Reload
    // notification and then topLevelFunction being printed again.
    final Future<List<String>> outputEventsFuture = dap.client.output
        // But skip any topLevelFunctions that come before the reload.
        .skipWhile((String output) => output.startsWith('topLevelFunction'))
        .take(2)
        .toList();

    await dap.client.hotReload();

    expectLines(
        (await outputEventsFuture).join(),
        <Object>[
          startsWith('Reloaded'),
          'topLevelFunction',
        ],
    );

    await dap.client.terminate();
  });

  testWithoutContext('can hot restart', () async {
    final BasicProject _project = BasicProject();
    await _project.setUpIn(tempDir);

    // Launch the app and wait for it to print "topLevelFunction".
    await Future.wait(<Future<Object>>[
      dap.client.outputEvents.firstWhere((OutputEventBody output) => output.output.startsWith('topLevelFunction')),
      dap.client.start(
        launch: () => dap.client.launch(
          cwd: _project.dir.path,
          noDebug: true,
          toolArgs: <String>['-d', 'flutter-tester'],
        ),
      ),
    ], eagerError: true);

    // Capture the next two output events that we expect to be the Restart
    // notification and then topLevelFunction being printed again.
    final Future<List<String>> outputEventsFuture = dap.client.output
        // But skip any topLevelFunctions that come before the restart.
        .skipWhile((String output) => output.startsWith('topLevelFunction'))
        .take(2)
        .toList();

    await dap.client.hotRestart();

    expectLines(
        (await outputEventsFuture).join(),
        <Object>[
          startsWith('Restarted application'),
          'topLevelFunction',
        ],
    );

    await dap.client.terminate();
  });
}

/// Extracts the output from a set of [OutputEventBody], removing any
/// adjacent duplicates and combining into a single string.
String _uniqueOutputLines(List<OutputEventBody> outputEvents) {
  String/*?*/ lastItem;
  return outputEvents
      .map((OutputEventBody e) => e.output)
      .where((String output) {
        // Skip the item if it's the same as the previous one.
        final bool isDupe = output == lastItem;
        lastItem = output;
        return !isDupe;
      })
      .join();
}