| 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() { return manager_; } |
| 42 |
| 43 int GetToastId() { 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_ : std::string(); |
| 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 root_bounds = |
| 139 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); |
| 140 |
| 141 EXPECT_TRUE(toast_bounds.Intersects(shelf->user_work_area_bounds())); |
| 142 EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1); |
| 143 |
| 144 if (SupportsHostWindowResize()) { |
| 145 // If host resize is not supported, ShelfLayoutManager::GetIdealBounds() |
| 146 // doesn't return correct value. |
| 147 gfx::Rect shelf_bounds = shelf->GetIdealBounds(); |
| 148 EXPECT_FALSE(toast_bounds.Intersects(shelf_bounds)); |
| 149 EXPECT_EQ(shelf_bounds.y() - 5, toast_bounds.bottom()); |
| 150 EXPECT_EQ(root_bounds.bottom() - shelf_bounds.height() - 5, |
| 151 toast_bounds.bottom()); |
| 152 } |
| 153 } |
| 154 |
| 155 TEST_F(ToastManagerTest, PositionWithAutoHiddenBottomShelf) { |
| 156 scoped_ptr<aura::Window> window( |
| 157 CreateTestWindowInShellWithBounds(gfx::Rect(1, 2, 3, 4))); |
| 158 |
| 159 ShelfLayoutManager* shelf = |
| 160 Shelf::ForPrimaryDisplay()->shelf_layout_manager(); |
| 161 SetShelfAlignment(ash::SHELF_ALIGNMENT_BOTTOM); |
| 162 SetShelfAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS); |
| 163 shelf->LayoutShelf(); |
| 164 EXPECT_EQ(SHELF_AUTO_HIDE_HIDDEN, shelf->auto_hide_state()); |
| 165 |
| 166 manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); |
| 167 base::RunLoop().RunUntilIdle(); |
| 168 EXPECT_EQ(1, GetToastId()); |
| 169 |
| 170 gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen(); |
| 171 gfx::Rect root_bounds = |
| 172 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); |
| 173 |
| 174 EXPECT_TRUE(toast_bounds.Intersects(shelf->user_work_area_bounds())); |
| 175 EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1); |
| 176 EXPECT_EQ(root_bounds.bottom() - ShelfLayoutManager::kAutoHideSize - 5, |
| 177 toast_bounds.bottom()); |
| 178 } |
| 179 |
| 180 TEST_F(ToastManagerTest, PositionWithHiddenBottomShelf) { |
| 181 ShelfLayoutManager* shelf = |
| 182 Shelf::ForPrimaryDisplay()->shelf_layout_manager(); |
| 183 SetShelfAutoHideBehavior(SHELF_AUTO_HIDE_ALWAYS_HIDDEN); |
| 184 SetShelfAlignment(ash::SHELF_ALIGNMENT_BOTTOM); |
| 185 SetShelfState(ash::SHELF_HIDDEN); |
| 186 |
| 187 manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); |
| 188 base::RunLoop().RunUntilIdle(); |
| 189 EXPECT_EQ(1, GetToastId()); |
| 190 |
| 191 gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen(); |
| 192 gfx::Rect root_bounds = |
| 193 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); |
| 194 |
| 195 EXPECT_TRUE(toast_bounds.Intersects(shelf->user_work_area_bounds())); |
| 196 EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1); |
| 197 EXPECT_EQ(root_bounds.bottom() - 5, toast_bounds.bottom()); |
| 198 } |
| 199 |
| 200 TEST_F(ToastManagerTest, PositionWithVisibleLeftShelf) { |
| 201 ShelfLayoutManager* shelf = |
| 202 Shelf::ForPrimaryDisplay()->shelf_layout_manager(); |
| 203 SetShelfState(ash::SHELF_VISIBLE); |
| 204 SetShelfAlignment(ash::SHELF_ALIGNMENT_LEFT); |
| 205 |
| 206 manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); |
| 207 base::RunLoop().RunUntilIdle(); |
| 208 EXPECT_EQ(1, GetToastId()); |
| 209 |
| 210 gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen(); |
| 211 gfx::Rect root_bounds = |
| 212 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); |
| 213 |
| 214 EXPECT_TRUE(toast_bounds.Intersects(shelf->user_work_area_bounds())); |
| 215 EXPECT_EQ(root_bounds.bottom() - 5, toast_bounds.bottom()); |
| 216 |
| 217 if (SupportsHostWindowResize()) { |
| 218 // If host resize is not supported, ShelfLayoutManager::GetIdealBounds() |
| 219 // doesn't return correct value. |
| 220 gfx::Rect shelf_bounds = shelf->GetIdealBounds(); |
| 221 EXPECT_FALSE(toast_bounds.Intersects(shelf_bounds)); |
| 222 EXPECT_EQ( |
| 223 shelf_bounds.right() + (root_bounds.width() - shelf_bounds.width()) / 2, |
| 224 toast_bounds.CenterPoint().x()); |
| 225 } |
| 226 } |
| 227 |
| 228 TEST_F(ToastManagerTest, PositionWithUnifiedDesktop) { |
| 229 if (!SupportsMultipleDisplays()) |
| 230 return; |
| 231 |
| 232 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
| 233 display_manager->SetUnifiedDesktopEnabled(true); |
| 234 UpdateDisplay("1000x500,0+600-100x500"); |
| 235 |
| 236 ShelfLayoutManager* shelf = |
| 237 Shelf::ForPrimaryDisplay()->shelf_layout_manager(); |
| 238 SetShelfState(ash::SHELF_VISIBLE); |
| 239 SetShelfAlignment(ash::SHELF_ALIGNMENT_BOTTOM); |
| 240 |
| 241 manager()->Show("DUMMY", kLongLongDuration /* prevent timeout */); |
| 242 base::RunLoop().RunUntilIdle(); |
| 243 EXPECT_EQ(1, GetToastId()); |
| 244 |
| 245 gfx::Rect toast_bounds = GetCurrentWidget()->GetWindowBoundsInScreen(); |
| 246 gfx::Rect root_bounds = |
| 247 ScreenUtil::GetShelfDisplayBoundsInRoot(Shell::GetPrimaryRootWindow()); |
| 248 |
| 249 EXPECT_TRUE(toast_bounds.Intersects(shelf->user_work_area_bounds())); |
| 250 EXPECT_TRUE(root_bounds.Contains(toast_bounds)); |
| 251 EXPECT_NEAR(root_bounds.CenterPoint().x(), toast_bounds.CenterPoint().x(), 1); |
| 252 |
| 253 if (SupportsHostWindowResize()) { |
| 254 // If host resize is not supported, ShelfLayoutManager::GetIdealBounds() |
| 255 // doesn't return correct value. |
| 256 gfx::Rect shelf_bounds = shelf->GetIdealBounds(); |
| 257 EXPECT_FALSE(toast_bounds.Intersects(shelf_bounds)); |
| 258 EXPECT_EQ(shelf_bounds.y() - 5, toast_bounds.bottom()); |
| 259 EXPECT_EQ(root_bounds.bottom() - shelf_bounds.height() - 5, |
| 260 toast_bounds.bottom()); |
| 261 } |
| 262 } |
| 263 |
| 264 } // namespace ash |
| OLD | NEW |