Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/system/night_light/night_light_controller.h" | 5 #include "ash/system/night_light/night_light_controller.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 #include <limits> | 8 #include <limits> |
| 9 | 9 |
| 10 #include "ash/public/cpp/ash_pref_names.h" | 10 #include "ash/public/cpp/ash_pref_names.h" |
| 11 #include "ash/public/cpp/config.h" | 11 #include "ash/public/cpp/config.h" |
| 12 #include "ash/public/cpp/session_types.h" | 12 #include "ash/public/cpp/session_types.h" |
| 13 #include "ash/shell.h" | 13 #include "ash/shell.h" |
| 14 #include "ash/test/ash_test_base.h" | 14 #include "ash/test/ash_test_base.h" |
| 15 #include "ash/test/ash_test_helper.h" | 15 #include "ash/test/ash_test_helper.h" |
| 16 #include "ash/test/test_session_controller_client.h" | 16 #include "ash/test/test_session_controller_client.h" |
| 17 #include "ash/test/test_shell_delegate.h" | 17 #include "ash/test/test_shell_delegate.h" |
| 18 #include "base/bind.h" | |
| 19 #include "base/callback_forward.h" | |
| 18 #include "base/macros.h" | 20 #include "base/macros.h" |
| 19 #include "components/prefs/testing_pref_service.h" | 21 #include "components/prefs/testing_pref_service.h" |
| 20 #include "ui/compositor/layer.h" | 22 #include "ui/compositor/layer.h" |
| 21 | 23 |
| 22 namespace ash { | 24 namespace ash { |
| 23 | 25 |
| 24 namespace { | 26 namespace { |
| 25 | 27 |
| 26 constexpr char kUser1Email[] = "user1@nightlight"; | 28 constexpr char kUser1Email[] = "user1@nightlight"; |
| 27 constexpr char kUser2Email[] = "user2@nightlight"; | 29 constexpr char kUser2Email[] = "user2@nightlight"; |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 55 void OnNightLightEnabledChanged(bool enabled) override { status_ = enabled; } | 57 void OnNightLightEnabledChanged(bool enabled) override { status_ = enabled; } |
| 56 | 58 |
| 57 bool status() const { return status_; } | 59 bool status() const { return status_; } |
| 58 | 60 |
| 59 private: | 61 private: |
| 60 bool status_ = false; | 62 bool status_ = false; |
| 61 | 63 |
| 62 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 64 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 63 }; | 65 }; |
| 64 | 66 |
| 67 class TestDelegate : public NightLightController::Delegate { | |
| 68 public: | |
| 69 TestDelegate() = default; | |
| 70 ~TestDelegate() override = default; | |
| 71 | |
| 72 void SetFakeNow(NightLightTime time) { fake_now_ = time.ToTime(); } | |
| 73 void SetFakeSunset(NightLightTime time) { fake_sunset_ = time.ToTime(); } | |
| 74 void SetFakeSunrise(NightLightTime time) { fake_sunrise_ = time.ToTime(); } | |
| 75 | |
| 76 // ash::NightLightController::Delegate | |
| 77 base::Time GetNow() const override { return fake_now_; } | |
| 78 base::Time GetSunsetTime() const override { return fake_sunset_; } | |
| 79 base::Time GetSunriseTime() const override { return fake_sunrise_; } | |
| 80 | |
| 81 private: | |
| 82 base::Time fake_now_; | |
| 83 base::Time fake_sunset_; | |
| 84 base::Time fake_sunrise_; | |
| 85 | |
| 86 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | |
| 87 }; | |
| 88 | |
| 65 class NightLightTest : public test::AshTestBase { | 89 class NightLightTest : public test::AshTestBase { |
| 66 public: | 90 public: |
| 67 NightLightTest() = default; | 91 NightLightTest() = default; |
| 68 ~NightLightTest() override = default; | 92 ~NightLightTest() override = default; |
| 69 | 93 |
| 70 TestingPrefServiceSimple* user1_pref_service() { | 94 TestingPrefServiceSimple* user1_pref_service() { |
| 71 return &user1_pref_service_; | 95 return &user1_pref_service_; |
| 72 } | 96 } |
| 73 TestingPrefServiceSimple* user2_pref_service() { | 97 TestingPrefServiceSimple* user2_pref_service() { |
| 74 return &user2_pref_service_; | 98 return &user2_pref_service_; |
| 75 } | 99 } |
| 76 | 100 |
| 101 TestDelegate* delegate() const { return delegate_; } | |
| 102 | |
| 77 // ash::test::AshTestBase: | 103 // ash::test::AshTestBase: |
| 78 void SetUp() override { | 104 void SetUp() override { |
| 79 test::AshTestBase::SetUp(); | 105 test::AshTestBase::SetUp(); |
| 80 CreateTestUserSessions(); | 106 CreateTestUserSessions(); |
| 81 Shell::RegisterPrefs(user1_pref_service_.registry()); | 107 Shell::RegisterPrefs(user1_pref_service_.registry()); |
| 82 Shell::RegisterPrefs(user2_pref_service_.registry()); | 108 Shell::RegisterPrefs(user2_pref_service_.registry()); |
| 83 | 109 |
| 84 // Simulate user 1 login. | 110 // Simulate user 1 login. |
| 85 InjectTestPrefService(&user1_pref_service_); | 111 InjectTestPrefService(&user1_pref_service_); |
| 86 SwitchActiveUser(kUser1Email); | 112 SwitchActiveUser(kUser1Email); |
| 113 | |
| 114 delegate_ = new TestDelegate; | |
| 115 GetController()->OverrideDelegateForTesting(base::WrapUnique(delegate_)); | |
| 87 } | 116 } |
| 88 | 117 |
| 89 void CreateTestUserSessions() { | 118 void CreateTestUserSessions() { |
| 90 GetSessionControllerClient()->Reset(); | 119 GetSessionControllerClient()->Reset(); |
| 91 GetSessionControllerClient()->AddUserSession(kUser1Email); | 120 GetSessionControllerClient()->AddUserSession(kUser1Email); |
| 92 GetSessionControllerClient()->AddUserSession(kUser2Email); | 121 GetSessionControllerClient()->AddUserSession(kUser2Email); |
| 93 } | 122 } |
| 94 | 123 |
| 95 void SwitchActiveUser(const std::string& email) { | 124 void SwitchActiveUser(const std::string& email) { |
| 96 GetSessionControllerClient()->SwitchActiveUser( | 125 GetSessionControllerClient()->SwitchActiveUser( |
| 97 AccountId::FromUserEmail(email)); | 126 AccountId::FromUserEmail(email)); |
| 98 } | 127 } |
| 99 | 128 |
| 100 void InjectTestPrefService(PrefService* pref_service) { | 129 void InjectTestPrefService(PrefService* pref_service) { |
| 101 ash_test_helper()->test_shell_delegate()->set_active_user_pref_service( | 130 ash_test_helper()->test_shell_delegate()->set_active_user_pref_service( |
| 102 pref_service); | 131 pref_service); |
| 103 } | 132 } |
| 104 | 133 |
| 134 void SetNightLightEnabled(bool enabled) { | |
| 135 GetController()->SetEnabled( | |
| 136 enabled, NightLightController::AnimationDurationType::kShort); | |
| 137 } | |
| 138 | |
| 105 private: | 139 private: |
| 106 TestingPrefServiceSimple user1_pref_service_; | 140 TestingPrefServiceSimple user1_pref_service_; |
| 107 TestingPrefServiceSimple user2_pref_service_; | 141 TestingPrefServiceSimple user2_pref_service_; |
| 108 | 142 |
| 143 TestDelegate* delegate_ = nullptr; | |
| 144 | |
| 109 DISALLOW_COPY_AND_ASSIGN(NightLightTest); | 145 DISALLOW_COPY_AND_ASSIGN(NightLightTest); |
| 110 }; | 146 }; |
| 111 | 147 |
| 112 // Tests toggling NightLight on / off and makes sure the observer is updated and | 148 // Tests toggling NightLight on / off and makes sure the observer is updated and |
| 113 // the layer temperatures are modified. | 149 // the layer temperatures are modified. |
| 114 TEST_F(NightLightTest, TestToggle) { | 150 TEST_F(NightLightTest, TestToggle) { |
| 115 if (Shell::GetAshConfig() == Config::MASH) { | 151 if (Shell::GetAshConfig() == Config::MASH) { |
| 116 // PrefChangeRegistrar doesn't work on mash. crbug.com/721961. | 152 // PrefChangeRegistrar doesn't work on mash. crbug.com/721961. |
| 117 return; | 153 return; |
| 118 } | 154 } |
| 119 | 155 |
| 120 UpdateDisplay("800x600,800x600"); | 156 UpdateDisplay("800x600,800x600"); |
| 121 | 157 |
| 122 TestObserver observer; | 158 TestObserver observer; |
| 123 NightLightController* controller = GetController(); | 159 NightLightController* controller = GetController(); |
| 124 controller->SetEnabled(false); | 160 SetNightLightEnabled(false); |
| 125 ASSERT_FALSE(controller->GetEnabled()); | 161 ASSERT_FALSE(controller->GetEnabled()); |
| 126 EXPECT_TRUE(TestLayersTemperature(0.0f)); | 162 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 127 controller->Toggle(); | 163 controller->Toggle(); |
| 128 EXPECT_TRUE(controller->GetEnabled()); | 164 EXPECT_TRUE(controller->GetEnabled()); |
| 129 EXPECT_TRUE(observer.status()); | 165 EXPECT_TRUE(observer.status()); |
| 130 EXPECT_TRUE(TestLayersTemperature(GetController()->GetColorTemperature())); | 166 EXPECT_TRUE(TestLayersTemperature(GetController()->GetColorTemperature())); |
| 131 controller->Toggle(); | 167 controller->Toggle(); |
| 132 EXPECT_FALSE(controller->GetEnabled()); | 168 EXPECT_FALSE(controller->GetEnabled()); |
| 133 EXPECT_FALSE(observer.status()); | 169 EXPECT_FALSE(observer.status()); |
| 134 EXPECT_TRUE(TestLayersTemperature(0.0f)); | 170 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 135 } | 171 } |
| 136 | 172 |
| 137 // Tests setting the temperature in various situations. | 173 // Tests setting the temperature in various situations. |
| 138 TEST_F(NightLightTest, TestSetTemperature) { | 174 TEST_F(NightLightTest, TestSetTemperature) { |
| 139 if (Shell::GetAshConfig() == Config::MASH) { | 175 if (Shell::GetAshConfig() == Config::MASH) { |
| 140 // PrefChangeRegistrar doesn't work on mash. crbug.com/721961. | 176 // PrefChangeRegistrar doesn't work on mash. crbug.com/721961. |
| 141 return; | 177 return; |
| 142 } | 178 } |
| 143 | 179 |
| 144 UpdateDisplay("800x600,800x600"); | 180 UpdateDisplay("800x600,800x600"); |
| 145 | 181 |
| 146 TestObserver observer; | 182 TestObserver observer; |
| 147 NightLightController* controller = GetController(); | 183 NightLightController* controller = GetController(); |
| 148 controller->SetEnabled(false); | 184 SetNightLightEnabled(false); |
| 149 ASSERT_FALSE(controller->GetEnabled()); | 185 ASSERT_FALSE(controller->GetEnabled()); |
| 150 | 186 |
| 151 // Setting the temperature while NightLight is disabled only changes the | 187 // Setting the temperature while NightLight is disabled only changes the |
| 152 // color temperature field, but root layers temperatures are not affected, nor | 188 // color temperature field, but root layers temperatures are not affected, nor |
| 153 // the status of NightLight itself. | 189 // the status of NightLight itself. |
| 154 const float temperature1 = 0.2f; | 190 const float temperature1 = 0.2f; |
| 155 controller->SetColorTemperature(temperature1); | 191 controller->SetColorTemperature(temperature1); |
| 156 EXPECT_EQ(temperature1, controller->GetColorTemperature()); | 192 EXPECT_EQ(temperature1, controller->GetColorTemperature()); |
| 157 EXPECT_TRUE(TestLayersTemperature(0.0f)); | 193 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 158 | 194 |
| 159 // When NightLight is enabled, temperature changes actually affect the root | 195 // When NightLight is enabled, temperature changes actually affect the root |
| 160 // layers temperatures. | 196 // layers temperatures. |
| 161 controller->SetEnabled(true); | 197 SetNightLightEnabled(true); |
| 162 ASSERT_TRUE(controller->GetEnabled()); | 198 ASSERT_TRUE(controller->GetEnabled()); |
| 163 const float temperature2 = 0.7f; | 199 const float temperature2 = 0.7f; |
| 164 controller->SetColorTemperature(temperature2); | 200 controller->SetColorTemperature(temperature2); |
| 165 EXPECT_EQ(temperature2, controller->GetColorTemperature()); | 201 EXPECT_EQ(temperature2, controller->GetColorTemperature()); |
| 166 EXPECT_TRUE(TestLayersTemperature(temperature2)); | 202 EXPECT_TRUE(TestLayersTemperature(temperature2)); |
| 167 | 203 |
| 168 // When NightLight is disabled, the value of the color temperature field | 204 // When NightLight is disabled, the value of the color temperature field |
| 169 // doesn't change, however the temperatures set on the root layers are all | 205 // doesn't change, however the temperatures set on the root layers are all |
| 170 // 0.0f. Observers only receive an enabled status change notification; no | 206 // 0.0f. Observers only receive an enabled status change notification; no |
| 171 // temperature change notification. | 207 // temperature change notification. |
| 172 controller->SetEnabled(false); | 208 SetNightLightEnabled(false); |
| 173 ASSERT_FALSE(controller->GetEnabled()); | 209 ASSERT_FALSE(controller->GetEnabled()); |
| 174 EXPECT_FALSE(observer.status()); | 210 EXPECT_FALSE(observer.status()); |
| 175 EXPECT_EQ(temperature2, controller->GetColorTemperature()); | 211 EXPECT_EQ(temperature2, controller->GetColorTemperature()); |
| 176 EXPECT_TRUE(TestLayersTemperature(0.0f)); | 212 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 177 | 213 |
| 178 // When re-enabled, the stored temperature is re-applied. | 214 // When re-enabled, the stored temperature is re-applied. |
| 179 controller->SetEnabled(true); | 215 SetNightLightEnabled(true); |
| 180 EXPECT_TRUE(observer.status()); | 216 EXPECT_TRUE(observer.status()); |
| 181 ASSERT_TRUE(controller->GetEnabled()); | 217 ASSERT_TRUE(controller->GetEnabled()); |
| 182 EXPECT_TRUE(TestLayersTemperature(temperature2)); | 218 EXPECT_TRUE(TestLayersTemperature(temperature2)); |
| 183 } | 219 } |
| 184 | 220 |
| 185 // Tests that switching users retrieves NightLight settings for the active | 221 // Tests that switching users retrieves NightLight settings for the active |
| 186 // user's prefs. | 222 // user's prefs. |
| 187 TEST_F(NightLightTest, TestUserSwitchAndSettingsPersistence) { | 223 TEST_F(NightLightTest, TestUserSwitchAndSettingsPersistence) { |
| 188 if (Shell::GetAshConfig() == Config::MASH) { | 224 if (Shell::GetAshConfig() == Config::MASH) { |
| 189 // PrefChangeRegistrar doesn't work on mash. crbug.com/721961. | 225 // PrefChangeRegistrar doesn't work on mash. crbug.com/721961. |
| 190 return; | 226 return; |
| 191 } | 227 } |
| 192 | 228 |
| 193 // Test start with user1 logged in. | 229 // Test start with user1 logged in. |
| 194 NightLightController* controller = GetController(); | 230 NightLightController* controller = GetController(); |
| 195 controller->SetEnabled(true); | 231 SetNightLightEnabled(true); |
| 196 EXPECT_TRUE(controller->GetEnabled()); | 232 EXPECT_TRUE(controller->GetEnabled()); |
| 197 const float user1_temperature = 0.8f; | 233 const float user1_temperature = 0.8f; |
| 198 controller->SetColorTemperature(user1_temperature); | 234 controller->SetColorTemperature(user1_temperature); |
| 199 EXPECT_EQ(user1_temperature, controller->GetColorTemperature()); | 235 EXPECT_EQ(user1_temperature, controller->GetColorTemperature()); |
| 200 EXPECT_TRUE(TestLayersTemperature(user1_temperature)); | 236 EXPECT_TRUE(TestLayersTemperature(user1_temperature)); |
| 201 | 237 |
| 202 // Switch to user 2, and expect NightLight to be disabled. | 238 // Switch to user 2, and expect NightLight to be disabled. |
| 203 InjectTestPrefService(user2_pref_service()); | 239 InjectTestPrefService(user2_pref_service()); |
| 204 SwitchActiveUser(kUser2Email); | 240 SwitchActiveUser(kUser2Email); |
| 205 EXPECT_FALSE(controller->GetEnabled()); | 241 EXPECT_FALSE(controller->GetEnabled()); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 EXPECT_EQ(temperature1, controller->GetColorTemperature()); | 274 EXPECT_EQ(temperature1, controller->GetColorTemperature()); |
| 239 EXPECT_TRUE(TestLayersTemperature(temperature1)); | 275 EXPECT_TRUE(TestLayersTemperature(temperature1)); |
| 240 const float temperature2 = 0.23f; | 276 const float temperature2 = 0.23f; |
| 241 user1_pref_service()->SetDouble(prefs::kNightLightTemperature, temperature2); | 277 user1_pref_service()->SetDouble(prefs::kNightLightTemperature, temperature2); |
| 242 EXPECT_EQ(temperature2, controller->GetColorTemperature()); | 278 EXPECT_EQ(temperature2, controller->GetColorTemperature()); |
| 243 EXPECT_TRUE(TestLayersTemperature(temperature2)); | 279 EXPECT_TRUE(TestLayersTemperature(temperature2)); |
| 244 user1_pref_service()->SetBoolean(prefs::kNightLightEnabled, false); | 280 user1_pref_service()->SetBoolean(prefs::kNightLightEnabled, false); |
| 245 EXPECT_FALSE(controller->GetEnabled()); | 281 EXPECT_FALSE(controller->GetEnabled()); |
| 246 } | 282 } |
| 247 | 283 |
| 284 TEST_F(NightLightTest, TestScheduleType) { | |
| 285 NightLightController* controller = GetController(); | |
| 286 // Now is 6:00 PM. | |
| 287 delegate()->SetFakeNow(NightLightTime(18 * 60)); | |
| 288 SetNightLightEnabled(false); | |
| 289 controller->SetScheduleType(NightLightController::ScheduleType::kNever); | |
| 290 // Start time is at 3:00 PM and end timeis at 8:00 PM. | |
|
James Cook
2017/05/23 16:34:58
nit: timeis -> time is
afakhry
2017/05/24 04:21:10
Done.
| |
| 291 controller->SetCustomStartTime(NightLightTime(15 * 60)); | |
| 292 controller->SetCustomEndTime(NightLightTime(20 * 60)); | |
| 293 | |
| 294 // 15:00 18:00 20:00 | |
| 295 // <----- + ----------- + ----------- + -----> | |
| 296 // | | | | |
| 297 // start now end | |
| 298 // | |
| 299 // Even though "Now" is inside the NightLight interval, nothing should change, | |
| 300 // since the schedule type is "never". | |
| 301 EXPECT_FALSE(controller->GetEnabled()); | |
| 302 EXPECT_TRUE(TestLayersTemperature(0.0f)); | |
| 303 | |
| 304 // Now change the schedule type to custom, NightLight should turn on | |
| 305 // immediately with a short animation duration, and the timer should be | |
| 306 // running with a delay of exactly 2 hours scheduling the end. | |
| 307 controller->SetScheduleType(NightLightController::ScheduleType::kCustom); | |
| 308 EXPECT_TRUE(controller->GetEnabled()); | |
| 309 EXPECT_TRUE(TestLayersTemperature(controller->GetColorTemperature())); | |
| 310 EXPECT_EQ(NightLightController::AnimationDurationType::kShort, | |
| 311 controller->consumed_animation_type()); | |
| 312 EXPECT_TRUE(controller->timer().IsRunning()); | |
| 313 EXPECT_EQ(base::TimeDelta::FromHours(2), | |
| 314 controller->timer().GetCurrentDelay()); | |
| 315 EXPECT_TRUE(controller->timer().user_task()); | |
| 316 | |
| 317 // Simulate reaching the end time by triggering the timer's user task. Make | |
| 318 // sure that NightLight ended with a long animation. | |
| 319 // | |
| 320 // 15:00 20:00 | |
| 321 // <----- + ------------------------ + -----> | |
| 322 // | | | |
| 323 // start end & now | |
| 324 // | |
|
James Cook
2017/05/23 16:34:58
The ASCII art is super-helpful, thanks for doing i
afakhry
2017/05/24 04:21:10
You're welcome! I always find myself needing this
| |
| 325 // Now is 8:00 PM. | |
| 326 delegate()->SetFakeNow(NightLightTime(20 * 60)); | |
| 327 controller->timer().user_task().Run(); | |
| 328 EXPECT_FALSE(controller->GetEnabled()); | |
| 329 EXPECT_TRUE(TestLayersTemperature(0.0f)); | |
| 330 EXPECT_EQ(NightLightController::AnimationDurationType::kLong, | |
| 331 controller->consumed_animation_type()); | |
| 332 // The timer should still be running, but now scheduling the start at 3:00 PM | |
| 333 // tomorrow which is 19 hours from "now" (8:00 PM). | |
| 334 EXPECT_TRUE(controller->timer().IsRunning()); | |
| 335 EXPECT_EQ(base::TimeDelta::FromHours(19), | |
| 336 controller->timer().GetCurrentDelay()); | |
| 337 | |
| 338 // What happens if the user manually turns NightLight on while the schedule | |
| 339 // type says it should be off? | |
| 340 // User toggles either from the SystemMenu or the System Settings toggle | |
|
James Cook
2017/05/23 16:34:58
nit: SystemMenu -> system menu
afakhry
2017/05/24 04:21:10
Done.
| |
| 341 // button must override any automatic schedule, and should be performed with | |
| 342 // the short animation duration. | |
| 343 controller->Toggle(); | |
| 344 EXPECT_TRUE(controller->GetEnabled()); | |
| 345 EXPECT_TRUE(TestLayersTemperature(controller->GetColorTemperature())); | |
| 346 EXPECT_EQ(NightLightController::AnimationDurationType::kShort, | |
| 347 controller->consumed_animation_type()); | |
| 348 // The timer should still be running, but NightLight should automatically | |
| 349 // turn off at 8:00 PM tomorrow, which is 24 hours from now. | |
| 350 EXPECT_TRUE(controller->timer().IsRunning()); | |
| 351 EXPECT_EQ(base::TimeDelta::FromHours(24), | |
| 352 controller->timer().GetCurrentDelay()); | |
| 353 | |
| 354 // Manually turning it back off should also be respected, and this time the | |
| 355 // start is scheduled at 3:00 PM tomorrow after 19 hours from "now" (8:00 PM). | |
| 356 controller->Toggle(); | |
| 357 EXPECT_FALSE(controller->GetEnabled()); | |
| 358 EXPECT_TRUE(TestLayersTemperature(0.0f)); | |
| 359 EXPECT_EQ(NightLightController::AnimationDurationType::kShort, | |
| 360 controller->consumed_animation_type()); | |
| 361 EXPECT_TRUE(controller->timer().IsRunning()); | |
| 362 EXPECT_EQ(base::TimeDelta::FromHours(19), | |
| 363 controller->timer().GetCurrentDelay()); | |
| 364 | |
| 365 // If the user changes the schedule type to "never", the NightLight status | |
| 366 // should not change, but the timer should not be running. | |
| 367 controller->SetScheduleType(NightLightController::ScheduleType::kNever); | |
| 368 EXPECT_FALSE(controller->GetEnabled()); | |
| 369 EXPECT_TRUE(TestLayersTemperature(0.0f)); | |
| 370 EXPECT_FALSE(controller->timer().IsRunning()); | |
| 371 } | |
|
James Cook
2017/05/23 16:34:58
This test is a little long. Could it be broken int
afakhry
2017/05/24 04:21:10
Done.
| |
| 372 | |
| 373 TEST_F(NightLightTest, TestChangingStartTimesThatDontChangeTheStatus) { | |
| 374 // 16:00 18:00 22:00 | |
| 375 // <----- + ----------- + ----------- + -----> | |
| 376 // | | | | |
| 377 // now start end | |
| 378 // | |
| 379 NightLightController* controller = GetController(); | |
| 380 delegate()->SetFakeNow(NightLightTime(16 * 60)); // 4:00 PM. | |
| 381 SetNightLightEnabled(false); | |
| 382 controller->SetScheduleType(NightLightController::ScheduleType::kNever); | |
|
James Cook
2017/05/23 16:34:58
optional name idea: kNever might be clearer as kNo
afakhry
2017/05/24 04:21:10
I just named it after the string we will show in t
| |
| 383 controller->SetCustomStartTime(NightLightTime(18 * 60)); // 6:00 PM. | |
| 384 controller->SetCustomEndTime(NightLightTime(22 * 60)); // 10:00 PM. | |
| 385 | |
| 386 // Since now is outside the NightLight interval, changing the schedule type | |
| 387 // to kCustom, shouldn't affect the status. Validate the timer is running with | |
| 388 // a 2-hour delay. | |
| 389 controller->SetScheduleType(NightLightController::ScheduleType::kCustom); | |
| 390 EXPECT_FALSE(controller->GetEnabled()); | |
| 391 EXPECT_TRUE(TestLayersTemperature(0.0f)); | |
| 392 EXPECT_TRUE(controller->timer().IsRunning()); | |
| 393 EXPECT_EQ(base::TimeDelta::FromHours(2), | |
| 394 controller->timer().GetCurrentDelay()); | |
| 395 | |
| 396 // Change the start time in such a way that doesn't change the status, but | |
| 397 // despite that, confirm that schedule has been updated. | |
| 398 controller->SetCustomStartTime(NightLightTime(19 * 60)); // 7:00 PM. | |
| 399 EXPECT_FALSE(controller->GetEnabled()); | |
| 400 EXPECT_TRUE(TestLayersTemperature(0.0f)); | |
| 401 EXPECT_TRUE(controller->timer().IsRunning()); | |
| 402 EXPECT_EQ(base::TimeDelta::FromHours(3), | |
| 403 controller->timer().GetCurrentDelay()); | |
| 404 | |
| 405 // Changing the end time in a similar fashion to the above and expect no | |
| 406 // change. | |
| 407 controller->SetCustomEndTime(NightLightTime(23 * 60)); // 11:00 PM. | |
| 408 EXPECT_FALSE(controller->GetEnabled()); | |
| 409 EXPECT_TRUE(TestLayersTemperature(0.0f)); | |
| 410 EXPECT_TRUE(controller->timer().IsRunning()); | |
| 411 EXPECT_EQ(base::TimeDelta::FromHours(3), | |
| 412 controller->timer().GetCurrentDelay()); | |
| 413 } | |
| 414 | |
| 415 TEST_F(NightLightTest, TestSunsetSunrise) { | |
| 416 // 16:00 18:00 20:00 22:00 5:00 | |
| 417 // <----- + ----------- + ------- + -------- + --------------- + -------> | |
| 418 // | | | | | | |
| 419 // now custom start sunset custom end sunrise | |
| 420 // | |
| 421 NightLightController* controller = GetController(); | |
| 422 delegate()->SetFakeNow(NightLightTime(16 * 60)); // 4:00 PM. | |
| 423 delegate()->SetFakeSunset(NightLightTime(20 * 60)); // 8:00 PM. | |
| 424 delegate()->SetFakeSunrise(NightLightTime(5 * 60)); // 5:00 AM. | |
| 425 SetNightLightEnabled(false); | |
| 426 controller->SetScheduleType(NightLightController::ScheduleType::kNever); | |
| 427 controller->SetCustomStartTime(NightLightTime(18 * 60)); // 6:00 PM. | |
| 428 controller->SetCustomEndTime(NightLightTime(22 * 60)); // 10:00 PM. | |
| 429 | |
| 430 // Custom times should have no effect when the schedule type is sunset to | |
| 431 // sunrise. | |
| 432 controller->SetScheduleType( | |
| 433 NightLightController::ScheduleType::kSunsetToSunrise); | |
| 434 EXPECT_FALSE(controller->GetEnabled()); | |
| 435 EXPECT_TRUE(TestLayersTemperature(0.0f)); | |
| 436 EXPECT_TRUE(controller->timer().IsRunning()); | |
| 437 EXPECT_EQ(base::TimeDelta::FromHours(4), | |
| 438 controller->timer().GetCurrentDelay()); | |
| 439 | |
| 440 // Simulate reaching sunset. | |
| 441 delegate()->SetFakeNow(NightLightTime(20 * 60)); // Now is 8:00 PM. | |
| 442 controller->timer().user_task().Run(); | |
| 443 EXPECT_TRUE(controller->GetEnabled()); | |
| 444 EXPECT_TRUE(TestLayersTemperature(controller->GetColorTemperature())); | |
| 445 EXPECT_EQ(NightLightController::AnimationDurationType::kLong, | |
| 446 controller->consumed_animation_type()); | |
| 447 // Timer is running scheduling the end at sunrise. | |
| 448 EXPECT_TRUE(controller->timer().IsRunning()); | |
| 449 EXPECT_EQ(base::TimeDelta::FromHours(9), | |
| 450 controller->timer().GetCurrentDelay()); | |
| 451 | |
| 452 // Simulate reaching sunrise. | |
| 453 delegate()->SetFakeNow(NightLightTime(5 * 60)); // Now is 5:00 AM. | |
| 454 controller->timer().user_task().Run(); | |
| 455 EXPECT_FALSE(controller->GetEnabled()); | |
| 456 EXPECT_TRUE(TestLayersTemperature(0.0f)); | |
| 457 EXPECT_EQ(NightLightController::AnimationDurationType::kLong, | |
| 458 controller->consumed_animation_type()); | |
| 459 // Timer is running scheduling the start at the next sunset. | |
| 460 EXPECT_TRUE(controller->timer().IsRunning()); | |
| 461 EXPECT_EQ(base::TimeDelta::FromHours(15), | |
| 462 controller->timer().GetCurrentDelay()); | |
| 463 } | |
|
James Cook
2017/05/23 16:34:59
Nice test suite. Thorough and easy to read.
afakhry
2017/05/24 04:21:10
Acknowledged.
| |
| 464 | |
| 248 } // namespace | 465 } // namespace |
| 249 | 466 |
| 250 } // namespace ash | 467 } // namespace ash |
| OLD | NEW |