Home | History | Annotate | Download | only in policy
      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/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"
     32 
     33 namespace em = enterprise_management;
     34 
     35 namespace policy {
     36 
     37 namespace {
     38 
     39 const 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.
     45 class 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 
     62 PrefChangeWatcher::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 
     70 void PrefChangeWatcher::Wait() {
     71   if (!pref_changed_)
     72     run_loop_.Run();
     73 }
     74 
     75 void PrefChangeWatcher::OnPrefChange() {
     76   pref_changed_ = true;
     77   run_loop_.Quit();
     78 }
     79 
     80 }  // namespace
     81 
     82 class 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 
    100 class 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 
    118 class 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 
    133 LoginScreenDefaultPolicyBrowsertestBase::
    134     LoginScreenDefaultPolicyBrowsertestBase() : login_profile_(NULL) {
    135 }
    136 
    137 LoginScreenDefaultPolicyBrowsertestBase::
    138     ~LoginScreenDefaultPolicyBrowsertestBase() {
    139 }
    140 
    141 void LoginScreenDefaultPolicyBrowsertestBase::
    142     SetUpInProcessBrowserTestFixture() {
    143   InstallOwnerKey();
    144   MarkAsEnterpriseOwned();
    145   DevicePolicyCrosBrowserTest::SetUpInProcessBrowserTestFixture();
    146 }
    147 
    148 void LoginScreenDefaultPolicyBrowsertestBase::SetUpOnMainThread() {
    149   DevicePolicyCrosBrowserTest::SetUpOnMainThread();
    150   login_profile_ = chromeos::ProfileHelper::GetSigninProfile();
    151   ASSERT_TRUE(login_profile_);
    152 }
    153 
    154 void LoginScreenDefaultPolicyBrowsertestBase::
    155     RefreshDevicePolicyAndWaitForPrefChange(const char* pref_name) {
    156   PrefChangeWatcher watcher(pref_name, login_profile_->GetPrefs());
    157   RefreshDevicePolicy();
    158   watcher.Wait();
    159 }
    160 
    161 LoginScreenDefaultPolicyLoginScreenBrowsertest::
    162     LoginScreenDefaultPolicyLoginScreenBrowsertest() {
    163 }
    164 
    165 LoginScreenDefaultPolicyLoginScreenBrowsertest::
    166     ~LoginScreenDefaultPolicyLoginScreenBrowsertest() {
    167 }
    168 
    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);
    174 }
    175 
    176 void 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 
    193 void LoginScreenDefaultPolicyLoginScreenBrowsertest::CleanUpOnMainThread() {
    194   base::MessageLoop::current()->PostTask(FROM_HERE,
    195                                          base::Bind(&chrome::AttemptExit));
    196   base::RunLoop().RunUntilIdle();
    197   LoginScreenDefaultPolicyBrowsertestBase::CleanUpOnMainThread();
    198 }
    199 
    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);
    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 
    213 LoginScreenDefaultPolicyInSessionBrowsertest::
    214     LoginScreenDefaultPolicyInSessionBrowsertest() {
    215 }
    216 
    217 LoginScreenDefaultPolicyInSessionBrowsertest::
    218     ~LoginScreenDefaultPolicyInSessionBrowsertest() {
    219 }
    220 
    221 void LoginScreenDefaultPolicyInSessionBrowsertest::SetUpOnMainThread() {
    222   LoginScreenDefaultPolicyBrowsertestBase::SetUpOnMainThread();
    223 
    224   // Tell the DeviceSettingsService that there is no local owner.
    225   chromeos::DeviceSettingsService::Get()->SetUsername(std::string());
    226 }
    227 
    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);
    234   ASSERT_TRUE(pref);
    235   EXPECT_FALSE(pref->IsManaged());
    236   EXPECT_TRUE(pref->IsDefaultValue());
    237   EXPECT_FALSE(pref->GetRecommendedValue());
    238 }
    239 
    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.
    244 
    245   // Enable the large cursor through device policy and wait for the change to
    246   // take effect.
    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);
    251 
    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));
    256 
    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());
    262 }
    263 
    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.
    268 
    269   // Enable spoken feedback through device policy and wait for the change to
    270   // take effect.
    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);
    275 
    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));
    280 
    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());
    286 }
    287 
    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.
    292 
    293   // Enable high contrast mode through device policy and wait for the change to
    294   // take effect.
    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);
    299 
    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));
    304 
    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());
    310 }
    311 
    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.
    316 
    317   // Set the screen magnifier type through device policy and wait for the change
    318   // to take effect.
    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);
    323 
    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));
    330 
    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());
    337 }
    338 
    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
    343   // session.
    344 
    345   // Enable the large cursor through device policy and wait for the change to
    346   // take effect.
    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);
    351 
    352   // Verify that the pref which controls the large cursor in the session is
    353   // unchanged.
    354   VerifyPrefFollowsDefault(prefs::kLargeCursorEnabled);
    355 
    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());
    361 }
    362 
    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.
    368 
    369   // Enable spoken feedback through device policy and wait for the change to
    370   // take effect.
    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);
    375 
    376   // Verify that the pref which controls the spoken feedback in the session is
    377   // unchanged.
    378   VerifyPrefFollowsDefault(prefs::kSpokenFeedbackEnabled);
    379 
    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());
    385 }
    386 
    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.
    392 
    393   // Enable high contrast mode through device policy and wait for the change to
    394   // take effect.
    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);
    399 
    400   // Verify that the pref which controls high contrast mode in the session is
    401   // unchanged.
    402   VerifyPrefFollowsDefault(prefs::kHighContrastEnabled);
    403 
    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());
    409 }
    410 
    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.
    415 
    416   // Set the screen magnifier type through device policy and wait for the change
    417   // to take effect.
    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);
    422 
    423   // Verify that the prefs which control the screen magnifier in the session are
    424   // unchanged.
    425   VerifyPrefFollowsDefault(prefs::kScreenMagnifierEnabled);
    426   VerifyPrefFollowsDefault(prefs::kScreenMagnifierType);
    427 
    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());
    435 }
    436 
    437 } // namespace policy
    438