1 // Copyright (c) 2013 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.
5 #include "ash/touch/touch_observer_hud.h"
7 #include "ash/ash_switches.h"
8 #include "ash/display/display_manager.h"
9 #include "ash/root_window_controller.h"
10 #include "ash/test/ash_test_base.h"
11 #include "ash/test/display_manager_test_api.h"
12 #include "ash/wm/property_util.h"
13 #include "base/command_line.h"
14 #include "base/stringprintf.h"
19 class TouchHudTest
: public test::AshTestBase
{
22 virtual ~TouchHudTest() {}
24 virtual void SetUp() OVERRIDE
{
25 // Add ash-touch-hud flag to enable touch HUD. This flag should be set
26 // before Ash environment is set up, i.e., before
27 // test::AshTestBase::SetUp().
28 CommandLine::ForCurrentProcess()->AppendSwitch(
29 ash::switches::kAshTouchHud
);
31 test::AshTestBase::SetUp();
33 // Initialize display infos. They should be initialized after Ash
34 // environment is set up, i.e., after test::AshTestBase::SetUp().
35 internal_display_id_
= test::DisplayManagerTestApi(GetDisplayManager()).
36 SetFirstDisplayAsInternalDisplay();
37 external_display_id_
= 10;
38 mirrored_display_id_
= 11;
40 internal_display_info_
=
41 CreateDisplayInfo(internal_display_id_
, gfx::Rect(0, 0, 500, 500));
42 external_display_info_
=
43 CreateDisplayInfo(external_display_id_
, gfx::Rect(1, 1, 100, 100));
44 mirrored_display_info_
=
45 CreateDisplayInfo(mirrored_display_id_
, gfx::Rect(0, 0, 100, 100));
48 const gfx::Display
& GetPrimaryDisplay() {
49 return GetDisplayController()->GetPrimaryDisplay();
52 const gfx::Display
& GetSecondaryDisplay() {
53 return *GetDisplayController()->GetSecondaryDisplay();
56 void SetupSingleDisplay() {
57 display_info_list_
.clear();
58 display_info_list_
.push_back(internal_display_info_
);
59 GetDisplayManager()->OnNativeDisplaysChanged(display_info_list_
);
62 void SetupDualDisplays() {
63 display_info_list_
.clear();
64 display_info_list_
.push_back(internal_display_info_
);
65 display_info_list_
.push_back(external_display_info_
);
66 GetDisplayManager()->OnNativeDisplaysChanged(display_info_list_
);
69 void SetInternalAsPrimary() {
70 const gfx::Display
& internal_display
=
71 GetDisplayManager()->GetDisplayForId(internal_display_id_
);
72 GetDisplayController()->SetPrimaryDisplay(internal_display
);
75 void SetExternalAsPrimary() {
76 const gfx::Display
& external_display
=
77 GetDisplayManager()->GetDisplayForId(external_display_id_
);
78 GetDisplayController()->SetPrimaryDisplay(external_display
);
81 void MirrorDisplays() {
82 DCHECK_EQ(2U, display_info_list_
.size());
83 DCHECK_EQ(internal_display_id_
, display_info_list_
[0].id());
84 DCHECK_EQ(external_display_id_
, display_info_list_
[1].id());
85 display_info_list_
[1] = mirrored_display_info_
;
86 GetDisplayManager()->OnNativeDisplaysChanged(display_info_list_
);
89 void UnmirrorDisplays() {
90 DCHECK_EQ(2U, display_info_list_
.size());
91 DCHECK_EQ(internal_display_id_
, display_info_list_
[0].id());
92 DCHECK_EQ(mirrored_display_id_
, display_info_list_
[1].id());
93 display_info_list_
[1] = external_display_info_
;
94 GetDisplayManager()->OnNativeDisplaysChanged(display_info_list_
);
97 void RemoveInternalDisplay() {
98 DCHECK_LT(0U, display_info_list_
.size());
99 DCHECK_EQ(internal_display_id_
, display_info_list_
[0].id());
100 display_info_list_
.erase(display_info_list_
.begin());
101 GetDisplayManager()->OnNativeDisplaysChanged(display_info_list_
);
104 void RemoveExternalDisplay() {
105 DCHECK_EQ(2U, display_info_list_
.size());
106 display_info_list_
.pop_back();
107 GetDisplayManager()->OnNativeDisplaysChanged(display_info_list_
);
110 void AddInternalDisplay() {
111 DCHECK_EQ(0U, display_info_list_
.size());
112 display_info_list_
.push_back(internal_display_info_
);
113 GetDisplayManager()->OnNativeDisplaysChanged(display_info_list_
);
116 void AddExternalDisplay() {
117 DCHECK_EQ(1U, display_info_list_
.size());
118 display_info_list_
.push_back(external_display_info_
);
119 GetDisplayManager()->OnNativeDisplaysChanged(display_info_list_
);
122 int64
internal_display_id() const {
123 return internal_display_id_
;
126 int64
external_display_id() const {
127 return external_display_id_
;
130 void CheckInternalDisplay() {
131 EXPECT_NE(static_cast<internal::TouchObserverHUD
*>(NULL
),
132 GetInternalTouchHud());
133 EXPECT_EQ(internal_display_id(), GetInternalTouchHud()->display_id_
);
134 EXPECT_EQ(GetInternalRootWindow(), GetInternalTouchHud()->root_window_
);
135 EXPECT_EQ(GetInternalRootWindow(),
136 GetInternalTouchHud()->widget_
->GetNativeView()->GetRootWindow());
137 EXPECT_EQ(GetInternalDisplay().size(),
138 GetInternalTouchHud()->widget_
->GetWindowBoundsInScreen().size());
141 void CheckExternalDisplay() {
142 EXPECT_NE(static_cast<internal::TouchObserverHUD
*>(NULL
),
143 GetExternalTouchHud());
144 EXPECT_EQ(external_display_id(), GetExternalTouchHud()->display_id_
);
145 EXPECT_EQ(GetExternalRootWindow(), GetExternalTouchHud()->root_window_
);
146 EXPECT_EQ(GetExternalRootWindow(),
147 GetExternalTouchHud()->widget_
->GetNativeView()->GetRootWindow());
148 EXPECT_EQ(GetExternalDisplay().size(),
149 GetExternalTouchHud()->widget_
->GetWindowBoundsInScreen().size());
153 DisplayManager
* GetDisplayManager() {
154 return Shell::GetInstance()->display_manager();
157 DisplayController
* GetDisplayController() {
158 return Shell::GetInstance()->display_controller();
161 const gfx::Display
& GetInternalDisplay() {
162 return GetDisplayManager()->GetDisplayForId(internal_display_id_
);
165 const gfx::Display
& GetExternalDisplay() {
166 return GetDisplayManager()->GetDisplayForId(external_display_id_
);
169 aura::RootWindow
* GetInternalRootWindow() {
170 return GetDisplayController()->GetRootWindowForDisplayId(
171 internal_display_id_
);
174 aura::RootWindow
* GetExternalRootWindow() {
175 return GetDisplayController()->GetRootWindowForDisplayId(
176 external_display_id_
);
179 aura::RootWindow
* GetPrimaryRootWindow() {
180 const gfx::Display
& display
= GetPrimaryDisplay();
181 return GetDisplayController()->GetRootWindowForDisplayId(display
.id());
184 aura::RootWindow
* GetSecondaryRootWindow() {
185 const gfx::Display
& display
= GetSecondaryDisplay();
186 return GetDisplayController()->GetRootWindowForDisplayId(display
.id());
189 internal::RootWindowController
* GetInternalRootController() {
190 aura::RootWindow
* root
= GetInternalRootWindow();
191 return GetRootWindowController(root
);
194 internal::RootWindowController
* GetExternalRootController() {
195 aura::RootWindow
* root
= GetExternalRootWindow();
196 return GetRootWindowController(root
);
199 internal::RootWindowController
* GetPrimaryRootController() {
200 aura::RootWindow
* root
= GetPrimaryRootWindow();
201 return GetRootWindowController(root
);
204 internal::RootWindowController
* GetSecondaryRootController() {
205 aura::RootWindow
* root
= GetSecondaryRootWindow();
206 return GetRootWindowController(root
);
209 internal::TouchObserverHUD
* GetInternalTouchHud() {
210 return GetInternalRootController()->touch_observer_hud();
213 internal::TouchObserverHUD
* GetExternalTouchHud() {
214 return GetExternalRootController()->touch_observer_hud();
217 internal::TouchObserverHUD
* GetPrimaryTouchHud() {
218 return GetPrimaryRootController()->touch_observer_hud();
221 internal::TouchObserverHUD
* GetSecondaryTouchHud() {
222 return GetSecondaryRootController()->touch_observer_hud();
225 DisplayInfo
CreateDisplayInfo(int64 id
, const gfx::Rect
& bounds
) {
226 DisplayInfo
info(id
, base::StringPrintf("x-%ld", id
), false);
227 info
.SetBounds(bounds
);
231 int64 internal_display_id_
;
232 int64 external_display_id_
;
233 int64 mirrored_display_id_
;
234 DisplayInfo internal_display_info_
;
235 DisplayInfo external_display_info_
;
236 DisplayInfo mirrored_display_info_
;
238 std::vector
<DisplayInfo
> display_info_list_
;
240 DISALLOW_COPY_AND_ASSIGN(TouchHudTest
);
243 // Checks if touch HUDs are correctly initialized for displays.
244 TEST_F(TouchHudTest
, Basic
) {
245 // Setup a dual display setting.
248 // Check if touch HUDs are set correctly and associated with appropriate
250 CheckInternalDisplay();
251 CheckExternalDisplay();
254 // Checks if touch HUDs are correctly handled when primary display is changed.
255 TEST_F(TouchHudTest
, SwapPrimaryDisplay
) {
256 // Setup a dual display setting.
259 // Set the primary display to the external one.
260 SetExternalAsPrimary();
262 // Check if displays' touch HUDs are not swapped as root windows are.
263 EXPECT_EQ(external_display_id(), GetPrimaryDisplay().id());
264 EXPECT_EQ(internal_display_id(), GetSecondaryDisplay().id());
265 CheckInternalDisplay();
266 CheckExternalDisplay();
268 // Set the primary display back to the internal one.
269 SetInternalAsPrimary();
271 // Check if displays' touch HUDs are not swapped back as root windows are.
272 EXPECT_EQ(internal_display_id(), GetPrimaryDisplay().id());
273 EXPECT_EQ(external_display_id(), GetSecondaryDisplay().id());
274 CheckInternalDisplay();
275 CheckExternalDisplay();
278 // Checks if touch HUDs are correctly handled when displays are mirrored.
279 TEST_F(TouchHudTest
, MirrorDisplays
) {
280 // Setup a dual display setting.
286 // Check if the internal display is intact.
287 EXPECT_EQ(internal_display_id(), GetPrimaryDisplay().id());
288 CheckInternalDisplay();
290 // Unmirror displays.
293 // Check if external display is added back correctly.
294 EXPECT_EQ(internal_display_id(), GetPrimaryDisplay().id());
295 EXPECT_EQ(external_display_id(), GetSecondaryDisplay().id());
296 CheckInternalDisplay();
297 CheckExternalDisplay();
300 // Checks if touch HUDs are correctly handled when displays are mirrored after
301 // setting the external display as the primary one.
302 TEST_F(TouchHudTest
, SwapPrimaryThenMirrorDisplays
) {
303 // Setup a dual display setting.
306 // Set the primary display to the external one.
307 SetExternalAsPrimary();
312 // Check if the internal display is set as the primary one.
313 EXPECT_EQ(internal_display_id(), GetPrimaryDisplay().id());
314 CheckInternalDisplay();
316 // Unmirror displays.
319 // Check if the external display is added back as the primary display and
320 // touch HUDs are set correctly.
321 EXPECT_EQ(external_display_id(), GetPrimaryDisplay().id());
322 EXPECT_EQ(internal_display_id(), GetSecondaryDisplay().id());
323 CheckInternalDisplay();
324 CheckExternalDisplay();
327 // Checks if touch HUDs are correctly handled when the external display, which
328 // is the secondary one, is removed.
329 TEST_F(TouchHudTest
, RemoveSecondaryDisplay
) {
330 // Setup a dual display setting.
333 // Remove external display which is the secondary one.
334 RemoveExternalDisplay();
336 // Check if the internal display is intact.
337 EXPECT_EQ(internal_display_id(), GetPrimaryDisplay().id());
338 CheckInternalDisplay();
340 // Add external display back.
341 AddExternalDisplay();
343 // Check if displays' touch HUDs are set correctly.
344 EXPECT_EQ(internal_display_id(), GetPrimaryDisplay().id());
345 EXPECT_EQ(external_display_id(), GetSecondaryDisplay().id());
346 CheckInternalDisplay();
347 CheckExternalDisplay();
350 // Checks if touch HUDs are correctly handled when the external display, which
351 // is set as the primary display, is removed.
352 TEST_F(TouchHudTest
, RemovePrimaryDisplay
) {
353 // Setup a dual display setting.
356 // Set the primary display to the external one.
357 SetExternalAsPrimary();
359 // Remove the external display which is the primary display.
360 RemoveExternalDisplay();
362 // Check if the internal display is set as the primary one.
363 EXPECT_EQ(internal_display_id(), GetPrimaryDisplay().id());
364 CheckInternalDisplay();
366 // Add the external display back.
367 AddExternalDisplay();
369 // Check if the external display is set as primary and touch HUDs are set
371 EXPECT_EQ(external_display_id(), GetPrimaryDisplay().id());
372 EXPECT_EQ(internal_display_id(), GetSecondaryDisplay().id());
373 CheckInternalDisplay();
374 CheckExternalDisplay();
377 // Checks if touch HUDs are correctly handled when all displays are removed.
378 TEST_F(TouchHudTest
, Headless
) {
379 // Setup a single display setting.
380 SetupSingleDisplay();
382 // Remove the only display which is the internal one.
383 RemoveInternalDisplay();
385 // Add the internal display back.
386 AddInternalDisplay();
388 // Check if the display's touch HUD is set correctly.
389 EXPECT_EQ(internal_display_id(), GetPrimaryDisplay().id());
390 CheckInternalDisplay();
393 } // namespace internal