| 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/public/cpp/config.h" | 7 #include "ash/public/cpp/config.h" |
| 8 #include "ash/public/cpp/shelf_item.h" | 8 #include "ash/public/cpp/shelf_item.h" |
| 9 #include "ash/public/cpp/shelf_model.h" | 9 #include "ash/public/cpp/shelf_model.h" |
| 10 #include "ash/public/cpp/shell_window_ids.h" | 10 #include "ash/public/cpp/shell_window_ids.h" |
| 11 #include "ash/public/cpp/window_properties.h" | 11 #include "ash/public/cpp/window_properties.h" |
| 12 #include "ash/root_window_controller.h" | 12 #include "ash/root_window_controller.h" |
| 13 #include "ash/session/session_controller.h" | 13 #include "ash/session/session_controller.h" |
| 14 #include "ash/shell.h" | 14 #include "ash/shell.h" |
| 15 #include "ash/shell_port.h" | 15 #include "ash/shell_port.h" |
| 16 #include "ash/test/ash_test_base.h" | 16 #include "ash/test/ash_test_base.h" |
| 17 #include "ash/wm/window_resizer.h" | 17 #include "ash/wm/window_resizer.h" |
| 18 #include "ash/wm/window_state.h" | 18 #include "ash/wm/window_state.h" |
| 19 #include "base/strings/string_number_conversions.h" | |
| 20 #include "third_party/skia/include/core/SkBitmap.h" | 19 #include "third_party/skia/include/core/SkBitmap.h" |
| 21 #include "ui/aura/client/aura_constants.h" | 20 #include "ui/aura/client/aura_constants.h" |
| 22 #include "ui/aura/window.h" | 21 #include "ui/aura/window.h" |
| 23 #include "ui/base/hit_test.h" | 22 #include "ui/base/hit_test.h" |
| 24 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
| 25 #include "ui/gfx/image/image_skia.h" | 24 #include "ui/gfx/image/image_skia.h" |
| 26 #include "ui/resources/grit/ui_resources.h" | 25 #include "ui/resources/grit/ui_resources.h" |
| 27 #include "ui/views/widget/widget.h" | 26 #include "ui/views/widget/widget.h" |
| 27 #include "ui/wm/core/transient_window_controller.h" |
| 28 | 28 |
| 29 namespace ash { | 29 namespace ash { |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 // Create a test 1x1 icon image with a given |color|. | 32 // Create a test 1x1 icon image with a given |color|. |
| 33 gfx::ImageSkia CreateImageSkiaIcon(SkColor color) { | 33 gfx::ImageSkia CreateImageSkiaIcon(SkColor color) { |
| 34 SkBitmap bitmap; | 34 SkBitmap bitmap; |
| 35 bitmap.allocN32Pixels(1, 1); | 35 bitmap.allocN32Pixels(1, 1); |
| 36 bitmap.eraseColor(color); | 36 bitmap.eraseColor(color); |
| 37 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap); | 37 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap); |
| 38 } | 38 } |
| 39 | 39 |
| 40 class ShelfWindowWatcherTest : public test::AshTestBase { | 40 class ShelfWindowWatcherTest : public test::AshTestBase { |
| 41 public: | 41 public: |
| 42 ShelfWindowWatcherTest() : model_(nullptr) {} | 42 ShelfWindowWatcherTest() : model_(nullptr) {} |
| 43 ~ShelfWindowWatcherTest() override {} | 43 ~ShelfWindowWatcherTest() override {} |
| 44 | 44 |
| 45 void SetUp() override { | 45 void SetUp() override { |
| 46 test::AshTestBase::SetUp(); | 46 test::AshTestBase::SetUp(); |
| 47 model_ = Shell::Get()->shelf_model(); | 47 model_ = Shell::Get()->shelf_model(); |
| 48 } | 48 } |
| 49 | 49 |
| 50 void TearDown() override { | 50 void TearDown() override { |
| 51 model_ = nullptr; | 51 model_ = nullptr; |
| 52 test::AshTestBase::TearDown(); | 52 test::AshTestBase::TearDown(); |
| 53 } | 53 } |
| 54 | 54 |
| 55 static ShelfID CreateShelfItem(aura::Window* window) { | 55 static ShelfID CreateShelfItem(aura::Window* window) { |
| 56 static int id = 0; | 56 static int id = 0; |
| 57 ShelfID shelf_id(base::IntToString(id++)); | 57 ShelfID shelf_id(std::to_string(id++)); |
| 58 window->SetProperty(kShelfIDKey, new std::string(shelf_id.Serialize())); | 58 window->SetProperty(kShelfIDKey, new std::string(shelf_id.Serialize())); |
| 59 window->SetProperty(kShelfItemTypeKey, static_cast<int32_t>(TYPE_DIALOG)); | 59 window->SetProperty(kShelfItemTypeKey, static_cast<int32_t>(TYPE_DIALOG)); |
| 60 return shelf_id; | 60 return shelf_id; |
| 61 } | 61 } |
| 62 | 62 |
| 63 protected: | 63 protected: |
| 64 ShelfModel* model_; | 64 ShelfModel* model_; |
| 65 | 65 |
| 66 private: | 66 private: |
| 67 DISALLOW_COPY_AND_ASSIGN(ShelfWindowWatcherTest); | 67 DISALLOW_COPY_AND_ASSIGN(ShelfWindowWatcherTest); |
| 68 }; | 68 }; |
| 69 | 69 |
| 70 // Ensure shelf items are added and removed as windows are opened and closed. | 70 // Ensure shelf items are added and removed as windows are opened and closed. |
| 71 TEST_F(ShelfWindowWatcherTest, OpenAndClose) { | 71 TEST_F(ShelfWindowWatcherTest, OpenAndClose) { |
| 72 // ShelfModel only has an APP_LIST item. | 72 // ShelfModel only has an APP_LIST item. |
| 73 EXPECT_EQ(1, model_->item_count()); | 73 EXPECT_EQ(1, model_->item_count()); |
| 74 | 74 |
| 75 // Adding windows with valid ShelfItemType properties adds shelf items. | 75 // Windows with valid ShelfItemType and ShelfID properties get shelf items. |
| 76 std::unique_ptr<views::Widget> widget1 = | 76 std::unique_ptr<views::Widget> widget1 = |
| 77 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect()); | 77 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect()); |
| 78 CreateShelfItem(widget1->GetNativeWindow()); | 78 CreateShelfItem(widget1->GetNativeWindow()); |
| 79 EXPECT_EQ(2, model_->item_count()); | 79 EXPECT_EQ(2, model_->item_count()); |
| 80 std::unique_ptr<views::Widget> widget2 = | 80 std::unique_ptr<views::Widget> widget2 = |
| 81 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect()); | 81 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect()); |
| 82 CreateShelfItem(widget2->GetNativeWindow()); | 82 CreateShelfItem(widget2->GetNativeWindow()); |
| 83 EXPECT_EQ(3, model_->item_count()); | 83 EXPECT_EQ(3, model_->item_count()); |
| 84 | 84 |
| 85 // Each ShelfItem is removed when the associated window is destroyed. | 85 // Each ShelfItem is removed when the associated window is destroyed. |
| 86 widget1.reset(); | 86 widget1.reset(); |
| 87 EXPECT_EQ(2, model_->item_count()); | 87 EXPECT_EQ(2, model_->item_count()); |
| 88 widget2.reset(); | 88 widget2.reset(); |
| 89 EXPECT_EQ(1, model_->item_count()); | 89 EXPECT_EQ(1, model_->item_count()); |
| 90 } | 90 } |
| 91 | 91 |
| 92 // Ensure shelf items are added and removed for unknown windows in mash. |
| 93 TEST_F(ShelfWindowWatcherTest, OpenAndCloseMash) { |
| 94 if (Shell::GetAshConfig() != Config::MASH) |
| 95 return; |
| 96 |
| 97 // ShelfModel only has an APP_LIST item. |
| 98 EXPECT_EQ(1, model_->item_count()); |
| 99 |
| 100 // Windows with no valid ShelfItemType and ShelfID properties get shelf items. |
| 101 std::unique_ptr<views::Widget> widget1 = |
| 102 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect()); |
| 103 EXPECT_EQ(2, model_->item_count()); |
| 104 std::unique_ptr<views::Widget> widget2 = |
| 105 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect()); |
| 106 EXPECT_EQ(3, model_->item_count()); |
| 107 |
| 108 // Each ShelfItem is removed when the associated window is destroyed. |
| 109 widget1.reset(); |
| 110 EXPECT_EQ(2, model_->item_count()); |
| 111 widget2.reset(); |
| 112 EXPECT_EQ(1, model_->item_count()); |
| 113 |
| 114 // Windows with WindowState::ignored_by_shelf set do not get shelf items. |
| 115 widget1 = |
| 116 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect()); |
| 117 wm::GetWindowState(widget1->GetNativeWindow())->set_ignored_by_shelf(true); |
| 118 // TODO(msw): Make the flag a window property and remove this workaround. |
| 119 widget1->GetNativeWindow()->SetProperty(aura::client::kDrawAttentionKey, |
| 120 true); |
| 121 EXPECT_EQ(1, model_->item_count()); |
| 122 } |
| 123 |
| 92 TEST_F(ShelfWindowWatcherTest, CreateAndRemoveShelfItemProperties) { | 124 TEST_F(ShelfWindowWatcherTest, CreateAndRemoveShelfItemProperties) { |
| 93 // TODO: investigate failure in mash. http://crbug.com/695562. | 125 // TODO: investigate failure in mash. http://crbug.com/695562. |
| 94 if (Shell::GetAshConfig() == Config::MASH) | 126 if (Shell::GetAshConfig() == Config::MASH) |
| 95 return; | 127 return; |
| 96 | 128 |
| 97 // ShelfModel only has an APP_LIST item. | 129 // ShelfModel only has an APP_LIST item. |
| 98 EXPECT_EQ(1, model_->item_count()); | 130 EXPECT_EQ(1, model_->item_count()); |
| 99 | 131 |
| 100 // Creating windows without a valid ShelfItemType does not add items. | 132 // Creating windows without a valid ShelfItemType does not add items. |
| 101 std::unique_ptr<views::Widget> widget1 = | 133 std::unique_ptr<views::Widget> widget1 = |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 window->AddChild(child_window.get()); | 399 window->AddChild(child_window.get()); |
| 368 child_window->Show(); | 400 child_window->Show(); |
| 369 // |child_window| should not result in adding a new entry. | 401 // |child_window| should not result in adding a new entry. |
| 370 EXPECT_EQ(initial_item_count + 1, model_->item_count()); | 402 EXPECT_EQ(initial_item_count + 1, model_->item_count()); |
| 371 | 403 |
| 372 child_window.reset(); | 404 child_window.reset(); |
| 373 window.reset(); | 405 window.reset(); |
| 374 EXPECT_EQ(initial_item_count, model_->item_count()); | 406 EXPECT_EQ(initial_item_count, model_->item_count()); |
| 375 } | 407 } |
| 376 | 408 |
| 409 TEST_F(ShelfWindowWatcherTest, DontCreateShelfEntriesForTransientWindows) { |
| 410 const int initial_item_count = model_->item_count(); |
| 411 |
| 412 std::unique_ptr<aura::Window> window(base::MakeUnique<aura::Window>( |
| 413 nullptr, aura::client::WINDOW_TYPE_NORMAL)); |
| 414 window->Init(ui::LAYER_NOT_DRAWN); |
| 415 window->SetProperty(kShelfIDKey, new std::string(ShelfID("foo").Serialize())); |
| 416 window->SetProperty(kShelfItemTypeKey, static_cast<int32_t>(TYPE_APP)); |
| 417 Shell::GetPrimaryRootWindow() |
| 418 ->GetChildById(kShellWindowId_DefaultContainer) |
| 419 ->AddChild(window.get()); |
| 420 window->Show(); |
| 421 EXPECT_EQ(initial_item_count + 1, model_->item_count()); |
| 422 |
| 423 std::unique_ptr<aura::Window> transient_window(base::MakeUnique<aura::Window>( |
| 424 nullptr, aura::client::WINDOW_TYPE_NORMAL)); |
| 425 transient_window->Init(ui::LAYER_NOT_DRAWN); |
| 426 transient_window->SetProperty(kShelfItemTypeKey, |
| 427 static_cast<int32_t>(TYPE_APP)); |
| 428 ::wm::TransientWindowController::Get()->AddTransientChild( |
| 429 window.get(), transient_window.get()); |
| 430 transient_window->Show(); |
| 431 // |transient_window| should not result in adding a new entry. |
| 432 EXPECT_EQ(initial_item_count + 1, model_->item_count()); |
| 433 |
| 434 transient_window.reset(); |
| 435 window.reset(); |
| 436 EXPECT_EQ(initial_item_count, model_->item_count()); |
| 437 } |
| 438 |
| 377 // Ensures ShelfWindowWatcher supports windows opened prior to session start. | 439 // Ensures ShelfWindowWatcher supports windows opened prior to session start. |
| 378 using ShelfWindowWatcherSessionStartTest = test::NoSessionAshTestBase; | 440 using ShelfWindowWatcherSessionStartTest = test::NoSessionAshTestBase; |
| 379 TEST_F(ShelfWindowWatcherSessionStartTest, PreExistingWindow) { | 441 TEST_F(ShelfWindowWatcherSessionStartTest, PreExistingWindow) { |
| 380 ShelfModel* model = Shell::Get()->shelf_model(); | 442 ShelfModel* model = Shell::Get()->shelf_model(); |
| 381 ASSERT_FALSE( | 443 ASSERT_FALSE( |
| 382 Shell::Get()->session_controller()->IsActiveUserSessionStarted()); | 444 Shell::Get()->session_controller()->IsActiveUserSessionStarted()); |
| 383 | 445 |
| 384 // ShelfModel only has an APP_LIST item. | 446 // ShelfModel only has an APP_LIST item. |
| 385 EXPECT_EQ(1, model->item_count()); | 447 EXPECT_EQ(1, model->item_count()); |
| 386 | 448 |
| 387 // Construct a window that should get a shelf item once the session starts. | 449 // Construct a window that should get a shelf item once the session starts. |
| 388 std::unique_ptr<views::Widget> widget = | 450 std::unique_ptr<views::Widget> widget = |
| 389 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect()); | 451 CreateTestWidget(nullptr, kShellWindowId_DefaultContainer, gfx::Rect()); |
| 390 ShelfWindowWatcherTest::CreateShelfItem(widget->GetNativeWindow()); | 452 ShelfWindowWatcherTest::CreateShelfItem(widget->GetNativeWindow()); |
| 391 EXPECT_EQ(1, model->item_count()); | 453 EXPECT_EQ(1, model->item_count()); |
| 392 | 454 |
| 393 // Start the test user session; ShelfWindowWatcher will find the open window. | 455 // Start the test user session; ShelfWindowWatcher will find the open window. |
| 394 SetSessionStarted(true); | 456 SetSessionStarted(true); |
| 395 EXPECT_EQ(2, model->item_count()); | 457 EXPECT_EQ(2, model->item_count()); |
| 396 } | 458 } |
| 397 | 459 |
| 398 } // namespace | 460 } // namespace |
| 399 } // namespace ash | 461 } // namespace ash |
| OLD | NEW |