Chromium Code Reviews| 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 here invokes UpdateDisplay() to emit that event explicitly. | |
|
Daniel Erat
2013/08/09 15:50:24
nit: s/so here invokes/so invoke/
Jun Mukai
2013/08/09 16:49:16
Done.
| |
| 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 ClickOnNotificationButton(int index) { | |
| 60 message_center::MessageCenter::Get()->ClickOnNotificationButton( | |
| 61 ResolutionNotificationController::kNotificationId, index); | |
| 62 } | |
| 63 | |
| 64 bool IsNotificationVisible() { | |
| 65 return message_center::MessageCenter::Get()->HasNotification( | |
| 66 ResolutionNotificationController::kNotificationId); | |
| 67 } | |
| 68 | |
| 69 void TickTimer() { | |
| 70 controller()->OnTimerTick(); | |
| 71 } | |
| 72 | |
| 73 ResolutionNotificationController* controller() { | |
| 74 return Shell::GetInstance()->resolution_notification_controller(); | |
| 75 } | |
| 76 | |
| 77 int accept_count() const { | |
| 78 return accept_count_; | |
| 79 } | |
| 80 | |
| 81 private: | |
| 82 void OnAccepted() { | |
| 83 EXPECT_FALSE(controller()->DoesNotificationTimeout()); | |
| 84 accept_count_++; | |
| 85 } | |
| 86 | |
| 87 int accept_count_; | |
| 88 | |
| 89 DISALLOW_COPY_AND_ASSIGN(ResolutionNotificationControllerTest); | |
| 90 }; | |
| 91 | |
| 92 // Basic behaviors and verifies it doesn't cause crashes. | |
| 93 TEST_F(ResolutionNotificationControllerTest, Basic) { | |
| 94 if (!SupportsMultipleDisplays()) | |
| 95 return; | |
| 96 | |
| 97 UpdateDisplay("100x100,150x150"); | |
| 98 int64 id2 = ash::ScreenAsh::GetSecondaryDisplay().id(); | |
| 99 ash::internal::DisplayManager* display_manager = | |
| 100 ash::Shell::GetInstance()->display_manager(); | |
| 101 ASSERT_EQ(0, accept_count()); | |
| 102 EXPECT_FALSE(IsNotificationVisible()); | |
| 103 | |
| 104 // Changes the resolution and apply the result. | |
| 105 SetDisplayResolutionAndNotify( | |
| 106 ScreenAsh::GetSecondaryDisplay(), gfx::Size(200, 200)); | |
| 107 EXPECT_TRUE(IsNotificationVisible()); | |
| 108 EXPECT_FALSE(controller()->DoesNotificationTimeout()); | |
| 109 gfx::Size resolution; | |
| 110 EXPECT_TRUE( | |
| 111 display_manager->GetSelectedResolutionForDisplayId(id2, &resolution)); | |
| 112 EXPECT_EQ("200x200", resolution.ToString()); | |
| 113 | |
| 114 // Click the revert button, which reverts the resolution. | |
| 115 ClickOnNotificationButton(0); | |
| 116 RunAllPendingInMessageLoop(); | |
| 117 EXPECT_FALSE(IsNotificationVisible()); | |
| 118 EXPECT_EQ(0, accept_count()); | |
| 119 EXPECT_TRUE( | |
| 120 display_manager->GetSelectedResolutionForDisplayId(id2, &resolution)); | |
| 121 EXPECT_EQ("150x150", resolution.ToString()); | |
| 122 } | |
| 123 | |
| 124 TEST_F(ResolutionNotificationControllerTest, AcceptButton) { | |
| 125 if (!SupportsMultipleDisplays()) | |
| 126 return; | |
| 127 | |
| 128 ash::internal::DisplayManager* display_manager = | |
| 129 ash::Shell::GetInstance()->display_manager(); | |
| 130 | |
| 131 UpdateDisplay("100x100"); | |
| 132 const gfx::Display& display = ash::Shell::GetScreen()->GetPrimaryDisplay(); | |
| 133 SetDisplayResolutionAndNotify(display, gfx::Size(200, 200)); | |
| 134 EXPECT_TRUE(IsNotificationVisible()); | |
| 135 | |
| 136 // If there's a single display only, it will have timeout and the first button | |
| 137 // becomes accept. | |
| 138 EXPECT_TRUE(controller()->DoesNotificationTimeout()); | |
| 139 ClickOnNotificationButton(0); | |
| 140 EXPECT_FALSE(IsNotificationVisible()); | |
| 141 EXPECT_EQ(1, accept_count()); | |
| 142 gfx::Size resolution; | |
| 143 EXPECT_TRUE(display_manager->GetSelectedResolutionForDisplayId( | |
| 144 display.id(), &resolution)); | |
| 145 EXPECT_EQ("200x200", resolution.ToString()); | |
| 146 | |
| 147 // In that case the second button is revert. | |
| 148 UpdateDisplay("100x100"); | |
| 149 SetDisplayResolutionAndNotify(display, gfx::Size(200, 200)); | |
| 150 EXPECT_TRUE(IsNotificationVisible()); | |
| 151 | |
| 152 EXPECT_TRUE(controller()->DoesNotificationTimeout()); | |
| 153 ClickOnNotificationButton(1); | |
| 154 EXPECT_FALSE(IsNotificationVisible()); | |
| 155 EXPECT_EQ(1, accept_count()); | |
| 156 EXPECT_TRUE(display_manager->GetSelectedResolutionForDisplayId( | |
| 157 display.id(), &resolution)); | |
| 158 EXPECT_EQ("100x100", resolution.ToString()); | |
| 159 } | |
| 160 | |
| 161 TEST_F(ResolutionNotificationControllerTest, Timeout) { | |
| 162 if (!SupportsMultipleDisplays()) | |
| 163 return; | |
| 164 | |
| 165 UpdateDisplay("100x100"); | |
| 166 const gfx::Display& display = ash::Shell::GetScreen()->GetPrimaryDisplay(); | |
| 167 SetDisplayResolutionAndNotify(display, gfx::Size(200, 200)); | |
| 168 | |
| 169 for (int i = 0; i < ResolutionNotificationController::kTimeoutInSec; ++i) { | |
| 170 EXPECT_TRUE(IsNotificationVisible()) << "notification is closed after " | |
| 171 << i << "-th timer tick"; | |
| 172 TickTimer(); | |
| 173 RunAllPendingInMessageLoop(); | |
| 174 } | |
| 175 EXPECT_FALSE(IsNotificationVisible()); | |
| 176 EXPECT_EQ(0, accept_count()); | |
| 177 gfx::Size resolution; | |
| 178 ash::internal::DisplayManager* display_manager = | |
| 179 ash::Shell::GetInstance()->display_manager(); | |
| 180 EXPECT_TRUE(display_manager->GetSelectedResolutionForDisplayId( | |
| 181 display.id(), &resolution)); | |
| 182 EXPECT_EQ("100x100", resolution.ToString()); | |
| 183 } | |
| 184 | |
| 185 TEST_F(ResolutionNotificationControllerTest, DisplayDisconnected) { | |
| 186 if (!SupportsMultipleDisplays()) | |
| 187 return; | |
| 188 | |
| 189 UpdateDisplay("100x100,150x150"); | |
| 190 int64 id2 = ash::ScreenAsh::GetSecondaryDisplay().id(); | |
| 191 ash::internal::DisplayManager* display_manager = | |
| 192 ash::Shell::GetInstance()->display_manager(); | |
| 193 SetDisplayResolutionAndNotify( | |
| 194 ScreenAsh::GetSecondaryDisplay(), gfx::Size(200, 200)); | |
| 195 ASSERT_TRUE(IsNotificationVisible()); | |
| 196 | |
| 197 // Disconnects the secondary display and verifies it doesn't cause crashes. | |
| 198 UpdateDisplay("100x100"); | |
| 199 RunAllPendingInMessageLoop(); | |
| 200 EXPECT_FALSE(IsNotificationVisible()); | |
| 201 EXPECT_EQ(1, accept_count()); | |
| 202 gfx::Size resolution; | |
| 203 EXPECT_TRUE( | |
| 204 display_manager->GetSelectedResolutionForDisplayId(id2, &resolution)); | |
| 205 EXPECT_EQ("200x200", resolution.ToString()); | |
| 206 } | |
| 207 | |
| 208 } // namespace internal | |
| 209 } // namespace ash | |
| OLD | NEW |