| 
									
										
										
										
											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'; | 
					
						
							| 
									
										
										
										
											2023-08-10 15:46:16 +08:00
										 |  |  | import 'package:appflowy/plugins/database_view/application/field/field_info.dart'; | 
					
						
							| 
									
										
										
										
											2023-02-26 16:27:17 +08:00
										 |  |  | 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'; | 
					
						
							| 
									
										
										
										
											2023-07-14 13:37:13 +08:00
										 |  |  | import 'package:appflowy/plugins/database_view/application/row/row_controller.dart'; | 
					
						
							| 
									
										
										
										
											2023-02-26 16:27:17 +08:00
										 |  |  | import 'package:appflowy/plugins/database_view/board/board.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'; | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  | import 'package:appflowy/workspace/application/view/view_service.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'; | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  |     return ViewBackendService.createView( | 
					
						
							| 
									
										
										
										
											2023-05-31 14:08:54 +08:00
										 |  |  |       parentViewId: app.id, | 
					
						
							| 
									
										
										
										
											2022-11-17 16:44:17 +08:00
										 |  |  |       name: "Test Board", | 
					
						
							|  |  |  |       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 = BoardTestContext( | 
					
						
							|  |  |  |             view, | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  |             DatabaseController(view: view), | 
					
						
							| 
									
										
										
										
											2023-03-08 21:19:44 +08:00
										 |  |  |           ); | 
					
						
							|  |  |  |           final result = await context._boardDataController.open(); | 
					
						
							| 
									
										
										
										
											2022-11-17 16:44:17 +08:00
										 |  |  |           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; | 
					
						
							| 
									
										
										
										
											2023-03-08 21:19:44 +08:00
										 |  |  |   final DatabaseController _boardDataController; | 
					
						
							| 
									
										
										
										
											2022-11-17 16:44:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   BoardTestContext(this.gridView, this._boardDataController); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   List<RowInfo> get rowInfos { | 
					
						
							| 
									
										
										
										
											2023-03-20 21:16:37 +08:00
										 |  |  |     return _boardDataController.rowCache.rowInfos; | 
					
						
							| 
									
										
										
										
											2022-11-17 16:44:17 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-26 11:48:58 +08:00
										 |  |  |   DatabaseController get databaseController => _boardDataController; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-04 09:28:13 +08:00
										 |  |  |   FieldEditorBloc makeFieldEditor({ | 
					
						
							|  |  |  |     required FieldInfo fieldInfo, | 
					
						
							| 
									
										
										
										
											2022-11-17 16:44:17 +08:00
										 |  |  |   }) { | 
					
						
							| 
									
										
										
										
											2023-06-04 09:28:13 +08:00
										 |  |  |     final loader = FieldTypeOptionLoader( | 
					
						
							|  |  |  |       viewId: gridView.id, | 
					
						
							|  |  |  |       field: fieldInfo.field, | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2022-11-17 16:44:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     final editorBloc = FieldEditorBloc( | 
					
						
							| 
									
										
										
										
											2023-06-04 09:28:13 +08:00
										 |  |  |       isGroupField: fieldInfo.isGroupField, | 
					
						
							| 
									
										
										
										
											2022-11-17 16:44:17 +08:00
										 |  |  |       loader: loader, | 
					
						
							| 
									
										
										
										
											2023-06-04 09:28:13 +08:00
										 |  |  |       field: fieldInfo.field, | 
					
						
							| 
									
										
										
										
											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-03-09 09:51:04 +08:00
										 |  |  |     final rowDataController = RowController( | 
					
						
							| 
									
										
										
										
											2023-03-08 21:19:44 +08:00
										 |  |  |       viewId: rowInfo.viewId, | 
					
						
							| 
									
										
										
										
											2023-06-14 22:16:33 +08:00
										 |  |  |       rowMeta: rowInfo.rowMeta, | 
					
						
							| 
									
										
										
										
											2022-12-11 11:35:42 +08:00
										 |  |  |       rowCache: rowCache, | 
					
						
							| 
									
										
										
										
											2022-11-17 16:44:17 +08:00
										 |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     final rowBloc = RowBloc( | 
					
						
							| 
									
										
										
										
											2023-06-14 22:16:33 +08:00
										 |  |  |       viewId: rowInfo.viewId, | 
					
						
							| 
									
										
										
										
											2022-11-17 16:44:17 +08:00
										 |  |  |       dataController: rowDataController, | 
					
						
							| 
									
										
										
										
											2023-06-14 22:16:33 +08:00
										 |  |  |       rowId: rowInfo.rowMeta.id, | 
					
						
							| 
									
										
										
										
											2022-11-17 16:44:17 +08:00
										 |  |  |     )..add(const RowEvent.initial()); | 
					
						
							|  |  |  |     await gridResponseFuture(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-26 16:27:17 +08:00
										 |  |  |     return CellControllerBuilder( | 
					
						
							| 
									
										
										
										
											2023-06-04 09:28:13 +08:00
										 |  |  |       cellContext: rowBloc.state.cellByFieldId[fieldId]!, | 
					
						
							| 
									
										
										
										
											2022-11-17 16:44:17 +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-11-17 16:44:17 +08:00
										 |  |  |       ..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
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  |   } | 
					
						
							|  |  |  | } |