background_project.dart 2.31 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 '../test_utils.dart';
import 'project.dart';

/// Spawns a background isolate that prints a debug message.
class BackgroundProject extends Project {

  @override
  final String pubspec = '''
  name: test
  environment:
15
    sdk: ">=2.12.0-0 <3.0.0"
16 17 18 19 20 21 22 23 24 25 26 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

  dependencies:
    flutter:
      sdk: flutter
  ''';

  @override
  final String main = r'''
  import 'dart:async';
  import 'dart:isolate';

  import 'package:flutter/widgets.dart';
  import 'package:flutter/material.dart';

  void main() {
    Isolate.spawn<void>(background, null, debugName: 'background');
    TestMain();
  }

  void background(void message) {
    TestIsolate();
  }

  class TestMain {
    TestMain() {
      debugPrint('Main thread');
    }
  }

  class TestIsolate {
    TestIsolate() {
      debugPrint('Isolate thread');
    }
  }
  ''';

  void updateTestIsolatePhrase(String message) {
    final String newMainContents = main.replaceFirst('Isolate thread', message);
54 55
    writeFile(fileSystem.path.join(dir.path, 'lib', 'main.dart'), newMainContents,
        writeFutureModifiedDate: true);
56 57 58 59 60 61 62 63 64 65
  }
}

// Spawns a background isolate that repeats a message.
class RepeatingBackgroundProject extends Project {

  @override
  final String pubspec = '''
  name: test
  environment:
66
    sdk: ">=2.12.0-0 <3.0.0"
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

  dependencies:
    flutter:
      sdk: flutter
  ''';

  @override
  final String main = r'''
  import 'dart:async';
  import 'dart:isolate';

  import 'package:flutter/widgets.dart';
  import 'package:flutter/material.dart';

  void main() {
    Isolate.spawn<void>(background, null, debugName: 'background');
    TestMain();
  }

  void background(void message) {
    Timer.periodic(const Duration(milliseconds: 500), (Timer timer) => TestIsolate());
  }

  class TestMain {
    TestMain() {
      debugPrint('Main thread');
    }
  }

  class TestIsolate {
    TestIsolate() {
      debugPrint('Isolate thread');
    }
  }
  ''';

  void updateTestIsolatePhrase(String message) {
    final String newMainContents = main.replaceFirst('Isolate thread', message);
105 106 107 108 109
    writeFile(
      fileSystem.path.join(dir.path, 'lib', 'main.dart'),
      newMainContents,
      writeFutureModifiedDate: true,
    );
110 111
  }
}