context_test.dart 3.27 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
      AppContext context = new AppContext();
      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
      AppContext context = new AppContext();
      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
      AppContext context = new AppContext();
      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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

    test('awaitNestedZones', () async {
      AppContext outerContext = new AppContext();
      await outerContext.runInZone(() async {
        AppContext middleContext = new AppContext();
        await middleContext.runInZone(() async {
          AppContext innerContext = new AppContext();
          await innerContext.runInZone(() async {
            expect(innerContext.getVariable(String), isNull);
          });
        });
      });
    });

    test('fireAndForgetNestedZones', () async {
      AppContext outerContext = new AppContext();
      outerContext.runInZone(() async {
        AppContext middleContext = new AppContext();
        middleContext.runInZone(() async {
          AppContext innerContext = new AppContext();
          innerContext.runInZone(() async {
            expect(innerContext.getVariable(String), isNull);
          });
        });
      });
    });
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

    test('overriddenValuesInNestedZones', () async {
      expect(pkg.context, isNull);
      AppContext outerContext = new AppContext();
      outerContext.setVariable(String, 'outer');
      outerContext.runInZone(() async {
        expect(pkg.context[String], 'outer');
        AppContext middleContext = new AppContext();
        middleContext.setVariable(String, 'middle');
        middleContext.runInZone(() async {
          expect(pkg.context[String], 'middle');
          AppContext innerContext = new AppContext();
          innerContext.setVariable(String, 'inner');
          innerContext.runInZone(() async {
            expect(pkg.context[String], 'inner');
          });
          expect(pkg.context[String], 'middle');
        });
        expect(pkg.context[String], 'outer');
      });
    });
101 102
  });
}