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