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/shelf/shelf_layout_manager.h"
11 #include "ash/shelf/shelf_types.h"
12 #include "ash/shell.h"
13 #include "ash/shell_window_ids.h"
14 #include "ash/test/ash_test_base.h"
15 #include "ui/gfx/screen.h"
16 #include "ui/message_center/message_center_style.h"
20 class AshPopupAlignmentDelegateTest
: public test::AshTestBase
{
22 AshPopupAlignmentDelegateTest() {}
23 ~AshPopupAlignmentDelegateTest() override
{}
25 void SetUp() override
{
26 test::AshTestBase::SetUp();
27 alignment_delegate_
.reset(new AshPopupAlignmentDelegate());
28 alignment_delegate_
->StartObserving(
29 Shell::GetScreen(), Shell::GetScreen()->GetPrimaryDisplay());
32 void TearDown() override
{
33 alignment_delegate_
.reset();
34 test::AshTestBase::TearDown();
46 AshPopupAlignmentDelegate
* alignment_delegate() {
47 return alignment_delegate_
.get();
50 Position
GetPositionInDisplay(const gfx::Point
& point
) {
51 const gfx::Rect
& work_area
=
52 Shell::GetScreen()->GetPrimaryDisplay().work_area();
53 const gfx::Point center_point
= work_area
.CenterPoint();
54 if (work_area
.x() > point
.x() || work_area
.y() > point
.y() ||
55 work_area
.right() < point
.x() || work_area
.bottom() < point
.y()) {
59 if (center_point
.x() < point
.x())
60 return (center_point
.y() < point
.y()) ? BOTTOM_RIGHT
: TOP_RIGHT
;
62 return (center_point
.y() < point
.y()) ? BOTTOM_LEFT
: TOP_LEFT
;
65 gfx::Rect
GetWorkArea() {
66 return alignment_delegate_
->work_area_
;
70 scoped_ptr
<AshPopupAlignmentDelegate
> alignment_delegate_
;
73 TEST_F(AshPopupAlignmentDelegateTest
, ShelfAlignment
) {
74 const gfx::Rect
toast_size(0, 0, 10, 10);
75 UpdateDisplay("600x600");
76 gfx::Point toast_point
;
77 toast_point
.set_x(alignment_delegate()->GetToastOriginX(toast_size
));
78 toast_point
.set_y(alignment_delegate()->GetBaseLine());
79 EXPECT_EQ(BOTTOM_RIGHT
, GetPositionInDisplay(toast_point
));
80 EXPECT_FALSE(alignment_delegate()->IsTopDown());
81 EXPECT_FALSE(alignment_delegate()->IsFromLeft());
83 Shell::GetInstance()->SetShelfAlignment(
84 SHELF_ALIGNMENT_RIGHT
,
85 Shell::GetPrimaryRootWindow());
86 toast_point
.set_x(alignment_delegate()->GetToastOriginX(toast_size
));
87 toast_point
.set_y(alignment_delegate()->GetBaseLine());
88 EXPECT_EQ(BOTTOM_RIGHT
, GetPositionInDisplay(toast_point
));
89 EXPECT_FALSE(alignment_delegate()->IsTopDown());
90 EXPECT_FALSE(alignment_delegate()->IsFromLeft());
92 Shell::GetInstance()->SetShelfAlignment(
94 Shell::GetPrimaryRootWindow());
95 toast_point
.set_x(alignment_delegate()->GetToastOriginX(toast_size
));
96 toast_point
.set_y(alignment_delegate()->GetBaseLine());
97 EXPECT_EQ(BOTTOM_LEFT
, GetPositionInDisplay(toast_point
));
98 EXPECT_FALSE(alignment_delegate()->IsTopDown());
99 EXPECT_TRUE(alignment_delegate()->IsFromLeft());
101 Shell::GetInstance()->SetShelfAlignment(
103 Shell::GetPrimaryRootWindow());
104 toast_point
.set_x(alignment_delegate()->GetToastOriginX(toast_size
));
105 toast_point
.set_y(alignment_delegate()->GetBaseLine());
106 EXPECT_EQ(TOP_RIGHT
, GetPositionInDisplay(toast_point
));
107 EXPECT_TRUE(alignment_delegate()->IsTopDown());
108 EXPECT_FALSE(alignment_delegate()->IsFromLeft());
111 TEST_F(AshPopupAlignmentDelegateTest
, LockScreen
) {
112 const gfx::Rect
toast_size(0, 0, 10, 10);
114 Shell::GetInstance()->SetShelfAlignment(
115 SHELF_ALIGNMENT_LEFT
,
116 Shell::GetPrimaryRootWindow());
117 gfx::Point toast_point
;
118 toast_point
.set_x(alignment_delegate()->GetToastOriginX(toast_size
));
119 toast_point
.set_y(alignment_delegate()->GetBaseLine());
120 EXPECT_EQ(BOTTOM_LEFT
, GetPositionInDisplay(toast_point
));
121 EXPECT_FALSE(alignment_delegate()->IsTopDown());
122 EXPECT_TRUE(alignment_delegate()->IsFromLeft());
124 BlockUserSession(BLOCKED_BY_LOCK_SCREEN
);
125 toast_point
.set_x(alignment_delegate()->GetToastOriginX(toast_size
));
126 toast_point
.set_y(alignment_delegate()->GetBaseLine());
127 EXPECT_EQ(BOTTOM_RIGHT
, GetPositionInDisplay(toast_point
));
128 EXPECT_FALSE(alignment_delegate()->IsTopDown());
129 EXPECT_FALSE(alignment_delegate()->IsFromLeft());
132 TEST_F(AshPopupAlignmentDelegateTest
, AutoHide
) {
133 const gfx::Rect
toast_size(0, 0, 10, 10);
134 UpdateDisplay("600x600");
135 int origin_x
= alignment_delegate()->GetToastOriginX(toast_size
);
136 int baseline
= alignment_delegate()->GetBaseLine();
138 // Create a window, otherwise autohide doesn't work.
139 scoped_ptr
<aura::Window
> window(CreateTestWindowInShellWithId(0));
140 Shell::GetInstance()->SetShelfAutoHideBehavior(
141 SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS
,
142 Shell::GetPrimaryRootWindow());
143 ShelfLayoutManager::ForShelf(Shell::GetPrimaryRootWindow())->
144 UpdateAutoHideStateNow();
145 EXPECT_EQ(origin_x
, alignment_delegate()->GetToastOriginX(toast_size
));
146 EXPECT_LT(baseline
, alignment_delegate()->GetBaseLine());
149 // Verify that docked window doesn't affect the popup alignment.
150 TEST_F(AshPopupAlignmentDelegateTest
, DockedWindow
) {
151 const gfx::Rect
toast_size(0, 0, 10, 10);
152 UpdateDisplay("600x600");
153 int origin_x
= alignment_delegate()->GetToastOriginX(toast_size
);
154 int baseline
= alignment_delegate()->GetBaseLine();
156 scoped_ptr
<aura::Window
> window(
157 CreateTestWindowInShellWithBounds(gfx::Rect(0, 0, 50, 50)));
158 aura::Window
* docked_container
= Shell::GetContainer(
159 Shell::GetPrimaryRootWindow(),
160 kShellWindowId_DockedContainer
);
161 docked_container
->AddChild(window
.get());
163 EXPECT_EQ(origin_x
, alignment_delegate()->GetToastOriginX(toast_size
));
164 EXPECT_EQ(baseline
, alignment_delegate()->GetBaseLine());
165 EXPECT_FALSE(alignment_delegate()->IsTopDown());
166 EXPECT_FALSE(alignment_delegate()->IsFromLeft());
169 TEST_F(AshPopupAlignmentDelegateTest
, DisplayResize
) {
170 const gfx::Rect
toast_size(0, 0, 10, 10);
171 UpdateDisplay("600x600");
172 int origin_x
= alignment_delegate()->GetToastOriginX(toast_size
);
173 int baseline
= alignment_delegate()->GetBaseLine();
175 UpdateDisplay("800x800");
176 EXPECT_LT(origin_x
, alignment_delegate()->GetToastOriginX(toast_size
));
177 EXPECT_LT(baseline
, alignment_delegate()->GetBaseLine());
179 UpdateDisplay("400x400");
180 EXPECT_GT(origin_x
, alignment_delegate()->GetToastOriginX(toast_size
));
181 EXPECT_GT(baseline
, alignment_delegate()->GetBaseLine());
184 TEST_F(AshPopupAlignmentDelegateTest
, DockedMode
) {
185 if (!SupportsMultipleDisplays())
188 const gfx::Rect
toast_size(0, 0, 10, 10);
189 UpdateDisplay("600x600");
190 int origin_x
= alignment_delegate()->GetToastOriginX(toast_size
);
191 int baseline
= alignment_delegate()->GetBaseLine();
193 // Emulate the docked mode; enter to an extended mode, then invoke
194 // OnNativeDisplaysChanged() with the info for the secondary display only.
195 UpdateDisplay("600x600,800x800");
196 DisplayManager
* display_manager
= Shell::GetInstance()->display_manager();
198 std::vector
<DisplayInfo
> new_info
;
200 display_manager
->GetDisplayInfo(display_manager
->GetDisplayAt(1u).id()));
201 display_manager
->OnNativeDisplaysChanged(new_info
);
203 EXPECT_LT(origin_x
, alignment_delegate()->GetToastOriginX(toast_size
));
204 EXPECT_LT(baseline
, alignment_delegate()->GetBaseLine());
207 TEST_F(AshPopupAlignmentDelegateTest
, TrayHeight
) {
208 const gfx::Rect
toast_size(0, 0, 10, 10);
209 UpdateDisplay("600x600");
210 int origin_x
= alignment_delegate()->GetToastOriginX(toast_size
);
211 int baseline
= alignment_delegate()->GetBaseLine();
213 const int kTrayHeight
= 100;
214 alignment_delegate()->SetSystemTrayHeight(kTrayHeight
);
216 EXPECT_EQ(origin_x
, alignment_delegate()->GetToastOriginX(toast_size
));
217 EXPECT_EQ(baseline
- kTrayHeight
- message_center::kMarginBetweenItems
,
218 alignment_delegate()->GetBaseLine());