login_screen_default_policy_browsertest.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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. 4 5#include <string> 6 7#include "ash/magnifier/magnifier_constants.h" 8#include "base/basictypes.h" 9#include "base/bind.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/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/policy/proto/chrome_device_policy.pb.h" 24#include "chrome/browser/chromeos/profiles/profile_helper.h" 25#include "chrome/browser/chromeos/settings/device_settings_service.h" 26#include "chrome/browser/lifetime/application_lifetime.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" 32 33namespace em = enterprise_management; 34 35namespace policy { 36 37namespace { 38 39const em::AccessibilitySettingsProto_ScreenMagnifierType kFullScreenMagnifier = 40 em::AccessibilitySettingsProto_ScreenMagnifierType_SCREEN_MAGNIFIER_TYPE_FULL; 41 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. 45class PrefChangeWatcher { 46 public: 47 PrefChangeWatcher(const char* pref_name, PrefService* prefs); 48 49 void Wait(); 50 51 void OnPrefChange(); 52 53 private: 54 bool pref_changed_; 55 56 base::RunLoop run_loop_; 57 PrefChangeRegistrar registrar_; 58 59 DISALLOW_COPY_AND_ASSIGN(PrefChangeWatcher); 60}; 61 62PrefChangeWatcher::PrefChangeWatcher(const char* pref_name, 63 PrefService* prefs) 64 : pref_changed_(false) { 65 registrar_.Init(prefs); 66 registrar_.Add(pref_name, base::Bind(&PrefChangeWatcher::OnPrefChange, 67 base::Unretained(this))); 68} 69 70void PrefChangeWatcher::Wait() { 71 if (!pref_changed_) 72 run_loop_.Run(); 73} 74 75void PrefChangeWatcher::OnPrefChange() { 76 pref_changed_ = true; 77 run_loop_.Quit(); 78} 79 80} // namespace 81 82class LoginScreenDefaultPolicyBrowsertestBase 83 : public DevicePolicyCrosBrowserTest { 84 protected: 85 LoginScreenDefaultPolicyBrowsertestBase(); 86 virtual ~LoginScreenDefaultPolicyBrowsertestBase(); 87 88 // DevicePolicyCrosBrowserTest: 89 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE; 90 virtual void SetUpOnMainThread() OVERRIDE; 91 92 void RefreshDevicePolicyAndWaitForPrefChange(const char* pref_name); 93 94 Profile* login_profile_; 95 96 private: 97 DISALLOW_COPY_AND_ASSIGN(LoginScreenDefaultPolicyBrowsertestBase); 98}; 99 100class LoginScreenDefaultPolicyLoginScreenBrowsertest 101 : public LoginScreenDefaultPolicyBrowsertestBase { 102 protected: 103 LoginScreenDefaultPolicyLoginScreenBrowsertest(); 104 virtual ~LoginScreenDefaultPolicyLoginScreenBrowsertest(); 105 106 // LoginScreenDefaultPolicyBrowsertestBase: 107 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE; 108 virtual void SetUpOnMainThread() OVERRIDE; 109 virtual void CleanUpOnMainThread() OVERRIDE; 110 111 void VerifyPrefFollowsRecommendation(const char* pref_name, 112 const base::Value& recommended_value); 113 114 private: 115 DISALLOW_COPY_AND_ASSIGN(LoginScreenDefaultPolicyLoginScreenBrowsertest); 116}; 117 118class LoginScreenDefaultPolicyInSessionBrowsertest 119 : public LoginScreenDefaultPolicyBrowsertestBase { 120 protected: 121 LoginScreenDefaultPolicyInSessionBrowsertest(); 122 virtual ~LoginScreenDefaultPolicyInSessionBrowsertest(); 123 124 // LoginScreenDefaultPolicyBrowsertestBase: 125 virtual void SetUpOnMainThread() OVERRIDE; 126 127 void VerifyPrefFollowsDefault(const char* pref_name); 128 129 private: 130 DISALLOW_COPY_AND_ASSIGN(LoginScreenDefaultPolicyInSessionBrowsertest); 131}; 132 133LoginScreenDefaultPolicyBrowsertestBase:: 134 LoginScreenDefaultPolicyBrowsertestBase() : login_profile_(NULL) { 135} 136 137LoginScreenDefaultPolicyBrowsertestBase:: 138 ~LoginScreenDefaultPolicyBrowsertestBase() { 139} 140 141void LoginScreenDefaultPolicyBrowsertestBase:: 142 SetUpInProcessBrowserTestFixture() { 143 InstallOwnerKey(); 144 MarkAsEnterpriseOwned(); 145 DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture(); 146} 147 148void LoginScreenDefaultPolicyBrowsertestBase::SetUpOnMainThread() { 149 DevicePolicyCrosBrowserTest::SetUpOnMainThread(); 150 login_profile_ = chromeos::ProfileHelper::GetSigninProfile(); 151 ASSERT_TRUE(login_profile_); 152} 153 154void LoginScreenDefaultPolicyBrowsertestBase:: 155 RefreshDevicePolicyAndWaitForPrefChange(const char* pref_name) { 156 PrefChangeWatcher watcher(pref_name, login_profile_->GetPrefs()); 157 RefreshDevicePolicy(); 158 watcher.Wait(); 159} 160 161LoginScreenDefaultPolicyLoginScreenBrowsertest:: 162 LoginScreenDefaultPolicyLoginScreenBrowsertest() { 163} 164 165LoginScreenDefaultPolicyLoginScreenBrowsertest:: 166 ~LoginScreenDefaultPolicyLoginScreenBrowsertest() { 167} 168 169void 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); 174} 175 176void LoginScreenDefaultPolicyLoginScreenBrowsertest::SetUpOnMainThread() { 177 LoginScreenDefaultPolicyBrowsertestBase::SetUpOnMainThread(); 178 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()); 185 186 chromeos::MagnificationManager* magnification_manager = 187 chromeos::MagnificationManager::Get(); 188 ASSERT_TRUE(magnification_manager); 189 magnification_manager->SetProfileForTest( 190 chromeos::ProfileHelper::GetSigninProfile()); 191} 192 193void LoginScreenDefaultPolicyLoginScreenBrowsertest::CleanUpOnMainThread() { 194 base::MessageLoop::current()->PostTask(FROM_HERE, 195 base::Bind(&chrome::AttemptExit)); 196 base::RunLoop().RunUntilIdle(); 197 LoginScreenDefaultPolicyBrowsertestBase::CleanUpOnMainThread(); 198} 199 200void 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); 205 ASSERT_TRUE(pref); 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())); 211} 212 213LoginScreenDefaultPolicyInSessionBrowsertest:: 214 LoginScreenDefaultPolicyInSessionBrowsertest() { 215} 216 217LoginScreenDefaultPolicyInSessionBrowsertest:: 218 ~LoginScreenDefaultPolicyInSessionBrowsertest() { 219} 220 221void LoginScreenDefaultPolicyInSessionBrowsertest::SetUpOnMainThread() { 222 LoginScreenDefaultPolicyBrowsertestBase::SetUpOnMainThread(); 223 224 // Tell the DeviceSettingsService that there is no local owner. 225 crypto::ScopedPK11Slot slot; 226 chromeos::DeviceSettingsService::Get()->InitOwner(std::string(), slot.Pass()); 227} 228 229void LoginScreenDefaultPolicyInSessionBrowsertest::VerifyPrefFollowsDefault( 230 const char* pref_name) { 231 Profile* profile = ProfileManager::GetActiveUserProfile(); 232 ASSERT_TRUE(profile); 233 const PrefService::Preference* pref = 234 profile->GetPrefs()->FindPreference(pref_name); 235 ASSERT_TRUE(pref); 236 EXPECT_FALSE(pref->IsManaged()); 237 EXPECT_TRUE(pref->IsDefaultValue()); 238 EXPECT_FALSE(pref->GetRecommendedValue()); 239} 240 241IN_PROC_BROWSER_TEST_F(LoginScreenDefaultPolicyLoginScreenBrowsertest, 242 DeviceLoginScreenDefaultLargeCursorEnabled) { 243 // Verifies that the default state of the large cursor accessibility feature 244 // on the login screen can be controlled through device policy. 245 246 // Enable the large cursor through device policy and wait for the change to 247 // take effect. 248 em::ChromeDeviceSettingsProto& proto(device_policy()->payload()); 249 proto.mutable_accessibility_settings()-> 250 set_login_screen_default_large_cursor_enabled(true); 251 RefreshDevicePolicyAndWaitForPrefChange(prefs::kLargeCursorEnabled); 252 253 // Verify that the pref which controls the large cursor in the login profile 254 // has changed to the policy-supplied default. 255 VerifyPrefFollowsRecommendation(prefs::kLargeCursorEnabled, 256 base::FundamentalValue(true)); 257 258 // Verify that the large cursor is enabled. 259 chromeos::AccessibilityManager* accessibility_manager = 260 chromeos::AccessibilityManager::Get(); 261 ASSERT_TRUE(accessibility_manager); 262 EXPECT_TRUE(accessibility_manager->IsLargeCursorEnabled()); 263} 264 265IN_PROC_BROWSER_TEST_F(LoginScreenDefaultPolicyLoginScreenBrowsertest, 266 DeviceLoginScreenDefaultSpokenFeedbackEnabled) { 267 // Verifies that the default state of the spoken feedback accessibility 268 // feature on the login screen can be controlled through device policy. 269 270 // Enable spoken feedback through device policy and wait for the change to 271 // take effect. 272 em::ChromeDeviceSettingsProto& proto(device_policy()->payload()); 273 proto.mutable_accessibility_settings()-> 274 set_login_screen_default_spoken_feedback_enabled(true); 275 RefreshDevicePolicyAndWaitForPrefChange(prefs::kSpokenFeedbackEnabled); 276 277 // Verify that the pref which controls spoken feedback in the login profile 278 // has changed to the policy-supplied default. 279 VerifyPrefFollowsRecommendation(prefs::kSpokenFeedbackEnabled, 280 base::FundamentalValue(true)); 281 282 // Verify that spoken feedback is enabled. 283 chromeos::AccessibilityManager* accessibility_manager = 284 chromeos::AccessibilityManager::Get(); 285 ASSERT_TRUE(accessibility_manager); 286 EXPECT_TRUE(accessibility_manager->IsSpokenFeedbackEnabled()); 287} 288 289IN_PROC_BROWSER_TEST_F(LoginScreenDefaultPolicyLoginScreenBrowsertest, 290 DeviceLoginScreenDefaultHighContrastEnabled) { 291 // Verifies that the default state of the high contrast mode accessibility 292 // feature on the login screen can be controlled through device policy. 293 294 // Enable high contrast mode through device policy and wait for the change to 295 // take effect. 296 em::ChromeDeviceSettingsProto& proto(device_policy()->payload()); 297 proto.mutable_accessibility_settings()-> 298 set_login_screen_default_high_contrast_enabled(true); 299 RefreshDevicePolicyAndWaitForPrefChange(prefs::kHighContrastEnabled); 300 301 // Verify that the pref which controls high contrast mode in the login profile 302 // has changed to the policy-supplied default. 303 VerifyPrefFollowsRecommendation(prefs::kHighContrastEnabled, 304 base::FundamentalValue(true)); 305 306 // Verify that high contrast mode is enabled. 307 chromeos::AccessibilityManager* accessibility_manager = 308 chromeos::AccessibilityManager::Get(); 309 ASSERT_TRUE(accessibility_manager); 310 EXPECT_TRUE(accessibility_manager->IsHighContrastEnabled()); 311} 312 313IN_PROC_BROWSER_TEST_F(LoginScreenDefaultPolicyLoginScreenBrowsertest, 314 DeviceLoginScreenDefaultScreenMagnifierType) { 315 // Verifies that the default screen magnifier type enabled on the login screen 316 // can be controlled through device policy. 317 318 // Set the screen magnifier type through device policy and wait for the change 319 // to take effect. 320 em::ChromeDeviceSettingsProto& proto(device_policy()->payload()); 321 proto.mutable_accessibility_settings()-> 322 set_login_screen_default_screen_magnifier_type(kFullScreenMagnifier); 323 RefreshDevicePolicyAndWaitForPrefChange(prefs::kScreenMagnifierType); 324 325 // Verify that the prefs which control the screen magnifier type have changed 326 // to the policy-supplied default. 327 VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierEnabled, 328 base::FundamentalValue(true)); 329 VerifyPrefFollowsRecommendation(prefs::kScreenMagnifierType, 330 base::FundamentalValue(ash::MAGNIFIER_FULL)); 331 332 // Verify that the full-screen magnifier is enabled. 333 chromeos::MagnificationManager* magnification_manager = 334 chromeos::MagnificationManager::Get(); 335 ASSERT_TRUE(magnification_manager); 336 EXPECT_TRUE(magnification_manager->IsMagnifierEnabled()); 337 EXPECT_EQ(ash::MAGNIFIER_FULL, magnification_manager->GetMagnifierType()); 338} 339 340IN_PROC_BROWSER_TEST_F(LoginScreenDefaultPolicyInSessionBrowsertest, 341 DeviceLoginScreenDefaultLargeCursorEnabled) { 342 // Verifies that changing the default state of the large cursor accessibility 343 // feature on the login screen through policy does not affect its state in a 344 // session. 345 346 // Enable the large cursor through device policy and wait for the change to 347 // take effect. 348 em::ChromeDeviceSettingsProto& proto(device_policy()->payload()); 349 proto.mutable_accessibility_settings()-> 350 set_login_screen_default_large_cursor_enabled(true); 351 RefreshDevicePolicyAndWaitForPrefChange(prefs::kLargeCursorEnabled); 352 353 // Verify that the pref which controls the large cursor in the session is 354 // unchanged. 355 VerifyPrefFollowsDefault(prefs::kLargeCursorEnabled); 356 357 // Verify that the large cursor is disabled. 358 chromeos::AccessibilityManager* accessibility_manager = 359 chromeos::AccessibilityManager::Get(); 360 ASSERT_TRUE(accessibility_manager); 361 EXPECT_FALSE(accessibility_manager->IsLargeCursorEnabled()); 362} 363 364IN_PROC_BROWSER_TEST_F(LoginScreenDefaultPolicyInSessionBrowsertest, 365 DeviceLoginScreenDefaultSpokenFeedbackEnabled) { 366 // Verifies that changing the default state of the spoken feedback 367 // accessibility feature on the login screen through policy does not affect 368 // its state in a session. 369 370 // Enable spoken feedback through device policy and wait for the change to 371 // take effect. 372 em::ChromeDeviceSettingsProto& proto(device_policy()->payload()); 373 proto.mutable_accessibility_settings()-> 374 set_login_screen_default_spoken_feedback_enabled(true); 375 RefreshDevicePolicyAndWaitForPrefChange(prefs::kSpokenFeedbackEnabled); 376 377 // Verify that the pref which controls the spoken feedback in the session is 378 // unchanged. 379 VerifyPrefFollowsDefault(prefs::kSpokenFeedbackEnabled); 380 381 // Verify that spoken feedback is disabled. 382 chromeos::AccessibilityManager* accessibility_manager = 383 chromeos::AccessibilityManager::Get(); 384 ASSERT_TRUE(accessibility_manager); 385 EXPECT_FALSE(accessibility_manager->IsSpokenFeedbackEnabled()); 386} 387 388IN_PROC_BROWSER_TEST_F(LoginScreenDefaultPolicyInSessionBrowsertest, 389 DeviceLoginScreenDefaultHighContrastEnabled) { 390 // Verifies that changing the default state of the high contrast mode 391 // accessibility feature on the login screen through policy does not affect 392 // its state in a session. 393 394 // Enable high contrast mode through device policy and wait for the change to 395 // take effect. 396 em::ChromeDeviceSettingsProto& proto(device_policy()->payload()); 397 proto.mutable_accessibility_settings()-> 398 set_login_screen_default_high_contrast_enabled(true); 399 RefreshDevicePolicyAndWaitForPrefChange(prefs::kHighContrastEnabled); 400 401 // Verify that the pref which controls high contrast mode in the session is 402 // unchanged. 403 VerifyPrefFollowsDefault(prefs::kHighContrastEnabled); 404 405 // Verify that high contrast mode is disabled. 406 chromeos::AccessibilityManager* accessibility_manager = 407 chromeos::AccessibilityManager::Get(); 408 ASSERT_TRUE(accessibility_manager); 409 EXPECT_FALSE(accessibility_manager->IsHighContrastEnabled()); 410} 411 412IN_PROC_BROWSER_TEST_F(LoginScreenDefaultPolicyInSessionBrowsertest, 413 DeviceLoginScreenDefaultScreenMagnifierType) { 414 // Verifies that changing the default screen magnifier type enabled on the 415 // login screen through policy does not affect its state in a session. 416 417 // Set the screen magnifier type through device policy and wait for the change 418 // to take effect. 419 em::ChromeDeviceSettingsProto& proto(device_policy()->payload()); 420 proto.mutable_accessibility_settings()-> 421 set_login_screen_default_screen_magnifier_type(kFullScreenMagnifier); 422 RefreshDevicePolicyAndWaitForPrefChange(prefs::kScreenMagnifierType); 423 424 // Verify that the prefs which control the screen magnifier in the session are 425 // unchanged. 426 VerifyPrefFollowsDefault(prefs::kScreenMagnifierEnabled); 427 VerifyPrefFollowsDefault(prefs::kScreenMagnifierType); 428 429 // Verify that the screen magnifier is disabled. 430 chromeos::MagnificationManager* magnification_manager = 431 chromeos::MagnificationManager::Get(); 432 ASSERT_TRUE(magnification_manager); 433 EXPECT_FALSE(magnification_manager->IsMagnifierEnabled()); 434 EXPECT_EQ(ash::kDefaultMagnifierType, 435 magnification_manager->GetMagnifierType()); 436} 437 438IN_PROC_BROWSER_TEST_F(LoginScreenDefaultPolicyLoginScreenBrowsertest, 439 DeviceLoginScreenDefaultVirtualKeyboardEnabled) { 440 // Verifies that the default state of the on-screen keyboard accessibility 441 // feature on the login screen can be controlled through device policy. 442 443 // Enable the on-screen keyboard through device policy and wait for the change 444 // to take effect. 445 em::ChromeDeviceSettingsProto& proto(device_policy()->payload()); 446 proto.mutable_accessibility_settings()-> 447 set_login_screen_default_virtual_keyboard_enabled(true); 448 RefreshDevicePolicyAndWaitForPrefChange(prefs::kVirtualKeyboardEnabled); 449 450 // Verify that the pref which controls the on-screen keyboard in the login 451 // profile has changed to the policy-supplied default. 452 VerifyPrefFollowsRecommendation(prefs::kVirtualKeyboardEnabled, 453 base::FundamentalValue(true)); 454 455 // Verify that the on-screen keyboard is enabled. 456 chromeos::AccessibilityManager* accessibility_manager = 457 chromeos::AccessibilityManager::Get(); 458 ASSERT_TRUE(accessibility_manager); 459 EXPECT_TRUE(accessibility_manager->IsVirtualKeyboardEnabled()); 460} 461 462} // namespace policy 463