OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/strings/string16.h" | 5 #include "base/strings/string16.h" |
6 #include "base/strings/utf_string_conversions.h" | 6 #include "base/strings/utf_string_conversions.h" |
7 #include "content/browser/accessibility/browser_accessibility.h" | 7 #include "content/browser/accessibility/browser_accessibility.h" |
8 #include "content/browser/accessibility/browser_accessibility_manager.h" | 8 #include "content/browser/accessibility/browser_accessibility_manager.h" |
9 #include "content/common/accessibility_messages.h" | 9 #include "content/common/accessibility_messages.h" |
10 #include "content/common/accessibility_node_data.h" | 10 #include "content/common/accessibility_node_data.h" |
(...skipping 593 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
604 factory = new CountedBrowserAccessibilityFactory(); | 604 factory = new CountedBrowserAccessibilityFactory(); |
605 manager.reset(BrowserAccessibilityManager::Create( | 605 manager.reset(BrowserAccessibilityManager::Create( |
606 root2, | 606 root2, |
607 delegate.get(), | 607 delegate.get(), |
608 factory)); | 608 factory)); |
609 ASSERT_FALSE(delegate->got_fatal_error()); | 609 ASSERT_FALSE(delegate->got_fatal_error()); |
610 manager->UpdateNodesForTesting(child1, child2); | 610 manager->UpdateNodesForTesting(child1, child2); |
611 ASSERT_TRUE(delegate->got_fatal_error()); | 611 ASSERT_TRUE(delegate->got_fatal_error()); |
612 } | 612 } |
613 | 613 |
| 614 TEST(BrowserAccessibilityManagerTest, BoundsForRange) { |
| 615 AccessibilityNodeData root; |
| 616 root.id = 1; |
| 617 root.role = WebKit::WebAXRoleRootWebArea; |
| 618 |
| 619 AccessibilityNodeData static_text; |
| 620 static_text.id = 2; |
| 621 static_text.SetValue("Hello, world."); |
| 622 static_text.role = WebKit::WebAXRoleStaticText; |
| 623 static_text.location = gfx::Rect(100, 100, 29, 18); |
| 624 root.child_ids.push_back(2); |
| 625 |
| 626 AccessibilityNodeData inline_text1; |
| 627 inline_text1.id = 3; |
| 628 inline_text1.SetValue("Hello, "); |
| 629 inline_text1.role = WebKit::WebAXRoleInlineTextBox; |
| 630 inline_text1.location = gfx::Rect(100, 100, 29, 9); |
| 631 inline_text1.AddIntAttribute(AccessibilityNodeData::ATTR_TEXT_DIRECTION, |
| 632 WebKit::WebAXTextDirectionLR); |
| 633 std::vector<int32> character_offsets1; |
| 634 character_offsets1.push_back(6); // 0 |
| 635 character_offsets1.push_back(11); // 1 |
| 636 character_offsets1.push_back(16); // 2 |
| 637 character_offsets1.push_back(21); // 3 |
| 638 character_offsets1.push_back(26); // 4 |
| 639 character_offsets1.push_back(29); // 5 |
| 640 character_offsets1.push_back(29); // 6 (note that the space has no width) |
| 641 inline_text1.AddIntListAttribute( |
| 642 AccessibilityNodeData::ATTR_CHARACTER_OFFSETS, character_offsets1); |
| 643 static_text.child_ids.push_back(3); |
| 644 |
| 645 AccessibilityNodeData inline_text2; |
| 646 inline_text2.id = 4; |
| 647 inline_text2.SetValue("world."); |
| 648 inline_text2.role = WebKit::WebAXRoleInlineTextBox; |
| 649 inline_text2.location = gfx::Rect(100, 109, 28, 9); |
| 650 inline_text2.AddIntAttribute(AccessibilityNodeData::ATTR_TEXT_DIRECTION, |
| 651 WebKit::WebAXTextDirectionLR); |
| 652 std::vector<int32> character_offsets2; |
| 653 character_offsets2.push_back(5); |
| 654 character_offsets2.push_back(10); |
| 655 character_offsets2.push_back(15); |
| 656 character_offsets2.push_back(20); |
| 657 character_offsets2.push_back(25); |
| 658 character_offsets2.push_back(28); |
| 659 inline_text2.AddIntListAttribute( |
| 660 AccessibilityNodeData::ATTR_CHARACTER_OFFSETS, character_offsets2); |
| 661 static_text.child_ids.push_back(4); |
| 662 |
| 663 scoped_ptr<BrowserAccessibilityManager> manager( |
| 664 BrowserAccessibilityManager::Create( |
| 665 root, |
| 666 NULL, |
| 667 new CountedBrowserAccessibilityFactory())); |
| 668 manager->UpdateNodesForTesting(static_text, inline_text1, inline_text2); |
| 669 |
| 670 BrowserAccessibility* root_accessible = manager->GetRoot(); |
| 671 BrowserAccessibility* static_text_accessible = |
| 672 root_accessible->PlatformGetChild(0); |
| 673 |
| 674 EXPECT_EQ(gfx::Rect(100, 100, 6, 9).ToString(), |
| 675 static_text_accessible->GetLocalBoundsForRange(0, 1).ToString()); |
| 676 |
| 677 EXPECT_EQ(gfx::Rect(100, 100, 26, 9).ToString(), |
| 678 static_text_accessible->GetLocalBoundsForRange(0, 5).ToString()); |
| 679 |
| 680 EXPECT_EQ(gfx::Rect(100, 109, 5, 9).ToString(), |
| 681 static_text_accessible->GetLocalBoundsForRange(7, 1).ToString()); |
| 682 |
| 683 EXPECT_EQ(gfx::Rect(100, 109, 25, 9).ToString(), |
| 684 static_text_accessible->GetLocalBoundsForRange(7, 5).ToString()); |
| 685 |
| 686 EXPECT_EQ(gfx::Rect(100, 100, 29, 18).ToString(), |
| 687 static_text_accessible->GetLocalBoundsForRange(5, 3).ToString()); |
| 688 |
| 689 EXPECT_EQ(gfx::Rect(100, 100, 29, 18).ToString(), |
| 690 static_text_accessible->GetLocalBoundsForRange(0, 13).ToString()); |
| 691 |
| 692 // Test range that's beyond the text. |
| 693 EXPECT_EQ(gfx::Rect(100, 100, 29, 18).ToString(), |
| 694 static_text_accessible->GetLocalBoundsForRange(-1, 999).ToString()); |
| 695 } |
| 696 |
| 697 TEST(BrowserAccessibilityManagerTest, BoundsForRangeBiDi) { |
| 698 // In this example, we assume that the string "123abc" is rendered with |
| 699 // "123" going left-to-right and "abc" going right-to-left. In other |
| 700 // words, on-screen it would look like "123cba". This is possible to |
| 701 // acheive if the source string had unicode control characters |
| 702 // to switch directions. This test doesn't worry about how, though - it just |
| 703 // tests that if something like that were to occur, GetLocalBoundsForRange |
| 704 // returns the correct bounds for different ranges. |
| 705 |
| 706 AccessibilityNodeData root; |
| 707 root.id = 1; |
| 708 root.role = WebKit::WebAXRoleRootWebArea; |
| 709 |
| 710 AccessibilityNodeData static_text; |
| 711 static_text.id = 2; |
| 712 static_text.SetValue("123abc"); |
| 713 static_text.role = WebKit::WebAXRoleStaticText; |
| 714 static_text.location = gfx::Rect(100, 100, 60, 20); |
| 715 root.child_ids.push_back(2); |
| 716 |
| 717 AccessibilityNodeData inline_text1; |
| 718 inline_text1.id = 3; |
| 719 inline_text1.SetValue("123"); |
| 720 inline_text1.role = WebKit::WebAXRoleInlineTextBox; |
| 721 inline_text1.location = gfx::Rect(100, 100, 30, 20); |
| 722 inline_text1.AddIntAttribute(AccessibilityNodeData::ATTR_TEXT_DIRECTION, |
| 723 WebKit::WebAXTextDirectionLR); |
| 724 std::vector<int32> character_offsets1; |
| 725 character_offsets1.push_back(10); // 0 |
| 726 character_offsets1.push_back(20); // 1 |
| 727 character_offsets1.push_back(30); // 2 |
| 728 inline_text1.AddIntListAttribute( |
| 729 AccessibilityNodeData::ATTR_CHARACTER_OFFSETS, character_offsets1); |
| 730 static_text.child_ids.push_back(3); |
| 731 |
| 732 AccessibilityNodeData inline_text2; |
| 733 inline_text2.id = 4; |
| 734 inline_text2.SetValue("abc"); |
| 735 inline_text2.role = WebKit::WebAXRoleInlineTextBox; |
| 736 inline_text2.location = gfx::Rect(130, 100, 30, 20); |
| 737 inline_text2.AddIntAttribute(AccessibilityNodeData::ATTR_TEXT_DIRECTION, |
| 738 WebKit::WebAXTextDirectionRL); |
| 739 std::vector<int32> character_offsets2; |
| 740 character_offsets2.push_back(10); |
| 741 character_offsets2.push_back(20); |
| 742 character_offsets2.push_back(30); |
| 743 inline_text2.AddIntListAttribute( |
| 744 AccessibilityNodeData::ATTR_CHARACTER_OFFSETS, character_offsets2); |
| 745 static_text.child_ids.push_back(4); |
| 746 |
| 747 scoped_ptr<BrowserAccessibilityManager> manager( |
| 748 BrowserAccessibilityManager::Create( |
| 749 root, |
| 750 NULL, |
| 751 new CountedBrowserAccessibilityFactory())); |
| 752 manager->UpdateNodesForTesting(static_text, inline_text1, inline_text2); |
| 753 |
| 754 BrowserAccessibility* root_accessible = manager->GetRoot(); |
| 755 BrowserAccessibility* static_text_accessible = |
| 756 root_accessible->PlatformGetChild(0); |
| 757 |
| 758 EXPECT_EQ(gfx::Rect(100, 100, 60, 20).ToString(), |
| 759 static_text_accessible->GetLocalBoundsForRange(0, 6).ToString()); |
| 760 |
| 761 EXPECT_EQ(gfx::Rect(100, 100, 10, 20).ToString(), |
| 762 static_text_accessible->GetLocalBoundsForRange(0, 1).ToString()); |
| 763 |
| 764 EXPECT_EQ(gfx::Rect(100, 100, 30, 20).ToString(), |
| 765 static_text_accessible->GetLocalBoundsForRange(0, 3).ToString()); |
| 766 |
| 767 EXPECT_EQ(gfx::Rect(150, 100, 10, 20).ToString(), |
| 768 static_text_accessible->GetLocalBoundsForRange(3, 1).ToString()); |
| 769 |
| 770 EXPECT_EQ(gfx::Rect(130, 100, 30, 20).ToString(), |
| 771 static_text_accessible->GetLocalBoundsForRange(3, 3).ToString()); |
| 772 |
| 773 // This range is only two characters, but because of the direction switch |
| 774 // the bounds are as wide as four characters. |
| 775 EXPECT_EQ(gfx::Rect(120, 100, 40, 20).ToString(), |
| 776 static_text_accessible->GetLocalBoundsForRange(2, 2).ToString()); |
| 777 } |
| 778 |
614 } // namespace content | 779 } // namespace content |
OLD | NEW |