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