Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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/display/display_manager.h" | |
| 6 #include "ash/screen_util.h" | |
| 7 #include "ash/shelf/shelf.h" | |
| 8 #include "ash/shelf/shelf_layout_manager.h" | |
| 9 #include "ash/shell.h" | |
| 10 #include "ash/system/toast/toast_manager.h" | |
| 11 #include "ash/test/ash_test_base.h" | |
| 12 #include "base/run_loop.h" | |
| 13 | |
| 14 namespace ash { | |
| 15 | |
| 16 // Long duration so the timeout doesn't occur. | |
| 17 const int64_t kLongLongDuration = INT64_MAX; | |
| 18 | |
| 19 class DummyEvent : public ui::Event { | |
| 20 public: | |
| 21 DummyEvent() : Event(ui::ET_UNKNOWN, base::TimeDelta(), 0) {} | |
| 22 ~DummyEvent() override {} | |
| 23 }; | |
| 24 | |
| 25 class ToastManagerTest : public test::AshTestBase { | |
| 26 public: | |
| 27 ToastManagerTest() {} | |
| 28 ~ToastManagerTest() override {} | |
| 29 | |
| 30 private: | |
| 31 void SetUp() override { | |
| 32 test::AshTestBase::SetUp(); | |
| 33 | |
| 34 manager_ = Shell::GetInstance()->toast_manager(); | |
| 35 | |
| 36 manager_->ResetToastIdForTesting(); | |
| 37 EXPECT_EQ(0, GetToastId()); | |
| 38 } | |
| 39 | |
| 40 protected: | |
| 41 ToastManager* manager() const { return manager_; } | |
|
oshima
2016/03/15 19:03:19
remove const
yoshiki
2016/03/17 07:59:11
Done.
| |
| 42 | |
| 43 int GetToastId() const { return manager_->toast_id_for_testing(); } | |
| 44 | |
| 45 ToastOverlay* GetCurrentOverlay() { | |
| 46 return manager_->GetCurrentOverlayForTesting(); | |
| 47 } | |
| 48 | |
| 49 views::Widget* GetCurrentWidget() { | |
| 50 ToastOverlay* overlay = GetCurrentOverlay(); | |
| 51 return overlay ? overlay->widget_for_testing() : nullptr; | |
| 52 } | |
| 53 | |
| 54 std::string GetCurrentText() { | |
| 55 ToastOverlay* overlay = GetCurrentOverlay(); | |
| 56 return overlay ? overlay->text_ : ""; | |
|
oshima
2016/03/15 19:03:19
std::string()
yoshiki
2016/03/17 07:59:10
Done.
| |
| 57 } | |
| 58 | |
| 59 void ClickDismissButton() { | |
| 60 ToastOverlay* overlay = GetCurrentOverlay(); | |
| 61 if (overlay) | |
| 62 overlay->ClickDismissButtonForTesting(DummyEvent()); | |
| 63 } | |
| 64 | |
| 65 void SetShelfAlignment(ShelfAlignment alignment) { | |
| 66 Shelf::ForPrimaryDisplay()->shelf_layout_manager()->SetAlignment(alignment); | |
| 67 } | |
| 68 | |
| 69 void SetShelfState(ShelfVisibilityState state) { | |
| 70 Shelf::ForPrimaryDisplay()->shelf_layout_manager()->SetState(state); | |
| 71 } | |
| 72 | |
| 73 void SetShelfAutoHideBehavior(ShelfAutoHideBehavior behavior) { | |
| 74 Shelf::ForPrimaryDisplay()->shelf_layout_manager()->SetAutoHideBehavior( | |
| 75 behavior); | |
| 76 } | |
| 77 | |
| 78 private: | |
| 79 ToastManager* manager_ = nullptr; | |
| 80 | |
| 81 DISALLOW_COPY_AND_ASSIGN(ToastManagerTest); | |
| 82 }; | |
| 83 | |
| 84 TEST_F(ToastManagerTest, ShowAndCloseAutomatically) { | |
| 85 manager()->Show("DUMMY", 10); | |
| 86 base::RunLoop().RunUntilIdle(); | |
| 87 | |
| 88 EXPECT_EQ(1, GetToastId()); | |
| 89 | |
| 90 while (GetCurrentOverlay() != nullptr) | |
| 91 base::RunLoop().RunUntilIdle(); | |
| 92 } | |
| 93 | |
| 94 TEST_F(ToastManagerTest, ShowAndCloseManually) { | |
| 95 manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); | |
| 96 base::RunLoop().RunUntilIdle(); | |
| 97 | |
| 98 EXPECT_EQ(1, GetToastId()); | |
| 99 | |
| 100 ClickDismissButton(); | |
| 101 | |
| 102 while (GetCurrentOverlay() != nullptr) | |
| 103 base::RunLoop().RunUntilIdle(); | |
| 104 } | |
| 105 | |
| 106 TEST_F(ToastManagerTest, QueueMessage) { | |
| 107 manager()->Show("DUMMY1", 10); | |
| 108 manager()->Show("DUMMY2", 10); | |
| 109 manager()->Show("DUMMY3", 10); | |
| 110 | |
| 111 while (GetToastId() != 1) | |
| 112 base::RunLoop().RunUntilIdle(); | |
| 113 | |
| 114 EXPECT_EQ("DUMMY1", GetCurrentText()); | |
| 115 | |
| 116 while (GetToastId() != 2) | |
| 117 base::RunLoop().RunUntilIdle(); | |
| 118 | |
| 119 EXPECT_EQ("DUMMY2", GetCurrentText()); | |
| 120 | |
| 121 while (GetToastId() != 3) | |
| 122 base::RunLoop().RunUntilIdle(); | |
| 123 | |
| 124 EXPECT_EQ("DUMMY3", GetCurrentText()); | |
| 125 } | |
| 126 | |
| 127 TEST_F(ToastManagerTest, PositionWithVisibleBottomShelf) { | |
| 128 ShelfLayoutManager* shelf = | |
| 129 Shelf::ForPrimaryDisplay()->shelf_layout_manager(); | |
| 130 SetShelfState(ash::SHELF_VISIBLE); | |
| 131 SetShelfAlignment(ash::SHELF_ALIGNMENT_BOTTOM); | |
| 132 | |
| 133 manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); | |
| 134 base::RunLoop().RunUntilIdle(); | |
| 135 EXPECT_EQ(1, GetToastId()); | |
| 136 | |
| 137 gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen(); | |
| 138 gfx::Rect shelf_bounds = shelf->GetIdealBounds(); | |
| 139 gfx::Rect root_bounds = | |
| 140 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); | |
| 141 | |
| 142 EXPECT_FALSE(toast_bounds.Intersects(shelf_bounds)); | |
| 143 EXPECT_EQ(shelf_bounds.y() - 5, toast_bounds.bottom()); | |
| 144 EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1); | |
| 145 EXPECT_EQ(root_bounds.bottom() - shelf_bounds.height() - 5, | |
| 146 toast_bounds.bottom()); | |
| 147 } | |
| 148 | |
| 149 TEST_F(ToastManagerTest, PositionWithAutoHiddenBottomShelf) { | |
| 150 scoped_ptr<aura::Window> window( | |
| 151 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 3, 4))); | |
| 152 | |
| 153 ShelfLayoutManager* shelf = | |
| 154 Shelf::ForPrimaryDisplay()->shelf_layout_manager(); | |
| 155 SetShelfAlignment(ash::SHELF_ALIGNMENT_BOTTOM); | |
| 156 SetShelfAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS); | |
| 157 shelf->LayoutShelf(); | |
| 158 EXPECT_EQ(SHELF_AUTO_HIDE_HIDDEN, shelf->auto_hide_state()); | |
| 159 | |
| 160 manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); | |
| 161 base::RunLoop().RunUntilIdle(); | |
| 162 EXPECT_EQ(1, GetToastId()); | |
| 163 | |
| 164 gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen(); | |
| 165 gfx::Rect root_bounds = | |
| 166 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); | |
| 167 | |
| 168 EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1); | |
| 169 EXPECT_EQ(root_bounds.bottom() - ShelfLayoutManager::kAutoHideSize - 5, | |
| 170 toast_bounds.bottom()); | |
| 171 } | |
| 172 | |
| 173 TEST_F(ToastManagerTest, PositionWithHiddenBottomShelf) { | |
| 174 SetShelfAutoHideBehavior(SHELF_AUTO_HIDE_ALWAYS_HIDDEN); | |
| 175 SetShelfAlignment(ash::SHELF_ALIGNMENT_BOTTOM); | |
| 176 SetShelfState(ash::SHELF_HIDDEN); | |
| 177 | |
| 178 manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); | |
| 179 base::RunLoop().RunUntilIdle(); | |
| 180 EXPECT_EQ(1, GetToastId()); | |
| 181 | |
| 182 gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen(); | |
| 183 gfx::Rect root_bounds = | |
| 184 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); | |
| 185 | |
| 186 EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1); | |
| 187 EXPECT_EQ(root_bounds.bottom() - 5, toast_bounds.bottom()); | |
| 188 } | |
| 189 | |
| 190 TEST_F(ToastManagerTest, PositionWithVisibleLeftShelf) { | |
| 191 ShelfLayoutManager* shelf = | |
| 192 Shelf::ForPrimaryDisplay()->shelf_layout_manager(); | |
| 193 SetShelfState(ash::SHELF_VISIBLE); | |
| 194 SetShelfAlignment(ash::SHELF_ALIGNMENT_LEFT); | |
| 195 | |
| 196 manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); | |
| 197 base::RunLoop().RunUntilIdle(); | |
| 198 EXPECT_EQ(1, GetToastId()); | |
| 199 | |
| 200 gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen(); | |
| 201 gfx::Rect shelf_bounds = shelf->GetIdealBounds(); | |
| 202 gfx::Rect root_bounds = | |
| 203 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); | |
| 204 | |
| 205 EXPECT_FALSE(toast_bounds.Intersects(shelf_bounds)); | |
| 206 EXPECT_EQ(root_bounds.bottom() - 5, toast_bounds.bottom()); | |
| 207 EXPECT_EQ( | |
| 208 shelf_bounds.right() + (root_bounds.width() - shelf_bounds.width()) / 2, | |
| 209 toast_bounds.CenterPoint().x()); | |
| 210 } | |
| 211 | |
| 212 TEST_F(ToastManagerTest, PositionWithUnifiedDesktop) { | |
| 213 if (!SupportsMultipleDisplays()) | |
| 214 return; | |
| 215 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); | |
| 216 display_manager->SetUnifiedDesktopEnabled(true); | |
| 217 UpdateDisplay("1000x500,0+600-100x500"); | |
| 218 | |
| 219 ShelfLayoutManager* shelf = | |
| 220 Shelf::ForPrimaryDisplay()->shelf_layout_manager(); | |
| 221 SetShelfState(ash::SHELF_VISIBLE); | |
| 222 SetShelfAlignment(ash::SHELF_ALIGNMENT_BOTTOM); | |
| 223 | |
| 224 manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); | |
| 225 base::RunLoop().RunUntilIdle(); | |
| 226 EXPECT_EQ(1, GetToastId()); | |
| 227 | |
| 228 gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen(); | |
| 229 gfx::Rect shelf_bounds = shelf->GetIdealBounds(); | |
| 230 gfx::Rect root_bounds = | |
| 231 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); | |
| 232 | |
| 233 EXPECT_TRUE(root_bounds.Contains(toast_bounds)); | |
| 234 EXPECT_FALSE(toast_bounds.Intersects(shelf_bounds)); | |
| 235 EXPECT_EQ(shelf_bounds.y() - 5, toast_bounds.bottom()); | |
| 236 EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1); | |
| 237 EXPECT_EQ(root_bounds.bottom() - shelf_bounds.height() - 5, | |
| 238 toast_bounds.bottom()); | |
| 239 } | |
| 240 | |
| 241 } // namespace ash | |
| OLD | NEW |