Unverified Commit e53c4896 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] remove mock SystemClock (#74994)

parent 8db51f66
...@@ -154,23 +154,19 @@ void main() { ...@@ -154,23 +154,19 @@ void main() {
MemoryFileSystem memoryFileSystem; MemoryFileSystem memoryFileSystem;
MockStdio mockStdio; MockStdio mockStdio;
TestUsage testUsage; TestUsage testUsage;
SystemClock mockClock; FakeClock fakeClock;
Doctor mockDoctor; Doctor mockDoctor;
List<int> mockTimes;
setUp(() { setUp(() {
memoryFileSystem = MemoryFileSystem.test(); memoryFileSystem = MemoryFileSystem.test();
mockStdio = MockStdio(); mockStdio = MockStdio();
testUsage = TestUsage(); testUsage = TestUsage();
mockClock = MockClock(); fakeClock = FakeClock();
mockDoctor = MockDoctor(); mockDoctor = MockDoctor();
when(mockClock.now()).thenAnswer(
(Invocation _) => DateTime.fromMillisecondsSinceEpoch(mockTimes.removeAt(0))
);
}); });
testUsingContext('flutter commands send timing events', () async { testUsingContext('flutter commands send timing events', () async {
mockTimes = <int>[1000, 2000]; fakeClock.times = <int>[1000, 2000];
when(mockDoctor.diagnose( when(mockDoctor.diagnose(
androidLicenses: false, androidLicenses: false,
verbose: false, verbose: false,
...@@ -180,28 +176,25 @@ void main() { ...@@ -180,28 +176,25 @@ void main() {
final CommandRunner<void> runner = createTestCommandRunner(command); final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['doctor']); await runner.run(<String>['doctor']);
verify(mockClock.now()).called(2);
expect(testUsage.timings, contains( expect(testUsage.timings, contains(
const TestTimingEvent( const TestTimingEvent(
'flutter', 'doctor', Duration(milliseconds: 1000), label: 'success', 'flutter', 'doctor', Duration(milliseconds: 1000), label: 'success',
), ),
)); ));
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
SystemClock: () => mockClock, SystemClock: () => fakeClock,
Doctor: () => mockDoctor, Doctor: () => mockDoctor,
Usage: () => testUsage, Usage: () => testUsage,
}); });
testUsingContext('doctor fail sends warning', () async { testUsingContext('doctor fail sends warning', () async {
mockTimes = <int>[1000, 2000]; fakeClock.times = <int>[1000, 2000];
when(mockDoctor.diagnose(androidLicenses: false, verbose: false, androidLicenseValidator: anyNamed('androidLicenseValidator'))) when(mockDoctor.diagnose(androidLicenses: false, verbose: false, androidLicenseValidator: anyNamed('androidLicenseValidator')))
.thenAnswer((_) async => false); .thenAnswer((_) async => false);
final DoctorCommand command = DoctorCommand(); final DoctorCommand command = DoctorCommand();
final CommandRunner<void> runner = createTestCommandRunner(command); final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['doctor']); await runner.run(<String>['doctor']);
verify(mockClock.now()).called(2);
expect(testUsage.timings, contains( expect(testUsage.timings, contains(
const TestTimingEvent( const TestTimingEvent(
...@@ -209,7 +202,7 @@ void main() { ...@@ -209,7 +202,7 @@ void main() {
), ),
)); ));
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
SystemClock: () => mockClock, SystemClock: () => fakeClock,
Doctor: () => mockDoctor, Doctor: () => mockDoctor,
Usage: () => testUsage, Usage: () => testUsage,
}); });
...@@ -233,7 +226,7 @@ void main() { ...@@ -233,7 +226,7 @@ void main() {
testUsingContext('command sends localtime', () async { testUsingContext('command sends localtime', () async {
const int kMillis = 1000; const int kMillis = 1000;
mockTimes = <int>[kMillis]; fakeClock.times = <int>[kMillis];
// Since FLUTTER_ANALYTICS_LOG_FILE is set in the environment, analytics // Since FLUTTER_ANALYTICS_LOG_FILE is set in the environment, analytics
// will be written to a file. // will be written to a file.
final Usage usage = Usage( final Usage usage = Usage(
...@@ -252,7 +245,7 @@ void main() { ...@@ -252,7 +245,7 @@ void main() {
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FileSystem: () => memoryFileSystem, FileSystem: () => memoryFileSystem,
ProcessManager: () => FakeProcessManager.any(), ProcessManager: () => FakeProcessManager.any(),
SystemClock: () => mockClock, SystemClock: () => fakeClock,
Platform: () => FakePlatform( Platform: () => FakePlatform(
environment: <String, String>{ environment: <String, String>{
'FLUTTER_ANALYTICS_LOG_FILE': 'analytics.log', 'FLUTTER_ANALYTICS_LOG_FILE': 'analytics.log',
...@@ -263,7 +256,7 @@ void main() { ...@@ -263,7 +256,7 @@ void main() {
testUsingContext('event sends localtime', () async { testUsingContext('event sends localtime', () async {
const int kMillis = 1000; const int kMillis = 1000;
mockTimes = <int>[kMillis]; fakeClock.times = <int>[kMillis];
// Since FLUTTER_ANALYTICS_LOG_FILE is set in the environment, analytics // Since FLUTTER_ANALYTICS_LOG_FILE is set in the environment, analytics
// will be written to a file. // will be written to a file.
final Usage usage = Usage( final Usage usage = Usage(
...@@ -282,7 +275,7 @@ void main() { ...@@ -282,7 +275,7 @@ void main() {
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FileSystem: () => memoryFileSystem, FileSystem: () => memoryFileSystem,
ProcessManager: () => FakeProcessManager.any(), ProcessManager: () => FakeProcessManager.any(),
SystemClock: () => mockClock, SystemClock: () => fakeClock,
Platform: () => FakePlatform( Platform: () => FakePlatform(
environment: <String, String>{ environment: <String, String>{
'FLUTTER_ANALYTICS_LOG_FILE': 'analytics.log', 'FLUTTER_ANALYTICS_LOG_FILE': 'analytics.log',
...@@ -378,3 +371,12 @@ class FakeFlutterCommand extends FlutterCommand { ...@@ -378,3 +371,12 @@ class FakeFlutterCommand extends FlutterCommand {
class MockDoctor extends Mock implements Doctor {} class MockDoctor extends Mock implements Doctor {}
class MockFlutterConfig extends Mock implements Config {} class MockFlutterConfig extends Mock implements Config {}
class FakeClock extends Fake implements SystemClock {
List<int> times = <int>[];
@override
DateTime now() {
return DateTime.fromMillisecondsSinceEpoch(times.removeAt(0));
}
}
...@@ -37,7 +37,7 @@ void main() { ...@@ -37,7 +37,7 @@ void main() {
MockWebProject webProject; MockWebProject webProject;
MockWindowsProject windowsProject; MockWindowsProject windowsProject;
MockLinuxProject linuxProject; MockLinuxProject linuxProject;
SystemClock mockClock; FakeSystemClock systemClock;
FlutterVersion mockVersion; FlutterVersion mockVersion;
// A Windows-style filesystem. This is not populated by default, so tests // A Windows-style filesystem. This is not populated by default, so tests
// using it instead of fs must re-run any necessary setup (e.g., // using it instead of fs must re-run any necessary setup (e.g.,
...@@ -112,16 +112,14 @@ void main() { ...@@ -112,16 +112,14 @@ void main() {
setUp(() async { setUp(() async {
fs = MemoryFileSystem.test(); fs = MemoryFileSystem.test();
fsWindows = MemoryFileSystem(style: FileSystemStyle.windows); fsWindows = MemoryFileSystem(style: FileSystemStyle.windows);
mockClock = MockClock(); systemClock = FakeSystemClock()
..currentTime = DateTime(1970, 1, 1);
mockVersion = MockFlutterVersion(); mockVersion = MockFlutterVersion();
// Add basic properties to the Flutter project and subprojects // Add basic properties to the Flutter project and subprojects
setUpProject(fs); setUpProject(fs);
flutterProject.directory.childFile('.packages').createSync(recursive: true); flutterProject.directory.childFile('.packages').createSync(recursive: true);
when(mockClock.now()).thenAnswer(
(Invocation _) => DateTime(1970, 1, 1)
);
when(mockVersion.frameworkVersion).thenAnswer( when(mockVersion.frameworkVersion).thenAnswer(
(Invocation _) => '1.0.0' (Invocation _) => '1.0.0'
); );
...@@ -425,9 +423,7 @@ dependencies: ...@@ -425,9 +423,7 @@ dependencies:
when(iosProject.existsSync()).thenReturn(true); when(iosProject.existsSync()).thenReturn(true);
final DateTime dateCreated = DateTime(1970, 1, 1); final DateTime dateCreated = DateTime(1970, 1, 1);
when(mockClock.now()).thenAnswer( systemClock.currentTime = dateCreated;
(Invocation _) => dateCreated
);
const String version = '1.0.0'; const String version = '1.0.0';
when(mockVersion.frameworkVersion).thenAnswer( when(mockVersion.frameworkVersion).thenAnswer(
(Invocation _) => version (Invocation _) => version
...@@ -516,7 +512,7 @@ dependencies: ...@@ -516,7 +512,7 @@ dependencies:
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FileSystem: () => fs, FileSystem: () => fs,
ProcessManager: () => FakeProcessManager.any(), ProcessManager: () => FakeProcessManager.any(),
SystemClock: () => mockClock, SystemClock: () => systemClock,
FlutterVersion: () => mockVersion FlutterVersion: () => mockVersion
}); });
...@@ -533,7 +529,7 @@ dependencies: ...@@ -533,7 +529,7 @@ dependencies:
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FileSystem: () => fs, FileSystem: () => fs,
ProcessManager: () => FakeProcessManager.any(), ProcessManager: () => FakeProcessManager.any(),
SystemClock: () => mockClock, SystemClock: () => systemClock,
FlutterVersion: () => mockVersion FlutterVersion: () => mockVersion
}); });
...@@ -556,7 +552,7 @@ dependencies: ...@@ -556,7 +552,7 @@ dependencies:
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FileSystem: () => fs, FileSystem: () => fs,
ProcessManager: () => FakeProcessManager.any(), ProcessManager: () => FakeProcessManager.any(),
SystemClock: () => mockClock, SystemClock: () => systemClock,
FlutterVersion: () => mockVersion FlutterVersion: () => mockVersion
}); });
}); });
...@@ -1410,3 +1406,12 @@ class MockWebProject extends Mock implements WebProject {} ...@@ -1410,3 +1406,12 @@ class MockWebProject extends Mock implements WebProject {}
class MockWindowsProject extends Mock implements WindowsProject {} class MockWindowsProject extends Mock implements WindowsProject {}
class MockLinuxProject extends Mock implements LinuxProject {} class MockLinuxProject extends Mock implements LinuxProject {}
class MockOperatingSystemUtils extends Mock implements OperatingSystemUtils {} class MockOperatingSystemUtils extends Mock implements OperatingSystemUtils {}
class FakeSystemClock extends Fake implements SystemClock {
DateTime currentTime;
@override
DateTime now() {
return currentTime;
}
}
\ No newline at end of file
...@@ -31,20 +31,16 @@ void main() { ...@@ -31,20 +31,16 @@ void main() {
group('Flutter Command', () { group('Flutter Command', () {
MockitoCache cache; MockitoCache cache;
MockitoUsage usage; MockitoUsage usage;
MockClock clock; FakeClock clock;
MockProcessInfo mockProcessInfo; MockProcessInfo mockProcessInfo;
List<int> mockTimes;
setUp(() { setUp(() {
Cache.disableLocking(); Cache.disableLocking();
cache = MockitoCache(); cache = MockitoCache();
usage = MockitoUsage(); usage = MockitoUsage();
clock = MockClock(); clock = FakeClock();
mockProcessInfo = MockProcessInfo(); mockProcessInfo = MockProcessInfo();
when(clock.now()).thenAnswer(
(Invocation _) => DateTime.fromMillisecondsSinceEpoch(mockTimes.removeAt(0))
);
when(mockProcessInfo.maxRss).thenReturn(10); when(mockProcessInfo.maxRss).thenReturn(10);
}); });
...@@ -154,7 +150,7 @@ void main() { ...@@ -154,7 +150,7 @@ void main() {
testUsingCommandContext('reports command that results in success', () async { testUsingCommandContext('reports command that results in success', () async {
// Crash if called a third time which is unexpected. // Crash if called a third time which is unexpected.
mockTimes = <int>[1000, 2000]; clock.times = <int>[1000, 2000];
final DummyFlutterCommand flutterCommand = DummyFlutterCommand( final DummyFlutterCommand flutterCommand = DummyFlutterCommand(
commandFunction: () async { commandFunction: () async {
...@@ -185,7 +181,7 @@ void main() { ...@@ -185,7 +181,7 @@ void main() {
testUsingCommandContext('reports command that results in warning', () async { testUsingCommandContext('reports command that results in warning', () async {
// Crash if called a third time which is unexpected. // Crash if called a third time which is unexpected.
mockTimes = <int>[1000, 2000]; clock.times = <int>[1000, 2000];
final DummyFlutterCommand flutterCommand = DummyFlutterCommand( final DummyFlutterCommand flutterCommand = DummyFlutterCommand(
commandFunction: () async { commandFunction: () async {
...@@ -216,7 +212,7 @@ void main() { ...@@ -216,7 +212,7 @@ void main() {
testUsingCommandContext('reports command that results in failure', () async { testUsingCommandContext('reports command that results in failure', () async {
// Crash if called a third time which is unexpected. // Crash if called a third time which is unexpected.
mockTimes = <int>[1000, 2000]; clock.times = <int>[1000, 2000];
final DummyFlutterCommand flutterCommand = DummyFlutterCommand( final DummyFlutterCommand flutterCommand = DummyFlutterCommand(
commandFunction: () async { commandFunction: () async {
...@@ -250,7 +246,7 @@ void main() { ...@@ -250,7 +246,7 @@ void main() {
testUsingCommandContext('reports command that results in error', () async { testUsingCommandContext('reports command that results in error', () async {
// Crash if called a third time which is unexpected. // Crash if called a third time which is unexpected.
mockTimes = <int>[1000, 2000]; clock.times = <int>[1000, 2000];
final DummyFlutterCommand flutterCommand = DummyFlutterCommand( final DummyFlutterCommand flutterCommand = DummyFlutterCommand(
commandFunction: () async { commandFunction: () async {
...@@ -348,7 +344,7 @@ void main() { ...@@ -348,7 +344,7 @@ void main() {
testUsingContext('reports command that is killed', () async { testUsingContext('reports command that is killed', () async {
// Crash if called a third time which is unexpected. // Crash if called a third time which is unexpected.
mockTimes = <int>[1000, 2000]; clock.times = <int>[1000, 2000];
final Completer<void> completer = Completer<void>(); final Completer<void> completer = Completer<void>();
setExitFunctionForTests((int exitCode) { setExitFunctionForTests((int exitCode) {
...@@ -398,7 +394,7 @@ void main() { ...@@ -398,7 +394,7 @@ void main() {
}); });
testUsingContext('command release lock on kill signal', () async { testUsingContext('command release lock on kill signal', () async {
mockTimes = <int>[1000, 2000]; clock.times = <int>[1000, 2000];
final Completer<void> completer = Completer<void>(); final Completer<void> completer = Completer<void>();
setExitFunctionForTests((int exitCode) { setExitFunctionForTests((int exitCode) {
expect(exitCode, 0); expect(exitCode, 0);
...@@ -437,11 +433,10 @@ void main() { ...@@ -437,11 +433,10 @@ void main() {
testUsingCommandContext('report execution timing by default', () async { testUsingCommandContext('report execution timing by default', () async {
// Crash if called a third time which is unexpected. // Crash if called a third time which is unexpected.
mockTimes = <int>[1000, 2000]; clock.times = <int>[1000, 2000];
final DummyFlutterCommand flutterCommand = DummyFlutterCommand(); final DummyFlutterCommand flutterCommand = DummyFlutterCommand();
await flutterCommand.run(); await flutterCommand.run();
verify(clock.now()).called(2);
expect( expect(
verify(usage.sendTiming( verify(usage.sendTiming(
...@@ -458,12 +453,11 @@ void main() { ...@@ -458,12 +453,11 @@ void main() {
testUsingCommandContext('no timing report without usagePath', () async { testUsingCommandContext('no timing report without usagePath', () async {
// Crash if called a third time which is unexpected. // Crash if called a third time which is unexpected.
mockTimes = <int>[1000, 2000]; clock.times = <int>[1000, 2000];
final DummyFlutterCommand flutterCommand = final DummyFlutterCommand flutterCommand =
DummyFlutterCommand(noUsagePath: true); DummyFlutterCommand(noUsagePath: true);
await flutterCommand.run(); await flutterCommand.run();
verify(clock.now()).called(2);
verifyNever(usage.sendTiming( verifyNever(usage.sendTiming(
any, any, any, any, any, any,
label: anyNamed('label'))); label: anyNamed('label')));
...@@ -471,7 +465,7 @@ void main() { ...@@ -471,7 +465,7 @@ void main() {
testUsingCommandContext('report additional FlutterCommandResult data', () async { testUsingCommandContext('report additional FlutterCommandResult data', () async {
// Crash if called a third time which is unexpected. // Crash if called a third time which is unexpected.
mockTimes = <int>[1000, 2000]; clock.times = <int>[1000, 2000];
final FlutterCommandResult commandResult = FlutterCommandResult( final FlutterCommandResult commandResult = FlutterCommandResult(
ExitStatus.success, ExitStatus.success,
...@@ -484,7 +478,6 @@ void main() { ...@@ -484,7 +478,6 @@ void main() {
commandFunction: () async => commandResult commandFunction: () async => commandResult
); );
await flutterCommand.run(); await flutterCommand.run();
verify(clock.now()).called(2);
expect( expect(
verify(usage.sendTiming( verify(usage.sendTiming(
captureAny, captureAny, captureAny, captureAny, captureAny, captureAny,
...@@ -500,7 +493,7 @@ void main() { ...@@ -500,7 +493,7 @@ void main() {
testUsingCommandContext('report failed execution timing too', () async { testUsingCommandContext('report failed execution timing too', () async {
// Crash if called a third time which is unexpected. // Crash if called a third time which is unexpected.
mockTimes = <int>[1000, 2000]; clock.times = <int>[1000, 2000];
final DummyFlutterCommand flutterCommand = DummyFlutterCommand( final DummyFlutterCommand flutterCommand = DummyFlutterCommand(
commandFunction: () async { commandFunction: () async {
...@@ -513,8 +506,6 @@ void main() { ...@@ -513,8 +506,6 @@ void main() {
await flutterCommand.run(); await flutterCommand.run();
fail('Mock should make this fail'); fail('Mock should make this fail');
} on ToolExit { } on ToolExit {
// Should have still checked time twice.
verify(clock.now()).called(2);
expect( expect(
verify(usage.sendTiming( verify(usage.sendTiming(
...@@ -694,3 +685,12 @@ class FakePub extends Fake implements Pub { ...@@ -694,3 +685,12 @@ class FakePub extends Fake implements Pub {
bool checkUpToDate = false, bool checkUpToDate = false,
}) async { } }) async { }
} }
class FakeClock extends Fake implements SystemClock {
List<int> times = <int>[];
@override
DateTime now() {
return DateTime.fromMillisecondsSinceEpoch(times.removeAt(0));
}
}
...@@ -18,7 +18,6 @@ import 'package:flutter_tools/src/base/process.dart'; ...@@ -18,7 +18,6 @@ import 'package:flutter_tools/src/base/process.dart';
import 'package:flutter_tools/src/base/signals.dart'; import 'package:flutter_tools/src/base/signals.dart';
import 'package:flutter_tools/src/base/template.dart'; import 'package:flutter_tools/src/base/template.dart';
import 'package:flutter_tools/src/base/terminal.dart'; import 'package:flutter_tools/src/base/terminal.dart';
import 'package:flutter_tools/src/base/time.dart';
import 'package:flutter_tools/src/build_info.dart'; import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/isolated/mustache_template.dart'; import 'package:flutter_tools/src/isolated/mustache_template.dart';
import 'package:flutter_tools/src/cache.dart'; import 'package:flutter_tools/src/cache.dart';
...@@ -421,8 +420,6 @@ class FakeXcodeProjectInterpreter implements XcodeProjectInterpreter { ...@@ -421,8 +420,6 @@ class FakeXcodeProjectInterpreter implements XcodeProjectInterpreter {
class MockFlutterVersion extends Mock implements FlutterVersion {} class MockFlutterVersion extends Mock implements FlutterVersion {}
class MockClock extends Mock implements SystemClock {}
class MockHttpClient extends Mock implements HttpClient {} class MockHttpClient extends Mock implements HttpClient {}
class MockCrashReporter extends Mock implements CrashReporter {} class MockCrashReporter extends Mock implements CrashReporter {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment