util.dart 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import 'dart:collection';
  2. import 'package:app_flowy/plugins/board/application/board_data_controller.dart';
  3. import 'package:app_flowy/plugins/board/board.dart';
  4. import 'package:app_flowy/plugins/grid/application/block/block_cache.dart';
  5. import 'package:app_flowy/plugins/grid/application/cell/cell_service/cell_service.dart';
  6. import 'package:app_flowy/plugins/grid/application/field/field_controller.dart';
  7. import 'package:app_flowy/plugins/grid/application/field/field_editor_bloc.dart';
  8. import 'package:app_flowy/plugins/grid/application/field/field_service.dart';
  9. import 'package:app_flowy/plugins/grid/application/field/type_option/type_option_context.dart';
  10. import 'package:app_flowy/plugins/grid/application/grid_data_controller.dart';
  11. import 'package:app_flowy/plugins/grid/application/row/row_bloc.dart';
  12. import 'package:app_flowy/plugins/grid/application/row/row_cache.dart';
  13. import 'package:app_flowy/plugins/grid/application/row/row_data_controller.dart';
  14. import 'package:app_flowy/plugins/grid/grid.dart';
  15. import 'package:app_flowy/workspace/application/app/app_service.dart';
  16. import 'package:flowy_sdk/protobuf/flowy-folder/view.pb.dart';
  17. import 'package:flowy_sdk/protobuf/flowy-grid/field_entities.pb.dart';
  18. import '../../util.dart';
  19. /// Create a empty Grid for test
  20. class AppFlowyGridTest {
  21. final AppFlowyUnitTest unitTest;
  22. late ViewPB gridView;
  23. GridDataController? _gridDataController;
  24. BoardDataController? _boardDataController;
  25. AppFlowyGridTest({required this.unitTest});
  26. static Future<AppFlowyGridTest> ensureInitialized() async {
  27. final inner = await AppFlowyUnitTest.ensureInitialized();
  28. return AppFlowyGridTest(unitTest: inner);
  29. }
  30. List<RowInfo> get rowInfos {
  31. if (_gridDataController != null) {
  32. return _gridDataController!.rowInfos;
  33. }
  34. if (_boardDataController != null) {
  35. return _boardDataController!.rowInfos;
  36. }
  37. throw Exception();
  38. }
  39. UnmodifiableMapView<String, GridBlockCache> get blocks {
  40. if (_gridDataController != null) {
  41. return _gridDataController!.blocks;
  42. }
  43. if (_boardDataController != null) {
  44. return _boardDataController!.blocks;
  45. }
  46. throw Exception();
  47. }
  48. List<GridFieldContext> get fieldContexts => fieldController.fieldContexts;
  49. GridFieldController get fieldController {
  50. if (_gridDataController != null) {
  51. return _gridDataController!.fieldController;
  52. }
  53. if (_boardDataController != null) {
  54. return _boardDataController!.fieldController;
  55. }
  56. throw Exception();
  57. }
  58. Future<void> createRow() async {
  59. if (_gridDataController != null) {
  60. return _gridDataController!.createRow();
  61. }
  62. throw Exception();
  63. }
  64. FieldEditorBloc createFieldEditor({
  65. GridFieldContext? fieldContext,
  66. }) {
  67. IFieldTypeOptionLoader loader;
  68. if (fieldContext == null) {
  69. loader = NewFieldTypeOptionLoader(gridId: gridView.id);
  70. } else {
  71. loader =
  72. FieldTypeOptionLoader(gridId: gridView.id, field: fieldContext.field);
  73. }
  74. final editorBloc = FieldEditorBloc(
  75. fieldName: fieldContext?.name ?? '',
  76. isGroupField: fieldContext?.isGroupField ?? false,
  77. loader: loader,
  78. gridId: gridView.id,
  79. );
  80. return editorBloc;
  81. }
  82. Future<FieldEditorBloc> createFieldFromType(FieldType fieldType) async {
  83. final editor = createFieldEditor()..add(const FieldEditorEvent.initial());
  84. await gridResponseFuture();
  85. editor.dataController.switchToField(fieldType);
  86. await gridResponseFuture();
  87. return Future(() => editor);
  88. }
  89. GridFieldContext singleSelectFieldContext() {
  90. final fieldContext = fieldContexts
  91. .firstWhere((element) => element.fieldType == FieldType.SingleSelect);
  92. return fieldContext;
  93. }
  94. GridFieldCellContext singleSelectFieldCellContext() {
  95. final field = singleSelectFieldContext().field;
  96. return GridFieldCellContext(gridId: gridView.id, field: field);
  97. }
  98. Future<void> createTestGrid() async {
  99. final app = await unitTest.createTestApp();
  100. final builder = GridPluginBuilder();
  101. final result = await AppService().createView(
  102. appId: app.id,
  103. name: "Test Grid",
  104. dataFormatType: builder.dataFormatType,
  105. pluginType: builder.pluginType,
  106. layoutType: builder.layoutType!,
  107. );
  108. await result.fold(
  109. (view) async {
  110. gridView = view;
  111. _gridDataController = GridDataController(view: view);
  112. final result = await _gridDataController!.openGrid();
  113. result.fold((l) => null, (r) => throw Exception(r));
  114. },
  115. (error) {},
  116. );
  117. }
  118. Future<void> createTestBoard() async {
  119. final app = await unitTest.createTestApp();
  120. final builder = BoardPluginBuilder();
  121. final result = await AppService().createView(
  122. appId: app.id,
  123. name: "Test Board",
  124. dataFormatType: builder.dataFormatType,
  125. pluginType: builder.pluginType,
  126. layoutType: builder.layoutType!,
  127. );
  128. await result.fold(
  129. (view) async {
  130. _boardDataController = BoardDataController(view: view);
  131. final result = await _boardDataController!.openGrid();
  132. result.fold((l) => null, (r) => throw Exception(r));
  133. gridView = view;
  134. },
  135. (error) {},
  136. );
  137. }
  138. }
  139. /// Create a new Grid for cell test
  140. class AppFlowyGridCellTest {
  141. final AppFlowyGridTest _gridTest;
  142. AppFlowyGridCellTest(AppFlowyGridTest gridTest) : _gridTest = gridTest;
  143. static Future<AppFlowyGridCellTest> ensureInitialized() async {
  144. final gridTest = await AppFlowyGridTest.ensureInitialized();
  145. return AppFlowyGridCellTest(gridTest);
  146. }
  147. Future<void> createTestRow() async {
  148. await _gridTest.createRow();
  149. }
  150. Future<void> createTestGrid() async {
  151. await _gridTest.createTestGrid();
  152. }
  153. Future<GridCellControllerBuilder> cellControllerBuilder(
  154. String fieldId,
  155. ) async {
  156. final RowInfo rowInfo = _gridTest.rowInfos.last;
  157. final blockCache = _gridTest.blocks[rowInfo.rowPB.blockId];
  158. final rowCache = blockCache?.rowCache;
  159. final rowDataController = GridRowDataController(
  160. rowInfo: rowInfo,
  161. fieldController: _gridTest._gridDataController!.fieldController,
  162. rowCache: rowCache!,
  163. );
  164. final rowBloc = RowBloc(
  165. rowInfo: rowInfo,
  166. dataController: rowDataController,
  167. )..add(const RowEvent.initial());
  168. await gridResponseFuture();
  169. return GridCellControllerBuilder(
  170. cellId: rowBloc.state.gridCellMap[fieldId]!,
  171. cellCache: rowCache.cellCache,
  172. delegate: rowDataController,
  173. );
  174. }
  175. }
  176. class AppFlowyGridSelectOptionCellTest {
  177. final AppFlowyGridCellTest _gridCellTest;
  178. AppFlowyGridSelectOptionCellTest(AppFlowyGridCellTest cellTest)
  179. : _gridCellTest = cellTest;
  180. static Future<AppFlowyGridSelectOptionCellTest> ensureInitialized() async {
  181. final gridTest = await AppFlowyGridCellTest.ensureInitialized();
  182. return AppFlowyGridSelectOptionCellTest(gridTest);
  183. }
  184. Future<void> createTestGrid() async {
  185. await _gridCellTest.createTestGrid();
  186. }
  187. Future<void> createTestRow() async {
  188. await _gridCellTest.createTestRow();
  189. }
  190. Future<GridSelectOptionCellController> makeCellController(
  191. FieldType fieldType) async {
  192. assert(fieldType == FieldType.SingleSelect ||
  193. fieldType == FieldType.MultiSelect);
  194. final fieldContexts = _gridCellTest._gridTest.fieldContexts;
  195. final field =
  196. fieldContexts.firstWhere((element) => element.fieldType == fieldType);
  197. final builder = await _gridCellTest.cellControllerBuilder(field.id);
  198. final cellController = builder.build() as GridSelectOptionCellController;
  199. return cellController;
  200. }
  201. }
  202. Future<void> gridResponseFuture() {
  203. return Future.delayed(gridResponseDuration(milliseconds: 200));
  204. }
  205. Duration gridResponseDuration({int milliseconds = 200}) {
  206. return Duration(milliseconds: milliseconds);
  207. }