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

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

Issue 2857103007: [Night Light] CL2: Ash and system tray work (Closed)
Patch Set: James' comments 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
(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/system/night_light/tray_night_light.h"
10 #include "ash/system/tray/system_tray.h"
11 #include "ash/test/ash_test_base.h"
12 #include "ash/test/ash_test_helper.h"
13 #include "ash/test/test_session_controller_client.h"
14 #include "ash/test/test_shell_delegate.h"
15 #include "base/macros.h"
16 #include "components/prefs/testing_pref_service.h"
17 #include "ui/compositor/layer.h"
18
19 namespace ash {
20
21 namespace {
22
23 constexpr char user_1_email[] = "user1@nightlight";
24 constexpr char user_2_email[] = "user2@nightlight";
25
26 NightLightController* GetController() {
27 return Shell::Get()->night_light_controller();
28 }
29
30 // Tests that the color temperatures of all root layers are equal to the given
31 // |expected_temperature| and returns true if so, or false otherwise.
32 bool TestLayersTemperature(float expected_temperature) {
33 for (aura::Window* root_window : ash::Shell::GetAllRootWindows()) {
34 ui::Layer* layer = root_window->layer();
35 if (expected_temperature != layer->layer_temperature())
36 return false;
37 }
38
39 return true;
40 }
41
42 class TestObserver : public NightLightController::Observer {
43 public:
44 TestObserver() { GetController()->AddObserver(this); }
45 ~TestObserver() override { GetController()->RemoveObserver(this); }
46
47 // ash::NightLightController::Observer:
48 void OnNightLightEnabledChanged(bool enabled) override { status_ = enabled; }
49
50 bool status() const { return status_; }
51
52 private:
53 bool status_ = false;
54
55 DISALLOW_COPY_AND_ASSIGN(TestObserver);
56 };
57
58 class NightLightTest : public test::AshTestBase {
59 public:
60 NightLightTest() = default;
61 ~NightLightTest() override = default;
62
63 void CreateTestUserSessions() {
64 GetSessionControllerClient()->Reset();
65 GetSessionControllerClient()->AddUserSession(user_1_email);
66 GetSessionControllerClient()->AddUserSession(user_2_email);
67 }
68
69 void SwitchActiveUser(const std::string& email) {
70 GetSessionControllerClient()->SwitchActiveUser(
71 AccountId::FromUserEmail(email));
72 }
73
74 void InjectTestPrefService(PrefService* pref_service) {
75 ash_test_helper()->test_shell_delegate()->set_active_user_pref_service(
76 pref_service);
77 }
78
79 private:
80 DISALLOW_COPY_AND_ASSIGN(NightLightTest);
81 };
82
83 // Tests toggling NightLight on / off and makes sure the observer is updated and
84 // the layer temperatures are modified.
85 TEST_F(NightLightTest, TestToggle) {
86 UpdateDisplay("800x600,800x600");
87
88 TestObserver observer;
89 NightLightController* controller = GetController();
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 // Tests that when NightLight is active, its tray icon in the System Tray is
136 // visible.
137 TEST_F(NightLightTest, TestNightLightTrayVisibility) {
James Cook 2017/05/05 20:47:43 Since this tests TrayNightLight, put it in a new f
afakhry 2017/05/06 00:49:35 Good point. Done.
138 SystemTray* tray = GetPrimarySystemTray();
139 TrayNightLight* tray_night_light = tray->tray_night_light();
140 NightLightController* controller = GetController();
141 ASSERT_FALSE(controller->enabled());
142 EXPECT_FALSE(tray_night_light->tray_view()->visible());
143 controller->Toggle();
144 EXPECT_TRUE(tray_night_light->tray_view()->visible());
145 }
146
147 } // namespace
148
149 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698