1 // Copyright 2014 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/system/web_notification/ash_popup_alignment_delegate.h"
9 #include "ash/display/display_manager.h"
10 #include "ash/screen_util.h"
11 #include "ash/shelf/shelf_layout_manager.h"
12 #include "ash/shelf/shelf_types.h"
13 #include "ash/shell.h"
14 #include "ash/shell_window_ids.h"
15 #include "ash/test/ash_test_base.h"
16 #include "base/command_line.h"
17 #include "ui/gfx/geometry/rect.h"
18 #include "ui/gfx/screen.h"
19 #include "ui/keyboard/keyboard_switches.h"
20 #include "ui/keyboard/keyboard_util.h"
21 #include "ui/message_center/message_center_style.h"
25 class AshPopupAlignmentDelegateTest
: public test::AshTestBase
{
27 AshPopupAlignmentDelegateTest() {}
28 ~AshPopupAlignmentDelegateTest() override
{}
30 void SetUp() override
{
31 base::CommandLine::ForCurrentProcess()->AppendSwitch(
32 keyboard::switches::kEnableVirtualKeyboard
);
33 test::AshTestBase::SetUp();
34 SetAlignmentDelegate(make_scoped_ptr(new AshPopupAlignmentDelegate()));
37 void TearDown() override
{
38 alignment_delegate_
.reset();
39 test::AshTestBase::TearDown();
42 void SetKeyboardBounds(const gfx::Rect
& new_bounds
) {
43 ShelfLayoutManager::ForShelf(Shell::GetPrimaryRootWindow())
44 ->OnKeyboardBoundsChanging(new_bounds
);
56 AshPopupAlignmentDelegate
* alignment_delegate() {
57 return alignment_delegate_
.get();
60 void UpdateWorkArea(AshPopupAlignmentDelegate
* alignment_delegate
,
61 const gfx::Display
& display
) {
62 alignment_delegate
->StartObserving(Shell::GetScreen(), display
);
64 alignment_delegate
->OnDisplayWorkAreaInsetsChanged();
67 void SetAlignmentDelegate(scoped_ptr
<AshPopupAlignmentDelegate
> delegate
) {
68 if (!delegate
.get()) {
69 alignment_delegate_
.reset();
72 alignment_delegate_
= delegate
.Pass();
73 UpdateWorkArea(alignment_delegate_
.get(),
74 Shell::GetScreen()->GetPrimaryDisplay());
77 Position
GetPositionInDisplay(const gfx::Point
& point
) {
78 const gfx::Rect
& work_area
=
79 Shell::GetScreen()->GetPrimaryDisplay().work_area();
80 const gfx::Point center_point
= work_area
.CenterPoint();
81 if (work_area
.x() > point
.x() || work_area
.y() > point
.y() ||
82 work_area
.right() < point
.x() || work_area
.bottom() < point
.y()) {
86 if (center_point
.x() < point
.x())
87 return (center_point
.y() < point
.y()) ? BOTTOM_RIGHT
: TOP_RIGHT
;
89 return (center_point
.y() < point
.y()) ? BOTTOM_LEFT
: TOP_LEFT
;
92 gfx::Rect
GetWorkArea() {
93 return alignment_delegate_
->work_area_
;
97 scoped_ptr
<AshPopupAlignmentDelegate
> alignment_delegate_
;
100 TEST_F(AshPopupAlignmentDelegateTest
, ShelfAlignment
) {
101 const gfx::Rect
toast_size(0, 0, 10, 10);
102 UpdateDisplay("600x600");
103 gfx::Point toast_point
;
104 toast_point
.set_x(alignment_delegate()->GetToastOriginX(toast_size
));
105 toast_point
.set_y(alignment_delegate()->GetBaseLine());
106 EXPECT_EQ(BOTTOM_RIGHT
, GetPositionInDisplay(toast_point
));
107 EXPECT_FALSE(alignment_delegate()->IsTopDown());
108 EXPECT_FALSE(alignment_delegate()->IsFromLeft());
110 Shell::GetInstance()->SetShelfAlignment(
111 SHELF_ALIGNMENT_RIGHT
,
112 Shell::GetPrimaryRootWindow());
113 toast_point
.set_x(alignment_delegate()->GetToastOriginX(toast_size
));
114 toast_point
.set_y(alignment_delegate()->GetBaseLine());
115 EXPECT_EQ(BOTTOM_RIGHT
, GetPositionInDisplay(toast_point
));
116 EXPECT_FALSE(alignment_delegate()->IsTopDown());
117 EXPECT_FALSE(alignment_delegate()->IsFromLeft());
119 Shell::GetInstance()->SetShelfAlignment(
120 SHELF_ALIGNMENT_LEFT
,
121 Shell::GetPrimaryRootWindow());
122 toast_point
.set_x(alignment_delegate()->GetToastOriginX(toast_size
));
123 toast_point
.set_y(alignment_delegate()->GetBaseLine());
124 EXPECT_EQ(BOTTOM_LEFT
, GetPositionInDisplay(toast_point
));
125 EXPECT_FALSE(alignment_delegate()->IsTopDown());
126 EXPECT_TRUE(alignment_delegate()->IsFromLeft());
128 Shell::GetInstance()->SetShelfAlignment(
130 Shell::GetPrimaryRootWindow());
131 toast_point
.set_x(alignment_delegate()->GetToastOriginX(toast_size
));
132 toast_point
.set_y(alignment_delegate()->GetBaseLine());
133 EXPECT_EQ(TOP_RIGHT
, GetPositionInDisplay(toast_point
));
134 EXPECT_TRUE(alignment_delegate()->IsTopDown());
135 EXPECT_FALSE(alignment_delegate()->IsFromLeft());
138 TEST_F(AshPopupAlignmentDelegateTest
, LockScreen
) {
139 const gfx::Rect
toast_size(0, 0, 10, 10);
141 Shell::GetInstance()->SetShelfAlignment(
142 SHELF_ALIGNMENT_LEFT
,
143 Shell::GetPrimaryRootWindow());
144 gfx::Point toast_point
;
145 toast_point
.set_x(alignment_delegate()->GetToastOriginX(toast_size
));
146 toast_point
.set_y(alignment_delegate()->GetBaseLine());
147 EXPECT_EQ(BOTTOM_LEFT
, GetPositionInDisplay(toast_point
));
148 EXPECT_FALSE(alignment_delegate()->IsTopDown());
149 EXPECT_TRUE(alignment_delegate()->IsFromLeft());
151 BlockUserSession(BLOCKED_BY_LOCK_SCREEN
);
152 toast_point
.set_x(alignment_delegate()->GetToastOriginX(toast_size
));
153 toast_point
.set_y(alignment_delegate()->GetBaseLine());
154 EXPECT_EQ(BOTTOM_RIGHT
, GetPositionInDisplay(toast_point
));
155 EXPECT_FALSE(alignment_delegate()->IsTopDown());
156 EXPECT_FALSE(alignment_delegate()->IsFromLeft());
159 TEST_F(AshPopupAlignmentDelegateTest
, AutoHide
) {
160 const gfx::Rect
toast_size(0, 0, 10, 10);
161 UpdateDisplay("600x600");
162 int origin_x
= alignment_delegate()->GetToastOriginX(toast_size
);
163 int baseline
= alignment_delegate()->GetBaseLine();
165 // Create a window, otherwise autohide doesn't work.
166 scoped_ptr
<aura::Window
> window(CreateTestWindowInShellWithId(0));
167 Shell::GetInstance()->SetShelfAutoHideBehavior(
168 SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS
,
169 Shell::GetPrimaryRootWindow());
170 ShelfLayoutManager::ForShelf(Shell::GetPrimaryRootWindow())->
171 UpdateAutoHideStateNow();
172 EXPECT_EQ(origin_x
, alignment_delegate()->GetToastOriginX(toast_size
));
173 EXPECT_LT(baseline
, alignment_delegate()->GetBaseLine());
176 // Verify that docked window doesn't affect the popup alignment.
177 TEST_F(AshPopupAlignmentDelegateTest
, DockedWindow
) {
178 const gfx::Rect
toast_size(0, 0, 10, 10);
179 UpdateDisplay("600x600");
180 int origin_x
= alignment_delegate()->GetToastOriginX(toast_size
);
181 int baseline
= alignment_delegate()->GetBaseLine();
183 scoped_ptr
<aura::Window
> window(
184 CreateTestWindowInShellWithBounds(gfx::Rect(0, 0, 50, 50)));
185 aura::Window
* docked_container
= Shell::GetContainer(
186 Shell::GetPrimaryRootWindow(),
187 kShellWindowId_DockedContainer
);
188 docked_container
->AddChild(window
.get());
190 // Left-side dock should not affect popup alignment
191 EXPECT_EQ(origin_x
, alignment_delegate()->GetToastOriginX(toast_size
));
192 EXPECT_EQ(baseline
, alignment_delegate()->GetBaseLine());
193 EXPECT_FALSE(alignment_delegate()->IsTopDown());
194 EXPECT_FALSE(alignment_delegate()->IsFromLeft());
196 // Force dock to right-side
197 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT
,
198 Shell::GetPrimaryRootWindow());
199 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_BOTTOM
,
200 Shell::GetPrimaryRootWindow());
202 // Right-side dock should not affect popup alignment
203 EXPECT_EQ(origin_x
, alignment_delegate()->GetToastOriginX(toast_size
));
204 EXPECT_EQ(baseline
, alignment_delegate()->GetBaseLine());
205 EXPECT_FALSE(alignment_delegate()->IsTopDown());
206 EXPECT_FALSE(alignment_delegate()->IsFromLeft());
209 TEST_F(AshPopupAlignmentDelegateTest
, DisplayResize
) {
210 const gfx::Rect
toast_size(0, 0, 10, 10);
211 UpdateDisplay("600x600");
212 int origin_x
= alignment_delegate()->GetToastOriginX(toast_size
);
213 int baseline
= alignment_delegate()->GetBaseLine();
215 UpdateDisplay("800x800");
216 EXPECT_LT(origin_x
, alignment_delegate()->GetToastOriginX(toast_size
));
217 EXPECT_LT(baseline
, alignment_delegate()->GetBaseLine());
219 UpdateDisplay("400x400");
220 EXPECT_GT(origin_x
, alignment_delegate()->GetToastOriginX(toast_size
));
221 EXPECT_GT(baseline
, alignment_delegate()->GetBaseLine());
224 TEST_F(AshPopupAlignmentDelegateTest
, DockedMode
) {
225 if (!SupportsMultipleDisplays())
228 const gfx::Rect
toast_size(0, 0, 10, 10);
229 UpdateDisplay("600x600");
230 int origin_x
= alignment_delegate()->GetToastOriginX(toast_size
);
231 int baseline
= alignment_delegate()->GetBaseLine();
233 // Emulate the docked mode; enter to an extended mode, then invoke
234 // OnNativeDisplaysChanged() with the info for the secondary display only.
235 UpdateDisplay("600x600,800x800");
236 DisplayManager
* display_manager
= Shell::GetInstance()->display_manager();
238 std::vector
<DisplayInfo
> new_info
;
240 display_manager
->GetDisplayInfo(display_manager
->GetDisplayAt(1u).id()));
241 display_manager
->OnNativeDisplaysChanged(new_info
);
243 EXPECT_LT(origin_x
, alignment_delegate()->GetToastOriginX(toast_size
));
244 EXPECT_LT(baseline
, alignment_delegate()->GetBaseLine());
247 TEST_F(AshPopupAlignmentDelegateTest
, TrayHeight
) {
248 const gfx::Rect
toast_size(0, 0, 10, 10);
249 UpdateDisplay("600x600");
250 int origin_x
= alignment_delegate()->GetToastOriginX(toast_size
);
251 int baseline
= alignment_delegate()->GetBaseLine();
253 const int kTrayHeight
= 100;
254 alignment_delegate()->SetSystemTrayHeight(kTrayHeight
);
256 EXPECT_EQ(origin_x
, alignment_delegate()->GetToastOriginX(toast_size
));
257 EXPECT_EQ(baseline
- kTrayHeight
- message_center::kMarginBetweenItems
,
258 alignment_delegate()->GetBaseLine());
261 TEST_F(AshPopupAlignmentDelegateTest
, Extended
) {
262 if (!SupportsMultipleDisplays())
264 UpdateDisplay("600x600,800x800");
265 SetAlignmentDelegate(make_scoped_ptr(new AshPopupAlignmentDelegate()));
267 AshPopupAlignmentDelegate for_2nd_display
;
268 UpdateWorkArea(&for_2nd_display
, ScreenUtil::GetSecondaryDisplay());
269 // Make sure that the toast position on the secondary display is
270 // positioned correctly.
271 EXPECT_LT(1300, for_2nd_display
.GetToastOriginX(gfx::Rect(0, 0, 10, 10)));
272 EXPECT_LT(700, for_2nd_display
.GetBaseLine());
275 TEST_F(AshPopupAlignmentDelegateTest
, Unified
) {
276 if (!SupportsMultipleDisplays())
278 DisplayManager
* display_manager
= Shell::GetInstance()->display_manager();
279 display_manager
->SetUnifiedDesktopEnabled(true);
281 // Reset the delegate as the primary display's shelf will be destroyed during
283 SetAlignmentDelegate(scoped_ptr
<AshPopupAlignmentDelegate
>());
285 UpdateDisplay("600x600,800x800");
286 SetAlignmentDelegate(make_scoped_ptr(new AshPopupAlignmentDelegate()));
289 alignment_delegate()->GetToastOriginX(gfx::Rect(0, 0, 10, 10)));
292 // Tests that when the keyboard is showing that notifications appear above it,
293 // and that they return to normal once the keyboard is gone.
294 TEST_F(AshPopupAlignmentDelegateTest
, KeyboardShowing
) {
295 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
296 ASSERT_TRUE(keyboard::IsKeyboardOverscrollEnabled());
298 UpdateDisplay("600x600");
299 int baseline
= alignment_delegate()->GetBaseLine();
301 gfx::Rect
keyboard_bounds(0, 300, 600, 300);
302 SetKeyboardBounds(keyboard_bounds
);
303 int keyboard_baseline
= alignment_delegate()->GetBaseLine();
304 EXPECT_NE(baseline
, keyboard_baseline
);
305 EXPECT_GT(keyboard_bounds
.y(), keyboard_baseline
);
307 SetKeyboardBounds(gfx::Rect());
308 EXPECT_EQ(baseline
, alignment_delegate()->GetBaseLine());