| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  | import 'dart:io'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-15 22:43:07 +08:00
										 |  |  | import 'package:appflowy/startup/entry_point.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/startup/startup.dart'; | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  | import 'package:appflowy/workspace/application/settings/prelude.dart'; | 
					
						
							|  |  |  | import 'package:flowy_infra/uuid.dart'; | 
					
						
							| 
									
										
										
										
											2023-06-12 14:36:55 +08:00
										 |  |  | import 'package:flowy_infra_ui/flowy_infra_ui.dart'; | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  | import 'package:flutter/gestures.dart'; | 
					
						
							| 
									
										
										
										
											2023-06-15 22:43:07 +08:00
										 |  |  | import 'package:flutter/material.dart'; | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  | import 'package:flutter/services.dart'; | 
					
						
							|  |  |  | import 'package:flutter_test/flutter_test.dart'; | 
					
						
							|  |  |  | import 'package:path_provider/path_provider.dart'; | 
					
						
							| 
									
										
										
										
											2023-07-09 10:03:22 +07:00
										 |  |  | import 'package:path/path.dart' as p; | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  | class FlowyTestContext { | 
					
						
							|  |  |  |   FlowyTestContext({ | 
					
						
							|  |  |  |     required this.applicationDataDirectory, | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  |   final String applicationDataDirectory; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  | extension AppFlowyTestBase on WidgetTester { | 
					
						
							|  |  |  |   Future<FlowyTestContext> initializeAppFlowy({ | 
					
						
							|  |  |  |     // use to append after the application data directory
 | 
					
						
							|  |  |  |     String? pathExtension, | 
					
						
							| 
									
										
										
										
											2023-07-09 10:03:22 +07:00
										 |  |  |     Size windowsSize = const Size(1600, 1200), | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  |   }) async { | 
					
						
							| 
									
										
										
										
											2023-07-09 10:03:22 +07:00
										 |  |  |     binding.setSurfaceSize(windowsSize); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  |     mockHotKeyManagerHandlers(); | 
					
						
							|  |  |  |     final directory = await mockApplicationDataStorage( | 
					
						
							|  |  |  |       pathExtension: pathExtension, | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  |     WidgetsFlutterBinding.ensureInitialized(); | 
					
						
							|  |  |  |     await FlowyRunner.run( | 
					
						
							|  |  |  |       FlowyApp(), | 
					
						
							|  |  |  |       IntegrationMode.integrationTest, | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  |     await wait(3000); | 
					
						
							|  |  |  |     await pumpAndSettle(const Duration(seconds: 2)); | 
					
						
							|  |  |  |     return FlowyTestContext( | 
					
						
							|  |  |  |       applicationDataDirectory: directory, | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  |   void mockHotKeyManagerHandlers() { | 
					
						
							| 
									
										
										
										
											2023-05-28 05:09:39 +01:00
										 |  |  |     TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger | 
					
						
							| 
									
										
										
										
											2023-06-15 22:43:07 +08:00
										 |  |  |         .setMockMethodCallHandler(const MethodChannel('hotkey_manager'), | 
					
						
							|  |  |  |             (MethodCall methodCall) async { | 
					
						
							|  |  |  |       if (methodCall.method == 'unregisterAll') { | 
					
						
							|  |  |  |         // do nothing
 | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-06-15 22:43:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  |   Future<String> mockApplicationDataStorage({ | 
					
						
							|  |  |  |     // use to append after the application data directory
 | 
					
						
							|  |  |  |     String? pathExtension, | 
					
						
							|  |  |  |   }) async { | 
					
						
							|  |  |  |     final dir = await getTemporaryDirectory(); | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  |     // Use a random uuid to avoid conflict.
 | 
					
						
							| 
									
										
										
										
											2023-07-09 10:03:22 +07:00
										 |  |  |     String path = p.join(dir.path, 'appflowy_integration_test', uuid()); | 
					
						
							| 
									
										
										
										
											2023-07-02 23:37:30 +08:00
										 |  |  |     if (pathExtension != null && pathExtension.isNotEmpty) { | 
					
						
							|  |  |  |       path = '$path/$pathExtension'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     final directory = Directory(path); | 
					
						
							|  |  |  |     if (!directory.existsSync()) { | 
					
						
							|  |  |  |       await directory.create(recursive: true); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     MockApplicationDataStorage.initialPath = directory.path; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return directory.path; | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Future<void> tapButton( | 
					
						
							|  |  |  |     Finder finder, { | 
					
						
							|  |  |  |     int? pointer, | 
					
						
							|  |  |  |     int buttons = kPrimaryButton, | 
					
						
							| 
									
										
										
										
											2023-07-09 10:03:22 +07:00
										 |  |  |     bool warnIfMissed = false, | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |     int milliseconds = 500, | 
					
						
							|  |  |  |   }) async { | 
					
						
							| 
									
										
										
										
											2023-06-16 14:32:32 +08:00
										 |  |  |     await tap( | 
					
						
							|  |  |  |       finder, | 
					
						
							| 
									
										
										
										
											2023-06-20 23:48:34 +08:00
										 |  |  |       buttons: buttons, | 
					
						
							| 
									
										
										
										
											2023-06-16 14:32:32 +08:00
										 |  |  |       warnIfMissed: warnIfMissed, | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |     await pumpAndSettle(Duration(milliseconds: milliseconds)); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Future<void> tapButtonWithName( | 
					
						
							|  |  |  |     String tr, { | 
					
						
							|  |  |  |     int milliseconds = 500, | 
					
						
							|  |  |  |   }) async { | 
					
						
							| 
									
										
										
										
											2023-06-12 14:36:55 +08:00
										 |  |  |     Finder button = find.text( | 
					
						
							|  |  |  |       tr, | 
					
						
							|  |  |  |       findRichText: true, | 
					
						
							|  |  |  |       skipOffstage: false, | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     if (button.evaluate().isEmpty) { | 
					
						
							|  |  |  |       button = find.byWidgetPredicate( | 
					
						
							| 
									
										
										
										
											2023-06-16 15:32:28 +08:00
										 |  |  |         (widget) => widget is FlowyText && widget.text == tr, | 
					
						
							| 
									
										
										
										
											2023-06-12 14:36:55 +08:00
										 |  |  |       ); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |     await tapButton( | 
					
						
							|  |  |  |       button, | 
					
						
							|  |  |  |       milliseconds: milliseconds, | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Future<void> tapButtonWithTooltip( | 
					
						
							|  |  |  |     String tr, { | 
					
						
							|  |  |  |     int milliseconds = 500, | 
					
						
							|  |  |  |   }) async { | 
					
						
							|  |  |  |     final button = find.byTooltip(tr); | 
					
						
							|  |  |  |     await tapButton( | 
					
						
							|  |  |  |       button, | 
					
						
							|  |  |  |       milliseconds: milliseconds, | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-09 10:03:22 +07:00
										 |  |  |   Future<void> doubleTapAt( | 
					
						
							|  |  |  |     Offset location, { | 
					
						
							|  |  |  |     int? pointer, | 
					
						
							|  |  |  |     int buttons = kPrimaryButton, | 
					
						
							|  |  |  |     int milliseconds = 500, | 
					
						
							|  |  |  |   }) async { | 
					
						
							|  |  |  |     await tapAt(location, pointer: pointer, buttons: buttons); | 
					
						
							|  |  |  |     await pump(kDoubleTapMinTime); | 
					
						
							|  |  |  |     await tapAt(location, pointer: pointer, buttons: buttons); | 
					
						
							|  |  |  |     await pumpAndSettle(Duration(milliseconds: milliseconds)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-22 23:40:49 +08:00
										 |  |  |   Future<void> doubleTapButton( | 
					
						
							|  |  |  |     Finder finder, { | 
					
						
							|  |  |  |     int? pointer, | 
					
						
							|  |  |  |     int buttons = kPrimaryButton, | 
					
						
							|  |  |  |     bool warnIfMissed = true, | 
					
						
							|  |  |  |     int milliseconds = 500, | 
					
						
							|  |  |  |   }) async { | 
					
						
							| 
									
										
										
										
											2023-07-09 10:03:22 +07:00
										 |  |  |     await tap( | 
					
						
							| 
									
										
										
										
											2023-06-22 23:40:49 +08:00
										 |  |  |       finder, | 
					
						
							|  |  |  |       pointer: pointer, | 
					
						
							|  |  |  |       buttons: buttons, | 
					
						
							|  |  |  |       warnIfMissed: warnIfMissed, | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2023-07-09 10:03:22 +07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     await pump(kDoubleTapMinTime); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     await tap( | 
					
						
							| 
									
										
										
										
											2023-06-22 23:40:49 +08:00
										 |  |  |       finder, | 
					
						
							|  |  |  |       buttons: buttons, | 
					
						
							| 
									
										
										
										
											2023-07-09 10:03:22 +07:00
										 |  |  |       pointer: pointer, | 
					
						
							| 
									
										
										
										
											2023-06-22 23:40:49 +08:00
										 |  |  |       warnIfMissed: warnIfMissed, | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2023-07-09 10:03:22 +07:00
										 |  |  |     await pumpAndSettle(Duration(milliseconds: milliseconds)); | 
					
						
							| 
									
										
										
										
											2023-06-22 23:40:49 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-20 11:14:42 +08:00
										 |  |  |   Future<void> wait(int milliseconds) async { | 
					
						
							|  |  |  |     await pumpAndSettle(Duration(milliseconds: milliseconds)); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-06-15 16:33:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | extension AppFlowyFinderTestBase on CommonFinders { | 
					
						
							|  |  |  |   Finder findTextInFlowyText(String text) { | 
					
						
							|  |  |  |     return find.byWidgetPredicate( | 
					
						
							| 
									
										
										
										
											2023-06-16 15:32:28 +08:00
										 |  |  |       (widget) => widget is FlowyText && widget.text == text, | 
					
						
							| 
									
										
										
										
											2023-06-15 16:33:44 +08:00
										 |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |