expression_evaluation_test.dart 6.14 KB
Newer Older
1 2 3 4
// Copyright 2018 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 6 7 8 9
// Integration tests which invoke flutter instead of unit testing the code
// will not produce meaningful coverage information - we can measure coverage
// from the isolate running the test, but not from the isolate started via
// the command line process.
@Tags(<String>['no_coverage'])
10 11 12 13
import 'dart:async';

import 'package:file/file.dart';
import 'package:flutter_tools/src/base/file_system.dart';
14

15
import 'package:vm_service/vm_service.dart';
16

17
import '../src/common.dart';
18
import 'test_data/basic_project.dart';
19
import 'test_data/tests_project.dart';
20
import 'test_driver.dart';
21
import 'test_utils.dart';
22 23

void main() {
24
  group('flutter run expression evaluation', () {
25
    Directory tempDir;
26
    final BasicProject _project = BasicProject();
27
    FlutterRunTestDriver _flutter;
28

29
    setUp(() async {
30
      tempDir = createResolvedTempDirectorySync('run_expression_eval_test.');
31
      await _project.setUpIn(tempDir);
32
      _flutter = FlutterRunTestDriver(tempDir);
33 34 35
    });

    tearDown(() async {
36 37
      await _flutter.stop();
      tryToDelete(tempDir);
38 39
    });

40 41 42 43 44
    Future<void> breakInBuildMethod(FlutterTestDriver flutter) async {
      await _flutter.breakAt(
        _project.buildMethodBreakpointUri,
        _project.buildMethodBreakpointLine,
      );
45 46
    }

47 48 49 50 51
    Future<void> breakInTopLevelFunction(FlutterTestDriver flutter) async {
      await _flutter.breakAt(
        _project.topLevelFunctionBreakpointUri,
        _project.topLevelFunctionBreakpointLine,
      );
52 53 54 55 56
    }

    test('can evaluate trivial expressions in top level function', () async {
      await _flutter.run(withDebugger: true);
      await breakInTopLevelFunction(_flutter);
57
      await evaluateTrivialExpressions(_flutter);
58 59 60 61 62
    });

    test('can evaluate trivial expressions in build method', () async {
      await _flutter.run(withDebugger: true);
      await breakInBuildMethod(_flutter);
63
      await evaluateTrivialExpressions(_flutter);
64 65 66 67 68
    });

    test('can evaluate complex expressions in top level function', () async {
      await _flutter.run(withDebugger: true);
      await breakInTopLevelFunction(_flutter);
69
      await evaluateComplexExpressions(_flutter);
70 71 72 73 74
    });

    test('can evaluate complex expressions in build method', () async {
      await _flutter.run(withDebugger: true);
      await breakInBuildMethod(_flutter);
75
      await evaluateComplexExpressions(_flutter);
76 77 78 79 80
    });

    test('can evaluate expressions returning complex objects in top level function', () async {
      await _flutter.run(withDebugger: true);
      await breakInTopLevelFunction(_flutter);
81
      await evaluateComplexReturningExpressions(_flutter);
82 83 84 85 86
    });

    test('can evaluate expressions returning complex objects in build method', () async {
      await _flutter.run(withDebugger: true);
      await breakInBuildMethod(_flutter);
87
      await evaluateComplexReturningExpressions(_flutter);
88
    });
Dan Field's avatar
Dan Field committed
89
  }, timeout: const Timeout.factor(10), tags: <String>['integration']); // The DevFS sync takes a really long time, so these tests can be slow.
90 91 92 93 94 95 96

  group('flutter test expression evaluation', () {
    Directory tempDir;
    final TestsProject _project = TestsProject();
    FlutterTestTestDriver _flutter;

    setUp(() async {
97
      tempDir = createResolvedTempDirectorySync('test_expression_eval_test.');
98 99 100 101 102 103 104 105 106 107 108 109 110 111
      await _project.setUpIn(tempDir);
      _flutter = FlutterTestTestDriver(tempDir);
    });

    tearDown(() async {
      await _flutter.quit();
      tryToDelete(tempDir);
    });

    test('can evaluate trivial expressions in a test', () async {
      await _flutter.test(
        withDebugger: true,
        beforeStart: () => _flutter.addBreakpoint(_project.breakpointUri, _project.breakpointLine),
      );
112
      await _flutter.waitForPause();
113
      await evaluateTrivialExpressions(_flutter);
114
      await _flutter.resume();
115 116 117 118 119 120 121
    });

    test('can evaluate complex expressions in a test', () async {
      await _flutter.test(
        withDebugger: true,
        beforeStart: () => _flutter.addBreakpoint(_project.breakpointUri, _project.breakpointLine),
      );
122
      await _flutter.waitForPause();
123
      await evaluateComplexExpressions(_flutter);
124
      await _flutter.resume();
125 126 127 128 129 130 131
    });

    test('can evaluate expressions returning complex objects in a test', () async {
      await _flutter.test(
        withDebugger: true,
        beforeStart: () => _flutter.addBreakpoint(_project.breakpointUri, _project.breakpointLine),
      );
132
      await _flutter.waitForPause();
133
      await evaluateComplexReturningExpressions(_flutter);
134
      await _flutter.resume();
135
    });
136
  }, timeout: const Timeout.factor(10), tags: <String>['integration']); // The DevFS sync takes a really long time, so these tests can be slow.
137
}
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

Future<void> evaluateTrivialExpressions(FlutterTestDriver flutter) async {
  InstanceRef res;

  res = await flutter.evaluateInFrame('"test"');
  expect(res.kind == InstanceKind.kString && res.valueAsString == 'test', isTrue);

  res = await flutter.evaluateInFrame('1');
  expect(res.kind == InstanceKind.kInt && res.valueAsString == 1.toString(), isTrue);

  res = await flutter.evaluateInFrame('true');
  expect(res.kind == InstanceKind.kBool && res.valueAsString == true.toString(), isTrue);
}

Future<void> evaluateComplexExpressions(FlutterTestDriver flutter) async {
  final InstanceRef res = await flutter.evaluateInFrame('new DateTime.now().year');
154
  expect(res.kind == InstanceKind.kInt && res.valueAsString == DateTime.now().year.toString(), isTrue);
155 156 157 158
}

Future<void> evaluateComplexReturningExpressions(FlutterTestDriver flutter) async {
  final DateTime now = DateTime.now();
159 160 161 162 163 164 165
  final InstanceRef resp = await flutter.evaluateInFrame('new DateTime.now()');
  expect(resp.classRef.name, equals('DateTime'));
  // Ensure we got a reasonable approximation. The more accurate we try to
  // make this, the more likely it'll fail due to differences in the time
  // in the remote VM and the local VM at the time the code runs.
  final InstanceRef res = await flutter.evaluate(resp.id, r'"$year-$month-$day"');
  expect(res.valueAsString, equals('${now.year}-${now.month}-${now.day}'));
166
}