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

import '../test_utils.dart';
6
import 'project.dart';
7

8
class HotReloadProject extends Project {
9 10 11 12 13 14 15 16 17 18 19 20 21 22
  @override
  final String pubspec = '''
  name: test
  environment:
    sdk: ">=2.0.0-dev.68.0 <3.0.0"

  dependencies:
    flutter:
      sdk: flutter
  ''';

  @override
  final String main = r'''
  import 'package:flutter/material.dart';
23
  import 'package:flutter/scheduler.dart';
24 25 26 27 28 29 30 31 32
  import 'package:flutter/services.dart';
  import 'package:flutter/widgets.dart';

  void main() async {
    WidgetsFlutterBinding.ensureInitialized();
    final ByteData message = const StringCodec().encodeMessage('AppLifecycleState.resumed');
    await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage('flutter/lifecycle', message, (_) { });
    runApp(MyApp());
  }
33

34 35
  int count = 1;

36 37 38
  class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
39 40 41 42
      // This method gets called each time we hot reload, during reassemble.

      // Do not remove the next line, it's uncommented by a test to verify that
      // hot reloading worked:
43 44
      // printHotReloadWorked();

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
      print('((((TICK $count))))');
      // tick 1 = startup warmup frame
      // tick 2 = hot reload warmup reassemble frame
      // after that there's a post-hot-reload frame scheduled by the tool that
      // doesn't trigger this to rebuild, but does trigger the first callback
      // below, then that callback schedules another frame on which we do the
      // breakpoint.
      // tick 3 = second hot reload warmup reassemble frame (pre breakpoint)
      if (count == 2) {
        SchedulerBinding.instance.scheduleFrameCallback((Duration timestamp) {
          SchedulerBinding.instance.scheduleFrameCallback((Duration timestamp) {
            print('breakpoint line'); // SCHEDULED BREAKPOINT
          });
        });
      }
      count += 1;

      return MaterialApp( // BUILD BREAKPOINT
63
        title: 'Flutter Demo',
64
        home: Container(),
65 66 67 68
      );
    }
  }

69
  void printHotReloadWorked() {
70 71 72 73 74 75
    // The call to this function is uncommented by a test to verify that hot
    // reloading worked.
    print('(((((RELOAD WORKED)))))');
  }
  ''';

76 77 78 79 80 81
  Uri get scheduledBreakpointUri => mainDart;
  int get scheduledBreakpointLine => lineContaining(main, '// SCHEDULED BREAKPOINT');

  Uri get buildBreakpointUri => mainDart;
  int get buildBreakpointLine => lineContaining(main, '// BUILD BREAKPOINT');

82 83
  void uncommentHotReloadPrint() {
    final String newMainContents = main.replaceAll(
84
      '// printHotReloadWorked();',
85
      'printHotReloadWorked();',
86
    );
87
    writeFile(fileSystem.path.join(dir.path, 'lib', 'main.dart'), newMainContents);
88 89
  }
}