Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(272)

Side by Side Diff: ash/system/night_light/night_light_controller_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698