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';
|
|
|
|
import 'package:appflowy/plugins/database_view/board/application/board_data_controller.dart';
|
|
|
|
import 'package:appflowy/plugins/database_view/board/board.dart';
|
|
|
|
import 'package:appflowy/plugins/database_view/grid/application/row/row_bloc.dart';
|
|
|
|
import 'package:appflowy/workspace/application/app/app_service.dart';
|
2023-01-08 12:10:53 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart';
|
2023-01-31 08:28:31 +08:00
|
|
|
import 'package:appflowy_backend/protobuf/flowy-database/field_entities.pb.dart';
|
2022-11-17 16:44:17 +08:00
|
|
|
|
|
|
|
import '../../util.dart';
|
2022-10-26 22:36:34 +08:00
|
|
|
import '../grid_test/util.dart';
|
2022-10-25 19:50:11 +08:00
|
|
|
|
|
|
|
class AppFlowyBoardTest {
|
2022-11-17 16:44:17 +08:00
|
|
|
final AppFlowyUnitTest unitTest;
|
|
|
|
|
|
|
|
AppFlowyBoardTest({required this.unitTest});
|
2022-10-25 19:50:11 +08:00
|
|
|
|
|
|
|
static Future<AppFlowyBoardTest> ensureInitialized() async {
|
2022-11-17 16:44:17 +08:00
|
|
|
final inner = await AppFlowyUnitTest.ensureInitialized();
|
|
|
|
return AppFlowyBoardTest(unitTest: inner);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<BoardTestContext> createTestBoard() async {
|
|
|
|
final app = await unitTest.createTestApp();
|
|
|
|
final builder = BoardPluginBuilder();
|
|
|
|
return AppService()
|
|
|
|
.createView(
|
|
|
|
appId: app.id,
|
|
|
|
name: "Test Board",
|
|
|
|
layoutType: builder.layoutType!,
|
|
|
|
)
|
|
|
|
.then((result) {
|
|
|
|
return result.fold(
|
|
|
|
(view) async {
|
|
|
|
final context =
|
|
|
|
BoardTestContext(view, BoardDataController(view: view));
|
|
|
|
final result = await context._boardDataController.openGrid();
|
|
|
|
result.fold((l) => null, (r) => throw Exception(r));
|
|
|
|
return context;
|
|
|
|
},
|
|
|
|
(error) {
|
|
|
|
throw Exception();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
2022-10-25 19:50:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> boardResponseFuture() {
|
|
|
|
return Future.delayed(boardResponseDuration(milliseconds: 200));
|
|
|
|
}
|
|
|
|
|
|
|
|
Duration boardResponseDuration({int milliseconds = 200}) {
|
|
|
|
return Duration(milliseconds: milliseconds);
|
|
|
|
}
|
2022-11-17 16:44:17 +08:00
|
|
|
|
|
|
|
class BoardTestContext {
|
|
|
|
final ViewPB gridView;
|
|
|
|
final BoardDataController _boardDataController;
|
|
|
|
|
|
|
|
BoardTestContext(this.gridView, this._boardDataController);
|
|
|
|
|
|
|
|
List<RowInfo> get rowInfos {
|
|
|
|
return _boardDataController.rowInfos;
|
|
|
|
}
|
|
|
|
|
2022-11-26 21:28:08 +08:00
|
|
|
List<FieldInfo> get fieldContexts => fieldController.fieldInfos;
|
2022-11-17 16:44:17 +08:00
|
|
|
|
2023-02-26 16:27:17 +08:00
|
|
|
FieldController get fieldController {
|
2022-11-17 16:44:17 +08:00
|
|
|
return _boardDataController.fieldController;
|
|
|
|
}
|
|
|
|
|
|
|
|
FieldEditorBloc createFieldEditor({
|
2022-11-26 21:28:08 +08:00
|
|
|
FieldInfo? fieldInfo,
|
2022-11-17 16:44:17 +08:00
|
|
|
}) {
|
|
|
|
IFieldTypeOptionLoader 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-11-17 16:44:17 +08:00
|
|
|
} else {
|
2023-02-21 15:47:51 +08:00
|
|
|
loader =
|
|
|
|
FieldTypeOptionLoader(viewId: gridView.id, field: fieldInfo.field);
|
2022-11-17 16:44:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
final editorBloc = FieldEditorBloc(
|
2022-11-26 21:28:08 +08:00
|
|
|
fieldName: fieldInfo?.name ?? '',
|
|
|
|
isGroupField: fieldInfo?.isGroupField ?? false,
|
2022-11-17 16:44:17 +08:00
|
|
|
loader: loader,
|
2023-02-26 16:27:17 +08:00
|
|
|
viewId: gridView.id,
|
2022-11-17 16:44:17 +08:00
|
|
|
);
|
|
|
|
return editorBloc;
|
|
|
|
}
|
|
|
|
|
2023-02-26 16:27:17 +08:00
|
|
|
Future<CellController> makeCellController(String fieldId) async {
|
2022-11-17 16:44:17 +08:00
|
|
|
final builder = await makeCellControllerBuilder(fieldId);
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
2023-02-26 16:27:17 +08:00
|
|
|
Future<CellControllerBuilder> makeCellControllerBuilder(
|
2022-11-17 16:44:17 +08:00
|
|
|
String fieldId,
|
|
|
|
) async {
|
|
|
|
final RowInfo rowInfo = rowInfos.last;
|
2022-12-11 11:35:42 +08:00
|
|
|
final rowCache = _boardDataController.rowCache;
|
2022-11-17 16:44:17 +08:00
|
|
|
|
2023-01-12 10:01:17 +08:00
|
|
|
final rowDataController = RowDataController(
|
2022-11-17 16:44:17 +08:00
|
|
|
rowInfo: rowInfo,
|
2022-12-11 11:35:42 +08:00
|
|
|
rowCache: rowCache,
|
2022-11-17 16:44:17 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
final rowBloc = RowBloc(
|
|
|
|
rowInfo: rowInfo,
|
|
|
|
dataController: rowDataController,
|
|
|
|
)..add(const RowEvent.initial());
|
|
|
|
await gridResponseFuture();
|
|
|
|
|
2023-02-26 16:27:17 +08:00
|
|
|
return CellControllerBuilder(
|
|
|
|
cellId: rowBloc.state.cellByFieldId[fieldId]!,
|
2022-11-17 16:44:17 +08:00
|
|
|
cellCache: rowCache.cellCache,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<FieldEditorBloc> createField(FieldType fieldType) async {
|
|
|
|
final editorBloc = createFieldEditor()
|
|
|
|
..add(const FieldEditorEvent.initial());
|
|
|
|
await gridResponseFuture();
|
|
|
|
editorBloc.add(FieldEditorEvent.switchToField(fieldType));
|
|
|
|
await gridResponseFuture();
|
|
|
|
return Future(() => editorBloc);
|
|
|
|
}
|
|
|
|
|
2022-11-26 21:28:08 +08:00
|
|
|
FieldInfo singleSelectFieldContext() {
|
|
|
|
final fieldInfo = fieldContexts
|
2022-11-17 16:44:17 +08:00
|
|
|
.firstWhere((element) => element.fieldType == FieldType.SingleSelect);
|
2022-11-26 21:28:08 +08:00
|
|
|
return fieldInfo;
|
2022-11-17 16:44:17 +08:00
|
|
|
}
|
|
|
|
|
2023-02-26 16:27:17 +08:00
|
|
|
FieldCellContext singleSelectFieldCellContext() {
|
2022-11-17 16:44:17 +08:00
|
|
|
final field = singleSelectFieldContext().field;
|
2023-02-26 16:27:17 +08:00
|
|
|
return FieldCellContext(viewId: gridView.id, field: field);
|
2022-11-17 16:44:17 +08:00
|
|
|
}
|
|
|
|
|
2022-11-26 21:28:08 +08:00
|
|
|
FieldInfo textFieldContext() {
|
|
|
|
final fieldInfo = fieldContexts
|
2022-11-17 16:44:17 +08:00
|
|
|
.firstWhere((element) => element.fieldType == FieldType.RichText);
|
2022-11-26 21:28:08 +08:00
|
|
|
return fieldInfo;
|
2022-11-17 16:44:17 +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;
|
2022-11-17 16:44:17 +08:00
|
|
|
}
|
|
|
|
}
|