2023-02-26 16:27:17 +08:00
|
|
|
import 'package:appflowy/startup/launch_configuration.dart';
|
|
|
|
import 'package:appflowy/startup/startup.dart';
|
2023-05-21 18:53:59 +08:00
|
|
|
import 'package:appflowy/user/application/auth/auth_service.dart';
|
2023-02-26 16:27:17 +08:00
|
|
|
import 'package:appflowy/user/application/user_service.dart';
|
|
|
|
import 'package:appflowy/workspace/application/workspace/workspace_service.dart';
|
2023-12-31 07:29:40 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
|
|
|
|
import 'package:appflowy_backend/protobuf/flowy-folder/workspace.pb.dart';
|
2023-01-08 12:10:53 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart';
|
2023-12-20 10:08:35 +08:00
|
|
|
import 'package:flowy_infra/uuid.dart';
|
2022-10-15 23:34:56 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
|
2022-10-16 16:51:21 +08:00
|
|
|
class AppFlowyUnitTest {
|
|
|
|
late UserProfilePB userProfile;
|
2023-02-26 16:27:17 +08:00
|
|
|
late UserBackendService userService;
|
2022-10-16 16:51:21 +08:00
|
|
|
late WorkspaceService workspaceService;
|
2023-11-01 11:45:35 +08:00
|
|
|
late WorkspacePB workspace;
|
2022-10-16 16:51:21 +08:00
|
|
|
|
|
|
|
static Future<AppFlowyUnitTest> ensureInitialized() async {
|
2022-10-15 23:34:56 +08:00
|
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
SharedPreferences.setMockInitialValues({});
|
2022-10-16 16:51:21 +08:00
|
|
|
_pathProviderInitialized();
|
2022-10-15 23:34:56 +08:00
|
|
|
|
2023-06-15 22:43:07 +08:00
|
|
|
await FlowyRunner.run(
|
2023-12-26 02:03:42 +08:00
|
|
|
AppFlowyApplicationUniTest(),
|
2023-07-02 23:37:30 +08:00
|
|
|
IntegrationMode.unitTest,
|
2023-06-15 22:43:07 +08:00
|
|
|
);
|
2022-10-16 16:51:21 +08:00
|
|
|
|
|
|
|
final test = AppFlowyUnitTest();
|
|
|
|
await test._signIn();
|
|
|
|
await test._loadWorkspace();
|
|
|
|
|
|
|
|
await test._initialServices();
|
|
|
|
return test;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _signIn() async {
|
|
|
|
final authService = getIt<AuthService>();
|
|
|
|
const password = "AppFlowy123@";
|
|
|
|
final uid = uuid();
|
|
|
|
final userEmail = "$uid@appflowy.io";
|
|
|
|
final result = await authService.signUp(
|
|
|
|
name: "TestUser",
|
|
|
|
password: password,
|
|
|
|
email: userEmail,
|
|
|
|
);
|
2023-05-24 10:45:28 +08:00
|
|
|
result.fold(
|
2022-10-16 16:51:21 +08:00
|
|
|
(user) {
|
|
|
|
userProfile = user;
|
2023-02-26 16:27:17 +08:00
|
|
|
userService = UserBackendService(userId: userProfile.id);
|
2022-10-16 16:51:21 +08:00
|
|
|
},
|
2024-02-24 20:54:10 +07:00
|
|
|
(error) {
|
|
|
|
assert(false, 'Error: $error');
|
|
|
|
},
|
2022-10-16 16:51:21 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-01 11:45:35 +08:00
|
|
|
WorkspacePB get currentWorkspace => workspace;
|
2022-10-16 16:51:21 +08:00
|
|
|
Future<void> _loadWorkspace() async {
|
2024-06-27 09:57:06 +08:00
|
|
|
final result = await UserBackendService.getCurrentWorkspace();
|
2022-10-16 16:51:21 +08:00
|
|
|
result.fold(
|
2023-11-01 11:45:35 +08:00
|
|
|
(value) => workspace = value,
|
2022-10-16 16:51:21 +08:00
|
|
|
(error) {
|
|
|
|
throw Exception(error);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _initialServices() async {
|
|
|
|
workspaceService = WorkspaceService(workspaceId: currentWorkspace.id);
|
|
|
|
}
|
|
|
|
|
2023-12-20 10:08:35 +08:00
|
|
|
Future<ViewPB> createWorkspace() async {
|
2024-03-21 11:02:03 +07:00
|
|
|
final result = await workspaceService.createView(
|
|
|
|
name: "Test App",
|
|
|
|
viewSection: ViewSectionPB.Public,
|
|
|
|
);
|
2022-10-16 16:51:21 +08:00
|
|
|
return result.fold(
|
|
|
|
(app) => app,
|
|
|
|
(error) => throw Exception(error),
|
|
|
|
);
|
2022-10-15 23:34:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-16 16:51:21 +08:00
|
|
|
void _pathProviderInitialized() {
|
2022-10-15 23:34:56 +08:00
|
|
|
const MethodChannel channel =
|
|
|
|
MethodChannel('plugins.flutter.io/path_provider');
|
2023-05-28 05:09:39 +01:00
|
|
|
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
|
|
|
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
|
|
|
|
return '.';
|
2022-10-15 23:34:56 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-26 02:03:42 +08:00
|
|
|
class AppFlowyApplicationUniTest implements EntryPoint {
|
2022-10-15 23:34:56 +08:00
|
|
|
@override
|
2022-12-20 11:14:42 +08:00
|
|
|
Widget create(LaunchConfiguration config) {
|
2024-01-25 16:37:36 +01:00
|
|
|
return const SizedBox.shrink();
|
2022-10-15 23:34:56 +08:00
|
|
|
}
|
|
|
|
}
|
2022-10-16 16:51:21 +08:00
|
|
|
|
2022-10-26 22:36:34 +08:00
|
|
|
Future<void> blocResponseFuture({int millisecond = 200}) {
|
2022-10-25 16:51:51 +08:00
|
|
|
return Future.delayed(Duration(milliseconds: millisecond));
|
2022-10-23 17:52:17 +08:00
|
|
|
}
|
|
|
|
|
2022-10-26 22:36:34 +08:00
|
|
|
Duration blocResponseDuration({int milliseconds = 200}) {
|
2022-10-25 16:51:51 +08:00
|
|
|
return Duration(milliseconds: milliseconds);
|
2022-10-16 16:51:21 +08:00
|
|
|
}
|