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.
4
5// None of these tests is relevant for Chrome OS.
6GEN('#if !defined(OS_CHROMEOS)');
7
8/**
9 * TestFixture for ManageProfileOverlay and CreateProfileOverlay WebUI testing.
10 * @extends {testing.Test}
11 * @constructor
12 */
13function ManageProfileUITest() {}
14
15ManageProfileUITest.prototype = {
16  __proto__: testing.Test.prototype,
17
18  /** @override */
19  browsePreload: 'chrome://settings-frame/manageProfile',
20
21  /**
22   * No need to run these for every OptionsPage test, since they'll cover the
23   * whole consolidated page each time.
24   * @override
25   */
26  runAccessibilityChecks: false,
27
28  /**
29   * Some default profile infos.
30   */
31  defaultIconURLs: [],
32  defaultNames: [],
33
34  /**
35   * Returns a test profile-info object with configurable "supervised" status.
36   * @param {boolean} supervised If true, the test profile will be marked as
37   *     supervised.
38   * @return {Object} A test profile-info object.
39   */
40  testProfileInfo_: function(supervised) {
41    return {
42      name: 'Test Profile',
43      iconURL: 'chrome://path/to/icon/image',
44      filePath: '/path/to/profile/data/on/disk',
45      isCurrentProfile: true,
46      isSupervised: supervised
47    };
48  },
49
50  /**
51   * Overrides WebUI methods that provide profile info, making them return a
52   * test profile-info object.
53   * @param {boolean} supervised Whether the test profile should be marked
54   *     as supervised.
55   * @param {string} mode The mode of the overlay (either 'manage' or 'create').
56   */
57  setProfileSupervised_: function(supervised, mode) {
58    // Override the BrowserOptions method to return the fake info.
59    BrowserOptions.getCurrentProfile = function() {
60      return this.testProfileInfo_(supervised);
61    }.bind(this);
62    // Set the profile info in the overlay.
63    ManageProfileOverlay.setProfileInfo(this.testProfileInfo_(supervised),
64                                        mode);
65  },
66
67  /**
68   * Set some default profile infos (icon URLs and names).
69   * @param {boolean} supervised Whether the test profile should be marked as
70   *     supervised.
71   * @param {string} mode The mode of the overlay (either 'manage' or 'create').
72   */
73  initDefaultProfiles_: function(mode) {
74    PageManager.showPageByName(mode + 'Profile');
75
76    var defaultProfile = {
77      name: 'Default Name',
78      iconURL: '/default/path',
79    };
80    this.defaultIconURLs = ['/some/path',
81                            defaultProfile.iconURL,
82                            '/another/path',
83                            '/one/more/path'];
84    this.defaultNames = ['Some Name', defaultProfile.name, '', 'Another Name'];
85    ManageProfileOverlay.receiveDefaultProfileIconsAndNames(
86        mode, this.defaultIconURLs, this.defaultNames);
87    ManageProfileOverlay.receiveNewProfileDefaults(defaultProfile);
88
89    // Make sure the correct item in the icon grid was selected.
90    var gridEl = $(mode + '-profile-icon-grid');
91    expectEquals(defaultProfile.iconURL, gridEl.selectedItem);
92  },
93};
94
95// Receiving the new profile defaults in the manage-user overlay shouldn't mess
96// up the focus in a visible higher-level overlay.
97TEST_F('ManageProfileUITest', 'NewProfileDefaultsFocus', function() {
98  var self = this;
99
100  function checkFocus(pageName, expectedFocus, initialFocus) {
101    PageManager.showPageByName(pageName);
102    initialFocus.focus();
103    expectEquals(initialFocus, document.activeElement, pageName);
104
105    ManageProfileOverlay.receiveNewProfileDefaults(
106        self.testProfileInfo_(false));
107    expectEquals(expectedFocus, document.activeElement, pageName);
108    PageManager.closeOverlay();
109  }
110
111  // Receiving new profile defaults sets focus to the name field if the create
112  // overlay is open, and should not change focus at all otherwise.
113  checkFocus('manageProfile',
114             $('manage-profile-cancel'),
115             $('manage-profile-cancel'));
116  checkFocus('createProfile',
117             $('create-profile-name'),
118             $('create-profile-cancel'));
119  checkFocus('supervisedUserLearnMore',
120             $('supervised-user-learn-more-done'),
121             $('supervised-user-learn-more-done'));
122  checkFocus('supervisedUserLearnMore',
123             document.querySelector('#supervised-user-learn-more-text a'),
124             document.querySelector('#supervised-user-learn-more-text a'));
125});
126
127// The default options should be reset each time the creation overlay is shown.
128TEST_F('ManageProfileUITest', 'DefaultCreateOptions', function() {
129  PageManager.showPageByName('createProfile');
130  var shortcutsAllowed = loadTimeData.getBoolean('profileShortcutsEnabled');
131  var createShortcut = $('create-shortcut');
132  var createSupervised = $('create-profile-supervised');
133  assertEquals(shortcutsAllowed, createShortcut.checked);
134  assertFalse(createSupervised.checked);
135
136  createShortcut.checked = !shortcutsAllowed;
137  createSupervised.checked = true;
138  PageManager.closeOverlay();
139  PageManager.showPageByName('createProfile');
140  assertEquals(shortcutsAllowed, createShortcut.checked);
141  assertFalse(createSupervised.checked);
142});
143
144// The checkbox label should change depending on whether the user is signed in.
145TEST_F('ManageProfileUITest', 'CreateSupervisedUserText', function() {
146  var signedInText = $('create-profile-supervised-signed-in');
147  var notSignedInText = $('create-profile-supervised-not-signed-in');
148
149  ManageProfileOverlay.getInstance().initializePage();
150
151  var custodianEmail = 'chrome.playpen.test@gmail.com';
152  CreateProfileOverlay.updateSignedInStatus(custodianEmail);
153  assertEquals(custodianEmail,
154               CreateProfileOverlay.getInstance().signedInEmail_);
155  assertFalse(signedInText.hidden);
156  assertTrue(notSignedInText.hidden);
157  // Make sure the email is in the string somewhere, without depending on the
158  // exact details of the message.
159  assertNotEquals(-1, signedInText.textContent.indexOf(custodianEmail));
160
161  CreateProfileOverlay.updateSignedInStatus('');
162  assertEquals('', CreateProfileOverlay.getInstance().signedInEmail_);
163  assertTrue(signedInText.hidden);
164  assertFalse(notSignedInText.hidden);
165  assertFalse($('create-profile-supervised').checked);
166  assertTrue($('create-profile-supervised').disabled);
167});
168
169function ManageProfileUITestAsync() {}
170
171ManageProfileUITestAsync.prototype = {
172  __proto__: ManageProfileUITest.prototype,
173
174  isAsync: true,
175};
176
177// The import link should show up if the user tries to create a profile with the
178// same name as an existing supervised user profile.
179TEST_F('ManageProfileUITestAsync', 'CreateExistingSupervisedUser', function() {
180  // Initialize the list of existing supervised users.
181  var supervisedUsers = [
182    {
183      id: 'supervisedUser1',
184      name: 'Rosalie',
185      iconURL: 'chrome://path/to/icon/image',
186      onCurrentDevice: false,
187      needAvatar: false
188    },
189    {
190      id: 'supervisedUser2',
191      name: 'Fritz',
192      iconURL: 'chrome://path/to/icon/image',
193      onCurrentDevice: false,
194      needAvatar: true
195    },
196    {
197      id: 'supervisedUser3',
198      name: 'Test',
199      iconURL: 'chrome://path/to/icon/image',
200      onCurrentDevice: true,
201      needAvatar: false
202    },
203    {
204      id: 'supervisedUser4',
205      name: 'SameName',
206      iconURL: 'chrome://path/to/icon/image',
207      onCurrentDevice: false,
208      needAvatar: false
209    }];
210  var promise = Promise.resolve(supervisedUsers);
211  options.SupervisedUserListData.getInstance().promise_ = promise;
212
213  // Initialize the ManageProfileOverlay.
214  ManageProfileOverlay.getInstance().initializePage();
215  var custodianEmail = 'chrome.playpen.test@gmail.com';
216  CreateProfileOverlay.updateSignedInStatus(custodianEmail);
217  assertEquals(custodianEmail,
218               CreateProfileOverlay.getInstance().signedInEmail_);
219  this.setProfileSupervised_(false, 'create');
220
221  // Also add the names 'Test' and 'SameName' to |existingProfileNames_| to
222  // simulate that profiles with those names exist on the device.
223  ManageProfileOverlay.getInstance().existingProfileNames_.Test = true;
224  ManageProfileOverlay.getInstance().existingProfileNames_.SameName = true;
225
226  // Initially, the ok button should be enabled and the import link should not
227  // exist.
228  assertFalse($('create-profile-ok').disabled);
229  assertTrue($('supervised-user-import-existing') == null);
230
231  // Now try to create profiles with the names of existing supervised users.
232  $('create-profile-supervised').checked = true;
233  var nameField = $('create-profile-name');
234  // A profile which already has an avatar.
235  nameField.value = 'Rosalie';
236  ManageProfileOverlay.getInstance().onNameChanged_('create');
237  // Need to wait until the promise resolves.
238  promise.then(function() {
239    assertTrue($('create-profile-ok').disabled);
240    assertFalse($('supervised-user-import-existing') == null);
241
242    // A profile which doesn't have an avatar yet.
243    nameField.value = 'Fritz';
244    ManageProfileOverlay.getInstance().onNameChanged_('create');
245    return options.SupervisedUserListData.getInstance().promise_;
246  }).then(function() {
247    assertTrue($('create-profile-ok').disabled);
248    assertFalse($('supervised-user-import-existing') == null);
249
250    // A profile which already exists on the device.
251    nameField.value = 'Test';
252    ManageProfileOverlay.getInstance().onNameChanged_('create');
253    return options.SupervisedUserListData.getInstance().promise_;
254  }).then(function() {
255    assertTrue($('create-profile-ok').disabled);
256    assertTrue($('supervised-user-import-existing') == null);
257
258    // A profile which does not exist on the device, but there is a profile with
259    // the same name already on the device.
260    nameField.value = 'SameName';
261    ManageProfileOverlay.getInstance().onNameChanged_('create');
262    return options.SupervisedUserListData.getInstance().promise_;
263  }).then(function() {
264    assertTrue($('create-profile-ok').disabled);
265    assertFalse($('supervised-user-import-existing') == null);
266
267    // A profile which does not exist yet.
268    nameField.value = 'NewProfileName';
269    ManageProfileOverlay.getInstance().onNameChanged_('create');
270    return options.SupervisedUserListData.getInstance().promise_;
271  }).then(function() {
272    assertFalse($('create-profile-ok').disabled);
273    assertTrue($('supervised-user-import-existing') == null);
274    testDone();
275  });
276});
277
278// Supervised users should not be able to edit their profile names, and the
279// initial focus should be adjusted accordingly.
280TEST_F('ManageProfileUITest', 'EditSupervisedUserNameAllowed', function() {
281  var nameField = $('manage-profile-name');
282
283  this.setProfileSupervised_(false, 'manage');
284  ManageProfileOverlay.showManageDialog();
285  expectFalse(nameField.disabled);
286  expectEquals(nameField, document.activeElement);
287
288  PageManager.closeOverlay();
289
290  this.setProfileSupervised_(true, 'manage');
291  ManageProfileOverlay.showManageDialog();
292  expectTrue(nameField.disabled);
293  expectEquals($('manage-profile-ok'), document.activeElement);
294});
295
296// Setting profile information should allow the confirmation to be shown.
297TEST_F('ManageProfileUITest', 'ShowCreateConfirmation', function() {
298  var testProfile = this.testProfileInfo_(true);
299  testProfile.custodianEmail = 'foo@bar.example.com';
300  SupervisedUserCreateConfirmOverlay.setProfileInfo(testProfile);
301  assertTrue(SupervisedUserCreateConfirmOverlay.getInstance().canShowPage());
302  PageManager.showPageByName('supervisedUserCreateConfirm', false);
303  assertEquals('supervisedUserCreateConfirm',
304               PageManager.getTopmostVisiblePage().name);
305});
306
307// Trying to show a confirmation dialog with no profile information should fall
308// back to the default (main) settings page.
309TEST_F('ManageProfileUITest', 'NoEmptyConfirmation', function() {
310  assertEquals('manageProfile', PageManager.getTopmostVisiblePage().name);
311  assertFalse(SupervisedUserCreateConfirmOverlay.getInstance().canShowPage());
312  PageManager.showPageByName('supervisedUserCreateConfirm', true);
313  assertEquals('settings', PageManager.getTopmostVisiblePage().name);
314});
315
316// A confirmation dialog should be shown after creating a new supervised user.
317TEST_F('ManageProfileUITest', 'ShowCreateConfirmationOnSuccess', function() {
318  PageManager.showPageByName('createProfile');
319  assertEquals('createProfile', PageManager.getTopmostVisiblePage().name);
320  CreateProfileOverlay.onSuccess(this.testProfileInfo_(false));
321  assertEquals('settings', PageManager.getTopmostVisiblePage().name);
322
323  PageManager.showPageByName('createProfile');
324  assertEquals('createProfile', PageManager.getTopmostVisiblePage().name);
325  CreateProfileOverlay.onSuccess(this.testProfileInfo_(true));
326  assertEquals('supervisedUserCreateConfirm',
327               PageManager.getTopmostVisiblePage().name);
328  expectEquals($('supervised-user-created-switch'), document.activeElement);
329});
330
331// An error should be shown if creating a new supervised user fails.
332TEST_F('ManageProfileUITest', 'NoCreateConfirmationOnError', function() {
333  PageManager.showPageByName('createProfile');
334  assertEquals('createProfile', PageManager.getTopmostVisiblePage().name);
335  var errorBubble = $('create-profile-error-bubble');
336  assertTrue(errorBubble.hidden);
337
338  CreateProfileOverlay.onError('An Error Message!');
339  assertEquals('createProfile', PageManager.getTopmostVisiblePage().name);
340  assertFalse(errorBubble.hidden);
341});
342
343// The name and email should be inserted into the confirmation dialog.
344TEST_F('ManageProfileUITest', 'CreateConfirmationText', function() {
345  var self = this;
346  var custodianEmail = 'foo@example.com';
347
348  // Checks the strings in the confirmation dialog. If |expectedNameText| is
349  // given, it should be present in the dialog's textContent; otherwise the name
350  // is expected. If |expectedNameHtml| is given, it should be present in the
351  // dialog's innerHTML; otherwise the expected text is expected in the HTML
352  // too.
353  function checkDialog(name, expectedNameText, expectedNameHtml) {
354    var expectedText = expectedNameText || name;
355    var expectedHtml = expectedNameHtml || expectedText;
356
357    // Configure the test profile and show the confirmation dialog.
358    var testProfile = self.testProfileInfo_(true);
359    testProfile.name = name;
360    CreateProfileOverlay.onSuccess(testProfile);
361    assertEquals('supervisedUserCreateConfirm',
362                 PageManager.getTopmostVisiblePage().name);
363
364    // Check for the presence of the name and email in the UI, without depending
365    // on the details of the messages.
366    assertNotEquals(-1,
367        $('supervised-user-created-title').textContent.indexOf(expectedText));
368    assertNotEquals(-1,
369        $('supervised-user-created-switch').textContent.indexOf(expectedText));
370    var message = $('supervised-user-created-text');
371    assertNotEquals(-1, message.textContent.indexOf(expectedText));
372    assertNotEquals(-1, message.textContent.indexOf(custodianEmail));
373
374    // The name should be properly HTML-escaped.
375    assertNotEquals(-1, message.innerHTML.indexOf(expectedHtml));
376
377    PageManager.closeOverlay();
378    assertEquals('settings', PageManager.getTopmostVisiblePage().name, name);
379  }
380
381  // Show and configure the create-profile dialog.
382  PageManager.showPageByName('createProfile');
383  CreateProfileOverlay.updateSignedInStatus(custodianEmail);
384  assertEquals('createProfile', PageManager.getTopmostVisiblePage().name);
385
386  checkDialog('OneWord');
387  checkDialog('Multiple Words');
388  checkDialog('It\'s "<HTML> injection" & more!',
389              'It\'s "<HTML> injection" & more!',
390              // The innerHTML getter doesn't escape quotation marks,
391              // independent of whether they were escaped in the setter.
392              'It\'s "&lt;HTML&gt; injection" &amp; more!');
393
394  // Test elision. MAX_LENGTH = 50, minus 1 for the ellipsis.
395  var name49Characters = '0123456789012345678901234567890123456789012345678';
396  var name50Characters = name49Characters + '9';
397  var name51Characters = name50Characters + '0';
398  var name60Characters = name51Characters + '123456789';
399  checkDialog(name49Characters, name49Characters);
400  checkDialog(name50Characters, name50Characters);
401  checkDialog(name51Characters, name49Characters + '\u2026');
402  checkDialog(name60Characters, name49Characters + '\u2026');
403
404  // Test both elision and HTML escaping. The allowed string length is the
405  // visible length, not the length including the entity names.
406  name49Characters = name49Characters.replace('0', '&').replace('1', '>');
407  name60Characters = name60Characters.replace('0', '&').replace('1', '>');
408  var escaped = name49Characters.replace('&', '&amp;').replace('>', '&gt;');
409  checkDialog(
410      name60Characters, name49Characters + '\u2026', escaped + '\u2026');
411});
412
413// An additional warning should be shown when deleting a supervised user.
414TEST_F('ManageProfileUITest', 'DeleteSupervisedUserWarning', function() {
415  var addendum = $('delete-supervised-profile-addendum');
416
417  ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(true));
418  assertFalse(addendum.hidden);
419
420  ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
421  assertTrue(addendum.hidden);
422});
423
424// The policy prohibiting supervised users should update the UI dynamically.
425TEST_F('ManageProfileUITest', 'PolicyDynamicRefresh', function() {
426  ManageProfileOverlay.getInstance().initializePage();
427
428  var custodianEmail = 'chrome.playpen.test@gmail.com';
429  CreateProfileOverlay.updateSignedInStatus(custodianEmail);
430  CreateProfileOverlay.updateSupervisedUsersAllowed(true);
431  var checkbox = $('create-profile-supervised');
432  var signInPromo = $('create-profile-supervised-not-signed-in');
433  var signInLink = $('create-profile-supervised-sign-in-link');
434  var indicator = $('create-profile-supervised-indicator');
435
436  assertFalse(checkbox.disabled, 'allowed and signed in');
437  assertTrue(signInPromo.hidden, 'allowed and signed in');
438  assertEquals('none', window.getComputedStyle(indicator, null).display,
439               'allowed and signed in');
440
441  CreateProfileOverlay.updateSignedInStatus('');
442  CreateProfileOverlay.updateSupervisedUsersAllowed(true);
443  assertTrue(checkbox.disabled, 'allowed, not signed in');
444  assertFalse(signInPromo.hidden, 'allowed, not signed in');
445  assertTrue(signInLink.enabled, 'allowed, not signed in');
446  assertEquals('none', window.getComputedStyle(indicator, null).display,
447               'allowed, not signed in');
448
449  CreateProfileOverlay.updateSignedInStatus('');
450  CreateProfileOverlay.updateSupervisedUsersAllowed(false);
451  assertTrue(checkbox.disabled, 'disallowed, not signed in');
452  assertFalse(signInPromo.hidden, 'disallowed, not signed in');
453  assertFalse(signInLink.enabled, 'disallowed, not signed in');
454  assertEquals('inline-block', window.getComputedStyle(indicator, null).display,
455               'disallowed, not signed in');
456  assertEquals('policy', indicator.getAttribute('controlled-by'));
457
458  CreateProfileOverlay.updateSignedInStatus(custodianEmail);
459  CreateProfileOverlay.updateSupervisedUsersAllowed(false);
460  assertTrue(checkbox.disabled, 'disallowed, signed in');
461  assertTrue(signInPromo.hidden, 'disallowed, signed in');
462  assertEquals('inline-block', window.getComputedStyle(indicator, null).display,
463               'disallowed, signed in');
464  assertEquals('policy', indicator.getAttribute('controlled-by'));
465
466  CreateProfileOverlay.updateSignedInStatus(custodianEmail);
467  CreateProfileOverlay.updateSupervisedUsersAllowed(true);
468  assertFalse(checkbox.disabled, 're-allowed and signed in');
469  assertTrue(signInPromo.hidden, 're-allowed and signed in');
470  assertEquals('none', window.getComputedStyle(indicator, null).display,
471               're-allowed and signed in');
472});
473
474// The supervised user checkbox should correctly update its state during profile
475// creation and afterwards.
476TEST_F('ManageProfileUITest', 'CreateInProgress', function() {
477  ManageProfileOverlay.getInstance().initializePage();
478
479  var custodianEmail = 'chrome.playpen.test@gmail.com';
480  CreateProfileOverlay.updateSignedInStatus(custodianEmail);
481  CreateProfileOverlay.updateSupervisedUsersAllowed(true);
482  var checkbox = $('create-profile-supervised');
483  var signInPromo = $('create-profile-supervised-not-signed-in');
484  var indicator = $('create-profile-supervised-indicator');
485
486  assertFalse(checkbox.disabled, 'allowed and signed in');
487  assertTrue(signInPromo.hidden, 'allowed and signed in');
488  assertEquals('none', window.getComputedStyle(indicator, null).display,
489               'allowed and signed in');
490  assertFalse(indicator.hasAttribute('controlled-by'));
491
492  CreateProfileOverlay.updateCreateInProgress(true);
493  assertTrue(checkbox.disabled, 'creation in progress');
494
495  // A no-op update to the sign-in status should not change the UI.
496  CreateProfileOverlay.updateSignedInStatus(custodianEmail);
497  CreateProfileOverlay.updateSupervisedUsersAllowed(true);
498  assertTrue(checkbox.disabled, 'creation in progress');
499
500  CreateProfileOverlay.updateCreateInProgress(false);
501  assertFalse(checkbox.disabled, 'creation finished');
502});
503
504// Supervised users shouldn't be able to open the delete or create dialogs.
505TEST_F('ManageProfileUITest', 'SupervisedShowDeleteAndCreate', function() {
506  this.setProfileSupervised_(false, 'create');
507
508  ManageProfileOverlay.showCreateDialog();
509  assertEquals('createProfile', PageManager.getTopmostVisiblePage().name);
510  PageManager.closeOverlay();
511  assertEquals('settings', PageManager.getTopmostVisiblePage().name);
512  ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
513  assertEquals('manageProfile', PageManager.getTopmostVisiblePage().name);
514  assertFalse($('manage-profile-overlay-delete').hidden);
515  PageManager.closeOverlay();
516  assertEquals('settings', PageManager.getTopmostVisiblePage().name);
517
518  this.setProfileSupervised_(true, 'create');
519  ManageProfileOverlay.showCreateDialog();
520  assertEquals('settings', PageManager.getTopmostVisiblePage().name);
521  ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
522  assertEquals('settings', PageManager.getTopmostVisiblePage().name);
523});
524
525// Only non-supervised users should be able to delete profiles.
526TEST_F('ManageProfileUITest', 'SupervisedDelete', function() {
527  ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
528  assertEquals('manageProfile', PageManager.getTopmostVisiblePage().name);
529  assertFalse($('manage-profile-overlay-delete').hidden);
530
531  // Clicks the "Delete" button, after overriding chrome.send to record what
532  // messages were sent.
533  function clickAndListen() {
534    var originalChromeSend = chrome.send;
535    var chromeSendMessages = [];
536    chrome.send = function(message) {
537      chromeSendMessages.push(message);
538    };
539    $('delete-profile-ok').onclick();
540    // Restore the original function so the test framework can use it.
541    chrome.send = originalChromeSend;
542    return chromeSendMessages;
543  }
544
545  this.setProfileSupervised_(false, 'manage');
546  var messages = clickAndListen();
547  assertEquals(1, messages.length);
548  assertEquals('deleteProfile', messages[0]);
549  assertEquals('settings', PageManager.getTopmostVisiblePage().name);
550
551  ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
552  this.setProfileSupervised_(true, 'manage');
553  messages = clickAndListen();
554  assertEquals(0, messages.length);
555  assertEquals('settings', PageManager.getTopmostVisiblePage().name);
556});
557
558// Selecting a different avatar image should update the suggested profile name.
559TEST_F('ManageProfileUITest', 'Create_NameUpdateOnAvatarSelected', function() {
560  var mode = 'create';
561  this.initDefaultProfiles_(mode);
562
563  var gridEl = $(mode + '-profile-icon-grid');
564  var nameEl = $(mode + '-profile-name');
565
566  // Select another icon and check that the profile name was updated.
567  assertNotEquals(gridEl.selectedItem, this.defaultIconURLs[0]);
568  gridEl.selectedItem = this.defaultIconURLs[0];
569  expectEquals(this.defaultNames[0], nameEl.value);
570
571  // Select icon without an associated name; the profile name shouldn't change.
572  var oldName = nameEl.value;
573  assertEquals('', this.defaultNames[2]);
574  gridEl.selectedItem = this.defaultIconURLs[2];
575  expectEquals(oldName, nameEl.value);
576
577  // Select another icon with a name and check that the name is updated again.
578  assertNotEquals('', this.defaultNames[1]);
579  gridEl.selectedItem = this.defaultIconURLs[1];
580  expectEquals(this.defaultNames[1], nameEl.value);
581
582  PageManager.closeOverlay();
583});
584
585// After the user edited the profile name, selecting a different avatar image
586// should not update the suggested name anymore.
587TEST_F('ManageProfileUITest', 'Create_NoNameUpdateOnAvatarSelectedAfterEdit',
588    function() {
589  var mode = 'create';
590  this.initDefaultProfiles_(mode);
591
592  var gridEl = $(mode + '-profile-icon-grid');
593  var nameEl = $(mode + '-profile-name');
594
595  // After the user manually entered a name, it should not be changed anymore
596  // (even if the entered name is another default name).
597  nameEl.value = this.defaultNames[3];
598  nameEl.oninput();
599  gridEl.selectedItem = this.defaultIconURLs[0];
600  expectEquals(this.defaultNames[3], nameEl.value);
601
602  PageManager.closeOverlay();
603});
604
605// After the user edited the profile name, selecting a different avatar image
606// should not update the suggested name anymore even if the original suggestion
607// is entered again.
608TEST_F('ManageProfileUITest', 'Create_NoNameUpdateOnAvatarSelectedAfterRevert',
609    function() {
610  var mode = 'create';
611  this.initDefaultProfiles_(mode);
612
613  var gridEl = $(mode + '-profile-icon-grid');
614  var nameEl = $(mode + '-profile-name');
615
616  // After the user manually entered a name, it should not be changed anymore,
617  // even if the user then reverts to the original suggestion.
618  var oldName = nameEl.value;
619  nameEl.value = 'Custom Name';
620  nameEl.oninput();
621  nameEl.value = oldName;
622  nameEl.oninput();
623  // Now select another avatar and check that the name remained the same.
624  assertNotEquals(gridEl.selectedItem, this.defaultIconURLs[0]);
625  gridEl.selectedItem = this.defaultIconURLs[0];
626  expectEquals(oldName, nameEl.value);
627
628  PageManager.closeOverlay();
629});
630
631// In the manage dialog, the name should never be updated on avatar selection.
632TEST_F('ManageProfileUITest', 'Manage_NoNameUpdateOnAvatarSelected',
633    function() {
634  var mode = 'manage';
635  this.setProfileSupervised_(false, mode);
636  PageManager.showPageByName(mode + 'Profile');
637
638  var testProfile = this.testProfileInfo_(false);
639  var iconURLs = [testProfile.iconURL, '/some/path', '/another/path'];
640  var names = [testProfile.name, 'Some Name', ''];
641  ManageProfileOverlay.receiveDefaultProfileIconsAndNames(
642     mode, iconURLs, names);
643
644  var gridEl = $(mode + '-profile-icon-grid');
645  var nameEl = $(mode + '-profile-name');
646
647  // Select another icon and check if the profile name was updated.
648  var oldName = nameEl.value;
649  gridEl.selectedItem = iconURLs[1];
650  expectEquals(oldName, nameEl.value);
651
652  PageManager.closeOverlay();
653});
654
655GEN('#endif  // OS_CHROMEOS');
656