blob: aff1f778354cc358c6a6f1675c93a103846f0182 [file] [log] [blame]
James Cookb0bf8e82017-04-09 17:01:441// 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
Mitsuru Oshima04b54d02017-10-09 14:22:457#include <memory>
8
skye5fd1222017-04-12 18:43:239#include "ash/public/cpp/config.h"
James Cookb0bf8e82017-04-09 17:01:4410#include "ash/public/cpp/shelf_item.h"
msw109806d2017-06-02 20:11:5711#include "ash/public/cpp/shelf_model.h"
James Cookb0bf8e82017-04-09 17:01:4412#include "ash/public/cpp/shell_window_ids.h"
13#include "ash/public/cpp/window_properties.h"
14#include "ash/root_window_controller.h"
15#include "ash/session/session_controller.h"
James Cookb0bf8e82017-04-09 17:01:4416#include "ash/shell.h"
17#include "ash/test/ash_test_base.h"
18#include "ash/wm/window_resizer.h"
19#include "ash/wm/window_state.h"
msw19b30c2c2017-06-01 03:21:4020#include "third_party/skia/include/core/SkBitmap.h"
21#include "ui/aura/client/aura_constants.h"
James Cookb0bf8e82017-04-09 17:01:4422#include "ui/aura/window.h"
23#include "ui/base/hit_test.h"
msw19b30c2c2017-06-01 03:21:4024#include "ui/base/resource/resource_bundle.h"
25#include "ui/gfx/image/image_skia.h"
26#include "ui/resources/grit/ui_resources.h"
James Cookb0bf8e82017-04-09 17:01:4427#include "ui/views/widget/widget.h"
msw70ac45f2017-06-05 02:02:4528#include "ui/wm/core/transient_window_controller.h"
James Cookb0bf8e82017-04-09 17:01:4429
30namespace ash {
msw19b30c2c2017-06-01 03:21:4031namespace {
32
33// Create a test 1x1 icon image with a given |color|.
34gfx::ImageSkia CreateImageSkiaIcon(SkColor color) {
35 SkBitmap bitmap;
36 bitmap.allocN32Pixels(1, 1);
37 bitmap.eraseColor(color);
38 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
39}
James Cookb0bf8e82017-04-09 17:01:4440
James Cook317781a2017-07-18 02:08:0641class ShelfWindowWatcherTest : public AshTestBase {
James Cookb0bf8e82017-04-09 17:01:4442 public:
43 ShelfWindowWatcherTest() : model_(nullptr) {}
Chris Watkinsc24daf62017-11-28 03:43:0944 ~ShelfWindowWatcherTest() override = default;
James Cookb0bf8e82017-04-09 17:01:4445
46 void SetUp() override {
James Cook317781a2017-07-18 02:08:0647 AshTestBase::SetUp();
James Cookb0bf8e82017-04-09 17:01:4448 model_ = Shell::Get()->shelf_model();
Mike Wasserman717cd572017-09-23 17:01:4049 // ShelfModel creates an app list item.
50 ASSERT_EQ(1, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:4451 }
52
53 void TearDown() override {
54 model_ = nullptr;
James Cook317781a2017-07-18 02:08:0655 AshTestBase::TearDown();
James Cookb0bf8e82017-04-09 17:01:4456 }
57
msw6958e7f2017-05-15 22:55:0658 static ShelfID CreateShelfItem(aura::Window* window) {
msw84b8a5f2017-05-05 00:13:3659 static int id = 0;
msw70ac45f2017-06-05 02:02:4560 ShelfID shelf_id(std::to_string(id++));
msw6958e7f2017-05-15 22:55:0661 window->SetProperty(kShelfIDKey, new std::string(shelf_id.Serialize()));
62 window->SetProperty(kShelfItemTypeKey, static_cast<int32_t>(TYPE_DIALOG));
msw84b8a5f2017-05-05 00:13:3663 return shelf_id;
James Cookb0bf8e82017-04-09 17:01:4464 }
65
66 protected:
67 ShelfModel* model_;
68
69 private:
70 DISALLOW_COPY_AND_ASSIGN(ShelfWindowWatcherTest);
71};
72
73// Ensure shelf items are added and removed as windows are opened and closed.
74TEST_F(ShelfWindowWatcherTest, OpenAndClose) {
msw70ac45f2017-06-05 02:02:4575 // Windows with valid ShelfItemType and ShelfID properties get shelf items.
James Cookb0bf8e82017-04-09 17:01:4476 std::unique_ptr<views::Widget> widget1 =
77 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
msw6958e7f2017-05-15 22:55:0678 CreateShelfItem(widget1->GetNativeWindow());
Mike Wasserman717cd572017-09-23 17:01:4079 EXPECT_EQ(2, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:4480 std::unique_ptr<views::Widget> widget2 =
81 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
msw6958e7f2017-05-15 22:55:0682 CreateShelfItem(widget2->GetNativeWindow());
Mike Wasserman717cd572017-09-23 17:01:4083 EXPECT_EQ(3, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:4484
85 // Each ShelfItem is removed when the associated window is destroyed.
86 widget1.reset();
Michael Wasserman78b6f3e2017-07-20 19:51:2087 EXPECT_EQ(2, model_->item_count());
Mike Wasserman717cd572017-09-23 17:01:4088 widget2.reset();
89 EXPECT_EQ(1, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:4490}
91
mswfeae4322017-06-08 04:24:2292// Ensure shelf items are added and removed for some unknown windows in mash.
msw70ac45f2017-06-05 02:02:4593TEST_F(ShelfWindowWatcherTest, OpenAndCloseMash) {
94 if (Shell::GetAshConfig() != Config::MASH)
95 return;
96
msw70ac45f2017-06-05 02:02:4597 // Windows with no valid ShelfItemType and ShelfID properties get shelf items.
98 std::unique_ptr<views::Widget> widget1 =
99 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
Mike Wasserman717cd572017-09-23 17:01:40100 EXPECT_EQ(2, model_->item_count());
msw70ac45f2017-06-05 02:02:45101 std::unique_ptr<views::Widget> widget2 =
102 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
Mike Wasserman717cd572017-09-23 17:01:40103 EXPECT_EQ(3, model_->item_count());
msw70ac45f2017-06-05 02:02:45104
105 // Each ShelfItem is removed when the associated window is destroyed.
106 widget1.reset();
Michael Wasserman78b6f3e2017-07-20 19:51:20107 EXPECT_EQ(2, model_->item_count());
Mike Wasserman717cd572017-09-23 17:01:40108 widget2.reset();
109 EXPECT_EQ(1, model_->item_count());
msw70ac45f2017-06-05 02:02:45110
mswfeae4322017-06-08 04:24:22111 // Windows with type WINDOW_TYPE_NORMAL get shelf items, others do not.
112 aura::client::WindowType no_item_types[] = {
113 aura::client::WINDOW_TYPE_UNKNOWN, aura::client::WINDOW_TYPE_NORMAL,
114 aura::client::WINDOW_TYPE_POPUP, aura::client::WINDOW_TYPE_CONTROL,
115 aura::client::WINDOW_TYPE_PANEL, aura::client::WINDOW_TYPE_MENU,
116 aura::client::WINDOW_TYPE_TOOLTIP};
117 for (aura::client::WindowType type : no_item_types) {
118 std::unique_ptr<aura::Window> window =
Mitsuru Oshima04b54d02017-10-09 14:22:45119 std::make_unique<aura::Window>(nullptr, type);
mswfeae4322017-06-08 04:24:22120 window->Init(ui::LAYER_NOT_DRAWN);
121 Shell::GetPrimaryRootWindow()
122 ->GetChildById(kShellWindowId_DefaultContainer)
123 ->AddChild(window.get());
124 window->Show();
Mike Wasserman717cd572017-09-23 17:01:40125 EXPECT_EQ(type == aura::client::WINDOW_TYPE_NORMAL ? 2 : 1,
mswfeae4322017-06-08 04:24:22126 model_->item_count());
127 }
128
msw70ac45f2017-06-05 02:02:45129 // Windows with WindowState::ignored_by_shelf set do not get shelf items.
130 widget1 =
131 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
132 wm::GetWindowState(widget1->GetNativeWindow())->set_ignored_by_shelf(true);
133 // TODO(msw): Make the flag a window property and remove this workaround.
134 widget1->GetNativeWindow()->SetProperty(aura::client::kDrawAttentionKey,
135 true);
Mike Wasserman717cd572017-09-23 17:01:40136 EXPECT_EQ(1, model_->item_count());
msw70ac45f2017-06-05 02:02:45137}
138
James Cookb0bf8e82017-04-09 17:01:44139TEST_F(ShelfWindowWatcherTest, CreateAndRemoveShelfItemProperties) {
140 // TODO: investigate failure in mash. http://crbug.com/695562.
skye5fd1222017-04-12 18:43:23141 if (Shell::GetAshConfig() == Config::MASH)
James Cookb0bf8e82017-04-09 17:01:44142 return;
143
James Cookb0bf8e82017-04-09 17:01:44144 // Creating windows without a valid ShelfItemType does not add items.
Mike Wasserman717cd572017-09-23 17:01:40145 EXPECT_EQ(1, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:44146 std::unique_ptr<views::Widget> widget1 =
147 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
James Cookb0bf8e82017-04-09 17:01:44148 std::unique_ptr<views::Widget> widget2 =
149 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
Mike Wasserman717cd572017-09-23 17:01:40150 EXPECT_EQ(1, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:44151
152 // Create a ShelfItem for the first window.
msw6958e7f2017-05-15 22:55:06153 ShelfID id_w1 = CreateShelfItem(widget1->GetNativeWindow());
Mike Wasserman717cd572017-09-23 17:01:40154 EXPECT_EQ(2, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:44155
156 int index_w1 = model_->ItemIndexByID(id_w1);
157 EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w1].status);
158
159 // Create a ShelfItem for the second window.
msw6958e7f2017-05-15 22:55:06160 ShelfID id_w2 = CreateShelfItem(widget2->GetNativeWindow());
Mike Wasserman717cd572017-09-23 17:01:40161 EXPECT_EQ(3, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:44162
163 int index_w2 = model_->ItemIndexByID(id_w2);
James Cook263e93e2017-11-08 17:37:24164 EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w2].status);
James Cookb0bf8e82017-04-09 17:01:44165
166 // ShelfItem is removed when the item type window property is cleared.
msw6958e7f2017-05-15 22:55:06167 widget1->GetNativeWindow()->SetProperty(kShelfItemTypeKey,
168 static_cast<int32_t>(TYPE_UNDEFINED));
Mike Wasserman717cd572017-09-23 17:01:40169 EXPECT_EQ(2, model_->item_count());
msw6958e7f2017-05-15 22:55:06170 widget2->GetNativeWindow()->SetProperty(kShelfItemTypeKey,
171 static_cast<int32_t>(TYPE_UNDEFINED));
Mike Wasserman717cd572017-09-23 17:01:40172 EXPECT_EQ(1, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:44173 // Clearing twice doesn't do anything.
msw6958e7f2017-05-15 22:55:06174 widget2->GetNativeWindow()->SetProperty(kShelfItemTypeKey,
175 static_cast<int32_t>(TYPE_UNDEFINED));
Mike Wasserman717cd572017-09-23 17:01:40176 EXPECT_EQ(1, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:44177}
178
James Cookb0bf8e82017-04-09 17:01:44179TEST_F(ShelfWindowWatcherTest, UpdateWindowProperty) {
Michael Wasserman78b6f3e2017-07-20 19:51:20180 // Create a ShelfItem for a new window.
Mike Wasserman717cd572017-09-23 17:01:40181 EXPECT_EQ(1, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:44182 std::unique_ptr<views::Widget> widget =
183 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
msw6958e7f2017-05-15 22:55:06184 ShelfID id = CreateShelfItem(widget->GetNativeWindow());
Mike Wasserman717cd572017-09-23 17:01:40185 EXPECT_EQ(2, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:44186
187 int index = model_->ItemIndexByID(id);
James Cook263e93e2017-11-08 17:37:24188 EXPECT_EQ(STATUS_RUNNING, model_->items()[index].status);
James Cookb0bf8e82017-04-09 17:01:44189
msw6958e7f2017-05-15 22:55:06190 // Update the window's ShelfItemType.
191 widget->GetNativeWindow()->SetProperty(kShelfItemTypeKey,
mswfeae4322017-06-08 04:24:22192 static_cast<int32_t>(TYPE_APP_PANEL));
James Cookb0bf8e82017-04-09 17:01:44193 // No new item is created after updating a launcher item.
Mike Wasserman717cd572017-09-23 17:01:40194 EXPECT_EQ(2, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:44195 // index and id are not changed after updating a launcher item.
196 EXPECT_EQ(index, model_->ItemIndexByID(id));
197 EXPECT_EQ(id, model_->items()[index].id);
198}
199
200TEST_F(ShelfWindowWatcherTest, MaximizeAndRestoreWindow) {
Michael Wasserman78b6f3e2017-07-20 19:51:20201 // Create a ShelfItem for a new window.
Mike Wasserman717cd572017-09-23 17:01:40202 EXPECT_EQ(1, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:44203 std::unique_ptr<views::Widget> widget =
204 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
msw6958e7f2017-05-15 22:55:06205 ShelfID id = CreateShelfItem(widget->GetNativeWindow());
Mike Wasserman717cd572017-09-23 17:01:40206 EXPECT_EQ(2, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:44207
208 int index = model_->ItemIndexByID(id);
James Cook263e93e2017-11-08 17:37:24209 EXPECT_EQ(STATUS_RUNNING, model_->items()[index].status);
James Cookb0bf8e82017-04-09 17:01:44210
msw6958e7f2017-05-15 22:55:06211 // Maximize the window.
Michael Wasserman78b6f3e2017-07-20 19:51:20212 wm::WindowState* window_state = wm::GetWindowState(widget->GetNativeWindow());
James Cookb0bf8e82017-04-09 17:01:44213 EXPECT_FALSE(window_state->IsMaximized());
214 window_state->Maximize();
215 EXPECT_TRUE(window_state->IsMaximized());
msw6958e7f2017-05-15 22:55:06216 // No new item is created after maximizing the window.
Mike Wasserman717cd572017-09-23 17:01:40217 EXPECT_EQ(2, model_->item_count());
msw6958e7f2017-05-15 22:55:06218 // index and id are not changed after maximizing the window.
James Cookb0bf8e82017-04-09 17:01:44219 EXPECT_EQ(index, model_->ItemIndexByID(id));
220 EXPECT_EQ(id, model_->items()[index].id);
221
msw6958e7f2017-05-15 22:55:06222 // Restore the window.
James Cookb0bf8e82017-04-09 17:01:44223 window_state->Restore();
224 EXPECT_FALSE(window_state->IsMaximized());
msw6958e7f2017-05-15 22:55:06225 // No new item is created after restoring the window.
Mike Wasserman717cd572017-09-23 17:01:40226 EXPECT_EQ(2, model_->item_count());
msw6958e7f2017-05-15 22:55:06227 // Index and id are not changed after maximizing the window.
James Cookb0bf8e82017-04-09 17:01:44228 EXPECT_EQ(index, model_->ItemIndexByID(id));
229 EXPECT_EQ(id, model_->items()[index].id);
230}
231
232// Check |window|'s item is not changed during the dragging.
233// TODO(simonhong): Add a test for removing a Window during the dragging.
234TEST_F(ShelfWindowWatcherTest, DragWindow) {
Michael Wasserman78b6f3e2017-07-20 19:51:20235 // Create a ShelfItem for a new window.
Mike Wasserman717cd572017-09-23 17:01:40236 EXPECT_EQ(1, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:44237 std::unique_ptr<views::Widget> widget =
238 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
msw6958e7f2017-05-15 22:55:06239 ShelfID id = CreateShelfItem(widget->GetNativeWindow());
Mike Wasserman717cd572017-09-23 17:01:40240 EXPECT_EQ(2, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:44241
242 int index = model_->ItemIndexByID(id);
James Cook263e93e2017-11-08 17:37:24243 EXPECT_EQ(STATUS_RUNNING, model_->items()[index].status);
James Cookb0bf8e82017-04-09 17:01:44244
msw6958e7f2017-05-15 22:55:06245 // Simulate dragging of the window and check its item is not changed.
skya5e9a122017-05-22 20:45:35246 std::unique_ptr<WindowResizer> resizer(
247 CreateWindowResizer(widget->GetNativeWindow(), gfx::Point(), HTCAPTION,
Thiago Farina3b086a02017-05-30 22:32:50248 ::wm::WINDOW_MOVE_SOURCE_MOUSE));
James Cookb0bf8e82017-04-09 17:01:44249 ASSERT_TRUE(resizer.get());
250 resizer->Drag(gfx::Point(50, 50), 0);
251 resizer->CompleteDrag();
252
msw6958e7f2017-05-15 22:55:06253 // Index and id are not changed after dragging the window.
James Cookb0bf8e82017-04-09 17:01:44254 EXPECT_EQ(index, model_->ItemIndexByID(id));
255 EXPECT_EQ(id, model_->items()[index].id);
256}
257
mswfeae4322017-06-08 04:24:22258// Ensure panels and dialogs get shelf items.
259TEST_F(ShelfWindowWatcherTest, PanelAndDialogWindows) {
mswfeae4322017-06-08 04:24:22260 // An item is created for a dialog window.
Mike Wasserman717cd572017-09-23 17:01:40261 EXPECT_EQ(1, model_->item_count());
mswfeae4322017-06-08 04:24:22262 std::unique_ptr<views::Widget> dialog_widget =
msw6958e7f2017-05-15 22:55:06263 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
mswfeae4322017-06-08 04:24:22264 aura::Window* dialog = dialog_widget->GetNativeWindow();
265 dialog->SetProperty(kShelfIDKey, new std::string(ShelfID("a").Serialize()));
266 dialog->SetProperty(kShelfItemTypeKey, static_cast<int32_t>(TYPE_DIALOG));
Mike Wasserman717cd572017-09-23 17:01:40267 EXPECT_EQ(2, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:44268
mswfeae4322017-06-08 04:24:22269 // An item is created for a panel window.
James Cookb0bf8e82017-04-09 17:01:44270 views::Widget panel_widget;
271 views::Widget::InitParams panel_params(views::Widget::InitParams::TYPE_PANEL);
272 panel_params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
Scott Violet66193f72017-06-26 22:24:18273 panel_params.parent = Shell::GetPrimaryRootWindow()->GetChildById(
274 kShellWindowId_PanelContainer);
James Cookb0bf8e82017-04-09 17:01:44275 panel_widget.Init(panel_params);
276 panel_widget.Show();
mswfeae4322017-06-08 04:24:22277 aura::Window* panel = panel_widget.GetNativeWindow();
278 panel->SetProperty(kShelfIDKey, new std::string(ShelfID("b").Serialize()));
279 panel->SetProperty(kShelfItemTypeKey, static_cast<int32_t>(TYPE_APP_PANEL));
Mike Wasserman717cd572017-09-23 17:01:40280 EXPECT_EQ(3, model_->item_count());
mswfeae4322017-06-08 04:24:22281
282 // An item is not created for an app window.
283 std::unique_ptr<views::Widget> app_widget =
284 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
285 aura::Window* app = app_widget->GetNativeWindow();
286 app->SetProperty(kShelfIDKey, new std::string(ShelfID("c").Serialize()));
287 app->SetProperty(kShelfItemTypeKey, static_cast<int32_t>(TYPE_APP));
Mike Wasserman717cd572017-09-23 17:01:40288 EXPECT_EQ(3, model_->item_count());
mswfeae4322017-06-08 04:24:22289 app_widget.reset();
James Cookb0bf8e82017-04-09 17:01:44290
291 // Each ShelfItem is removed when the associated window is destroyed.
292 panel_widget.CloseNow();
Michael Wasserman78b6f3e2017-07-20 19:51:20293 EXPECT_EQ(2, model_->item_count());
Mike Wasserman717cd572017-09-23 17:01:40294 dialog_widget.reset();
295 EXPECT_EQ(1, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:44296}
297
msw19b30c2c2017-06-01 03:21:40298// Ensure items use the app icon and window icon aura::Window properties.
299TEST_F(ShelfWindowWatcherTest, ItemIcon) {
msw19b30c2c2017-06-01 03:21:40300 // Create a ShelfItem for a window; it should have a default icon.
Mike Wasserman717cd572017-09-23 17:01:40301 EXPECT_EQ(1, model_->item_count());
msw19b30c2c2017-06-01 03:21:40302 std::unique_ptr<views::Widget> widget =
303 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
304 aura::Window* window = widget->GetNativeWindow();
305 ShelfID id = CreateShelfItem(window);
Mike Wasserman717cd572017-09-23 17:01:40306 EXPECT_EQ(2, model_->item_count());
msw19b30c2c2017-06-01 03:21:40307 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
308 gfx::Image default_image = rb.GetImageNamed(IDR_DEFAULT_FAVICON_32);
Mike Wasserman717cd572017-09-23 17:01:40309 EXPECT_TRUE(model_->items()[1].image.BackedBySameObjectAs(
msw19b30c2c2017-06-01 03:21:40310 default_image.AsImageSkia()));
311
312 // Setting a window icon should update the item icon.
313 const gfx::ImageSkia red = CreateImageSkiaIcon(SK_ColorRED);
314 window->SetProperty(aura::client::kWindowIconKey, new gfx::ImageSkia(red));
Mike Wasserman717cd572017-09-23 17:01:40315 EXPECT_EQ(SK_ColorRED, model_->items()[1].image.bitmap()->getColor(0, 0));
msw19b30c2c2017-06-01 03:21:40316
317 // Setting an app icon should override the window icon.
318 const gfx::ImageSkia blue = CreateImageSkiaIcon(SK_ColorBLUE);
319 window->SetProperty(aura::client::kAppIconKey, new gfx::ImageSkia(blue));
Mike Wasserman717cd572017-09-23 17:01:40320 EXPECT_EQ(SK_ColorBLUE, model_->items()[1].image.bitmap()->getColor(0, 0));
msw19b30c2c2017-06-01 03:21:40321
322 // Clearing the app icon should restore the window icon to the shelf item.
323 window->ClearProperty(aura::client::kAppIconKey);
Mike Wasserman717cd572017-09-23 17:01:40324 EXPECT_EQ(SK_ColorRED, model_->items()[1].image.bitmap()->getColor(0, 0));
msw19b30c2c2017-06-01 03:21:40325}
326
James Cookb0bf8e82017-04-09 17:01:44327TEST_F(ShelfWindowWatcherTest, DontCreateShelfEntriesForChildWindows) {
Mike Wasserman717cd572017-09-23 17:01:40328 EXPECT_EQ(1, model_->item_count());
mswfeae4322017-06-08 04:24:22329 std::unique_ptr<aura::Window> window =
Mitsuru Oshima04b54d02017-10-09 14:22:45330 std::make_unique<aura::Window>(nullptr, aura::client::WINDOW_TYPE_NORMAL);
James Cookb0bf8e82017-04-09 17:01:44331 window->Init(ui::LAYER_NOT_DRAWN);
mswfeae4322017-06-08 04:24:22332 window->SetProperty(kShelfIDKey, new std::string(ShelfID("a").Serialize()));
333 window->SetProperty(kShelfItemTypeKey, static_cast<int32_t>(TYPE_DIALOG));
James Cookb0bf8e82017-04-09 17:01:44334 Shell::GetPrimaryRootWindow()
335 ->GetChildById(kShellWindowId_DefaultContainer)
336 ->AddChild(window.get());
337 window->Show();
Mike Wasserman717cd572017-09-23 17:01:40338 EXPECT_EQ(2, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:44339
mswfeae4322017-06-08 04:24:22340 std::unique_ptr<aura::Window> child =
Mitsuru Oshima04b54d02017-10-09 14:22:45341 std::make_unique<aura::Window>(nullptr, aura::client::WINDOW_TYPE_NORMAL);
mswfeae4322017-06-08 04:24:22342 child->Init(ui::LAYER_NOT_DRAWN);
343 child->SetProperty(kShelfIDKey, new std::string(ShelfID("b").Serialize()));
344 child->SetProperty(kShelfItemTypeKey, static_cast<int32_t>(TYPE_DIALOG));
345 window->AddChild(child.get());
346 child->Show();
347 // There should not be a new shelf item for |child|.
Mike Wasserman717cd572017-09-23 17:01:40348 EXPECT_EQ(2, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:44349
mswfeae4322017-06-08 04:24:22350 child.reset();
Michael Wasserman78b6f3e2017-07-20 19:51:20351 EXPECT_EQ(2, model_->item_count());
Mike Wasserman717cd572017-09-23 17:01:40352 window.reset();
353 EXPECT_EQ(1, model_->item_count());
James Cookb0bf8e82017-04-09 17:01:44354}
355
mswfeae4322017-06-08 04:24:22356TEST_F(ShelfWindowWatcherTest, CreateShelfEntriesForTransientWindows) {
Mike Wasserman717cd572017-09-23 17:01:40357 EXPECT_EQ(1, model_->item_count());
mswfeae4322017-06-08 04:24:22358 std::unique_ptr<aura::Window> window =
Mitsuru Oshima04b54d02017-10-09 14:22:45359 std::make_unique<aura::Window>(nullptr, aura::client::WINDOW_TYPE_NORMAL);
msw70ac45f2017-06-05 02:02:45360 window->Init(ui::LAYER_NOT_DRAWN);
mswfeae4322017-06-08 04:24:22361 window->SetProperty(kShelfIDKey, new std::string(ShelfID("a").Serialize()));
362 window->SetProperty(kShelfItemTypeKey, static_cast<int32_t>(TYPE_DIALOG));
msw70ac45f2017-06-05 02:02:45363 Shell::GetPrimaryRootWindow()
364 ->GetChildById(kShellWindowId_DefaultContainer)
365 ->AddChild(window.get());
366 window->Show();
Mike Wasserman717cd572017-09-23 17:01:40367 EXPECT_EQ(2, model_->item_count());
msw70ac45f2017-06-05 02:02:45368
mswfeae4322017-06-08 04:24:22369 std::unique_ptr<aura::Window> transient =
Mitsuru Oshima04b54d02017-10-09 14:22:45370 std::make_unique<aura::Window>(nullptr, aura::client::WINDOW_TYPE_NORMAL);
mswfeae4322017-06-08 04:24:22371 transient->Init(ui::LAYER_NOT_DRAWN);
372 transient->SetProperty(kShelfIDKey,
373 new std::string(ShelfID("b").Serialize()));
374 transient->SetProperty(kShelfItemTypeKey, static_cast<int32_t>(TYPE_DIALOG));
375 Shell::GetPrimaryRootWindow()
376 ->GetChildById(kShellWindowId_DefaultContainer)
377 ->AddChild(transient.get());
378 ::wm::TransientWindowController::Get()->AddTransientChild(window.get(),
379 transient.get());
380 transient->Show();
381 // There should be a new shelf item for |transient|.
Mike Wasserman717cd572017-09-23 17:01:40382 EXPECT_EQ(3, model_->item_count());
msw70ac45f2017-06-05 02:02:45383
mswfeae4322017-06-08 04:24:22384 transient.reset();
Michael Wasserman78b6f3e2017-07-20 19:51:20385 EXPECT_EQ(2, model_->item_count());
Mike Wasserman717cd572017-09-23 17:01:40386 window.reset();
387 EXPECT_EQ(1, model_->item_count());
msw70ac45f2017-06-05 02:02:45388}
389
James Cookb0bf8e82017-04-09 17:01:44390// Ensures ShelfWindowWatcher supports windows opened prior to session start.
James Cook317781a2017-07-18 02:08:06391using ShelfWindowWatcherSessionStartTest = NoSessionAshTestBase;
James Cookb0bf8e82017-04-09 17:01:44392TEST_F(ShelfWindowWatcherSessionStartTest, PreExistingWindow) {
393 ShelfModel* model = Shell::Get()->shelf_model();
394 ASSERT_FALSE(
395 Shell::Get()->session_controller()->IsActiveUserSessionStarted());
396
Mike Wasserman717cd572017-09-23 17:01:40397 // ShelfModel creates an app list item.
398 EXPECT_EQ(1, model->item_count());
James Cookb0bf8e82017-04-09 17:01:44399
400 // Construct a window that should get a shelf item once the session starts.
401 std::unique_ptr<views::Widget> widget =
402 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
msw6958e7f2017-05-15 22:55:06403 ShelfWindowWatcherTest::CreateShelfItem(widget->GetNativeWindow());
Mike Wasserman717cd572017-09-23 17:01:40404 EXPECT_EQ(1, model->item_count());
James Cookb0bf8e82017-04-09 17:01:44405
406 // Start the test user session; ShelfWindowWatcher will find the open window.
Xiyuan Xiaffbc92032017-08-21 18:36:13407 CreateUserSessions(1);
Mike Wasserman717cd572017-09-23 17:01:40408 EXPECT_EQ(2, model->item_count());
James Cookb0bf8e82017-04-09 17:01:44409}
410
msw19b30c2c2017-06-01 03:21:40411} // namespace
James Cookb0bf8e82017-04-09 17:01:44412} // namespace ash