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

5 6
// @dart = 2.8

7
import 'package:file/file.dart';
8

9
import 'package:vm_service/vm_service.dart';
10

11
import '../src/common.dart';
12
import 'test_data/basic_project.dart';
13
import 'test_data/tests_project.dart';
14
import 'test_driver.dart';
15
import 'test_utils.dart';
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
void batch1() {
  final BasicProject _project = BasicProject();
  Directory tempDir;
  FlutterRunTestDriver _flutter;

  Future<void> initProject() async {
    tempDir = createResolvedTempDirectorySync('run_expression_eval_test.');
    await _project.setUpIn(tempDir);
    _flutter = FlutterRunTestDriver(tempDir);
  }

  Future<void> cleanProject() async {
    await _flutter.stop();
    tryToDelete(tempDir);
  }

  Future<void> breakInBuildMethod(FlutterTestDriver flutter) async {
    await _flutter.breakAt(
      _project.buildMethodBreakpointUri,
      _project.buildMethodBreakpointLine,
    );
  }

  Future<void> breakInTopLevelFunction(FlutterTestDriver flutter) async {
    await _flutter.breakAt(
      _project.topLevelFunctionBreakpointUri,
      _project.topLevelFunctionBreakpointLine,
    );
  }

47
  testWithoutContext('flutter run expression evaluation - can evaluate trivial expressions in top level function', () async {
48 49 50 51 52 53 54
    await initProject();
    await _flutter.run(withDebugger: true);
    await breakInTopLevelFunction(_flutter);
    await evaluateTrivialExpressions(_flutter);
    await cleanProject();
  });

55
  testWithoutContext('flutter run expression evaluation - can evaluate trivial expressions in build method', () async {
56 57 58 59 60 61 62
    await initProject();
    await _flutter.run(withDebugger: true);
    await breakInBuildMethod(_flutter);
    await evaluateTrivialExpressions(_flutter);
    await cleanProject();
  });

63
  testWithoutContext('flutter run expression evaluation - can evaluate complex expressions in top level function', () async {
64 65 66 67 68 69 70
    await initProject();
    await _flutter.run(withDebugger: true);
    await breakInTopLevelFunction(_flutter);
    await evaluateComplexExpressions(_flutter);
    await cleanProject();
  });

71
  testWithoutContext('flutter run expression evaluation - can evaluate complex expressions in build method', () async {
72 73 74 75 76 77 78
    await initProject();
    await _flutter.run(withDebugger: true);
    await breakInBuildMethod(_flutter);
    await evaluateComplexExpressions(_flutter);
    await cleanProject();
  });

79
  testWithoutContext('flutter run expression evaluation - can evaluate expressions returning complex objects in top level function', () async {
80 81 82 83 84 85 86
    await initProject();
    await _flutter.run(withDebugger: true);
    await breakInTopLevelFunction(_flutter);
    await evaluateComplexReturningExpressions(_flutter);
    await cleanProject();
  });

87
  testWithoutContext('flutter run expression evaluation - can evaluate expressions returning complex objects in build method', () async {
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    await initProject();
    await _flutter.run(withDebugger: true);
    await breakInBuildMethod(_flutter);
    await evaluateComplexReturningExpressions(_flutter);
    await cleanProject();
  });
}

void batch2() {
  final TestsProject _project = TestsProject();
  Directory tempDir;
  FlutterTestTestDriver _flutter;

  Future<void> initProject() async {
    tempDir = createResolvedTempDirectorySync('test_expression_eval_test.');
    await _project.setUpIn(tempDir);
    _flutter = FlutterTestTestDriver(tempDir);
  }

  Future<void> cleanProject() async {
108
    await _flutter?.waitForCompletion();
109 110 111
    tryToDelete(tempDir);
  }

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

123
  testWithoutContext('flutter test expression evaluation - can evaluate complex expressions in a test', () async {
124 125 126 127 128 129 130 131 132 133
    await initProject();
    await _flutter.test(
      withDebugger: true,
      beforeStart: () => _flutter.addBreakpoint(_project.breakpointUri, _project.breakpointLine),
    );
    await _flutter.waitForPause();
    await evaluateComplexExpressions(_flutter);
    await cleanProject();
  });

134
  testWithoutContext('flutter test expression evaluation - can evaluate expressions returning complex objects in a test', () async {
135 136 137 138 139 140 141 142 143
    await initProject();
    await _flutter.test(
      withDebugger: true,
      beforeStart: () => _flutter.addBreakpoint(_project.breakpointUri, _project.breakpointLine),
    );
    await _flutter.waitForPause();
    await evaluateComplexReturningExpressions(_flutter);
    await cleanProject();
  });
144
}
145 146

Future<void> evaluateTrivialExpressions(FlutterTestDriver flutter) async {
147
  ObjRef res;
148 149

  res = await flutter.evaluateInFrame('"test"');
150
  expectValueOfType(res, InstanceKind.kString, 'test');
151 152

  res = await flutter.evaluateInFrame('1');
153
  expectValueOfType(res, InstanceKind.kInt, 1.toString());
154 155

  res = await flutter.evaluateInFrame('true');
156
  expectValueOfType(res, InstanceKind.kBool, true.toString());
157 158 159
}

Future<void> evaluateComplexExpressions(FlutterTestDriver flutter) async {
160 161
  final ObjRef res = await flutter.evaluateInFrame('new DateTime.now().year');
  expectValueOfType(res, InstanceKind.kInt, DateTime.now().year.toString());
162 163 164 165
}

Future<void> evaluateComplexReturningExpressions(FlutterTestDriver flutter) async {
  final DateTime now = DateTime.now();
166 167
  final ObjRef resp = await flutter.evaluateInFrame('new DateTime.now()');
  expectInstanceOfClass(resp, 'DateTime');
168 169 170 171
  // 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"');
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  expectValue(res, '${now.year}-${now.month}-${now.day}');
}

void expectInstanceOfClass(ObjRef result, String name) {
  expect(result,
    const TypeMatcher<InstanceRef>()
      .having((InstanceRef instance) => instance.classRef.name, 'resp.classRef.name', name));
}

void expectValueOfType(ObjRef result, String kind, String message) {
  expect(result,
    const TypeMatcher<InstanceRef>()
      .having((InstanceRef instance) => instance.kind, 'kind', kind)
      .having((InstanceRef instance) => instance.valueAsString, 'valueAsString', message));
}

void expectValue(ObjRef result, String message) {
  expect(result,
    const TypeMatcher<InstanceRef>()
      .having((InstanceRef instance) => instance.valueAsString, 'valueAsString', message));
192
}
193 194 195 196 197

void main() {
  batch1();
  batch2();
}