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/ash_switches.h" | 10 #include "ash/ash_switches.h" |
11 #include "ash/public/cpp/ash_pref_names.h" | 11 #include "ash/public/cpp/ash_pref_names.h" |
12 #include "ash/public/cpp/config.h" | 12 #include "ash/public/cpp/config.h" |
13 #include "ash/public/cpp/session_types.h" | 13 #include "ash/public/cpp/session_types.h" |
14 #include "ash/shell.h" | 14 #include "ash/shell.h" |
15 #include "ash/test/ash_test_base.h" | 15 #include "ash/test/ash_test_base.h" |
16 #include "ash/test/ash_test_helper.h" | 16 #include "ash/test/ash_test_helper.h" |
17 #include "ash/test/test_session_controller_client.h" | 17 #include "ash/test/test_session_controller_client.h" |
18 #include "ash/test/test_shell_delegate.h" | 18 #include "ash/test/test_shell_delegate.h" |
| 19 #include "base/bind.h" |
| 20 #include "base/callback_forward.h" |
19 #include "base/command_line.h" | 21 #include "base/command_line.h" |
20 #include "base/macros.h" | 22 #include "base/macros.h" |
21 #include "components/prefs/testing_pref_service.h" | 23 #include "components/prefs/testing_pref_service.h" |
22 #include "ui/compositor/layer.h" | 24 #include "ui/compositor/layer.h" |
23 | 25 |
24 namespace ash { | 26 namespace ash { |
25 | 27 |
26 namespace { | 28 namespace { |
27 | 29 |
28 constexpr char kUser1Email[] = "user1@nightlight"; | 30 constexpr char kUser1Email[] = "user1@nightlight"; |
(...skipping 28 matching lines...) Expand all Loading... |
57 void OnNightLightEnabledChanged(bool enabled) override { status_ = enabled; } | 59 void OnNightLightEnabledChanged(bool enabled) override { status_ = enabled; } |
58 | 60 |
59 bool status() const { return status_; } | 61 bool status() const { return status_; } |
60 | 62 |
61 private: | 63 private: |
62 bool status_ = false; | 64 bool status_ = false; |
63 | 65 |
64 DISALLOW_COPY_AND_ASSIGN(TestObserver); | 66 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
65 }; | 67 }; |
66 | 68 |
| 69 class TestDelegate : public NightLightController::Delegate { |
| 70 public: |
| 71 TestDelegate() = default; |
| 72 ~TestDelegate() override = default; |
| 73 |
| 74 void SetFakeNow(TimeOfDay time) { fake_now_ = time.ToTimeToday(); } |
| 75 void SetFakeSunset(TimeOfDay time) { fake_sunset_ = time.ToTimeToday(); } |
| 76 void SetFakeSunrise(TimeOfDay time) { fake_sunrise_ = time.ToTimeToday(); } |
| 77 |
| 78 // ash::NightLightController::Delegate |
| 79 base::Time GetNow() const override { return fake_now_; } |
| 80 base::Time GetSunsetTime() const override { return fake_sunset_; } |
| 81 base::Time GetSunriseTime() const override { return fake_sunrise_; } |
| 82 |
| 83 private: |
| 84 base::Time fake_now_; |
| 85 base::Time fake_sunset_; |
| 86 base::Time fake_sunrise_; |
| 87 |
| 88 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
| 89 }; |
| 90 |
67 class NightLightTest : public test::AshTestBase { | 91 class NightLightTest : public test::AshTestBase { |
68 public: | 92 public: |
69 NightLightTest() = default; | 93 NightLightTest() = default; |
70 ~NightLightTest() override = default; | 94 ~NightLightTest() override = default; |
71 | 95 |
72 TestingPrefServiceSimple* user1_pref_service() { | 96 TestingPrefServiceSimple* user1_pref_service() { |
73 return &user1_pref_service_; | 97 return &user1_pref_service_; |
74 } | 98 } |
75 TestingPrefServiceSimple* user2_pref_service() { | 99 TestingPrefServiceSimple* user2_pref_service() { |
76 return &user2_pref_service_; | 100 return &user2_pref_service_; |
77 } | 101 } |
78 | 102 |
| 103 TestDelegate* delegate() const { return delegate_; } |
| 104 |
79 // ash::test::AshTestBase: | 105 // ash::test::AshTestBase: |
80 void SetUp() override { | 106 void SetUp() override { |
81 // Explicitly enable the NightLight feature for the tests. | 107 // Explicitly enable the NightLight feature for the tests. |
82 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 108 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
83 ash::switches::kAshEnableNightLight); | 109 ash::switches::kAshEnableNightLight); |
84 | 110 |
85 test::AshTestBase::SetUp(); | 111 test::AshTestBase::SetUp(); |
86 CreateTestUserSessions(); | 112 CreateTestUserSessions(); |
87 Shell::RegisterPrefs(user1_pref_service_.registry()); | 113 Shell::RegisterPrefs(user1_pref_service_.registry()); |
88 Shell::RegisterPrefs(user2_pref_service_.registry()); | 114 Shell::RegisterPrefs(user2_pref_service_.registry()); |
89 | 115 |
90 // Simulate user 1 login. | 116 // Simulate user 1 login. |
91 InjectTestPrefService(&user1_pref_service_); | 117 InjectTestPrefService(&user1_pref_service_); |
92 SwitchActiveUser(kUser1Email); | 118 SwitchActiveUser(kUser1Email); |
| 119 |
| 120 delegate_ = new TestDelegate; |
| 121 GetController()->SetDelegateForTesting(base::WrapUnique(delegate_)); |
93 } | 122 } |
94 | 123 |
95 void CreateTestUserSessions() { | 124 void CreateTestUserSessions() { |
96 GetSessionControllerClient()->Reset(); | 125 GetSessionControllerClient()->Reset(); |
97 GetSessionControllerClient()->AddUserSession(kUser1Email); | 126 GetSessionControllerClient()->AddUserSession(kUser1Email); |
98 GetSessionControllerClient()->AddUserSession(kUser2Email); | 127 GetSessionControllerClient()->AddUserSession(kUser2Email); |
99 } | 128 } |
100 | 129 |
101 void SwitchActiveUser(const std::string& email) { | 130 void SwitchActiveUser(const std::string& email) { |
102 GetSessionControllerClient()->SwitchActiveUser( | 131 GetSessionControllerClient()->SwitchActiveUser( |
103 AccountId::FromUserEmail(email)); | 132 AccountId::FromUserEmail(email)); |
104 } | 133 } |
105 | 134 |
106 void InjectTestPrefService(PrefService* pref_service) { | 135 void InjectTestPrefService(PrefService* pref_service) { |
107 ash_test_helper()->test_shell_delegate()->set_active_user_pref_service( | 136 ash_test_helper()->test_shell_delegate()->set_active_user_pref_service( |
108 pref_service); | 137 pref_service); |
109 } | 138 } |
110 | 139 |
| 140 void SetNightLightEnabled(bool enabled) { |
| 141 GetController()->SetEnabled( |
| 142 enabled, NightLightController::AnimationDuration::kShort); |
| 143 } |
| 144 |
111 private: | 145 private: |
112 TestingPrefServiceSimple user1_pref_service_; | 146 TestingPrefServiceSimple user1_pref_service_; |
113 TestingPrefServiceSimple user2_pref_service_; | 147 TestingPrefServiceSimple user2_pref_service_; |
114 | 148 |
| 149 TestDelegate* delegate_ = nullptr; |
| 150 |
115 DISALLOW_COPY_AND_ASSIGN(NightLightTest); | 151 DISALLOW_COPY_AND_ASSIGN(NightLightTest); |
116 }; | 152 }; |
117 | 153 |
118 // Tests toggling NightLight on / off and makes sure the observer is updated and | 154 // Tests toggling NightLight on / off and makes sure the observer is updated and |
119 // the layer temperatures are modified. | 155 // the layer temperatures are modified. |
120 TEST_F(NightLightTest, TestToggle) { | 156 TEST_F(NightLightTest, TestToggle) { |
121 if (Shell::GetAshConfig() == Config::MASH) { | 157 if (Shell::GetAshConfig() == Config::MASH) { |
122 // PrefChangeRegistrar doesn't work on mash. crbug.com/721961. | 158 // PrefChangeRegistrar doesn't work on mash. crbug.com/721961. |
123 return; | 159 return; |
124 } | 160 } |
125 | 161 |
126 UpdateDisplay("800x600,800x600"); | 162 UpdateDisplay("800x600,800x600"); |
127 | 163 |
128 TestObserver observer; | 164 TestObserver observer; |
129 NightLightController* controller = GetController(); | 165 NightLightController* controller = GetController(); |
130 controller->SetEnabled(false); | 166 SetNightLightEnabled(false); |
131 ASSERT_FALSE(controller->GetEnabled()); | 167 ASSERT_FALSE(controller->GetEnabled()); |
132 EXPECT_TRUE(TestLayersTemperature(0.0f)); | 168 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
133 controller->Toggle(); | 169 controller->Toggle(); |
134 EXPECT_TRUE(controller->GetEnabled()); | 170 EXPECT_TRUE(controller->GetEnabled()); |
135 EXPECT_TRUE(observer.status()); | 171 EXPECT_TRUE(observer.status()); |
136 EXPECT_TRUE(TestLayersTemperature(GetController()->GetColorTemperature())); | 172 EXPECT_TRUE(TestLayersTemperature(GetController()->GetColorTemperature())); |
137 controller->Toggle(); | 173 controller->Toggle(); |
138 EXPECT_FALSE(controller->GetEnabled()); | 174 EXPECT_FALSE(controller->GetEnabled()); |
139 EXPECT_FALSE(observer.status()); | 175 EXPECT_FALSE(observer.status()); |
140 EXPECT_TRUE(TestLayersTemperature(0.0f)); | 176 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
141 } | 177 } |
142 | 178 |
143 // Tests setting the temperature in various situations. | 179 // Tests setting the temperature in various situations. |
144 TEST_F(NightLightTest, TestSetTemperature) { | 180 TEST_F(NightLightTest, TestSetTemperature) { |
145 if (Shell::GetAshConfig() == Config::MASH) { | 181 if (Shell::GetAshConfig() == Config::MASH) { |
146 // PrefChangeRegistrar doesn't work on mash. crbug.com/721961. | 182 // PrefChangeRegistrar doesn't work on mash. crbug.com/721961. |
147 return; | 183 return; |
148 } | 184 } |
149 | 185 |
150 UpdateDisplay("800x600,800x600"); | 186 UpdateDisplay("800x600,800x600"); |
151 | 187 |
152 TestObserver observer; | 188 TestObserver observer; |
153 NightLightController* controller = GetController(); | 189 NightLightController* controller = GetController(); |
154 controller->SetEnabled(false); | 190 SetNightLightEnabled(false); |
155 ASSERT_FALSE(controller->GetEnabled()); | 191 ASSERT_FALSE(controller->GetEnabled()); |
156 | 192 |
157 // Setting the temperature while NightLight is disabled only changes the | 193 // Setting the temperature while NightLight is disabled only changes the |
158 // color temperature field, but root layers temperatures are not affected, nor | 194 // color temperature field, but root layers temperatures are not affected, nor |
159 // the status of NightLight itself. | 195 // the status of NightLight itself. |
160 const float temperature1 = 0.2f; | 196 const float temperature1 = 0.2f; |
161 controller->SetColorTemperature(temperature1); | 197 controller->SetColorTemperature(temperature1); |
162 EXPECT_EQ(temperature1, controller->GetColorTemperature()); | 198 EXPECT_EQ(temperature1, controller->GetColorTemperature()); |
163 EXPECT_TRUE(TestLayersTemperature(0.0f)); | 199 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
164 | 200 |
165 // When NightLight is enabled, temperature changes actually affect the root | 201 // When NightLight is enabled, temperature changes actually affect the root |
166 // layers temperatures. | 202 // layers temperatures. |
167 controller->SetEnabled(true); | 203 SetNightLightEnabled(true); |
168 ASSERT_TRUE(controller->GetEnabled()); | 204 ASSERT_TRUE(controller->GetEnabled()); |
169 const float temperature2 = 0.7f; | 205 const float temperature2 = 0.7f; |
170 controller->SetColorTemperature(temperature2); | 206 controller->SetColorTemperature(temperature2); |
171 EXPECT_EQ(temperature2, controller->GetColorTemperature()); | 207 EXPECT_EQ(temperature2, controller->GetColorTemperature()); |
172 EXPECT_TRUE(TestLayersTemperature(temperature2)); | 208 EXPECT_TRUE(TestLayersTemperature(temperature2)); |
173 | 209 |
174 // When NightLight is disabled, the value of the color temperature field | 210 // When NightLight is disabled, the value of the color temperature field |
175 // doesn't change, however the temperatures set on the root layers are all | 211 // doesn't change, however the temperatures set on the root layers are all |
176 // 0.0f. Observers only receive an enabled status change notification; no | 212 // 0.0f. Observers only receive an enabled status change notification; no |
177 // temperature change notification. | 213 // temperature change notification. |
178 controller->SetEnabled(false); | 214 SetNightLightEnabled(false); |
179 ASSERT_FALSE(controller->GetEnabled()); | 215 ASSERT_FALSE(controller->GetEnabled()); |
180 EXPECT_FALSE(observer.status()); | 216 EXPECT_FALSE(observer.status()); |
181 EXPECT_EQ(temperature2, controller->GetColorTemperature()); | 217 EXPECT_EQ(temperature2, controller->GetColorTemperature()); |
182 EXPECT_TRUE(TestLayersTemperature(0.0f)); | 218 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
183 | 219 |
184 // When re-enabled, the stored temperature is re-applied. | 220 // When re-enabled, the stored temperature is re-applied. |
185 controller->SetEnabled(true); | 221 SetNightLightEnabled(true); |
186 EXPECT_TRUE(observer.status()); | 222 EXPECT_TRUE(observer.status()); |
187 ASSERT_TRUE(controller->GetEnabled()); | 223 ASSERT_TRUE(controller->GetEnabled()); |
188 EXPECT_TRUE(TestLayersTemperature(temperature2)); | 224 EXPECT_TRUE(TestLayersTemperature(temperature2)); |
189 } | 225 } |
190 | 226 |
191 // Tests that switching users retrieves NightLight settings for the active | 227 // Tests that switching users retrieves NightLight settings for the active |
192 // user's prefs. | 228 // user's prefs. |
193 TEST_F(NightLightTest, TestUserSwitchAndSettingsPersistence) { | 229 TEST_F(NightLightTest, TestUserSwitchAndSettingsPersistence) { |
194 if (Shell::GetAshConfig() == Config::MASH) { | 230 if (Shell::GetAshConfig() == Config::MASH) { |
195 // PrefChangeRegistrar doesn't work on mash. crbug.com/721961. | 231 // PrefChangeRegistrar doesn't work on mash. crbug.com/721961. |
196 return; | 232 return; |
197 } | 233 } |
198 | 234 |
199 // Test start with user1 logged in. | 235 // Test start with user1 logged in. |
200 NightLightController* controller = GetController(); | 236 NightLightController* controller = GetController(); |
201 controller->SetEnabled(true); | 237 SetNightLightEnabled(true); |
202 EXPECT_TRUE(controller->GetEnabled()); | 238 EXPECT_TRUE(controller->GetEnabled()); |
203 const float user1_temperature = 0.8f; | 239 const float user1_temperature = 0.8f; |
204 controller->SetColorTemperature(user1_temperature); | 240 controller->SetColorTemperature(user1_temperature); |
205 EXPECT_EQ(user1_temperature, controller->GetColorTemperature()); | 241 EXPECT_EQ(user1_temperature, controller->GetColorTemperature()); |
206 EXPECT_TRUE(TestLayersTemperature(user1_temperature)); | 242 EXPECT_TRUE(TestLayersTemperature(user1_temperature)); |
207 | 243 |
208 // Switch to user 2, and expect NightLight to be disabled. | 244 // Switch to user 2, and expect NightLight to be disabled. |
209 InjectTestPrefService(user2_pref_service()); | 245 InjectTestPrefService(user2_pref_service()); |
210 SwitchActiveUser(kUser2Email); | 246 SwitchActiveUser(kUser2Email); |
211 EXPECT_FALSE(controller->GetEnabled()); | 247 EXPECT_FALSE(controller->GetEnabled()); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 EXPECT_EQ(temperature1, controller->GetColorTemperature()); | 280 EXPECT_EQ(temperature1, controller->GetColorTemperature()); |
245 EXPECT_TRUE(TestLayersTemperature(temperature1)); | 281 EXPECT_TRUE(TestLayersTemperature(temperature1)); |
246 const float temperature2 = 0.23f; | 282 const float temperature2 = 0.23f; |
247 user1_pref_service()->SetDouble(prefs::kNightLightTemperature, temperature2); | 283 user1_pref_service()->SetDouble(prefs::kNightLightTemperature, temperature2); |
248 EXPECT_EQ(temperature2, controller->GetColorTemperature()); | 284 EXPECT_EQ(temperature2, controller->GetColorTemperature()); |
249 EXPECT_TRUE(TestLayersTemperature(temperature2)); | 285 EXPECT_TRUE(TestLayersTemperature(temperature2)); |
250 user1_pref_service()->SetBoolean(prefs::kNightLightEnabled, false); | 286 user1_pref_service()->SetBoolean(prefs::kNightLightEnabled, false); |
251 EXPECT_FALSE(controller->GetEnabled()); | 287 EXPECT_FALSE(controller->GetEnabled()); |
252 } | 288 } |
253 | 289 |
| 290 // Tests transitioning from kNone to kCustom and back to kNone schedule types. |
| 291 TEST_F(NightLightTest, TestScheduleNoneToCustomTransition) { |
| 292 if (Shell::GetAshConfig() == Config::MASH) { |
| 293 // PrefChangeRegistrar doesn't work on mash. crbug.com/721961. |
| 294 return; |
| 295 } |
| 296 |
| 297 NightLightController* controller = GetController(); |
| 298 // Now is 6:00 PM. |
| 299 delegate()->SetFakeNow(TimeOfDay(18 * 60)); |
| 300 SetNightLightEnabled(false); |
| 301 controller->SetScheduleType(NightLightController::ScheduleType::kNone); |
| 302 // Start time is at 3:00 PM and end time is at 8:00 PM. |
| 303 controller->SetCustomStartTime(TimeOfDay(15 * 60)); |
| 304 controller->SetCustomEndTime(TimeOfDay(20 * 60)); |
| 305 |
| 306 // 15:00 18:00 20:00 |
| 307 // <----- + ----------- + ----------- + -----> |
| 308 // | | | |
| 309 // start now end |
| 310 // |
| 311 // Even though "Now" is inside the NightLight interval, nothing should change, |
| 312 // since the schedule type is "none". |
| 313 EXPECT_FALSE(controller->GetEnabled()); |
| 314 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 315 |
| 316 // Now change the schedule type to custom, NightLight should turn on |
| 317 // immediately with a short animation duration, and the timer should be |
| 318 // running with a delay of exactly 2 hours scheduling the end. |
| 319 controller->SetScheduleType(NightLightController::ScheduleType::kCustom); |
| 320 EXPECT_TRUE(controller->GetEnabled()); |
| 321 EXPECT_TRUE(TestLayersTemperature(controller->GetColorTemperature())); |
| 322 EXPECT_EQ(NightLightController::AnimationDuration::kShort, |
| 323 controller->last_animation_duration()); |
| 324 EXPECT_TRUE(controller->timer().IsRunning()); |
| 325 EXPECT_EQ(base::TimeDelta::FromHours(2), |
| 326 controller->timer().GetCurrentDelay()); |
| 327 EXPECT_TRUE(controller->timer().user_task()); |
| 328 |
| 329 // If the user changes the schedule type to "none", the NightLight status |
| 330 // should not change, but the timer should not be running. |
| 331 controller->SetScheduleType(NightLightController::ScheduleType::kNone); |
| 332 EXPECT_TRUE(controller->GetEnabled()); |
| 333 EXPECT_TRUE(TestLayersTemperature(controller->GetColorTemperature())); |
| 334 EXPECT_FALSE(controller->timer().IsRunning()); |
| 335 } |
| 336 |
| 337 // Tests what happens when the time now reaches the end of the NightLight |
| 338 // interval when NightLight mode is on. |
| 339 TEST_F(NightLightTest, TestCustomScheduleReachingEndTime) { |
| 340 if (Shell::GetAshConfig() == Config::MASH) { |
| 341 // PrefChangeRegistrar doesn't work on mash. crbug.com/721961. |
| 342 return; |
| 343 } |
| 344 |
| 345 NightLightController* controller = GetController(); |
| 346 delegate()->SetFakeNow(TimeOfDay(18 * 60)); |
| 347 controller->SetCustomStartTime(TimeOfDay(15 * 60)); |
| 348 controller->SetCustomEndTime(TimeOfDay(20 * 60)); |
| 349 controller->SetScheduleType(NightLightController::ScheduleType::kCustom); |
| 350 EXPECT_TRUE(controller->GetEnabled()); |
| 351 EXPECT_TRUE(TestLayersTemperature(controller->GetColorTemperature())); |
| 352 |
| 353 // Simulate reaching the end time by triggering the timer's user task. Make |
| 354 // sure that NightLight ended with a long animation. |
| 355 // |
| 356 // 15:00 20:00 |
| 357 // <----- + ------------------------ + -----> |
| 358 // | | |
| 359 // start end & now |
| 360 // |
| 361 // Now is 8:00 PM. |
| 362 delegate()->SetFakeNow(TimeOfDay(20 * 60)); |
| 363 controller->timer().user_task().Run(); |
| 364 EXPECT_FALSE(controller->GetEnabled()); |
| 365 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 366 EXPECT_EQ(NightLightController::AnimationDuration::kLong, |
| 367 controller->last_animation_duration()); |
| 368 // The timer should still be running, but now scheduling the start at 3:00 PM |
| 369 // tomorrow which is 19 hours from "now" (8:00 PM). |
| 370 EXPECT_TRUE(controller->timer().IsRunning()); |
| 371 EXPECT_EQ(base::TimeDelta::FromHours(19), |
| 372 controller->timer().GetCurrentDelay()); |
| 373 } |
| 374 |
| 375 // Tests that user toggles from the system menu or system settings override any |
| 376 // status set by an automatic schedule. |
| 377 TEST_F(NightLightTest, TestExplicitUserTogglesWhileScheduleIsActive) { |
| 378 if (Shell::GetAshConfig() == Config::MASH) { |
| 379 // PrefChangeRegistrar doesn't work on mash. crbug.com/721961. |
| 380 return; |
| 381 } |
| 382 |
| 383 // Start with the below custom schedule, where NightLight is off. |
| 384 // |
| 385 // 15:00 20:00 23:00 |
| 386 // <----- + ----------------- + ------------ + ----> |
| 387 // | | | |
| 388 // start end now |
| 389 // |
| 390 NightLightController* controller = GetController(); |
| 391 delegate()->SetFakeNow(TimeOfDay(23 * 60)); |
| 392 controller->SetCustomStartTime(TimeOfDay(15 * 60)); |
| 393 controller->SetCustomEndTime(TimeOfDay(20 * 60)); |
| 394 controller->SetScheduleType(NightLightController::ScheduleType::kCustom); |
| 395 EXPECT_FALSE(controller->GetEnabled()); |
| 396 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 397 |
| 398 // What happens if the user manually turns NightLight on while the schedule |
| 399 // type says it should be off? |
| 400 // User toggles either from the system menu or the System Settings toggle |
| 401 // button must override any automatic schedule, and should be performed with |
| 402 // the short animation duration. |
| 403 controller->Toggle(); |
| 404 EXPECT_TRUE(controller->GetEnabled()); |
| 405 EXPECT_TRUE(TestLayersTemperature(controller->GetColorTemperature())); |
| 406 EXPECT_EQ(NightLightController::AnimationDuration::kShort, |
| 407 controller->last_animation_duration()); |
| 408 // The timer should still be running, but NightLight should automatically |
| 409 // turn off at 8:00 PM tomorrow, which is 21 hours from now (11:00 PM). |
| 410 EXPECT_TRUE(controller->timer().IsRunning()); |
| 411 EXPECT_EQ(base::TimeDelta::FromHours(21), |
| 412 controller->timer().GetCurrentDelay()); |
| 413 |
| 414 // Manually turning it back off should also be respected, and this time the |
| 415 // start is scheduled at 3:00 PM tomorrow after 19 hours from "now" (8:00 PM). |
| 416 controller->Toggle(); |
| 417 EXPECT_FALSE(controller->GetEnabled()); |
| 418 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 419 EXPECT_EQ(NightLightController::AnimationDuration::kShort, |
| 420 controller->last_animation_duration()); |
| 421 EXPECT_TRUE(controller->timer().IsRunning()); |
| 422 EXPECT_EQ(base::TimeDelta::FromHours(16), |
| 423 controller->timer().GetCurrentDelay()); |
| 424 } |
| 425 |
| 426 // Tests that changing the custom start and end times, in such a way that |
| 427 // shouldn't change the current status, only updates the timer but doesn't |
| 428 // change the status. |
| 429 TEST_F(NightLightTest, TestChangingStartTimesThatDontChangeTheStatus) { |
| 430 if (Shell::GetAshConfig() == Config::MASH) { |
| 431 // PrefChangeRegistrar doesn't work on mash. crbug.com/721961. |
| 432 return; |
| 433 } |
| 434 |
| 435 // 16:00 18:00 22:00 |
| 436 // <----- + ----------- + ----------- + -----> |
| 437 // | | | |
| 438 // now start end |
| 439 // |
| 440 NightLightController* controller = GetController(); |
| 441 delegate()->SetFakeNow(TimeOfDay(16 * 60)); // 4:00 PM. |
| 442 SetNightLightEnabled(false); |
| 443 controller->SetScheduleType(NightLightController::ScheduleType::kNone); |
| 444 controller->SetCustomStartTime(TimeOfDay(18 * 60)); // 6:00 PM. |
| 445 controller->SetCustomEndTime(TimeOfDay(22 * 60)); // 10:00 PM. |
| 446 |
| 447 // Since now is outside the NightLight interval, changing the schedule type |
| 448 // to kCustom, shouldn't affect the status. Validate the timer is running with |
| 449 // a 2-hour delay. |
| 450 controller->SetScheduleType(NightLightController::ScheduleType::kCustom); |
| 451 EXPECT_FALSE(controller->GetEnabled()); |
| 452 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 453 EXPECT_TRUE(controller->timer().IsRunning()); |
| 454 EXPECT_EQ(base::TimeDelta::FromHours(2), |
| 455 controller->timer().GetCurrentDelay()); |
| 456 |
| 457 // Change the start time in such a way that doesn't change the status, but |
| 458 // despite that, confirm that schedule has been updated. |
| 459 controller->SetCustomStartTime(TimeOfDay(19 * 60)); // 7:00 PM. |
| 460 EXPECT_FALSE(controller->GetEnabled()); |
| 461 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 462 EXPECT_TRUE(controller->timer().IsRunning()); |
| 463 EXPECT_EQ(base::TimeDelta::FromHours(3), |
| 464 controller->timer().GetCurrentDelay()); |
| 465 |
| 466 // Changing the end time in a similar fashion to the above and expect no |
| 467 // change. |
| 468 controller->SetCustomEndTime(TimeOfDay(23 * 60)); // 11:00 PM. |
| 469 EXPECT_FALSE(controller->GetEnabled()); |
| 470 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 471 EXPECT_TRUE(controller->timer().IsRunning()); |
| 472 EXPECT_EQ(base::TimeDelta::FromHours(3), |
| 473 controller->timer().GetCurrentDelay()); |
| 474 } |
| 475 |
| 476 // Tests the behavior of the sunset to sunrise automatic schedule type. |
| 477 TEST_F(NightLightTest, TestSunsetSunrise) { |
| 478 if (Shell::GetAshConfig() == Config::MASH) { |
| 479 // PrefChangeRegistrar doesn't work on mash. crbug.com/721961. |
| 480 return; |
| 481 } |
| 482 |
| 483 // 16:00 18:00 20:00 22:00 5:00 |
| 484 // <----- + ----------- + ------- + -------- + --------------- + -------> |
| 485 // | | | | | |
| 486 // now custom start sunset custom end sunrise |
| 487 // |
| 488 NightLightController* controller = GetController(); |
| 489 delegate()->SetFakeNow(TimeOfDay(16 * 60)); // 4:00 PM. |
| 490 delegate()->SetFakeSunset(TimeOfDay(20 * 60)); // 8:00 PM. |
| 491 delegate()->SetFakeSunrise(TimeOfDay(5 * 60)); // 5:00 AM. |
| 492 SetNightLightEnabled(false); |
| 493 controller->SetScheduleType(NightLightController::ScheduleType::kNone); |
| 494 controller->SetCustomStartTime(TimeOfDay(18 * 60)); // 6:00 PM. |
| 495 controller->SetCustomEndTime(TimeOfDay(22 * 60)); // 10:00 PM. |
| 496 |
| 497 // Custom times should have no effect when the schedule type is sunset to |
| 498 // sunrise. |
| 499 controller->SetScheduleType( |
| 500 NightLightController::ScheduleType::kSunsetToSunrise); |
| 501 EXPECT_FALSE(controller->GetEnabled()); |
| 502 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 503 EXPECT_TRUE(controller->timer().IsRunning()); |
| 504 EXPECT_EQ(base::TimeDelta::FromHours(4), |
| 505 controller->timer().GetCurrentDelay()); |
| 506 |
| 507 // Simulate reaching sunset. |
| 508 delegate()->SetFakeNow(TimeOfDay(20 * 60)); // Now is 8:00 PM. |
| 509 controller->timer().user_task().Run(); |
| 510 EXPECT_TRUE(controller->GetEnabled()); |
| 511 EXPECT_TRUE(TestLayersTemperature(controller->GetColorTemperature())); |
| 512 EXPECT_EQ(NightLightController::AnimationDuration::kLong, |
| 513 controller->last_animation_duration()); |
| 514 // Timer is running scheduling the end at sunrise. |
| 515 EXPECT_TRUE(controller->timer().IsRunning()); |
| 516 EXPECT_EQ(base::TimeDelta::FromHours(9), |
| 517 controller->timer().GetCurrentDelay()); |
| 518 |
| 519 // Simulate reaching sunrise. |
| 520 delegate()->SetFakeNow(TimeOfDay(5 * 60)); // Now is 5:00 AM. |
| 521 controller->timer().user_task().Run(); |
| 522 EXPECT_FALSE(controller->GetEnabled()); |
| 523 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 524 EXPECT_EQ(NightLightController::AnimationDuration::kLong, |
| 525 controller->last_animation_duration()); |
| 526 // Timer is running scheduling the start at the next sunset. |
| 527 EXPECT_TRUE(controller->timer().IsRunning()); |
| 528 EXPECT_EQ(base::TimeDelta::FromHours(15), |
| 529 controller->timer().GetCurrentDelay()); |
| 530 } |
| 531 |
254 } // namespace | 532 } // namespace |
255 | 533 |
256 } // namespace ash | 534 } // namespace ash |
OLD | NEW |