context_test.dart 3.36 KB
Newer Older
1 2 3 4
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'package:flutter_tools/src/base/context.dart' hide context;
6
import 'package:flutter_tools/src/base/context.dart' as pkg;
7
import 'package:flutter_tools/src/base/logger.dart';
8
import 'package:flutter_tools/src/globals.dart';
9 10
import 'package:test/test.dart';

11
void main() {
12
  group('AppContext', () {
13
    test('error', () async {
14 15
      final AppContext context = new AppContext();
      final BufferLogger mockLogger = new BufferLogger();
16
      context.setVariable(Logger, mockLogger);
17

18
      await context.runInZone(() {
19
        printError('foo bar');
20
      });
21

22 23 24
      expect(mockLogger.errorText, 'foo bar\n');
      expect(mockLogger.statusText, '');
      expect(mockLogger.traceText, '');
25 26 27
    });

    test('status', () async {
28 29
      final AppContext context = new AppContext();
      final BufferLogger mockLogger = new BufferLogger();
30
      context.setVariable(Logger, mockLogger);
31

32
      await context.runInZone(() {
33
        printStatus('foo bar');
34
      });
35

36 37 38
      expect(mockLogger.errorText, '');
      expect(mockLogger.statusText, 'foo bar\n');
      expect(mockLogger.traceText, '');
39 40 41
    });

    test('trace', () async {
42 43
      final AppContext context = new AppContext();
      final BufferLogger mockLogger = new BufferLogger();
44
      context.setVariable(Logger, mockLogger);
45

46
      await context.runInZone(() {
47
        printTrace('foo bar');
48
      });
49

50 51 52
      expect(mockLogger.errorText, '');
      expect(mockLogger.statusText, '');
      expect(mockLogger.traceText, 'foo bar\n');
53
    });
54 55

    test('awaitNestedZones', () async {
56
      final AppContext outerContext = new AppContext();
57
      await outerContext.runInZone(() async {
58
        final AppContext middleContext = new AppContext();
59
        await middleContext.runInZone(() async {
60
          final AppContext innerContext = new AppContext();
61 62 63 64 65 66 67 68
          await innerContext.runInZone(() async {
            expect(innerContext.getVariable(String), isNull);
          });
        });
      });
    });

    test('fireAndForgetNestedZones', () async {
69
      final AppContext outerContext = new AppContext();
70
      outerContext.runInZone(() async {
71
        final AppContext middleContext = new AppContext();
72
        middleContext.runInZone(() async {
73
          final AppContext innerContext = new AppContext();
74 75 76 77 78 79
          innerContext.runInZone(() async {
            expect(innerContext.getVariable(String), isNull);
          });
        });
      });
    });
80 81 82

    test('overriddenValuesInNestedZones', () async {
      expect(pkg.context, isNull);
83
      final AppContext outerContext = new AppContext();
84 85 86
      outerContext.setVariable(String, 'outer');
      outerContext.runInZone(() async {
        expect(pkg.context[String], 'outer');
87
        final AppContext middleContext = new AppContext();
88 89 90
        middleContext.setVariable(String, 'middle');
        middleContext.runInZone(() async {
          expect(pkg.context[String], 'middle');
91
          final AppContext innerContext = new AppContext();
92 93 94 95 96 97 98 99 100
          innerContext.setVariable(String, 'inner');
          innerContext.runInZone(() async {
            expect(pkg.context[String], 'inner');
          });
          expect(pkg.context[String], 'middle');
        });
        expect(pkg.context[String], 'outer');
      });
    });
101 102
  });
}