2024-10-07 16:41:20 +08:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2024-01-05 17:30:54 +08:00
|
|
|
import 'package:appflowy/plugins/database/application/cell/cell_controller.dart';
|
|
|
|
import 'package:appflowy/plugins/database/application/cell/cell_controller_builder.dart';
|
|
|
|
import 'package:appflowy/plugins/database/application/field/field_controller.dart';
|
|
|
|
import 'package:appflowy/plugins/database/application/field/field_editor_bloc.dart';
|
2024-02-25 18:38:18 +08:00
|
|
|
import 'package:appflowy/plugins/database/domain/field_service.dart';
|
2024-01-05 17:30:54 +08:00
|
|
|
import 'package:appflowy/plugins/database/application/row/row_cache.dart';
|
|
|
|
import 'package:appflowy/plugins/database/application/database_controller.dart';
|
2024-10-07 16:41:20 +08:00
|
|
|
import 'package:appflowy/workspace/application/settings/share/import_service.dart';
|
2023-06-01 20:23:27 +08:00
|
|
|
import 'package:appflowy/workspace/application/view/view_service.dart';
|
2024-10-07 16:41:20 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-folder/protobuf.dart';
|
2023-04-28 14:08:53 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-database2/field_entities.pb.dart';
|
2024-10-07 16:41:20 +08:00
|
|
|
import 'package:appflowy_result/appflowy_result.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
2022-10-16 16:51:21 +08:00
|
|
|
|
|
|
|
import '../../util.dart';
|
|
|
|
|
2024-10-07 16:41:20 +08:00
|
|
|
const v020GridFileName = "v020.afdb";
|
|
|
|
const v069GridFileName = "v069.afdb";
|
|
|
|
|
2022-11-17 16:44:17 +08:00
|
|
|
class GridTestContext {
|
2024-10-07 16:41:20 +08:00
|
|
|
GridTestContext(this.view, this.databaseController);
|
2024-01-25 16:37:36 +01:00
|
|
|
|
2024-10-07 16:41:20 +08:00
|
|
|
final ViewPB view;
|
|
|
|
final DatabaseController databaseController;
|
2022-10-16 16:51:21 +08:00
|
|
|
|
2024-10-10 11:38:17 +08:00
|
|
|
String get viewId => view.id;
|
|
|
|
|
2022-10-26 22:36:34 +08:00
|
|
|
List<RowInfo> get rowInfos {
|
2024-10-07 16:41:20 +08:00
|
|
|
return databaseController.rowCache.rowInfos;
|
2022-10-26 22:36:34 +08:00
|
|
|
}
|
|
|
|
|
2024-10-07 16:41:20 +08:00
|
|
|
FieldController get fieldController => databaseController.fieldController;
|
2022-10-23 16:44:10 +08:00
|
|
|
|
2022-10-27 14:11:15 +08:00
|
|
|
Future<FieldEditorBloc> createField(FieldType fieldType) async {
|
2023-11-23 16:43:29 +08:00
|
|
|
final editorBloc =
|
2024-10-07 16:41:20 +08:00
|
|
|
await createFieldEditor(databaseController: databaseController);
|
2022-10-26 22:36:34 +08:00
|
|
|
await gridResponseFuture();
|
2023-11-23 16:43:29 +08:00
|
|
|
editorBloc.add(FieldEditorEvent.switchFieldType(fieldType));
|
2022-10-27 14:11:15 +08:00
|
|
|
await gridResponseFuture();
|
2024-10-07 16:41:20 +08:00
|
|
|
return editorBloc;
|
2022-11-15 23:17:01 +08:00
|
|
|
}
|
|
|
|
|
2024-10-07 16:41:20 +08:00
|
|
|
CellController makeGridCellController(int fieldIndex, int rowIndex) {
|
2024-01-24 23:59:45 +08:00
|
|
|
return makeCellController(
|
2024-10-07 16:41:20 +08:00
|
|
|
databaseController,
|
|
|
|
CellContext(
|
|
|
|
fieldId: fieldController.fieldInfos[fieldIndex].id,
|
|
|
|
rowId: rowInfos[rowIndex].rowId,
|
|
|
|
),
|
2024-01-24 23:59:45 +08:00
|
|
|
).as();
|
2022-11-28 19:41:57 +08:00
|
|
|
}
|
2022-11-17 16:44:17 +08:00
|
|
|
}
|
|
|
|
|
2023-06-04 09:28:13 +08:00
|
|
|
Future<FieldEditorBloc> createFieldEditor({
|
2023-11-23 16:43:29 +08:00
|
|
|
required DatabaseController databaseController,
|
2023-06-04 09:28:13 +08:00
|
|
|
}) async {
|
2023-12-11 11:19:20 +08:00
|
|
|
final result = await FieldBackendService.createField(
|
2023-11-23 16:43:29 +08:00
|
|
|
viewId: databaseController.viewId,
|
2023-06-04 09:28:13 +08:00
|
|
|
);
|
2023-11-23 16:43:29 +08:00
|
|
|
await gridResponseFuture();
|
2023-06-04 09:28:13 +08:00
|
|
|
return result.fold(
|
2023-12-20 11:11:38 +08:00
|
|
|
(field) {
|
2023-06-04 09:28:13 +08:00
|
|
|
return FieldEditorBloc(
|
2023-11-23 16:43:29 +08:00
|
|
|
viewId: databaseController.viewId,
|
|
|
|
fieldController: databaseController.fieldController,
|
2024-09-05 13:54:50 +08:00
|
|
|
fieldInfo: databaseController.fieldController.getField(field.id)!,
|
2024-09-02 13:54:21 +08:00
|
|
|
isNew: true,
|
2023-06-04 09:28:13 +08:00
|
|
|
);
|
|
|
|
},
|
|
|
|
(err) => throw Exception(err),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-17 16:44:17 +08:00
|
|
|
/// Create a empty Grid for test
|
|
|
|
class AppFlowyGridTest {
|
|
|
|
AppFlowyGridTest({required this.unitTest});
|
2022-10-26 22:36:34 +08:00
|
|
|
|
2024-01-25 16:37:36 +01:00
|
|
|
final AppFlowyUnitTest unitTest;
|
|
|
|
|
2022-11-17 16:44:17 +08:00
|
|
|
static Future<AppFlowyGridTest> ensureInitialized() async {
|
|
|
|
final inner = await AppFlowyUnitTest.ensureInitialized();
|
|
|
|
return AppFlowyGridTest(unitTest: inner);
|
2022-11-10 20:22:37 +08:00
|
|
|
}
|
|
|
|
|
2024-10-07 16:41:20 +08:00
|
|
|
Future<GridTestContext> makeDefaultTestGrid() async {
|
|
|
|
final workspace = await unitTest.createWorkspace();
|
2023-06-01 20:23:27 +08:00
|
|
|
final context = await ViewBackendService.createView(
|
2024-10-07 16:41:20 +08:00
|
|
|
parentViewId: workspace.id,
|
2022-11-17 16:44:17 +08:00
|
|
|
name: "Test Grid",
|
2023-06-20 23:48:34 +08:00
|
|
|
layoutType: ViewLayoutPB.Grid,
|
2023-06-12 12:57:01 +08:00
|
|
|
openAfterCreate: true,
|
2024-10-07 16:41:20 +08:00
|
|
|
).fold(
|
|
|
|
(view) async {
|
|
|
|
final databaseController = DatabaseController(view: view);
|
|
|
|
await databaseController
|
|
|
|
.open()
|
|
|
|
.fold((l) => null, (r) => throw Exception(r));
|
|
|
|
return GridTestContext(
|
|
|
|
view,
|
|
|
|
databaseController,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
(error) => throw Exception(),
|
|
|
|
);
|
2022-11-17 16:44:17 +08:00
|
|
|
|
|
|
|
return context;
|
2022-10-16 16:51:21 +08:00
|
|
|
}
|
2022-10-17 10:31:56 +08:00
|
|
|
|
2024-10-07 16:41:20 +08:00
|
|
|
Future<GridTestContext> makeTestGridFromImportedData(
|
|
|
|
String fileName,
|
|
|
|
) async {
|
|
|
|
final workspace = await unitTest.createWorkspace();
|
|
|
|
|
|
|
|
// Don't use the p.join to build the path that used in loadString. It
|
|
|
|
// is not working on windows.
|
|
|
|
final data = await rootBundle
|
|
|
|
.loadString("assets/test/workspaces/database/$fileName");
|
|
|
|
|
|
|
|
final context = await ImportBackendService.importPages(
|
|
|
|
workspace.id,
|
|
|
|
[
|
2024-12-08 18:25:25 +08:00
|
|
|
ImportItemPayloadPB()
|
2024-10-07 16:41:20 +08:00
|
|
|
..name = fileName
|
|
|
|
..data = utf8.encode(data)
|
|
|
|
..viewLayout = ViewLayoutPB.Grid
|
2024-10-14 20:38:04 +08:00
|
|
|
..importType = ImportTypePB.AFDatabase,
|
2024-10-07 16:41:20 +08:00
|
|
|
],
|
|
|
|
).fold(
|
|
|
|
(views) async {
|
|
|
|
final view = views.items.first;
|
|
|
|
final databaseController = DatabaseController(view: view);
|
|
|
|
await databaseController
|
|
|
|
.open()
|
|
|
|
.fold((l) => null, (r) => throw Exception(r));
|
|
|
|
return GridTestContext(
|
|
|
|
view,
|
|
|
|
databaseController,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
(err) => throw Exception(),
|
|
|
|
);
|
2022-10-23 16:44:10 +08:00
|
|
|
|
2024-10-07 16:41:20 +08:00
|
|
|
return context;
|
2022-10-23 16:44:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-07 16:41:20 +08:00
|
|
|
Future<void> gridResponseFuture({int milliseconds = 300}) {
|
|
|
|
return Future.delayed(
|
|
|
|
gridResponseDuration(milliseconds: milliseconds),
|
|
|
|
);
|
2022-10-16 16:51:21 +08:00
|
|
|
}
|
|
|
|
|
2024-10-07 16:41:20 +08:00
|
|
|
Duration gridResponseDuration({int milliseconds = 300}) {
|
2022-10-17 10:31:56 +08:00
|
|
|
return Duration(milliseconds: milliseconds);
|
2022-10-16 16:51:21 +08:00
|
|
|
}
|