| OLD | NEW |
| (Empty) |
| 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/aura/wm_window_aura.h" | |
| 8 #include "ash/common/ash_switches.h" | |
| 9 #include "ash/common/shelf/shelf_item_types.h" | |
| 10 #include "ash/common/shelf/shelf_model.h" | |
| 11 #include "ash/common/shell_window_ids.h" | |
| 12 #include "ash/common/wm/window_resizer.h" | |
| 13 #include "ash/common/wm/window_state.h" | |
| 14 #include "ash/common/wm_shell.h" | |
| 15 #include "ash/shelf/shelf_util.h" | |
| 16 #include "ash/shell.h" | |
| 17 #include "ash/test/ash_test_base.h" | |
| 18 #include "ash/wm/window_state_aura.h" | |
| 19 #include "ash/wm/window_util.h" | |
| 20 #include "base/command_line.h" | |
| 21 #include "ui/aura/client/aura_constants.h" | |
| 22 #include "ui/aura/window.h" | |
| 23 #include "ui/base/hit_test.h" | |
| 24 | |
| 25 namespace ash { | |
| 26 | |
| 27 class ShelfWindowWatcherTest : public test::AshTestBase { | |
| 28 public: | |
| 29 ShelfWindowWatcherTest() : model_(NULL) {} | |
| 30 ~ShelfWindowWatcherTest() override {} | |
| 31 | |
| 32 void SetUp() override { | |
| 33 test::AshTestBase::SetUp(); | |
| 34 model_ = WmShell::Get()->shelf_model(); | |
| 35 } | |
| 36 | |
| 37 void TearDown() override { | |
| 38 model_ = NULL; | |
| 39 test::AshTestBase::TearDown(); | |
| 40 } | |
| 41 | |
| 42 ShelfID CreateShelfItem(aura::Window* window) { | |
| 43 ShelfID id = model_->next_id(); | |
| 44 ShelfItemDetails item_details; | |
| 45 item_details.type = TYPE_PLATFORM_APP; | |
| 46 SetShelfItemDetailsForWindow(window, item_details); | |
| 47 return id; | |
| 48 } | |
| 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 | |
| 64 protected: | |
| 65 ShelfModel* model_; | |
| 66 | |
| 67 private: | |
| 68 DISALLOW_COPY_AND_ASSIGN(ShelfWindowWatcherTest); | |
| 69 }; | |
| 70 | |
| 71 // Tests that shelf items are added and removed as windows are opened and | |
| 72 // closed. | |
| 73 TEST_F(ShelfWindowWatcherTest, OpenAndClose) { | |
| 74 // ShelfModel only has an APP_LIST item. | |
| 75 EXPECT_EQ(1, model_->item_count()); | |
| 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. | |
| 95 std::unique_ptr<aura::Window> w1(CreateTestWindowInShellWithId(0)); | |
| 96 std::unique_ptr<aura::Window> w2(CreateTestWindowInShellWithId(0)); | |
| 97 EXPECT_EQ(1, model_->item_count()); | |
| 98 | |
| 99 // Create a ShelfItem for w1. | |
| 100 ShelfID id_w1 = CreateShelfItem(w1.get()); | |
| 101 EXPECT_EQ(2, model_->item_count()); | |
| 102 | |
| 103 int index_w1 = model_->ItemIndexByID(id_w1); | |
| 104 EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w1].status); | |
| 105 | |
| 106 // Create a ShelfItem for w2. | |
| 107 ShelfID id_w2 = CreateShelfItem(w2.get()); | |
| 108 EXPECT_EQ(3, model_->item_count()); | |
| 109 | |
| 110 int index_w2 = model_->ItemIndexByID(id_w2); | |
| 111 EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w2].status); | |
| 112 | |
| 113 // ShelfItem is removed when its window property is cleared. | |
| 114 ClearShelfItemDetailsForWindow(w1.get()); | |
| 115 EXPECT_EQ(2, model_->item_count()); | |
| 116 ClearShelfItemDetailsForWindow(w2.get()); | |
| 117 EXPECT_EQ(1, model_->item_count()); | |
| 118 // Clearing twice doesn't do anything. | |
| 119 ClearShelfItemDetailsForWindow(w2.get()); | |
| 120 EXPECT_EQ(1, model_->item_count()); | |
| 121 } | |
| 122 | |
| 123 TEST_F(ShelfWindowWatcherTest, ActivateWindow) { | |
| 124 // ShelfModel only have APP_LIST item. | |
| 125 EXPECT_EQ(1, model_->item_count()); | |
| 126 std::unique_ptr<aura::Window> w1(CreateTestWindowInShellWithId(0)); | |
| 127 std::unique_ptr<aura::Window> w2(CreateTestWindowInShellWithId(0)); | |
| 128 | |
| 129 // Create a ShelfItem for w1. | |
| 130 ShelfID id_w1 = CreateShelfItem(w1.get()); | |
| 131 EXPECT_EQ(2, model_->item_count()); | |
| 132 int index_w1 = model_->ItemIndexByID(id_w1); | |
| 133 EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w1].status); | |
| 134 | |
| 135 // Create a ShelfItem for w2. | |
| 136 ShelfID id_w2 = CreateShelfItem(w2.get()); | |
| 137 EXPECT_EQ(3, model_->item_count()); | |
| 138 int index_w2 = model_->ItemIndexByID(id_w2); | |
| 139 EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w1].status); | |
| 140 EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w2].status); | |
| 141 | |
| 142 // ShelfItem for w1 is active when w1 is activated. | |
| 143 wm::ActivateWindow(w1.get()); | |
| 144 EXPECT_EQ(STATUS_ACTIVE, model_->items()[index_w1].status); | |
| 145 | |
| 146 // ShelfItem for w2 is active state when w2 is activated. | |
| 147 wm::ActivateWindow(w2.get()); | |
| 148 EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w1].status); | |
| 149 EXPECT_EQ(STATUS_ACTIVE, model_->items()[index_w2].status); | |
| 150 } | |
| 151 | |
| 152 TEST_F(ShelfWindowWatcherTest, UpdateWindowProperty) { | |
| 153 // ShelfModel only has an APP_LIST item. | |
| 154 EXPECT_EQ(1, model_->item_count()); | |
| 155 | |
| 156 std::unique_ptr<aura::Window> window(CreateTestWindowInShellWithId(0)); | |
| 157 | |
| 158 // Create a ShelfItem for |window|. | |
| 159 ShelfID id = CreateShelfItem(window.get()); | |
| 160 EXPECT_EQ(2, model_->item_count()); | |
| 161 | |
| 162 int index = model_->ItemIndexByID(id); | |
| 163 EXPECT_EQ(STATUS_RUNNING, model_->items()[index].status); | |
| 164 | |
| 165 // Update ShelfItem for |window|. | |
| 166 ShelfItemDetails details; | |
| 167 details.type = TYPE_PLATFORM_APP; | |
| 168 | |
| 169 SetShelfItemDetailsForWindow(window.get(), details); | |
| 170 // No new item is created after updating a launcher item. | |
| 171 EXPECT_EQ(2, model_->item_count()); | |
| 172 // index and id are not changed after updating a launcher item. | |
| 173 EXPECT_EQ(index, model_->ItemIndexByID(id)); | |
| 174 EXPECT_EQ(id, model_->items()[index].id); | |
| 175 } | |
| 176 | |
| 177 TEST_F(ShelfWindowWatcherTest, MaximizeAndRestoreWindow) { | |
| 178 // ShelfModel only has an APP_LIST item. | |
| 179 EXPECT_EQ(1, model_->item_count()); | |
| 180 | |
| 181 std::unique_ptr<aura::Window> window(CreateTestWindowInShellWithId(0)); | |
| 182 wm::WindowState* window_state = wm::GetWindowState(window.get()); | |
| 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 // Maximize window |window|. | |
| 192 EXPECT_FALSE(window_state->IsMaximized()); | |
| 193 window_state->Maximize(); | |
| 194 EXPECT_TRUE(window_state->IsMaximized()); | |
| 195 // No new item is created after maximizing a window |window|. | |
| 196 EXPECT_EQ(2, model_->item_count()); | |
| 197 // index and id are not changed after maximizing a window |window|. | |
| 198 EXPECT_EQ(index, model_->ItemIndexByID(id)); | |
| 199 EXPECT_EQ(id, model_->items()[index].id); | |
| 200 | |
| 201 // Restore window |window|. | |
| 202 window_state->Restore(); | |
| 203 EXPECT_FALSE(window_state->IsMaximized()); | |
| 204 // No new item is created after restoring a window |window|. | |
| 205 EXPECT_EQ(2, model_->item_count()); | |
| 206 // Index and id are not changed after maximizing a window |window|. | |
| 207 EXPECT_EQ(index, model_->ItemIndexByID(id)); | |
| 208 EXPECT_EQ(id, model_->items()[index].id); | |
| 209 } | |
| 210 | |
| 211 // Check that an item is maintained when its associated Window is docked. | |
| 212 TEST_F(ShelfWindowWatcherTest, DockWindow) { | |
| 213 // ShelfModel only has an APP_LIST item. | |
| 214 EXPECT_EQ(1, model_->item_count()); | |
| 215 | |
| 216 std::unique_ptr<aura::Window> window(CreateTestWindowInShellWithId(0)); | |
| 217 window->set_owned_by_parent(false); | |
| 218 | |
| 219 // Create a ShelfItem for |window|. | |
| 220 ShelfID id = CreateShelfItem(window.get()); | |
| 221 EXPECT_EQ(2, model_->item_count()); | |
| 222 | |
| 223 int index = model_->ItemIndexByID(id); | |
| 224 EXPECT_EQ(STATUS_RUNNING, model_->items()[index].status); | |
| 225 | |
| 226 aura::Window* root_window = window->GetRootWindow(); | |
| 227 aura::Window* default_container = | |
| 228 Shell::GetContainer(root_window, kShellWindowId_DefaultContainer); | |
| 229 EXPECT_EQ(default_container, window->parent()); | |
| 230 | |
| 231 aura::Window* docked_container = | |
| 232 Shell::GetContainer(root_window, kShellWindowId_DockedContainer); | |
| 233 | |
| 234 // Check |window|'s item is not removed when it is re-parented to the dock. | |
| 235 docked_container->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(); | |
| 241 EXPECT_EQ(1, model_->item_count()); | |
| 242 } | |
| 243 | |
| 244 // Check |window|'s item is not changed during the dragging. | |
| 245 // TODO(simonhong): Add a test for removing a Window during the dragging. | |
| 246 TEST_F(ShelfWindowWatcherTest, DragWindow) { | |
| 247 // ShelfModel only has an APP_LIST item. | |
| 248 EXPECT_EQ(1, model_->item_count()); | |
| 249 | |
| 250 std::unique_ptr<aura::Window> window(CreateTestWindowInShellWithId(0)); | |
| 251 | |
| 252 // Create a ShelfItem for |window|. | |
| 253 ShelfID id = CreateShelfItem(window.get()); | |
| 254 EXPECT_EQ(2, model_->item_count()); | |
| 255 | |
| 256 int index = model_->ItemIndexByID(id); | |
| 257 EXPECT_EQ(STATUS_RUNNING, model_->items()[index].status); | |
| 258 | |
| 259 // Simulate dragging of |window| and check its item is not changed. | |
| 260 std::unique_ptr<WindowResizer> resizer( | |
| 261 CreateWindowResizer(WmWindowAura::Get(window.get()), gfx::Point(), | |
| 262 HTCAPTION, aura::client::WINDOW_MOVE_SOURCE_MOUSE)); | |
| 263 ASSERT_TRUE(resizer.get()); | |
| 264 resizer->Drag(gfx::Point(50, 50), 0); | |
| 265 resizer->CompleteDrag(); | |
| 266 | |
| 267 // Index and id are not changed after dragging a |window|. | |
| 268 EXPECT_EQ(index, model_->ItemIndexByID(id)); | |
| 269 EXPECT_EQ(id, model_->items()[index].id); | |
| 270 } | |
| 271 | |
| 272 } // namespace ash | |
| OLD | NEW |