Home | History | Annotate | Download | only in chromeos
      1 // Copyright 2014 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 <sys/types.h>
      6 
      7 #include "ash/shell.h"
      8 #include "base/compiler_specific.h"
      9 #include "base/macros.h"
     10 #include "base/prefs/pref_service.h"
     11 #include "chrome/browser/browser_process.h"
     12 #include "chrome/browser/chromeos/input_method/input_method_manager_impl.h"
     13 #include "chrome/browser/chromeos/login/login_manager_test.h"
     14 #include "chrome/browser/chromeos/login/startup_utils.h"
     15 #include "chrome/browser/chromeos/login/ui/user_adding_screen.h"
     16 #include "chrome/browser/chromeos/preferences.h"
     17 #include "chrome/browser/chromeos/settings/cros_settings.h"
     18 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h"
     19 #include "chrome/browser/chromeos/system/fake_input_device_settings.h"
     20 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager_chromeos.h"
     21 #include "chrome/common/pref_names.h"
     22 #include "chromeos/chromeos_switches.h"
     23 #include "chromeos/ime/fake_ime_keyboard.h"
     24 #include "components/feedback/tracing_manager.h"
     25 #include "content/public/test/test_utils.h"
     26 #include "testing/gtest/include/gtest/gtest.h"
     27 #include "ui/events/event_utils.h"
     28 
     29 namespace chromeos {
     30 
     31 namespace {
     32 
     33 const char* kTestUsers[] = {"test-user1 (at) gmail.com", "test-user2 (at) gmail.com"};
     34 
     35 }  // namespace
     36 
     37 class PreferencesTest : public LoginManagerTest {
     38  public:
     39   PreferencesTest()
     40       : LoginManagerTest(true),
     41         input_settings_(NULL),
     42         keyboard_(NULL) {}
     43 
     44   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
     45     LoginManagerTest::SetUpCommandLine(command_line);
     46     command_line->AppendSwitch(switches::kStubCrosSettings);
     47   }
     48 
     49   virtual void SetUpOnMainThread() OVERRIDE {
     50     LoginManagerTest::SetUpOnMainThread();
     51     input_settings_ = new system::FakeInputDeviceSettings();
     52     system::InputDeviceSettings::SetSettingsForTesting(input_settings_);
     53     keyboard_ = new input_method::FakeImeKeyboard();
     54     static_cast<input_method::InputMethodManagerImpl*>(
     55         input_method::InputMethodManager::Get())
     56         ->SetImeKeyboardForTesting(keyboard_);
     57     CrosSettings::Get()->SetString(kDeviceOwner, kTestUsers[0]);
     58   }
     59 
     60   // Sets set of preferences in given |prefs|. Value of prefernece depends of
     61   // |variant| value. For opposite |variant| values all preferences receive
     62   // different values.
     63   void SetPrefs(PrefService* prefs, bool variant) {
     64     prefs->SetBoolean(prefs::kTapToClickEnabled, variant);
     65     prefs->SetBoolean(prefs::kPrimaryMouseButtonRight, !variant);
     66     prefs->SetBoolean(prefs::kTapDraggingEnabled, variant);
     67     prefs->SetBoolean(prefs::kEnableTouchpadThreeFingerClick, !variant);
     68     prefs->SetBoolean(prefs::kNaturalScroll, variant);
     69     prefs->SetInteger(prefs::kMouseSensitivity, !variant);
     70     prefs->SetInteger(prefs::kTouchpadSensitivity, variant);
     71     prefs->SetBoolean(prefs::kTouchHudProjectionEnabled, !variant);
     72     prefs->SetBoolean(prefs::kLanguageXkbAutoRepeatEnabled, variant);
     73     prefs->SetInteger(prefs::kLanguageXkbAutoRepeatDelay, variant ? 100 : 500);
     74     prefs->SetInteger(prefs::kLanguageXkbAutoRepeatInterval, variant ? 1 : 4);
     75     prefs->SetString(prefs::kLanguagePreloadEngines,
     76                      variant ? "xkb:us::eng,xkb:us:dvorak:eng"
     77                              : "xkb:us::eng,xkb:ru::rus");
     78   }
     79 
     80   void CheckSettingsCorrespondToPrefs(PrefService* prefs) {
     81     EXPECT_EQ(prefs->GetBoolean(prefs::kTapToClickEnabled),
     82               input_settings_->current_touchpad_settings().GetTapToClick());
     83     EXPECT_EQ(prefs->GetBoolean(prefs::kPrimaryMouseButtonRight),
     84               input_settings_->current_mouse_settings()
     85                   .GetPrimaryButtonRight());
     86     EXPECT_EQ(prefs->GetBoolean(prefs::kTapDraggingEnabled),
     87               input_settings_->current_touchpad_settings().GetTapDragging());
     88     EXPECT_EQ(prefs->GetBoolean(prefs::kEnableTouchpadThreeFingerClick),
     89               input_settings_->current_touchpad_settings()
     90                   .GetThreeFingerClick());
     91     EXPECT_EQ(prefs->GetInteger(prefs::kMouseSensitivity),
     92               input_settings_->current_mouse_settings().GetSensitivity());
     93     EXPECT_EQ(prefs->GetInteger(prefs::kTouchpadSensitivity),
     94               input_settings_->current_touchpad_settings().GetSensitivity());
     95     EXPECT_EQ(prefs->GetBoolean(prefs::kTouchHudProjectionEnabled),
     96               ash::Shell::GetInstance()->is_touch_hud_projection_enabled());
     97     EXPECT_EQ(prefs->GetBoolean(prefs::kLanguageXkbAutoRepeatEnabled),
     98               keyboard_->auto_repeat_is_enabled_);
     99     input_method::AutoRepeatRate rate = keyboard_->last_auto_repeat_rate_;
    100     EXPECT_EQ(prefs->GetInteger(prefs::kLanguageXkbAutoRepeatDelay),
    101               (int)rate.initial_delay_in_ms);
    102     EXPECT_EQ(prefs->GetInteger(prefs::kLanguageXkbAutoRepeatInterval),
    103               (int)rate.repeat_interval_in_ms);
    104     EXPECT_EQ(
    105         prefs->GetString(prefs::kLanguageCurrentInputMethod),
    106         input_method::InputMethodManager::Get()->GetCurrentInputMethod().id());
    107   }
    108 
    109   void CheckLocalStateCorrespondsToPrefs(PrefService* prefs) {
    110     PrefService* local_state = g_browser_process->local_state();
    111     EXPECT_EQ(local_state->GetBoolean(prefs::kOwnerTapToClickEnabled),
    112               prefs->GetBoolean(prefs::kTapToClickEnabled));
    113     EXPECT_EQ(local_state->GetBoolean(prefs::kOwnerPrimaryMouseButtonRight),
    114               prefs->GetBoolean(prefs::kPrimaryMouseButtonRight));
    115   }
    116 
    117   void DisableAnimations() {
    118     // Disable animations for user transitions.
    119     chrome::MultiUserWindowManagerChromeOS* manager =
    120         static_cast<chrome::MultiUserWindowManagerChromeOS*>(
    121             chrome::MultiUserWindowManager::GetInstance());
    122     manager->SetAnimationSpeedForTest(
    123         chrome::MultiUserWindowManagerChromeOS::ANIMATION_SPEED_DISABLED);
    124   }
    125 
    126  private:
    127   system::FakeInputDeviceSettings* input_settings_;
    128   input_method::FakeImeKeyboard* keyboard_;
    129 
    130   DISALLOW_COPY_AND_ASSIGN(PreferencesTest);
    131 };
    132 
    133 IN_PROC_BROWSER_TEST_F(PreferencesTest, PRE_MultiProfiles) {
    134   RegisterUser(kTestUsers[0]);
    135   RegisterUser(kTestUsers[1]);
    136   chromeos::StartupUtils::MarkOobeCompleted();
    137 }
    138 
    139 IN_PROC_BROWSER_TEST_F(PreferencesTest, MultiProfiles) {
    140   UserManager* user_manager = UserManager::Get();
    141 
    142   // Add first user and init its preferences. Check that corresponding
    143   // settings has been changed.
    144   LoginUser(kTestUsers[0]);
    145   const User* user1 = user_manager->FindUser(kTestUsers[0]);
    146   PrefService* prefs1 = user_manager->GetProfileByUser(user1)->GetPrefs();
    147   SetPrefs(prefs1, false);
    148   content::RunAllPendingInMessageLoop();
    149   CheckSettingsCorrespondToPrefs(prefs1);
    150 
    151   // Add second user and init its prefs with different values.
    152   UserAddingScreen::Get()->Start();
    153   content::RunAllPendingInMessageLoop();
    154   DisableAnimations();
    155   AddUser(kTestUsers[1]);
    156   content::RunAllPendingInMessageLoop();
    157   const User* user2 = user_manager->FindUser(kTestUsers[1]);
    158   EXPECT_TRUE(user2->is_active());
    159   PrefService* prefs2 = user_manager->GetProfileByUser(user2)->GetPrefs();
    160   SetPrefs(prefs2, true);
    161 
    162   // Check that settings were changed accordingly.
    163   EXPECT_TRUE(user2->is_active());
    164   CheckSettingsCorrespondToPrefs(prefs2);
    165 
    166   // Check that changing prefs of the active user doesn't affect prefs of the
    167   // inactive user.
    168   scoped_ptr<base::DictionaryValue> prefs_backup =
    169       prefs1->GetPreferenceValues();
    170   SetPrefs(prefs2, false);
    171   CheckSettingsCorrespondToPrefs(prefs2);
    172   EXPECT_TRUE(prefs_backup->Equals(prefs1->GetPreferenceValues().get()));
    173   SetPrefs(prefs2, true);
    174   CheckSettingsCorrespondToPrefs(prefs2);
    175   EXPECT_TRUE(prefs_backup->Equals(prefs1->GetPreferenceValues().get()));
    176 
    177   // Check that changing prefs of the inactive user doesn't affect prefs of the
    178   // active user.
    179   prefs_backup = prefs2->GetPreferenceValues();
    180   SetPrefs(prefs1, true);
    181   CheckSettingsCorrespondToPrefs(prefs2);
    182   EXPECT_TRUE(prefs_backup->Equals(prefs2->GetPreferenceValues().get()));
    183   SetPrefs(prefs1, false);
    184   CheckSettingsCorrespondToPrefs(prefs2);
    185   EXPECT_TRUE(prefs_backup->Equals(prefs2->GetPreferenceValues().get()));
    186 
    187   // Check that changing non-owner prefs doesn't change corresponding local
    188   // state prefs and vice versa.
    189   EXPECT_EQ(user_manager->GetOwnerEmail(), kTestUsers[0]);
    190   CheckLocalStateCorrespondsToPrefs(prefs1);
    191   prefs2->SetBoolean(prefs::kTapToClickEnabled,
    192                      !prefs1->GetBoolean(prefs::kTapToClickEnabled));
    193   CheckLocalStateCorrespondsToPrefs(prefs1);
    194   prefs1->SetBoolean(prefs::kTapToClickEnabled,
    195                      !prefs1->GetBoolean(prefs::kTapToClickEnabled));
    196   CheckLocalStateCorrespondsToPrefs(prefs1);
    197 
    198   // Switch user back.
    199   user_manager->SwitchActiveUser(kTestUsers[0]);
    200   CheckSettingsCorrespondToPrefs(prefs1);
    201   CheckLocalStateCorrespondsToPrefs(prefs1);
    202 }
    203 
    204 }  // namespace chromeos
    205