From 3b2f9138499fe8741ae9a3f6cb009c5cad514c9e Mon Sep 17 00:00:00 2001 From: done <23427957+sensuikan1973@users.noreply.github.com> Date: Wed, 24 Feb 2021 05:44:46 +0900 Subject: [PATCH] fix unstable widget test on windows/ubuntu (#47) * fix wrong path string * check mounted? See: https://github.com/sensuikan1973/pedax/runs/1963322568 * 200millisec -> 400millisec * use path package --- integration_test/app_test.dart | 4 +-- lib/board/pedax_board.dart | 1 + lib/engine/edax_asset.dart | 5 ++-- lib/engine/options/book_file_option.dart | 5 ++-- lib/engine/options/eval_file_option.dart | 5 ++-- lib/home/book_file_path_setting_dialog.dart | 2 +- test/debug_dump_test.dart | 8 +++--- test/first_launch_app_test.dart | 5 ++-- test/home_test.dart | 30 ++++++++++----------- test_helper/async_delay.dart | 4 +-- test_helper/edax_server.dart | 4 +-- 11 files changed, 36 insertions(+), 37 deletions(-) diff --git a/integration_test/app_test.dart b/integration_test/app_test.dart index ddb972e0e..e81655e07 100644 --- a/integration_test/app_test.dart +++ b/integration_test/app_test.dart @@ -36,12 +36,12 @@ Future main() async { expect(find.byType(Home), findsOneWidget); expect(find.byType(PedaxBoard), findsOneWidget); - await delay200millisec(tester); + await delay400millisec(tester); await tester.pump(); expectStoneNum(tester, SquareType.black, 2); // e4, d5 await tester.tap(findByCoordinate('f5')); - await delay200millisec(tester); + await delay400millisec(tester); await tester.pump(); expectStoneNum(tester, SquareType.black, 4); // e4, d5, e5, f5 }); diff --git a/lib/board/pedax_board.dart b/lib/board/pedax_board.dart index 1699b0d69..164c4d5ea 100644 --- a/lib/board/pedax_board.dart +++ b/lib/board/pedax_board.dart @@ -68,6 +68,7 @@ class _PedaxBoardState extends State { const BookFileOption().val.then( (path) { WidgetsBinding.instance?.addPostFrameCallback((_) { + if (!mounted) return; ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text(AppLocalizations.of(context)!.loadingBookFile, textAlign: TextAlign.center), diff --git a/lib/engine/edax_asset.dart b/lib/engine/edax_asset.dart index 476f24e53..b53968266 100644 --- a/lib/engine/edax_asset.dart +++ b/lib/engine/edax_asset.dart @@ -3,6 +3,7 @@ import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:path_provider/path_provider.dart'; +import 'package:path/path.dart' as p; import 'options/book_file_option.dart'; import 'options/edax_option.dart'; @@ -39,8 +40,8 @@ class EdaxAsset { if (Platform.isMacOS) return defaultLibedaxName; // FIXME: temporary implement. final docDir = await _docDir; - if (Platform.isWindows) return '${docDir.path}/$defaultLibedaxName'; - if (Platform.isLinux) return '${docDir.path}/$defaultLibedaxName'; + if (Platform.isWindows) return p.join(docDir.path, defaultLibedaxName); + if (Platform.isLinux) return p.join(docDir.path, defaultLibedaxName); throw Exception('${Platform.operatingSystem} is not supported'); } diff --git a/lib/engine/options/book_file_option.dart b/lib/engine/options/book_file_option.dart index 96730abee..a52776042 100644 --- a/lib/engine/options/book_file_option.dart +++ b/lib/engine/options/book_file_option.dart @@ -1,5 +1,6 @@ import 'package:flutter/foundation.dart'; import 'package:logger/logger.dart'; +import 'package:path/path.dart' as p; import 'edax_option.dart'; @@ -15,10 +16,10 @@ class BookFileOption extends EdaxOption { String get prefKey => 'bookFilePath'; @override - String get nativeDefaultValue => 'data/$_defaultFileName'; // relative path + String get nativeDefaultValue => p.join('data', _defaultFileName); // relative path @override - Future get appDefaultValue async => '${(await docDir).path}/$_defaultFileName'; + Future get appDefaultValue async => p.join((await docDir).path, _defaultFileName); String get _defaultFileName => 'book.dat'; diff --git a/lib/engine/options/eval_file_option.dart b/lib/engine/options/eval_file_option.dart index bdd9cc69a..ebb2b5a4b 100644 --- a/lib/engine/options/eval_file_option.dart +++ b/lib/engine/options/eval_file_option.dart @@ -1,5 +1,6 @@ import 'package:flutter/foundation.dart'; import 'package:logger/logger.dart'; +import 'package:path/path.dart' as p; import 'edax_option.dart'; @@ -15,10 +16,10 @@ class EvalFileOption extends EdaxOption { String get prefKey => 'evalFilePath'; @override - String get nativeDefaultValue => 'data/$_defaultFileName'; // relative path + String get nativeDefaultValue => p.join('data', _defaultFileName); // relative path @override - Future get appDefaultValue async => '${(await docDir).path}/$_defaultFileName'; + Future get appDefaultValue async => p.join((await docDir).path, _defaultFileName); String get _defaultFileName => 'eval.dat'; diff --git a/lib/home/book_file_path_setting_dialog.dart b/lib/home/book_file_path_setting_dialog.dart index 13a5bfa83..a4da8f05e 100644 --- a/lib/home/book_file_path_setting_dialog.dart +++ b/lib/home/book_file_path_setting_dialog.dart @@ -48,7 +48,7 @@ class BookFilePathSettingDialog extends StatelessWidget { final currentBookFilePath = await _option.val; if (newBookFilePath == currentBookFilePath) return Navigator.pop(context); await _option.update(newBookFilePath); - edaxServerPort.send(BookLoadRequest(newBookFilePath)); + edaxServerPort.send(BookLoadRequest(await _option.val)); Navigator.pop(context); }, child: Text(AppLocalizations.of(context)!.updateSettingOnDialog), diff --git a/test/debug_dump_test.dart b/test/debug_dump_test.dart index f436d2a56..5105ff6f0 100644 --- a/test/debug_dump_test.dart +++ b/test/debug_dump_test.dart @@ -46,7 +46,7 @@ void main() { await tester.pumpAndSettle(); await waitEdaxSetuped(tester); debugDumpApp(); - await delay200millisec(tester); + await delay400millisec(tester); }); }); @@ -59,7 +59,7 @@ void main() { await tester.pumpAndSettle(); await waitEdaxSetuped(tester); debugDumpApp(); - await delay200millisec(tester); + await delay400millisec(tester); }); }); @@ -72,7 +72,7 @@ void main() { await tester.pumpAndSettle(); await waitEdaxSetuped(tester); debugDumpApp(); - await delay200millisec(tester); + await delay400millisec(tester); }); }); @@ -85,7 +85,7 @@ void main() { await tester.pumpAndSettle(); await waitEdaxSetuped(tester); debugDumpApp(); - await delay200millisec(tester); + await delay400millisec(tester); }); }); } diff --git a/test/first_launch_app_test.dart b/test/first_launch_app_test.dart index adb87eda1..0a8424e6a 100644 --- a/test/first_launch_app_test.dart +++ b/test/first_launch_app_test.dart @@ -19,11 +19,10 @@ void main() { expectStoneNum(tester, SquareType.black, 2); // e4, d5 await tester.tap(findByCoordinate('f5')); - // await delay200millisec(tester); - await Future.delayed(const Duration(seconds: 1)); + await delay400millisec(tester); await tester.pump(); expectStoneNum(tester, SquareType.black, 4); // e4, d5, e5, f5 - await delay200millisec(tester); + await delay400millisec(tester); }); }); } diff --git a/test/home_test.dart b/test/home_test.dart index e5e4f8355..9192a91f8 100644 --- a/test/home_test.dart +++ b/test/home_test.dart @@ -31,29 +31,27 @@ Future main() async { expectStoneNum(tester, SquareType.black, 2); // e4, d5 await tester.tap(findByCoordinate('f5')); - // await delay200millisec(tester); - await Future.delayed(const Duration(seconds: 1)); + await delay400millisec(tester); await tester.pump(); expectStoneNum(tester, SquareType.black, 4); // e4, d5, e5, f5 await tester.tap(findByCoordinate('f4')); - await delay200millisec(tester); + await delay400millisec(tester); await tester.pump(); expectStoneNum(tester, SquareType.black, 3); // d5, e5, f5 await tester.tap(findByCoordinate('e3')); - await delay200millisec(tester); + await delay400millisec(tester); await tester.pump(); expectStoneNum(tester, SquareType.black, 5); // e3, e4, d5, e5, f5 await tester.sendKeyEvent(LogicalKeyboardKey.keyU); - await delay200millisec(tester); + await delay400millisec(tester); await tester.pump(); expectStoneNum(tester, SquareType.black, 3); // d5, e5, f5 await tester.sendKeyEvent(LogicalKeyboardKey.keyR); - // await delay200millisec(tester); - await Future.delayed(const Duration(seconds: 1)); + await delay400millisec(tester); await tester.pump(); expectStoneNum(tester, SquareType.black, 5); // e3, e4, d5, e5, f5 @@ -74,7 +72,7 @@ Future main() async { await tester.tap(find.text(l10nEn.license)); await tester.pumpAndSettle(); expect(find.byType(LicensePage), findsOneWidget); - await delay200millisec(tester); + await delay400millisec(tester); }); }); @@ -93,7 +91,7 @@ Future main() async { await tester.tap(find.text(l10nEn.cancelOnDialog)); await tester.pump(); expect(find.byType(PedaxApp), findsOneWidget); - await delay200millisec(tester); + await delay400millisec(tester); }); }); @@ -113,7 +111,7 @@ Future main() async { await tester.tap(find.text(l10nEn.updateSettingOnDialog)); await tester.pumpAndSettle(); expect(find.byType(BookFilePathSettingDialog), findsOneWidget); // nothing happens and dialog isn't closed - await delay200millisec(tester); + await delay400millisec(tester); }); }); @@ -134,7 +132,7 @@ Future main() async { await tester.pumpAndSettle(); await Future.delayed(const Duration(seconds: 1)); expect(find.byType(BookFilePathSettingDialog), findsNothing); - await delay200millisec(tester); + await delay400millisec(tester); }); }); @@ -154,7 +152,7 @@ Future main() async { await tester.pumpAndSettle(); await Future.delayed(const Duration(seconds: 1)); expect(find.byType(BookFilePathSettingDialog), findsNothing); - await delay200millisec(tester); + await delay400millisec(tester); }); }); @@ -172,7 +170,7 @@ Future main() async { await tester.tap(find.text(l10nEn.cancelOnDialog)); await tester.pump(); expect(find.byType(PedaxApp), findsOneWidget); - await delay200millisec(tester); + await delay400millisec(tester); }); }); @@ -193,7 +191,7 @@ Future main() async { await tester.pumpAndSettle(); await Future.delayed(const Duration(seconds: 1)); expect(find.byType(NTasksSettingDialog), findsNothing); - await delay200millisec(tester); + await delay400millisec(tester); }); }); @@ -212,7 +210,7 @@ Future main() async { await tester.tap(find.text(l10nEn.cancelOnDialog)); await tester.pump(); expect(find.byType(PedaxApp), findsOneWidget); - await delay200millisec(tester); + await delay400millisec(tester); }); }); @@ -233,7 +231,7 @@ Future main() async { await tester.pumpAndSettle(); await Future.delayed(const Duration(seconds: 1)); expect(find.byType(LevelSettingDialog), findsNothing); - await delay200millisec(tester); + await delay400millisec(tester); }); }); }); diff --git a/test_helper/async_delay.dart b/test_helper/async_delay.dart index f03f62c7a..4b6aed96b 100644 --- a/test_helper/async_delay.dart +++ b/test_helper/async_delay.dart @@ -1,6 +1,6 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:meta/meta.dart'; -// For EdaxServer: expect move response ~ rendering is less than 200 millisec +// For EdaxServer: expect move response ~ rendering is less than 400 millisec @isTest -Future delay200millisec(WidgetTester tester) async => Future.delayed(const Duration(milliseconds: 200)); +Future delay400millisec(WidgetTester tester) async => Future.delayed(const Duration(milliseconds: 400)); diff --git a/test_helper/edax_server.dart b/test_helper/edax_server.dart index 203314704..fc2e093ad 100644 --- a/test_helper/edax_server.dart +++ b/test_helper/edax_server.dart @@ -1,12 +1,10 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:meta/meta.dart'; -import 'async_delay.dart'; - @isTest Future waitEdaxSetuped(WidgetTester tester) async { await Future.delayed(const Duration(seconds: 1)); await tester.pump(); // after spwaning EdaxServer has completed, render UI. - await delay200millisec(tester); // awit EdaxServer execute edax_init. + await Future.delayed(const Duration(seconds: 1)); // awit EdaxServer execute edax_init. await tester.pump(); // after edax init has completed, render UI. }