OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "components/mus/ws/focus_controller.h" | 5 #include "components/mus/ws/focus_controller.h" |
6 | 6 |
7 #include "components/mus/ws/focus_controller_delegate.h" | 7 #include "components/mus/ws/focus_controller_observer.h" |
8 #include "components/mus/ws/server_window.h" | 8 #include "components/mus/ws/server_window.h" |
9 #include "components/mus/ws/test_server_window_delegate.h" | 9 #include "components/mus/ws/test_server_window_delegate.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 namespace mus { | 12 namespace mus { |
13 | 13 |
14 namespace ws { | 14 namespace ws { |
15 namespace { | 15 namespace { |
16 | 16 |
17 class TestFocusControllerDelegate : public FocusControllerDelegate { | 17 class TestFocusControllerObserver : public FocusControllerObserver { |
18 public: | 18 public: |
19 TestFocusControllerDelegate() | 19 TestFocusControllerObserver() |
20 : change_count_(0u), | 20 : change_count_(0u), |
21 old_focused_window_(nullptr), | 21 old_focused_window_(nullptr), |
22 new_focused_window_(nullptr) {} | 22 new_focused_window_(nullptr) {} |
23 | 23 |
24 void ClearAll() { | 24 void ClearAll() { |
25 change_count_ = 0u; | 25 change_count_ = 0u; |
26 old_focused_window_ = nullptr; | 26 old_focused_window_ = nullptr; |
27 new_focused_window_ = nullptr; | 27 new_focused_window_ = nullptr; |
28 } | 28 } |
29 size_t change_count() const { return change_count_; } | 29 size_t change_count() const { return change_count_; } |
30 ServerWindow* old_focused_window() { return old_focused_window_; } | 30 ServerWindow* old_focused_window() { return old_focused_window_; } |
31 ServerWindow* new_focused_window() { return new_focused_window_; } | 31 ServerWindow* new_focused_window() { return new_focused_window_; } |
32 | 32 |
33 private: | 33 private: |
34 // FocusControllerDelegate: | 34 // FocusControllerObserver: |
35 void OnFocusChanged(ServerWindow* old_focused_window, | 35 void OnFocusChanged(FocusControllerChangeSource source, |
| 36 ServerWindow* old_focused_window, |
36 ServerWindow* new_focused_window) override { | 37 ServerWindow* new_focused_window) override { |
| 38 if (source == FocusControllerChangeSource::EXPLICIT) |
| 39 return; |
| 40 |
37 change_count_++; | 41 change_count_++; |
38 old_focused_window_ = old_focused_window; | 42 old_focused_window_ = old_focused_window; |
39 new_focused_window_ = new_focused_window; | 43 new_focused_window_ = new_focused_window; |
40 } | 44 } |
41 | 45 |
42 size_t change_count_; | 46 size_t change_count_; |
43 ServerWindow* old_focused_window_; | 47 ServerWindow* old_focused_window_; |
44 ServerWindow* new_focused_window_; | 48 ServerWindow* new_focused_window_; |
45 | 49 |
46 DISALLOW_COPY_AND_ASSIGN(TestFocusControllerDelegate); | 50 DISALLOW_COPY_AND_ASSIGN(TestFocusControllerObserver); |
47 }; | 51 }; |
48 | 52 |
49 } // namespace | 53 } // namespace |
50 | 54 |
51 TEST(FocusControllerTest, Basic) { | 55 TEST(FocusControllerTest, Basic) { |
52 TestServerWindowDelegate server_window_delegate; | 56 TestServerWindowDelegate server_window_delegate; |
53 ServerWindow root(&server_window_delegate, WindowId()); | 57 ServerWindow root(&server_window_delegate, WindowId()); |
54 server_window_delegate.set_root_window(&root); | 58 server_window_delegate.set_root_window(&root); |
55 root.SetVisible(true); | 59 root.SetVisible(true); |
56 ServerWindow child(&server_window_delegate, WindowId()); | 60 ServerWindow child(&server_window_delegate, WindowId()); |
57 child.SetVisible(true); | 61 child.SetVisible(true); |
58 root.Add(&child); | 62 root.Add(&child); |
59 ServerWindow child_child(&server_window_delegate, WindowId()); | 63 ServerWindow child_child(&server_window_delegate, WindowId()); |
60 child_child.SetVisible(true); | 64 child_child.SetVisible(true); |
61 child.Add(&child_child); | 65 child.Add(&child_child); |
62 | 66 |
63 TestFocusControllerDelegate focus_delegate; | 67 TestFocusControllerObserver focus_observer; |
64 FocusController focus_controller(&focus_delegate); | 68 FocusController focus_controller; |
| 69 focus_controller.AddObserver(&focus_observer); |
65 | 70 |
66 focus_controller.SetFocusedWindow(&child_child); | 71 focus_controller.SetFocusedWindow(&child_child); |
67 EXPECT_EQ(0u, focus_delegate.change_count()); | 72 EXPECT_EQ(0u, focus_observer.change_count()); |
68 | 73 |
69 // Remove the ancestor of the focused window, focus should go to the |root|. | 74 // Remove the ancestor of the focused window, focus should go to the |root|. |
70 root.Remove(&child); | 75 root.Remove(&child); |
71 EXPECT_EQ(1u, focus_delegate.change_count()); | 76 EXPECT_EQ(1u, focus_observer.change_count()); |
72 EXPECT_EQ(&root, focus_delegate.new_focused_window()); | 77 EXPECT_EQ(&root, focus_observer.new_focused_window()); |
73 EXPECT_EQ(&child_child, focus_delegate.old_focused_window()); | 78 EXPECT_EQ(&child_child, focus_observer.old_focused_window()); |
74 focus_delegate.ClearAll(); | 79 focus_observer.ClearAll(); |
75 | 80 |
76 // Make the focused window invisible. Focus is lost in this case (as no one | 81 // Make the focused window invisible. Focus is lost in this case (as no one |
77 // to give focus to). | 82 // to give focus to). |
78 root.SetVisible(false); | 83 root.SetVisible(false); |
79 EXPECT_EQ(1u, focus_delegate.change_count()); | 84 EXPECT_EQ(1u, focus_observer.change_count()); |
80 EXPECT_EQ(nullptr, focus_delegate.new_focused_window()); | 85 EXPECT_EQ(nullptr, focus_observer.new_focused_window()); |
81 EXPECT_EQ(&root, focus_delegate.old_focused_window()); | 86 EXPECT_EQ(&root, focus_observer.old_focused_window()); |
82 focus_delegate.ClearAll(); | 87 focus_observer.ClearAll(); |
83 | 88 |
84 // Go back to initial state and focus |child_child|. | 89 // Go back to initial state and focus |child_child|. |
85 root.SetVisible(true); | 90 root.SetVisible(true); |
86 root.Add(&child); | 91 root.Add(&child); |
87 focus_controller.SetFocusedWindow(&child_child); | 92 focus_controller.SetFocusedWindow(&child_child); |
88 EXPECT_EQ(0u, focus_delegate.change_count()); | 93 EXPECT_EQ(0u, focus_observer.change_count()); |
89 | 94 |
90 // Hide the focused window, focus should go to parent. | 95 // Hide the focused window, focus should go to parent. |
91 child_child.SetVisible(false); | 96 child_child.SetVisible(false); |
92 EXPECT_EQ(1u, focus_delegate.change_count()); | 97 EXPECT_EQ(1u, focus_observer.change_count()); |
93 EXPECT_EQ(&child, focus_delegate.new_focused_window()); | 98 EXPECT_EQ(&child, focus_observer.new_focused_window()); |
94 EXPECT_EQ(&child_child, focus_delegate.old_focused_window()); | 99 EXPECT_EQ(&child_child, focus_observer.old_focused_window()); |
95 focus_delegate.ClearAll(); | 100 focus_observer.ClearAll(); |
96 | 101 |
97 child_child.SetVisible(true); | 102 child_child.SetVisible(true); |
98 focus_controller.SetFocusedWindow(&child_child); | 103 focus_controller.SetFocusedWindow(&child_child); |
99 EXPECT_EQ(0u, focus_delegate.change_count()); | 104 EXPECT_EQ(0u, focus_observer.change_count()); |
100 | 105 |
101 // Hide the parent of the focused window. | 106 // Hide the parent of the focused window. |
102 child.SetVisible(false); | 107 child.SetVisible(false); |
103 EXPECT_EQ(1u, focus_delegate.change_count()); | 108 EXPECT_EQ(1u, focus_observer.change_count()); |
104 EXPECT_EQ(&root, focus_delegate.new_focused_window()); | 109 EXPECT_EQ(&root, focus_observer.new_focused_window()); |
105 EXPECT_EQ(&child_child, focus_delegate.old_focused_window()); | 110 EXPECT_EQ(&child_child, focus_observer.old_focused_window()); |
106 focus_delegate.ClearAll(); | 111 focus_observer.ClearAll(); |
| 112 focus_controller.RemoveObserver(&focus_observer); |
107 } | 113 } |
108 | 114 |
109 } // namespace ws | 115 } // namespace ws |
110 | 116 |
111 } // namespace mus | 117 } // namespace mus |
OLD | NEW |