OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ash/display/resolution_notification_controller.h" |
| 6 |
| 7 #include "ash/display/display_manager.h" |
| 8 #include "ash/screen_ash.h" |
| 9 #include "ash/shell.h" |
| 10 #include "ash/test/ash_test_base.h" |
| 11 #include "base/bind.h" |
| 12 #include "ui/gfx/size.h" |
| 13 #include "ui/message_center/message_center.h" |
| 14 |
| 15 namespace ash { |
| 16 namespace internal { |
| 17 |
| 18 class ResolutionNotificationControllerTest : public ash::test::AshTestBase { |
| 19 public: |
| 20 ResolutionNotificationControllerTest() |
| 21 : accept_count_(0) { |
| 22 } |
| 23 |
| 24 virtual ~ResolutionNotificationControllerTest() {} |
| 25 |
| 26 protected: |
| 27 virtual void SetUp() OVERRIDE { |
| 28 ash::test::AshTestBase::SetUp(); |
| 29 ResolutionNotificationController::SuppressTimerForTest(); |
| 30 } |
| 31 |
| 32 void SetDisplayResolutionAndNotify(const gfx::Display& display, |
| 33 const gfx::Size& new_resolution) { |
| 34 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
| 35 const DisplayInfo& info = display_manager->GetDisplayInfo(display.id()); |
| 36 Shell::GetInstance()->resolution_notification_controller()-> |
| 37 SetDisplayResolutionAndNotify( |
| 38 display.id(), |
| 39 info.size_in_pixel(), |
| 40 new_resolution, |
| 41 base::Bind(&ResolutionNotificationControllerTest::OnAccepted, |
| 42 base::Unretained(this))); |
| 43 |
| 44 // OnConfigurationChanged event won't be emitted in the test environment, |
| 45 // so invoke UpdateDisplay() to emit that event explicitly. |
| 46 std::string display_spec; |
| 47 for (size_t i = 0; i < display_manager->GetNumDisplays(); ++i) { |
| 48 if (i > 0) |
| 49 display_spec.append(","); |
| 50 int64 id = display_manager->GetDisplayAt(i).id(); |
| 51 gfx::Size size = (display.id() == id) ? |
| 52 new_resolution : display_manager->GetDisplayInfo(id).size_in_pixel(); |
| 53 display_spec.append(size.ToString()); |
| 54 } |
| 55 UpdateDisplay(display_spec); |
| 56 RunAllPendingInMessageLoop(); |
| 57 } |
| 58 |
| 59 void ClickOnNotification() { |
| 60 message_center::MessageCenter::Get()->ClickOnNotification( |
| 61 ResolutionNotificationController::kNotificationId); |
| 62 } |
| 63 |
| 64 void ClickOnNotificationButton(int index) { |
| 65 message_center::MessageCenter::Get()->ClickOnNotificationButton( |
| 66 ResolutionNotificationController::kNotificationId, index); |
| 67 } |
| 68 |
| 69 bool IsNotificationVisible() { |
| 70 return message_center::MessageCenter::Get()->HasNotification( |
| 71 ResolutionNotificationController::kNotificationId); |
| 72 } |
| 73 |
| 74 void TickTimer() { |
| 75 controller()->OnTimerTick(); |
| 76 } |
| 77 |
| 78 ResolutionNotificationController* controller() { |
| 79 return Shell::GetInstance()->resolution_notification_controller(); |
| 80 } |
| 81 |
| 82 int accept_count() const { |
| 83 return accept_count_; |
| 84 } |
| 85 |
| 86 private: |
| 87 void OnAccepted() { |
| 88 EXPECT_FALSE(controller()->DoesNotificationTimeout()); |
| 89 accept_count_++; |
| 90 } |
| 91 |
| 92 int accept_count_; |
| 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(ResolutionNotificationControllerTest); |
| 95 }; |
| 96 |
| 97 // Basic behaviors and verifies it doesn't cause crashes. |
| 98 TEST_F(ResolutionNotificationControllerTest, Basic) { |
| 99 if (!SupportsMultipleDisplays()) |
| 100 return; |
| 101 |
| 102 UpdateDisplay("100x100,150x150"); |
| 103 int64 id2 = ash::ScreenAsh::GetSecondaryDisplay().id(); |
| 104 ash::internal::DisplayManager* display_manager = |
| 105 ash::Shell::GetInstance()->display_manager(); |
| 106 ASSERT_EQ(0, accept_count()); |
| 107 EXPECT_FALSE(IsNotificationVisible()); |
| 108 |
| 109 // Changes the resolution and apply the result. |
| 110 SetDisplayResolutionAndNotify( |
| 111 ScreenAsh::GetSecondaryDisplay(), gfx::Size(200, 200)); |
| 112 EXPECT_TRUE(IsNotificationVisible()); |
| 113 EXPECT_FALSE(controller()->DoesNotificationTimeout()); |
| 114 gfx::Size resolution; |
| 115 EXPECT_TRUE( |
| 116 display_manager->GetSelectedResolutionForDisplayId(id2, &resolution)); |
| 117 EXPECT_EQ("200x200", resolution.ToString()); |
| 118 |
| 119 // Click the revert button, which reverts the resolution. |
| 120 ClickOnNotificationButton(0); |
| 121 RunAllPendingInMessageLoop(); |
| 122 EXPECT_FALSE(IsNotificationVisible()); |
| 123 EXPECT_EQ(0, accept_count()); |
| 124 EXPECT_TRUE( |
| 125 display_manager->GetSelectedResolutionForDisplayId(id2, &resolution)); |
| 126 EXPECT_EQ("150x150", resolution.ToString()); |
| 127 } |
| 128 |
| 129 TEST_F(ResolutionNotificationControllerTest, ClickMeansAccept) { |
| 130 if (!SupportsMultipleDisplays()) |
| 131 return; |
| 132 |
| 133 UpdateDisplay("100x100,150x150"); |
| 134 int64 id2 = ash::ScreenAsh::GetSecondaryDisplay().id(); |
| 135 ash::internal::DisplayManager* display_manager = |
| 136 ash::Shell::GetInstance()->display_manager(); |
| 137 ASSERT_EQ(0, accept_count()); |
| 138 EXPECT_FALSE(IsNotificationVisible()); |
| 139 |
| 140 // Changes the resolution and apply the result. |
| 141 SetDisplayResolutionAndNotify( |
| 142 ScreenAsh::GetSecondaryDisplay(), gfx::Size(200, 200)); |
| 143 EXPECT_TRUE(IsNotificationVisible()); |
| 144 EXPECT_FALSE(controller()->DoesNotificationTimeout()); |
| 145 gfx::Size resolution; |
| 146 EXPECT_TRUE( |
| 147 display_manager->GetSelectedResolutionForDisplayId(id2, &resolution)); |
| 148 EXPECT_EQ("200x200", resolution.ToString()); |
| 149 |
| 150 // Click the revert button, which reverts the resolution. |
| 151 ClickOnNotification(); |
| 152 RunAllPendingInMessageLoop(); |
| 153 EXPECT_FALSE(IsNotificationVisible()); |
| 154 EXPECT_EQ(1, accept_count()); |
| 155 EXPECT_TRUE( |
| 156 display_manager->GetSelectedResolutionForDisplayId(id2, &resolution)); |
| 157 EXPECT_EQ("200x200", resolution.ToString()); |
| 158 } |
| 159 |
| 160 TEST_F(ResolutionNotificationControllerTest, AcceptButton) { |
| 161 if (!SupportsMultipleDisplays()) |
| 162 return; |
| 163 |
| 164 ash::internal::DisplayManager* display_manager = |
| 165 ash::Shell::GetInstance()->display_manager(); |
| 166 |
| 167 UpdateDisplay("100x100"); |
| 168 const gfx::Display& display = ash::Shell::GetScreen()->GetPrimaryDisplay(); |
| 169 SetDisplayResolutionAndNotify(display, gfx::Size(200, 200)); |
| 170 EXPECT_TRUE(IsNotificationVisible()); |
| 171 |
| 172 // If there's a single display only, it will have timeout and the first button |
| 173 // becomes accept. |
| 174 EXPECT_TRUE(controller()->DoesNotificationTimeout()); |
| 175 ClickOnNotificationButton(0); |
| 176 EXPECT_FALSE(IsNotificationVisible()); |
| 177 EXPECT_EQ(1, accept_count()); |
| 178 gfx::Size resolution; |
| 179 EXPECT_TRUE(display_manager->GetSelectedResolutionForDisplayId( |
| 180 display.id(), &resolution)); |
| 181 EXPECT_EQ("200x200", resolution.ToString()); |
| 182 |
| 183 // In that case the second button is revert. |
| 184 UpdateDisplay("100x100"); |
| 185 SetDisplayResolutionAndNotify(display, gfx::Size(200, 200)); |
| 186 EXPECT_TRUE(IsNotificationVisible()); |
| 187 |
| 188 EXPECT_TRUE(controller()->DoesNotificationTimeout()); |
| 189 ClickOnNotificationButton(1); |
| 190 EXPECT_FALSE(IsNotificationVisible()); |
| 191 EXPECT_EQ(1, accept_count()); |
| 192 EXPECT_TRUE(display_manager->GetSelectedResolutionForDisplayId( |
| 193 display.id(), &resolution)); |
| 194 EXPECT_EQ("100x100", resolution.ToString()); |
| 195 } |
| 196 |
| 197 TEST_F(ResolutionNotificationControllerTest, Timeout) { |
| 198 if (!SupportsMultipleDisplays()) |
| 199 return; |
| 200 |
| 201 UpdateDisplay("100x100"); |
| 202 const gfx::Display& display = ash::Shell::GetScreen()->GetPrimaryDisplay(); |
| 203 SetDisplayResolutionAndNotify(display, gfx::Size(200, 200)); |
| 204 |
| 205 for (int i = 0; i < ResolutionNotificationController::kTimeoutInSec; ++i) { |
| 206 EXPECT_TRUE(IsNotificationVisible()) << "notification is closed after " |
| 207 << i << "-th timer tick"; |
| 208 TickTimer(); |
| 209 RunAllPendingInMessageLoop(); |
| 210 } |
| 211 EXPECT_FALSE(IsNotificationVisible()); |
| 212 EXPECT_EQ(0, accept_count()); |
| 213 gfx::Size resolution; |
| 214 ash::internal::DisplayManager* display_manager = |
| 215 ash::Shell::GetInstance()->display_manager(); |
| 216 EXPECT_TRUE(display_manager->GetSelectedResolutionForDisplayId( |
| 217 display.id(), &resolution)); |
| 218 EXPECT_EQ("100x100", resolution.ToString()); |
| 219 } |
| 220 |
| 221 TEST_F(ResolutionNotificationControllerTest, DisplayDisconnected) { |
| 222 if (!SupportsMultipleDisplays()) |
| 223 return; |
| 224 |
| 225 UpdateDisplay("100x100,150x150"); |
| 226 int64 id2 = ash::ScreenAsh::GetSecondaryDisplay().id(); |
| 227 ash::internal::DisplayManager* display_manager = |
| 228 ash::Shell::GetInstance()->display_manager(); |
| 229 SetDisplayResolutionAndNotify( |
| 230 ScreenAsh::GetSecondaryDisplay(), gfx::Size(200, 200)); |
| 231 ASSERT_TRUE(IsNotificationVisible()); |
| 232 |
| 233 // Disconnects the secondary display and verifies it doesn't cause crashes. |
| 234 UpdateDisplay("100x100"); |
| 235 RunAllPendingInMessageLoop(); |
| 236 EXPECT_FALSE(IsNotificationVisible()); |
| 237 EXPECT_EQ(0, accept_count()); |
| 238 gfx::Size resolution; |
| 239 EXPECT_TRUE( |
| 240 display_manager->GetSelectedResolutionForDisplayId(id2, &resolution)); |
| 241 EXPECT_EQ("150x150", resolution.ToString()); |
| 242 } |
| 243 |
| 244 TEST_F(ResolutionNotificationControllerTest, MultipleResolutionChange) { |
| 245 if (!SupportsMultipleDisplays()) |
| 246 return; |
| 247 |
| 248 UpdateDisplay("100x100,150x150"); |
| 249 int64 id2 = ash::ScreenAsh::GetSecondaryDisplay().id(); |
| 250 ash::internal::DisplayManager* display_manager = |
| 251 ash::Shell::GetInstance()->display_manager(); |
| 252 |
| 253 SetDisplayResolutionAndNotify( |
| 254 ScreenAsh::GetSecondaryDisplay(), gfx::Size(200, 200)); |
| 255 EXPECT_TRUE(IsNotificationVisible()); |
| 256 EXPECT_FALSE(controller()->DoesNotificationTimeout()); |
| 257 gfx::Size resolution; |
| 258 EXPECT_TRUE( |
| 259 display_manager->GetSelectedResolutionForDisplayId(id2, &resolution)); |
| 260 EXPECT_EQ("200x200", resolution.ToString()); |
| 261 |
| 262 // Invokes SetDisplayResolutionAndNotify during the previous notification is |
| 263 // visible. |
| 264 SetDisplayResolutionAndNotify( |
| 265 ScreenAsh::GetSecondaryDisplay(), gfx::Size(250, 250)); |
| 266 EXPECT_TRUE( |
| 267 display_manager->GetSelectedResolutionForDisplayId(id2, &resolution)); |
| 268 EXPECT_EQ("250x250", resolution.ToString()); |
| 269 |
| 270 // Then, click the revert button. Although |old_resolution| for the second |
| 271 // SetDisplayResolutionAndNotify is 200x200, it should revert to the original |
| 272 // size 150x150. |
| 273 ClickOnNotificationButton(0); |
| 274 RunAllPendingInMessageLoop(); |
| 275 EXPECT_FALSE(IsNotificationVisible()); |
| 276 EXPECT_EQ(0, accept_count()); |
| 277 EXPECT_TRUE( |
| 278 display_manager->GetSelectedResolutionForDisplayId(id2, &resolution)); |
| 279 EXPECT_EQ("150x150", resolution.ToString()); |
| 280 } |
| 281 |
| 282 } // namespace internal |
| 283 } // namespace ash |
OLD | NEW |