OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "mojo/services/view_manager/focus_controller.h" |
| 6 |
| 7 #include "mojo/services/view_manager/focus_controller_delegate.h" |
| 8 #include "mojo/services/view_manager/server_view.h" |
| 9 #include "mojo/services/view_manager/test_server_view_delegate.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace view_manager { |
| 13 namespace { |
| 14 |
| 15 class TestFocusControllerDelegate : public FocusControllerDelegate { |
| 16 public: |
| 17 TestFocusControllerDelegate() |
| 18 : change_count_(0u), |
| 19 old_focused_view_(nullptr), |
| 20 new_focused_view_(nullptr) {} |
| 21 |
| 22 void ClearAll() { |
| 23 change_count_ = 0u; |
| 24 old_focused_view_ = nullptr; |
| 25 new_focused_view_ = nullptr; |
| 26 } |
| 27 size_t change_count() const { return change_count_; } |
| 28 ServerView* old_focused_view() { return old_focused_view_; } |
| 29 ServerView* new_focused_view() { return new_focused_view_; } |
| 30 |
| 31 private: |
| 32 // FocusControllerDelegate: |
| 33 void OnFocusChanged(ServerView* old_focused_view, |
| 34 ServerView* new_focused_view) override { |
| 35 change_count_++; |
| 36 old_focused_view_ = old_focused_view; |
| 37 new_focused_view_ = new_focused_view; |
| 38 } |
| 39 |
| 40 size_t change_count_; |
| 41 ServerView* old_focused_view_; |
| 42 ServerView* new_focused_view_; |
| 43 |
| 44 DISALLOW_COPY_AND_ASSIGN(TestFocusControllerDelegate); |
| 45 }; |
| 46 |
| 47 } // namespace |
| 48 |
| 49 TEST(FocusControllerTest, Basic) { |
| 50 TestServerViewDelegate server_view_delegate; |
| 51 ServerView root(&server_view_delegate, ViewId()); |
| 52 root.SetVisible(true); |
| 53 ServerView child(&server_view_delegate, ViewId()); |
| 54 child.SetVisible(true); |
| 55 root.Add(&child); |
| 56 ServerView child_child(&server_view_delegate, ViewId()); |
| 57 child_child.SetVisible(true); |
| 58 child.Add(&child_child); |
| 59 |
| 60 TestFocusControllerDelegate focus_delegate; |
| 61 FocusController focus_controller(&focus_delegate, &root); |
| 62 |
| 63 focus_controller.SetFocusedView(&child_child); |
| 64 EXPECT_EQ(0u, focus_delegate.change_count()); |
| 65 |
| 66 // Remove the ancestor of the focused view, focus should go to the |root|. |
| 67 root.Remove(&child); |
| 68 EXPECT_EQ(1u, focus_delegate.change_count()); |
| 69 EXPECT_EQ(&root, focus_delegate.new_focused_view()); |
| 70 EXPECT_EQ(&child_child, focus_delegate.old_focused_view()); |
| 71 focus_delegate.ClearAll(); |
| 72 |
| 73 // Make the focused view invisible. Focus is lost in this case (as no one |
| 74 // to give focus to). |
| 75 root.SetVisible(false); |
| 76 EXPECT_EQ(1u, focus_delegate.change_count()); |
| 77 EXPECT_EQ(nullptr, focus_delegate.new_focused_view()); |
| 78 EXPECT_EQ(&root, focus_delegate.old_focused_view()); |
| 79 focus_delegate.ClearAll(); |
| 80 |
| 81 // Go back to initial state and focus |child_child|. |
| 82 root.SetVisible(true); |
| 83 root.Add(&child); |
| 84 focus_controller.SetFocusedView(&child_child); |
| 85 EXPECT_EQ(0u, focus_delegate.change_count()); |
| 86 |
| 87 // Hide the focused view, focus should go to parent. |
| 88 child_child.SetVisible(false); |
| 89 EXPECT_EQ(1u, focus_delegate.change_count()); |
| 90 EXPECT_EQ(&child, focus_delegate.new_focused_view()); |
| 91 EXPECT_EQ(&child_child, focus_delegate.old_focused_view()); |
| 92 focus_delegate.ClearAll(); |
| 93 |
| 94 child_child.SetVisible(true); |
| 95 focus_controller.SetFocusedView(&child_child); |
| 96 EXPECT_EQ(0u, focus_delegate.change_count()); |
| 97 |
| 98 // Hide the parent of the focused view. |
| 99 child.SetVisible(false); |
| 100 EXPECT_EQ(1u, focus_delegate.change_count()); |
| 101 EXPECT_EQ(&root, focus_delegate.new_focused_view()); |
| 102 EXPECT_EQ(&child_child, focus_delegate.old_focused_view()); |
| 103 focus_delegate.ClearAll(); |
| 104 } |
| 105 |
| 106 } // namespace view_manager |
OLD | NEW |