1 // Copyright 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/shelf/shelf_window_watcher.h"
7 #include "ash/ash_switches.h"
8 #include "ash/shelf/shelf_item_types.h"
9 #include "ash/shelf/shelf_model.h"
10 #include "ash/shelf/shelf_util.h"
11 #include "ash/shell.h"
12 #include "ash/shell_window_ids.h"
13 #include "ash/test/ash_test_base.h"
14 #include "ash/test/shell_test_api.h"
15 #include "ash/wm/window_resizer.h"
16 #include "ash/wm/window_state.h"
17 #include "ash/wm/window_util.h"
18 #include "base/command_line.h"
19 #include "ui/aura/client/aura_constants.h"
20 #include "ui/aura/window.h"
21 #include "ui/base/hit_test.h"
26 class ShelfWindowWatcherTest : public test::AshTestBase {
28 ShelfWindowWatcherTest() : model_(NULL) {}
29 virtual ~ShelfWindowWatcherTest() {}
31 virtual void SetUp() OVERRIDE {
32 test::AshTestBase::SetUp();
33 model_ = test::ShellTestApi(Shell::GetInstance()).shelf_model();
36 virtual void TearDown() OVERRIDE {
38 test::AshTestBase::TearDown();
41 ShelfID CreateShelfItem(aura::Window* window) {
42 ShelfID id = model_->next_id();
43 ShelfItemDetails item_details;
44 item_details.type = TYPE_PLATFORM_APP;
45 SetShelfItemDetailsForWindow(window, item_details);
53 DISALLOW_COPY_AND_ASSIGN(ShelfWindowWatcherTest);
56 TEST_F(ShelfWindowWatcherTest, CreateAndRemoveShelfItem) {
57 // ShelfModel only has an APP_LIST item.
58 EXPECT_EQ(1, model_->item_count());
60 scoped_ptr<aura::Window> w1(CreateTestWindowInShellWithId(0));
61 scoped_ptr<aura::Window> w2(CreateTestWindowInShellWithId(0));
63 // Create a ShelfItem for w1.
64 ShelfID id_w1 = CreateShelfItem(w1.get());
65 EXPECT_EQ(2, model_->item_count());
67 int index_w1 = model_->ItemIndexByID(id_w1);
68 EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w1].status);
70 // Create a ShelfItem for w2.
71 ShelfID id_w2 = CreateShelfItem(w2.get());
72 EXPECT_EQ(3, model_->item_count());
74 int index_w2 = model_->ItemIndexByID(id_w2);
75 EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w2].status);
77 // ShelfItem is removed when assoicated window is destroyed.
78 ClearShelfItemDetailsForWindow(w1.get());
79 EXPECT_EQ(2, model_->item_count());
80 ClearShelfItemDetailsForWindow(w2.get());
81 EXPECT_EQ(1, model_->item_count());
82 // Clears twice doesn't do anything.
83 ClearShelfItemDetailsForWindow(w2.get());
84 EXPECT_EQ(1, model_->item_count());
88 TEST_F(ShelfWindowWatcherTest, ActivateWindow) {
89 // ShelfModel only have APP_LIST item.
90 EXPECT_EQ(1, model_->item_count());
91 scoped_ptr<aura::Window> w1(CreateTestWindowInShellWithId(0));
92 scoped_ptr<aura::Window> w2(CreateTestWindowInShellWithId(0));
94 // Create a ShelfItem for w1.
95 ShelfID id_w1 = CreateShelfItem(w1.get());
96 EXPECT_EQ(2, model_->item_count());
97 int index_w1 = model_->ItemIndexByID(id_w1);
98 EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w1].status);
100 // Create a ShelfItem for w2.
101 ShelfID id_w2 = CreateShelfItem(w2.get());
102 EXPECT_EQ(3, model_->item_count());
103 int index_w2 = model_->ItemIndexByID(id_w2);
104 EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w1].status);
105 EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w2].status);
107 // ShelfItem for w1 is active when w1 is activated.
108 wm::ActivateWindow(w1.get());
109 EXPECT_EQ(STATUS_ACTIVE, model_->items()[index_w1].status);
111 // ShelfItem for w2 is active state when w2 is activated.
112 wm::ActivateWindow(w2.get());
113 EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w1].status);
114 EXPECT_EQ(STATUS_ACTIVE, model_->items()[index_w2].status);
117 TEST_F(ShelfWindowWatcherTest, UpdateWindowProperty) {
118 // ShelfModel only has an APP_LIST item.
119 EXPECT_EQ(1, model_->item_count());
121 scoped_ptr<aura::Window> window(CreateTestWindowInShellWithId(0));
123 // Create a ShelfItem for |window|.
124 ShelfID id = CreateShelfItem(window.get());
125 EXPECT_EQ(2, model_->item_count());
127 int index = model_->ItemIndexByID(id);
128 EXPECT_EQ(STATUS_RUNNING, model_->items()[index].status);
130 // Update ShelfItem for |window|.
131 ShelfItemDetails details;
132 details.type = TYPE_PLATFORM_APP;
134 SetShelfItemDetailsForWindow(window.get(), details);
135 // No new item is created after updating a launcher item.
136 EXPECT_EQ(2, model_->item_count());
137 // index and id are not changed after updating a launcher item.
138 EXPECT_EQ(index, model_->ItemIndexByID(id));
139 EXPECT_EQ(id, model_->items()[index].id);
142 TEST_F(ShelfWindowWatcherTest, MaximizeAndRestoreWindow) {
143 // ShelfModel only has an APP_LIST item.
144 EXPECT_EQ(1, model_->item_count());
146 scoped_ptr<aura::Window> window(CreateTestWindowInShellWithId(0));
147 wm::WindowState* window_state = wm::GetWindowState(window.get());
149 // Create a ShelfItem for |window|.
150 ShelfID id = CreateShelfItem(window.get());
151 EXPECT_EQ(2, model_->item_count());
153 int index = model_->ItemIndexByID(id);
154 EXPECT_EQ(STATUS_RUNNING, model_->items()[index].status);
156 // Maximize window |window|.
157 EXPECT_FALSE(window_state->IsMaximized());
158 window_state->Maximize();
159 EXPECT_TRUE(window_state->IsMaximized());
160 // No new item is created after maximizing a window |window|.
161 EXPECT_EQ(2, model_->item_count());
162 // index and id are not changed after maximizing a window |window|.
163 EXPECT_EQ(index, model_->ItemIndexByID(id));
164 EXPECT_EQ(id, model_->items()[index].id);
166 // Restore window |window|.
167 window_state->Restore();
168 EXPECT_FALSE(window_state->IsMaximized());
169 // No new item is created after restoring a window |window|.
170 EXPECT_EQ(2, model_->item_count());
171 // Index and id are not changed after maximizing a window |window|.
172 EXPECT_EQ(index, model_->ItemIndexByID(id));
173 EXPECT_EQ(id, model_->items()[index].id);
176 // Check that an item is removed when its associated Window is re-parented.
177 TEST_F(ShelfWindowWatcherTest, ReparentWindow) {
178 // ShelfModel only has an APP_LIST item.
179 EXPECT_EQ(1, model_->item_count());
181 scoped_ptr<aura::Window> window(CreateTestWindowInShellWithId(0));
182 window->set_owned_by_parent(false);
184 // Create a ShelfItem for |window|.
185 ShelfID id = CreateShelfItem(window.get());
186 EXPECT_EQ(2, model_->item_count());
188 int index = model_->ItemIndexByID(id);
189 EXPECT_EQ(STATUS_RUNNING, model_->items()[index].status);
191 aura::Window* root_window = window->GetRootWindow();
192 aura::Window* default_container = Shell::GetContainer(
194 kShellWindowId_DefaultContainer);
195 EXPECT_EQ(default_container, window->parent());
197 aura::Window* new_parent = Shell::GetContainer(
199 kShellWindowId_PanelContainer);
201 // Check |window|'s item is removed when it is re-parented to |new_parent|
202 // which is not default container.
203 new_parent->AddChild(window.get());
204 EXPECT_EQ(1, model_->item_count());
206 // Check |window|'s item is added when it is re-parented to
207 // |default_container|.
208 default_container->AddChild(window.get());
209 EXPECT_EQ(2, model_->item_count());
212 // Check |window|'s item is not changed during the dragging.
213 // TODO(simonhong): Add a test for removing a Window during the dragging.
214 TEST_F(ShelfWindowWatcherTest, DragWindow) {
215 // ShelfModel only has an APP_LIST item.
216 EXPECT_EQ(1, model_->item_count());
218 scoped_ptr<aura::Window> window(CreateTestWindowInShellWithId(0));
220 // Create a ShelfItem for |window|.
221 ShelfID id = CreateShelfItem(window.get());
222 EXPECT_EQ(2, model_->item_count());
224 int index = model_->ItemIndexByID(id);
225 EXPECT_EQ(STATUS_RUNNING, model_->items()[index].status);
227 // Simulate dragging of |window| and check its item is not changed.
228 scoped_ptr<WindowResizer> resizer(
229 CreateWindowResizer(window.get(),
232 aura::client::WINDOW_MOVE_SOURCE_MOUSE));
233 ASSERT_TRUE(resizer.get());
234 resizer->Drag(gfx::Point(50, 50), 0);
235 resizer->CompleteDrag();
237 //Index and id are not changed after dragging a |window|.
238 EXPECT_EQ(index, model_->ItemIndexByID(id));
239 EXPECT_EQ(id, model_->items()[index].id);
242 // Check |window|'s item is removed when it is re-parented not to default
243 // container during the dragging.
244 TEST_F(ShelfWindowWatcherTest, ReparentWindowDuringTheDragging) {
245 // ShelfModel only has an APP_LIST item.
246 EXPECT_EQ(1, model_->item_count());
248 scoped_ptr<aura::Window> window(CreateTestWindowInShellWithId(0));
249 window->set_owned_by_parent(false);
251 // Create a ShelfItem for |window|.
252 ShelfID id = CreateShelfItem(window.get());
253 EXPECT_EQ(2, model_->item_count());
254 int index = model_->ItemIndexByID(id);
255 EXPECT_EQ(STATUS_RUNNING, model_->items()[index].status);
257 aura::Window* root_window = window->GetRootWindow();
258 aura::Window* default_container = Shell::GetContainer(
260 kShellWindowId_DefaultContainer);
261 EXPECT_EQ(default_container, window->parent());
263 aura::Window* new_parent = Shell::GetContainer(
265 kShellWindowId_PanelContainer);
267 // Simulate re-parenting to |new_parent| during the dragging.
269 scoped_ptr<WindowResizer> resizer(
270 CreateWindowResizer(window.get(),
273 aura::client::WINDOW_MOVE_SOURCE_MOUSE));
274 ASSERT_TRUE(resizer.get());
275 resizer->Drag(gfx::Point(50, 50), 0);
276 resizer->CompleteDrag();
277 EXPECT_EQ(2, model_->item_count());
279 // Item should be removed when |window| is re-parented not to default
280 // container before fininshing the dragging.
281 EXPECT_TRUE(wm::GetWindowState(window.get())->is_dragged());
282 new_parent->AddChild(window.get());
283 EXPECT_EQ(1, model_->item_count());
285 EXPECT_FALSE(wm::GetWindowState(window.get())->is_dragged());
286 EXPECT_EQ(1, model_->item_count());
289 } // namespace internal