Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ash / shelf / shelf_window_watcher_unittest.cc
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.
4
5 #include "ash/shelf/shelf_window_watcher.h"
6
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"
22
23 namespace ash {
24 namespace internal {
25
26 class ShelfWindowWatcherTest : public test::AshTestBase {
27  public:
28   ShelfWindowWatcherTest() : model_(NULL) {}
29   virtual ~ShelfWindowWatcherTest() {}
30
31   virtual void SetUp() OVERRIDE {
32     test::AshTestBase::SetUp();
33     model_ = test::ShellTestApi(Shell::GetInstance()).shelf_model();
34   }
35
36   virtual void TearDown() OVERRIDE {
37     model_ = NULL;
38     test::AshTestBase::TearDown();
39   }
40
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);
46     return id;
47   }
48
49  protected:
50   ShelfModel* model_;
51
52  private:
53   DISALLOW_COPY_AND_ASSIGN(ShelfWindowWatcherTest);
54 };
55
56 TEST_F(ShelfWindowWatcherTest, CreateAndRemoveShelfItem) {
57   // ShelfModel only has an APP_LIST item.
58   EXPECT_EQ(1, model_->item_count());
59
60   scoped_ptr<aura::Window> w1(CreateTestWindowInShellWithId(0));
61   scoped_ptr<aura::Window> w2(CreateTestWindowInShellWithId(0));
62
63   // Create a ShelfItem for w1.
64   ShelfID id_w1 = CreateShelfItem(w1.get());
65   EXPECT_EQ(2, model_->item_count());
66
67   int index_w1 = model_->ItemIndexByID(id_w1);
68   EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w1].status);
69
70   // Create a ShelfItem for w2.
71   ShelfID id_w2 = CreateShelfItem(w2.get());
72   EXPECT_EQ(3, model_->item_count());
73
74   int index_w2 = model_->ItemIndexByID(id_w2);
75   EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w2].status);
76
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());
85
86 }
87
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));
93
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);
99
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);
106
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);
110
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);
115 }
116
117 TEST_F(ShelfWindowWatcherTest, UpdateWindowProperty) {
118   // ShelfModel only has an APP_LIST item.
119   EXPECT_EQ(1, model_->item_count());
120
121   scoped_ptr<aura::Window> window(CreateTestWindowInShellWithId(0));
122
123   // Create a ShelfItem for |window|.
124   ShelfID id = CreateShelfItem(window.get());
125   EXPECT_EQ(2, model_->item_count());
126
127   int index = model_->ItemIndexByID(id);
128   EXPECT_EQ(STATUS_RUNNING, model_->items()[index].status);
129
130   // Update ShelfItem for |window|.
131   ShelfItemDetails details;
132   details.type = TYPE_PLATFORM_APP;
133
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);
140 }
141
142 TEST_F(ShelfWindowWatcherTest, MaximizeAndRestoreWindow) {
143   // ShelfModel only has an APP_LIST item.
144   EXPECT_EQ(1, model_->item_count());
145
146   scoped_ptr<aura::Window> window(CreateTestWindowInShellWithId(0));
147   wm::WindowState* window_state = wm::GetWindowState(window.get());
148
149   // Create a ShelfItem for |window|.
150   ShelfID id = CreateShelfItem(window.get());
151   EXPECT_EQ(2, model_->item_count());
152
153   int index = model_->ItemIndexByID(id);
154   EXPECT_EQ(STATUS_RUNNING, model_->items()[index].status);
155
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);
165
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);
174 }
175
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());
180
181   scoped_ptr<aura::Window> window(CreateTestWindowInShellWithId(0));
182   window->set_owned_by_parent(false);
183
184   // Create a ShelfItem for |window|.
185   ShelfID id = CreateShelfItem(window.get());
186   EXPECT_EQ(2, model_->item_count());
187
188   int index = model_->ItemIndexByID(id);
189   EXPECT_EQ(STATUS_RUNNING, model_->items()[index].status);
190
191   aura::Window* root_window = window->GetRootWindow();
192   aura::Window* default_container = Shell::GetContainer(
193       root_window,
194       kShellWindowId_DefaultContainer);
195   EXPECT_EQ(default_container, window->parent());
196
197   aura::Window* new_parent = Shell::GetContainer(
198       root_window,
199       kShellWindowId_PanelContainer);
200
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());
205
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());
210 }
211
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());
217
218   scoped_ptr<aura::Window> window(CreateTestWindowInShellWithId(0));
219
220   // Create a ShelfItem for |window|.
221   ShelfID id = CreateShelfItem(window.get());
222   EXPECT_EQ(2, model_->item_count());
223
224   int index = model_->ItemIndexByID(id);
225   EXPECT_EQ(STATUS_RUNNING, model_->items()[index].status);
226
227   // Simulate dragging of |window| and check its item is not changed.
228   scoped_ptr<WindowResizer> resizer(
229       CreateWindowResizer(window.get(),
230                           gfx::Point(),
231                           HTCAPTION,
232                           aura::client::WINDOW_MOVE_SOURCE_MOUSE));
233   ASSERT_TRUE(resizer.get());
234   resizer->Drag(gfx::Point(50, 50), 0);
235   resizer->CompleteDrag();
236
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);
240 }
241
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());
247
248   scoped_ptr<aura::Window> window(CreateTestWindowInShellWithId(0));
249   window->set_owned_by_parent(false);
250
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);
256
257   aura::Window* root_window = window->GetRootWindow();
258   aura::Window* default_container = Shell::GetContainer(
259       root_window,
260       kShellWindowId_DefaultContainer);
261   EXPECT_EQ(default_container, window->parent());
262
263   aura::Window* new_parent = Shell::GetContainer(
264       root_window,
265       kShellWindowId_PanelContainer);
266
267   // Simulate re-parenting to |new_parent| during the dragging.
268   {
269     scoped_ptr<WindowResizer> resizer(
270         CreateWindowResizer(window.get(),
271                             gfx::Point(),
272                             HTCAPTION,
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());
278
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());
284   }
285   EXPECT_FALSE(wm::GetWindowState(window.get())->is_dragged());
286   EXPECT_EQ(1, model_->item_count());
287 }
288
289 }  // namespace internal
290 }  // namespace ash