NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / manage_profile_browsertest.js
blob950d1b8e21d7d28e5aad305d051acacca03155a7
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.
5 // None of these tests is relevant for Chrome OS.
6 GEN('#if !defined(OS_CHROMEOS)');
8 /**
9 * TestFixture for ManageProfileOverlay and CreateProfileOverlay WebUI testing.
10 * @extends {testing.Test}
11 * @constructor
13 function ManageProfileUITest() {}
15 ManageProfileUITest.prototype = {
16 __proto__: testing.Test.prototype,
18 /** @override */
19 browsePreload: 'chrome://settings-frame/manageProfile',
21 /**
22 * No need to run these for every OptionsPage test, since they'll cover the
23 * whole consolidated page each time.
24 * @override
26 runAccessibilityChecks: false,
28 /**
29 * Returns a test profile-info object with configurable "managed" status.
30 * @param {boolean} managed If true, the test profile will be marked as
31 * managed.
32 * @return {Object} A test profile-info object.
34 testProfileInfo_: function(managed) {
35 return {
36 name: 'Test Profile',
37 iconURL: 'chrome://path/to/icon/image',
38 filePath: '/path/to/profile/data/on/disk',
39 isCurrentProfile: true,
40 isManaged: managed
44 /**
45 * Overrides WebUI methods that provide profile info, making them return a
46 * test profile-info object.
47 * @param {boolean} managed Whether the test profile should be marked managed.
48 * @param {string} mode The mode of the overlay (either 'manage' or 'create').
50 setProfileManaged_: function(managed, mode) {
51 // Override the BrowserOptions method to return the fake info.
52 BrowserOptions.getCurrentProfile = function() {
53 return this.testProfileInfo_(managed);
54 }.bind(this);
55 // Set the profile info in the overlay.
56 ManageProfileOverlay.setProfileInfo(this.testProfileInfo_(managed), mode);
60 // Receiving the new profile defaults in the manage-user overlay shouldn't mess
61 // up the focus in a visible higher-level overlay.
62 TEST_F('ManageProfileUITest', 'NewProfileDefaultsFocus', function() {
63 var self = this;
65 function checkFocus(pageName, expectedFocus, initialFocus) {
66 OptionsPage.showPageByName(pageName);
67 initialFocus.focus();
68 expectEquals(initialFocus, document.activeElement, pageName);
70 ManageProfileOverlay.receiveNewProfileDefaults(
71 self.testProfileInfo_(false));
72 expectEquals(expectedFocus, document.activeElement, pageName);
73 OptionsPage.closeOverlay();
76 // Receiving new profile defaults sets focus to the name field if the create
77 // overlay is open, and should not change focus at all otherwise.
78 checkFocus('manageProfile',
79 $('manage-profile-cancel'),
80 $('manage-profile-cancel'));
81 checkFocus('createProfile',
82 $('create-profile-name'),
83 $('create-profile-cancel'));
84 checkFocus('managedUserLearnMore',
85 $('managed-user-learn-more-done'),
86 $('managed-user-learn-more-done'));
87 checkFocus('managedUserLearnMore',
88 document.querySelector('#managed-user-learn-more-text a'),
89 document.querySelector('#managed-user-learn-more-text a'));
90 });
92 // The default options should be reset each time the creation overlay is shown.
93 TEST_F('ManageProfileUITest', 'DefaultCreateOptions', function() {
94 OptionsPage.showPageByName('createProfile');
95 var shortcutsAllowed = loadTimeData.getBoolean('profileShortcutsEnabled');
96 var createShortcut = $('create-shortcut');
97 var createManaged = $('create-profile-managed');
98 assertEquals(shortcutsAllowed, createShortcut.checked);
99 assertFalse(createManaged.checked);
101 createShortcut.checked = !shortcutsAllowed;
102 createManaged.checked = true;
103 OptionsPage.closeOverlay();
104 OptionsPage.showPageByName('createProfile');
105 assertEquals(shortcutsAllowed, createShortcut.checked);
106 assertFalse(createManaged.checked);
109 // The checkbox label should change depending on whether the user is signed in.
110 TEST_F('ManageProfileUITest', 'CreateManagedUserText', function() {
111 var signedInText = $('create-profile-managed-signed-in');
112 var notSignedInText = $('create-profile-managed-not-signed-in');
114 ManageProfileOverlay.getInstance().initializePage();
116 var custodianEmail = 'chrome.playpen.test@gmail.com';
117 CreateProfileOverlay.updateSignedInStatus(custodianEmail);
118 assertEquals(custodianEmail,
119 CreateProfileOverlay.getInstance().signedInEmail_);
120 assertFalse(signedInText.hidden);
121 assertTrue(notSignedInText.hidden);
122 // Make sure the email is in the string somewhere, without depending on the
123 // exact details of the message.
124 assertNotEquals(-1, signedInText.textContent.indexOf(custodianEmail));
126 CreateProfileOverlay.updateSignedInStatus('');
127 assertEquals('', CreateProfileOverlay.getInstance().signedInEmail_);
128 assertTrue(signedInText.hidden);
129 assertFalse(notSignedInText.hidden);
130 assertFalse($('create-profile-managed').checked);
131 assertTrue($('create-profile-managed').disabled);
134 function ManageProfileUITestAsync() {}
136 ManageProfileUITestAsync.prototype = {
137 __proto__: ManageProfileUITest.prototype,
139 isAsync: true,
142 // The import link should show up if the user tries to create a profile with the
143 // same name as an existing managed user profile.
144 TEST_F('ManageProfileUITestAsync', 'CreateExistingManagedUser', function() {
145 // Initialize the list of existing managed users.
146 var managedUsers = [
148 id: 'managedUser1',
149 name: 'Rosalie',
150 iconURL: 'chrome://path/to/icon/image',
151 onCurrentDevice: false,
152 needAvatar: false
155 id: 'managedUser2',
156 name: 'Fritz',
157 iconURL: 'chrome://path/to/icon/image',
158 onCurrentDevice: false,
159 needAvatar: true
162 id: 'managedUser3',
163 name: 'Test',
164 iconURL: 'chrome://path/to/icon/image',
165 onCurrentDevice: true,
166 needAvatar: false
169 id: 'managedUser4',
170 name: 'SameName',
171 iconURL: 'chrome://path/to/icon/image',
172 onCurrentDevice: false,
173 needAvatar: false
175 var promise = Promise.resolve(managedUsers);
176 options.ManagedUserListData.getInstance().promise_ = promise;
178 // Initialize the ManageProfileOverlay.
179 ManageProfileOverlay.getInstance().initializePage();
180 var custodianEmail = 'chrome.playpen.test@gmail.com';
181 CreateProfileOverlay.updateSignedInStatus(custodianEmail);
182 assertEquals(custodianEmail,
183 CreateProfileOverlay.getInstance().signedInEmail_);
184 this.setProfileManaged_(false, 'create');
186 // Also add the names 'Test' and 'Test2' to |profileNames_| to simulate that
187 // profiles with those names exist on the device.
188 ManageProfileOverlay.getInstance().profileNames_.Test = true;
189 ManageProfileOverlay.getInstance().profileNames_.SameName = true;
191 // Initially, the ok button should be enabled and the import link should not
192 // exist.
193 assertFalse($('create-profile-ok').disabled);
194 assertTrue($('supervised-user-import') == null);
196 // Now try to create profiles with the names of existing supervised users.
197 $('create-profile-managed').checked = true;
198 var nameField = $('create-profile-name');
199 // A profile which already has an avatar.
200 nameField.value = 'Rosalie';
201 ManageProfileOverlay.getInstance().onNameChanged_('create');
202 // Need to wait until the promise resolves.
203 promise.then(function() {
204 assertTrue($('create-profile-ok').disabled);
205 assertFalse($('supervised-user-import') == null);
207 // A profile which doesn't have an avatar yet.
208 nameField.value = 'Fritz';
209 ManageProfileOverlay.getInstance().onNameChanged_('create');
210 return options.ManagedUserListData.getInstance().promise_;
211 }).then(function() {
212 assertTrue($('create-profile-ok').disabled);
213 assertFalse($('supervised-user-import') == null);
215 // A profile which already exists on the device.
216 nameField.value = 'Test';
217 ManageProfileOverlay.getInstance().onNameChanged_('create');
218 return options.ManagedUserListData.getInstance().promise_;
219 }).then(function() {
220 assertTrue($('create-profile-ok').disabled);
221 assertTrue($('supervised-user-import') == null);
223 // A profile which does not exist on the device, but there is a profile with
224 // the same name already on the device.
225 nameField.value = 'SameName';
226 ManageProfileOverlay.getInstance().onNameChanged_('create');
227 return options.ManagedUserListData.getInstance().promise_;
228 }).then(function() {
229 assertTrue($('create-profile-ok').disabled);
230 assertFalse($('supervised-user-import') == null);
232 // A profile which does not exist yet.
233 nameField.value = 'NewProfileName';
234 ManageProfileOverlay.getInstance().onNameChanged_('create');
235 return options.ManagedUserListData.getInstance().promise_;
236 }).then(function() {
237 assertFalse($('create-profile-ok').disabled);
238 assertTrue($('supervised-user-import') == null);
239 testDone();
243 // Managed users should not be able to edit their profile names, and the initial
244 // focus should be adjusted accordingly.
245 TEST_F('ManageProfileUITest', 'EditManagedUserNameAllowed', function() {
246 var nameField = $('manage-profile-name');
248 this.setProfileManaged_(false, 'manage');
249 ManageProfileOverlay.showManageDialog();
250 expectFalse(nameField.disabled);
251 expectEquals(nameField, document.activeElement);
253 OptionsPage.closeOverlay();
255 this.setProfileManaged_(true, 'manage');
256 ManageProfileOverlay.showManageDialog();
257 expectTrue(nameField.disabled);
258 expectEquals($('manage-profile-ok'), document.activeElement);
261 // Setting profile information should allow the confirmation to be shown.
262 TEST_F('ManageProfileUITest', 'ShowCreateConfirmation', function() {
263 var testProfile = this.testProfileInfo_(true);
264 testProfile.custodianEmail = 'foo@bar.example.com';
265 ManagedUserCreateConfirmOverlay.setProfileInfo(testProfile);
266 assertTrue(ManagedUserCreateConfirmOverlay.getInstance().canShowPage());
267 OptionsPage.showPageByName('managedUserCreateConfirm', false);
268 assertEquals('managedUserCreateConfirm',
269 OptionsPage.getTopmostVisiblePage().name);
272 // Trying to show a confirmation dialog with no profile information should fall
273 // back to the default (main) settings page.
274 TEST_F('ManageProfileUITest', 'NoEmptyConfirmation', function() {
275 assertEquals('manageProfile', OptionsPage.getTopmostVisiblePage().name);
276 assertFalse(ManagedUserCreateConfirmOverlay.getInstance().canShowPage());
277 OptionsPage.showPageByName('managedUserCreateConfirm', true);
278 assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);
281 // A confirmation dialog should be shown after creating a new managed user.
282 TEST_F('ManageProfileUITest', 'ShowCreateConfirmationOnSuccess', function() {
283 OptionsPage.showPageByName('createProfile');
284 assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name);
285 CreateProfileOverlay.onSuccess(this.testProfileInfo_(false));
286 assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);
288 OptionsPage.showPageByName('createProfile');
289 assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name);
290 CreateProfileOverlay.onSuccess(this.testProfileInfo_(true));
291 assertEquals('managedUserCreateConfirm',
292 OptionsPage.getTopmostVisiblePage().name);
293 expectEquals($('managed-user-created-switch'), document.activeElement);
296 // An error should be shown if creating a new managed user fails.
297 TEST_F('ManageProfileUITest', 'NoCreateConfirmationOnError', function() {
298 OptionsPage.showPageByName('createProfile');
299 assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name);
300 var errorBubble = $('create-profile-error-bubble');
301 assertTrue(errorBubble.hidden);
303 CreateProfileOverlay.onError('An Error Message!');
304 assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name);
305 assertFalse(errorBubble.hidden);
308 // The name and email sould be inserted into the confirmation dialog.
309 TEST_F('ManageProfileUITest', 'CreateConfirmationText', function () {
310 var self = this;
311 var custodianEmail = 'foo@example.com';
313 // Checks the strings in the confirmation dialog. If |expectedNameText| is
314 // given, it should be present in the dialog's textContent; otherwise the name
315 // is expected. If |expectedNameHtml| is given, it should be present in the
316 // dialog's innerHTML; otherwise the expected text is expected in the HTML
317 // too.
318 function checkDialog(name, expectedNameText, expectedNameHtml) {
319 var expectedText = expectedNameText || name;
320 var expectedHtml = expectedNameHtml || expectedText;
322 // Configure the test profile and show the confirmation dialog.
323 var testProfile = self.testProfileInfo_(true);
324 testProfile.name = name;
325 CreateProfileOverlay.onSuccess(testProfile);
326 assertEquals('managedUserCreateConfirm',
327 OptionsPage.getTopmostVisiblePage().name);
329 // Check for the presence of the name and email in the UI, without depending
330 // on the details of the messsages.
331 assertNotEquals(-1,
332 $('managed-user-created-title').textContent.indexOf(expectedText));
333 assertNotEquals(-1,
334 $('managed-user-created-switch').textContent.indexOf(expectedText));
335 var message = $('managed-user-created-text');
336 assertNotEquals(-1, message.textContent.indexOf(expectedText));
337 assertNotEquals(-1, message.textContent.indexOf(custodianEmail));
339 // The name should be properly HTML-escaped.
340 assertNotEquals(-1, message.innerHTML.indexOf(expectedHtml));
342 OptionsPage.closeOverlay();
343 assertEquals('settings', OptionsPage.getTopmostVisiblePage().name, name);
346 // Show and configure the create-profile dialog.
347 OptionsPage.showPageByName('createProfile');
348 CreateProfileOverlay.updateSignedInStatus(custodianEmail);
349 assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name);
351 checkDialog('OneWord');
352 checkDialog('Multiple Words');
353 checkDialog('It\'s "<HTML> injection" & more!',
354 'It\'s "<HTML> injection" & more!',
355 // The innerHTML getter doesn't escape quotation marks,
356 // independent of whether they were escaped in the setter.
357 'It\'s "&lt;HTML&gt; injection" &amp; more!');
359 // Test elision. MAX_LENGTH = 50, minus 1 for the ellipsis.
360 var name49Characters = '0123456789012345678901234567890123456789012345678';
361 var name50Characters = name49Characters + '9';
362 var name51Characters = name50Characters + '0';
363 var name60Characters = name51Characters + '123456789';
364 checkDialog(name49Characters, name49Characters);
365 checkDialog(name50Characters, name50Characters);
366 checkDialog(name51Characters, name49Characters + '\u2026');
367 checkDialog(name60Characters, name49Characters + '\u2026');
369 // Test both elision and HTML escaping. The allowed string length is the
370 // visible length, not the length including the entity names.
371 name49Characters = name49Characters.replace('0', '&').replace('1', '>');
372 name60Characters = name60Characters.replace('0', '&').replace('1', '>');
373 var escaped = name49Characters.replace('&', '&amp;').replace('>', '&gt;');
374 checkDialog(
375 name60Characters, name49Characters + '\u2026', escaped + '\u2026');
378 // An additional warning should be shown when deleting a managed user.
379 TEST_F('ManageProfileUITest', 'DeleteManagedUserWarning', function() {
380 var addendum = $('delete-managed-profile-addendum');
382 ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(true));
383 assertFalse(addendum.hidden);
385 ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
386 assertTrue(addendum.hidden);
389 // The policy prohibiting managed users should update the UI dynamically.
390 TEST_F('ManageProfileUITest', 'PolicyDynamicRefresh', function() {
391 ManageProfileOverlay.getInstance().initializePage();
393 var custodianEmail = 'chrome.playpen.test@gmail.com';
394 CreateProfileOverlay.updateSignedInStatus(custodianEmail);
395 CreateProfileOverlay.updateManagedUsersAllowed(true);
396 var checkbox = $('create-profile-managed');
397 var link = $('create-profile-managed-not-signed-in-link');
398 var indicator = $('create-profile-managed-indicator');
400 assertFalse(checkbox.disabled, 'allowed and signed in');
401 assertFalse(link.hidden, 'allowed and signed in');
402 assertEquals('none', window.getComputedStyle(indicator, null).display,
403 'allowed and signed in');
405 CreateProfileOverlay.updateSignedInStatus('');
406 CreateProfileOverlay.updateManagedUsersAllowed(true);
407 assertTrue(checkbox.disabled, 'allowed, not signed in');
408 assertFalse(link.hidden, 'allowed, not signed in');
409 assertEquals('none', window.getComputedStyle(indicator, null).display,
410 'allowed, not signed in');
412 CreateProfileOverlay.updateSignedInStatus('');
413 CreateProfileOverlay.updateManagedUsersAllowed(false);
414 assertTrue(checkbox.disabled, 'disallowed, not signed in');
415 assertTrue(link.hidden, 'disallowed, not signed in');
416 assertEquals('inline-block', window.getComputedStyle(indicator, null).display,
417 'disallowed, not signed in');
418 assertEquals('policy', indicator.getAttribute('controlled-by'));
420 CreateProfileOverlay.updateSignedInStatus(custodianEmail);
421 CreateProfileOverlay.updateManagedUsersAllowed(false);
422 assertTrue(checkbox.disabled, 'disallowed, signed in');
423 assertTrue(link.hidden, 'disallowed, signed in');
424 assertEquals('inline-block', window.getComputedStyle(indicator, null).display,
425 'disallowed, signed in');
426 assertEquals('policy', indicator.getAttribute('controlled-by'));
428 CreateProfileOverlay.updateSignedInStatus(custodianEmail);
429 CreateProfileOverlay.updateManagedUsersAllowed(true);
430 assertFalse(checkbox.disabled, 're-allowed and signed in');
431 assertFalse(link.hidden, 're-allowed and signed in');
432 assertEquals('none', window.getComputedStyle(indicator, null).display,
433 're-allowed and signed in');
436 // The managed user checkbox should correctly update its state during profile
437 // creation and afterwards.
438 TEST_F('ManageProfileUITest', 'CreateInProgress', function() {
439 ManageProfileOverlay.getInstance().initializePage();
441 var custodianEmail = 'chrome.playpen.test@gmail.com';
442 CreateProfileOverlay.updateSignedInStatus(custodianEmail);
443 CreateProfileOverlay.updateManagedUsersAllowed(true);
444 var checkbox = $('create-profile-managed');
445 var link = $('create-profile-managed-not-signed-in-link');
446 var indicator = $('create-profile-managed-indicator');
448 assertFalse(checkbox.disabled, 'allowed and signed in');
449 assertFalse(link.hidden, 'allowed and signed in');
450 assertEquals('none', window.getComputedStyle(indicator, null).display,
451 'allowed and signed in');
452 assertFalse(indicator.hasAttribute('controlled-by'));
454 CreateProfileOverlay.updateCreateInProgress(true);
455 assertTrue(checkbox.disabled, 'creation in progress');
457 // A no-op update to the sign-in status should not change the UI.
458 CreateProfileOverlay.updateSignedInStatus(custodianEmail);
459 CreateProfileOverlay.updateManagedUsersAllowed(true);
460 assertTrue(checkbox.disabled, 'creation in progress');
462 CreateProfileOverlay.updateCreateInProgress(false);
463 assertFalse(checkbox.disabled, 'creation finished');
466 // Managed users shouldn't be able to open the delete or create dialogs.
467 TEST_F('ManageProfileUITest', 'ManagedShowDeleteAndCreate', function() {
468 this.setProfileManaged_(false, 'create');
470 ManageProfileOverlay.showCreateDialog();
471 assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name);
472 OptionsPage.closeOverlay();
473 assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);
474 ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
475 assertEquals('manageProfile', OptionsPage.getTopmostVisiblePage().name);
476 assertFalse($('manage-profile-overlay-delete').hidden);
477 OptionsPage.closeOverlay();
478 assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);
480 this.setProfileManaged_(true, 'create');
481 ManageProfileOverlay.showCreateDialog();
482 assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);
483 ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
484 assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);
487 // Only non-managed users should be able to delete profiles.
488 TEST_F('ManageProfileUITest', 'ManagedDelete', function() {
489 ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
490 assertEquals('manageProfile', OptionsPage.getTopmostVisiblePage().name);
491 assertFalse($('manage-profile-overlay-delete').hidden);
493 // Clicks the "Delete" button, after overriding chrome.send to record what
494 // messages were sent.
495 function clickAndListen() {
496 var originalChromeSend = chrome.send;
497 var chromeSendMessages = [];
498 chrome.send = function(message) {
499 chromeSendMessages.push(message);
501 $('delete-profile-ok').onclick();
502 // Restore the original function so the test framework can use it.
503 chrome.send = originalChromeSend;
504 return chromeSendMessages;
507 this.setProfileManaged_(false, 'manage');
508 var messages = clickAndListen();
509 assertEquals(1, messages.length);
510 assertEquals('deleteProfile', messages[0]);
511 assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);
513 ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
514 this.setProfileManaged_(true, 'manage');
515 messages = clickAndListen();
516 assertEquals(0, messages.length);
517 assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);
520 GEN('#endif // OS_CHROMEOS');