|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | +import 'package:otzaria/models/books.dart'; |
| 5 | +import 'package:otzaria/personal_notes/bloc/personal_notes_bloc.dart'; |
| 6 | +import 'package:otzaria/personal_notes/bloc/personal_notes_state.dart'; |
| 7 | +import 'package:otzaria/settings/engine/app_settings_store.dart'; |
| 8 | +import 'package:otzaria/settings/engine/settings_bloc.dart'; |
| 9 | +import 'package:otzaria/settings/engine/settings_state.dart'; |
| 10 | +import 'package:otzaria/tabs/models/text_tab.dart'; |
| 11 | +import 'package:otzaria/text_book/bloc/text_book_bloc.dart'; |
| 12 | +import 'package:otzaria/text_book/bloc/text_book_state.dart'; |
| 13 | +import 'package:otzaria/text_book/view/combined_view/combined_book_screen.dart'; |
| 14 | +import 'package:otzaria/text_book/view/reader_line/reader_line_render.dart'; |
| 15 | +import 'package:otzaria/text_book/view/widgets/selection_gutter_painter.dart'; |
| 16 | +import 'package:otzaria/widgets/smart_text/smart_text.dart'; |
| 17 | + |
| 18 | +import '../../../test_helpers/memory_cache_provider.dart'; |
| 19 | +import '../../../test_helpers/stub_personal_notes_bloc.dart'; |
| 20 | +import '../../../test_helpers/stub_settings_bloc.dart'; |
| 21 | +import '../../../test_helpers/stub_text_book_bloc.dart'; |
| 22 | + |
| 23 | +/// בחירת שורה בקורא בונה מחדש רק את הפריטים שהבחירה בהם השתנתה, ולא את כל |
| 24 | +/// השורות הגלויות (perf2, Refs #1692). |
| 25 | +final List<String> _lines = [ |
| 26 | + for (var i = 0; i < 100; i++) 'שורה $i בספר הבדיקה', |
| 27 | +]; |
| 28 | + |
| 29 | +TextBookLoaded _state({int? selectedIndex, int? highlightedLine}) => |
| 30 | + TextBookLoaded( |
| 31 | + book: TextBook(title: 'ספר בחירה'), |
| 32 | + showLeftPane: false, |
| 33 | + content: _lines, |
| 34 | + fontSize: 18, |
| 35 | + showSplitView: true, |
| 36 | + activeCommentators: const [], |
| 37 | + commentatorGroups: const [], |
| 38 | + availableCommentators: const [], |
| 39 | + links: const [], |
| 40 | + linksByLine: const {}, |
| 41 | + tableOfContents: const [], |
| 42 | + visibleIndices: const [0], |
| 43 | + selectedIndex: selectedIndex, |
| 44 | + selectedIndices: {?selectedIndex}, |
| 45 | + highlightedLine: highlightedLine, |
| 46 | + searchText: '', |
| 47 | + ); |
| 48 | + |
| 49 | +void main() { |
| 50 | + TestWidgetsFlutterBinding.ensureInitialized(); |
| 51 | + setUpAll(() async { |
| 52 | + await Settings.init(cacheProvider: MemoryCacheProvider()); |
| 53 | + }); |
| 54 | + |
| 55 | + Future<EmittingStubTextBookBloc> mount(WidgetTester tester) async { |
| 56 | + final initial = _state(); |
| 57 | + final bloc = EmittingStubTextBookBloc(initial); |
| 58 | + final notes = StubPersonalNotesBloc(const PersonalNotesState.initial()); |
| 59 | + final settings = StubSettingsBloc(SettingsState.initial()); |
| 60 | + final tab = TextBookTab(book: initial.book, index: 0); |
| 61 | + addTearDown(bloc.close); |
| 62 | + addTearDown(notes.close); |
| 63 | + addTearDown(settings.close); |
| 64 | + addTearDown(tab.dispose); |
| 65 | + await tester.pumpWidget( |
| 66 | + MaterialApp( |
| 67 | + home: MultiBlocProvider( |
| 68 | + providers: [ |
| 69 | + BlocProvider<TextBookBloc>.value(value: bloc), |
| 70 | + BlocProvider<PersonalNotesBloc>.value(value: notes), |
| 71 | + BlocProvider<SettingsBloc>.value(value: settings), |
| 72 | + ], |
| 73 | + child: Scaffold( |
| 74 | + body: CombinedView( |
| 75 | + data: initial.content, |
| 76 | + openBookCallback: (_) {}, |
| 77 | + openLeftPaneTab: (_, {searchText}) {}, |
| 78 | + textSize: 18, |
| 79 | + showCommentaryAsExpansionTiles: false, |
| 80 | + tab: tab, |
| 81 | + ), |
| 82 | + ), |
| 83 | + ), |
| 84 | + ), |
| 85 | + ); |
| 86 | + await tester.pumpAndSettle(); |
| 87 | + return bloc; |
| 88 | + } |
| 89 | + |
| 90 | + /// ההחלפה מגיעה לקורא בפריים שאחרי ה-emit. |
| 91 | + Future<void> emit( |
| 92 | + WidgetTester tester, |
| 93 | + EmittingStubTextBookBloc bloc, |
| 94 | + TextBookLoaded state, |
| 95 | + ) async { |
| 96 | + bloc.emitState(state); |
| 97 | + await tester.pump(); |
| 98 | + await tester.pump(); |
| 99 | + } |
| 100 | + |
| 101 | + /// מספר בניות [SmartTextWidget] בזמן [body]. |
| 102 | + Future<int> countLineBuilds(Future<void> Function() body) async { |
| 103 | + var builds = 0; |
| 104 | + final previous = debugOnRebuildDirtyWidget; |
| 105 | + debugOnRebuildDirtyWidget = (element, _) { |
| 106 | + if (element.widget is SmartTextWidget) builds++; |
| 107 | + }; |
| 108 | + try { |
| 109 | + await body(); |
| 110 | + } finally { |
| 111 | + debugOnRebuildDirtyWidget = previous; |
| 112 | + } |
| 113 | + return builds; |
| 114 | + } |
| 115 | + |
| 116 | + Finder gutterOn(int line) => find.descendant( |
| 117 | + of: find.byWidgetPredicate( |
| 118 | + (w) => w is CustomPaint && w.painter is SelectionGutterPainter, |
| 119 | + ), |
| 120 | + matching: find.byWidgetPredicate( |
| 121 | + (w) => w is ReaderLineBlock && w.lineIndex == line, |
| 122 | + ), |
| 123 | + ); |
| 124 | + |
| 125 | + testWidgets('בחירת שורה בונה מחדש רק את השורות שהבחירה בהן השתנתה', ( |
| 126 | + tester, |
| 127 | + ) async { |
| 128 | + final bloc = await mount(tester); |
| 129 | + final visible = find.byType(SmartTextWidget).evaluate().length; |
| 130 | + expect(visible, greaterThan(10)); |
| 131 | + |
| 132 | + final first = await countLineBuilds(() async { |
| 133 | + await emit(tester, bloc, _state(selectedIndex: 3)); |
| 134 | + }); |
| 135 | + expect(first, lessThanOrEqualTo(1)); |
| 136 | + expect(gutterOn(3), findsOneWidget); |
| 137 | + |
| 138 | + final second = await countLineBuilds(() async { |
| 139 | + await emit(tester, bloc, _state(selectedIndex: 5)); |
| 140 | + }); |
| 141 | + expect(second, lessThanOrEqualTo(2)); |
| 142 | + expect(gutterOn(5), findsOneWidget); |
| 143 | + expect(gutterOn(3), findsNothing); |
| 144 | + |
| 145 | + final cleared = await countLineBuilds(() async { |
| 146 | + await emit(tester, bloc, _state()); |
| 147 | + }); |
| 148 | + expect(cleared, lessThanOrEqualTo(1)); |
| 149 | + expect(gutterOn(5), findsNothing); |
| 150 | + }); |
| 151 | + |
| 152 | + testWidgets('שינוי שאינו בחירה עדיין בונה את כל השורות הגלויות', ( |
| 153 | + tester, |
| 154 | + ) async { |
| 155 | + final bloc = await mount(tester); |
| 156 | + await emit(tester, bloc, _state(selectedIndex: 3)); |
| 157 | + final visible = find.byType(SmartTextWidget).evaluate().length; |
| 158 | + |
| 159 | + final builds = await countLineBuilds(() async { |
| 160 | + await emit(tester, bloc, _state(selectedIndex: 3, highlightedLine: 6)); |
| 161 | + }); |
| 162 | + expect(builds, greaterThanOrEqualTo(visible)); |
| 163 | + expect(gutterOn(3), findsOneWidget); |
| 164 | + }); |
| 165 | +} |
0 commit comments