OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ash/system/night_light/night_light_controller.h" |
| 6 |
| 7 #include "ash/public/cpp/config.h" |
| 8 #include "ash/public/cpp/session_types.h" |
| 9 #include "ash/shell.h" |
| 10 #include "ash/test/ash_test_base.h" |
| 11 #include "ash/test/ash_test_helper.h" |
| 12 #include "ash/test/test_session_controller_client.h" |
| 13 #include "ash/test/test_shell_delegate.h" |
| 14 #include "base/macros.h" |
| 15 #include "components/prefs/testing_pref_service.h" |
| 16 #include "ui/compositor/layer.h" |
| 17 |
| 18 namespace ash { |
| 19 |
| 20 namespace { |
| 21 |
| 22 constexpr char user_1_email[] = "user1@nightlight"; |
| 23 constexpr char user_2_email[] = "user2@nightlight"; |
| 24 |
| 25 NightLightController* GetController() { |
| 26 return Shell::Get()->night_light_controller(); |
| 27 } |
| 28 |
| 29 // Tests that the color temperatures of all root layers are equal to the given |
| 30 // |expected_temperature| and returns true if so, or false otherwise. |
| 31 bool TestLayersTemperature(float expected_temperature) { |
| 32 for (aura::Window* root_window : ash::Shell::GetAllRootWindows()) { |
| 33 ui::Layer* layer = root_window->layer(); |
| 34 if (expected_temperature != layer->layer_temperature()) |
| 35 return false; |
| 36 } |
| 37 |
| 38 return true; |
| 39 } |
| 40 |
| 41 class TestObserver : public NightLightController::Observer { |
| 42 public: |
| 43 TestObserver() { GetController()->AddObserver(this); } |
| 44 ~TestObserver() override { GetController()->RemoveObserver(this); } |
| 45 |
| 46 // ash::NightLightController::Observer: |
| 47 void OnNightLightEnabledChanged(bool enabled) override { status_ = enabled; } |
| 48 |
| 49 bool status() const { return status_; } |
| 50 |
| 51 private: |
| 52 bool status_ = false; |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(TestObserver); |
| 55 }; |
| 56 |
| 57 class NightLightTest : public test::AshTestBase { |
| 58 public: |
| 59 NightLightTest() = default; |
| 60 ~NightLightTest() override = default; |
| 61 |
| 62 void CreateTestUserSessions() { |
| 63 GetSessionControllerClient()->Reset(); |
| 64 GetSessionControllerClient()->AddUserSession(user_1_email); |
| 65 GetSessionControllerClient()->AddUserSession(user_2_email); |
| 66 } |
| 67 |
| 68 void SwitchActiveUser(const std::string& email) { |
| 69 GetSessionControllerClient()->SwitchActiveUser( |
| 70 AccountId::FromUserEmail(email)); |
| 71 } |
| 72 |
| 73 void InjectTestPrefService(PrefService* pref_service) { |
| 74 ash_test_helper()->test_shell_delegate()->set_active_user_pref_service( |
| 75 pref_service); |
| 76 } |
| 77 |
| 78 private: |
| 79 DISALLOW_COPY_AND_ASSIGN(NightLightTest); |
| 80 }; |
| 81 |
| 82 // Tests toggling NightLight on / off and makes sure the observer is updated and |
| 83 // the layer temperatures are modified. |
| 84 TEST_F(NightLightTest, TestToggle) { |
| 85 UpdateDisplay("800x600,800x600"); |
| 86 |
| 87 TestObserver observer; |
| 88 NightLightController* controller = GetController(); |
| 89 controller->SetEnabled(false); |
| 90 ASSERT_FALSE(controller->enabled()); |
| 91 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 92 controller->Toggle(); |
| 93 EXPECT_TRUE(controller->enabled()); |
| 94 EXPECT_TRUE(observer.status()); |
| 95 EXPECT_TRUE(TestLayersTemperature(GetController()->color_temperature())); |
| 96 controller->Toggle(); |
| 97 EXPECT_FALSE(controller->enabled()); |
| 98 EXPECT_FALSE(observer.status()); |
| 99 EXPECT_TRUE(TestLayersTemperature(0.0f)); |
| 100 } |
| 101 |
| 102 // Tests that switching users retrieves NightLight settings for the active |
| 103 // user's prefs. |
| 104 TEST_F(NightLightTest, TestUserSwitchAndSettingsPersistence) { |
| 105 if (Shell::GetAshConfig() == Config::MASH) { |
| 106 // User switching doesn't work on mash. |
| 107 return; |
| 108 } |
| 109 |
| 110 CreateTestUserSessions(); |
| 111 TestingPrefServiceSimple user_1_pref_service; |
| 112 TestingPrefServiceSimple user_2_pref_service; |
| 113 NightLightController::RegisterPrefs(user_1_pref_service.registry()); |
| 114 NightLightController::RegisterPrefs(user_2_pref_service.registry()); |
| 115 |
| 116 // Simulate user 1 login. |
| 117 InjectTestPrefService(&user_1_pref_service); |
| 118 SwitchActiveUser(user_1_email); |
| 119 NightLightController* controller = GetController(); |
| 120 controller->SetEnabled(true); |
| 121 EXPECT_TRUE(GetController()->enabled()); |
| 122 EXPECT_TRUE(TestLayersTemperature(GetController()->color_temperature())); |
| 123 |
| 124 // Switch to user 2, and expect NightLight to be disabled. |
| 125 InjectTestPrefService(&user_2_pref_service); |
| 126 SwitchActiveUser(user_2_email); |
| 127 EXPECT_FALSE(controller->enabled()); |
| 128 |
| 129 // Switch back to user 1, to find NightLight is still enabled. |
| 130 InjectTestPrefService(&user_1_pref_service); |
| 131 SwitchActiveUser(user_1_email); |
| 132 EXPECT_TRUE(controller->enabled()); |
| 133 } |
| 134 |
| 135 } // namespace |
| 136 |
| 137 } // namespace ash |
OLD | NEW |