Chromium Code Reviews| Index: ash/system/toast/toast_manager_unittest.cc |
| diff --git a/ash/system/toast/toast_manager_unittest.cc b/ash/system/toast/toast_manager_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c1f435f170a4e0a6f2953e95d9ba64c31ee85e93 |
| --- /dev/null |
| +++ b/ash/system/toast/toast_manager_unittest.cc |
| @@ -0,0 +1,241 @@ |
| +// Copyright 2016 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/display/display_manager.h" |
| +#include "ash/screen_util.h" |
| +#include "ash/shelf/shelf.h" |
| +#include "ash/shelf/shelf_layout_manager.h" |
| +#include "ash/shell.h" |
| +#include "ash/system/toast/toast_manager.h" |
| +#include "ash/test/ash_test_base.h" |
| +#include "base/run_loop.h" |
| + |
| +namespace ash { |
| + |
| +// Long duration so the timeout doesn't occur. |
| +const int64_t kLongLongDuration = INT64_MAX; |
| + |
| +class DummyEvent : public ui::Event { |
| + public: |
| + DummyEvent() : Event(ui::ET_UNKNOWN, base::TimeDelta(), 0) {} |
| + ~DummyEvent() override {} |
| +}; |
| + |
| +class ToastManagerTest : public test::AshTestBase { |
| + public: |
| + ToastManagerTest() {} |
| + ~ToastManagerTest() override {} |
| + |
| + private: |
| + void SetUp() override { |
| + test::AshTestBase::SetUp(); |
| + |
| + manager_ = Shell::GetInstance()->toast_manager(); |
| + |
| + manager_->ResetToastIdForTesting(); |
| + EXPECT_EQ(0, GetToastId()); |
| + } |
| + |
| + protected: |
| + ToastManager* manager() const { return manager_; } |
|
oshima
2016/03/15 19:03:19
remove const
yoshiki
2016/03/17 07:59:11
Done.
|
| + |
| + int GetToastId() const { return manager_->toast_id_for_testing(); } |
| + |
| + ToastOverlay* GetCurrentOverlay() { |
| + return manager_->GetCurrentOverlayForTesting(); |
| + } |
| + |
| + views::Widget* GetCurrentWidget() { |
| + ToastOverlay* overlay = GetCurrentOverlay(); |
| + return overlay ? overlay->widget_for_testing() : nullptr; |
| + } |
| + |
| + std::string GetCurrentText() { |
| + ToastOverlay* overlay = GetCurrentOverlay(); |
| + return overlay ? overlay->text_ : ""; |
|
oshima
2016/03/15 19:03:19
std::string()
yoshiki
2016/03/17 07:59:10
Done.
|
| + } |
| + |
| + void ClickDismissButton() { |
| + ToastOverlay* overlay = GetCurrentOverlay(); |
| + if (overlay) |
| + overlay->ClickDismissButtonForTesting(DummyEvent()); |
| + } |
| + |
| + void SetShelfAlignment(ShelfAlignment alignment) { |
| + Shelf::ForPrimaryDisplay()->shelf_layout_manager()->SetAlignment(alignment); |
| + } |
| + |
| + void SetShelfState(ShelfVisibilityState state) { |
| + Shelf::ForPrimaryDisplay()->shelf_layout_manager()->SetState(state); |
| + } |
| + |
| + void SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior) { |
| + Shelf::ForPrimaryDisplay()->shelf_layout_manager()->SetAutoHideBehavior( |
| + behavior); |
| + } |
| + |
| + private: |
| + ToastManager* manager_ = nullptr; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ToastManagerTest); |
| +}; |
| + |
| +TEST_F(ToastManagerTest, ShowAndCloseAutomatically) { |
| + manager()->Show("DUMMY", 10); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + EXPECT_EQ(1, GetToastId()); |
| + |
| + while (GetCurrentOverlay() != nullptr) |
| + base::RunLoop().RunUntilIdle(); |
| +} |
| + |
| +TEST_F(ToastManagerTest, ShowAndCloseManually) { |
| + manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + EXPECT_EQ(1, GetToastId()); |
| + |
| + ClickDismissButton(); |
| + |
| + while (GetCurrentOverlay() != nullptr) |
| + base::RunLoop().RunUntilIdle(); |
| +} |
| + |
| +TEST_F(ToastManagerTest, QueueMessage) { |
| + manager()->Show("DUMMY1", 10); |
| + manager()->Show("DUMMY2", 10); |
| + manager()->Show("DUMMY3", 10); |
| + |
| + while (GetToastId() != 1) |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + EXPECT_EQ("DUMMY1", GetCurrentText()); |
| + |
| + while (GetToastId() != 2) |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + EXPECT_EQ("DUMMY2", GetCurrentText()); |
| + |
| + while (GetToastId() != 3) |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + EXPECT_EQ("DUMMY3", GetCurrentText()); |
| +} |
| + |
| +TEST_F(ToastManagerTest, PositionWithVisibleBottomShelf) { |
| + ShelfLayoutManager* shelf = |
| + Shelf::ForPrimaryDisplay()->shelf_layout_manager(); |
| + SetShelfState(ash::SHELF_VISIBLE); |
| + SetShelfAlignment(ash::SHELF_ALIGNMENT_BOTTOM); |
| + |
| + manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(1, GetToastId()); |
| + |
| + gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen(); |
| + gfx::Rect shelf_bounds = shelf->GetIdealBounds(); |
| + gfx::Rect root_bounds = |
| + ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); |
| + |
| + EXPECT_FALSE(toast_bounds.Intersects(shelf_bounds)); |
| + EXPECT_EQ(shelf_bounds.y() - 5, toast_bounds.bottom()); |
| + EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1); |
| + EXPECT_EQ(root_bounds.bottom() - shelf_bounds.height() - 5, |
| + toast_bounds.bottom()); |
| +} |
| + |
| +TEST_F(ToastManagerTest, PositionWithAutoHiddenBottomShelf) { |
| + scoped_ptr<aura::Window> window( |
| + CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 3, 4))); |
| + |
| + ShelfLayoutManager* shelf = |
| + Shelf::ForPrimaryDisplay()->shelf_layout_manager(); |
| + SetShelfAlignment(ash::SHELF_ALIGNMENT_BOTTOM); |
| + SetShelfAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS); |
| + shelf->LayoutShelf(); |
| + EXPECT_EQ(SHELF_AUTO_HIDE_HIDDEN, shelf->auto_hide_state()); |
| + |
| + manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(1, GetToastId()); |
| + |
| + gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen(); |
| + gfx::Rect root_bounds = |
| + ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); |
| + |
| + EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1); |
| + EXPECT_EQ(root_bounds.bottom() - ShelfLayoutManager::kAutoHideSize - 5, |
| + toast_bounds.bottom()); |
| +} |
| + |
| +TEST_F(ToastManagerTest, PositionWithHiddenBottomShelf) { |
| + SetShelfAutoHideBehavior(SHELF_AUTO_HIDE_ALWAYS_HIDDEN); |
| + SetShelfAlignment(ash::SHELF_ALIGNMENT_BOTTOM); |
| + SetShelfState(ash::SHELF_HIDDEN); |
| + |
| + manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(1, GetToastId()); |
| + |
| + gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen(); |
| + gfx::Rect root_bounds = |
| + ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); |
| + |
| + EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1); |
| + EXPECT_EQ(root_bounds.bottom() - 5, toast_bounds.bottom()); |
| +} |
| + |
| +TEST_F(ToastManagerTest, PositionWithVisibleLeftShelf) { |
| + ShelfLayoutManager* shelf = |
| + Shelf::ForPrimaryDisplay()->shelf_layout_manager(); |
| + SetShelfState(ash::SHELF_VISIBLE); |
| + SetShelfAlignment(ash::SHELF_ALIGNMENT_LEFT); |
| + |
| + manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(1, GetToastId()); |
| + |
| + gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen(); |
| + gfx::Rect shelf_bounds = shelf->GetIdealBounds(); |
| + gfx::Rect root_bounds = |
| + ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); |
| + |
| + EXPECT_FALSE(toast_bounds.Intersects(shelf_bounds)); |
| + EXPECT_EQ(root_bounds.bottom() - 5, toast_bounds.bottom()); |
| + EXPECT_EQ( |
| + shelf_bounds.right() + (root_bounds.width() - shelf_bounds.width()) / 2, |
| + toast_bounds.CenterPoint().x()); |
| +} |
| + |
| +TEST_F(ToastManagerTest, PositionWithUnifiedDesktop) { |
| + if (!SupportsMultipleDisplays()) |
| + return; |
| + DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
| + display_manager->SetUnifiedDesktopEnabled(true); |
| + UpdateDisplay("1000x500,0+600-100x500"); |
| + |
| + ShelfLayoutManager* shelf = |
| + Shelf::ForPrimaryDisplay()->shelf_layout_manager(); |
| + SetShelfState(ash::SHELF_VISIBLE); |
| + SetShelfAlignment(ash::SHELF_ALIGNMENT_BOTTOM); |
| + |
| + manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_EQ(1, GetToastId()); |
| + |
| + gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen(); |
| + gfx::Rect shelf_bounds = shelf->GetIdealBounds(); |
| + gfx::Rect root_bounds = |
| + ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); |
| + |
| + EXPECT_TRUE(root_bounds.Contains(toast_bounds)); |
| + EXPECT_FALSE(toast_bounds.Intersects(shelf_bounds)); |
| + EXPECT_EQ(shelf_bounds.y() - 5, toast_bounds.bottom()); |
| + EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1); |
| + EXPECT_EQ(root_bounds.bottom() - shelf_bounds.height() - 5, |
| + toast_bounds.bottom()); |
| +} |
| + |
| +} // namespace ash |