| 
									
										
										
										
											2024-01-05 17:30:54 +08:00
										 |  |  | import 'package:appflowy/plugins/database/application/cell/cell_controller.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/plugins/database/application/cell/cell_controller_builder.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/plugins/database/application/field/field_controller.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/plugins/database/application/field/field_editor_bloc.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/plugins/database/application/field/field_info.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/plugins/database/application/field/field_service.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/plugins/database/application/row/row_cache.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/plugins/database/application/database_controller.dart'; | 
					
						
							|  |  |  | import 'package:appflowy/plugins/database/application/row/row_service.dart'; | 
					
						
							| 
									
										
										
										
											2023-06-01 20:23:27 +08:00
										 |  |  | import 'package:appflowy/workspace/application/view/view_service.dart'; | 
					
						
							| 
									
										
										
										
											2023-12-31 07:29:40 +08:00
										 |  |  | import 'package:appflowy_backend/protobuf/flowy-folder/view.pb.dart'; | 
					
						
							| 
									
										
										
										
											2023-04-28 14:08:53 +08:00
										 |  |  | import 'package:appflowy_backend/protobuf/flowy-database2/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 { | 
					
						
							| 
									
										
										
										
											2024-01-25 16:37:36 +01:00
										 |  |  |   GridTestContext(this.gridView, this.gridController); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-17 16:44:17 +08:00
										 |  |  |   final ViewPB gridView; | 
					
						
							| 
									
										
										
										
											2023-02-26 16:27:17 +08:00
										 |  |  |   final DatabaseController 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
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |   List<FieldInfo> get fieldInfos => 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-27 14:11:15 +08:00
										 |  |  |   Future<FieldEditorBloc> createField(FieldType fieldType) async { | 
					
						
							| 
									
										
										
										
											2023-11-23 16:43:29 +08:00
										 |  |  |     final editorBloc = | 
					
						
							|  |  |  |         await createFieldEditor(databaseController: gridController) | 
					
						
							|  |  |  |           ..add(const FieldEditorEvent.initial()); | 
					
						
							| 
									
										
										
										
											2022-10-26 22:36:34 +08:00
										 |  |  |     await gridResponseFuture(); | 
					
						
							| 
									
										
										
										
											2023-11-23 16:43:29 +08:00
										 |  |  |     editorBloc.add(FieldEditorEvent.switchFieldType(fieldType)); | 
					
						
							| 
									
										
										
										
											2022-10-27 14:11:15 +08:00
										 |  |  |     await gridResponseFuture(); | 
					
						
							|  |  |  |     return Future(() => editorBloc); | 
					
						
							| 
									
										
										
										
											2022-10-23 16:44:10 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-26 21:28:08 +08:00
										 |  |  |   FieldInfo singleSelectFieldContext() { | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |     final fieldInfo = fieldInfos | 
					
						
							| 
									
										
										
										
											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
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-26 21:28:08 +08:00
										 |  |  |   FieldInfo textFieldContext() { | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |     final fieldInfo = fieldInfos | 
					
						
							| 
									
										
										
										
											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() { | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |     final fieldInfo = fieldInfos | 
					
						
							| 
									
										
										
										
											2022-11-17 16:44:17 +08:00
										 |  |  |         .firstWhere((element) => element.fieldType == FieldType.Checkbox); | 
					
						
							| 
									
										
										
										
											2022-11-26 21:28:08 +08:00
										 |  |  |     return fieldInfo; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |   SelectOptionCellController makeSelectOptionCellController( | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |     FieldType fieldType, | 
					
						
							|  |  |  |     int rowIndex, | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |   ) { | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |     assert( | 
					
						
							|  |  |  |       fieldType == FieldType.SingleSelect || fieldType == FieldType.MultiSelect, | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2022-11-26 21:28:08 +08:00
										 |  |  |     final field = | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |         fieldInfos.firstWhere((fieldInfo) => fieldInfo.fieldType == fieldType); | 
					
						
							|  |  |  |     return makeCellController( | 
					
						
							|  |  |  |       gridController, | 
					
						
							|  |  |  |       CellContext(fieldId: field.id, rowId: rowInfos[rowIndex].rowId), | 
					
						
							|  |  |  |     ).as(); | 
					
						
							| 
									
										
										
										
											2022-11-26 21:28:08 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |   TextCellController makeTextCellController(int rowIndex) { | 
					
						
							|  |  |  |     final field = fieldInfos | 
					
						
							| 
									
										
										
										
											2022-11-26 21:28:08 +08:00
										 |  |  |         .firstWhere((element) => element.fieldType == FieldType.RichText); | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |     return makeCellController( | 
					
						
							|  |  |  |       gridController, | 
					
						
							|  |  |  |       CellContext(fieldId: field.id, rowId: rowInfos[rowIndex].rowId), | 
					
						
							|  |  |  |     ).as(); | 
					
						
							| 
									
										
										
										
											2022-10-26 22:36:34 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-11-28 19:41:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |   CheckboxCellController makeCheckboxCellController(int rowIndex) { | 
					
						
							|  |  |  |     final field = fieldInfos | 
					
						
							| 
									
										
										
										
											2022-11-28 19:41:57 +08:00
										 |  |  |         .firstWhere((element) => element.fieldType == FieldType.Checkbox); | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |     return makeCellController( | 
					
						
							|  |  |  |       gridController, | 
					
						
							|  |  |  |       CellContext(fieldId: field.id, rowId: rowInfos[rowIndex].rowId), | 
					
						
							|  |  |  |     ).as(); | 
					
						
							| 
									
										
										
										
											2022-11-28 19:41:57 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-11-17 16:44:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-04 09:28:13 +08:00
										 |  |  | Future<FieldEditorBloc> createFieldEditor({ | 
					
						
							| 
									
										
										
										
											2023-11-23 16:43:29 +08:00
										 |  |  |   required DatabaseController databaseController, | 
					
						
							| 
									
										
										
										
											2023-06-04 09:28:13 +08:00
										 |  |  | }) async { | 
					
						
							| 
									
										
										
										
											2023-12-11 11:19:20 +08:00
										 |  |  |   final result = await FieldBackendService.createField( | 
					
						
							| 
									
										
										
										
											2023-11-23 16:43:29 +08:00
										 |  |  |     viewId: databaseController.viewId, | 
					
						
							| 
									
										
										
										
											2023-06-04 09:28:13 +08:00
										 |  |  |   ); | 
					
						
							| 
									
										
										
										
											2023-11-23 16:43:29 +08:00
										 |  |  |   await gridResponseFuture(); | 
					
						
							| 
									
										
										
										
											2023-06-04 09:28:13 +08:00
										 |  |  |   return result.fold( | 
					
						
							| 
									
										
										
										
											2023-12-20 11:11:38 +08:00
										 |  |  |     (field) { | 
					
						
							| 
									
										
										
										
											2023-06-04 09:28:13 +08:00
										 |  |  |       return FieldEditorBloc( | 
					
						
							| 
									
										
										
										
											2023-11-23 16:43:29 +08:00
										 |  |  |         viewId: databaseController.viewId, | 
					
						
							|  |  |  |         fieldController: databaseController.fieldController, | 
					
						
							| 
									
										
										
										
											2023-12-20 11:11:38 +08:00
										 |  |  |         field: field, | 
					
						
							| 
									
										
										
										
											2023-06-04 09:28:13 +08:00
										 |  |  |       ); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     (err) => throw Exception(err), | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-17 16:44:17 +08:00
										 |  |  | /// Create a empty Grid for test
 | 
					
						
							|  |  |  | class AppFlowyGridTest { | 
					
						
							|  |  |  |   AppFlowyGridTest({required this.unitTest}); | 
					
						
							| 
									
										
										
										
											2022-10-26 22:36:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-25 16:37:36 +01:00
										 |  |  |   final AppFlowyUnitTest unitTest; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							| 
									
										
										
										
											2023-12-20 10:08:35 +08:00
										 |  |  |     final app = await unitTest.createWorkspace(); | 
					
						
							| 
									
										
										
										
											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", | 
					
						
							| 
									
										
										
										
											2023-06-20 23:48:34 +08:00
										 |  |  |       layoutType: ViewLayoutPB.Grid, | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							| 
									
										
										
										
											2024-01-25 16:37:36 +01:00
										 |  |  |   AppFlowyGridCellTest({required this.gridTest}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-17 16:44:17 +08:00
										 |  |  |   late GridTestContext context; | 
					
						
							| 
									
										
										
										
											2022-10-27 14:11:15 +08:00
										 |  |  |   final AppFlowyGridTest 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 { | 
					
						
							| 
									
										
										
										
											2023-12-11 11:19:20 +08:00
										 |  |  |     await RowBackendService.createRow(viewId: context.gridView.id); | 
					
						
							| 
									
										
										
										
											2022-10-23 16:44:10 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |   SelectOptionCellController makeSelectOptionCellController( | 
					
						
							| 
									
										
										
										
											2023-04-10 15:10:42 +08:00
										 |  |  |     FieldType fieldType, | 
					
						
							|  |  |  |     int rowIndex, | 
					
						
							| 
									
										
										
										
											2024-01-24 23:59:45 +08:00
										 |  |  |   ) => | 
					
						
							|  |  |  |       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
										 |  |  | } |