forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshelf_window_watcher_unittest.cc
465 lines (394 loc) · 18.3 KB
/
shelf_window_watcher_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/shelf/shelf_window_watcher.h"
#include "ash/public/cpp/config.h"
#include "ash/public/cpp/shelf_item.h"
#include "ash/public/cpp/shelf_model.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/public/cpp/window_properties.h"
#include "ash/root_window_controller.h"
#include "ash/session/session_controller.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ash/wm/window_resizer.h"
#include "ash/wm/window_state.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/window.h"
#include "ui/base/hit_test.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/resources/grit/ui_resources.h"
#include "ui/views/widget/widget.h"
#include "ui/wm/core/transient_window_controller.h"
namespace ash {
namespace {
// Create a test 1x1 icon image with a given |color|.
gfx::ImageSkia CreateImageSkiaIcon(SkColor color) {
SkBitmap bitmap;
bitmap.allocN32Pixels(1, 1);
bitmap.eraseColor(color);
return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
}
class ShelfWindowWatcherTest : public AshTestBase {
public:
ShelfWindowWatcherTest() : model_(nullptr) {}
~ShelfWindowWatcherTest() override {}
void SetUp() override {
AshTestBase::SetUp();
model_ = Shell::Get()->shelf_model();
}
void TearDown() override {
model_ = nullptr;
AshTestBase::TearDown();
}
static ShelfID CreateShelfItem(aura::Window* window) {
static int id = 0;
ShelfID shelf_id(std::to_string(id++));
window->SetProperty(kShelfIDKey, new std::string(shelf_id.Serialize()));
window->SetProperty(kShelfItemTypeKey, static_cast<int32_t>(TYPE_DIALOG));
return shelf_id;
}
protected:
ShelfModel* model_;
private:
DISALLOW_COPY_AND_ASSIGN(ShelfWindowWatcherTest);
};
// Ensure shelf items are added and removed as windows are opened and closed.
TEST_F(ShelfWindowWatcherTest, OpenAndClose) {
// ShelfModel only has an APP_LIST item.
EXPECT_EQ(1, model_->item_count());
// Windows with valid ShelfItemType and ShelfID properties get shelf items.
std::unique_ptr<views::Widget> widget1 =
CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
CreateShelfItem(widget1->GetNativeWindow());
EXPECT_EQ(2, model_->item_count());
std::unique_ptr<views::Widget> widget2 =
CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
CreateShelfItem(widget2->GetNativeWindow());
EXPECT_EQ(3, model_->item_count());
// Each ShelfItem is removed when the associated window is destroyed.
widget1.reset();
EXPECT_EQ(2, model_->item_count());
widget2.reset();
EXPECT_EQ(1, model_->item_count());
}
// Ensure shelf items are added and removed for some unknown windows in mash.
TEST_F(ShelfWindowWatcherTest, OpenAndCloseMash) {
if (Shell::GetAshConfig() != Config::MASH)
return;
// ShelfModel only has an APP_LIST item.
EXPECT_EQ(1, model_->item_count());
// Windows with no valid ShelfItemType and ShelfID properties get shelf items.
std::unique_ptr<views::Widget> widget1 =
CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
EXPECT_EQ(2, model_->item_count());
std::unique_ptr<views::Widget> widget2 =
CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
EXPECT_EQ(3, model_->item_count());
// Each ShelfItem is removed when the associated window is destroyed.
widget1.reset();
EXPECT_EQ(2, model_->item_count());
widget2.reset();
EXPECT_EQ(1, model_->item_count());
// Windows with type WINDOW_TYPE_NORMAL get shelf items, others do not.
aura::client::WindowType no_item_types[] = {
aura::client::WINDOW_TYPE_UNKNOWN, aura::client::WINDOW_TYPE_NORMAL,
aura::client::WINDOW_TYPE_POPUP, aura::client::WINDOW_TYPE_CONTROL,
aura::client::WINDOW_TYPE_PANEL, aura::client::WINDOW_TYPE_MENU,
aura::client::WINDOW_TYPE_TOOLTIP};
for (aura::client::WindowType type : no_item_types) {
std::unique_ptr<aura::Window> window =
base::MakeUnique<aura::Window>(nullptr, type);
window->Init(ui::LAYER_NOT_DRAWN);
Shell::GetPrimaryRootWindow()
->GetChildById(kShellWindowId_DefaultContainer)
->AddChild(window.get());
window->Show();
EXPECT_EQ(type == aura::client::WINDOW_TYPE_NORMAL ? 2 : 1,
model_->item_count());
}
// Windows with WindowState::ignored_by_shelf set do not get shelf items.
widget1 =
CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
wm::GetWindowState(widget1->GetNativeWindow())->set_ignored_by_shelf(true);
// TODO(msw): Make the flag a window property and remove this workaround.
widget1->GetNativeWindow()->SetProperty(aura::client::kDrawAttentionKey,
true);
EXPECT_EQ(1, model_->item_count());
}
TEST_F(ShelfWindowWatcherTest, CreateAndRemoveShelfItemProperties) {
// TODO: investigate failure in mash. http://crbug.com/695562.
if (Shell::GetAshConfig() == Config::MASH)
return;
// ShelfModel only has an APP_LIST item.
EXPECT_EQ(1, model_->item_count());
// Creating windows without a valid ShelfItemType does not add items.
std::unique_ptr<views::Widget> widget1 =
CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
std::unique_ptr<views::Widget> widget2 =
CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
EXPECT_EQ(1, model_->item_count());
// Create a ShelfItem for the first window.
ShelfID id_w1 = CreateShelfItem(widget1->GetNativeWindow());
EXPECT_EQ(2, model_->item_count());
int index_w1 = model_->ItemIndexByID(id_w1);
EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w1].status);
// Create a ShelfItem for the second window.
ShelfID id_w2 = CreateShelfItem(widget2->GetNativeWindow());
EXPECT_EQ(3, model_->item_count());
int index_w2 = model_->ItemIndexByID(id_w2);
EXPECT_EQ(STATUS_ACTIVE, model_->items()[index_w2].status);
// ShelfItem is removed when the item type window property is cleared.
widget1->GetNativeWindow()->SetProperty(kShelfItemTypeKey,
static_cast<int32_t>(TYPE_UNDEFINED));
EXPECT_EQ(2, model_->item_count());
widget2->GetNativeWindow()->SetProperty(kShelfItemTypeKey,
static_cast<int32_t>(TYPE_UNDEFINED));
EXPECT_EQ(1, model_->item_count());
// Clearing twice doesn't do anything.
widget2->GetNativeWindow()->SetProperty(kShelfItemTypeKey,
static_cast<int32_t>(TYPE_UNDEFINED));
EXPECT_EQ(1, model_->item_count());
}
TEST_F(ShelfWindowWatcherTest, ActivateWindow) {
// TODO: investigate failure in mash. http://crbug.com/695562.
if (Shell::GetAshConfig() == Config::MASH)
return;
// ShelfModel only has an APP_LIST item.
EXPECT_EQ(1, model_->item_count());
std::unique_ptr<views::Widget> widget1 =
CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
std::unique_ptr<views::Widget> widget2 =
CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
// Create a ShelfItem for the first window.
ShelfID id_w1 = CreateShelfItem(widget1->GetNativeWindow());
EXPECT_EQ(2, model_->item_count());
int index_w1 = model_->ItemIndexByID(id_w1);
EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w1].status);
// Create a ShelfItem for the second window.
ShelfID id_w2 = CreateShelfItem(widget2->GetNativeWindow());
EXPECT_EQ(3, model_->item_count());
int index_w2 = model_->ItemIndexByID(id_w2);
EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w1].status);
EXPECT_EQ(STATUS_ACTIVE, model_->items()[index_w2].status);
// The ShelfItem for the first window is active when the window is activated.
widget1->Activate();
EXPECT_EQ(STATUS_ACTIVE, model_->items()[index_w1].status);
EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w2].status);
// The ShelfItem for the second window is active when the window is activated.
widget2->Activate();
EXPECT_EQ(STATUS_RUNNING, model_->items()[index_w1].status);
EXPECT_EQ(STATUS_ACTIVE, model_->items()[index_w2].status);
}
TEST_F(ShelfWindowWatcherTest, UpdateWindowProperty) {
// ShelfModel only has an APP_LIST item.
EXPECT_EQ(1, model_->item_count());
std::unique_ptr<views::Widget> widget =
CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
// Create a ShelfItem for |window|.
ShelfID id = CreateShelfItem(widget->GetNativeWindow());
EXPECT_EQ(2, model_->item_count());
int index = model_->ItemIndexByID(id);
EXPECT_EQ(STATUS_ACTIVE, model_->items()[index].status);
// Update the window's ShelfItemType.
widget->GetNativeWindow()->SetProperty(kShelfItemTypeKey,
static_cast<int32_t>(TYPE_APP_PANEL));
// No new item is created after updating a launcher item.
EXPECT_EQ(2, model_->item_count());
// index and id are not changed after updating a launcher item.
EXPECT_EQ(index, model_->ItemIndexByID(id));
EXPECT_EQ(id, model_->items()[index].id);
}
TEST_F(ShelfWindowWatcherTest, MaximizeAndRestoreWindow) {
// ShelfModel only has an APP_LIST item.
EXPECT_EQ(1, model_->item_count());
std::unique_ptr<views::Widget> widget =
CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
wm::WindowState* window_state = wm::GetWindowState(widget->GetNativeWindow());
// Create a ShelfItem for the window.
ShelfID id = CreateShelfItem(widget->GetNativeWindow());
EXPECT_EQ(2, model_->item_count());
int index = model_->ItemIndexByID(id);
EXPECT_EQ(STATUS_ACTIVE, model_->items()[index].status);
// Maximize the window.
EXPECT_FALSE(window_state->IsMaximized());
window_state->Maximize();
EXPECT_TRUE(window_state->IsMaximized());
// No new item is created after maximizing the window.
EXPECT_EQ(2, model_->item_count());
// index and id are not changed after maximizing the window.
EXPECT_EQ(index, model_->ItemIndexByID(id));
EXPECT_EQ(id, model_->items()[index].id);
// Restore the window.
window_state->Restore();
EXPECT_FALSE(window_state->IsMaximized());
// No new item is created after restoring the window.
EXPECT_EQ(2, model_->item_count());
// Index and id are not changed after maximizing the window.
EXPECT_EQ(index, model_->ItemIndexByID(id));
EXPECT_EQ(id, model_->items()[index].id);
}
// Check |window|'s item is not changed during the dragging.
// TODO(simonhong): Add a test for removing a Window during the dragging.
TEST_F(ShelfWindowWatcherTest, DragWindow) {
// ShelfModel only has an APP_LIST item.
EXPECT_EQ(1, model_->item_count());
std::unique_ptr<views::Widget> widget =
CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
// Create a ShelfItem for the window.
ShelfID id = CreateShelfItem(widget->GetNativeWindow());
EXPECT_EQ(2, model_->item_count());
int index = model_->ItemIndexByID(id);
EXPECT_EQ(STATUS_ACTIVE, model_->items()[index].status);
// Simulate dragging of the window and check its item is not changed.
std::unique_ptr<WindowResizer> resizer(
CreateWindowResizer(widget->GetNativeWindow(), gfx::Point(), HTCAPTION,
::wm::WINDOW_MOVE_SOURCE_MOUSE));
ASSERT_TRUE(resizer.get());
resizer->Drag(gfx::Point(50, 50), 0);
resizer->CompleteDrag();
// Index and id are not changed after dragging the window.
EXPECT_EQ(index, model_->ItemIndexByID(id));
EXPECT_EQ(id, model_->items()[index].id);
}
// Ensure panels and dialogs get shelf items.
TEST_F(ShelfWindowWatcherTest, PanelAndDialogWindows) {
// ShelfModel only has an APP_LIST item.
EXPECT_EQ(1, model_->item_count());
// An item is created for a dialog window.
std::unique_ptr<views::Widget> dialog_widget =
CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
aura::Window* dialog = dialog_widget->GetNativeWindow();
dialog->SetProperty(kShelfIDKey, new std::string(ShelfID("a").Serialize()));
dialog->SetProperty(kShelfItemTypeKey, static_cast<int32_t>(TYPE_DIALOG));
EXPECT_EQ(2, model_->item_count());
// An item is created for a panel window.
views::Widget panel_widget;
views::Widget::InitParams panel_params(views::Widget::InitParams::TYPE_PANEL);
panel_params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
panel_params.parent = Shell::GetPrimaryRootWindow()->GetChildById(
kShellWindowId_PanelContainer);
panel_widget.Init(panel_params);
panel_widget.Show();
aura::Window* panel = panel_widget.GetNativeWindow();
panel->SetProperty(kShelfIDKey, new std::string(ShelfID("b").Serialize()));
panel->SetProperty(kShelfItemTypeKey, static_cast<int32_t>(TYPE_APP_PANEL));
EXPECT_EQ(3, model_->item_count());
// An item is not created for an app window.
std::unique_ptr<views::Widget> app_widget =
CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
aura::Window* app = app_widget->GetNativeWindow();
app->SetProperty(kShelfIDKey, new std::string(ShelfID("c").Serialize()));
app->SetProperty(kShelfItemTypeKey, static_cast<int32_t>(TYPE_APP));
EXPECT_EQ(3, model_->item_count());
app_widget.reset();
// Each ShelfItem is removed when the associated window is destroyed.
panel_widget.CloseNow();
EXPECT_EQ(2, model_->item_count());
dialog_widget.reset();
EXPECT_EQ(1, model_->item_count());
}
// Ensure items use the app icon and window icon aura::Window properties.
TEST_F(ShelfWindowWatcherTest, ItemIcon) {
// ShelfModel only has an APP_LIST item.
EXPECT_EQ(1, model_->item_count());
// Create a ShelfItem for a window; it should have a default icon.
std::unique_ptr<views::Widget> widget =
CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
aura::Window* window = widget->GetNativeWindow();
ShelfID id = CreateShelfItem(window);
EXPECT_EQ(2, model_->item_count());
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
gfx::Image default_image = rb.GetImageNamed(IDR_DEFAULT_FAVICON_32);
EXPECT_TRUE(model_->items()[1].image.BackedBySameObjectAs(
default_image.AsImageSkia()));
// Setting a window icon should update the item icon.
const gfx::ImageSkia red = CreateImageSkiaIcon(SK_ColorRED);
window->SetProperty(aura::client::kWindowIconKey, new gfx::ImageSkia(red));
EXPECT_EQ(SK_ColorRED, model_->items()[1].image.bitmap()->getColor(0, 0));
// Setting an app icon should override the window icon.
const gfx::ImageSkia blue = CreateImageSkiaIcon(SK_ColorBLUE);
window->SetProperty(aura::client::kAppIconKey, new gfx::ImageSkia(blue));
EXPECT_EQ(SK_ColorBLUE, model_->items()[1].image.bitmap()->getColor(0, 0));
// Clearing the app icon should restore the window icon to the shelf item.
window->ClearProperty(aura::client::kAppIconKey);
EXPECT_EQ(SK_ColorRED, model_->items()[1].image.bitmap()->getColor(0, 0));
}
TEST_F(ShelfWindowWatcherTest, DontCreateShelfEntriesForChildWindows) {
EXPECT_EQ(1, model_->item_count());
std::unique_ptr<aura::Window> window =
base::MakeUnique<aura::Window>(nullptr, aura::client::WINDOW_TYPE_NORMAL);
window->Init(ui::LAYER_NOT_DRAWN);
window->SetProperty(kShelfIDKey, new std::string(ShelfID("a").Serialize()));
window->SetProperty(kShelfItemTypeKey, static_cast<int32_t>(TYPE_DIALOG));
Shell::GetPrimaryRootWindow()
->GetChildById(kShellWindowId_DefaultContainer)
->AddChild(window.get());
window->Show();
EXPECT_EQ(2, model_->item_count());
std::unique_ptr<aura::Window> child =
base::MakeUnique<aura::Window>(nullptr, aura::client::WINDOW_TYPE_NORMAL);
child->Init(ui::LAYER_NOT_DRAWN);
child->SetProperty(kShelfIDKey, new std::string(ShelfID("b").Serialize()));
child->SetProperty(kShelfItemTypeKey, static_cast<int32_t>(TYPE_DIALOG));
window->AddChild(child.get());
child->Show();
// There should not be a new shelf item for |child|.
EXPECT_EQ(2, model_->item_count());
child.reset();
EXPECT_EQ(2, model_->item_count());
window.reset();
EXPECT_EQ(1, model_->item_count());
}
TEST_F(ShelfWindowWatcherTest, CreateShelfEntriesForTransientWindows) {
EXPECT_EQ(1, model_->item_count());
std::unique_ptr<aura::Window> window =
base::MakeUnique<aura::Window>(nullptr, aura::client::WINDOW_TYPE_NORMAL);
window->Init(ui::LAYER_NOT_DRAWN);
window->SetProperty(kShelfIDKey, new std::string(ShelfID("a").Serialize()));
window->SetProperty(kShelfItemTypeKey, static_cast<int32_t>(TYPE_DIALOG));
Shell::GetPrimaryRootWindow()
->GetChildById(kShellWindowId_DefaultContainer)
->AddChild(window.get());
window->Show();
EXPECT_EQ(2, model_->item_count());
std::unique_ptr<aura::Window> transient =
base::MakeUnique<aura::Window>(nullptr, aura::client::WINDOW_TYPE_NORMAL);
transient->Init(ui::LAYER_NOT_DRAWN);
transient->SetProperty(kShelfIDKey,
new std::string(ShelfID("b").Serialize()));
transient->SetProperty(kShelfItemTypeKey, static_cast<int32_t>(TYPE_DIALOG));
Shell::GetPrimaryRootWindow()
->GetChildById(kShellWindowId_DefaultContainer)
->AddChild(transient.get());
::wm::TransientWindowController::Get()->AddTransientChild(window.get(),
transient.get());
transient->Show();
// There should be a new shelf item for |transient|.
EXPECT_EQ(3, model_->item_count());
transient.reset();
EXPECT_EQ(2, model_->item_count());
window.reset();
EXPECT_EQ(1, model_->item_count());
}
// Ensures ShelfWindowWatcher supports windows opened prior to session start.
using ShelfWindowWatcherSessionStartTest = NoSessionAshTestBase;
TEST_F(ShelfWindowWatcherSessionStartTest, PreExistingWindow) {
ShelfModel* model = Shell::Get()->shelf_model();
ASSERT_FALSE(
Shell::Get()->session_controller()->IsActiveUserSessionStarted());
// ShelfModel only has an APP_LIST item.
EXPECT_EQ(1, model->item_count());
// Construct a window that should get a shelf item once the session starts.
std::unique_ptr<views::Widget> widget =
CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect());
ShelfWindowWatcherTest::CreateShelfItem(widget->GetNativeWindow());
EXPECT_EQ(1, model->item_count());
// Start the test user session; ShelfWindowWatcher will find the open window.
SetSessionStarted(true);
EXPECT_EQ(2, model->item_count());
}
} // namespace
} // namespace ash