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

5 6
// @dart = 2.8

7 8
import 'dart:async';

9
import 'package:file/file.dart';
10
import 'package:flutter_tools/src/base/common.dart';
11
import 'package:vm_service/vm_service.dart';
12

13
import '../src/common.dart';
14
import 'test_data/hot_reload_project.dart';
15
import 'test_driver.dart';
16
import 'test_utils.dart';
17 18

void main() {
19
  Directory tempDir;
20 21
  final HotReloadProject project = HotReloadProject();
  FlutterRunTestDriver flutter;
22

23 24
  setUp(() async {
    tempDir = createResolvedTempDirectorySync('hot_reload_test.');
25 26
    await project.setUpIn(tempDir);
    flutter = FlutterRunTestDriver(tempDir);
27
  });
28

29
  tearDown(() async {
30
    await flutter?.stop();
31 32
    tryToDelete(tempDir);
  });
33

34
  testWithoutContext('hot reload works without error', () async {
35 36
    await flutter.run();
    await flutter.hotReload();
37 38
  });

39
  testWithoutContext('multiple overlapping hot reload are debounced and queued', () async {
40
    await flutter.run();
41 42
    // Capture how many *real* hot reloads occur.
    int numReloads = 0;
43
    final StreamSubscription<void> subscription = flutter.stdout
44 45 46 47 48 49 50 51 52 53 54
        .map(parseFlutterResponse)
        .where(_isHotReloadCompletionEvent)
        .listen((_) => numReloads++);

    // To reduce tests flaking, override the debounce timer to something higher than
    // the default to ensure the hot reloads that are supposed to arrive within the
    // debounce period will even on slower CI machines.
    const int hotReloadDebounceOverrideMs = 250;
    const Duration delay = Duration(milliseconds: hotReloadDebounceOverrideMs * 2);

    Future<void> doReload([void _]) =>
55
        flutter.hotReload(debounce: true, debounceDurationOverrideMs: hotReloadDebounceOverrideMs);
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

    try {
      await Future.wait<void>(<Future<void>>[
        doReload(),
        doReload(),
        Future<void>.delayed(delay).then(doReload),
        Future<void>.delayed(delay).then(doReload),
      ]);

      // We should only get two reloads, as the first two will have been
      // merged together by the debounce, and the second two also.
      expect(numReloads, equals(2));
    } finally {
      await subscription.cancel();
    }
  });

73
  testWithoutContext('newly added code executes during hot reload', () async {
74
    final StringBuffer stdout = StringBuffer();
75 76 77
    final StreamSubscription<String> subscription = flutter.stdout.listen(stdout.writeln);
    await flutter.run();
    project.uncommentHotReloadPrint();
78
    try {
79
      await flutter.hotReload();
80 81 82 83 84
      expect(stdout.toString(), contains('(((((RELOAD WORKED)))))'));
    } finally {
      await subscription.cancel();
    }
  });
85

86
  testWithoutContext('hot restart works without error', () async {
87 88
    await flutter.run();
    await flutter.hotRestart();
89
  });
90

91
  testWithoutContext('breakpoints are hit after hot reload', () async {
92 93 94
    Isolate isolate;
    final Completer<void> sawTick1 = Completer<void>();
    final Completer<void> sawDebuggerPausedMessage = Completer<void>();
95
    final StreamSubscription<String> subscription = flutter.stdout.listen(
96 97 98 99 100 101 102 103 104 105 106
      (String line) {
        if (line.contains('((((TICK 1))))')) {
          expect(sawTick1.isCompleted, isFalse);
          sawTick1.complete();
        }
        if (line.contains('The application is paused in the debugger on a breakpoint.')) {
          expect(sawDebuggerPausedMessage.isCompleted, isFalse);
          sawDebuggerPausedMessage.complete();
        }
      },
    );
107 108
    await flutter.run(withDebugger: true, startPaused: true);
    await flutter.resume(); // we start paused so we can set up our TICK 1 listener before the app starts
109 110
    unawaited(sawTick1.future.timeout(
      const Duration(seconds: 5),
111 112 113 114 115 116
      onTimeout: () {
        // This print is useful for people debugging this test. Normally we would avoid printing in
        // a test but this is an exception because it's useful ambient information.
        // ignore: avoid_print
        print('The test app is taking longer than expected to print its synchronization line...');
      },
117
    ));
118
    printOnFailure('waiting for synchronization line...');
119
    await sawTick1.future; // after this, app is in steady state
120 121 122
    await flutter.addBreakpoint(
      project.scheduledBreakpointUri,
      project.scheduledBreakpointLine,
123
    );
124
    await Future<void>.delayed(const Duration(seconds: 2));
125 126
    await flutter.hotReload(); // reload triggers code which eventually hits the breakpoint
    isolate = await flutter.waitForPause();
127
    expect(isolate.pauseEvent.kind, equals(EventKind.kPauseBreakpoint));
128 129 130 131
    await flutter.resume();
    await flutter.addBreakpoint(
      project.buildBreakpointUri,
      project.buildBreakpointLine,
132 133
    );
    bool reloaded = false;
134
    final Future<void> reloadFuture = flutter.hotReload().then((void value) { reloaded = true; });
135
    printOnFailure('waiting for pause...');
136
    isolate = await flutter.waitForPause();
137
    expect(isolate.pauseEvent.kind, equals(EventKind.kPauseBreakpoint));
138
    printOnFailure('waiting for debugger message...');
139 140
    await sawDebuggerPausedMessage.future;
    expect(reloaded, isFalse);
141
    printOnFailure('waiting for resume...');
142
    await flutter.resume();
143
    printOnFailure('waiting for reload future...');
144 145 146
    await reloadFuture;
    expect(reloaded, isTrue);
    reloaded = false;
147
    printOnFailure('subscription cancel...');
148 149
    await subscription.cancel();
  });
150

151
  testWithoutContext("hot reload doesn't reassemble if paused", () async {
152
    final Completer<void> sawTick1 = Completer<void>();
153 154
    final Completer<void> sawDebuggerPausedMessage1 = Completer<void>();
    final Completer<void> sawDebuggerPausedMessage2 = Completer<void>();
155
    final StreamSubscription<String> subscription = flutter.stdout.listen(
156
      (String line) {
157
        printOnFailure('[LOG]:"$line"');
158 159 160
        if (line.contains('(((TICK 1)))')) {
          expect(sawTick1.isCompleted, isFalse);
          sawTick1.complete();
161 162 163 164 165 166 167 168 169 170 171
        }
        if (line.contains('The application is paused in the debugger on a breakpoint.')) {
          expect(sawDebuggerPausedMessage1.isCompleted, isFalse);
          sawDebuggerPausedMessage1.complete();
        }
        if (line.contains('The application is paused in the debugger on a breakpoint; interface might not update.')) {
          expect(sawDebuggerPausedMessage2.isCompleted, isFalse);
          sawDebuggerPausedMessage2.complete();
        }
      },
    );
172
    await flutter.run(withDebugger: true);
173
    await Future<void>.delayed(const Duration(seconds: 1));
174
    await sawTick1.future;
175 176 177
    await flutter.addBreakpoint(
      project.buildBreakpointUri,
      project.buildBreakpointLine,
178 179
    );
    bool reloaded = false;
180
    await Future<void>.delayed(const Duration(seconds: 1));
181 182
    final Future<void> reloadFuture = flutter.hotReload().then((void value) { reloaded = true; });
    final Isolate isolate = await flutter.waitForPause();
183 184 185 186 187
    expect(isolate.pauseEvent.kind, equals(EventKind.kPauseBreakpoint));
    expect(reloaded, isFalse);
    await sawDebuggerPausedMessage1.future; // this is the one where it say "uh, you broke into the debugger while reloading"
    await reloadFuture; // this is the one where it times out because you're in the debugger
    expect(reloaded, isTrue);
188
    await flutter.hotReload(); // now we're already paused
189
    await sawDebuggerPausedMessage2.future; // so we just get told that nothing is going to happen
190
    await flutter.resume();
191 192
    await subscription.cancel();
  });
193
}
194 195 196 197 198

bool _isHotReloadCompletionEvent(Map<String, dynamic> event) {
  return event != null &&
      event['event'] == 'app.progress' &&
      event['params'] != null &&
199 200
      (event['params'] as Map<String, dynamic>)['progressId'] == 'hot.reload' &&
      (event['params'] as Map<String, dynamic>)['finished'] == true;
201
}