| 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 "ui/views/focus/focus_manager.h" | 5 #include "ui/views/focus/focus_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 FocusSearch* focus_search_; | 127 FocusSearch* focus_search_; |
| 128 }; | 128 }; |
| 129 | 129 |
| 130 // BorderView is a view containing a native window with its own view hierarchy. | 130 // BorderView is a view containing a native window with its own view hierarchy. |
| 131 // It is interesting to test focus traversal from a view hierarchy to an inner | 131 // It is interesting to test focus traversal from a view hierarchy to an inner |
| 132 // view hierarchy. | 132 // view hierarchy. |
| 133 class BorderView : public NativeViewHost { | 133 class BorderView : public NativeViewHost { |
| 134 public: | 134 public: |
| 135 explicit BorderView(View* child) : child_(child), widget_(NULL) { | 135 explicit BorderView(View* child) : child_(child), widget_(NULL) { |
| 136 DCHECK(child); | 136 DCHECK(child); |
| 137 SetFocusable(false); | 137 SetFocusBehavior(View::FocusBehavior::NEVER); |
| 138 } | 138 } |
| 139 | 139 |
| 140 ~BorderView() override {} | 140 ~BorderView() override {} |
| 141 | 141 |
| 142 virtual internal::RootView* GetContentsRootView() { | 142 virtual internal::RootView* GetContentsRootView() { |
| 143 return static_cast<internal::RootView*>(widget_->GetRootView()); | 143 return static_cast<internal::RootView*>(widget_->GetRootView()); |
| 144 } | 144 } |
| 145 | 145 |
| 146 FocusTraversable* GetFocusTraversable() override { | 146 FocusTraversable* GetFocusTraversable() override { |
| 147 return static_cast<internal::RootView*>(widget_->GetRootView()); | 147 return static_cast<internal::RootView*>(widget_->GetRootView()); |
| (...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 | 536 |
| 537 search_border_view_ = new BorderView(contents); | 537 search_border_view_ = new BorderView(contents); |
| 538 search_border_view_->set_id(kSearchContainerID); | 538 search_border_view_->set_id(kSearchContainerID); |
| 539 | 539 |
| 540 GetContentsView()->AddChildView(search_border_view_); | 540 GetContentsView()->AddChildView(search_border_view_); |
| 541 search_border_view_->SetBounds(300, y, 240, 50); | 541 search_border_view_->SetBounds(300, y, 240, 50); |
| 542 | 542 |
| 543 y += 60; | 543 y += 60; |
| 544 | 544 |
| 545 contents = new View(); | 545 contents = new View(); |
| 546 contents->SetFocusable(true); | 546 contents->SetFocusBehavior(View::FocusBehavior::ALWAYS); |
| 547 contents->set_background(Background::CreateSolidBackground(SK_ColorBLUE)); | 547 contents->set_background(Background::CreateSolidBackground(SK_ColorBLUE)); |
| 548 contents->set_id(kThumbnailContainerID); | 548 contents->set_id(kThumbnailContainerID); |
| 549 button = new LabelButton(NULL, ASCIIToUTF16("Star")); | 549 button = new LabelButton(NULL, ASCIIToUTF16("Star")); |
| 550 button->SetStyle(Button::STYLE_BUTTON); | 550 button->SetStyle(Button::STYLE_BUTTON); |
| 551 contents->AddChildView(button); | 551 contents->AddChildView(button); |
| 552 button->SetBounds(5, 5, 50, 30); | 552 button->SetBounds(5, 5, 50, 30); |
| 553 button->set_id(kThumbnailStarID); | 553 button->set_id(kThumbnailStarID); |
| 554 button = new LabelButton(NULL, ASCIIToUTF16("SuperStar")); | 554 button = new LabelButton(NULL, ASCIIToUTF16("SuperStar")); |
| 555 button->SetStyle(Button::STYLE_BUTTON); | 555 button->SetStyle(Button::STYLE_BUTTON); |
| 556 contents->AddChildView(button); | 556 contents->AddChildView(button); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 596 for (int j = arraysize(kTraversalIDs) - 1; j >= 0; --j) { | 596 for (int j = arraysize(kTraversalIDs) - 1; j >= 0; --j) { |
| 597 GetFocusManager()->AdvanceFocus(true); | 597 GetFocusManager()->AdvanceFocus(true); |
| 598 View* focused_view = GetFocusManager()->GetFocusedView(); | 598 View* focused_view = GetFocusManager()->GetFocusedView(); |
| 599 EXPECT_TRUE(focused_view != NULL); | 599 EXPECT_TRUE(focused_view != NULL); |
| 600 if (focused_view) | 600 if (focused_view) |
| 601 EXPECT_EQ(kTraversalIDs[j], focused_view->id()); | 601 EXPECT_EQ(kTraversalIDs[j], focused_view->id()); |
| 602 } | 602 } |
| 603 } | 603 } |
| 604 } | 604 } |
| 605 | 605 |
| 606 #if defined(OS_MACOSX) |
| 607 // Test focus traversal with full keyboard access off on Mac. |
| 608 TEST_F(FocusTraversalTest, NormalTraversalMac) { |
| 609 GetFocusManager()->SetKeyboardAccessible(false); |
| 610 |
| 611 // Now only views with FocusBehavior of ALWAYS will be focusable. |
| 612 const int kTraversalIDs[] = { kAppleTextfieldID, |
| 613 kOrangeTextfieldID, kBananaTextfieldID, kKiwiTextfieldID, |
| 614 kStyleTextEditID, |
| 615 kSearchTextfieldID, |
| 616 kThumbnailContainerID}; |
| 617 |
| 618 // Let's traverse the whole focus hierarchy (several times, to make sure it |
| 619 // loops OK). |
| 620 GetFocusManager()->ClearFocus(); |
| 621 for (int i = 0; i < 3; ++i) { |
| 622 for (size_t j = 0; j < arraysize(kTraversalIDs); j++) { |
| 623 GetFocusManager()->AdvanceFocus(false); |
| 624 View* focused_view = GetFocusManager()->GetFocusedView(); |
| 625 EXPECT_TRUE(focused_view != NULL); |
| 626 if (focused_view) |
| 627 EXPECT_EQ(kTraversalIDs[j], focused_view->id()); |
| 628 } |
| 629 } |
| 630 |
| 631 // Let's traverse in reverse order. |
| 632 GetFocusManager()->ClearFocus(); |
| 633 for (int i = 0; i < 3; ++i) { |
| 634 for (int j = arraysize(kTraversalIDs) - 1; j >= 0; --j) { |
| 635 GetFocusManager()->AdvanceFocus(true); |
| 636 View* focused_view = GetFocusManager()->GetFocusedView(); |
| 637 EXPECT_TRUE(focused_view != NULL); |
| 638 if (focused_view) |
| 639 EXPECT_EQ(kTraversalIDs[j], focused_view->id()); |
| 640 } |
| 641 } |
| 642 } |
| 643 |
| 644 // Test toggling full keyboard access correctly changes the focused view on Mac. |
| 645 TEST_F(FocusTraversalTest, FullKeyboardToggle) { |
| 646 // Give focus to kTopCheckBoxID . |
| 647 FindViewByID(kTopCheckBoxID)->RequestFocus(); |
| 648 EXPECT_EQ(kTopCheckBoxID, GetFocusManager()->GetFocusedView()->id()); |
| 649 |
| 650 // Turn off full keyboard access. Focus should move to next view with ALWAYS |
| 651 // focus behavior. |
| 652 GetFocusManager()->SetKeyboardAccessible(false); |
| 653 EXPECT_EQ(kAppleTextfieldID, GetFocusManager()->GetFocusedView()->id()); |
| 654 |
| 655 // Turning on full keyboard access should not change the focused view. |
| 656 GetFocusManager()->SetKeyboardAccessible(true); |
| 657 EXPECT_EQ(kAppleTextfieldID, GetFocusManager()->GetFocusedView()->id()); |
| 658 |
| 659 // Give focus to kSearchButtonID. |
| 660 FindViewByID(kSearchButtonID)->RequestFocus(); |
| 661 EXPECT_EQ(kSearchButtonID, GetFocusManager()->GetFocusedView()->id()); |
| 662 |
| 663 // Turn off full keyboard access. Focus should move to next view with ALWAYS |
| 664 // focus behavior. |
| 665 GetFocusManager()->SetKeyboardAccessible(false); |
| 666 EXPECT_EQ(kThumbnailContainerID, GetFocusManager()->GetFocusedView()->id()); |
| 667 |
| 668 // See focus advances correctly in both directions. |
| 669 GetFocusManager()->AdvanceFocus(false); |
| 670 EXPECT_EQ(kAppleTextfieldID, GetFocusManager()->GetFocusedView()->id()); |
| 671 |
| 672 GetFocusManager()->AdvanceFocus(true); |
| 673 EXPECT_EQ(kThumbnailContainerID, GetFocusManager()->GetFocusedView()->id()); |
| 674 } |
| 675 #endif |
| 676 |
| 606 TEST_F(FocusTraversalTest, TraversalWithNonEnabledViews) { | 677 TEST_F(FocusTraversalTest, TraversalWithNonEnabledViews) { |
| 607 const int kDisabledIDs[] = { | 678 const int kDisabledIDs[] = { |
| 608 kBananaTextfieldID, kFruitCheckBoxID, kComboboxID, kAsparagusButtonID, | 679 kBananaTextfieldID, kFruitCheckBoxID, kComboboxID, kAsparagusButtonID, |
| 609 kCauliflowerButtonID, kClosetLinkID, kVisitingLinkID, kBriceDeNiceLinkID, | 680 kCauliflowerButtonID, kClosetLinkID, kVisitingLinkID, kBriceDeNiceLinkID, |
| 610 kTaxiLinkID, kAsterixLinkID, kHelpButtonID, kBoldCheckBoxID, | 681 kTaxiLinkID, kAsterixLinkID, kHelpButtonID, kBoldCheckBoxID, |
| 611 kSearchTextfieldID, kHelpLinkID }; | 682 kSearchTextfieldID, kHelpLinkID }; |
| 612 | 683 |
| 613 const int kTraversalIDs[] = { kTopCheckBoxID, kAppleTextfieldID, | 684 const int kTraversalIDs[] = { kTopCheckBoxID, kAppleTextfieldID, |
| 614 kOrangeTextfieldID, kKiwiTextfieldID, kFruitButtonID, kBroccoliButtonID, | 685 kOrangeTextfieldID, kKiwiTextfieldID, kFruitButtonID, kBroccoliButtonID, |
| 615 kRosettaLinkID, kStupeurEtTremblementLinkID, kDinerGameLinkID, | 686 kRosettaLinkID, kStupeurEtTremblementLinkID, kDinerGameLinkID, |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 740 // Now test the right container, but this time with accessibility mode. | 811 // Now test the right container, but this time with accessibility mode. |
| 741 // Make some links not focusable, but mark one of them as | 812 // Make some links not focusable, but mark one of them as |
| 742 // "accessibility focusable", so it should show up in the traversal. | 813 // "accessibility focusable", so it should show up in the traversal. |
| 743 const int kRightTraversalIDs[] = { | 814 const int kRightTraversalIDs[] = { |
| 744 kBroccoliButtonID, kDinerGameLinkID, kRidiculeLinkID, | 815 kBroccoliButtonID, kDinerGameLinkID, kRidiculeLinkID, |
| 745 kClosetLinkID, kVisitingLinkID, kAmelieLinkID, kJoyeuxNoelLinkID, | 816 kClosetLinkID, kVisitingLinkID, kAmelieLinkID, kJoyeuxNoelLinkID, |
| 746 kCampingLinkID, kBriceDeNiceLinkID, kTaxiLinkID, kAsterixLinkID }; | 817 kCampingLinkID, kBriceDeNiceLinkID, kTaxiLinkID, kAsterixLinkID }; |
| 747 | 818 |
| 748 FocusSearch focus_search_right(right_container_, true, true); | 819 FocusSearch focus_search_right(right_container_, true, true); |
| 749 right_container_->EnablePaneFocus(&focus_search_right); | 820 right_container_->EnablePaneFocus(&focus_search_right); |
| 750 FindViewByID(kRosettaLinkID)->SetFocusable(false); | 821 FindViewByID(kRosettaLinkID)->SetFocusBehavior(View::FocusBehavior::NEVER); |
| 751 FindViewByID(kStupeurEtTremblementLinkID)->SetFocusable(false); | 822 FindViewByID(kStupeurEtTremblementLinkID) |
| 752 FindViewByID(kDinerGameLinkID)->SetAccessibilityFocusable(true); | 823 ->SetFocusBehavior(View::FocusBehavior::NEVER); |
| 753 FindViewByID(kDinerGameLinkID)->SetFocusable(false); | 824 FindViewByID(kDinerGameLinkID) |
| 825 ->SetFocusBehavior(View::FocusBehavior::ACCESSIBLE_ONLY); |
| 754 FindViewByID(kAsterixLinkID)->RequestFocus(); | 826 FindViewByID(kAsterixLinkID)->RequestFocus(); |
| 755 | 827 |
| 756 // Traverse the focus hierarchy within the pane several times. | 828 // Traverse the focus hierarchy within the pane several times. |
| 757 for (int i = 0; i < 3; ++i) { | 829 for (int i = 0; i < 3; ++i) { |
| 758 for (size_t j = 0; j < arraysize(kRightTraversalIDs); j++) { | 830 for (size_t j = 0; j < arraysize(kRightTraversalIDs); j++) { |
| 759 GetFocusManager()->AdvanceFocus(false); | 831 GetFocusManager()->AdvanceFocus(false); |
| 760 View* focused_view = GetFocusManager()->GetFocusedView(); | 832 View* focused_view = GetFocusManager()->GetFocusedView(); |
| 761 EXPECT_TRUE(focused_view != NULL); | 833 EXPECT_TRUE(focused_view != NULL); |
| 762 if (focused_view) | 834 if (focused_view) |
| 763 EXPECT_EQ(kRightTraversalIDs[j], focused_view->id()); | 835 EXPECT_EQ(kRightTraversalIDs[j], focused_view->id()); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 846 GetFocusManager()->AdvanceFocus(false); | 918 GetFocusManager()->AdvanceFocus(false); |
| 847 EXPECT_FALSE(GetFocusManager()->GetFocusedView()); | 919 EXPECT_FALSE(GetFocusManager()->GetFocusedView()); |
| 848 | 920 |
| 849 // Advance backwards from the root node. | 921 // Advance backwards from the root node. |
| 850 GetFocusManager()->ClearFocus(); | 922 GetFocusManager()->ClearFocus(); |
| 851 GetFocusManager()->AdvanceFocus(true); | 923 GetFocusManager()->AdvanceFocus(true); |
| 852 EXPECT_FALSE(GetFocusManager()->GetFocusedView()); | 924 EXPECT_FALSE(GetFocusManager()->GetFocusedView()); |
| 853 } | 925 } |
| 854 | 926 |
| 855 } // namespace views | 927 } // namespace views |
| OLD | NEW |