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