|
3 | 3 | // found in the LICENSE file.
|
4 | 4 |
|
5 | 5 | import 'dart:convert';
|
| 6 | +import 'dart:io' as io; |
6 | 7 |
|
7 | 8 | import 'package:args/command_runner.dart';
|
| 9 | +import 'package:fake_async/fake_async.dart'; |
8 | 10 | import 'package:file/file.dart';
|
9 | 11 | import 'package:file/memory.dart';
|
10 | 12 | import 'package:flutter_plugin_tools/src/common/core.dart';
|
11 | 13 | import 'package:flutter_plugin_tools/src/common/plugin_utils.dart';
|
12 | 14 | import 'package:flutter_plugin_tools/src/drive_examples_command.dart';
|
| 15 | +import 'package:mockito/mockito.dart'; |
13 | 16 | import 'package:platform/platform.dart';
|
14 | 17 | import 'package:test/test.dart';
|
15 | 18 |
|
@@ -387,6 +390,82 @@ void main() {
|
387 | 390 | ]));
|
388 | 391 | });
|
389 | 392 |
|
| 393 | + test('saves a screenshot if test is taking too long', () async { |
| 394 | + setMockFlutterDevicesOutput(); |
| 395 | + final RepositoryPackage plugin = createFakePlugin( |
| 396 | + 'plugin', |
| 397 | + packagesDir, |
| 398 | + extraFiles: <String>[ |
| 399 | + 'example/integration_test/bar_test.dart', |
| 400 | + 'example/ios/ios.m', |
| 401 | + ], |
| 402 | + platformSupport: <String, PlatformDetails>{ |
| 403 | + platformAndroid: const PlatformDetails(PlatformSupport.inline), |
| 404 | + platformIOS: const PlatformDetails(PlatformSupport.inline), |
| 405 | + }, |
| 406 | + ); |
| 407 | + |
| 408 | + final FakeAsync fakeAsync = FakeAsync(); |
| 409 | + processRunner.mockProcessesForExecutable['flutter']! |
| 410 | + .addAll(<FakeProcessInfo>[ |
| 411 | + FakeProcessInfo( |
| 412 | + _FakeDelayingProcess( |
| 413 | + delayDuration: const Duration(minutes: 11), |
| 414 | + fakeAsync: fakeAsync), |
| 415 | + <String>['test']), |
| 416 | + FakeProcessInfo(MockProcess(), <String>['screenshot']), |
| 417 | + ]); |
| 418 | + |
| 419 | + final Directory pluginExampleDirectory = getExampleDir(plugin); |
| 420 | + |
| 421 | + List<String> output = <String>[]; |
| 422 | + fakeAsync.run((_) { |
| 423 | + () async { |
| 424 | + output = await runCapturingPrint( |
| 425 | + runner, <String>['drive-examples', '--ios']); |
| 426 | + }(); |
| 427 | + }); |
| 428 | + fakeAsync.flushTimers(); |
| 429 | + |
| 430 | + expect( |
| 431 | + output, |
| 432 | + containsAllInOrder(<Matcher>[ |
| 433 | + contains('Running for plugin'), |
| 434 | + contains( |
| 435 | + 'Test is taking a long time, taking screenshot test-timeout-screenshot_integration_test.png...'), |
| 436 | + contains('No issues found!'), |
| 437 | + ]), |
| 438 | + ); |
| 439 | + |
| 440 | + expect( |
| 441 | + processRunner.recordedCalls, |
| 442 | + orderedEquals(<ProcessCall>[ |
| 443 | + ProcessCall(getFlutterCommand(mockPlatform), |
| 444 | + const <String>['devices', '--machine'], null), |
| 445 | + ProcessCall( |
| 446 | + getFlutterCommand(mockPlatform), |
| 447 | + const <String>[ |
| 448 | + 'test', |
| 449 | + '-d', |
| 450 | + _fakeIOSDevice, |
| 451 | + '--debug-logs-dir=/path/to/logs', |
| 452 | + 'integration_test', |
| 453 | + ], |
| 454 | + pluginExampleDirectory.path, |
| 455 | + ), |
| 456 | + ProcessCall( |
| 457 | + getFlutterCommand(mockPlatform), |
| 458 | + const <String>[ |
| 459 | + 'screenshot', |
| 460 | + '-d', |
| 461 | + _fakeIOSDevice, |
| 462 | + '--out=/path/to/logs/test-timeout-screenshot_integration_test.png', |
| 463 | + ], |
| 464 | + pluginExampleDirectory.path, |
| 465 | + ), |
| 466 | + ])); |
| 467 | + }); |
| 468 | + |
390 | 469 | test('driving when plugin does not support Linux is a no-op', () async {
|
391 | 470 | createFakePlugin('plugin', packagesDir, extraFiles: <String>[
|
392 | 471 | 'example/integration_test/plugin_test.dart',
|
@@ -1634,3 +1713,20 @@ void main() {
|
1634 | 1713 | });
|
1635 | 1714 | });
|
1636 | 1715 | }
|
| 1716 | + |
| 1717 | +class _FakeDelayingProcess extends Fake implements io.Process { |
| 1718 | + /// Creates a mock process that takes [delayDuration] time to exit successfully. |
| 1719 | + _FakeDelayingProcess( |
| 1720 | + {required Duration delayDuration, required FakeAsync fakeAsync}) |
| 1721 | + : _delayDuration = delayDuration, |
| 1722 | + _fakeAsync = fakeAsync; |
| 1723 | + |
| 1724 | + final Duration _delayDuration; |
| 1725 | + final FakeAsync _fakeAsync; |
| 1726 | + |
| 1727 | + @override |
| 1728 | + Future<int> get exitCode async { |
| 1729 | + _fakeAsync.elapse(_delayDuration); |
| 1730 | + return 0; |
| 1731 | + } |
| 1732 | +} |
0 commit comments