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