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