OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 <stdint.h> | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/atomicops.h" | |
10 #include "base/macros.h" | |
11 #include "base/message_loop/message_loop.h" | |
12 #include "base/strings/string_number_conversions.h" | |
13 #include "components/mus/common/types.h" | |
14 #include "components/mus/common/util.h" | |
15 #include "components/mus/public/interfaces/window_tree.mojom.h" | |
16 #include "components/mus/surfaces/surfaces_state.h" | |
17 #include "components/mus/ws/display_binding.h" | |
18 #include "components/mus/ws/display_manager.h" | |
19 #include "components/mus/ws/ids.h" | |
20 #include "components/mus/ws/platform_display.h" | |
21 #include "components/mus/ws/platform_display_factory.h" | |
22 #include "components/mus/ws/platform_display_init_params.h" | |
23 #include "components/mus/ws/server_window.h" | |
24 #include "components/mus/ws/test_utils.h" | |
25 #include "components/mus/ws/window_manager_state.h" | |
26 #include "components/mus/ws/window_manager_window_tree_factory_set.h" | |
27 #include "components/mus/ws/window_server.h" | |
28 #include "components/mus/ws/window_server_delegate.h" | |
29 #include "components/mus/ws/window_tree.h" | |
30 #include "components/mus/ws/window_tree_binding.h" | |
31 #include "testing/gtest/include/gtest/gtest.h" | |
32 #include "ui/gfx/geometry/rect.h" | |
33 | |
34 namespace mus { | |
35 namespace ws { | |
36 namespace test { | |
37 namespace { | |
38 | |
39 class TestDisplayManagerObserver : public mojom::DisplayManagerObserver { | |
40 public: | |
41 TestDisplayManagerObserver() {} | |
42 ~TestDisplayManagerObserver() override {} | |
43 | |
44 std::string GetAndClearObserverCalls() { | |
45 std::string result; | |
46 std::swap(observer_calls_, result); | |
47 return result; | |
48 } | |
49 | |
50 private: | |
51 void AddCall(const std::string& call) { | |
52 if (!observer_calls_.empty()) | |
53 observer_calls_ += "\n"; | |
54 observer_calls_ += call; | |
55 } | |
56 | |
57 std::string DisplayIdsToString( | |
58 const mojo::Array<mojom::DisplayPtr>& displays) { | |
59 std::string display_ids; | |
60 for (const auto& display : displays) { | |
61 if (!display_ids.empty()) | |
62 display_ids += " "; | |
63 display_ids += base::Int64ToString(display->id); | |
64 } | |
65 return display_ids; | |
66 } | |
67 | |
68 // mojom::DisplayManagerObserver: | |
69 void OnDisplays(mojo::Array<mojom::DisplayPtr> displays) override { | |
70 AddCall("OnDisplays " + DisplayIdsToString(displays)); | |
71 } | |
72 void OnDisplaysChanged(mojo::Array<mojom::DisplayPtr> displays) override { | |
73 AddCall("OnDisplaysChanged " + DisplayIdsToString(displays)); | |
74 } | |
75 void OnDisplayRemoved(int64_t id) override { | |
76 AddCall("OnDisplayRemoved " + base::Int64ToString(id)); | |
77 } | |
78 | |
79 std::string observer_calls_; | |
80 | |
81 DISALLOW_COPY_AND_ASSIGN(TestDisplayManagerObserver); | |
82 }; | |
83 | |
84 mojom::FrameDecorationValuesPtr CreateDefaultFrameDecorationValues() { | |
85 return mojom::FrameDecorationValues::New(); | |
86 } | |
87 | |
88 } // namespace | |
89 | |
90 // ----------------------------------------------------------------------------- | |
91 | |
92 class UserDisplayManagerTest : public testing::Test { | |
93 public: | |
94 UserDisplayManagerTest() | |
95 : cursor_id_(0), platform_display_factory_(&cursor_id_) {} | |
96 ~UserDisplayManagerTest() override {} | |
97 | |
98 protected: | |
99 // testing::Test: | |
100 void SetUp() override { | |
101 PlatformDisplay::set_factory_for_testing(&platform_display_factory_); | |
102 window_server_.reset(new WindowServer(&window_server_delegate_, | |
103 scoped_refptr<SurfacesState>())); | |
104 window_server_delegate_.set_window_server(window_server_.get()); | |
105 } | |
106 | |
107 protected: | |
108 int32_t cursor_id_; | |
109 TestPlatformDisplayFactory platform_display_factory_; | |
110 TestWindowServerDelegate window_server_delegate_; | |
111 std::unique_ptr<WindowServer> window_server_; | |
112 base::MessageLoop message_loop_; | |
113 | |
114 private: | |
115 DISALLOW_COPY_AND_ASSIGN(UserDisplayManagerTest); | |
116 }; | |
117 | |
118 TEST_F(UserDisplayManagerTest, OnlyNotifyWhenFrameDecorationsSet) { | |
119 window_server_delegate_.set_num_displays_to_create(1); | |
120 | |
121 const UserId kUserId1 = "2"; | |
122 TestDisplayManagerObserver display_manager_observer1; | |
123 DisplayManager* display_manager = window_server_->display_manager(); | |
124 WindowManagerWindowTreeFactorySetTestApi( | |
125 window_server_->window_manager_window_tree_factory_set()) | |
126 .Add(kUserId1); | |
127 UserDisplayManager* user_display_manager1 = | |
128 display_manager->GetUserDisplayManager(kUserId1); | |
129 ASSERT_TRUE(user_display_manager1); | |
130 UserDisplayManagerTestApi(user_display_manager1) | |
131 .SetTestObserver(&display_manager_observer1); | |
132 // Observer should not have been notified yet. | |
133 EXPECT_EQ(std::string(), | |
134 display_manager_observer1.GetAndClearObserverCalls()); | |
135 | |
136 // Set the frame decoration values, which should trigger sending immediately. | |
137 ASSERT_EQ(1u, display_manager->displays().size()); | |
138 window_server_->window_manager_window_tree_factory_set() | |
139 ->GetWindowManagerStateForUser(kUserId1) | |
140 ->SetFrameDecorationValues(CreateDefaultFrameDecorationValues()); | |
141 EXPECT_EQ("OnDisplays 1", | |
142 display_manager_observer1.GetAndClearObserverCalls()); | |
143 | |
144 UserDisplayManagerTestApi(user_display_manager1).SetTestObserver(nullptr); | |
145 } | |
146 | |
147 TEST_F(UserDisplayManagerTest, AddObserverAfterFrameDecorationsSet) { | |
148 window_server_delegate_.set_num_displays_to_create(1); | |
149 | |
150 const UserId kUserId1 = "2"; | |
151 TestDisplayManagerObserver display_manager_observer1; | |
152 DisplayManager* display_manager = window_server_->display_manager(); | |
153 WindowManagerWindowTreeFactorySetTestApi( | |
154 window_server_->window_manager_window_tree_factory_set()) | |
155 .Add(kUserId1); | |
156 UserDisplayManager* user_display_manager1 = | |
157 display_manager->GetUserDisplayManager(kUserId1); | |
158 ASSERT_TRUE(user_display_manager1); | |
159 ASSERT_EQ(1u, display_manager->displays().size()); | |
160 window_server_->window_manager_window_tree_factory_set() | |
161 ->GetWindowManagerStateForUser(kUserId1) | |
162 ->SetFrameDecorationValues(CreateDefaultFrameDecorationValues()); | |
163 | |
164 UserDisplayManagerTestApi(user_display_manager1) | |
165 .SetTestObserver(&display_manager_observer1); | |
166 EXPECT_EQ("OnDisplays 1", | |
167 display_manager_observer1.GetAndClearObserverCalls()); | |
168 | |
169 UserDisplayManagerTestApi(user_display_manager1).SetTestObserver(nullptr); | |
170 } | |
171 | |
172 TEST_F(UserDisplayManagerTest, AddRemoveDisplay) { | |
173 window_server_delegate_.set_num_displays_to_create(1); | |
174 | |
175 const UserId kUserId1 = "2"; | |
176 TestDisplayManagerObserver display_manager_observer1; | |
177 DisplayManager* display_manager = window_server_->display_manager(); | |
178 WindowManagerWindowTreeFactorySetTestApi( | |
179 window_server_->window_manager_window_tree_factory_set()) | |
180 .Add(kUserId1); | |
181 UserDisplayManager* user_display_manager1 = | |
182 display_manager->GetUserDisplayManager(kUserId1); | |
183 ASSERT_TRUE(user_display_manager1); | |
184 ASSERT_EQ(1u, display_manager->displays().size()); | |
185 window_server_->window_manager_window_tree_factory_set() | |
186 ->GetWindowManagerStateForUser(kUserId1) | |
187 ->SetFrameDecorationValues(CreateDefaultFrameDecorationValues()); | |
188 UserDisplayManagerTestApi(user_display_manager1) | |
189 .SetTestObserver(&display_manager_observer1); | |
190 EXPECT_EQ("OnDisplays 1", | |
191 display_manager_observer1.GetAndClearObserverCalls()); | |
192 | |
193 // Add another display. | |
194 Display* display2 = | |
195 new Display(window_server_.get(), PlatformDisplayInitParams()); | |
196 display2->Init(nullptr); | |
197 | |
198 // Observer should be notified immediately as frame decorations were set. | |
199 EXPECT_EQ("OnDisplaysChanged 2", | |
200 display_manager_observer1.GetAndClearObserverCalls()); | |
201 | |
202 // Remove the display and verify observer is notified. | |
203 display_manager->DestroyDisplay(display2); | |
204 display2 = nullptr; | |
205 EXPECT_EQ("OnDisplayRemoved 2", | |
206 display_manager_observer1.GetAndClearObserverCalls()); | |
207 | |
208 UserDisplayManagerTestApi(user_display_manager1).SetTestObserver(nullptr); | |
209 } | |
210 | |
211 TEST_F(UserDisplayManagerTest, NegativeCoordinates) { | |
212 window_server_delegate_.set_num_displays_to_create(1); | |
213 | |
214 const UserId kUserId1 = "2"; | |
215 TestDisplayManagerObserver display_manager_observer1; | |
216 DisplayManager* display_manager = window_server_->display_manager(); | |
217 WindowManagerWindowTreeFactorySetTestApi( | |
218 window_server_->window_manager_window_tree_factory_set()) | |
219 .Add(kUserId1); | |
220 UserDisplayManager* user_display_manager1 = | |
221 display_manager->GetUserDisplayManager(kUserId1); | |
222 ASSERT_TRUE(user_display_manager1); | |
223 | |
224 user_display_manager1->OnMouseCursorLocationChanged(gfx::Point(-10, -11)); | |
225 | |
226 base::subtle::Atomic32* cursor_location_memory = nullptr; | |
227 mojo::ScopedSharedBufferHandle handle = | |
228 user_display_manager1->GetCursorLocationMemory(); | |
229 mojo::ScopedSharedBufferMapping cursor_location_mapping = | |
230 handle->Map(sizeof(base::subtle::Atomic32)); | |
231 ASSERT_TRUE(cursor_location_mapping); | |
232 cursor_location_memory = | |
233 reinterpret_cast<base::subtle::Atomic32*>(cursor_location_mapping.get()); | |
234 | |
235 base::subtle::Atomic32 location = | |
236 base::subtle::NoBarrier_Load(cursor_location_memory); | |
237 EXPECT_EQ(gfx::Point(static_cast<int16_t>(location >> 16), | |
238 static_cast<int16_t>(location & 0xFFFF)), | |
239 gfx::Point(-10, -11)); | |
240 } | |
241 | |
242 } // namespace test | |
243 } // namespace ws | |
244 } // namespace mus | |
OLD | NEW |