| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/shelf/shelf_window_watcher.h" | 5 #include "ash/shelf/shelf_window_watcher.h" |
| 6 | 6 |
| 7 #include "ash/aura/wm_window_aura.h" | 7 #include "ash/aura/wm_window_aura.h" |
| 8 #include "ash/common/ash_switches.h" | 8 #include "ash/common/ash_switches.h" |
| 9 #include "ash/common/shelf/shelf_item_types.h" | 9 #include "ash/common/shelf/shelf_item_types.h" |
| 10 #include "ash/common/shelf/shelf_model.h" | 10 #include "ash/common/shelf/shelf_model.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 } | 40 } |
| 41 | 41 |
| 42 ShelfID CreateShelfItem(aura::Window* window) { | 42 ShelfID CreateShelfItem(aura::Window* window) { |
| 43 ShelfID id = model_->next_id(); | 43 ShelfID id = model_->next_id(); |
| 44 ShelfItemDetails item_details; | 44 ShelfItemDetails item_details; |
| 45 item_details.type = TYPE_PLATFORM_APP; | 45 item_details.type = TYPE_PLATFORM_APP; |
| 46 SetShelfItemDetailsForWindow(window, item_details); | 46 SetShelfItemDetailsForWindow(window, item_details); |
| 47 return id; | 47 return id; |
| 48 } | 48 } |
| 49 | 49 |
| 50 // Creates a window with ShelfItemDetails and adds it to the default window |
| 51 // container. |
| 52 std::unique_ptr<aura::Window> CreateWindowWithShelfItemDetails() { |
| 53 std::unique_ptr<aura::Window> window(new aura::Window(nullptr)); |
| 54 window->SetType(ui::wm::WINDOW_TYPE_NORMAL); |
| 55 window->Init(ui::LAYER_TEXTURED); |
| 56 window->Show(); |
| 57 |
| 58 CreateShelfItem(window.get()); |
| 59 |
| 60 ParentWindowInPrimaryRootWindow(window.get()); |
| 61 return window; |
| 62 } |
| 63 |
| 50 protected: | 64 protected: |
| 51 ShelfModel* model_; | 65 ShelfModel* model_; |
| 52 | 66 |
| 53 private: | 67 private: |
| 54 DISALLOW_COPY_AND_ASSIGN(ShelfWindowWatcherTest); | 68 DISALLOW_COPY_AND_ASSIGN(ShelfWindowWatcherTest); |
| 55 }; | 69 }; |
| 56 | 70 |
| 57 TEST_F(ShelfWindowWatcherTest, CreateAndRemoveShelfItem) { | 71 // Tests that shelf items are added and removed as windows are opened and |
| 72 // closed. |
| 73 TEST_F(ShelfWindowWatcherTest, OpenAndClose) { |
| 58 // ShelfModel only has an APP_LIST item. | 74 // ShelfModel only has an APP_LIST item. |
| 59 EXPECT_EQ(1, model_->item_count()); | 75 EXPECT_EQ(1, model_->item_count()); |
| 60 | 76 |
| 77 // Adding windows with ShelfItemDetails properties adds shelf items. |
| 78 std::unique_ptr<aura::Window> w1(CreateWindowWithShelfItemDetails()); |
| 79 EXPECT_EQ(2, model_->item_count()); |
| 80 std::unique_ptr<aura::Window> w2(CreateWindowWithShelfItemDetails()); |
| 81 EXPECT_EQ(3, model_->item_count()); |
| 82 |
| 83 // Each ShelfItem is removed when the associated window is destroyed. |
| 84 w1.reset(); |
| 85 EXPECT_EQ(2, model_->item_count()); |
| 86 w2.reset(); |
| 87 EXPECT_EQ(1, model_->item_count()); |
| 88 } |
| 89 |
| 90 TEST_F(ShelfWindowWatcherTest, CreateAndRemoveShelfItemDetails) { |
| 91 // ShelfModel only has an APP_LIST item. |
| 92 EXPECT_EQ(1, model_->item_count()); |
| 93 |
| 94 // Creating windows without ShelfItemDetails does not add items. |
| 61 std::unique_ptr<aura::Window> w1(CreateTestWindowInShellWithId(0)); | 95 std::unique_ptr<aura::Window> w1(CreateTestWindowInShellWithId(0)); |
| 62 std::unique_ptr<aura::Window> w2(CreateTestWindowInShellWithId(0)); | 96 std::unique_ptr<aura::Window> w2(CreateTestWindowInShellWithId(0)); |
| 97 EXPECT_EQ(1, model_->item_count()); |
| 63 | 98 |
| 64 // Create a ShelfItem for w1. | 99 // Create a ShelfItem for w1. |
| 65 ShelfID id_w1 = CreateShelfItem(w1.get()); | 100 ShelfID id_w1 = CreateShelfItem(w1.get()); |
| 66 EXPECT_EQ(2, model_->item_count()); | 101 EXPECT_EQ(2, model_->item_count()); |
| 67 | 102 |
| 68 int index_w1 = model_->ItemIndexByID(id_w1); | 103 int index_w1 = model_->ItemIndexByID(id_w1); |
| 69 EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w1].status); | 104 EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w1].status); |
| 70 | 105 |
| 71 // Create a ShelfItem for w2. | 106 // Create a ShelfItem for w2. |
| 72 ShelfID id_w2 = CreateShelfItem(w2.get()); | 107 ShelfID id_w2 = CreateShelfItem(w2.get()); |
| 73 EXPECT_EQ(3, model_->item_count()); | 108 EXPECT_EQ(3, model_->item_count()); |
| 74 | 109 |
| 75 int index_w2 = model_->ItemIndexByID(id_w2); | 110 int index_w2 = model_->ItemIndexByID(id_w2); |
| 76 EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w2].status); | 111 EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w2].status); |
| 77 | 112 |
| 78 // ShelfItem is removed when assoicated window is destroyed. | 113 // ShelfItem is removed when its window property is cleared. |
| 79 ClearShelfItemDetailsForWindow(w1.get()); | 114 ClearShelfItemDetailsForWindow(w1.get()); |
| 80 EXPECT_EQ(2, model_->item_count()); | 115 EXPECT_EQ(2, model_->item_count()); |
| 81 ClearShelfItemDetailsForWindow(w2.get()); | 116 ClearShelfItemDetailsForWindow(w2.get()); |
| 82 EXPECT_EQ(1, model_->item_count()); | 117 EXPECT_EQ(1, model_->item_count()); |
| 83 // Clears twice doesn't do anything. | 118 // Clearing twice doesn't do anything. |
| 84 ClearShelfItemDetailsForWindow(w2.get()); | 119 ClearShelfItemDetailsForWindow(w2.get()); |
| 85 EXPECT_EQ(1, model_->item_count()); | 120 EXPECT_EQ(1, model_->item_count()); |
| 86 } | 121 } |
| 87 | 122 |
| 88 TEST_F(ShelfWindowWatcherTest, ActivateWindow) { | 123 TEST_F(ShelfWindowWatcherTest, ActivateWindow) { |
| 89 // ShelfModel only have APP_LIST item. | 124 // ShelfModel only have APP_LIST item. |
| 90 EXPECT_EQ(1, model_->item_count()); | 125 EXPECT_EQ(1, model_->item_count()); |
| 91 std::unique_ptr<aura::Window> w1(CreateTestWindowInShellWithId(0)); | 126 std::unique_ptr<aura::Window> w1(CreateTestWindowInShellWithId(0)); |
| 92 std::unique_ptr<aura::Window> w2(CreateTestWindowInShellWithId(0)); | 127 std::unique_ptr<aura::Window> w2(CreateTestWindowInShellWithId(0)); |
| 93 | 128 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 // Restore window |window|. | 201 // Restore window |window|. |
| 167 window_state->Restore(); | 202 window_state->Restore(); |
| 168 EXPECT_FALSE(window_state->IsMaximized()); | 203 EXPECT_FALSE(window_state->IsMaximized()); |
| 169 // No new item is created after restoring a window |window|. | 204 // No new item is created after restoring a window |window|. |
| 170 EXPECT_EQ(2, model_->item_count()); | 205 EXPECT_EQ(2, model_->item_count()); |
| 171 // Index and id are not changed after maximizing a window |window|. | 206 // Index and id are not changed after maximizing a window |window|. |
| 172 EXPECT_EQ(index, model_->ItemIndexByID(id)); | 207 EXPECT_EQ(index, model_->ItemIndexByID(id)); |
| 173 EXPECT_EQ(id, model_->items()[index].id); | 208 EXPECT_EQ(id, model_->items()[index].id); |
| 174 } | 209 } |
| 175 | 210 |
| 176 // Check that an item is removed when its associated Window is re-parented. | 211 // Check that an item is maintained when its associated Window is docked. |
| 177 TEST_F(ShelfWindowWatcherTest, ReparentWindow) { | 212 TEST_F(ShelfWindowWatcherTest, DockWindow) { |
| 178 // ShelfModel only has an APP_LIST item. | 213 // ShelfModel only has an APP_LIST item. |
| 179 EXPECT_EQ(1, model_->item_count()); | 214 EXPECT_EQ(1, model_->item_count()); |
| 180 | 215 |
| 181 std::unique_ptr<aura::Window> window(CreateTestWindowInShellWithId(0)); | 216 std::unique_ptr<aura::Window> window(CreateTestWindowInShellWithId(0)); |
| 182 window->set_owned_by_parent(false); | 217 window->set_owned_by_parent(false); |
| 183 | 218 |
| 184 // Create a ShelfItem for |window|. | 219 // Create a ShelfItem for |window|. |
| 185 ShelfID id = CreateShelfItem(window.get()); | 220 ShelfID id = CreateShelfItem(window.get()); |
| 186 EXPECT_EQ(2, model_->item_count()); | 221 EXPECT_EQ(2, model_->item_count()); |
| 187 | 222 |
| 188 int index = model_->ItemIndexByID(id); | 223 int index = model_->ItemIndexByID(id); |
| 189 EXPECT_EQ(STATUS_RUNNING, model_->items()[index].status); | 224 EXPECT_EQ(STATUS_RUNNING, model_->items()[index].status); |
| 190 | 225 |
| 191 aura::Window* root_window = window->GetRootWindow(); | 226 aura::Window* root_window = window->GetRootWindow(); |
| 192 aura::Window* default_container = | 227 aura::Window* default_container = |
| 193 Shell::GetContainer(root_window, kShellWindowId_DefaultContainer); | 228 Shell::GetContainer(root_window, kShellWindowId_DefaultContainer); |
| 194 EXPECT_EQ(default_container, window->parent()); | 229 EXPECT_EQ(default_container, window->parent()); |
| 195 | 230 |
| 196 aura::Window* new_parent = | 231 aura::Window* docked_container = |
| 197 Shell::GetContainer(root_window, kShellWindowId_PanelContainer); | 232 Shell::GetContainer(root_window, kShellWindowId_DockedContainer); |
| 198 | 233 |
| 199 // Check |window|'s item is removed when it is re-parented to |new_parent| | 234 // Check |window|'s item is not removed when it is re-parented to the dock. |
| 200 // which is not default container. | 235 docked_container->AddChild(window.get()); |
| 201 new_parent->AddChild(window.get()); | 236 EXPECT_EQ(2, model_->item_count()); |
| 237 |
| 238 // The shelf item is removed when the window is closed, even if it is in the |
| 239 // docked container at the time. |
| 240 window.reset(); |
| 202 EXPECT_EQ(1, model_->item_count()); | 241 EXPECT_EQ(1, model_->item_count()); |
| 203 | |
| 204 // Check |window|'s item is added when it is re-parented to | |
| 205 // |default_container|. | |
| 206 default_container->AddChild(window.get()); | |
| 207 EXPECT_EQ(2, model_->item_count()); | |
| 208 } | 242 } |
| 209 | 243 |
| 210 // Check |window|'s item is not changed during the dragging. | 244 // Check |window|'s item is not changed during the dragging. |
| 211 // TODO(simonhong): Add a test for removing a Window during the dragging. | 245 // TODO(simonhong): Add a test for removing a Window during the dragging. |
| 212 TEST_F(ShelfWindowWatcherTest, DragWindow) { | 246 TEST_F(ShelfWindowWatcherTest, DragWindow) { |
| 213 // ShelfModel only has an APP_LIST item. | 247 // ShelfModel only has an APP_LIST item. |
| 214 EXPECT_EQ(1, model_->item_count()); | 248 EXPECT_EQ(1, model_->item_count()); |
| 215 | 249 |
| 216 std::unique_ptr<aura::Window> window(CreateTestWindowInShellWithId(0)); | 250 std::unique_ptr<aura::Window> window(CreateTestWindowInShellWithId(0)); |
| 217 | 251 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 228 HTCAPTION, aura::client::WINDOW_MOVE_SOURCE_MOUSE)); | 262 HTCAPTION, aura::client::WINDOW_MOVE_SOURCE_MOUSE)); |
| 229 ASSERT_TRUE(resizer.get()); | 263 ASSERT_TRUE(resizer.get()); |
| 230 resizer->Drag(gfx::Point(50, 50), 0); | 264 resizer->Drag(gfx::Point(50, 50), 0); |
| 231 resizer->CompleteDrag(); | 265 resizer->CompleteDrag(); |
| 232 | 266 |
| 233 // Index and id are not changed after dragging a |window|. | 267 // Index and id are not changed after dragging a |window|. |
| 234 EXPECT_EQ(index, model_->ItemIndexByID(id)); | 268 EXPECT_EQ(index, model_->ItemIndexByID(id)); |
| 235 EXPECT_EQ(id, model_->items()[index].id); | 269 EXPECT_EQ(id, model_->items()[index].id); |
| 236 } | 270 } |
| 237 | 271 |
| 238 // Check |window|'s item is removed when it is re-parented not to default | |
| 239 // container during the dragging. | |
| 240 TEST_F(ShelfWindowWatcherTest, ReparentWindowDuringTheDragging) { | |
| 241 // ShelfModel only has an APP_LIST item. | |
| 242 EXPECT_EQ(1, model_->item_count()); | |
| 243 | |
| 244 std::unique_ptr<aura::Window> window(CreateTestWindowInShellWithId(0)); | |
| 245 window->set_owned_by_parent(false); | |
| 246 | |
| 247 // Create a ShelfItem for |window|. | |
| 248 ShelfID id = CreateShelfItem(window.get()); | |
| 249 EXPECT_EQ(2, model_->item_count()); | |
| 250 int index = model_->ItemIndexByID(id); | |
| 251 EXPECT_EQ(STATUS_RUNNING, model_->items()[index].status); | |
| 252 | |
| 253 aura::Window* root_window = window->GetRootWindow(); | |
| 254 aura::Window* default_container = | |
| 255 Shell::GetContainer(root_window, kShellWindowId_DefaultContainer); | |
| 256 EXPECT_EQ(default_container, window->parent()); | |
| 257 | |
| 258 aura::Window* new_parent = | |
| 259 Shell::GetContainer(root_window, kShellWindowId_PanelContainer); | |
| 260 | |
| 261 // Simulate re-parenting to |new_parent| during the dragging. | |
| 262 { | |
| 263 std::unique_ptr<WindowResizer> resizer( | |
| 264 CreateWindowResizer(WmWindowAura::Get(window.get()), gfx::Point(), | |
| 265 HTCAPTION, aura::client::WINDOW_MOVE_SOURCE_MOUSE)); | |
| 266 ASSERT_TRUE(resizer.get()); | |
| 267 resizer->Drag(gfx::Point(50, 50), 0); | |
| 268 resizer->CompleteDrag(); | |
| 269 EXPECT_EQ(2, model_->item_count()); | |
| 270 | |
| 271 // Item should be removed when |window| is re-parented not to default | |
| 272 // container before fininshing the dragging. | |
| 273 EXPECT_TRUE(wm::GetWindowState(window.get())->is_dragged()); | |
| 274 new_parent->AddChild(window.get()); | |
| 275 EXPECT_EQ(1, model_->item_count()); | |
| 276 } | |
| 277 EXPECT_FALSE(wm::GetWindowState(window.get())->is_dragged()); | |
| 278 EXPECT_EQ(1, model_->item_count()); | |
| 279 } | |
| 280 | |
| 281 } // namespace ash | 272 } // namespace ash |
| OLD | NEW |