1 // Copyright 2013 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.
7 #include "ash/magnifier/magnifier_constants.h"
8 #include "base/basictypes.h"
10 #include "base/bind_helpers.h"
11 #include "base/command_line.h"
12 #include "base/compiler_specific.h"
13 #include "base/location.h"
14 #include "base/message_loop.h"
15 #include "base/prefs/pref_change_registrar.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/run_loop.h"
18 #include "base/values.h"
19 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
20 #include "chrome/browser/chromeos/accessibility/magnification_manager.h"
21 #include "chrome/browser/chromeos/policy/device_policy_builder.h"
22 #include "chrome/browser/chromeos/policy/device_policy_cros_browser_test.h"
23 #include "chrome/browser/chromeos/profiles/profile_helper.h"
24 #include "chrome/browser/chromeos/settings/device_settings_service.h"
25 #include "chrome/browser/lifetime/application_lifetime.h"
26 #include "chrome/browser/policy/proto/chromeos/chrome_device_policy.pb.h"
27 #include "chrome/browser/profiles/profile.h"
28 #include "chrome/browser/profiles/profile_manager.h"
29 #include "chrome/common/pref_names.h"
30 #include "chromeos/chromeos_switches.h"
31 #include "testing/gtest/include/gtest/gtest.h"
33 namespace em
= enterprise_management
;
39 const em::AccessibilitySettingsProto_ScreenMagnifierType kFullScreenMagnifier
=
40 em::AccessibilitySettingsProto_ScreenMagnifierType_SCREEN_MAGNIFIER_TYPE_FULL
;
42 // Spins the loop until a notification is received from |prefs| that the value
43 // of |pref_name| has changed. If the notification is received before Wait()
44 // has been called, Wait() returns immediately and no loop is spun.
45 class PrefChangeWatcher
{
47 PrefChangeWatcher(const char* pref_name
, PrefService
* prefs
);
56 base::RunLoop run_loop_
;
57 PrefChangeRegistrar registrar_
;
59 DISALLOW_COPY_AND_ASSIGN(PrefChangeWatcher
);
62 PrefChangeWatcher::PrefChangeWatcher(const char* pref_name
,
64 : pref_changed_(false) {
65 registrar_
.Init(prefs
);
66 registrar_
.Add(pref_name
, base::Bind(&PrefChangeWatcher::OnPrefChange
,
67 base::Unretained(this)));
70 void PrefChangeWatcher::Wait() {
75 void PrefChangeWatcher::OnPrefChange() {
82 class LoginScreenDefaultPolicyBrowsertestBase
83 : public DevicePolicyCrosBrowserTest
{
85 LoginScreenDefaultPolicyBrowsertestBase();
86 virtual ~LoginScreenDefaultPolicyBrowsertestBase();
88 // DevicePolicyCrosBrowserTest:
89 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE
;
90 virtual void SetUpOnMainThread() OVERRIDE
;
92 void RefreshDevicePolicyAndWaitForPrefChange(const char* pref_name
);
94 Profile
* login_profile_
;
97 DISALLOW_COPY_AND_ASSIGN(LoginScreenDefaultPolicyBrowsertestBase
);
100 class LoginScreenDefaultPolicyLoginScreenBrowsertest
101 : public LoginScreenDefaultPolicyBrowsertestBase
{
103 LoginScreenDefaultPolicyLoginScreenBrowsertest();
104 virtual ~LoginScreenDefaultPolicyLoginScreenBrowsertest();
106 // LoginScreenDefaultPolicyBrowsertestBase:
107 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
;
108 virtual void SetUpOnMainThread() OVERRIDE
;
109 virtual void CleanUpOnMainThread() OVERRIDE
;
111 void VerifyPrefFollowsRecommendation(const char* pref_name
,
112 const base::Value
& recommended_value
);
115 DISALLOW_COPY_AND_ASSIGN(LoginScreenDefaultPolicyLoginScreenBrowsertest
);
118 class LoginScreenDefaultPolicyInSessionBrowsertest
119 : public LoginScreenDefaultPolicyBrowsertestBase
{
121 LoginScreenDefaultPolicyInSessionBrowsertest();
122 virtual ~LoginScreenDefaultPolicyInSessionBrowsertest();
124 // LoginScreenDefaultPolicyBrowsertestBase:
125 virtual void SetUpOnMainThread() OVERRIDE
;
127 void VerifyPrefFollowsDefault(const char* pref_name
);
130 DISALLOW_COPY_AND_ASSIGN(LoginScreenDefaultPolicyInSessionBrowsertest
);
133 LoginScreenDefaultPolicyBrowsertestBase::
134 LoginScreenDefaultPolicyBrowsertestBase() : login_profile_(NULL
) {
137 LoginScreenDefaultPolicyBrowsertestBase::
138 ~LoginScreenDefaultPolicyBrowsertestBase() {
141 void LoginScreenDefaultPolicyBrowsertestBase::
142 SetUpInProcessBrowserTestFixture() {
144 MarkAsEnterpriseOwned();
145 DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture();
148 void LoginScreenDefaultPolicyBrowsertestBase::SetUpOnMainThread() {
149 DevicePolicyCrosBrowserTest::SetUpOnMainThread();
150 login_profile_
= chromeos::ProfileHelper::GetSigninProfile();
151 ASSERT_TRUE(login_profile_
);
154 void LoginScreenDefaultPolicyBrowsertestBase::
155 RefreshDevicePolicyAndWaitForPrefChange(const char* pref_name
) {
156 PrefChangeWatcher
watcher(pref_name
, login_profile_
->GetPrefs());
157 RefreshDevicePolicy();
161 LoginScreenDefaultPolicyLoginScreenBrowsertest::
162 LoginScreenDefaultPolicyLoginScreenBrowsertest() {
165 LoginScreenDefaultPolicyLoginScreenBrowsertest::
166 ~LoginScreenDefaultPolicyLoginScreenBrowsertest() {
169 void LoginScreenDefaultPolicyLoginScreenBrowsertest::SetUpCommandLine(
170 CommandLine
* command_line
) {
171 LoginScreenDefaultPolicyBrowsertestBase::SetUpCommandLine(command_line
);
172 command_line
->AppendSwitch(chromeos::switches::kLoginManager
);
173 command_line
->AppendSwitch(chromeos::switches::kForceLoginManagerInTests
);
176 void LoginScreenDefaultPolicyLoginScreenBrowsertest::SetUpOnMainThread() {
177 LoginScreenDefaultPolicyBrowsertestBase::SetUpOnMainThread();
179 // Set the login screen profile.
180 chromeos::AccessibilityManager
* accessibility_manager
=
181 chromeos::AccessibilityManager::Get();
182 ASSERT_TRUE(accessibility_manager
);
183 accessibility_manager
->SetProfileForTest(
184 chromeos::ProfileHelper::GetSigninProfile());
186 chromeos::MagnificationManager
* magnification_manager
=
187 chromeos::MagnificationManager::Get();
188 ASSERT_TRUE(magnification_manager
);
189 magnification_manager
->SetProfileForTest(
190 chromeos::ProfileHelper::GetSigninProfile());
193 void LoginScreenDefaultPolicyLoginScreenBrowsertest::CleanUpOnMainThread() {
194 base::MessageLoop::current()->PostTask(FROM_HERE
,
195 base::Bind(&chrome::AttemptExit
));
196 base::RunLoop().RunUntilIdle();
197 LoginScreenDefaultPolicyBrowsertestBase::CleanUpOnMainThread();
200 void LoginScreenDefaultPolicyLoginScreenBrowsertest::
201 VerifyPrefFollowsRecommendation(const char* pref_name
,
202 const base::Value
& recommended_value
) {
203 const PrefService::Preference
* pref
=
204 login_profile_
->GetPrefs()->FindPreference(pref_name
);
206 EXPECT_FALSE(pref
->IsManaged());
207 EXPECT_FALSE(pref
->IsDefaultValue());
208 EXPECT_TRUE(base::Value::Equals(&recommended_value
, pref
->GetValue()));
209 EXPECT_TRUE(base::Value::Equals(&recommended_value
,
210 pref
->GetRecommendedValue()));
213 LoginScreenDefaultPolicyInSessionBrowsertest::
214 LoginScreenDefaultPolicyInSessionBrowsertest() {
217 LoginScreenDefaultPolicyInSessionBrowsertest::
218 ~LoginScreenDefaultPolicyInSessionBrowsertest() {
221 void LoginScreenDefaultPolicyInSessionBrowsertest::SetUpOnMainThread() {
222 LoginScreenDefaultPolicyBrowsertestBase::SetUpOnMainThread();
224 // Tell the DeviceSettingsService that there is no local owner.
225 chromeos::DeviceSettingsService::Get()->SetUsername(std::string());
228 void LoginScreenDefaultPolicyInSessionBrowsertest::VerifyPrefFollowsDefault(
229 const char* pref_name
) {
230 Profile
* profile
= ProfileManager::GetDefaultProfile();
231 ASSERT_TRUE(profile
);
232 const PrefService::Preference
* pref
=
233 profile
->GetPrefs()->FindPreference(pref_name
);
235 EXPECT_FALSE(pref
->IsManaged());
236 EXPECT_TRUE(pref
->IsDefaultValue());
237 EXPECT_FALSE(pref
->GetRecommendedValue());
240 IN_PROC_BROWSER_TEST_F(LoginScreenDefaultPolicyLoginScreenBrowsertest
,
241 DeviceLoginScreenDefaultLargeCursorEnabled
) {
242 // Verifies that the default state of the large cursor accessibility feature
243 // on the login screen can be controlled through device policy.
245 // Enable the large cursor through device policy and wait for the change to
247 em::ChromeDeviceSettingsProto
& proto(device_policy()->payload());
248 proto
.mutable_accessibility_settings()->
249 set_login_screen_default_large_cursor_enabled(true);
250 RefreshDevicePolicyAndWaitForPrefChange(prefs::kLargeCursorEnabled
);
252 // Verify that the pref which controls the large cursor in the login profile
253 // has changed to the policy-supplied default.
254 VerifyPrefFollowsRecommendation(prefs::kLargeCursorEnabled
,
255 base::FundamentalValue(true));
257 // Verify that the large cursor is enabled.
258 chromeos::AccessibilityManager
* accessibility_manager
=
259 chromeos::AccessibilityManager::Get();
260 ASSERT_TRUE(accessibility_manager
);
261 EXPECT_TRUE(accessibility_manager
->IsLargeCursorEnabled());
264 IN_PROC_BROWSER_TEST_F(LoginScreenDefaultPolicyLoginScreenBrowsertest
,
265 DeviceLoginScreenDefaultSpokenFeedbackEnabled
) {
266 // Verifies that the default state of the spoken feedback accessibility
267 // feature on the login screen can be controlled through device policy.
269 // Enable spoken feedback through device policy and wait for the change to
271 em::ChromeDeviceSettingsProto
& proto(device_policy()->payload());
272 proto
.mutable_accessibility_settings()->
273 set_login_screen_default_spoken_feedback_enabled(true);
274 RefreshDevicePolicyAndWaitForPrefChange(prefs::kSpokenFeedbackEnabled
);
276 // Verify that the pref which controls spoken feedback in the login profile
277 // has changed to the policy-supplied default.
278 VerifyPrefFollowsRecommendation(prefs::kSpokenFeedbackEnabled
,
279 base::FundamentalValue(true));
281 // Verify that spoken feedback is enabled.
282 chromeos::AccessibilityManager
* accessibility_manager
=
283 chromeos::AccessibilityManager::Get();
284 ASSERT_TRUE(accessibility_manager
);
285 EXPECT_TRUE(accessibility_manager
->IsSpokenFeedbackEnabled());
288 IN_PROC_BROWSER_TEST_F(LoginScreenDefaultPolicyLoginScreenBrowsertest
,
289 DeviceLoginScreenDefaultHighContrastEnabled
) {
290 // Verifies that the default state of the high contrast mode accessibility
291 // feature on the login screen can be controlled through device policy.
293 // Enable high contrast mode through device policy and wait for the change to
295 em::ChromeDeviceSettingsProto
& proto(device_policy()->payload());
296 proto
.mutable_accessibility_settings()->
297 set_login_screen_default_high_contrast_enabled(true);
298 RefreshDevicePolicyAndWaitForPrefChange(prefs::kHighContrastEnabled
);
300 // Verify that the pref which controls high contrast mode in the login profile
301 // has changed to the policy-supplied default.
302 VerifyPrefFollowsRecommendation(prefs::kHighContrastEnabled
,
303 base::FundamentalValue(true));
305 // Verify that high contrast mode is enabled.
306 chromeos::AccessibilityManager
* accessibility_manager
=
307 chromeos::AccessibilityManager::Get();
308 ASSERT_TRUE(accessibility_manager
);
309 EXPECT_TRUE(accessibility_manager
->IsHighContrastEnabled());
312 IN_PROC_BROWSER_TEST_F(LoginScreenDefaultPolicyLoginScreenBrowsertest
,
313 DeviceLoginScreenDefaultScreenMagnifierType
) {
314 // Verifies that the default screen magnifier type enabled on the login screen
315 // can be controlled through device policy.
317 // Set the screen magnifier type through device policy and wait for the change
319 em::ChromeDeviceSettingsProto
& proto(device_policy()->payload());
320 proto
.mutable_accessibility_settings()->
321 set_login_screen_default_screen_magnifier_type(kFullScreenMagnifier
);
322 RefreshDevicePolicyAndWaitForPrefChange(prefs::kScreenMagnifierType
);
324 // Verify that the prefs which control the screen magnifier type have changed
325 // to the policy-supplied default.
326 VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierEnabled
,
327 base::FundamentalValue(true));
328 VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierType
,
329 base::FundamentalValue(ash::MAGNIFIER_FULL
));
331 // Verify that the full-screen magnifier is enabled.
332 chromeos::MagnificationManager
* magnification_manager
=
333 chromeos::MagnificationManager::Get();
334 ASSERT_TRUE(magnification_manager
);
335 EXPECT_TRUE(magnification_manager
->IsMagnifierEnabled());
336 EXPECT_EQ(ash::MAGNIFIER_FULL
, magnification_manager
->GetMagnifierType());
339 IN_PROC_BROWSER_TEST_F(LoginScreenDefaultPolicyInSessionBrowsertest
,
340 DeviceLoginScreenDefaultLargeCursorEnabled
) {
341 // Verifies that changing the default state of the large cursor accessibility
342 // feature on the login screen through policy does not affect its state in a
345 // Enable the large cursor through device policy and wait for the change to
347 em::ChromeDeviceSettingsProto
& proto(device_policy()->payload());
348 proto
.mutable_accessibility_settings()->
349 set_login_screen_default_large_cursor_enabled(true);
350 RefreshDevicePolicyAndWaitForPrefChange(prefs::kLargeCursorEnabled
);
352 // Verify that the pref which controls the large cursor in the session is
354 VerifyPrefFollowsDefault(prefs::kLargeCursorEnabled
);
356 // Verify that the large cursor is disabled.
357 chromeos::AccessibilityManager
* accessibility_manager
=
358 chromeos::AccessibilityManager::Get();
359 ASSERT_TRUE(accessibility_manager
);
360 EXPECT_FALSE(accessibility_manager
->IsLargeCursorEnabled());
363 IN_PROC_BROWSER_TEST_F(LoginScreenDefaultPolicyInSessionBrowsertest
,
364 DeviceLoginScreenDefaultSpokenFeedbackEnabled
) {
365 // Verifies that changing the default state of the spoken feedback
366 // accessibility feature on the login screen through policy does not affect
367 // its state in a session.
369 // Enable spoken feedback through device policy and wait for the change to
371 em::ChromeDeviceSettingsProto
& proto(device_policy()->payload());
372 proto
.mutable_accessibility_settings()->
373 set_login_screen_default_spoken_feedback_enabled(true);
374 RefreshDevicePolicyAndWaitForPrefChange(prefs::kSpokenFeedbackEnabled
);
376 // Verify that the pref which controls the spoken feedback in the session is
378 VerifyPrefFollowsDefault(prefs::kSpokenFeedbackEnabled
);
380 // Verify that spoken feedback is disabled.
381 chromeos::AccessibilityManager
* accessibility_manager
=
382 chromeos::AccessibilityManager::Get();
383 ASSERT_TRUE(accessibility_manager
);
384 EXPECT_FALSE(accessibility_manager
->IsSpokenFeedbackEnabled());
387 IN_PROC_BROWSER_TEST_F(LoginScreenDefaultPolicyInSessionBrowsertest
,
388 DeviceLoginScreenDefaultHighContrastEnabled
) {
389 // Verifies that changing the default state of the high contrast mode
390 // accessibility feature on the login screen through policy does not affect
391 // its state in a session.
393 // Enable high contrast mode through device policy and wait for the change to
395 em::ChromeDeviceSettingsProto
& proto(device_policy()->payload());
396 proto
.mutable_accessibility_settings()->
397 set_login_screen_default_high_contrast_enabled(true);
398 RefreshDevicePolicyAndWaitForPrefChange(prefs::kHighContrastEnabled
);
400 // Verify that the pref which controls high contrast mode in the session is
402 VerifyPrefFollowsDefault(prefs::kHighContrastEnabled
);
404 // Verify that high contrast mode is disabled.
405 chromeos::AccessibilityManager
* accessibility_manager
=
406 chromeos::AccessibilityManager::Get();
407 ASSERT_TRUE(accessibility_manager
);
408 EXPECT_FALSE(accessibility_manager
->IsHighContrastEnabled());
411 IN_PROC_BROWSER_TEST_F(LoginScreenDefaultPolicyInSessionBrowsertest
,
412 DeviceLoginScreenDefaultScreenMagnifierType
) {
413 // Verifies that changing the default screen magnifier type enabled on the
414 // login screen through policy does not affect its state in a session.
416 // Set the screen magnifier type through device policy and wait for the change
418 em::ChromeDeviceSettingsProto
& proto(device_policy()->payload());
419 proto
.mutable_accessibility_settings()->
420 set_login_screen_default_screen_magnifier_type(kFullScreenMagnifier
);
421 RefreshDevicePolicyAndWaitForPrefChange(prefs::kScreenMagnifierType
);
423 // Verify that the prefs which control the screen magnifier in the session are
425 VerifyPrefFollowsDefault(prefs::kScreenMagnifierEnabled
);
426 VerifyPrefFollowsDefault(prefs::kScreenMagnifierType
);
428 // Verify that the screen magnifier is disabled.
429 chromeos::MagnificationManager
* magnification_manager
=
430 chromeos::MagnificationManager::Get();
431 ASSERT_TRUE(magnification_manager
);
432 EXPECT_FALSE(magnification_manager
->IsMagnifierEnabled());
433 EXPECT_EQ(ash::kDefaultMagnifierType
,
434 magnification_manager
->GetMagnifierType());
437 } // namespace policy