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 "athena/home/public/home_card.h"
7 #include "athena/activity/public/activity_factory.h"
8 #include "athena/home/home_card_constants.h"
9 #include "athena/home/home_card_impl.h"
10 #include "athena/home/home_card_view.h"
11 #include "athena/test/base/athena_test_base.h"
12 #include "athena/test/base/test_windows.h"
13 #include "athena/wm/public/window_manager.h"
14 #include "ui/app_list/app_list_model.h"
15 #include "ui/app_list/app_list_view_delegate.h"
16 #include "ui/app_list/views/app_list_main_view.h"
17 #include "ui/app_list/views/contents_view.h"
18 #include "ui/aura/test/test_window_delegate.h"
19 #include "ui/aura/window.h"
20 #include "ui/events/test/event_generator.h"
21 #include "ui/gfx/display.h"
22 #include "ui/gfx/screen.h"
23 #include "ui/views/controls/textfield/textfield.h"
24 #include "ui/views/focus/focus_manager.h"
25 #include "ui/views/widget/widget.h"
26 #include "ui/wm/core/shadow_types.h"
27 #include "ui/wm/core/window_util.h"
31 aura::Window
* GetHomeCardWindow() {
32 return static_cast<HomeCardImpl
*>(HomeCard::Get())->
33 GetHomeCardWindowForTest();
36 typedef test::AthenaTestBase HomeCardTest
;
38 TEST_F(HomeCardTest
, BasicTransition
) {
39 ASSERT_EQ(HomeCard::VISIBLE_MINIMIZED
, HomeCard::Get()->GetState());
40 aura::Window
* home_card
= GetHomeCardWindow();
41 const int screen_height
= root_window()->bounds().height();
42 const int work_area_height
= gfx::Screen::GetScreenFor(root_window())->
43 GetDisplayNearestWindow(root_window()).work_area().height();
44 ASSERT_TRUE(home_card
);
46 // In the minimized state, home card should be outside (below) the work area.
47 EXPECT_EQ(screen_height
- kHomeCardMinimizedHeight
,
48 home_card
->GetTargetBounds().y());
49 EXPECT_EQ(work_area_height
, home_card
->GetTargetBounds().y());
51 WindowManager::Get()->EnterOverview();
52 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, HomeCard::Get()->GetState());
53 EXPECT_EQ(screen_height
- kHomeCardHeight
, home_card
->GetTargetBounds().y());
55 WindowManager::Get()->ExitOverview();
56 EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED
, HomeCard::Get()->GetState());
57 EXPECT_EQ(work_area_height
, home_card
->GetTargetBounds().y());
60 TEST_F(HomeCardTest
, VirtualKeyboardTransition
) {
61 // Minimized -> Hidden for virtual keyboard.
62 EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED
, HomeCard::Get()->GetState());
63 const gfx::Rect
vk_bounds(0, 0, 100, 100);
64 HomeCard::Get()->UpdateVirtualKeyboardBounds(vk_bounds
);
65 EXPECT_EQ(HomeCard::HIDDEN
, HomeCard::Get()->GetState());
66 HomeCard::Get()->UpdateVirtualKeyboardBounds(gfx::Rect());
67 EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED
, HomeCard::Get()->GetState());
69 // bottom -> centered for virtual keyboard.
70 WindowManager::Get()->EnterOverview();
71 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, HomeCard::Get()->GetState());
72 HomeCard::Get()->UpdateVirtualKeyboardBounds(vk_bounds
);
73 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, HomeCard::Get()->GetState());
75 aura::Window
* home_card
= GetHomeCardWindow();
76 EXPECT_EQ(0, home_card
->GetTargetBounds().y());
78 HomeCard::Get()->UpdateVirtualKeyboardBounds(gfx::Rect());
79 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, HomeCard::Get()->GetState());
82 TEST_F(HomeCardTest
, ToggleOverviewWithVirtualKeyboard
) {
83 // Minimized -> Hidden for virtual keyboard.
84 EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED
, HomeCard::Get()->GetState());
85 const gfx::Rect
vk_bounds(0, 0, 100, 100);
86 HomeCard::Get()->UpdateVirtualKeyboardBounds(vk_bounds
);
87 EXPECT_EQ(HomeCard::HIDDEN
, HomeCard::Get()->GetState());
89 // EnterOverview() revives the bottom home card. Home card also gets
90 /// activated which will close the virtual keyboard.
91 WindowManager::Get()->EnterOverview();
92 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, HomeCard::Get()->GetState());
93 aura::Window
* home_card
= GetHomeCardWindow();
94 EXPECT_TRUE(wm::IsActiveWindow(home_card
));
97 // Verify if the home card is correctly minimized after app launch.
98 TEST_F(HomeCardTest
, AppSelection
) {
99 EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED
, HomeCard::Get()->GetState());
101 WindowManager::Get()->EnterOverview();
102 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, HomeCard::Get()->GetState());
104 athena::ActivityFactory::Get()->CreateWebActivity(
105 nullptr, base::string16(), GURL("http://www.google.com/"));
106 EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED
, HomeCard::Get()->GetState());
109 TEST_F(HomeCardTest
, Accelerators
) {
110 EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED
, HomeCard::Get()->GetState());
112 ui::test::EventGenerator
generator(root_window());
114 // CTRL+L toggles centered home card, check that overview mode follows
115 generator
.PressKey(ui::VKEY_L
, ui::EF_CONTROL_DOWN
);
116 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, HomeCard::Get()->GetState());
117 EXPECT_TRUE(WindowManager::Get()->IsOverviewModeActive());
118 generator
.PressKey(ui::VKEY_L
, ui::EF_CONTROL_DOWN
);
119 EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED
, HomeCard::Get()->GetState());
120 EXPECT_FALSE(WindowManager::Get()->IsOverviewModeActive());
122 // ESC key hides centered home card
123 generator
.PressKey(ui::VKEY_L
, ui::EF_CONTROL_DOWN
);
124 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, HomeCard::Get()->GetState());
125 EXPECT_TRUE(WindowManager::Get()->IsOverviewModeActive());
126 generator
.PressKey(ui::VKEY_ESCAPE
, ui::EF_NONE
);
127 EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED
, HomeCard::Get()->GetState());
128 EXPECT_FALSE(WindowManager::Get()->IsOverviewModeActive());
130 // Do nothing with bottom home card with CTRL+L, hide with ESC key
131 WindowManager::Get()->EnterOverview();
132 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, HomeCard::Get()->GetState());
133 EXPECT_TRUE(WindowManager::Get()->IsOverviewModeActive());
134 generator
.PressKey(ui::VKEY_L
, ui::EF_CONTROL_DOWN
);
135 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, HomeCard::Get()->GetState());
136 EXPECT_TRUE(WindowManager::Get()->IsOverviewModeActive());
137 generator
.PressKey(ui::VKEY_ESCAPE
, ui::EF_NONE
);
138 EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED
, HomeCard::Get()->GetState());
139 EXPECT_FALSE(WindowManager::Get()->IsOverviewModeActive());
141 // Do nothing if the centered state is a temporary state.
142 WindowManager::Get()->EnterOverview();
143 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, HomeCard::Get()->GetState());
144 EXPECT_TRUE(WindowManager::Get()->IsOverviewModeActive());
145 HomeCard::Get()->UpdateVirtualKeyboardBounds(gfx::Rect(0, 0, 100, 100));
146 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, HomeCard::Get()->GetState());
147 EXPECT_TRUE(WindowManager::Get()->IsOverviewModeActive());
148 generator
.PressKey(ui::VKEY_L
, ui::EF_CONTROL_DOWN
);
149 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, HomeCard::Get()->GetState());
150 EXPECT_TRUE(WindowManager::Get()->IsOverviewModeActive());
153 TEST_F(HomeCardTest
, MouseClick
) {
154 ASSERT_EQ(HomeCard::VISIBLE_MINIMIZED
, HomeCard::Get()->GetState());
156 // Mouse click at the bottom of the screen should invokes overview mode and
157 // changes the state to BOTTOM.
158 gfx::Rect
screen_rect(root_window()->bounds());
159 ui::test::EventGenerator
generator(
160 root_window(), gfx::Point(
161 screen_rect
.x() + screen_rect
.width() / 2, screen_rect
.bottom() - 1));
162 generator
.ClickLeftButton();
164 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, HomeCard::Get()->GetState());
165 EXPECT_TRUE(WindowManager::Get()->IsOverviewModeActive());
167 // Further clicks are simply ignored.
168 generator
.ClickLeftButton();
169 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, HomeCard::Get()->GetState());
170 EXPECT_TRUE(WindowManager::Get()->IsOverviewModeActive());
173 TEST_F(HomeCardTest
, Gestures
) {
174 ASSERT_EQ(HomeCard::VISIBLE_MINIMIZED
, HomeCard::Get()->GetState());
175 ui::test::EventGenerator
generator(root_window());
176 gfx::Rect
screen_rect(root_window()->bounds());
178 const int bottom
= screen_rect
.bottom();
179 const int x
= screen_rect
.x() + 1;
181 generator
.GestureScrollSequence(gfx::Point(x
, bottom
- 1),
182 gfx::Point(x
, bottom
- 70),
183 base::TimeDelta::FromSeconds(1),
185 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, HomeCard::Get()->GetState());
186 EXPECT_TRUE(WindowManager::Get()->IsOverviewModeActive());
188 // Too short moves. Nothing has changed.
189 generator
.GestureScrollSequence(gfx::Point(x
, bottom
- 40),
190 gfx::Point(x
, bottom
- 80),
191 base::TimeDelta::FromSeconds(1),
193 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, HomeCard::Get()->GetState());
194 EXPECT_TRUE(WindowManager::Get()->IsOverviewModeActive());
196 generator
.GestureScrollSequence(gfx::Point(x
, bottom
- 40),
197 gfx::Point(x
, bottom
- 20),
198 base::TimeDelta::FromSeconds(1),
200 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, HomeCard::Get()->GetState());
201 EXPECT_TRUE(WindowManager::Get()->IsOverviewModeActive());
203 // Swipe up to the centered state.
204 generator
.GestureScrollSequence(gfx::Point(x
, bottom
- 40),
205 gfx::Point(x
, bottom
- 300),
206 base::TimeDelta::FromSeconds(1),
208 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, HomeCard::Get()->GetState());
209 EXPECT_TRUE(WindowManager::Get()->IsOverviewModeActive());
211 // Swipe up from centered; nothing has to be changed.
212 generator
.GestureScrollSequence(gfx::Point(x
, bottom
- 300),
213 gfx::Point(x
, bottom
- 350),
214 base::TimeDelta::FromSeconds(1),
216 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, HomeCard::Get()->GetState());
217 EXPECT_TRUE(WindowManager::Get()->IsOverviewModeActive());
219 // Swipe down slightly; nothing has to be changed.
220 generator
.GestureScrollSequence(gfx::Point(x
, bottom
- 300),
221 gfx::Point(x
, bottom
- 250),
222 base::TimeDelta::FromSeconds(1),
224 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, HomeCard::Get()->GetState());
225 EXPECT_TRUE(WindowManager::Get()->IsOverviewModeActive());
227 // Swipe down to the bottom state.
228 generator
.GestureScrollSequence(gfx::Point(x
, 50),
229 gfx::Point(x
, bottom
- 120),
230 base::TimeDelta::FromSeconds(1), 10);
231 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, HomeCard::Get()->GetState());
232 EXPECT_TRUE(WindowManager::Get()->IsOverviewModeActive());
234 generator
.GestureScrollSequence(gfx::Point(x
, bottom
- 40),
235 gfx::Point(x
, bottom
- 300),
236 base::TimeDelta::FromSeconds(1),
238 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, HomeCard::Get()->GetState());
239 EXPECT_TRUE(WindowManager::Get()->IsOverviewModeActive());
241 // Swipe down to the minimized state.
242 generator
.GestureScrollSequence(gfx::Point(x
, 50), gfx::Point(x
, bottom
- 1),
243 base::TimeDelta::FromSeconds(1), 10);
244 EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED
, HomeCard::Get()->GetState());
245 EXPECT_FALSE(WindowManager::Get()->IsOverviewModeActive());
248 TEST_F(HomeCardTest
, GesturesToFullDirectly
) {
249 ASSERT_EQ(HomeCard::VISIBLE_MINIMIZED
, HomeCard::Get()->GetState());
250 ui::test::EventGenerator
generator(root_window());
251 gfx::Rect
screen_rect(root_window()->bounds());
253 const int bottom
= screen_rect
.bottom();
254 const int x
= screen_rect
.x() + 1;
256 generator
.GestureScrollSequence(gfx::Point(x
, bottom
- 1),
258 base::TimeDelta::FromSeconds(1),
260 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, HomeCard::Get()->GetState());
261 EXPECT_TRUE(WindowManager::Get()->IsOverviewModeActive());
264 TEST_F(HomeCardTest
, DontMinimizeWithModalWindow
) {
265 aura::Window
* home_card
= GetHomeCardWindow();
267 WindowManager::Get()->EnterOverview();
268 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, HomeCard::Get()->GetState());
269 EXPECT_TRUE(wm::IsActiveWindow(home_card
));
271 aura::test::TestWindowDelegate delegate
;
272 scoped_ptr
<aura::Window
> modal(test::CreateTransientWindow(
273 &delegate
, nullptr, ui::MODAL_TYPE_SYSTEM
, false));
275 wm::ActivateWindow(modal
.get());
276 EXPECT_TRUE(wm::IsActiveWindow(modal
.get()));
277 EXPECT_FALSE(wm::IsActiveWindow(home_card
));
278 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, HomeCard::Get()->GetState());
282 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, HomeCard::Get()->GetState());
283 EXPECT_TRUE(wm::IsActiveWindow(home_card
));
286 TEST_F(HomeCardTest
, AppListStates
) {
287 app_list::AppListModel
* model
=
288 static_cast<HomeCardImpl
*>(HomeCard::Get())->view_delegate_
->GetModel();
290 WindowManager::Get()->EnterOverview();
291 ASSERT_EQ(HomeCard::VISIBLE_BOTTOM
, HomeCard::Get()->GetState());
292 ASSERT_EQ(app_list::AppListModel::STATE_START
, model
->state());
294 // Changes the contents of the home card to "apps" view, which should change
295 // the home card state to VISIBLE_CENTERED.
296 app_list::ContentsView
* contents_view
=
297 static_cast<HomeCardImpl
*>(HomeCard::Get())
298 ->home_card_view_
->main_view_
->contents_view();
299 contents_view
->SetActivePage(
300 contents_view
->GetPageIndexForState(app_list::AppListModel::STATE_APPS
));
301 EXPECT_EQ(HomeCard::VISIBLE_CENTERED
, HomeCard::Get()->GetState());
302 EXPECT_EQ(app_list::AppListModel::STATE_APPS
, model
->state());
304 // VISIBLE_BOTTOM state should always show the start page.
305 HomeCard::Get()->SetState(HomeCard::VISIBLE_BOTTOM
);
306 EXPECT_EQ(app_list::AppListModel::STATE_START
, model
->state());
308 // VISIBLE_CENTERED with apps mode state to minimized -- and then back to
310 contents_view
->SetActivePage(
311 contents_view
->GetPageIndexForState(app_list::AppListModel::STATE_APPS
));
312 EXPECT_EQ(app_list::AppListModel::STATE_APPS
, model
->state());
313 WindowManager::Get()->ExitOverview();
314 WindowManager::Get()->EnterOverview();
315 EXPECT_EQ(HomeCard::VISIBLE_BOTTOM
, HomeCard::Get()->GetState());
316 EXPECT_EQ(app_list::AppListModel::STATE_START
, model
->state());
319 } // namespace athena