2023-09-14 11:22:32 +00:00
|
|
|
import 'dart:async';
|
2022-12-20 03:14:42 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2024-06-02 12:52:20 +00:00
|
|
|
import 'package:appflowy_editor_plugins/appflowy_editor_plugins.dart';
|
2024-04-30 12:09:08 +00:00
|
|
|
import 'package:flutter/gestures.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
2023-11-25 09:18:31 +00:00
|
|
|
import 'package:appflowy/env/cloud_env.dart';
|
|
|
|
import 'package:appflowy/env/cloud_env_test.dart';
|
2023-06-15 14:43:07 +00:00
|
|
|
import 'package:appflowy/startup/entry_point.dart';
|
|
|
|
import 'package:appflowy/startup/startup.dart';
|
2023-11-28 23:49:47 +00:00
|
|
|
import 'package:appflowy/user/application/auth/af_cloud_mock_auth_service.dart';
|
|
|
|
import 'package:appflowy/user/application/auth/auth_service.dart';
|
|
|
|
import 'package:appflowy/user/application/auth/supabase_mock_auth_service.dart';
|
2023-09-14 11:22:32 +00:00
|
|
|
import 'package:appflowy/user/presentation/presentation.dart';
|
2023-11-02 07:24:17 +00:00
|
|
|
import 'package:appflowy/user/presentation/screens/sign_in_screen/widgets/widgets.dart';
|
2023-07-02 15:37:30 +00:00
|
|
|
import 'package:appflowy/workspace/application/settings/prelude.dart';
|
|
|
|
import 'package:flowy_infra/uuid.dart';
|
2023-06-12 06:36:55 +00:00
|
|
|
import 'package:flowy_infra_ui/flowy_infra_ui.dart';
|
2022-12-20 03:14:42 +00:00
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
2023-07-09 03:03:22 +00:00
|
|
|
import 'package:path/path.dart' as p;
|
2023-09-14 11:22:32 +00:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2022-12-20 03:14:42 +00:00
|
|
|
|
2023-07-02 15:37:30 +00:00
|
|
|
class FlowyTestContext {
|
2024-04-30 12:09:08 +00:00
|
|
|
FlowyTestContext({required this.applicationDataDirectory});
|
2022-12-20 03:14:42 +00:00
|
|
|
|
2023-07-02 15:37:30 +00:00
|
|
|
final String applicationDataDirectory;
|
|
|
|
}
|
2022-12-20 03:14:42 +00:00
|
|
|
|
2023-07-02 15:37:30 +00:00
|
|
|
extension AppFlowyTestBase on WidgetTester {
|
|
|
|
Future<FlowyTestContext> initializeAppFlowy({
|
|
|
|
// use to append after the application data directory
|
|
|
|
String? pathExtension,
|
2023-12-25 18:03:42 +00:00
|
|
|
// use to specify the application data directory, if not specified, a temporary directory will be used.
|
|
|
|
String? dataDirectory,
|
2024-01-29 02:26:45 +00:00
|
|
|
Size windowSize = const Size(1600, 1200),
|
2023-11-28 02:54:31 +00:00
|
|
|
AuthenticatorType? cloudType,
|
2023-12-21 00:12:40 +00:00
|
|
|
String? email,
|
2023-07-02 15:37:30 +00:00
|
|
|
}) async {
|
2024-06-02 12:52:20 +00:00
|
|
|
VideoBlockKit.ensureInitialized();
|
|
|
|
|
2024-03-05 09:20:27 +00:00
|
|
|
if (Platform.isLinux || Platform.isWindows || Platform.isMacOS) {
|
|
|
|
// Set the window size
|
|
|
|
await binding.setSurfaceSize(windowSize);
|
|
|
|
}
|
2023-07-09 03:03:22 +00:00
|
|
|
|
2023-07-02 15:37:30 +00:00
|
|
|
mockHotKeyManagerHandlers();
|
2023-12-25 18:03:42 +00:00
|
|
|
final applicationDataDirectory = dataDirectory ??
|
|
|
|
await mockApplicationDataStorage(
|
|
|
|
pathExtension: pathExtension,
|
|
|
|
);
|
2023-11-25 09:18:31 +00:00
|
|
|
|
2023-07-02 15:37:30 +00:00
|
|
|
await FlowyRunner.run(
|
2023-12-25 18:03:42 +00:00
|
|
|
AppFlowyApplication(),
|
2023-07-02 15:37:30 +00:00
|
|
|
IntegrationMode.integrationTest,
|
2023-11-28 02:54:31 +00:00
|
|
|
rustEnvsBuilder: () {
|
|
|
|
final rustEnvs = <String, String>{};
|
|
|
|
if (cloudType != null) {
|
|
|
|
switch (cloudType) {
|
|
|
|
case AuthenticatorType.local:
|
|
|
|
break;
|
|
|
|
case AuthenticatorType.supabase:
|
|
|
|
break;
|
2024-01-12 06:34:59 +00:00
|
|
|
case AuthenticatorType.appflowyCloudSelfHost:
|
|
|
|
rustEnvs["GOTRUE_ADMIN_EMAIL"] = "admin@example.com";
|
|
|
|
rustEnvs["GOTRUE_ADMIN_PASSWORD"] = "password";
|
|
|
|
break;
|
2024-01-15 04:53:53 +00:00
|
|
|
default:
|
|
|
|
throw Exception("not supported");
|
2023-11-28 02:54:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rustEnvs;
|
|
|
|
},
|
2023-11-28 23:49:47 +00:00
|
|
|
didInitGetItCallback: () {
|
|
|
|
return Future(
|
|
|
|
() async {
|
|
|
|
if (cloudType != null) {
|
|
|
|
switch (cloudType) {
|
|
|
|
case AuthenticatorType.local:
|
2024-04-30 12:09:08 +00:00
|
|
|
await useLocalServer();
|
2023-11-28 23:49:47 +00:00
|
|
|
break;
|
|
|
|
case AuthenticatorType.supabase:
|
2024-01-30 01:33:34 +00:00
|
|
|
await useTestSupabaseCloud();
|
2023-11-28 23:49:47 +00:00
|
|
|
getIt.unregister<AuthService>();
|
|
|
|
getIt.registerFactory<AuthService>(
|
|
|
|
() => SupabaseMockAuthService(),
|
|
|
|
);
|
|
|
|
break;
|
2024-01-12 06:34:59 +00:00
|
|
|
case AuthenticatorType.appflowyCloudSelfHost:
|
2024-01-30 01:33:34 +00:00
|
|
|
await useTestSelfHostedAppFlowyCloud();
|
2024-01-12 06:34:59 +00:00
|
|
|
getIt.unregister<AuthService>();
|
|
|
|
getIt.registerFactory<AuthService>(
|
|
|
|
() => AppFlowyCloudMockAuthService(email: email),
|
|
|
|
);
|
2024-01-15 04:53:53 +00:00
|
|
|
default:
|
|
|
|
throw Exception("not supported");
|
2023-11-28 23:49:47 +00:00
|
|
|
}
|
2023-11-25 09:18:31 +00:00
|
|
|
}
|
2023-11-28 23:49:47 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
2023-07-02 15:37:30 +00:00
|
|
|
);
|
2023-12-25 18:03:42 +00:00
|
|
|
|
2023-09-14 11:22:32 +00:00
|
|
|
await waitUntilSignInPageShow();
|
2023-07-02 15:37:30 +00:00
|
|
|
return FlowyTestContext(
|
2023-12-25 18:03:42 +00:00
|
|
|
applicationDataDirectory: applicationDataDirectory,
|
2023-07-02 15:37:30 +00:00
|
|
|
);
|
2022-12-20 03:14:42 +00:00
|
|
|
}
|
|
|
|
|
2023-07-02 15:37:30 +00:00
|
|
|
void mockHotKeyManagerHandlers() {
|
2023-05-28 04:09:39 +00:00
|
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
2023-06-15 14:43:07 +00:00
|
|
|
.setMockMethodCallHandler(const MethodChannel('hotkey_manager'),
|
|
|
|
(MethodCall methodCall) async {
|
|
|
|
if (methodCall.method == 'unregisterAll') {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
});
|
2023-07-02 15:37:30 +00:00
|
|
|
}
|
2023-06-15 14:43:07 +00:00
|
|
|
|
2023-09-14 11:22:32 +00:00
|
|
|
Future<void> waitUntilSignInPageShow() async {
|
2023-11-24 03:54:47 +00:00
|
|
|
if (isAuthEnabled) {
|
2024-04-11 08:33:28 +00:00
|
|
|
final finder = find.byType(SignInAnonymousButtonV2);
|
2024-03-05 09:20:27 +00:00
|
|
|
await pumpUntilFound(finder, timeout: const Duration(seconds: 30));
|
2023-11-02 07:24:17 +00:00
|
|
|
expect(finder, findsOneWidget);
|
|
|
|
} else {
|
|
|
|
final finder = find.byType(GoButton);
|
|
|
|
await pumpUntilFound(finder);
|
|
|
|
expect(finder, findsOneWidget);
|
|
|
|
}
|
2023-09-14 11:22:32 +00:00
|
|
|
}
|
|
|
|
|
2023-12-25 18:03:42 +00:00
|
|
|
Future<void> waitForSeconds(int seconds) async {
|
2024-01-25 15:37:36 +00:00
|
|
|
await Future.delayed(Duration(seconds: seconds), () {});
|
2023-12-25 18:03:42 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 11:22:32 +00:00
|
|
|
Future<void> pumpUntilFound(
|
|
|
|
Finder finder, {
|
|
|
|
Duration timeout = const Duration(seconds: 10),
|
2024-03-05 05:51:03 +00:00
|
|
|
Duration pumpInterval = const Duration(
|
|
|
|
milliseconds: 50,
|
|
|
|
), // Interval between pumps
|
2023-09-14 11:22:32 +00:00
|
|
|
}) async {
|
|
|
|
bool timerDone = false;
|
|
|
|
final timer = Timer(timeout, () => timerDone = true);
|
2023-12-25 18:03:42 +00:00
|
|
|
while (!timerDone) {
|
|
|
|
await pump(pumpInterval); // Pump with an interval
|
2023-09-14 11:22:32 +00:00
|
|
|
if (any(finder)) {
|
2023-12-25 18:03:42 +00:00
|
|
|
break;
|
2023-09-14 11:22:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
timer.cancel();
|
|
|
|
}
|
|
|
|
|
2024-03-05 05:51:03 +00:00
|
|
|
Future<void> pumpUntilNotFound(
|
|
|
|
Finder finder, {
|
|
|
|
Duration timeout = const Duration(seconds: 10),
|
|
|
|
Duration pumpInterval = const Duration(
|
|
|
|
milliseconds: 50,
|
|
|
|
), // Interval between pumps
|
|
|
|
}) async {
|
|
|
|
bool timerDone = false;
|
|
|
|
final timer = Timer(timeout, () => timerDone = true);
|
|
|
|
while (!timerDone) {
|
|
|
|
await pump(pumpInterval); // Pump with an interval
|
|
|
|
if (!any(finder)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
timer.cancel();
|
|
|
|
}
|
|
|
|
|
2022-12-20 03:14:42 +00:00
|
|
|
Future<void> tapButton(
|
|
|
|
Finder finder, {
|
|
|
|
int? pointer,
|
|
|
|
int buttons = kPrimaryButton,
|
2023-07-09 03:03:22 +00:00
|
|
|
bool warnIfMissed = false,
|
2022-12-20 03:14:42 +00:00
|
|
|
int milliseconds = 500,
|
2024-05-10 14:08:32 +00:00
|
|
|
bool pumpAndSettle = true,
|
2022-12-20 03:14:42 +00:00
|
|
|
}) async {
|
2023-06-16 06:32:32 +00:00
|
|
|
await tap(
|
|
|
|
finder,
|
2023-06-20 15:48:34 +00:00
|
|
|
buttons: buttons,
|
2023-06-16 06:32:32 +00:00
|
|
|
warnIfMissed: warnIfMissed,
|
|
|
|
);
|
2024-05-10 14:08:32 +00:00
|
|
|
|
|
|
|
if (pumpAndSettle) {
|
|
|
|
await this.pumpAndSettle(
|
|
|
|
Duration(milliseconds: milliseconds),
|
|
|
|
EnginePhase.sendSemanticsUpdate,
|
|
|
|
const Duration(seconds: 5),
|
|
|
|
);
|
|
|
|
}
|
2022-12-20 03:14:42 +00:00
|
|
|
}
|
|
|
|
|
2024-05-10 14:08:32 +00:00
|
|
|
Future<void> tapButtonWithName(
|
|
|
|
String tr, {
|
|
|
|
int milliseconds = 500,
|
|
|
|
bool pumpAndSettle = true,
|
|
|
|
}) async {
|
2024-04-30 12:09:08 +00:00
|
|
|
Finder button = find.text(tr, findRichText: true, skipOffstage: false);
|
2023-06-12 06:36:55 +00:00
|
|
|
if (button.evaluate().isEmpty) {
|
|
|
|
button = find.byWidgetPredicate(
|
2023-06-16 07:32:28 +00:00
|
|
|
(widget) => widget is FlowyText && widget.text == tr,
|
2023-06-12 06:36:55 +00:00
|
|
|
);
|
|
|
|
}
|
2024-05-10 14:08:32 +00:00
|
|
|
await tapButton(
|
|
|
|
button,
|
|
|
|
milliseconds: milliseconds,
|
|
|
|
pumpAndSettle: pumpAndSettle,
|
|
|
|
);
|
2022-12-20 03:14:42 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 03:03:22 +00: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));
|
|
|
|
}
|
|
|
|
|
2022-12-20 03:14:42 +00:00
|
|
|
Future<void> wait(int milliseconds) async {
|
|
|
|
await pumpAndSettle(Duration(milliseconds: milliseconds));
|
|
|
|
}
|
|
|
|
}
|
2023-06-15 08:33:44 +00:00
|
|
|
|
|
|
|
extension AppFlowyFinderTestBase on CommonFinders {
|
|
|
|
Finder findTextInFlowyText(String text) {
|
|
|
|
return find.byWidgetPredicate(
|
2023-06-16 07:32:28 +00:00
|
|
|
(widget) => widget is FlowyText && widget.text == text,
|
2023-06-15 08:33:44 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-11-25 09:18:31 +00:00
|
|
|
|
2024-01-30 01:33:34 +00:00
|
|
|
Future<void> useTestSupabaseCloud() async {
|
|
|
|
await useSupabaseCloud(
|
|
|
|
url: TestEnv.supabaseUrl,
|
|
|
|
anonKey: TestEnv.supabaseAnonKey,
|
2023-11-25 09:18:31 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-01-30 01:33:34 +00:00
|
|
|
Future<void> useTestSelfHostedAppFlowyCloud() async {
|
|
|
|
await useSelfHostedAppFlowyCloudWithURL(TestEnv.afCloudUrl);
|
2023-11-25 09:18:31 +00:00
|
|
|
}
|
2023-12-25 18:03:42 +00:00
|
|
|
|
|
|
|
Future<String> mockApplicationDataStorage({
|
|
|
|
// use to append after the application data directory
|
|
|
|
String? pathExtension,
|
|
|
|
}) async {
|
|
|
|
final dir = await getTemporaryDirectory();
|
|
|
|
|
|
|
|
// Use a random uuid to avoid conflict.
|
|
|
|
String path = p.join(dir.path, 'appflowy_integration_test', uuid());
|
|
|
|
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;
|
|
|
|
}
|