2023-03-03 18:31:30 +08:00
|
|
|
import 'package:appflowy/plugins/database_view/application/cell/cell_controller.dart';
|
|
|
|
import 'package:appflowy/plugins/database_view/application/cell/cell_controller_builder.dart';
|
2023-02-26 16:27:17 +08:00
|
|
|
import 'package:appflowy/plugins/database_view/application/field/field_controller.dart';
|
|
|
|
import 'package:appflowy/plugins/database_view/application/field/field_editor_bloc.dart';
|
|
|
|
import 'package:appflowy/plugins/database_view/application/field/field_service.dart';
|
|
|
|
import 'package:appflowy/plugins/database_view/application/field/type_option/type_option_context.dart';
|
2023-06-04 09:28:13 +08:00
|
|
|
import 'package:appflowy/plugins/database_view/application/field/type_option/type_option_service.dart';
|
2023-02-26 16:27:17 +08:00
|
|
|
import 'package:appflowy/plugins/database_view/application/row/row_cache.dart';
|
|
|
|
import 'package:appflowy/plugins/database_view/application/row/row_data_controller.dart';
|
2023-03-08 21:19:44 +08:00
|
|
|
import 'package:appflowy/plugins/database_view/application/database_controller.dart';
|
2023-02-26 16:27:17 +08:00
|
|
|
import 'package:appflowy/plugins/database_view/grid/application/row/row_bloc.dart';
|
|
|
|
import 'package:appflowy/plugins/database_view/grid/grid.dart';
|
2023-06-01 20:23:27 +08:00
|
|
|
import 'package:appflowy/workspace/application/view/view_service.dart';
|
2023-04-28 14:08:53 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-database2/row_entities.pb.dart';
|
2023-03-08 21:19:44 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-error/errors.pbserver.dart';
|
2023-04-04 08:41:16 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-folder2/view.pb.dart';
|
2023-04-28 14:08:53 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-database2/field_entities.pb.dart';
|
2023-03-08 21:19:44 +08:00
|
|
|
import 'package:dartz/dartz.dart';
|
2022-10-16 16:51:21 +08:00
|
|
|
|
|
|
|
import '../../util.dart';
|
|
|
|
|
2022-11-17 16:44:17 +08:00
|
|
|
class GridTestContext {
|
|
|
|
final ViewPB gridView;
|
2023-02-26 16:27:17 +08:00
|
|
|
final DatabaseController gridController;
|
2022-10-16 16:51:21 +08:00
|
|
|
|
2022-11-27 15:38:02 +08:00
|
|
|
GridTestContext(this.gridView, this.gridController);
|
2022-10-16 16:51:21 +08:00
|
|
|
|
2022-10-26 22:36:34 +08:00
|
|
|
List<RowInfo> get rowInfos {
|
2023-03-20 21:16:37 +08:00
|
|
|
return gridController.rowCache.rowInfos;
|
2022-10-26 22:36:34 +08:00
|
|
|
}
|
|
|
|
|
2022-11-26 21:28:08 +08:00
|
|
|
List<FieldInfo> get fieldContexts => fieldController.fieldInfos;
|
2022-10-26 22:36:34 +08:00
|
|
|
|
2023-02-26 16:27:17 +08:00
|
|
|
FieldController get fieldController {
|
2022-11-27 15:38:02 +08:00
|
|
|
return gridController.fieldController;
|
2022-10-26 22:36:34 +08:00
|
|
|
}
|
2022-10-23 16:44:10 +08:00
|
|
|
|
2023-06-14 22:16:33 +08:00
|
|
|
Future<Either<RowMetaPB, FlowyError>> createRow() async {
|
2022-11-27 15:38:02 +08:00
|
|
|
return gridController.createRow();
|
2022-10-26 22:36:34 +08:00
|
|
|
}
|
|
|
|
|
2023-02-26 16:27:17 +08:00
|
|
|
Future<CellController> makeCellController(
|
2023-04-10 15:10:42 +08:00
|
|
|
String fieldId,
|
|
|
|
int rowIndex,
|
|
|
|
) async {
|
2022-11-27 16:53:42 +08:00
|
|
|
final builder = await makeCellControllerBuilder(fieldId, rowIndex);
|
2022-10-27 14:11:15 +08:00
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
2023-02-26 16:27:17 +08:00
|
|
|
Future<CellControllerBuilder> makeCellControllerBuilder(
|
2022-10-27 14:11:15 +08:00
|
|
|
String fieldId,
|
2022-11-27 16:53:42 +08:00
|
|
|
int rowIndex,
|
2022-10-27 14:11:15 +08:00
|
|
|
) async {
|
2022-11-27 16:53:42 +08:00
|
|
|
final RowInfo rowInfo = rowInfos[rowIndex];
|
2022-12-11 11:35:42 +08:00
|
|
|
final rowCache = gridController.rowCache;
|
2022-10-27 14:11:15 +08:00
|
|
|
|
2023-03-09 09:51:04 +08:00
|
|
|
final rowDataController = RowController(
|
2023-06-14 22:16:33 +08:00
|
|
|
rowMeta: rowInfo.rowMeta,
|
2023-03-08 21:19:44 +08:00
|
|
|
viewId: rowInfo.viewId,
|
2022-12-11 11:35:42 +08:00
|
|
|
rowCache: rowCache,
|
2022-10-27 14:11:15 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
final rowBloc = RowBloc(
|
2023-06-14 22:16:33 +08:00
|
|
|
viewId: rowInfo.viewId,
|
2022-10-27 14:11:15 +08:00
|
|
|
dataController: rowDataController,
|
2023-06-14 22:16:33 +08:00
|
|
|
rowId: rowInfo.rowMeta.id,
|
2022-10-27 14:11:15 +08:00
|
|
|
)..add(const RowEvent.initial());
|
2022-10-26 22:36:34 +08:00
|
|
|
await gridResponseFuture();
|
2022-10-27 14:11:15 +08:00
|
|
|
|
2023-02-26 16:27:17 +08:00
|
|
|
return CellControllerBuilder(
|
2023-06-04 09:28:13 +08:00
|
|
|
cellContext: rowBloc.state.cellByFieldId[fieldId]!,
|
2022-10-27 14:11:15 +08:00
|
|
|
cellCache: rowCache.cellCache,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<FieldEditorBloc> createField(FieldType fieldType) async {
|
2023-06-04 09:28:13 +08:00
|
|
|
final editorBloc = await createFieldEditor(viewId: gridView.id)
|
2022-10-27 14:11:15 +08:00
|
|
|
..add(const FieldEditorEvent.initial());
|
2022-10-26 22:36:34 +08:00
|
|
|
await gridResponseFuture();
|
2022-10-27 14:11:15 +08:00
|
|
|
editorBloc.add(FieldEditorEvent.switchToField(fieldType));
|
|
|
|
await gridResponseFuture();
|
|
|
|
return Future(() => editorBloc);
|
2022-10-23 16:44:10 +08:00
|
|
|
}
|
|
|
|
|
2022-11-26 21:28:08 +08:00
|
|
|
FieldInfo singleSelectFieldContext() {
|
|
|
|
final fieldInfo = fieldContexts
|
2022-10-23 16:44:10 +08:00
|
|
|
.firstWhere((element) => element.fieldType == FieldType.SingleSelect);
|
2022-11-26 21:28:08 +08:00
|
|
|
return fieldInfo;
|
2022-10-23 16:44:10 +08:00
|
|
|
}
|
|
|
|
|
2023-06-04 09:28:13 +08:00
|
|
|
FieldContext singleSelectFieldCellContext() {
|
2022-10-23 16:44:10 +08:00
|
|
|
final field = singleSelectFieldContext().field;
|
2023-06-04 09:28:13 +08:00
|
|
|
return FieldContext(viewId: gridView.id, field: field);
|
2022-10-23 16:44:10 +08:00
|
|
|
}
|
|
|
|
|
2022-11-26 21:28:08 +08:00
|
|
|
FieldInfo textFieldContext() {
|
|
|
|
final fieldInfo = fieldContexts
|
2022-11-15 23:17:01 +08:00
|
|
|
.firstWhere((element) => element.fieldType == FieldType.RichText);
|
2022-11-26 21:28:08 +08:00
|
|
|
return fieldInfo;
|
2022-11-15 23:17:01 +08:00
|
|
|
}
|
|
|
|
|
2022-11-26 21:28:08 +08:00
|
|
|
FieldInfo checkboxFieldContext() {
|
|
|
|
final fieldInfo = fieldContexts
|
2022-11-17 16:44:17 +08:00
|
|
|
.firstWhere((element) => element.fieldType == FieldType.Checkbox);
|
2022-11-26 21:28:08 +08:00
|
|
|
return fieldInfo;
|
|
|
|
}
|
|
|
|
|
2023-02-26 16:27:17 +08:00
|
|
|
Future<SelectOptionCellController> makeSelectOptionCellController(
|
2023-04-10 15:10:42 +08:00
|
|
|
FieldType fieldType,
|
|
|
|
int rowIndex,
|
|
|
|
) async {
|
|
|
|
assert(
|
|
|
|
fieldType == FieldType.SingleSelect || fieldType == FieldType.MultiSelect,
|
|
|
|
);
|
2022-11-26 21:28:08 +08:00
|
|
|
|
|
|
|
final field =
|
|
|
|
fieldContexts.firstWhere((element) => element.fieldType == fieldType);
|
2022-11-27 16:53:42 +08:00
|
|
|
final cellController = await makeCellController(field.id, rowIndex)
|
2023-02-26 16:27:17 +08:00
|
|
|
as SelectOptionCellController;
|
2022-11-26 21:28:08 +08:00
|
|
|
return cellController;
|
|
|
|
}
|
|
|
|
|
2023-02-26 16:27:17 +08:00
|
|
|
Future<TextCellController> makeTextCellController(int rowIndex) async {
|
2022-11-26 21:28:08 +08:00
|
|
|
final field = fieldContexts
|
|
|
|
.firstWhere((element) => element.fieldType == FieldType.RichText);
|
|
|
|
final cellController =
|
2023-02-26 16:27:17 +08:00
|
|
|
await makeCellController(field.id, rowIndex) as TextCellController;
|
2022-11-26 21:28:08 +08:00
|
|
|
return cellController;
|
2022-10-26 22:36:34 +08:00
|
|
|
}
|
2022-11-28 19:41:57 +08:00
|
|
|
|
2023-02-26 16:27:17 +08:00
|
|
|
Future<TextCellController> makeCheckboxCellController(int rowIndex) async {
|
2022-11-28 19:41:57 +08:00
|
|
|
final field = fieldContexts
|
|
|
|
.firstWhere((element) => element.fieldType == FieldType.Checkbox);
|
|
|
|
final cellController =
|
2023-02-26 16:27:17 +08:00
|
|
|
await makeCellController(field.id, rowIndex) as TextCellController;
|
2022-11-28 19:41:57 +08:00
|
|
|
return cellController;
|
|
|
|
}
|
2022-11-17 16:44:17 +08:00
|
|
|
}
|
|
|
|
|
2023-06-04 09:28:13 +08:00
|
|
|
Future<FieldEditorBloc> createFieldEditor({
|
|
|
|
required String viewId,
|
|
|
|
}) async {
|
|
|
|
final result = await TypeOptionBackendService.createFieldTypeOption(
|
|
|
|
viewId: viewId,
|
|
|
|
);
|
|
|
|
return result.fold(
|
|
|
|
(data) {
|
|
|
|
final loader = FieldTypeOptionLoader(
|
|
|
|
viewId: viewId,
|
|
|
|
field: data.field_2,
|
|
|
|
);
|
|
|
|
return FieldEditorBloc(
|
|
|
|
isGroupField: FieldInfo(field: data.field_2).isGroupField,
|
|
|
|
loader: loader,
|
|
|
|
field: data.field_2,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
(err) => throw Exception(err),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-17 16:44:17 +08:00
|
|
|
/// Create a empty Grid for test
|
|
|
|
class AppFlowyGridTest {
|
|
|
|
final AppFlowyUnitTest unitTest;
|
|
|
|
|
|
|
|
AppFlowyGridTest({required this.unitTest});
|
2022-10-26 22:36:34 +08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-17 16:44:17 +08:00
|
|
|
Future<GridTestContext> createTestGrid() async {
|
2022-10-26 22:36:34 +08:00
|
|
|
final app = await unitTest.createTestApp();
|
2022-11-17 16:44:17 +08:00
|
|
|
final builder = GridPluginBuilder();
|
2023-06-01 20:23:27 +08:00
|
|
|
final context = await ViewBackendService.createView(
|
2023-05-31 14:08:54 +08:00
|
|
|
parentViewId: app.id,
|
2022-11-17 16:44:17 +08:00
|
|
|
name: "Test Grid",
|
2022-10-26 22:36:34 +08:00
|
|
|
layoutType: builder.layoutType!,
|
2023-06-12 12:57:01 +08:00
|
|
|
openAfterCreate: true,
|
2023-06-01 20:23:27 +08:00
|
|
|
).then((result) {
|
2022-11-17 16:44:17 +08:00
|
|
|
return result.fold(
|
|
|
|
(view) async {
|
2023-03-08 21:19:44 +08:00
|
|
|
final context = GridTestContext(
|
2023-04-10 15:10:42 +08:00
|
|
|
view,
|
2023-06-01 20:23:27 +08:00
|
|
|
DatabaseController(view: view),
|
2023-04-10 15:10:42 +08:00
|
|
|
);
|
2023-03-08 21:19:44 +08:00
|
|
|
final result = await context.gridController.open();
|
2022-11-17 16:44:17 +08:00
|
|
|
result.fold((l) => null, (r) => throw Exception(r));
|
|
|
|
return context;
|
|
|
|
},
|
|
|
|
(error) {
|
|
|
|
throw Exception();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return context;
|
2022-10-16 16:51:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 11:52:15 +08:00
|
|
|
/// Create a new Grid for cell test
|
2022-10-17 10:31:56 +08:00
|
|
|
class AppFlowyGridCellTest {
|
2022-11-17 16:44:17 +08:00
|
|
|
late GridTestContext context;
|
2022-10-27 14:11:15 +08:00
|
|
|
final AppFlowyGridTest gridTest;
|
|
|
|
AppFlowyGridCellTest({required this.gridTest});
|
2022-10-17 10:31:56 +08:00
|
|
|
|
|
|
|
static Future<AppFlowyGridCellTest> ensureInitialized() async {
|
|
|
|
final gridTest = await AppFlowyGridTest.ensureInitialized();
|
2022-10-27 14:11:15 +08:00
|
|
|
return AppFlowyGridCellTest(gridTest: gridTest);
|
2022-10-23 11:52:15 +08:00
|
|
|
}
|
|
|
|
|
2022-10-23 16:44:10 +08:00
|
|
|
Future<void> createTestGrid() async {
|
2022-11-17 16:44:17 +08:00
|
|
|
context = await gridTest.createTestGrid();
|
2022-10-23 16:44:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> createTestRow() async {
|
2022-11-17 16:44:17 +08:00
|
|
|
await context.createRow();
|
2022-10-23 16:44:10 +08:00
|
|
|
}
|
|
|
|
|
2023-02-26 16:27:17 +08:00
|
|
|
Future<SelectOptionCellController> makeSelectOptionCellController(
|
2023-04-10 15:10:42 +08:00
|
|
|
FieldType fieldType,
|
|
|
|
int rowIndex,
|
|
|
|
) async {
|
2023-02-06 15:59:30 +08:00
|
|
|
return await context.makeSelectOptionCellController(fieldType, rowIndex);
|
2022-10-23 16:44:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-27 16:53:42 +08:00
|
|
|
Future<void> gridResponseFuture({int milliseconds = 200}) {
|
2022-11-17 16:44:17 +08:00
|
|
|
return Future.delayed(gridResponseDuration(milliseconds: milliseconds));
|
2022-10-16 16:51:21 +08:00
|
|
|
}
|
|
|
|
|
2022-10-23 11:52:15 +08:00
|
|
|
Duration gridResponseDuration({int milliseconds = 200}) {
|
2022-10-17 10:31:56 +08:00
|
|
|
return Duration(milliseconds: milliseconds);
|
2022-10-16 16:51:21 +08:00
|
|
|
}
|