|
1 | 1 | import 'package:flutter/gestures.dart'; |
2 | 2 | import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter/rendering.dart'; |
3 | 4 | import 'package:flutter/services.dart'; |
4 | 5 | import 'package:flutter_test/flutter_test.dart'; |
5 | 6 | import 'package:otzaria/widgets/text/rtl_selection_shortcuts.dart'; |
| 7 | +import 'package:otzaria/widgets/selection/item_selection_container.dart'; |
| 8 | +import 'package:otzaria/widgets/selection/paragraph_selection_container.dart'; |
6 | 9 |
|
7 | 10 | /// בודק את [RtlSelectionShortcuts]: Shift+חץ פועל תמיד על קצה ה-focus (לעולם |
8 | 11 | /// לא משחית את ה-anchor/תחילת המחרוזת), ובכיוון הנכון — בשורה אחת (דרך |
@@ -526,6 +529,200 @@ void main() { |
526 | 529 | }); |
527 | 530 | }); |
528 | 531 |
|
| 532 | + group('כיוון הטקסט בשורה אחת בתוך אזור RTL', () { |
| 533 | + for (final selection in const [ |
| 534 | + ('אנגלית', 'abcde fghij klmno', 0, 4), |
| 535 | + ('אנגלית בגרירה הפוכה', 'abcde fghij klmno', 4, 0), |
| 536 | + ('אנגלית בגבולות הפסקה', 'abcde fghij klmno', 0, 17), |
| 537 | + ('אנגלית בתוך עברית', 'אבגדה abcde fghij וזחטי', 6, 10), |
| 538 | + ('אנגלית בתוך עברית בגרירה הפוכה', 'אבגדה abcde fghij וזחטי', 10, 6), |
| 539 | + ('קצוות בכיוונים שונים', 'אבגדה abcde fghij וזחטי', 2, 10), |
| 540 | + ('קצוות בכיוונים שונים בגרירה הפוכה', 'אבגדה abcde fghij וזחטי', 10, 2), |
| 541 | + ('עברית סביב אנגלית', 'אבגדה abcde fghij וזחטי', 2, 20), |
| 542 | + ('עברית סביב אנגלית בגרירה הפוכה', 'אבגדה abcde fghij וזחטי', 20, 2), |
| 543 | + ('עברית', 'אבגדה והוזח טיכלמ', 0, 4), |
| 544 | + ('עברית בגרירה הפוכה', 'אבגדה והוזח טיכלמ', 4, 0), |
| 545 | + ]) { |
| 546 | + testWidgets(selection.$1, (tester) async { |
| 547 | + Future<({String before, String after})> extend({ |
| 548 | + required bool corrected, |
| 549 | + }) async { |
| 550 | + String? selected; |
| 551 | + final region = SelectableRegion( |
| 552 | + selectionControls: emptyTextSelectionControls, |
| 553 | + onSelectionChanged: (value) { |
| 554 | + if (!rtlSelectionPriming) selected = value?.plainText; |
| 555 | + }, |
| 556 | + child: Column( |
| 557 | + mainAxisSize: MainAxisSize.min, |
| 558 | + children: [ |
| 559 | + Text( |
| 560 | + selection.$2, |
| 561 | + textDirection: selection.$2.startsWith('abcde') |
| 562 | + ? TextDirection.ltr |
| 563 | + : null, |
| 564 | + style: const TextStyle(fontSize: 24), |
| 565 | + ), |
| 566 | + const Text( |
| 567 | + 'שורה שניה abcde fghij', |
| 568 | + style: TextStyle(fontSize: 24), |
| 569 | + ), |
| 570 | + ], |
| 571 | + ), |
| 572 | + ); |
| 573 | + await tester.pumpWidget( |
| 574 | + MaterialApp( |
| 575 | + home: Scaffold( |
| 576 | + body: Directionality( |
| 577 | + textDirection: TextDirection.rtl, |
| 578 | + child: Center( |
| 579 | + child: SizedBox( |
| 580 | + width: 600, |
| 581 | + child: corrected |
| 582 | + ? RtlSelectionShortcuts(child: region) |
| 583 | + : region, |
| 584 | + ), |
| 585 | + ), |
| 586 | + ), |
| 587 | + ), |
| 588 | + ), |
| 589 | + ); |
| 590 | + await tester.pumpAndSettle(); |
| 591 | + final paragraph = tester.renderObject<RenderParagraph>( |
| 592 | + find.descendant( |
| 593 | + of: find.text(selection.$2), |
| 594 | + matching: find.byType(RichText), |
| 595 | + ), |
| 596 | + ); |
| 597 | + Offset caret(int offset) { |
| 598 | + final atEnd = offset == selection.$2.length; |
| 599 | + final box = paragraph |
| 600 | + .getBoxesForSelection( |
| 601 | + TextSelection( |
| 602 | + baseOffset: atEnd ? offset - 1 : offset, |
| 603 | + extentOffset: atEnd ? offset : offset + 1, |
| 604 | + ), |
| 605 | + ) |
| 606 | + .first; |
| 607 | + return paragraph.localToGlobal( |
| 608 | + Offset( |
| 609 | + (atEnd ? box.end : box.start) + |
| 610 | + (atEnd ? -1 : 1) * |
| 611 | + (box.direction == TextDirection.ltr ? 1 : -1), |
| 612 | + (box.top + box.bottom) / 2, |
| 613 | + ), |
| 614 | + ); |
| 615 | + } |
| 616 | + |
| 617 | + final gesture = await tester.startGesture( |
| 618 | + caret(selection.$3), |
| 619 | + kind: PointerDeviceKind.mouse, |
| 620 | + ); |
| 621 | + addTearDown(gesture.removePointer); |
| 622 | + await tester.pump(); |
| 623 | + await gesture.moveTo(caret(selection.$4)); |
| 624 | + await tester.pump(); |
| 625 | + await gesture.up(); |
| 626 | + await tester.pumpAndSettle(); |
| 627 | + final before = selected; |
| 628 | + expect(before, isNotEmpty); |
| 629 | + await shiftPress(tester, LogicalKeyboardKey.arrowDown); |
| 630 | + expect(selected, isNotNull); |
| 631 | + final result = (before: before!, after: selected!); |
| 632 | + await tester.pumpWidget(const SizedBox.shrink()); |
| 633 | + return result; |
| 634 | + } |
| 635 | + |
| 636 | + if (selection.$1.startsWith('עברית')) { |
| 637 | + final result = await extend(corrected: true); |
| 638 | + expect(result.after, contains(result.before)); |
| 639 | + } else { |
| 640 | + final baseline = await extend(corrected: false); |
| 641 | + final corrected = await extend(corrected: true); |
| 642 | + expect(corrected.before, baseline.before); |
| 643 | + expect(corrected.after, baseline.after); |
| 644 | + } |
| 645 | + }); |
| 646 | + } |
| 647 | + }); |
| 648 | + |
| 649 | + for (final reversed in [false, true]) { |
| 650 | + testWidgets('אותה שורה בין containers נשארת כפי שהייתה, הפוכה=$reversed', ( |
| 651 | + tester, |
| 652 | + ) async { |
| 653 | + String? baselineBefore; |
| 654 | + String? baselineAfter; |
| 655 | + for (final corrected in [false, true]) { |
| 656 | + String? selected; |
| 657 | + final region = SelectableRegion( |
| 658 | + selectionControls: emptyTextSelectionControls, |
| 659 | + onSelectionChanged: (value) { |
| 660 | + if (!rtlSelectionPriming) selected = value?.plainText; |
| 661 | + }, |
| 662 | + child: Column( |
| 663 | + mainAxisSize: MainAxisSize.min, |
| 664 | + children: [ |
| 665 | + ItemSelectionContainer( |
| 666 | + child: Row( |
| 667 | + mainAxisSize: MainAxisSize.min, |
| 668 | + children: const [ |
| 669 | + ParagraphSelectionContainer( |
| 670 | + child: Text('אבגדה', style: TextStyle(fontSize: 24)), |
| 671 | + ), |
| 672 | + ParagraphSelectionContainer( |
| 673 | + child: Text('וזחטי', style: TextStyle(fontSize: 24)), |
| 674 | + ), |
| 675 | + ], |
| 676 | + ), |
| 677 | + ), |
| 678 | + const Text('קלמנס עפרשת', style: TextStyle(fontSize: 24)), |
| 679 | + ], |
| 680 | + ), |
| 681 | + ); |
| 682 | + await tester.pumpWidget( |
| 683 | + MaterialApp( |
| 684 | + home: Scaffold( |
| 685 | + body: Directionality( |
| 686 | + textDirection: TextDirection.rtl, |
| 687 | + child: Center( |
| 688 | + child: corrected |
| 689 | + ? RtlSelectionShortcuts(child: region) |
| 690 | + : region, |
| 691 | + ), |
| 692 | + ), |
| 693 | + ), |
| 694 | + ), |
| 695 | + ); |
| 696 | + await tester.pumpAndSettle(); |
| 697 | + final first = tester.getRect(find.text('אבגדה')); |
| 698 | + final last = tester.getRect(find.text('וזחטי')); |
| 699 | + final from = Offset(first.right - 24, first.center.dy); |
| 700 | + final to = Offset(last.left + 24, last.center.dy); |
| 701 | + final gesture = await tester.startGesture( |
| 702 | + reversed ? to : from, |
| 703 | + kind: PointerDeviceKind.mouse, |
| 704 | + ); |
| 705 | + addTearDown(gesture.removePointer); |
| 706 | + await tester.pump(); |
| 707 | + await gesture.moveTo(reversed ? from : to); |
| 708 | + await tester.pump(); |
| 709 | + await gesture.up(); |
| 710 | + await tester.pumpAndSettle(); |
| 711 | + final before = selected; |
| 712 | + expect(before, isNotEmpty); |
| 713 | + await shiftPress(tester, LogicalKeyboardKey.arrowDown); |
| 714 | + if (corrected) { |
| 715 | + expect(before, baselineBefore); |
| 716 | + expect(selected, baselineAfter); |
| 717 | + } else { |
| 718 | + baselineBefore = before; |
| 719 | + baselineAfter = selected; |
| 720 | + } |
| 721 | + await tester.pumpWidget(const SizedBox.shrink()); |
| 722 | + } |
| 723 | + }); |
| 724 | + } |
| 725 | + |
529 | 726 | testWidgets('LTR: ה-widget שקוף ואינו מוסיף Shortcuts', (tester) async { |
530 | 727 | await tester.pumpWidget( |
531 | 728 | const Directionality( |
|
0 commit comments