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 #ifndef COMPONENTS_MUS_WS_TEST_UTILS_H_ | |
6 #define COMPONENTS_MUS_WS_TEST_UTILS_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <vector> | |
11 | |
12 #include "components/mus/public/interfaces/display.mojom.h" | |
13 #include "components/mus/public/interfaces/window_tree.mojom.h" | |
14 #include "components/mus/ws/display.h" | |
15 #include "components/mus/ws/display_binding.h" | |
16 #include "components/mus/ws/event_dispatcher.h" | |
17 #include "components/mus/ws/platform_display.h" | |
18 #include "components/mus/ws/platform_display_factory.h" | |
19 #include "components/mus/ws/test_change_tracker.h" | |
20 #include "components/mus/ws/user_activity_monitor.h" | |
21 #include "components/mus/ws/user_display_manager.h" | |
22 #include "components/mus/ws/user_id.h" | |
23 #include "components/mus/ws/window_manager_state.h" | |
24 #include "components/mus/ws/window_manager_window_tree_factory_set.h" | |
25 #include "components/mus/ws/window_server_delegate.h" | |
26 #include "components/mus/ws/window_tree.h" | |
27 #include "components/mus/ws/window_tree_binding.h" | |
28 | |
29 namespace mus { | |
30 namespace ws { | |
31 namespace test { | |
32 | |
33 // Collection of utilities useful in creating mus tests. | |
34 | |
35 class WindowManagerWindowTreeFactorySetTestApi { | |
36 public: | |
37 explicit WindowManagerWindowTreeFactorySetTestApi( | |
38 WindowManagerWindowTreeFactorySet* | |
39 window_manager_window_tree_factory_set); | |
40 ~WindowManagerWindowTreeFactorySetTestApi(); | |
41 | |
42 void Add(const UserId& user_id); | |
43 | |
44 private: | |
45 WindowManagerWindowTreeFactorySet* window_manager_window_tree_factory_set_; | |
46 | |
47 DISALLOW_COPY_AND_ASSIGN(WindowManagerWindowTreeFactorySetTestApi); | |
48 }; | |
49 | |
50 // ----------------------------------------------------------------------------- | |
51 | |
52 class UserDisplayManagerTestApi { | |
53 public: | |
54 explicit UserDisplayManagerTestApi(UserDisplayManager* udm) : udm_(udm) {} | |
55 ~UserDisplayManagerTestApi() {} | |
56 | |
57 void SetTestObserver(mojom::DisplayManagerObserver* observer) { | |
58 udm_->test_observer_ = observer; | |
59 if (observer) | |
60 udm_->OnObserverAdded(observer); | |
61 } | |
62 | |
63 private: | |
64 UserDisplayManager* udm_; | |
65 | |
66 DISALLOW_COPY_AND_ASSIGN(UserDisplayManagerTestApi); | |
67 }; | |
68 | |
69 // ----------------------------------------------------------------------------- | |
70 | |
71 class UserActivityMonitorTestApi { | |
72 public: | |
73 explicit UserActivityMonitorTestApi(UserActivityMonitor* monitor) | |
74 : monitor_(monitor) {} | |
75 | |
76 void SetTimerTaskRunner( | |
77 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { | |
78 monitor_->idle_timer_.SetTaskRunner(task_runner); | |
79 } | |
80 | |
81 private: | |
82 UserActivityMonitor* monitor_; | |
83 DISALLOW_COPY_AND_ASSIGN(UserActivityMonitorTestApi); | |
84 }; | |
85 | |
86 // ----------------------------------------------------------------------------- | |
87 | |
88 class WindowTreeTestApi { | |
89 public: | |
90 explicit WindowTreeTestApi(WindowTree* tree); | |
91 ~WindowTreeTestApi(); | |
92 | |
93 void set_user_id(const UserId& user_id) { tree_->user_id_ = user_id; } | |
94 void set_window_manager_internal(mojom::WindowManager* wm_internal) { | |
95 tree_->window_manager_internal_ = wm_internal; | |
96 } | |
97 void AckOldestEvent() { | |
98 tree_->OnWindowInputEventAck(tree_->event_ack_id_, | |
99 mojom::EventResult::UNHANDLED); | |
100 } | |
101 void EnableCapture() { tree_->event_ack_id_ = 1u; } | |
102 void AckLastEvent(mojom::EventResult result) { | |
103 tree_->OnWindowInputEventAck(tree_->event_ack_id_, result); | |
104 } | |
105 | |
106 void SetEventObserver(mojom::EventMatcherPtr matcher, | |
107 uint32_t event_observer_id); | |
108 | |
109 private: | |
110 WindowTree* tree_; | |
111 | |
112 DISALLOW_COPY_AND_ASSIGN(WindowTreeTestApi); | |
113 }; | |
114 | |
115 // ----------------------------------------------------------------------------- | |
116 | |
117 class DisplayTestApi { | |
118 public: | |
119 explicit DisplayTestApi(Display* display); | |
120 ~DisplayTestApi(); | |
121 | |
122 void OnEvent(const ui::Event& event) { display_->OnEvent(event); } | |
123 | |
124 private: | |
125 Display* display_; | |
126 | |
127 DISALLOW_COPY_AND_ASSIGN(DisplayTestApi); | |
128 }; | |
129 | |
130 // ----------------------------------------------------------------------------- | |
131 | |
132 class EventDispatcherTestApi { | |
133 public: | |
134 explicit EventDispatcherTestApi(EventDispatcher* ed) : ed_(ed) {} | |
135 ~EventDispatcherTestApi() {} | |
136 | |
137 bool AreAnyPointersDown() const { return ed_->AreAnyPointersDown(); } | |
138 bool is_mouse_button_down() const { return ed_->mouse_button_down_; } | |
139 bool IsWindowPointerTarget(const ServerWindow* window) const; | |
140 int NumberPointerTargetsForWindow(ServerWindow* window); | |
141 ModalWindowController* modal_window_controller() const { | |
142 return &ed_->modal_window_controller_; | |
143 } | |
144 ServerWindow* capture_window() { return ed_->capture_window_; } | |
145 | |
146 private: | |
147 EventDispatcher* ed_; | |
148 | |
149 DISALLOW_COPY_AND_ASSIGN(EventDispatcherTestApi); | |
150 }; | |
151 | |
152 // ----------------------------------------------------------------------------- | |
153 | |
154 class ModalWindowControllerTestApi { | |
155 public: | |
156 explicit ModalWindowControllerTestApi(ModalWindowController* mwc) | |
157 : mwc_(mwc) {} | |
158 ~ModalWindowControllerTestApi() {} | |
159 | |
160 ServerWindow* GetActiveSystemModalWindow() const { | |
161 return mwc_->GetActiveSystemModalWindow(); | |
162 } | |
163 | |
164 private: | |
165 ModalWindowController* mwc_; | |
166 | |
167 DISALLOW_COPY_AND_ASSIGN(ModalWindowControllerTestApi); | |
168 }; | |
169 | |
170 // ----------------------------------------------------------------------------- | |
171 | |
172 class WindowManagerStateTestApi { | |
173 public: | |
174 explicit WindowManagerStateTestApi(WindowManagerState* wms) : wms_(wms) {} | |
175 ~WindowManagerStateTestApi() {} | |
176 | |
177 void DispatchInputEventToWindow(ServerWindow* target, | |
178 ClientSpecificId client_id, | |
179 const ui::Event& event, | |
180 Accelerator* accelerator) { | |
181 wms_->DispatchInputEventToWindow(target, client_id, event, accelerator); | |
182 } | |
183 | |
184 ClientSpecificId GetEventTargetClientId(ServerWindow* window, | |
185 bool in_nonclient_area) { | |
186 return wms_->GetEventTargetClientId(window, in_nonclient_area); | |
187 } | |
188 | |
189 void ProcessEvent(const ui::Event& event) { wms_->ProcessEvent(event); } | |
190 | |
191 void OnEventAckTimeout(ClientSpecificId client_id) { | |
192 wms_->OnEventAckTimeout(client_id); | |
193 } | |
194 | |
195 ClientSpecificId GetEventTargetClientId(const ServerWindow* window, | |
196 bool in_nonclient_area) { | |
197 return wms_->GetEventTargetClientId(window, in_nonclient_area); | |
198 } | |
199 | |
200 mojom::WindowTree* tree_awaiting_input_ack() { | |
201 return wms_->tree_awaiting_input_ack_; | |
202 } | |
203 | |
204 private: | |
205 WindowManagerState* wms_; | |
206 | |
207 DISALLOW_COPY_AND_ASSIGN(WindowManagerStateTestApi); | |
208 }; | |
209 | |
210 // ----------------------------------------------------------------------------- | |
211 | |
212 // Factory that always embeds the new WindowTree as the root user id. | |
213 class TestDisplayBinding : public DisplayBinding { | |
214 public: | |
215 explicit TestDisplayBinding(WindowServer* window_server) | |
216 : window_server_(window_server) {} | |
217 ~TestDisplayBinding() override {} | |
218 | |
219 private: | |
220 // DisplayBinding: | |
221 WindowTree* CreateWindowTree(ServerWindow* root) override; | |
222 | |
223 WindowServer* window_server_; | |
224 | |
225 DISALLOW_COPY_AND_ASSIGN(TestDisplayBinding); | |
226 }; | |
227 | |
228 // ----------------------------------------------------------------------------- | |
229 | |
230 // Factory that dispenses TestPlatformDisplays. | |
231 class TestPlatformDisplayFactory : public PlatformDisplayFactory { | |
232 public: | |
233 explicit TestPlatformDisplayFactory(int32_t* cursor_id_storage); | |
234 ~TestPlatformDisplayFactory(); | |
235 | |
236 // PlatformDisplayFactory: | |
237 PlatformDisplay* CreatePlatformDisplay() override; | |
238 | |
239 private: | |
240 int32_t* cursor_id_storage_; | |
241 | |
242 DISALLOW_COPY_AND_ASSIGN(TestPlatformDisplayFactory); | |
243 }; | |
244 | |
245 // ----------------------------------------------------------------------------- | |
246 | |
247 class TestWindowManager : public mojom::WindowManager { | |
248 public: | |
249 TestWindowManager() | |
250 : got_create_top_level_window_(false), | |
251 change_id_(0u), | |
252 on_accelerator_called_(false), | |
253 on_accelerator_id_(0u) {} | |
254 ~TestWindowManager() override {} | |
255 | |
256 bool did_call_create_top_level_window(uint32_t* change_id) { | |
257 if (!got_create_top_level_window_) | |
258 return false; | |
259 | |
260 got_create_top_level_window_ = false; | |
261 *change_id = change_id_; | |
262 return true; | |
263 } | |
264 | |
265 bool on_accelerator_called() { return on_accelerator_called_; } | |
266 uint32_t on_accelerator_id() { return on_accelerator_id_; } | |
267 | |
268 private: | |
269 // WindowManager: | |
270 void OnConnect(uint16_t client_id) override {} | |
271 void WmNewDisplayAdded(mus::mojom::DisplayPtr display, | |
272 mus::mojom::WindowDataPtr root, | |
273 bool drawn) override {} | |
274 void WmSetBounds(uint32_t change_id, | |
275 uint32_t window_id, | |
276 const gfx::Rect& bounds) override {} | |
277 void WmSetProperty(uint32_t change_id, | |
278 uint32_t window_id, | |
279 const mojo::String& name, | |
280 mojo::Array<uint8_t> value) override {} | |
281 void WmCreateTopLevelWindow( | |
282 uint32_t change_id, | |
283 ClientSpecificId requesting_client_id, | |
284 mojo::Map<mojo::String, mojo::Array<uint8_t>> properties) override; | |
285 void WmClientJankinessChanged(ClientSpecificId client_id, | |
286 bool janky) override; | |
287 void OnAccelerator(uint32_t id, std::unique_ptr<ui::Event> event) override; | |
288 | |
289 bool got_create_top_level_window_; | |
290 uint32_t change_id_; | |
291 | |
292 bool on_accelerator_called_; | |
293 uint32_t on_accelerator_id_; | |
294 | |
295 DISALLOW_COPY_AND_ASSIGN(TestWindowManager); | |
296 }; | |
297 | |
298 // ----------------------------------------------------------------------------- | |
299 | |
300 // WindowTreeClient implementation that logs all calls to a TestChangeTracker. | |
301 class TestWindowTreeClient : public mus::mojom::WindowTreeClient { | |
302 public: | |
303 TestWindowTreeClient(); | |
304 ~TestWindowTreeClient() override; | |
305 | |
306 TestChangeTracker* tracker() { return &tracker_; } | |
307 | |
308 void Bind(mojo::InterfaceRequest<mojom::WindowTreeClient> request); | |
309 | |
310 void set_record_on_change_completed(bool value) { | |
311 record_on_change_completed_ = value; | |
312 } | |
313 | |
314 private: | |
315 // WindowTreeClient: | |
316 void OnEmbed(uint16_t client_id, | |
317 mojom::WindowDataPtr root, | |
318 mus::mojom::WindowTreePtr tree, | |
319 int64_t display_id, | |
320 Id focused_window_id, | |
321 bool drawn) override; | |
322 void OnEmbeddedAppDisconnected(uint32_t window) override; | |
323 void OnUnembed(Id window_id) override; | |
324 void OnLostCapture(Id window_id) override; | |
325 void OnTopLevelCreated(uint32_t change_id, | |
326 mojom::WindowDataPtr data, | |
327 int64_t display_id, | |
328 bool drawn) override; | |
329 void OnWindowBoundsChanged(uint32_t window, | |
330 const gfx::Rect& old_bounds, | |
331 const gfx::Rect& new_bounds) override; | |
332 void OnClientAreaChanged( | |
333 uint32_t window_id, | |
334 const gfx::Insets& new_client_area, | |
335 mojo::Array<gfx::Rect> new_additional_client_areas) override; | |
336 void OnTransientWindowAdded(uint32_t window_id, | |
337 uint32_t transient_window_id) override; | |
338 void OnTransientWindowRemoved(uint32_t window_id, | |
339 uint32_t transient_window_id) override; | |
340 void OnWindowHierarchyChanged( | |
341 uint32_t window, | |
342 uint32_t old_parent, | |
343 uint32_t new_parent, | |
344 mojo::Array<mojom::WindowDataPtr> windows) override; | |
345 void OnWindowReordered(uint32_t window_id, | |
346 uint32_t relative_window_id, | |
347 mojom::OrderDirection direction) override; | |
348 void OnWindowDeleted(uint32_t window) override; | |
349 void OnWindowVisibilityChanged(uint32_t window, bool visible) override; | |
350 void OnWindowOpacityChanged(uint32_t window, | |
351 float old_opacity, | |
352 float new_opacity) override; | |
353 void OnWindowParentDrawnStateChanged(uint32_t window, bool drawn) override; | |
354 void OnWindowSharedPropertyChanged(uint32_t window, | |
355 const mojo::String& name, | |
356 mojo::Array<uint8_t> new_data) override; | |
357 void OnWindowInputEvent(uint32_t event_id, | |
358 uint32_t window, | |
359 std::unique_ptr<ui::Event> event, | |
360 uint32_t event_observer_id) override; | |
361 void OnEventObserved(std::unique_ptr<ui::Event> event, | |
362 uint32_t event_observer_id) override; | |
363 void OnWindowFocused(uint32_t focused_window_id) override; | |
364 void OnWindowPredefinedCursorChanged(uint32_t window_id, | |
365 mojom::Cursor cursor_id) override; | |
366 void OnChangeCompleted(uint32_t change_id, bool success) override; | |
367 void RequestClose(uint32_t window_id) override; | |
368 void GetWindowManager( | |
369 mojo::AssociatedInterfaceRequest<mojom::WindowManager> internal) override; | |
370 | |
371 TestChangeTracker tracker_; | |
372 mojo::Binding<mojom::WindowTreeClient> binding_; | |
373 bool record_on_change_completed_ = false; | |
374 | |
375 DISALLOW_COPY_AND_ASSIGN(TestWindowTreeClient); | |
376 }; | |
377 | |
378 // ----------------------------------------------------------------------------- | |
379 | |
380 // WindowTreeBinding implementation that vends TestWindowTreeBinding. | |
381 class TestWindowTreeBinding : public WindowTreeBinding { | |
382 public: | |
383 explicit TestWindowTreeBinding(WindowTree* tree); | |
384 ~TestWindowTreeBinding() override; | |
385 | |
386 WindowTree* tree() { return tree_; } | |
387 TestWindowTreeClient* client() { return &client_; } | |
388 | |
389 bool is_paused() const { return is_paused_; } | |
390 | |
391 // WindowTreeBinding: | |
392 mojom::WindowManager* GetWindowManager() override; | |
393 void SetIncomingMethodCallProcessingPaused(bool paused) override; | |
394 | |
395 private: | |
396 WindowTree* tree_; | |
397 TestWindowTreeClient client_; | |
398 bool is_paused_ = false; | |
399 std::unique_ptr<TestWindowManager> window_manager_; | |
400 | |
401 DISALLOW_COPY_AND_ASSIGN(TestWindowTreeBinding); | |
402 }; | |
403 | |
404 // ----------------------------------------------------------------------------- | |
405 | |
406 // WindowServerDelegate that creates TestWindowTreeClients. | |
407 class TestWindowServerDelegate : public WindowServerDelegate { | |
408 public: | |
409 TestWindowServerDelegate(); | |
410 ~TestWindowServerDelegate() override; | |
411 | |
412 void set_window_server(WindowServer* window_server) { | |
413 window_server_ = window_server; | |
414 } | |
415 | |
416 void set_num_displays_to_create(int count) { | |
417 num_displays_to_create_ = count; | |
418 } | |
419 | |
420 TestWindowTreeClient* last_client() { | |
421 return last_binding() ? last_binding()->client() : nullptr; | |
422 } | |
423 TestWindowTreeBinding* last_binding() { | |
424 return bindings_.empty() ? nullptr : bindings_.back(); | |
425 } | |
426 | |
427 std::vector<TestWindowTreeBinding*>* bindings() { return &bindings_; } | |
428 | |
429 bool got_on_no_more_displays() const { return got_on_no_more_displays_; } | |
430 | |
431 Display* AddDisplay(); | |
432 | |
433 // WindowServerDelegate: | |
434 void OnNoMoreDisplays() override; | |
435 std::unique_ptr<WindowTreeBinding> CreateWindowTreeBinding( | |
436 BindingType type, | |
437 ws::WindowServer* window_server, | |
438 ws::WindowTree* tree, | |
439 mojom::WindowTreeRequest* tree_request, | |
440 mojom::WindowTreeClientPtr* client) override; | |
441 void CreateDefaultDisplays() override; | |
442 bool IsTestConfig() const override; | |
443 | |
444 private: | |
445 // If CreateDefaultDisplays() this is the number of Displays that are | |
446 // created. The default is 0, which results in a DCHECK. | |
447 int num_displays_to_create_ = 0; | |
448 WindowServer* window_server_ = nullptr; | |
449 bool got_on_no_more_displays_ = false; | |
450 // All TestWindowTreeBinding objects created via CreateWindowTreeBinding. | |
451 // These are owned by the corresponding WindowTree. | |
452 std::vector<TestWindowTreeBinding*> bindings_; | |
453 | |
454 DISALLOW_COPY_AND_ASSIGN(TestWindowServerDelegate); | |
455 }; | |
456 | |
457 // ----------------------------------------------------------------------------- | |
458 | |
459 // Helper class which owns all of the necessary objects to test event targeting | |
460 // of ServerWindow objects. | |
461 class WindowEventTargetingHelper { | |
462 public: | |
463 WindowEventTargetingHelper(); | |
464 ~WindowEventTargetingHelper(); | |
465 | |
466 // Creates |window| as an embeded window of the primary tree. This window is a | |
467 // root window of its own tree, with bounds |window_bounds|. The bounds of the | |
468 // root window of |display_| are defined by |root_window_bounds|. | |
469 ServerWindow* CreatePrimaryTree(const gfx::Rect& root_window_bounds, | |
470 const gfx::Rect& window_bounds); | |
471 // Creates a secondary tree, embedded as a child of |embed_window|. The | |
472 // resulting |window| is setup for event targeting, with bounds | |
473 // |window_bounds|. | |
474 void CreateSecondaryTree(ServerWindow* embed_window, | |
475 const gfx::Rect& window_bounds, | |
476 TestWindowTreeClient** out_client, | |
477 WindowTree** window_tree, | |
478 ServerWindow** window); | |
479 // Sets the task runner for |message_loop_| | |
480 void SetTaskRunner(scoped_refptr<base::SingleThreadTaskRunner> task_runner); | |
481 | |
482 int32_t cursor_id() { return cursor_id_; } | |
483 Display* display() { return display_; } | |
484 TestWindowTreeBinding* last_binding() { | |
485 return window_server_delegate_.last_binding(); | |
486 } | |
487 TestWindowTreeClient* last_window_tree_client() { | |
488 return window_server_delegate_.last_client(); | |
489 } | |
490 TestWindowTreeClient* wm_client() { return wm_client_; } | |
491 WindowServer* window_server() { return window_server_.get(); } | |
492 | |
493 private: | |
494 // TestWindowTreeClient that is used for the WM client. Owned by | |
495 // |window_server_delegate_| | |
496 TestWindowTreeClient* wm_client_; | |
497 int32_t cursor_id_; | |
498 TestPlatformDisplayFactory platform_display_factory_; | |
499 TestWindowServerDelegate window_server_delegate_; | |
500 // Owned by WindowServer | |
501 TestDisplayBinding* display_binding_; | |
502 // Owned by WindowServer's DisplayManager. | |
503 Display* display_; | |
504 scoped_refptr<SurfacesState> surfaces_state_; | |
505 std::unique_ptr<WindowServer> window_server_; | |
506 // Needed to Bind to |wm_client_| | |
507 base::MessageLoop message_loop_; | |
508 | |
509 DISALLOW_COPY_AND_ASSIGN(WindowEventTargetingHelper); | |
510 }; | |
511 | |
512 // ----------------------------------------------------------------------------- | |
513 | |
514 // Returns the first and only root of |tree|. If |tree| has zero or more than | |
515 // one root returns null. | |
516 ServerWindow* FirstRoot(WindowTree* tree); | |
517 | |
518 // Returns the ClientWindowId of the first root of |tree|, or an empty | |
519 // ClientWindowId if |tree| has zero or more than one root. | |
520 ClientWindowId FirstRootId(WindowTree* tree); | |
521 | |
522 // Returns |tree|s ClientWindowId for |window|. | |
523 ClientWindowId ClientWindowIdForWindow(WindowTree* tree, | |
524 const ServerWindow* window); | |
525 | |
526 // Creates a new visible window as a child of the single root of |tree|. | |
527 // |client_id| set to the ClientWindowId of the new window. | |
528 ServerWindow* NewWindowInTree(WindowTree* tree, ClientWindowId* client_id); | |
529 ServerWindow* NewWindowInTreeWithParent(WindowTree* tree, | |
530 ServerWindow* parent, | |
531 ClientWindowId* client_id); | |
532 | |
533 } // namespace test | |
534 } // namespace ws | |
535 } // namespace mus | |
536 | |
537 #endif // COMPONENTS_MUS_WS_TEST_UTILS_H_ | |
OLD | NEW |