single_widget_reload_test.dart 1.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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/base/file_system.dart';

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

void main() {
15
  late Directory tempDir;
16
  final SingleWidgetReloadProject project = SingleWidgetReloadProject();
17
  late FlutterRunTestDriver flutter;
18 19 20 21 22 23 24 25

  setUp(() async {
    tempDir = createResolvedTempDirectorySync('hot_reload_test.');
    await project.setUpIn(tempDir);
    flutter = FlutterRunTestDriver(tempDir);
  });

  tearDown(() async {
26
    await flutter.stop();
27 28 29 30 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
    tryToDelete(tempDir);
  });

  testWithoutContext('newly added code executes during hot reload with single widget reloads, but only invalidated widget', () async {
    final StringBuffer stdout = StringBuffer();
    final StreamSubscription<String> subscription = flutter.stdout.listen(stdout.writeln);
    await flutter.run(singleWidgetReloads: true);
    project.uncommentHotReloadPrint();
    try {
      await flutter.hotReload();
      expect(stdout.toString(), allOf(
        contains('(((TICK 1)))'),
        contains('(((((RELOAD WORKED)))))'),
        // Does not invalidate parent widget, so second tick is not output.
        isNot(contains('(((TICK 2)))'),
      )));
    } finally {
      await subscription.cancel();
    }
  });

  testWithoutContext('changes outside of the class body triggers a full reload', () async {
    final StringBuffer stdout = StringBuffer();
    final StreamSubscription<String> subscription = flutter.stdout.listen(stdout.writeln);
    await flutter.run(singleWidgetReloads: true);
    project.modifyFunction();
    try {
      await flutter.hotReload();
      expect(stdout.toString(), allOf(
        contains('(((TICK 1)))'),
        contains('(((TICK 2)))'),
      ));
    } finally {
      await subscription.cancel();
    }
  });
}