Chromium Code Reviews| Index: ash/system/night_light/night_light_controller_unittest.cc |
| diff --git a/ash/system/night_light/night_light_controller_unittest.cc b/ash/system/night_light/night_light_controller_unittest.cc |
| index 175a01dcc88322f1c384b37df5b7323aafd23943..97fcd5d06b3c918b7abc1ef9a9cbabd02fb188ef 100644 |
| --- a/ash/system/night_light/night_light_controller_unittest.cc |
| +++ b/ash/system/night_light/night_light_controller_unittest.cc |
| @@ -15,6 +15,8 @@ |
| #include "ash/test/ash_test_helper.h" |
| #include "ash/test/test_session_controller_client.h" |
| #include "ash/test/test_shell_delegate.h" |
| +#include "base/bind.h" |
| +#include "base/callback_forward.h" |
| #include "base/macros.h" |
| #include "components/prefs/testing_pref_service.h" |
| #include "ui/compositor/layer.h" |
| @@ -62,6 +64,28 @@ class TestObserver : public NightLightController::Observer { |
| DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| }; |
| +class TestDelegate : public NightLightController::Delegate { |
| + public: |
| + TestDelegate() = default; |
| + ~TestDelegate() override = default; |
| + |
| + void SetFakeNow(NightLightTime time) { fake_now_ = time.ToTime(); } |
| + void SetFakeSunset(NightLightTime time) { fake_sunset_ = time.ToTime(); } |
| + void SetFakeSunrise(NightLightTime time) { fake_sunrise_ = time.ToTime(); } |
| + |
| + // ash::NightLightController::Delegate |
| + base::Time GetNow() const override { return fake_now_; } |
| + base::Time GetSunsetTime() const override { return fake_sunset_; } |
| + base::Time GetSunriseTime() const override { return fake_sunrise_; } |
| + |
| + private: |
| + base::Time fake_now_; |
| + base::Time fake_sunset_; |
| + base::Time fake_sunrise_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
| +}; |
| + |
| class NightLightTest : public test::AshTestBase { |
| public: |
| NightLightTest() = default; |
| @@ -74,6 +98,8 @@ class NightLightTest : public test::AshTestBase { |
| return &user2_pref_service_; |
| } |
| + TestDelegate* delegate() const { return delegate_; } |
| + |
| // ash::test::AshTestBase: |
| void SetUp() override { |
| test::AshTestBase::SetUp(); |
| @@ -84,6 +110,9 @@ class NightLightTest : public test::AshTestBase { |
| // Simulate user 1 login. |
| InjectTestPrefService(&user1_pref_service_); |
| SwitchActiveUser(kUser1Email); |
| + |
| + delegate_ = new TestDelegate; |
| + GetController()->OverrideDelegateForTesting(base::WrapUnique(delegate_)); |
| } |
| void CreateTestUserSessions() { |
| @@ -102,10 +131,17 @@ class NightLightTest : public test::AshTestBase { |
| pref_service); |
| } |
| + void SetNightLightEnabled(bool enabled) { |
| + GetController()->SetEnabled( |
| + enabled, NightLightController::AnimationDurationType::kShort); |
| + } |
| + |
| private: |
| TestingPrefServiceSimple user1_pref_service_; |
| TestingPrefServiceSimple user2_pref_service_; |
| + TestDelegate* delegate_ = nullptr; |
| + |
| DISALLOW_COPY_AND_ASSIGN(NightLightTest); |
| }; |
| @@ -121,7 +157,7 @@ TEST_F(NightLightTest, TestToggle) { |
| TestObserver observer; |
| NightLightController* controller = GetController(); |
| - controller->SetEnabled(false); |
| + SetNightLightEnabled(false); |
| ASSERT_FALSE(controller->GetEnabled()); |
| EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| controller->Toggle(); |
| @@ -145,7 +181,7 @@ TEST_F(NightLightTest, TestSetTemperature) { |
| TestObserver observer; |
| NightLightController* controller = GetController(); |
| - controller->SetEnabled(false); |
| + SetNightLightEnabled(false); |
| ASSERT_FALSE(controller->GetEnabled()); |
| // Setting the temperature while NightLight is disabled only changes the |
| @@ -158,7 +194,7 @@ TEST_F(NightLightTest, TestSetTemperature) { |
| // When NightLight is enabled, temperature changes actually affect the root |
| // layers temperatures. |
| - controller->SetEnabled(true); |
| + SetNightLightEnabled(true); |
| ASSERT_TRUE(controller->GetEnabled()); |
| const float temperature2 = 0.7f; |
| controller->SetColorTemperature(temperature2); |
| @@ -169,14 +205,14 @@ TEST_F(NightLightTest, TestSetTemperature) { |
| // doesn't change, however the temperatures set on the root layers are all |
| // 0.0f. Observers only receive an enabled status change notification; no |
| // temperature change notification. |
| - controller->SetEnabled(false); |
| + SetNightLightEnabled(false); |
| ASSERT_FALSE(controller->GetEnabled()); |
| EXPECT_FALSE(observer.status()); |
| EXPECT_EQ(temperature2, controller->GetColorTemperature()); |
| EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| // When re-enabled, the stored temperature is re-applied. |
| - controller->SetEnabled(true); |
| + SetNightLightEnabled(true); |
| EXPECT_TRUE(observer.status()); |
| ASSERT_TRUE(controller->GetEnabled()); |
| EXPECT_TRUE(TestLayersTemperature(temperature2)); |
| @@ -192,7 +228,7 @@ TEST_F(NightLightTest, TestUserSwitchAndSettingsPersistence) { |
| // Test start with user1 logged in. |
| NightLightController* controller = GetController(); |
| - controller->SetEnabled(true); |
| + SetNightLightEnabled(true); |
| EXPECT_TRUE(controller->GetEnabled()); |
| const float user1_temperature = 0.8f; |
| controller->SetColorTemperature(user1_temperature); |
| @@ -245,6 +281,187 @@ TEST_F(NightLightTest, TestOutsidePreferencesChangesAreApplied) { |
| EXPECT_FALSE(controller->GetEnabled()); |
| } |
| +TEST_F(NightLightTest, TestScheduleType) { |
| + NightLightController* controller = GetController(); |
| + // Now is 6:00 PM. |
| + delegate()->SetFakeNow(NightLightTime(18 * 60)); |
| + SetNightLightEnabled(false); |
| + controller->SetScheduleType(NightLightController::ScheduleType::kNever); |
| + // 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.
|
| + controller->SetCustomStartTime(NightLightTime(15 * 60)); |
| + controller->SetCustomEndTime(NightLightTime(20 * 60)); |
| + |
| + // 15:00 18:00 20:00 |
| + // <----- + ----------- + ----------- + -----> |
| + // | | | |
| + // start now end |
| + // |
| + // Even though "Now" is inside the NightLight interval, nothing should change, |
| + // since the schedule type is "never". |
| + EXPECT_FALSE(controller->GetEnabled()); |
| + EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| + |
| + // Now change the schedule type to custom, NightLight should turn on |
| + // immediately with a short animation duration, and the timer should be |
| + // running with a delay of exactly 2 hours scheduling the end. |
| + controller->SetScheduleType(NightLightController::ScheduleType::kCustom); |
| + EXPECT_TRUE(controller->GetEnabled()); |
| + EXPECT_TRUE(TestLayersTemperature(controller->GetColorTemperature())); |
| + EXPECT_EQ(NightLightController::AnimationDurationType::kShort, |
| + controller->consumed_animation_type()); |
| + EXPECT_TRUE(controller->timer().IsRunning()); |
| + EXPECT_EQ(base::TimeDelta::FromHours(2), |
| + controller->timer().GetCurrentDelay()); |
| + EXPECT_TRUE(controller->timer().user_task()); |
| + |
| + // Simulate reaching the end time by triggering the timer's user task. Make |
| + // sure that NightLight ended with a long animation. |
| + // |
| + // 15:00 20:00 |
| + // <----- + ------------------------ + -----> |
| + // | | |
| + // start end & now |
| + // |
|
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
|
| + // Now is 8:00 PM. |
| + delegate()->SetFakeNow(NightLightTime(20 * 60)); |
| + controller->timer().user_task().Run(); |
| + EXPECT_FALSE(controller->GetEnabled()); |
| + EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| + EXPECT_EQ(NightLightController::AnimationDurationType::kLong, |
| + controller->consumed_animation_type()); |
| + // The timer should still be running, but now scheduling the start at 3:00 PM |
| + // tomorrow which is 19 hours from "now" (8:00 PM). |
| + EXPECT_TRUE(controller->timer().IsRunning()); |
| + EXPECT_EQ(base::TimeDelta::FromHours(19), |
| + controller->timer().GetCurrentDelay()); |
| + |
| + // What happens if the user manually turns NightLight on while the schedule |
| + // type says it should be off? |
| + // 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.
|
| + // button must override any automatic schedule, and should be performed with |
| + // the short animation duration. |
| + controller->Toggle(); |
| + EXPECT_TRUE(controller->GetEnabled()); |
| + EXPECT_TRUE(TestLayersTemperature(controller->GetColorTemperature())); |
| + EXPECT_EQ(NightLightController::AnimationDurationType::kShort, |
| + controller->consumed_animation_type()); |
| + // The timer should still be running, but NightLight should automatically |
| + // turn off at 8:00 PM tomorrow, which is 24 hours from now. |
| + EXPECT_TRUE(controller->timer().IsRunning()); |
| + EXPECT_EQ(base::TimeDelta::FromHours(24), |
| + controller->timer().GetCurrentDelay()); |
| + |
| + // Manually turning it back off should also be respected, and this time the |
| + // start is scheduled at 3:00 PM tomorrow after 19 hours from "now" (8:00 PM). |
| + controller->Toggle(); |
| + EXPECT_FALSE(controller->GetEnabled()); |
| + EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| + EXPECT_EQ(NightLightController::AnimationDurationType::kShort, |
| + controller->consumed_animation_type()); |
| + EXPECT_TRUE(controller->timer().IsRunning()); |
| + EXPECT_EQ(base::TimeDelta::FromHours(19), |
| + controller->timer().GetCurrentDelay()); |
| + |
| + // If the user changes the schedule type to "never", the NightLight status |
| + // should not change, but the timer should not be running. |
| + controller->SetScheduleType(NightLightController::ScheduleType::kNever); |
| + EXPECT_FALSE(controller->GetEnabled()); |
| + EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| + EXPECT_FALSE(controller->timer().IsRunning()); |
| +} |
|
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.
|
| + |
| +TEST_F(NightLightTest, TestChangingStartTimesThatDontChangeTheStatus) { |
| + // 16:00 18:00 22:00 |
| + // <----- + ----------- + ----------- + -----> |
| + // | | | |
| + // now start end |
| + // |
| + NightLightController* controller = GetController(); |
| + delegate()->SetFakeNow(NightLightTime(16 * 60)); // 4:00 PM. |
| + SetNightLightEnabled(false); |
| + 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
|
| + controller->SetCustomStartTime(NightLightTime(18 * 60)); // 6:00 PM. |
| + controller->SetCustomEndTime(NightLightTime(22 * 60)); // 10:00 PM. |
| + |
| + // Since now is outside the NightLight interval, changing the schedule type |
| + // to kCustom, shouldn't affect the status. Validate the timer is running with |
| + // a 2-hour delay. |
| + controller->SetScheduleType(NightLightController::ScheduleType::kCustom); |
| + EXPECT_FALSE(controller->GetEnabled()); |
| + EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| + EXPECT_TRUE(controller->timer().IsRunning()); |
| + EXPECT_EQ(base::TimeDelta::FromHours(2), |
| + controller->timer().GetCurrentDelay()); |
| + |
| + // Change the start time in such a way that doesn't change the status, but |
| + // despite that, confirm that schedule has been updated. |
| + controller->SetCustomStartTime(NightLightTime(19 * 60)); // 7:00 PM. |
| + EXPECT_FALSE(controller->GetEnabled()); |
| + EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| + EXPECT_TRUE(controller->timer().IsRunning()); |
| + EXPECT_EQ(base::TimeDelta::FromHours(3), |
| + controller->timer().GetCurrentDelay()); |
| + |
| + // Changing the end time in a similar fashion to the above and expect no |
| + // change. |
| + controller->SetCustomEndTime(NightLightTime(23 * 60)); // 11:00 PM. |
| + EXPECT_FALSE(controller->GetEnabled()); |
| + EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| + EXPECT_TRUE(controller->timer().IsRunning()); |
| + EXPECT_EQ(base::TimeDelta::FromHours(3), |
| + controller->timer().GetCurrentDelay()); |
| +} |
| + |
| +TEST_F(NightLightTest, TestSunsetSunrise) { |
| + // 16:00 18:00 20:00 22:00 5:00 |
| + // <----- + ----------- + ------- + -------- + --------------- + -------> |
| + // | | | | | |
| + // now custom start sunset custom end sunrise |
| + // |
| + NightLightController* controller = GetController(); |
| + delegate()->SetFakeNow(NightLightTime(16 * 60)); // 4:00 PM. |
| + delegate()->SetFakeSunset(NightLightTime(20 * 60)); // 8:00 PM. |
| + delegate()->SetFakeSunrise(NightLightTime(5 * 60)); // 5:00 AM. |
| + SetNightLightEnabled(false); |
| + controller->SetScheduleType(NightLightController::ScheduleType::kNever); |
| + controller->SetCustomStartTime(NightLightTime(18 * 60)); // 6:00 PM. |
| + controller->SetCustomEndTime(NightLightTime(22 * 60)); // 10:00 PM. |
| + |
| + // Custom times should have no effect when the schedule type is sunset to |
| + // sunrise. |
| + controller->SetScheduleType( |
| + NightLightController::ScheduleType::kSunsetToSunrise); |
| + EXPECT_FALSE(controller->GetEnabled()); |
| + EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| + EXPECT_TRUE(controller->timer().IsRunning()); |
| + EXPECT_EQ(base::TimeDelta::FromHours(4), |
| + controller->timer().GetCurrentDelay()); |
| + |
| + // Simulate reaching sunset. |
| + delegate()->SetFakeNow(NightLightTime(20 * 60)); // Now is 8:00 PM. |
| + controller->timer().user_task().Run(); |
| + EXPECT_TRUE(controller->GetEnabled()); |
| + EXPECT_TRUE(TestLayersTemperature(controller->GetColorTemperature())); |
| + EXPECT_EQ(NightLightController::AnimationDurationType::kLong, |
| + controller->consumed_animation_type()); |
| + // Timer is running scheduling the end at sunrise. |
| + EXPECT_TRUE(controller->timer().IsRunning()); |
| + EXPECT_EQ(base::TimeDelta::FromHours(9), |
| + controller->timer().GetCurrentDelay()); |
| + |
| + // Simulate reaching sunrise. |
| + delegate()->SetFakeNow(NightLightTime(5 * 60)); // Now is 5:00 AM. |
| + controller->timer().user_task().Run(); |
| + EXPECT_FALSE(controller->GetEnabled()); |
| + EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| + EXPECT_EQ(NightLightController::AnimationDurationType::kLong, |
| + controller->consumed_animation_type()); |
| + // Timer is running scheduling the start at the next sunset. |
| + EXPECT_TRUE(controller->timer().IsRunning()); |
| + EXPECT_EQ(base::TimeDelta::FromHours(15), |
| + controller->timer().GetCurrentDelay()); |
| +} |
|
James Cook
2017/05/23 16:34:59
Nice test suite. Thorough and easy to read.
afakhry
2017/05/24 04:21:10
Acknowledged.
|
| + |
| } // namespace |
| } // namespace ash |