OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
8 #include "base/strings/stringprintf.h" | |
8 #include "chrome/browser/chromeos/login/login_manager_test.h" | 9 #include "chrome/browser/chromeos/login/login_manager_test.h" |
9 #include "chrome/browser/chromeos/login/startup_utils.h" | 10 #include "chrome/browser/chromeos/login/startup_utils.h" |
10 #include "chrome/browser/chromeos/login/ui/user_adding_screen.h" | 11 #include "chrome/browser/chromeos/login/ui/user_adding_screen.h" |
11 #include "chrome/browser/chromeos/login/users/user_manager.h" | 12 #include "chrome/browser/chromeos/login/users/user_manager.h" |
12 #include "chrome/browser/chromeos/settings/cros_settings.h" | 13 #include "chrome/browser/chromeos/settings/cros_settings.h" |
13 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" | 14 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h" |
14 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/browser_commands.h" | |
15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
16 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
17 #include "chrome/test/base/ui_test_utils.h" | 19 #include "chrome/test/base/ui_test_utils.h" |
18 #include "chromeos/settings/cros_settings_names.h" | 20 #include "chromeos/settings/cros_settings_names.h" |
19 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
20 #include "content/public/test/browser_test_utils.h" | 22 #include "content/public/test/browser_test_utils.h" |
21 #include "content/public/test/test_utils.h" | 23 #include "content/public/test/test_utils.h" |
22 | 24 |
23 namespace chromeos { | 25 namespace chromeos { |
24 | 26 |
25 namespace { | 27 namespace { |
26 | 28 |
27 const char* kTestUsers[] = { "test-user1@gmail.com", "test-user2@gmail.com" }; | 29 const char* kTestUsers[] = { "test-user1@example.com", |
30 "test-user2@example.com" }; | |
31 | |
32 const char* kKnownSettings[] = { | |
33 kDeviceOwner, | |
34 kAccountsPrefAllowGuest, | |
35 kAccountsPrefAllowNewUser, | |
36 kAccountsPrefDeviceLocalAccounts, | |
37 kAccountsPrefShowUserNamesOnSignIn, | |
38 kAccountsPrefSupervisedUsersEnabled, | |
39 }; | |
40 | |
41 // Stub settings provider that only handles the settings we need to control. | |
42 // StubCrosSettingsProvider handles more settings but leaves many of them unset | |
43 // which the Settings page doesn't expect. | |
44 class StubAccountSettingsProvider : public StubCrosSettingsProvider { | |
45 public: | |
46 StubAccountSettingsProvider() { | |
47 } | |
48 | |
49 virtual ~StubAccountSettingsProvider() { | |
50 } | |
51 | |
52 // StubCrosSettingsProvider implementation. | |
53 virtual bool HandlesSetting(const std::string& path) const OVERRIDE { | |
54 const char** end = kKnownSettings + arraysize(kKnownSettings); | |
55 return std::find(kKnownSettings, end, path) != end; | |
56 } | |
57 }; | |
58 | |
59 struct PrefTest { | |
60 const char* pref_name; | |
61 bool owner_only; | |
62 bool indicator; | |
63 }; | |
64 | |
65 const PrefTest kPrefTests[] = { | |
66 { kSystemTimezone, false, false }, | |
67 { prefs::kUse24HourClock, false, false }, | |
68 { kAttestationForContentProtectionEnabled, true, true }, | |
69 { kAccountsPrefAllowGuest, true, false }, | |
70 { kAccountsPrefAllowNewUser, true, false }, | |
71 { kAccountsPrefShowUserNamesOnSignIn, true, false }, | |
72 { kAccountsPrefSupervisedUsersEnabled, true, false }, | |
73 #if defined(GOOGLE_CHROME_BUILD) | |
74 { kStatsReportingPref, true, true }, | |
75 { prefs::kSpellCheckUseSpellingService, false, false }, | |
76 #endif | |
77 }; | |
28 | 78 |
29 } // namespace | 79 } // namespace |
30 | 80 |
31 class SharedOptionsTest : public LoginManagerTest { | 81 class SharedOptionsTest : public LoginManagerTest { |
32 public: | 82 public: |
33 SharedOptionsTest() | 83 SharedOptionsTest() |
34 : LoginManagerTest(false), | 84 : LoginManagerTest(false), |
35 device_settings_provider_(NULL) { | 85 device_settings_provider_(NULL) { |
36 stub_settings_provider_.Set(kDeviceOwner, base::StringValue(kTestUsers[0])); | 86 stub_settings_provider_.Set(kDeviceOwner, base::StringValue(kTestUsers[0])); |
37 } | 87 } |
38 | 88 |
39 virtual ~SharedOptionsTest() { | 89 virtual ~SharedOptionsTest() { |
40 } | 90 } |
41 | 91 |
42 virtual void SetUpOnMainThread() OVERRIDE { | 92 virtual void SetUpOnMainThread() OVERRIDE { |
43 LoginManagerTest::SetUpOnMainThread(); | 93 LoginManagerTest::SetUpOnMainThread(); |
94 | |
44 CrosSettings* settings = CrosSettings::Get(); | 95 CrosSettings* settings = CrosSettings::Get(); |
96 // Add the stub settings provider, moving the device settings provider | |
Nikita (slow)
2014/06/16 16:25:16
nit: insert empty line.
michaelpg
2014/06/16 18:31:17
Done.
| |
97 // behind it so our stub takes precedence. | |
45 device_settings_provider_ = settings->GetProvider(kDeviceOwner); | 98 device_settings_provider_ = settings->GetProvider(kDeviceOwner); |
46 settings->RemoveSettingsProvider(device_settings_provider_); | 99 settings->RemoveSettingsProvider(device_settings_provider_); |
47 settings->AddSettingsProvider(&stub_settings_provider_); | 100 settings->AddSettingsProvider(&stub_settings_provider_); |
101 settings->AddSettingsProvider(device_settings_provider_); | |
48 } | 102 } |
49 | 103 |
50 virtual void CleanUpOnMainThread() OVERRIDE { | 104 virtual void CleanUpOnMainThread() OVERRIDE { |
51 CrosSettings* settings = CrosSettings::Get(); | 105 CrosSettings* settings = CrosSettings::Get(); |
52 settings->RemoveSettingsProvider(&stub_settings_provider_); | 106 settings->RemoveSettingsProvider(&stub_settings_provider_); |
53 settings->AddSettingsProvider(device_settings_provider_); | |
54 LoginManagerTest::CleanUpOnMainThread(); | 107 LoginManagerTest::CleanUpOnMainThread(); |
55 } | 108 } |
56 | 109 |
57 protected: | 110 protected: |
58 void CheckOptionsUI(const User* user, bool is_primary) { | 111 void CheckOptionsUI(const User* user, bool is_owner) { |
112 Browser* browser = CreateBrowserForUser(user); | |
113 content::WebContents* contents = | |
114 browser->tab_strip_model()->GetActiveWebContents(); | |
115 | |
116 for (size_t i = 0; i < sizeof(kPrefTests) / sizeof(kPrefTests[0]); i++) { | |
117 CheckPreference(contents, | |
118 kPrefTests[i].pref_name, | |
119 !is_owner && kPrefTests[i].owner_only, | |
120 !is_owner && kPrefTests[i].indicator ? "owner" : ""); | |
121 } | |
122 CheckBanner(contents, !is_owner); | |
123 CheckSharedSections(contents, !is_owner); | |
Nikita (slow)
2014/06/16 16:25:16
nit: !is_owner: I assume this only works when owne
michaelpg
2014/06/16 18:31:17
Added is_primary and a TODO.
| |
124 CheckAccountsOverlay(contents, is_owner); | |
125 } | |
126 | |
127 // Creates a browser and navigates to the Settings page. | |
128 Browser* CreateBrowserForUser(const User* user) { | |
59 Profile* profile = UserManager::Get()->GetProfileByUser(user); | 129 Profile* profile = UserManager::Get()->GetProfileByUser(user); |
60 profile->GetPrefs()->SetString(prefs::kGoogleServicesUsername, | 130 profile->GetPrefs()->SetString(prefs::kGoogleServicesUsername, |
61 user->email()); | 131 user->email()); |
62 | 132 |
63 ui_test_utils::BrowserAddedObserver observer; | 133 ui_test_utils::BrowserAddedObserver observer; |
64 Browser* browser = CreateBrowser(profile); | 134 Browser* browser = CreateBrowser(profile); |
65 observer.WaitForSingleNewBrowser(); | 135 observer.WaitForSingleNewBrowser(); |
66 | 136 |
67 ui_test_utils::NavigateToURL(browser, | 137 ui_test_utils::NavigateToURL(browser, |
68 GURL("chrome://settings-frame")); | 138 GURL("chrome://settings-frame")); |
69 content::WebContents* contents = | 139 return browser; |
70 browser->tab_strip_model()->GetActiveWebContents(); | 140 } |
71 | 141 |
142 // Verifies a preference's disabled state and controlled-by indicator. | |
143 void CheckPreference(content::WebContents* contents, | |
144 std::string pref_name, | |
145 bool disabled, | |
146 std::string controlled_by) { | |
147 bool success; | |
148 std::string js_expression = base::StringPrintf( | |
149 "var prefSelector = '[pref=\"%s\"]';" | |
150 "var controlledBy = '%s';" | |
151 "var input = document.querySelector(" | |
152 " 'input' + prefSelector + ', select' + prefSelector);" | |
153 "var success = false;" | |
154 "if (input) {" | |
155 " success = input.disabled == %d;" | |
156 " var indicator = document.querySelector(input.tagName +" | |
157 " prefSelector + ' + span span.controlled-setting-indicator');" | |
158 " if (controlledBy) {" | |
159 " success = success && indicator &&" | |
160 " indicator.getAttribute('controlled-by') == controlledBy;" | |
161 " } else {" | |
162 " success = success && (!indicator ||" | |
163 " !indicator.hasAttribute('controlled-by') ||" | |
164 " indicator.getAttribute('controlled-by') == '')" | |
165 " }" | |
166 "}" | |
167 "window.domAutomationController.send(!!success);", | |
168 pref_name.c_str(), controlled_by.c_str(), disabled); | |
169 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | |
170 contents, js_expression, &success)); | |
171 EXPECT_TRUE(success); | |
172 } | |
173 | |
174 // Verifies a checkbox's disabled state, controlled-by indicator and value. | |
175 void CheckBooleanPreference(content::WebContents* contents, | |
176 std::string pref_name, | |
177 bool disabled, | |
178 std::string controlled_by, | |
179 bool expected_value) { | |
180 CheckPreference(contents, pref_name, disabled, controlled_by); | |
181 bool actual_value; | |
182 std::string js_expression = base::StringPrintf( | |
183 "window.domAutomationController.send(document.querySelector('" | |
184 " input[type=\"checkbox\"][pref=\"%s\"]').checked);", | |
185 pref_name.c_str()); | |
186 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | |
187 contents, js_expression, &actual_value)); | |
188 EXPECT_EQ(expected_value, actual_value); | |
189 } | |
190 | |
191 // Verifies that the shared settings banner is visible only for | |
192 // secondary users. | |
193 void CheckBanner(content::WebContents* contents, | |
194 bool is_secondary) { | |
72 bool banner_visible; | 195 bool banner_visible; |
73 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | 196 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( |
74 contents, | 197 contents, |
75 "var e = document.getElementById('secondary-user-banner');" | 198 "var e = $('secondary-user-banner');" |
76 "var visible = e.offsetWidth > 0 && e.offsetHeight > 0;" | 199 "window.domAutomationController.send(e && !e.hidden);", |
77 "window.domAutomationController.send(visible);", | |
78 &banner_visible)); | 200 &banner_visible)); |
79 EXPECT_EQ(is_primary, !banner_visible); | 201 EXPECT_EQ(is_secondary, banner_visible); |
80 } | 202 } |
81 | 203 |
82 StubCrosSettingsProvider stub_settings_provider_; | 204 // Verifies that sections of shared settings have the appropriate indicator. |
205 void CheckSharedSections(content::WebContents* contents, | |
206 bool is_secondary) { | |
207 // This only applies to the Internet options section. | |
208 std::string controlled_by; | |
209 ASSERT_TRUE(content::ExecuteScriptAndExtractString( | |
210 contents, | |
211 "var e = document.querySelector(" | |
212 " '#network-section-header span.controlled-setting-indicator');" | |
213 "if (!e || !e.getAttribute('controlled-by')) {" | |
214 " window.domAutomationController.send('');" | |
215 "} else {" | |
216 " window.domAutomationController.send(" | |
217 " e.getAttribute('controlled-by'));" | |
218 "}", | |
219 &controlled_by)); | |
220 EXPECT_EQ(is_secondary ? "shared" : "", controlled_by); | |
221 } | |
222 | |
223 // Checks the Accounts header and non-checkbox inputs. | |
224 void CheckAccountsOverlay(content::WebContents* contents, bool is_owner) { | |
225 // Set cros.accounts.allowGuest to false so we can test the accounts list. | |
226 // This has to be done after the PRE_* test or we can't add the owner. | |
227 stub_settings_provider_.Set( | |
228 kAccountsPrefAllowNewUser, base::FundamentalValue(false)); | |
229 | |
230 bool success; | |
231 std::string js_expression = base::StringPrintf( | |
232 "var controlled = %d;" | |
233 "var warning = $('ownerOnlyWarning');" | |
234 "var userList = $('userList');" | |
235 "var input = $('userNameEdit');" | |
236 "var success;" | |
237 "if (controlled)" | |
238 " success = warning && !warning.hidden && userList.disabled &&" | |
239 " input.disabled;" | |
240 "else" | |
241 " success = (!warning || warning.hidden) && !userList.disabled &&" | |
242 " !input.disabled;" | |
243 "window.domAutomationController.send(!!success);", | |
244 !is_owner); | |
245 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( | |
246 contents, js_expression, &success)); | |
247 EXPECT_TRUE(success) << "Accounts overlay incorrect for " << | |
248 (is_owner ? "owner." : "non-owner."); | |
249 } | |
250 | |
251 StubAccountSettingsProvider stub_settings_provider_; | |
83 CrosSettingsProvider* device_settings_provider_; | 252 CrosSettingsProvider* device_settings_provider_; |
84 | 253 |
85 private: | 254 private: |
86 DISALLOW_COPY_AND_ASSIGN(SharedOptionsTest); | 255 DISALLOW_COPY_AND_ASSIGN(SharedOptionsTest); |
87 }; | 256 }; |
88 | 257 |
89 IN_PROC_BROWSER_TEST_F(SharedOptionsTest, PRE_SharedOptions) { | 258 IN_PROC_BROWSER_TEST_F(SharedOptionsTest, PRE_SharedOptions) { |
90 RegisterUser(kTestUsers[0]); | 259 RegisterUser(kTestUsers[0]); |
91 RegisterUser(kTestUsers[1]); | 260 RegisterUser(kTestUsers[1]); |
92 StartupUtils::MarkOobeCompleted(); | 261 StartupUtils::MarkOobeCompleted(); |
93 } | 262 } |
94 | 263 |
95 IN_PROC_BROWSER_TEST_F(SharedOptionsTest, SharedOptions) { | 264 IN_PROC_BROWSER_TEST_F(SharedOptionsTest, SharedOptions) { |
265 // Log in the owner first, then add a secondary user. | |
96 LoginUser(kTestUsers[0]); | 266 LoginUser(kTestUsers[0]); |
97 UserAddingScreen::Get()->Start(); | 267 UserAddingScreen::Get()->Start(); |
98 content::RunAllPendingInMessageLoop(); | 268 content::RunAllPendingInMessageLoop(); |
99 AddUser(kTestUsers[1]); | 269 AddUser(kTestUsers[1]); |
100 | 270 |
101 UserManager* manager = UserManager::Get(); | 271 UserManager* manager = UserManager::Get(); |
102 ASSERT_EQ(2u, manager->GetLoggedInUsers().size()); | 272 ASSERT_EQ(2u, manager->GetLoggedInUsers().size()); |
273 { | |
274 SCOPED_TRACE("Checking settings for owner, primary user."); | |
275 CheckOptionsUI(manager->FindUser(kTestUsers[0]), true); | |
Nikita (slow)
2014/06/16 16:25:16
Can you rely on UserManager to define which user i
michaelpg
2014/06/16 18:31:17
I set kDeviceOwner already to make the tests work.
| |
276 } | |
277 { | |
278 SCOPED_TRACE("Checking settings for non-owner, secondary user."); | |
279 CheckOptionsUI(manager->FindUser(kTestUsers[1]), false); | |
280 } | |
281 } | |
103 | 282 |
104 CheckOptionsUI(manager->FindUser(kTestUsers[0]), true /* is_primary */); | 283 IN_PROC_BROWSER_TEST_F(SharedOptionsTest, PRE_ScreenLockPreference) { |
105 CheckOptionsUI(manager->FindUser(kTestUsers[1]), false /* is_primary */); | 284 RegisterUser(kTestUsers[0]); |
285 RegisterUser(kTestUsers[1]); | |
286 StartupUtils::MarkOobeCompleted(); | |
287 } | |
288 | |
289 // Tests that a shared setting indicator appears for the auto-lock setting | |
290 // when the user has the checkbox unselected but another user has enabled | |
291 // auto-lock. (The checkbox is unset if the user's preference is false, | |
292 // but if any other signed-in user has enabled this preference, the shared | |
293 // setting indicator explains this.) | |
294 IN_PROC_BROWSER_TEST_F(SharedOptionsTest, ScreenLockPreference) { | |
295 LoginUser(kTestUsers[0]); | |
296 UserAddingScreen::Get()->Start(); | |
297 content::RunAllPendingInMessageLoop(); | |
298 AddUser(kTestUsers[1]); | |
299 | |
300 UserManager* manager = UserManager::Get(); | |
301 const User* user1 = manager->FindUser(kTestUsers[0]); | |
302 const User* user2 = manager->FindUser(kTestUsers[1]); | |
303 | |
304 PrefService* prefs1 = manager->GetProfileByUser(user1)->GetPrefs(); | |
305 PrefService* prefs2 = manager->GetProfileByUser(user2)->GetPrefs(); | |
306 prefs1->SetBoolean(prefs::kEnableAutoScreenLock, false); | |
307 prefs2->SetBoolean(prefs::kEnableAutoScreenLock, false); | |
308 | |
309 Browser* browser1 = CreateBrowserForUser(user1); | |
310 Browser* browser2 = CreateBrowserForUser(user2); | |
311 content::WebContents* contents1 = | |
312 browser1->tab_strip_model()->GetActiveWebContents(); | |
313 content::WebContents* contents2 = | |
314 browser2->tab_strip_model()->GetActiveWebContents(); | |
315 { | |
316 SCOPED_TRACE("Screen lock false for both users"); | |
317 CheckBooleanPreference(contents1, prefs::kEnableAutoScreenLock, false, "", | |
Nikita (slow)
2014/06/16 16:25:16
nit: std::string() here and below
Nikita (slow)
2014/06/16 16:25:16
It would be great to have something like
bool dis
michaelpg
2014/06/16 18:31:17
Done.
michaelpg
2014/06/16 18:31:17
Done.
| |
318 false); | |
319 CheckBooleanPreference(contents2, prefs::kEnableAutoScreenLock, false, "", | |
320 false); | |
321 } | |
322 // Set the preference to true for the primary user and check that the value | |
323 // changes appropriately. | |
324 prefs1->SetBoolean(prefs::kEnableAutoScreenLock, true); | |
325 { | |
326 SCOPED_TRACE("Screen lock true for primary user"); | |
327 CheckBooleanPreference(contents1, prefs::kEnableAutoScreenLock, false, "", | |
328 true); | |
329 } | |
330 // Reload the secondary user's browser to see the updated controlled-by | |
331 // indicator. Also reload the primary user's to make sure the setting still | |
332 // starts out correctly. | |
333 { | |
334 SCOPED_TRACE("Screen lock true for primary user, false for secondary user"); | |
335 chrome::Reload(browser1, CURRENT_TAB); | |
336 chrome::Reload(browser2, CURRENT_TAB); | |
337 content::WaitForLoadStop(contents1); | |
338 content::WaitForLoadStop(contents2); | |
339 CheckBooleanPreference(contents1, prefs::kEnableAutoScreenLock, false, "", | |
340 true); | |
341 CheckBooleanPreference(contents2, prefs::kEnableAutoScreenLock, false, | |
342 "shared", false); | |
343 } | |
344 // Set the preference to true for the secondary user and check that the | |
345 // indicator disappears. | |
346 prefs2->SetBoolean(prefs::kEnableAutoScreenLock, true); | |
347 { | |
348 SCOPED_TRACE("Screen lock true for secondary user"); | |
349 CheckBooleanPreference(contents2, prefs::kEnableAutoScreenLock, false, "", | |
350 true); | |
351 } | |
352 // Reload the browsers to check that they still start out correctly. | |
353 { | |
354 SCOPED_TRACE("Screen lock true for both users"); | |
355 chrome::Reload(browser1, CURRENT_TAB); | |
356 chrome::Reload(browser2, CURRENT_TAB); | |
357 content::WaitForLoadStop(contents1); | |
358 content::WaitForLoadStop(contents2); | |
359 CheckBooleanPreference(contents1, prefs::kEnableAutoScreenLock, false, "", | |
360 true); | |
361 CheckBooleanPreference(contents2, prefs::kEnableAutoScreenLock, false, "", | |
362 true); | |
363 } | |
364 // Set the preference to false for the primary user and check that the | |
365 // value changes correctly. | |
366 prefs1->SetBoolean(prefs::kEnableAutoScreenLock, false); | |
367 { | |
368 SCOPED_TRACE("Screen lock false for primary user"); | |
369 CheckBooleanPreference(contents1, prefs::kEnableAutoScreenLock, false, | |
370 "shared", false); | |
371 } | |
372 // The primary user should now see a shared setting indicator. | |
373 { | |
374 SCOPED_TRACE("Screen lock false for primary user, true for secondary user"); | |
375 chrome::Reload(browser1, CURRENT_TAB); | |
376 chrome::Reload(browser2, CURRENT_TAB); | |
377 content::WaitForLoadStop(contents1); | |
378 content::WaitForLoadStop(contents2); | |
379 CheckBooleanPreference(contents1, prefs::kEnableAutoScreenLock, false, | |
380 "shared", false); | |
381 CheckBooleanPreference(contents2, prefs::kEnableAutoScreenLock, false, "", | |
382 true); | |
383 } | |
106 } | 384 } |
107 | 385 |
108 } // namespace chromeos | 386 } // namespace chromeos |
OLD | NEW |