OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
James Cook
2017/05/05 17:12:32
I would put this file in night_light_controller_un
afakhry
2017/05/05 20:04:11
Done.
| |
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/public/cpp/config.h" | |
6 #include "ash/public/cpp/session_types.h" | |
7 #include "ash/shell.h" | |
8 #include "ash/system/night_light/night_light_controller.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 "components/prefs/testing_pref_service.h" | |
14 #include "ui/compositor/layer.h" | |
15 | |
16 namespace ash { | |
17 | |
18 namespace { | |
19 | |
20 constexpr char user_1_email[] = "user1@nightlight"; | |
21 constexpr char user_2_email[] = "user2@nightlight"; | |
22 | |
23 NightLightController* GetController() { | |
24 return Shell::Get()->night_light_controller(); | |
25 } | |
26 | |
27 // Tests that the color temperatures of all root layers are equal to the given | |
28 // |expected_temperature| and returns true if so, or false otherwise. | |
29 bool TestLayersTemperature(float expected_temperature) { | |
30 for (aura::Window* root_window : ash::Shell::GetAllRootWindows()) { | |
James Cook
2017/05/05 17:12:32
By default AshTestBase only creates a single displ
afakhry
2017/05/05 20:04:11
Oh, I like UpdateDisplay(). That makes the tests m
| |
31 ui::Layer* layer = root_window->layer(); | |
32 if (expected_temperature != layer->layer_temperature()) | |
33 return false; | |
34 } | |
35 | |
36 return true; | |
37 } | |
38 | |
39 class TestObserver : public NightLightController::Observer { | |
40 public: | |
41 TestObserver() { GetController()->AddObserver(this); } | |
42 ~TestObserver() override { GetController()->RemoveObserver(this); } | |
43 | |
44 // ash::NightLightController::Observer: | |
45 void OnStatusChanged(bool new_status) override { status_ = new_status; } | |
46 | |
47 bool status() const { return status_; } | |
48 | |
49 private: | |
50 bool status_ = false; | |
51 | |
52 DISALLOW_COPY_AND_ASSIGN(TestObserver); | |
53 }; | |
54 | |
55 class NightLightTest : public test::AshTestBase { | |
56 public: | |
57 NightLightTest() {} | |
James Cook
2017/05/05 17:12:32
nit: = default, and below
afakhry
2017/05/05 20:04:11
Done.
| |
58 ~NightLightTest() override {} | |
59 | |
60 // ash::test::AshTestBase: | |
61 void SetUp() override { | |
62 test::AshTestBase::SetUp(); | |
63 NightLightController::DisableAnimationsForTests(); | |
64 } | |
65 | |
66 void CreateTestUserSessions() { | |
67 GetSessionControllerClient()->Reset(); | |
68 GetSessionControllerClient()->AddUserSession(user_1_email); | |
69 GetSessionControllerClient()->AddUserSession(user_2_email); | |
70 } | |
71 | |
72 void SwitchActiveUser(const std::string& email) { | |
73 GetSessionControllerClient()->SwitchActiveUser( | |
74 AccountId::FromUserEmail(email)); | |
75 } | |
76 | |
77 void InjectTestPrefService(PrefService* pref_service) { | |
78 ash_test_helper()->test_shell_delegate()->set_active_user_pref_service( | |
79 pref_service); | |
80 } | |
81 | |
82 private: | |
83 DISALLOW_COPY_AND_ASSIGN(NightLightTest); | |
James Cook
2017/05/05 17:12:31
#include base/macros.h
afakhry
2017/05/05 20:04:11
Done.
| |
84 }; | |
85 | |
86 // Tests toggling NightLight on /off and makes sure the observer is updated and | |
James Cook
2017/05/05 17:12:32
super nit: "on/off" or "on / off"
afakhry
2017/05/05 20:04:11
My bad. Done.
| |
87 // the layer temperatures are modified. | |
James Cook
2017/05/05 17:12:32
Thanks for documenting what you are testing.
afakhry
2017/05/05 20:04:11
Acknowledged.
| |
88 TEST_F(NightLightTest, TestToggle) { | |
89 TestObserver observer; | |
90 const bool initial_enabled = GetController()->enabled(); | |
James Cook
2017/05/05 17:12:32
nit: I don't think you need this variable, just in
afakhry
2017/05/05 20:04:11
I was planning to use it, but I changed my mind an
| |
91 ASSERT_FALSE(initial_enabled); | |
92 EXPECT_TRUE(TestLayersTemperature(0.0f)); | |
93 GetController()->Toggle(); | |
James Cook
2017/05/05 17:12:32
optional: Just cache a NightLightController* contr
afakhry
2017/05/05 20:04:11
Done.
| |
94 EXPECT_TRUE(GetController()->enabled()); | |
95 EXPECT_TRUE(observer.status()); | |
96 EXPECT_TRUE(TestLayersTemperature(GetController()->color_temperature())); | |
97 GetController()->Toggle(); | |
98 EXPECT_FALSE(GetController()->enabled()); | |
99 EXPECT_FALSE(observer.status()); | |
100 EXPECT_TRUE(TestLayersTemperature(0.0f)); | |
101 } | |
102 | |
103 // Tests that switching users retrieves NightLight settings for the active | |
104 // user's prefs. | |
105 TEST_F(NightLightTest, TestUserSwitchAndSettingsPersistence) { | |
106 if (Shell::GetAshConfig() == Config::MASH) { | |
107 // Doesn't work on mash. | |
James Cook
2017/05/05 17:12:32
Can you clarify why? (Feature doesn't work at all
afakhry
2017/05/05 20:04:11
I think it's because of user switching doesn't wor
| |
108 return; | |
109 } | |
110 | |
111 CreateTestUserSessions(); | |
112 TestingPrefServiceSimple user_1_pref_service; | |
113 TestingPrefServiceSimple user_2_pref_service; | |
114 NightLightController::RegisterPrefs(user_1_pref_service.registry()); | |
115 NightLightController::RegisterPrefs(user_2_pref_service.registry()); | |
116 | |
117 // Simulate user 1 login. | |
118 InjectTestPrefService(&user_1_pref_service); | |
119 SwitchActiveUser(user_1_email); | |
120 GetController()->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(GetController()->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(GetController()->enabled()); | |
133 } | |
James Cook
2017/05/05 17:12:32
Nice tests. Easy to read. Good balance between cla
afakhry
2017/05/05 20:04:11
Thank you very much!
| |
134 | |
135 } // namespace | |
136 | |
137 } // namespace ash | |
OLD | NEW |