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 GEN('#include "base/command_line.h"');
9 GEN('#include "chrome/common/chrome_switches.h"');
12 * TestFixture for ManageProfileOverlay and CreateProfileOverlay WebUI testing.
13 * @extends {testing.Test}
16 function ManageProfileUITest() {}
18 ManageProfileUITest
.prototype = {
19 __proto__
: testing
.Test
.prototype,
22 browsePreload
: 'chrome://settings-frame/manageProfile',
25 * No need to run these for every OptionsPage test, since they'll cover the
26 * whole consolidated page each time.
29 runAccessibilityChecks
: false,
32 testGenPreamble: function() {
33 GEN('CommandLine::ForCurrentProcess()->' +
34 'AppendSwitch(switches::kAllowCreateExistingManagedUsers);');
38 * Returns a test profile-info object with configurable "managed" status.
39 * @param {boolean} managed If true, the test profile will be marked as
41 * @return {Object} A test profile-info object.
43 testProfileInfo_: function(managed
) {
46 iconURL
: 'chrome://path/to/icon/image',
47 filePath
: '/path/to/profile/data/on/disk',
48 isCurrentProfile
: true,
54 * Overrides WebUI methods that provide profile info, making them return a
55 * test profile-info object.
56 * @param {boolean} managed Whether the test profile should be marked managed.
57 * @param {string} mode The mode of the overlay (either 'manage' or 'create').
59 setProfileManaged_: function(managed
, mode
) {
60 // Override the BrowserOptions method to return the fake info.
61 BrowserOptions
.getCurrentProfile = function() {
62 return this.testProfileInfo_(managed
);
64 // Set the profile info in the overlay.
65 ManageProfileOverlay
.setProfileInfo(this.testProfileInfo_(managed
), mode
);
69 // Receiving the new profile defaults in the manage-user overlay shouldn't mess
70 // up the focus in a visible higher-level overlay.
71 TEST_F('ManageProfileUITest', 'NewProfileDefaultsFocus', function() {
74 function checkFocus(pageName
, expectedFocus
, initialFocus
) {
75 OptionsPage
.showPageByName(pageName
);
77 expectEquals(initialFocus
, document
.activeElement
, pageName
);
79 ManageProfileOverlay
.receiveNewProfileDefaults(
80 self
.testProfileInfo_(false));
81 expectEquals(expectedFocus
, document
.activeElement
, pageName
);
82 OptionsPage
.closeOverlay();
85 // Receiving new profile defaults sets focus to the name field if the create
86 // overlay is open, and should not change focus at all otherwise.
87 checkFocus('manageProfile',
88 $('manage-profile-cancel'),
89 $('manage-profile-cancel'));
90 checkFocus('createProfile',
91 $('create-profile-name'),
92 $('create-profile-cancel'));
93 checkFocus('managedUserLearnMore',
94 $('managed-user-learn-more-done'),
95 $('managed-user-learn-more-done'));
96 checkFocus('managedUserLearnMore',
97 document
.querySelector('#managed-user-learn-more-text a'),
98 document
.querySelector('#managed-user-learn-more-text a'));
101 // The default options should be reset each time the creation overlay is shown.
102 TEST_F('ManageProfileUITest', 'DefaultCreateOptions', function() {
103 OptionsPage
.showPageByName('createProfile');
104 var shortcutsAllowed
= loadTimeData
.getBoolean('profileShortcutsEnabled');
105 var createShortcut
= $('create-shortcut');
106 var createManaged
= $('create-profile-managed');
107 assertEquals(shortcutsAllowed
, createShortcut
.checked
);
108 assertFalse(createManaged
.checked
);
110 createShortcut
.checked
= !shortcutsAllowed
;
111 createManaged
.checked
= true;
112 OptionsPage
.closeOverlay();
113 OptionsPage
.showPageByName('createProfile');
114 assertEquals(shortcutsAllowed
, createShortcut
.checked
);
115 assertFalse(createManaged
.checked
);
118 // The checkbox label should change depending on whether the user is signed in.
119 TEST_F('ManageProfileUITest', 'CreateManagedUserText', function() {
120 var signedInText
= $('create-profile-managed-signed-in');
121 var notSignedInText
= $('create-profile-managed-not-signed-in');
123 ManageProfileOverlay
.getInstance().initializePage();
125 var custodianEmail
= 'chrome.playpen.test@gmail.com';
126 CreateProfileOverlay
.updateSignedInStatus(custodianEmail
);
127 assertEquals(custodianEmail
,
128 CreateProfileOverlay
.getInstance().signedInEmail_
);
129 assertFalse(signedInText
.hidden
);
130 assertTrue(notSignedInText
.hidden
);
131 // Make sure the email is in the string somewhere, without depending on the
132 // exact details of the message.
133 assertNotEquals(-1, signedInText
.textContent
.indexOf(custodianEmail
));
135 CreateProfileOverlay
.updateSignedInStatus('');
136 assertEquals('', CreateProfileOverlay
.getInstance().signedInEmail_
);
137 assertTrue(signedInText
.hidden
);
138 assertFalse(notSignedInText
.hidden
);
139 assertFalse($('create-profile-managed').checked
);
140 assertTrue($('create-profile-managed').disabled
);
143 function ManageProfileUITestAsync() {}
145 ManageProfileUITestAsync
.prototype = {
146 __proto__
: ManageProfileUITest
.prototype,
151 // The import link should show up if the user tries to create a profile with the
152 // same name as an existing managed user profile.
153 TEST_F('ManageProfileUITestAsync', 'CreateExistingManagedUser', function() {
154 // Initialize the list of existing managed users.
159 iconURL
: 'chrome://path/to/icon/image',
160 onCurrentDevice
: false,
166 iconURL
: 'chrome://path/to/icon/image',
167 onCurrentDevice
: false,
173 iconURL
: 'chrome://path/to/icon/image',
174 onCurrentDevice
: true,
180 iconURL
: 'chrome://path/to/icon/image',
181 onCurrentDevice
: false,
184 var promise
= Promise
.resolve(managedUsers
);
185 options
.ManagedUserListData
.getInstance().promise_
= promise
;
187 // Initialize the ManageProfileOverlay.
188 ManageProfileOverlay
.getInstance().initializePage();
189 var custodianEmail
= 'chrome.playpen.test@gmail.com';
190 CreateProfileOverlay
.updateSignedInStatus(custodianEmail
);
191 assertEquals(custodianEmail
,
192 CreateProfileOverlay
.getInstance().signedInEmail_
);
193 this.setProfileManaged_(false, 'create');
195 // Also add the names 'Test' and 'Test2' to |profileNames_| to simulate that
196 // profiles with those names exist on the device.
197 ManageProfileOverlay
.getInstance().profileNames_
.Test
= true;
198 ManageProfileOverlay
.getInstance().profileNames_
.SameName
= true;
200 // Initially, the ok button should be enabled and the import link should not
202 assertFalse($('create-profile-ok').disabled
);
203 assertTrue($('supervised-user-import') == null);
205 // Now try to create profiles with the names of existing supervised users.
206 $('create-profile-managed').checked
= true;
207 var nameField
= $('create-profile-name');
208 // A profile which already has an avatar.
209 nameField
.value
= 'Rosalie';
210 ManageProfileOverlay
.getInstance().onNameChanged_('create');
211 // Need to wait until the promise resolves.
212 promise
.then(function() {
213 assertTrue($('create-profile-ok').disabled
);
214 assertFalse($('supervised-user-import') == null);
216 // A profile which doesn't have an avatar yet.
217 nameField
.value
= 'Fritz';
218 ManageProfileOverlay
.getInstance().onNameChanged_('create');
219 return options
.ManagedUserListData
.getInstance().promise_
;
221 assertTrue($('create-profile-ok').disabled
);
222 assertFalse($('supervised-user-import') == null);
224 // A profile which already exists on the device.
225 nameField
.value
= 'Test';
226 ManageProfileOverlay
.getInstance().onNameChanged_('create');
227 return options
.ManagedUserListData
.getInstance().promise_
;
229 assertTrue($('create-profile-ok').disabled
);
230 assertTrue($('supervised-user-import') == null);
232 // A profile which does not exist on the device, but there is a profile with
233 // the same name already on the device.
234 nameField
.value
= 'SameName';
235 ManageProfileOverlay
.getInstance().onNameChanged_('create');
236 return options
.ManagedUserListData
.getInstance().promise_
;
238 assertTrue($('create-profile-ok').disabled
);
239 assertFalse($('supervised-user-import') == null);
241 // A profile which does not exist yet.
242 nameField
.value
= 'NewProfileName';
243 ManageProfileOverlay
.getInstance().onNameChanged_('create');
244 return options
.ManagedUserListData
.getInstance().promise_
;
246 assertFalse($('create-profile-ok').disabled
);
247 assertTrue($('supervised-user-import') == null);
252 // Managed users should not be able to edit their profile names, and the initial
253 // focus should be adjusted accordingly.
254 TEST_F('ManageProfileUITest', 'EditManagedUserNameAllowed', function() {
255 var nameField
= $('manage-profile-name');
257 this.setProfileManaged_(false, 'manage');
258 ManageProfileOverlay
.showManageDialog();
259 expectFalse(nameField
.disabled
);
260 expectEquals(nameField
, document
.activeElement
);
262 OptionsPage
.closeOverlay();
264 this.setProfileManaged_(true, 'manage');
265 ManageProfileOverlay
.showManageDialog();
266 expectTrue(nameField
.disabled
);
267 expectEquals($('manage-profile-ok'), document
.activeElement
);
270 // Setting profile information should allow the confirmation to be shown.
271 TEST_F('ManageProfileUITest', 'ShowCreateConfirmation', function() {
272 var testProfile
= this.testProfileInfo_(true);
273 testProfile
.custodianEmail
= 'foo@bar.example.com';
274 ManagedUserCreateConfirmOverlay
.setProfileInfo(testProfile
);
275 assertTrue(ManagedUserCreateConfirmOverlay
.getInstance().canShowPage());
276 OptionsPage
.showPageByName('managedUserCreateConfirm', false);
277 assertEquals('managedUserCreateConfirm',
278 OptionsPage
.getTopmostVisiblePage().name
);
281 // Trying to show a confirmation dialog with no profile information should fall
282 // back to the default (main) settings page.
283 TEST_F('ManageProfileUITest', 'NoEmptyConfirmation', function() {
284 assertEquals('manageProfile', OptionsPage
.getTopmostVisiblePage().name
);
285 assertFalse(ManagedUserCreateConfirmOverlay
.getInstance().canShowPage());
286 OptionsPage
.showPageByName('managedUserCreateConfirm', true);
287 assertEquals('settings', OptionsPage
.getTopmostVisiblePage().name
);
290 // A confirmation dialog should be shown after creating a new managed user.
291 TEST_F('ManageProfileUITest', 'ShowCreateConfirmationOnSuccess', function() {
292 OptionsPage
.showPageByName('createProfile');
293 assertEquals('createProfile', OptionsPage
.getTopmostVisiblePage().name
);
294 CreateProfileOverlay
.onSuccess(this.testProfileInfo_(false));
295 assertEquals('settings', OptionsPage
.getTopmostVisiblePage().name
);
297 OptionsPage
.showPageByName('createProfile');
298 assertEquals('createProfile', OptionsPage
.getTopmostVisiblePage().name
);
299 CreateProfileOverlay
.onSuccess(this.testProfileInfo_(true));
300 assertEquals('managedUserCreateConfirm',
301 OptionsPage
.getTopmostVisiblePage().name
);
302 expectEquals($('managed-user-created-switch'), document
.activeElement
);
305 // An error should be shown if creating a new managed user fails.
306 TEST_F('ManageProfileUITest', 'NoCreateConfirmationOnError', function() {
307 OptionsPage
.showPageByName('createProfile');
308 assertEquals('createProfile', OptionsPage
.getTopmostVisiblePage().name
);
309 var errorBubble
= $('create-profile-error-bubble');
310 assertTrue(errorBubble
.hidden
);
312 CreateProfileOverlay
.onError('An Error Message!');
313 assertEquals('createProfile', OptionsPage
.getTopmostVisiblePage().name
);
314 assertFalse(errorBubble
.hidden
);
317 // The name and email sould be inserted into the confirmation dialog.
318 TEST_F('ManageProfileUITest', 'CreateConfirmationText', function () {
320 var custodianEmail
= 'foo@example.com';
322 // Checks the strings in the confirmation dialog. If |expectedNameText| is
323 // given, it should be present in the dialog's textContent; otherwise the name
324 // is expected. If |expectedNameHtml| is given, it should be present in the
325 // dialog's innerHTML; otherwise the expected text is expected in the HTML
327 function checkDialog(name
, expectedNameText
, expectedNameHtml
) {
328 var expectedText
= expectedNameText
|| name
;
329 var expectedHtml
= expectedNameHtml
|| expectedText
;
331 // Configure the test profile and show the confirmation dialog.
332 var testProfile
= self
.testProfileInfo_(true);
333 testProfile
.name
= name
;
334 CreateProfileOverlay
.onSuccess(testProfile
);
335 assertEquals('managedUserCreateConfirm',
336 OptionsPage
.getTopmostVisiblePage().name
);
338 // Check for the presence of the name and email in the UI, without depending
339 // on the details of the messsages.
341 $('managed-user-created-title').textContent
.indexOf(expectedText
));
343 $('managed-user-created-switch').textContent
.indexOf(expectedText
));
344 var message
= $('managed-user-created-text');
345 assertNotEquals(-1, message
.textContent
.indexOf(expectedText
));
346 assertNotEquals(-1, message
.textContent
.indexOf(custodianEmail
));
348 // The name should be properly HTML-escaped.
349 assertNotEquals(-1, message
.innerHTML
.indexOf(expectedHtml
));
351 OptionsPage
.closeOverlay();
352 assertEquals('settings', OptionsPage
.getTopmostVisiblePage().name
, name
);
355 // Show and configure the create-profile dialog.
356 OptionsPage
.showPageByName('createProfile');
357 CreateProfileOverlay
.updateSignedInStatus(custodianEmail
);
358 assertEquals('createProfile', OptionsPage
.getTopmostVisiblePage().name
);
360 checkDialog('OneWord');
361 checkDialog('Multiple Words');
362 checkDialog('It\'s "<HTML> injection" & more!',
363 'It\'s "<HTML> injection" & more!',
364 // The innerHTML getter doesn't escape quotation marks,
365 // independent of whether they were escaped in the setter.
366 'It\'s "<HTML> injection" & more!');
368 // Test elision. MAX_LENGTH = 50, minus 1 for the ellipsis.
369 var name49Characters
= '0123456789012345678901234567890123456789012345678';
370 var name50Characters
= name49Characters
+ '9';
371 var name51Characters
= name50Characters
+ '0';
372 var name60Characters
= name51Characters
+ '123456789';
373 checkDialog(name49Characters
, name49Characters
);
374 checkDialog(name50Characters
, name50Characters
);
375 checkDialog(name51Characters
, name49Characters
+ '\u2026');
376 checkDialog(name60Characters
, name49Characters
+ '\u2026');
378 // Test both elision and HTML escaping. The allowed string length is the
379 // visible length, not the length including the entity names.
380 name49Characters
= name49Characters
.replace('0', '&').replace('1', '>');
381 name60Characters
= name60Characters
.replace('0', '&').replace('1', '>');
382 var escaped
= name49Characters
.replace('&', '&').replace('>', '>');
384 name60Characters
, name49Characters
+ '\u2026', escaped
+ '\u2026');
387 // An additional warning should be shown when deleting a managed user.
388 TEST_F('ManageProfileUITest', 'DeleteManagedUserWarning', function() {
389 var addendum
= $('delete-managed-profile-addendum');
391 ManageProfileOverlay
.showDeleteDialog(this.testProfileInfo_(true));
392 assertFalse(addendum
.hidden
);
394 ManageProfileOverlay
.showDeleteDialog(this.testProfileInfo_(false));
395 assertTrue(addendum
.hidden
);
398 // The policy prohibiting managed users should update the UI dynamically.
399 TEST_F('ManageProfileUITest', 'PolicyDynamicRefresh', function() {
400 ManageProfileOverlay
.getInstance().initializePage();
402 var custodianEmail
= 'chrome.playpen.test@gmail.com';
403 CreateProfileOverlay
.updateSignedInStatus(custodianEmail
);
404 CreateProfileOverlay
.updateManagedUsersAllowed(true);
405 var checkbox
= $('create-profile-managed');
406 var link
= $('create-profile-managed-not-signed-in-link');
407 var indicator
= $('create-profile-managed-indicator');
409 assertFalse(checkbox
.disabled
, 'allowed and signed in');
410 assertFalse(link
.hidden
, 'allowed and signed in');
411 assertEquals('none', window
.getComputedStyle(indicator
, null).display
,
412 'allowed and signed in');
414 CreateProfileOverlay
.updateSignedInStatus('');
415 CreateProfileOverlay
.updateManagedUsersAllowed(true);
416 assertTrue(checkbox
.disabled
, 'allowed, not signed in');
417 assertFalse(link
.hidden
, 'allowed, not signed in');
418 assertEquals('none', window
.getComputedStyle(indicator
, null).display
,
419 'allowed, not signed in');
421 CreateProfileOverlay
.updateSignedInStatus('');
422 CreateProfileOverlay
.updateManagedUsersAllowed(false);
423 assertTrue(checkbox
.disabled
, 'disallowed, not signed in');
424 assertTrue(link
.hidden
, 'disallowed, not signed in');
425 assertEquals('inline-block', window
.getComputedStyle(indicator
, null).display
,
426 'disallowed, not signed in');
427 assertEquals('policy', indicator
.getAttribute('controlled-by'));
429 CreateProfileOverlay
.updateSignedInStatus(custodianEmail
);
430 CreateProfileOverlay
.updateManagedUsersAllowed(false);
431 assertTrue(checkbox
.disabled
, 'disallowed, signed in');
432 assertTrue(link
.hidden
, 'disallowed, signed in');
433 assertEquals('inline-block', window
.getComputedStyle(indicator
, null).display
,
434 'disallowed, signed in');
435 assertEquals('policy', indicator
.getAttribute('controlled-by'));
437 CreateProfileOverlay
.updateSignedInStatus(custodianEmail
);
438 CreateProfileOverlay
.updateManagedUsersAllowed(true);
439 assertFalse(checkbox
.disabled
, 're-allowed and signed in');
440 assertFalse(link
.hidden
, 're-allowed and signed in');
441 assertEquals('none', window
.getComputedStyle(indicator
, null).display
,
442 're-allowed and signed in');
445 // The managed user checkbox should correctly update its state during profile
446 // creation and afterwards.
447 TEST_F('ManageProfileUITest', 'CreateInProgress', function() {
448 ManageProfileOverlay
.getInstance().initializePage();
450 var custodianEmail
= 'chrome.playpen.test@gmail.com';
451 CreateProfileOverlay
.updateSignedInStatus(custodianEmail
);
452 CreateProfileOverlay
.updateManagedUsersAllowed(true);
453 var checkbox
= $('create-profile-managed');
454 var link
= $('create-profile-managed-not-signed-in-link');
455 var indicator
= $('create-profile-managed-indicator');
457 assertFalse(checkbox
.disabled
, 'allowed and signed in');
458 assertFalse(link
.hidden
, 'allowed and signed in');
459 assertEquals('none', window
.getComputedStyle(indicator
, null).display
,
460 'allowed and signed in');
461 assertFalse(indicator
.hasAttribute('controlled-by'));
463 CreateProfileOverlay
.updateCreateInProgress(true);
464 assertTrue(checkbox
.disabled
, 'creation in progress');
466 // A no-op update to the sign-in status should not change the UI.
467 CreateProfileOverlay
.updateSignedInStatus(custodianEmail
);
468 CreateProfileOverlay
.updateManagedUsersAllowed(true);
469 assertTrue(checkbox
.disabled
, 'creation in progress');
471 CreateProfileOverlay
.updateCreateInProgress(false);
472 assertFalse(checkbox
.disabled
, 'creation finished');
475 // Managed users shouldn't be able to open the delete or create dialogs.
476 TEST_F('ManageProfileUITest', 'ManagedShowDeleteAndCreate', function() {
477 this.setProfileManaged_(false, 'create');
479 ManageProfileOverlay
.showCreateDialog();
480 assertEquals('createProfile', OptionsPage
.getTopmostVisiblePage().name
);
481 OptionsPage
.closeOverlay();
482 assertEquals('settings', OptionsPage
.getTopmostVisiblePage().name
);
483 ManageProfileOverlay
.showDeleteDialog(this.testProfileInfo_(false));
484 assertEquals('manageProfile', OptionsPage
.getTopmostVisiblePage().name
);
485 assertFalse($('manage-profile-overlay-delete').hidden
);
486 OptionsPage
.closeOverlay();
487 assertEquals('settings', OptionsPage
.getTopmostVisiblePage().name
);
489 this.setProfileManaged_(true, 'create');
490 ManageProfileOverlay
.showCreateDialog();
491 assertEquals('settings', OptionsPage
.getTopmostVisiblePage().name
);
492 ManageProfileOverlay
.showDeleteDialog(this.testProfileInfo_(false));
493 assertEquals('settings', OptionsPage
.getTopmostVisiblePage().name
);
496 // Only non-managed users should be able to delete profiles.
497 TEST_F('ManageProfileUITest', 'ManagedDelete', function() {
498 ManageProfileOverlay
.showDeleteDialog(this.testProfileInfo_(false));
499 assertEquals('manageProfile', OptionsPage
.getTopmostVisiblePage().name
);
500 assertFalse($('manage-profile-overlay-delete').hidden
);
502 // Clicks the "Delete" button, after overriding chrome.send to record what
503 // messages were sent.
504 function clickAndListen() {
505 var originalChromeSend
= chrome
.send
;
506 var chromeSendMessages
= [];
507 chrome
.send = function(message
) {
508 chromeSendMessages
.push(message
);
510 $('delete-profile-ok').onclick();
511 // Restore the original function so the test framework can use it.
512 chrome
.send
= originalChromeSend
;
513 return chromeSendMessages
;
516 this.setProfileManaged_(false, 'manage');
517 var messages
= clickAndListen();
518 assertEquals(1, messages
.length
);
519 assertEquals('deleteProfile', messages
[0]);
520 assertEquals('settings', OptionsPage
.getTopmostVisiblePage().name
);
522 ManageProfileOverlay
.showDeleteDialog(this.testProfileInfo_(false));
523 this.setProfileManaged_(true, 'manage');
524 messages
= clickAndListen();
525 assertEquals(0, messages
.length
);
526 assertEquals('settings', OptionsPage
.getTopmostVisiblePage().name
);
529 GEN('#endif // OS_CHROMEOS');