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(TimeOfDay time) { fake_now_ = time.ToTimeToday(); } |
| 73 void SetFakeSunset(TimeOfDay time) { fake_sunset_ = time.ToTimeToday(); } |
| 74 void SetFakeSunrise(TimeOfDay time) { fake_sunrise_ = time.ToTimeToday(); } |
| 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()->SetDelegateForTesting(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 // Tests transitioning from kNone to kCustom and back to kNone schedule types. |
| 285 TEST_F(NightLightTest, TestScheduleNoneToCustomTransition) { |
| 286 NightLightController* controller = GetController(); |
| 287 // Now is 6:00 PM. |
| 288 delegate()->SetFakeNow(TimeOfDay(18 * 60)); |
| 289 SetNightLightEnabled(false); |
| 290 controller->SetScheduleType(NightLightController::ScheduleType::kNone); |
| 291 // Start time is at 3:00 PM and end time is at 8:00 PM. |
| 292 controller->SetCustomStartTime(TimeOfDay(15 * 60)); |
| 293 controller->SetCustomEndTime(TimeOfDay(20 * 60)); |
| 294 |
| 295 // 15:00 18:00 20:00 |
| 296 // <----- + ----------- + ----------- + -----> |
| 297 // | | | |
| 298 // start now end |
| 299 // |
| 300 // Even though "Now" is inside the NightLight interval, nothing should change, |
| 301 // since the schedule type is "none". |
| 302 EXPECT_FALSE(controller->GetEnabled()); |
| 303 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 304 |
| 305 // Now change the schedule type to custom, NightLight should turn on |
| 306 // immediately with a short animation duration, and the timer should be |
| 307 // running with a delay of exactly 2 hours scheduling the end. |
| 308 controller->SetScheduleType(NightLightController::ScheduleType::kCustom); |
| 309 EXPECT_TRUE(controller->GetEnabled()); |
| 310 EXPECT_TRUE(TestLayersTemperature(controller->GetColorTemperature())); |
| 311 EXPECT_EQ(NightLightController::AnimationDurationType::kShort, |
| 312 controller->last_animation_type()); |
| 313 EXPECT_TRUE(controller->timer().IsRunning()); |
| 314 EXPECT_EQ(base::TimeDelta::FromHours(2), |
| 315 controller->timer().GetCurrentDelay()); |
| 316 EXPECT_TRUE(controller->timer().user_task()); |
| 317 |
| 318 // If the user changes the schedule type to "none", the NightLight status |
| 319 // should not change, but the timer should not be running. |
| 320 controller->SetScheduleType(NightLightController::ScheduleType::kNone); |
| 321 EXPECT_TRUE(controller->GetEnabled()); |
| 322 EXPECT_TRUE(TestLayersTemperature(controller->GetColorTemperature())); |
| 323 EXPECT_FALSE(controller->timer().IsRunning()); |
| 324 } |
| 325 |
| 326 // Tests what happens when the time now reaches the end of the NightLight |
| 327 // interval when NightLight mode is on. |
| 328 TEST_F(NightLightTest, TestCustomScheduleReachingEndTime) { |
| 329 NightLightController* controller = GetController(); |
| 330 delegate()->SetFakeNow(TimeOfDay(18 * 60)); |
| 331 controller->SetCustomStartTime(TimeOfDay(15 * 60)); |
| 332 controller->SetCustomEndTime(TimeOfDay(20 * 60)); |
| 333 controller->SetScheduleType(NightLightController::ScheduleType::kCustom); |
| 334 EXPECT_TRUE(controller->GetEnabled()); |
| 335 EXPECT_TRUE(TestLayersTemperature(controller->GetColorTemperature())); |
| 336 |
| 337 // Simulate reaching the end time by triggering the timer's user task. Make |
| 338 // sure that NightLight ended with a long animation. |
| 339 // |
| 340 // 15:00 20:00 |
| 341 // <----- + ------------------------ + -----> |
| 342 // | | |
| 343 // start end & now |
| 344 // |
| 345 // Now is 8:00 PM. |
| 346 delegate()->SetFakeNow(TimeOfDay(20 * 60)); |
| 347 controller->timer().user_task().Run(); |
| 348 EXPECT_FALSE(controller->GetEnabled()); |
| 349 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 350 EXPECT_EQ(NightLightController::AnimationDurationType::kLong, |
| 351 controller->last_animation_type()); |
| 352 // The timer should still be running, but now scheduling the start at 3:00 PM |
| 353 // tomorrow which is 19 hours from "now" (8:00 PM). |
| 354 EXPECT_TRUE(controller->timer().IsRunning()); |
| 355 EXPECT_EQ(base::TimeDelta::FromHours(19), |
| 356 controller->timer().GetCurrentDelay()); |
| 357 } |
| 358 |
| 359 // Tests that user toggles from the system menu or system settings override any |
| 360 // status set by an automatic schedule. |
| 361 TEST_F(NightLightTest, TestExplicitUserTogglesWhileScheduleIsActive) { |
| 362 // Start with the below custom schedule, where NightLight is off. |
| 363 // |
| 364 // 15:00 20:00 23:00 |
| 365 // <----- + ----------------- + ------------ + ----> |
| 366 // | | | |
| 367 // start end now |
| 368 // |
| 369 NightLightController* controller = GetController(); |
| 370 delegate()->SetFakeNow(TimeOfDay(23 * 60)); |
| 371 controller->SetCustomStartTime(TimeOfDay(15 * 60)); |
| 372 controller->SetCustomEndTime(TimeOfDay(20 * 60)); |
| 373 controller->SetScheduleType(NightLightController::ScheduleType::kCustom); |
| 374 EXPECT_FALSE(controller->GetEnabled()); |
| 375 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 376 |
| 377 // What happens if the user manually turns NightLight on while the schedule |
| 378 // type says it should be off? |
| 379 // User toggles either from the system menu or the System Settings toggle |
| 380 // button must override any automatic schedule, and should be performed with |
| 381 // the short animation duration. |
| 382 controller->Toggle(); |
| 383 EXPECT_TRUE(controller->GetEnabled()); |
| 384 EXPECT_TRUE(TestLayersTemperature(controller->GetColorTemperature())); |
| 385 EXPECT_EQ(NightLightController::AnimationDurationType::kShort, |
| 386 controller->last_animation_type()); |
| 387 // The timer should still be running, but NightLight should automatically |
| 388 // turn off at 8:00 PM tomorrow, which is 21 hours from now (11:00 PM). |
| 389 EXPECT_TRUE(controller->timer().IsRunning()); |
| 390 EXPECT_EQ(base::TimeDelta::FromHours(21), |
| 391 controller->timer().GetCurrentDelay()); |
| 392 |
| 393 // Manually turning it back off should also be respected, and this time the |
| 394 // start is scheduled at 3:00 PM tomorrow after 19 hours from "now" (8:00 PM). |
| 395 controller->Toggle(); |
| 396 EXPECT_FALSE(controller->GetEnabled()); |
| 397 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 398 EXPECT_EQ(NightLightController::AnimationDurationType::kShort, |
| 399 controller->last_animation_type()); |
| 400 EXPECT_TRUE(controller->timer().IsRunning()); |
| 401 EXPECT_EQ(base::TimeDelta::FromHours(16), |
| 402 controller->timer().GetCurrentDelay()); |
| 403 } |
| 404 |
| 405 // Tests that changing the custom start and end times, in such a way that |
| 406 // shouldn't change the current status, only updates the timer but doesn't |
| 407 // change the status. |
| 408 TEST_F(NightLightTest, TestChangingStartTimesThatDontChangeTheStatus) { |
| 409 // 16:00 18:00 22:00 |
| 410 // <----- + ----------- + ----------- + -----> |
| 411 // | | | |
| 412 // now start end |
| 413 // |
| 414 NightLightController* controller = GetController(); |
| 415 delegate()->SetFakeNow(TimeOfDay(16 * 60)); // 4:00 PM. |
| 416 SetNightLightEnabled(false); |
| 417 controller->SetScheduleType(NightLightController::ScheduleType::kNone); |
| 418 controller->SetCustomStartTime(TimeOfDay(18 * 60)); // 6:00 PM. |
| 419 controller->SetCustomEndTime(TimeOfDay(22 * 60)); // 10:00 PM. |
| 420 |
| 421 // Since now is outside the NightLight interval, changing the schedule type |
| 422 // to kCustom, shouldn't affect the status. Validate the timer is running with |
| 423 // a 2-hour delay. |
| 424 controller->SetScheduleType(NightLightController::ScheduleType::kCustom); |
| 425 EXPECT_FALSE(controller->GetEnabled()); |
| 426 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 427 EXPECT_TRUE(controller->timer().IsRunning()); |
| 428 EXPECT_EQ(base::TimeDelta::FromHours(2), |
| 429 controller->timer().GetCurrentDelay()); |
| 430 |
| 431 // Change the start time in such a way that doesn't change the status, but |
| 432 // despite that, confirm that schedule has been updated. |
| 433 controller->SetCustomStartTime(TimeOfDay(19 * 60)); // 7:00 PM. |
| 434 EXPECT_FALSE(controller->GetEnabled()); |
| 435 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 436 EXPECT_TRUE(controller->timer().IsRunning()); |
| 437 EXPECT_EQ(base::TimeDelta::FromHours(3), |
| 438 controller->timer().GetCurrentDelay()); |
| 439 |
| 440 // Changing the end time in a similar fashion to the above and expect no |
| 441 // change. |
| 442 controller->SetCustomEndTime(TimeOfDay(23 * 60)); // 11:00 PM. |
| 443 EXPECT_FALSE(controller->GetEnabled()); |
| 444 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 445 EXPECT_TRUE(controller->timer().IsRunning()); |
| 446 EXPECT_EQ(base::TimeDelta::FromHours(3), |
| 447 controller->timer().GetCurrentDelay()); |
| 448 } |
| 449 |
| 450 // Tests the behavior of the sunset to sunrise automatic schedule type. |
| 451 TEST_F(NightLightTest, TestSunsetSunrise) { |
| 452 // 16:00 18:00 20:00 22:00 5:00 |
| 453 // <----- + ----------- + ------- + -------- + --------------- + -------> |
| 454 // | | | | | |
| 455 // now custom start sunset custom end sunrise |
| 456 // |
| 457 NightLightController* controller = GetController(); |
| 458 delegate()->SetFakeNow(TimeOfDay(16 * 60)); // 4:00 PM. |
| 459 delegate()->SetFakeSunset(TimeOfDay(20 * 60)); // 8:00 PM. |
| 460 delegate()->SetFakeSunrise(TimeOfDay(5 * 60)); // 5:00 AM. |
| 461 SetNightLightEnabled(false); |
| 462 controller->SetScheduleType(NightLightController::ScheduleType::kNone); |
| 463 controller->SetCustomStartTime(TimeOfDay(18 * 60)); // 6:00 PM. |
| 464 controller->SetCustomEndTime(TimeOfDay(22 * 60)); // 10:00 PM. |
| 465 |
| 466 // Custom times should have no effect when the schedule type is sunset to |
| 467 // sunrise. |
| 468 controller->SetScheduleType( |
| 469 NightLightController::ScheduleType::kSunsetToSunrise); |
| 470 EXPECT_FALSE(controller->GetEnabled()); |
| 471 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 472 EXPECT_TRUE(controller->timer().IsRunning()); |
| 473 EXPECT_EQ(base::TimeDelta::FromHours(4), |
| 474 controller->timer().GetCurrentDelay()); |
| 475 |
| 476 // Simulate reaching sunset. |
| 477 delegate()->SetFakeNow(TimeOfDay(20 * 60)); // Now is 8:00 PM. |
| 478 controller->timer().user_task().Run(); |
| 479 EXPECT_TRUE(controller->GetEnabled()); |
| 480 EXPECT_TRUE(TestLayersTemperature(controller->GetColorTemperature())); |
| 481 EXPECT_EQ(NightLightController::AnimationDurationType::kLong, |
| 482 controller->last_animation_type()); |
| 483 // Timer is running scheduling the end at sunrise. |
| 484 EXPECT_TRUE(controller->timer().IsRunning()); |
| 485 EXPECT_EQ(base::TimeDelta::FromHours(9), |
| 486 controller->timer().GetCurrentDelay()); |
| 487 |
| 488 // Simulate reaching sunrise. |
| 489 delegate()->SetFakeNow(TimeOfDay(5 * 60)); // Now is 5:00 AM. |
| 490 controller->timer().user_task().Run(); |
| 491 EXPECT_FALSE(controller->GetEnabled()); |
| 492 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 493 EXPECT_EQ(NightLightController::AnimationDurationType::kLong, |
| 494 controller->last_animation_type()); |
| 495 // Timer is running scheduling the start at the next sunset. |
| 496 EXPECT_TRUE(controller->timer().IsRunning()); |
| 497 EXPECT_EQ(base::TimeDelta::FromHours(15), |
| 498 controller->timer().GetCurrentDelay()); |
| 499 } |
| 500 |
248 } // namespace | 501 } // namespace |
249 | 502 |
250 } // namespace ash | 503 } // namespace ash |
OLD | NEW |