hot_reload_test.dart 7.68 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
import 'dart:async';

7
import 'package:file/file.dart';
8
import 'package:vm_service/vm_service.dart';
9

10
import '../src/common.dart';
11
import 'test_data/hot_reload_project.dart';
12
import 'test_driver.dart';
13
import 'test_utils.dart';
14 15

void main() {
16
  late Directory tempDir;
17
  final HotReloadProject project = HotReloadProject();
18
  late FlutterRunTestDriver flutter;
19

20 21
  setUp(() async {
    tempDir = createResolvedTempDirectorySync('hot_reload_test.');
22 23
    await project.setUpIn(tempDir);
    flutter = FlutterRunTestDriver(tempDir);
24
  });
25

26
  tearDown(() async {
27
    await flutter.stop();
28 29
    tryToDelete(tempDir);
  });
30

31
  testWithoutContext('hot reload works without error', () async {
32 33
    await flutter.run();
    await flutter.hotReload();
34 35
  });

36
  testWithoutContext('multiple overlapping hot reload are debounced and queued', () async {
37
    await flutter.run();
38 39
    // Capture how many *real* hot reloads occur.
    int numReloads = 0;
40
    final StreamSubscription<void> subscription = flutter.stdout
41 42 43 44 45 46 47 48 49 50 51
        .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 _]) =>
52
        flutter.hotReload(debounce: true, debounceDurationOverrideMs: hotReloadDebounceOverrideMs);
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

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

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

83
  testWithoutContext('hot restart works without error', () async {
84 85
    await flutter.run();
    await flutter.hotRestart();
86
  });
87

88
  testWithoutContext('breakpoints are hit after hot reload', () async {
89 90 91
    Isolate isolate;
    final Completer<void> sawTick1 = Completer<void>();
    final Completer<void> sawDebuggerPausedMessage = Completer<void>();
92
    final StreamSubscription<String> subscription = flutter.stdout.listen(
93 94 95 96 97 98 99 100 101 102 103
      (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();
        }
      },
    );
104 105
    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
106 107
    unawaited(sawTick1.future.timeout(
      const Duration(seconds: 5),
108 109 110 111 112 113
      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...');
      },
114
    ));
115
    printOnFailure('waiting for synchronization line...');
116
    await sawTick1.future; // after this, app is in steady state
117 118 119
    await flutter.addBreakpoint(
      project.scheduledBreakpointUri,
      project.scheduledBreakpointLine,
120
    );
121
    await Future<void>.delayed(const Duration(seconds: 2));
122 123
    await flutter.hotReload(); // reload triggers code which eventually hits the breakpoint
    isolate = await flutter.waitForPause();
124
    expect(isolate.pauseEvent?.kind, equals(EventKind.kPauseBreakpoint));
125 126 127 128
    await flutter.resume();
    await flutter.addBreakpoint(
      project.buildBreakpointUri,
      project.buildBreakpointLine,
129 130
    );
    bool reloaded = false;
131
    final Future<void> reloadFuture = flutter.hotReload().then((void value) { reloaded = true; });
132
    printOnFailure('waiting for pause...');
133
    isolate = await flutter.waitForPause();
134
    expect(isolate.pauseEvent?.kind, equals(EventKind.kPauseBreakpoint));
135
    printOnFailure('waiting for debugger message...');
136 137
    await sawDebuggerPausedMessage.future;
    expect(reloaded, isFalse);
138
    printOnFailure('waiting for resume...');
139
    await flutter.resume();
140
    printOnFailure('waiting for reload future...');
141 142 143
    await reloadFuture;
    expect(reloaded, isTrue);
    reloaded = false;
144
    printOnFailure('subscription cancel...');
145 146
    await subscription.cancel();
  });
147

148
  testWithoutContext("hot reload doesn't reassemble if paused", () async {
149
    final Completer<void> sawTick1 = Completer<void>();
150 151
    final Completer<void> sawDebuggerPausedMessage1 = Completer<void>();
    final Completer<void> sawDebuggerPausedMessage2 = Completer<void>();
152
    final StreamSubscription<String> subscription = flutter.stdout.listen(
153
      (String line) {
154
        printOnFailure('[LOG]:"$line"');
155 156 157
        if (line.contains('(((TICK 1)))')) {
          expect(sawTick1.isCompleted, isFalse);
          sawTick1.complete();
158 159 160 161 162 163 164 165 166 167 168
        }
        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();
        }
      },
    );
169
    await flutter.run(withDebugger: true);
170
    await Future<void>.delayed(const Duration(seconds: 1));
171
    await sawTick1.future;
172 173 174
    await flutter.addBreakpoint(
      project.buildBreakpointUri,
      project.buildBreakpointLine,
175 176
    );
    bool reloaded = false;
177
    await Future<void>.delayed(const Duration(seconds: 1));
178 179
    final Future<void> reloadFuture = flutter.hotReload().then((void value) { reloaded = true; });
    final Isolate isolate = await flutter.waitForPause();
180
    expect(isolate.pauseEvent?.kind, equals(EventKind.kPauseBreakpoint));
181 182 183 184
    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);
185
    await flutter.hotReload(); // now we're already paused
186
    await sawDebuggerPausedMessage2.future; // so we just get told that nothing is going to happen
187
    await flutter.resume();
188 189
    await subscription.cancel();
  });
190
}
191

192
bool _isHotReloadCompletionEvent(Map<String, Object?>? event) {
193 194 195
  return event != null &&
      event['event'] == 'app.progress' &&
      event['params'] != null &&
196 197
      (event['params'] as Map<String, Object?>?)!['progressId'] == 'hot.reload' &&
      (event['params'] as Map<String, Object?>?)!['finished'] == true;
198
}