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';
|
|
|
|
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';
|
|
|
|
import 'package:appflowy/workspace/application/app/app_service.dart';
|
2023-04-28 14:08:53 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-database2/row_entities.pb.dart';
|
|
|
|
import 'package:appflowy_backend/protobuf/flowy-database2/setting_entities.pbenum.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-03-08 21:19:44 +08:00
|
|
|
Future<Either<RowPB, FlowyError>> createRow() async {
|
2022-11-27 15:38:02 +08:00
|
|
|
return gridController.createRow();
|
2022-10-26 22:36:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
FieldEditorBloc createFieldEditor({
|
2022-11-26 21:28:08 +08:00
|
|
|
FieldInfo? fieldInfo,
|
2022-10-26 22:36:34 +08:00
|
|
|
}) {
|
2023-05-30 17:11:16 +08:00
|
|
|
ITypeOptionLoader loader;
|
2022-11-26 21:28:08 +08:00
|
|
|
if (fieldInfo == null) {
|
2023-02-21 15:47:51 +08:00
|
|
|
loader = NewFieldTypeOptionLoader(viewId: gridView.id);
|
2022-10-26 22:36:34 +08:00
|
|
|
} else {
|
2023-02-21 15:47:51 +08:00
|
|
|
loader =
|
|
|
|
FieldTypeOptionLoader(viewId: gridView.id, field: fieldInfo.field);
|
2022-10-26 22:36:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
final editorBloc = FieldEditorBloc(
|
2022-11-26 21:28:08 +08:00
|
|
|
fieldName: fieldInfo?.name ?? '',
|
|
|
|
isGroupField: fieldInfo?.isGroupField ?? false,
|
2022-10-26 22:36:34 +08:00
|
|
|
loader: loader,
|
2023-02-26 16:27:17 +08:00
|
|
|
viewId: gridView.id,
|
2022-10-26 22:36:34 +08:00
|
|
|
);
|
|
|
|
return editorBloc;
|
|
|
|
}
|
|
|
|
|
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-03-08 21:19:44 +08:00
|
|
|
rowId: rowInfo.rowPB.id,
|
|
|
|
viewId: rowInfo.viewId,
|
2022-12-11 11:35:42 +08:00
|
|
|
rowCache: rowCache,
|
2022-10-27 14:11:15 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
final rowBloc = RowBloc(
|
|
|
|
rowInfo: rowInfo,
|
|
|
|
dataController: rowDataController,
|
|
|
|
)..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(
|
|
|
|
cellId: rowBloc.state.cellByFieldId[fieldId]!,
|
2022-10-27 14:11:15 +08:00
|
|
|
cellCache: rowCache.cellCache,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<FieldEditorBloc> createField(FieldType fieldType) async {
|
|
|
|
final editorBloc = createFieldEditor()
|
|
|
|
..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-02-26 16:27:17 +08:00
|
|
|
FieldCellContext singleSelectFieldCellContext() {
|
2022-10-23 16:44:10 +08:00
|
|
|
final field = singleSelectFieldContext().field;
|
2023-02-26 16:27:17 +08:00
|
|
|
return FieldCellContext(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
|
|
|
}
|
|
|
|
|
|
|
|
/// 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-03-14 01:02:54 +08:00
|
|
|
final context = await AppBackendService()
|
2022-11-17 16:44:17 +08:00
|
|
|
.createView(
|
2022-10-26 22:36:34 +08:00
|
|
|
appId: app.id,
|
2022-11-17 16:44:17 +08:00
|
|
|
name: "Test Grid",
|
2022-10-26 22:36:34 +08:00
|
|
|
layoutType: builder.layoutType!,
|
2022-11-17 16:44:17 +08:00
|
|
|
)
|
|
|
|
.then((result) {
|
|
|
|
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,
|
|
|
|
DatabaseController(
|
|
|
|
view: view,
|
2023-04-18 19:06:21 +08:00
|
|
|
layoutType: DatabaseLayoutPB.Grid,
|
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
|
|
|
}
|