2022-10-23 16:44:10 +08:00
|
|
|
import 'dart:collection';
|
|
|
|
import 'package:app_flowy/plugins/grid/application/block/block_cache.dart';
|
2022-10-17 10:31:56 +08:00
|
|
|
import 'package:app_flowy/plugins/grid/application/cell/cell_service/cell_service.dart';
|
2022-10-23 16:44:10 +08:00
|
|
|
import 'package:app_flowy/plugins/grid/application/field/field_controller.dart';
|
2022-10-26 22:36:34 +08:00
|
|
|
import 'package:app_flowy/plugins/grid/application/field/field_editor_bloc.dart';
|
2022-10-23 16:44:10 +08:00
|
|
|
import 'package:app_flowy/plugins/grid/application/field/field_service.dart';
|
2022-10-26 22:36:34 +08:00
|
|
|
import 'package:app_flowy/plugins/grid/application/field/type_option/type_option_context.dart';
|
2022-10-17 10:31:56 +08:00
|
|
|
import 'package:app_flowy/plugins/grid/application/grid_data_controller.dart';
|
|
|
|
import 'package:app_flowy/plugins/grid/application/row/row_bloc.dart';
|
|
|
|
import 'package:app_flowy/plugins/grid/application/row/row_cache.dart';
|
|
|
|
import 'package:app_flowy/plugins/grid/application/row/row_data_controller.dart';
|
2022-10-16 16:51:21 +08:00
|
|
|
import 'package:app_flowy/plugins/grid/grid.dart';
|
|
|
|
import 'package:app_flowy/workspace/application/app/app_service.dart';
|
|
|
|
import 'package:flowy_sdk/protobuf/flowy-folder/view.pb.dart';
|
2022-10-17 10:31:56 +08:00
|
|
|
import 'package:flowy_sdk/protobuf/flowy-grid/field_entities.pb.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;
|
2022-11-26 21:28:08 +08:00
|
|
|
final GridController _gridController;
|
2022-10-16 16:51:21 +08:00
|
|
|
|
2022-11-26 21:28:08 +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 {
|
2022-11-26 21:28:08 +08:00
|
|
|
return _gridController.rowInfos;
|
2022-10-26 22:36:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UnmodifiableMapView<String, GridBlockCache> get blocks {
|
2022-11-26 21:28:08 +08:00
|
|
|
return _gridController.blocks;
|
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
|
|
|
|
|
|
|
GridFieldController get fieldController {
|
2022-11-26 21:28:08 +08:00
|
|
|
return _gridController.fieldController;
|
2022-10-26 22:36:34 +08:00
|
|
|
}
|
2022-10-23 16:44:10 +08:00
|
|
|
|
|
|
|
Future<void> createRow() async {
|
2022-11-26 21:28:08 +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
|
|
|
}) {
|
|
|
|
IFieldTypeOptionLoader loader;
|
2022-11-26 21:28:08 +08:00
|
|
|
if (fieldInfo == null) {
|
2022-10-26 22:36:34 +08:00
|
|
|
loader = NewFieldTypeOptionLoader(gridId: gridView.id);
|
|
|
|
} else {
|
|
|
|
loader =
|
2022-11-26 21:28:08 +08:00
|
|
|
FieldTypeOptionLoader(gridId: 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,
|
|
|
|
gridId: gridView.id,
|
|
|
|
);
|
|
|
|
return editorBloc;
|
|
|
|
}
|
|
|
|
|
2022-10-27 14:11:15 +08:00
|
|
|
Future<IGridCellController> makeCellController(String fieldId) async {
|
|
|
|
final builder = await makeCellControllerBuilder(fieldId);
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<GridCellControllerBuilder> makeCellControllerBuilder(
|
|
|
|
String fieldId,
|
|
|
|
) async {
|
|
|
|
final RowInfo rowInfo = rowInfos.last;
|
|
|
|
final blockCache = blocks[rowInfo.rowPB.blockId];
|
|
|
|
final rowCache = blockCache?.rowCache;
|
2022-11-26 21:28:08 +08:00
|
|
|
final fieldController = _gridController.fieldController;
|
2022-10-27 14:11:15 +08:00
|
|
|
|
|
|
|
final rowDataController = GridRowDataController(
|
|
|
|
rowInfo: rowInfo,
|
|
|
|
fieldController: fieldController,
|
|
|
|
rowCache: rowCache!,
|
|
|
|
);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
return GridCellControllerBuilder(
|
|
|
|
cellId: rowBloc.state.gridCellMap[fieldId]!,
|
|
|
|
cellCache: rowCache.cellCache,
|
|
|
|
delegate: rowDataController,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
GridFieldCellContext singleSelectFieldCellContext() {
|
|
|
|
final field = singleSelectFieldContext().field;
|
|
|
|
return GridFieldCellContext(gridId: gridView.id, field: field);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<GridSelectOptionCellController> makeSelectOptionCellController(
|
|
|
|
FieldType fieldType) async {
|
|
|
|
assert(fieldType == FieldType.SingleSelect ||
|
|
|
|
fieldType == FieldType.MultiSelect);
|
|
|
|
|
|
|
|
final field =
|
|
|
|
fieldContexts.firstWhere((element) => element.fieldType == fieldType);
|
|
|
|
final cellController =
|
|
|
|
await makeCellController(field.id) as GridSelectOptionCellController;
|
|
|
|
return cellController;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<GridCellController> makeTextCellController() async {
|
|
|
|
final field = fieldContexts
|
|
|
|
.firstWhere((element) => element.fieldType == FieldType.RichText);
|
|
|
|
final cellController =
|
|
|
|
await makeCellController(field.id) as GridCellController;
|
|
|
|
return cellController;
|
2022-10-26 22:36:34 +08:00
|
|
|
}
|
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();
|
|
|
|
final context = await AppService()
|
|
|
|
.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
|
|
|
dataFormatType: builder.dataFormatType,
|
|
|
|
pluginType: builder.pluginType,
|
|
|
|
layoutType: builder.layoutType!,
|
2022-11-17 16:44:17 +08:00
|
|
|
)
|
|
|
|
.then((result) {
|
|
|
|
return result.fold(
|
|
|
|
(view) async {
|
2022-11-26 21:28:08 +08:00
|
|
|
final context = GridTestContext(view, GridController(view: view));
|
|
|
|
final result = await context._gridController.openGrid();
|
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
|
|
|
}
|
|
|
|
|
|
|
|
Future<GridSelectOptionCellController> makeCellController(
|
|
|
|
FieldType fieldType) async {
|
2022-11-26 21:28:08 +08:00
|
|
|
return context.makeSelectOptionCellController(fieldType);
|
2022-10-23 16:44:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 16:44:17 +08:00
|
|
|
Future<void> gridResponseFuture({int milliseconds = 500}) {
|
|
|
|
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
|
|
|
}
|