summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/options/manage_profile_browsertest.js
blob: 427a6e719e541c41ee39d81b59de1e09fcdb7219 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// None of these tests is relevant for Chrome OS.
GEN('#if !defined(OS_CHROMEOS)');

GEN('#include "base/command_line.h"');
GEN('#include "chrome/common/chrome_switches.h"');

/**
 * TestFixture for ManageProfileOverlay and CreateProfileOverlay WebUI testing.
 * @extends {testing.Test}
 * @constructor
 */
function ManageProfileUITest() {}

ManageProfileUITest.prototype = {
  __proto__: testing.Test.prototype,

  /** @override */
  browsePreload: 'chrome://settings-frame/manageProfile',

  /**
   * No need to run these for every OptionsPage test, since they'll cover the
   * whole consolidated page each time.
   * @override
   */
  runAccessibilityChecks: false,

  /** @override */
  testGenPreamble: function() {
    GEN('CommandLine::ForCurrentProcess()->' +
        'AppendSwitch(switches::kEnableManagedUsers);');
  },

  /**
   * Returns a test profile-info object with configurable "managed" status.
   * @param {boolean} managed If true, the test profile will be marked as
   *     managed.
   * @return {Object} A test profile-info object.
   */
  testProfileInfo_: function(managed) {
    return {
      name: 'Test Profile',
      iconURL: 'chrome://path/to/icon/image',
      filePath: '/path/to/profile/data/on/disk',
      isCurrentProfile: true,
      isManaged: managed
    };
  },

  /**
   * Overrides WebUI methods that provide profile info, making them return a
   * test profile-info object.
   * @param {boolean} managed Whether the test profile should be marked managed.
   */
  setProfileManaged_: function(managed) {
    // Override the BrowserOptions method to return the fake info.
    BrowserOptions.getCurrentProfile = function() {
      return this.testProfileInfo_(managed);
    }.bind(this);
    // Set the profile info in the overlay.
    ManageProfileOverlay.setProfileInfo(this.testProfileInfo_(managed),
                                        'manage');
  },
};

// Receiving the new profile defaults in the manage-user overlay shouldn't mess
// up the focus in a visible higher-level overlay.
TEST_F('ManageProfileUITest', 'NewProfileDefaultsFocus', function() {
  var self = this;

  function checkFocus(pageName, expectedFocus, initialFocus) {
    OptionsPage.showPageByName(pageName);
    initialFocus.focus();
    expectEquals(initialFocus, document.activeElement, pageName);

    ManageProfileOverlay.receiveNewProfileDefaults(
        self.testProfileInfo_(false));
    expectEquals(expectedFocus, document.activeElement, pageName);
    OptionsPage.closeOverlay();
  }

  // Receiving new profile defaults sets focus to the name field if the create
  // overlay is open, and should not change focus at all otherwise.
  checkFocus('manageProfile',
             $('manage-profile-cancel'),
             $('manage-profile-cancel'));
  checkFocus('createProfile',
             $('create-profile-name'),
             $('create-profile-cancel'));
  checkFocus('managedUserLearnMore',
             $('managed-user-learn-more-done'),
             $('managed-user-learn-more-done'));
  checkFocus('managedUserLearnMore',
             document.querySelector('#managed-user-learn-more-text a'),
             document.querySelector('#managed-user-learn-more-text a'));
});

// The default options should be reset each time the creation overlay is shown.
TEST_F('ManageProfileUITest', 'DefaultCreateOptions', function() {
  OptionsPage.showPageByName('createProfile');
  var shortcutsAllowed = loadTimeData.getBoolean('profileShortcutsEnabled');
  var createShortcut = $('create-shortcut');
  var createManaged = $('create-profile-managed');
  assertEquals(shortcutsAllowed, createShortcut.checked);
  assertFalse(createManaged.checked);

  createShortcut.checked = !shortcutsAllowed;
  createManaged.checked = true;
  OptionsPage.closeOverlay();
  OptionsPage.showPageByName('createProfile');
  assertEquals(shortcutsAllowed, createShortcut.checked);
  assertFalse(createManaged.checked);
});

// Creating managed users should be disallowed when they are not enabled.
TEST_F('ManageProfileUITest', 'CreateManagedUserAllowed', function() {
  var container = $('create-profile-managed-container');

  ManageProfileOverlay.getInstance().initializePage();
  assertFalse(container.hidden);

  loadTimeData.overrideValues({'managedUsersEnabled': false});
  ManageProfileOverlay.getInstance().initializePage();
  assertTrue(container.hidden);
});

// The checkbox label should change depending on whether the user is signed in.
TEST_F('ManageProfileUITest', 'CreateManagedUserText', function() {
  var signedInText =  $('create-profile-managed-signed-in');
  var notSignedInText = $('create-profile-managed-not-signed-in');

  ManageProfileOverlay.getInstance().initializePage();

  var custodianEmail = 'chrome.playpen.test@gmail.com';
  CreateProfileOverlay.updateSignedInStatus(custodianEmail);
  assertEquals(custodianEmail,
               CreateProfileOverlay.getInstance().signedInEmail_);
  assertFalse(signedInText.hidden);
  assertTrue(notSignedInText.hidden);
  // Make sure the email is in the string somewhere, without depending on the
  // exact details of the message.
  assertNotEquals(-1, signedInText.textContent.indexOf(custodianEmail));

  CreateProfileOverlay.updateSignedInStatus('');
  assertEquals('', CreateProfileOverlay.getInstance().signedInEmail_);
  assertTrue(signedInText.hidden);
  assertFalse(notSignedInText.hidden);
  assertFalse($('create-profile-managed').checked);
  assertTrue($('create-profile-managed').disabled);
});

// Managed users should not be able to edit their profile names, and the initial
// focus should be adjusted accordingly.
TEST_F('ManageProfileUITest', 'EditManagedUserNameAllowed', function() {
  var nameField = $('manage-profile-name');

  this.setProfileManaged_(false);
  ManageProfileOverlay.showManageDialog();
  expectFalse(nameField.disabled);
  expectEquals(nameField, document.activeElement);

  OptionsPage.closeOverlay();

  this.setProfileManaged_(true);
  ManageProfileOverlay.showManageDialog();
  expectTrue(nameField.disabled);
  expectEquals($('manage-profile-ok'), document.activeElement);
});

// Setting profile information should allow the confirmation to be shown.
TEST_F('ManageProfileUITest', 'ShowCreateConfirmation', function() {
  var testProfile = this.testProfileInfo_(true);
  testProfile.custodianEmail = 'foo@bar.example.com';
  ManagedUserCreateConfirmOverlay.setProfileInfo(testProfile);
  assertTrue(ManagedUserCreateConfirmOverlay.getInstance().canShowPage());
  OptionsPage.showPageByName('managedUserCreateConfirm', false);
  assertEquals('managedUserCreateConfirm',
               OptionsPage.getTopmostVisiblePage().name);
});

// Trying to show a confirmation dialog with no profile information should fall
// back to the default (main) settings page.
TEST_F('ManageProfileUITest', 'NoEmptyConfirmation', function() {
  assertEquals('manageProfile', OptionsPage.getTopmostVisiblePage().name);
  assertFalse(ManagedUserCreateConfirmOverlay.getInstance().canShowPage());
  OptionsPage.showPageByName('managedUserCreateConfirm', true);
  assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);
});

// A confirmation dialog should be shown after creating a new managed user.
TEST_F('ManageProfileUITest', 'ShowCreateConfirmationOnSuccess', function() {
  OptionsPage.showPageByName('createProfile');
  assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name);
  CreateProfileOverlay.onSuccess(this.testProfileInfo_(false));
  assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);

  OptionsPage.showPageByName('createProfile');
  assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name);
  CreateProfileOverlay.onSuccess(this.testProfileInfo_(true));
  assertEquals('managedUserCreateConfirm',
               OptionsPage.getTopmostVisiblePage().name);
  expectEquals($('managed-user-created-switch'), document.activeElement);
});

// An error should be shown if creating a new managed user fails.
TEST_F('ManageProfileUITest', 'NoCreateConfirmationOnError', function() {
  OptionsPage.showPageByName('createProfile');
  assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name);
  var errorBubble = $('create-profile-error-bubble');
  assertTrue(errorBubble.hidden);

  CreateProfileOverlay.onError('An Error Message!');
  assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name);
  assertFalse(errorBubble.hidden);
});

// The name and email sould be inserted into the confirmation dialog.
TEST_F('ManageProfileUITest', 'CreateConfirmationText', function () {
  var self = this;
  var custodianEmail = 'foo@example.com';

  // Checks the strings in the confirmation dialog. If |expectedNameText| is
  // given, it should be present in the dialog's textContent; otherwise the name
  // is expected. If |expectedNameHtml| is given, it should be present in the
  // dialog's innerHTML; otherwise the expected text is expected in the HTML
  // too.
  function checkDialog(name, expectedNameText, expectedNameHtml) {
    var expectedText = expectedNameText || name;
    var expectedHtml = expectedNameHtml || expectedText;

    // Configure the test profile and show the confirmation dialog.
    var testProfile = self.testProfileInfo_(true);
    testProfile.name = name;
    CreateProfileOverlay.onSuccess(testProfile);
    assertEquals('managedUserCreateConfirm',
                 OptionsPage.getTopmostVisiblePage().name);

    // Check for the presence of the name and email in the UI, without depending
    // on the details of the messsages.
    assertNotEquals(-1,
        $('managed-user-created-title').textContent.indexOf(expectedText));
    assertNotEquals(-1,
        $('managed-user-created-switch').textContent.indexOf(expectedText));
    var message = $('managed-user-created-text');
    assertNotEquals(-1, message.textContent.indexOf(expectedText));
    assertNotEquals(-1, message.textContent.indexOf(custodianEmail));

    // The name should be properly HTML-escaped.
    assertNotEquals(-1, message.innerHTML.indexOf(expectedHtml));

    OptionsPage.closeOverlay();
    assertEquals('settings', OptionsPage.getTopmostVisiblePage().name, name);
  }

  // Show and configure the create-profile dialog.
  OptionsPage.showPageByName('createProfile');
  CreateProfileOverlay.updateSignedInStatus(custodianEmail);
  assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name);

  checkDialog('OneWord');
  checkDialog('Multiple Words');
  checkDialog('It\'s "<HTML> injection" & more!',
              'It\'s "<HTML> injection" & more!',
              // The innerHTML getter doesn't escape quotation marks,
              // independent of whether they were escaped in the setter.
              'It\'s "&lt;HTML&gt; injection" &amp; more!');

  // Test elision. MAX_LENGTH = 50, minus 3 for the ellipsis.
  var name47Characters = '01234567890123456789012345678901234567890123456';
  var name60Characters = name47Characters + '0123456789012';
  checkDialog(name60Characters, name47Characters + '...');

  // Test both elision and HTML escaping. The allowed string length is the
  // visible length, not the length including the entity names.
  name47Characters = name47Characters.replace('0', '&').replace('1', '>');
  name60Characters = name60Characters.replace('0', '&').replace('1', '>');
  var escaped = name47Characters.replace('&', '&amp;').replace('>', '&gt;');
  checkDialog(name60Characters, name47Characters + '...', escaped + '...');
});

// An additional warning should be shown when deleting a managed user.
TEST_F('ManageProfileUITest', 'DeleteManagedUserWarning', function() {
  var addendum = $('delete-managed-profile-addendum');

  ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(true));
  assertFalse(addendum.hidden);

  ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
  assertTrue(addendum.hidden);
});

// The policy prohibiting managed users should update the UI dynamically.
TEST_F('ManageProfileUITest', 'PolicyDynamicRefresh', function() {
  ManageProfileOverlay.getInstance().initializePage();

  var custodianEmail = 'chrome.playpen.test@gmail.com';
  CreateProfileOverlay.updateSignedInStatus(custodianEmail);
  CreateProfileOverlay.updateManagedUsersAllowed(true);
  var checkbox = $('create-profile-managed');
  var link = $('create-profile-managed-not-signed-in-link');
  var indicator = $('create-profile-managed-indicator');

  assertFalse(checkbox.disabled, 'allowed and signed in');
  assertFalse(link.hidden, 'allowed and signed in');
  assertEquals('none', window.getComputedStyle(indicator, null).display,
               'allowed and signed in');

  CreateProfileOverlay.updateSignedInStatus('');
  CreateProfileOverlay.updateManagedUsersAllowed(true);
  assertTrue(checkbox.disabled, 'allowed, not signed in');
  assertFalse(link.hidden, 'allowed, not signed in');
  assertEquals('none', window.getComputedStyle(indicator, null).display,
               'allowed, not signed in');

  CreateProfileOverlay.updateSignedInStatus('');
  CreateProfileOverlay.updateManagedUsersAllowed(false);
  assertTrue(checkbox.disabled, 'disallowed, not signed in');
  assertTrue(link.hidden, 'disallowed, not signed in');
  assertEquals('inline-block', window.getComputedStyle(indicator, null).display,
               'disallowed, not signed in');
  assertEquals('policy', indicator.getAttribute('controlled-by'));

  CreateProfileOverlay.updateSignedInStatus(custodianEmail);
  CreateProfileOverlay.updateManagedUsersAllowed(false);
  assertTrue(checkbox.disabled, 'disallowed, signed in');
  assertTrue(link.hidden, 'disallowed, signed in');
  assertEquals('inline-block', window.getComputedStyle(indicator, null).display,
               'disallowed, signed in');
  assertEquals('policy', indicator.getAttribute('controlled-by'));

  CreateProfileOverlay.updateSignedInStatus(custodianEmail);
  CreateProfileOverlay.updateManagedUsersAllowed(true);
  assertFalse(checkbox.disabled, 're-allowed and signed in');
  assertFalse(link.hidden, 're-allowed and signed in');
  assertEquals('none', window.getComputedStyle(indicator, null).display,
               're-allowed and signed in');
});

// The managed user checkbox should correctly update its state during profile
// creation and afterwards.
TEST_F('ManageProfileUITest', 'CreateInProgress', function() {
  ManageProfileOverlay.getInstance().initializePage();

  var custodianEmail = 'chrome.playpen.test@gmail.com';
  CreateProfileOverlay.updateSignedInStatus(custodianEmail);
  CreateProfileOverlay.updateManagedUsersAllowed(true);
  var checkbox = $('create-profile-managed');
  var link = $('create-profile-managed-not-signed-in-link');
  var indicator = $('create-profile-managed-indicator');

  assertFalse(checkbox.disabled, 'allowed and signed in');
  assertFalse(link.hidden, 'allowed and signed in');
  assertEquals('none', window.getComputedStyle(indicator, null).display,
               'allowed and signed in');
  assertFalse(indicator.hasAttribute('controlled-by'));

  CreateProfileOverlay.updateCreateInProgress(true);
  assertTrue(checkbox.disabled, 'creation in progress');

  // A no-op update to the sign-in status should not change the UI.
  CreateProfileOverlay.updateSignedInStatus(custodianEmail);
  CreateProfileOverlay.updateManagedUsersAllowed(true);
  assertTrue(checkbox.disabled, 'creation in progress');

  CreateProfileOverlay.updateCreateInProgress(false);
  assertFalse(checkbox.disabled, 'creation finished');
});

// Managed users shouldn't be able to open the delete or create dialogs.
TEST_F('ManageProfileUITest', 'ManagedShowDeleteAndCreate', function() {
  this.setProfileManaged_(false);

  ManageProfileOverlay.showCreateDialog();
  assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name);
  OptionsPage.closeOverlay();
  assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);
  ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
  assertEquals('manageProfile', OptionsPage.getTopmostVisiblePage().name);
  assertFalse($('manage-profile-overlay-delete').hidden);
  OptionsPage.closeOverlay();
  assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);

  this.setProfileManaged_(true);
  ManageProfileOverlay.showCreateDialog();
  assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);
  ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
  assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);
});

// Only non-managed users should be able to delete profiles.
TEST_F('ManageProfileUITest', 'ManagedDelete', function() {
  ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
  assertEquals('manageProfile', OptionsPage.getTopmostVisiblePage().name);
  assertFalse($('manage-profile-overlay-delete').hidden);

  // Clicks the "Delete" button, after overriding chrome.send to record what
  // messages were sent.
  function clickAndListen() {
    var originalChromeSend = chrome.send;
    var chromeSendMessages = [];
    chrome.send = function(message) {
      chromeSendMessages.push(message);
    };
    $('delete-profile-ok').onclick();
    // Restore the original function so the test framework can use it.
    chrome.send = originalChromeSend;
    return chromeSendMessages;
  }

  this.setProfileManaged_(false);
  var messages = clickAndListen();
  assertEquals(1, messages.length);
  assertEquals('deleteProfile', messages[0]);
  assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);

  ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
  this.setProfileManaged_(true);
  messages = clickAndListen();
  assertEquals(0, messages.length);
  assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);
});

GEN('#endif  // OS_CHROMEOS');