manage_profile_browsertest.js revision 58537e28ecd584eab876aee8be7156509866d23a
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 8GEN('#include "base/command_line.h"'); 9GEN('#include "chrome/common/chrome_switches.h"'); 10 11/** 12 * TestFixture for ManageProfileOverlay and CreateProfileOverlay WebUI testing. 13 * @extends {testing.Test} 14 * @constructor 15 */ 16function ManageProfileUITest() {} 17 18ManageProfileUITest.prototype = { 19 __proto__: testing.Test.prototype, 20 21 /** @override */ 22 browsePreload: 'chrome://settings-frame/manageProfile', 23 24 /** 25 * No need to run these for every OptionsPage test, since they'll cover the 26 * whole consolidated page each time. 27 * @override 28 */ 29 runAccessibilityChecks: false, 30 31 /** @override */ 32 testGenPreamble: function() { 33 GEN('CommandLine::ForCurrentProcess()->' + 34 'AppendSwitch(switches::kEnableManagedUsers);'); 35 }, 36 37 /** 38 * Returns a test profile-info object with configurable "managed" status. 39 * @param {boolean} managed If true, the test profile will be marked as 40 * managed. 41 * @return {Object} A test profile-info object. 42 */ 43 testProfileInfo_: function(managed) { 44 return { 45 name: 'Test Profile', 46 iconURL: 'chrome://path/to/icon/image', 47 filePath: '/path/to/profile/data/on/disk', 48 isCurrentProfile: true, 49 isManaged: managed 50 }; 51 }, 52 53 /** 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 */ 58 setProfileManaged_: function(managed) { 59 // Override the BrowserOptions method to return the fake info. 60 BrowserOptions.getCurrentProfile = function() { 61 return this.testProfileInfo_(managed); 62 }.bind(this); 63 // Set the profile info in the overlay. 64 ManageProfileOverlay.setProfileInfo(this.testProfileInfo_(managed), 65 'manage'); 66 }, 67}; 68 69// The default options should be reset each time the creation overlay is shown. 70TEST_F('ManageProfileUITest', 'DefaultCreateOptions', function() { 71 OptionsPage.showPageByName('createProfile'); 72 var shortcutsAllowed = loadTimeData.getBoolean('profileShortcutsEnabled'); 73 var createShortcut = $('create-shortcut'); 74 var createManaged = $('create-profile-managed'); 75 assertEquals(shortcutsAllowed, createShortcut.checked); 76 assertFalse(createManaged.checked); 77 78 createShortcut.checked = !shortcutsAllowed; 79 createManaged.checked = true; 80 OptionsPage.closeOverlay(); 81 OptionsPage.showPageByName('createProfile'); 82 assertEquals(shortcutsAllowed, createShortcut.checked); 83 assertFalse(createManaged.checked); 84}); 85 86// Creating managed users should be disallowed when they are not enabled. 87TEST_F('ManageProfileUITest', 'CreateManagedUserAllowed', function() { 88 var container = $('create-profile-managed-container'); 89 90 ManageProfileOverlay.getInstance().initializePage(); 91 assertFalse(container.hidden); 92 93 loadTimeData.overrideValues({'managedUsersEnabled': false}); 94 ManageProfileOverlay.getInstance().initializePage(); 95 assertTrue(container.hidden); 96}); 97 98// The checkbox label should change depending on whether the user is signed in. 99TEST_F('ManageProfileUITest', 'CreateManagedUserText', function() { 100 var signedInText = $('create-profile-managed-signed-in'); 101 var notSignedInText = $('create-profile-managed-not-signed-in'); 102 103 ManageProfileOverlay.getInstance().initializePage(); 104 105 var custodianEmail = 'chrome.playpen.test@gmail.com'; 106 CreateProfileOverlay.updateSignedInStatus(custodianEmail); 107 assertEquals(custodianEmail, 108 CreateProfileOverlay.getInstance().signedInEmail_); 109 assertFalse(signedInText.hidden); 110 assertTrue(notSignedInText.hidden); 111 // Make sure the email is in the string somewhere, without depending on the 112 // exact details of the message. 113 assertNotEquals(-1, signedInText.textContent.indexOf(custodianEmail)); 114 115 CreateProfileOverlay.updateSignedInStatus(''); 116 assertEquals('', CreateProfileOverlay.getInstance().signedInEmail_); 117 assertTrue(signedInText.hidden); 118 assertFalse(notSignedInText.hidden); 119 assertFalse($('create-profile-managed').checked); 120 assertTrue($('create-profile-managed').disabled); 121}); 122 123// Managed users should not be able to edit their profile names. 124TEST_F('ManageProfileUITest', 'EditManagedUserNameAllowed', function() { 125 var nameField = $('manage-profile-name'); 126 127 this.setProfileManaged_(false); 128 ManageProfileOverlay.showManageDialog(); 129 assertFalse(nameField.disabled); 130 131 this.setProfileManaged_(true); 132 ManageProfileOverlay.showManageDialog(); 133 assertTrue(nameField.disabled); 134}); 135 136// Setting profile information should allow the confirmation to be shown. 137TEST_F('ManageProfileUITest', 'ShowCreateConfirmation', function() { 138 var testProfile = this.testProfileInfo_(true); 139 testProfile.custodianEmail = 'foo@bar.example.com'; 140 ManagedUserCreateConfirmOverlay.setProfileInfo(testProfile); 141 assertTrue(ManagedUserCreateConfirmOverlay.getInstance().canShowPage()); 142 OptionsPage.showPageByName('managedUserCreateConfirm', false); 143 assertEquals('managedUserCreateConfirm', 144 OptionsPage.getTopmostVisiblePage().name); 145}); 146 147// Trying to show a confirmation dialog with no profile information should fall 148// back to the default (main) settings page. 149TEST_F('ManageProfileUITest', 'NoEmptyConfirmation', function() { 150 assertEquals('manageProfile', OptionsPage.getTopmostVisiblePage().name); 151 assertFalse(ManagedUserCreateConfirmOverlay.getInstance().canShowPage()); 152 OptionsPage.showPageByName('managedUserCreateConfirm', true); 153 assertEquals('settings', OptionsPage.getTopmostVisiblePage().name); 154}); 155 156// A confirmation dialog should be shown after creating a new managed user. 157TEST_F('ManageProfileUITest', 'ShowCreateConfirmationOnSuccess', function() { 158 OptionsPage.showPageByName('createProfile'); 159 assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name); 160 CreateProfileOverlay.onSuccess(this.testProfileInfo_(false)); 161 assertEquals('settings', OptionsPage.getTopmostVisiblePage().name); 162 163 OptionsPage.showPageByName('createProfile'); 164 assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name); 165 CreateProfileOverlay.onSuccess(this.testProfileInfo_(true)); 166 assertEquals('managedUserCreateConfirm', 167 OptionsPage.getTopmostVisiblePage().name); 168}); 169 170// An error should be shown if creating a new managed user fails. 171TEST_F('ManageProfileUITest', 'NoCreateConfirmationOnError', function() { 172 OptionsPage.showPageByName('createProfile'); 173 assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name); 174 var errorBubble = $('create-profile-error-bubble'); 175 assertTrue(errorBubble.hidden); 176 177 CreateProfileOverlay.onError('An Error Message!'); 178 assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name); 179 assertFalse(errorBubble.hidden); 180}); 181 182// The name and email sould be inserted into the confirmation dialog. 183TEST_F('ManageProfileUITest', 'CreateConfirmationText', function () { 184 var self = this; 185 var custodianEmail = 'foo@example.com'; 186 187 // Checks the strings in the confirmation dialog. If |expectedNameText| is 188 // given, it should be present in the dialog's textContent; otherwise the name 189 // is expected. If |expectedNameHtml| is given, it should be present in the 190 // dialog's innerHTML; otherwise the expected text is expected in the HTML 191 // too. 192 function checkDialog(name, expectedNameText, expectedNameHtml) { 193 var expectedText = expectedNameText || name; 194 var expectedHtml = expectedNameHtml || expectedText; 195 196 // Configure the test profile and show the confirmation dialog. 197 var testProfile = self.testProfileInfo_(true); 198 testProfile.name = name; 199 CreateProfileOverlay.onSuccess(testProfile); 200 assertEquals('managedUserCreateConfirm', 201 OptionsPage.getTopmostVisiblePage().name); 202 203 // Check for the presence of the name and email in the UI, without depending 204 // on the details of the messsages. 205 assertNotEquals(-1, 206 $('managed-user-created-title').textContent.indexOf(expectedText)); 207 assertNotEquals(-1, 208 $('managed-user-created-switch').textContent.indexOf(expectedText)); 209 var message = $('managed-user-created-text'); 210 assertNotEquals(-1, message.textContent.indexOf(expectedText)); 211 assertNotEquals(-1, message.textContent.indexOf(custodianEmail)); 212 213 // The name should be properly HTML-escaped. 214 assertNotEquals(-1, message.innerHTML.indexOf(expectedHtml)); 215 216 OptionsPage.closeOverlay(); 217 assertEquals('settings', OptionsPage.getTopmostVisiblePage().name, name); 218 } 219 220 // Show and configure the create-profile dialog. 221 OptionsPage.showPageByName('createProfile'); 222 CreateProfileOverlay.updateSignedInStatus(custodianEmail); 223 assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name); 224 225 checkDialog('OneWord'); 226 checkDialog('Multiple Words'); 227 checkDialog('It\'s "<HTML> injection" & more!', 228 'It\'s "<HTML> injection" & more!', 229 // The innerHTML getter doesn't escape quotation marks, 230 // independent of whether they were escaped in the setter. 231 'It\'s "<HTML> injection" & more!'); 232 233 // Test elision. MAX_LENGTH = 50, minus 3 for the ellipsis. 234 var name47Characters = '01234567890123456789012345678901234567890123456'; 235 var name60Characters = name47Characters + '0123456789012'; 236 checkDialog(name60Characters, name47Characters + '...'); 237 238 // Test both elision and HTML escaping. The allowed string length is the 239 // visible length, not the length including the entity names. 240 name47Characters = name47Characters.replace('0', '&').replace('1', '>'); 241 name60Characters = name60Characters.replace('0', '&').replace('1', '>'); 242 var escaped = name47Characters.replace('&', '&').replace('>', '>'); 243 checkDialog(name60Characters, name47Characters + '...', escaped + '...'); 244}); 245 246// An additional warning should be shown when deleting a managed user. 247TEST_F('ManageProfileUITest', 'DeleteManagedUserWarning', function() { 248 var addendum = $('delete-managed-profile-addendum'); 249 250 ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(true)); 251 assertFalse(addendum.hidden); 252 253 ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false)); 254 assertTrue(addendum.hidden); 255}); 256 257// The policy prohibiting managed users should update the UI dynamically. 258TEST_F('ManageProfileUITest', 'PolicyDynamicRefresh', function() { 259 ManageProfileOverlay.getInstance().initializePage(); 260 261 var custodianEmail = 'chrome.playpen.test@gmail.com'; 262 CreateProfileOverlay.updateSignedInStatus(custodianEmail); 263 CreateProfileOverlay.updateManagedUsersAllowed(true); 264 var checkbox = $('create-profile-managed'); 265 var link = $('create-profile-managed-not-signed-in-link'); 266 var indicator = $('create-profile-managed-indicator'); 267 268 assertFalse(checkbox.disabled, 'allowed and signed in'); 269 assertFalse(link.hidden, 'allowed and signed in'); 270 assertEquals('none', window.getComputedStyle(indicator, null).display, 271 'allowed and signed in'); 272 273 CreateProfileOverlay.updateSignedInStatus(''); 274 CreateProfileOverlay.updateManagedUsersAllowed(true); 275 assertTrue(checkbox.disabled, 'allowed, not signed in'); 276 assertFalse(link.hidden, 'allowed, not signed in'); 277 assertEquals('none', window.getComputedStyle(indicator, null).display, 278 'allowed, not signed in'); 279 280 CreateProfileOverlay.updateSignedInStatus(''); 281 CreateProfileOverlay.updateManagedUsersAllowed(false); 282 assertTrue(checkbox.disabled, 'disallowed, not signed in'); 283 assertTrue(link.hidden, 'disallowed, not signed in'); 284 assertEquals('inline-block', window.getComputedStyle(indicator, null).display, 285 'disallowed, not signed in'); 286 assertEquals('policy', indicator.getAttribute('controlled-by')); 287 288 CreateProfileOverlay.updateSignedInStatus(custodianEmail); 289 CreateProfileOverlay.updateManagedUsersAllowed(false); 290 assertTrue(checkbox.disabled, 'disallowed, signed in'); 291 assertTrue(link.hidden, 'disallowed, signed in'); 292 assertEquals('inline-block', window.getComputedStyle(indicator, null).display, 293 'disallowed, signed in'); 294 assertEquals('policy', indicator.getAttribute('controlled-by')); 295 296 CreateProfileOverlay.updateSignedInStatus(custodianEmail); 297 CreateProfileOverlay.updateManagedUsersAllowed(true); 298 assertFalse(checkbox.disabled, 're-allowed and signed in'); 299 assertFalse(link.hidden, 're-allowed and signed in'); 300 assertEquals('none', window.getComputedStyle(indicator, null).display, 301 're-allowed and signed in'); 302}); 303 304// The managed user checkbox should correctly update its state during profile 305// creation and afterwards. 306TEST_F('ManageProfileUITest', 'CreateInProgress', function() { 307 ManageProfileOverlay.getInstance().initializePage(); 308 309 var custodianEmail = 'chrome.playpen.test@gmail.com'; 310 CreateProfileOverlay.updateSignedInStatus(custodianEmail); 311 CreateProfileOverlay.updateManagedUsersAllowed(true); 312 var checkbox = $('create-profile-managed'); 313 var link = $('create-profile-managed-not-signed-in-link'); 314 var indicator = $('create-profile-managed-indicator'); 315 316 assertFalse(checkbox.disabled, 'allowed and signed in'); 317 assertFalse(link.hidden, 'allowed and signed in'); 318 assertEquals('none', window.getComputedStyle(indicator, null).display, 319 'allowed and signed in'); 320 assertFalse(indicator.hasAttribute('controlled-by')); 321 322 CreateProfileOverlay.updateCreateInProgress(true); 323 assertTrue(checkbox.disabled, 'creation in progress'); 324 325 // A no-op update to the sign-in status should not change the UI. 326 CreateProfileOverlay.updateSignedInStatus(custodianEmail); 327 CreateProfileOverlay.updateManagedUsersAllowed(true); 328 assertTrue(checkbox.disabled, 'creation in progress'); 329 330 CreateProfileOverlay.updateCreateInProgress(false); 331 assertFalse(checkbox.disabled, 'creation finished'); 332}); 333 334// Managed users shouldn't be able to open the delete or create dialogs. 335TEST_F('ManageProfileUITest', 'ManagedShowDeleteAndCreate', function() { 336 this.setProfileManaged_(false); 337 338 ManageProfileOverlay.showCreateDialog(); 339 assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name); 340 OptionsPage.closeOverlay(); 341 assertEquals('settings', OptionsPage.getTopmostVisiblePage().name); 342 ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false)); 343 assertEquals('manageProfile', OptionsPage.getTopmostVisiblePage().name); 344 assertFalse($('manage-profile-overlay-delete').hidden); 345 OptionsPage.closeOverlay(); 346 assertEquals('settings', OptionsPage.getTopmostVisiblePage().name); 347 348 this.setProfileManaged_(true); 349 ManageProfileOverlay.showCreateDialog(); 350 assertEquals('settings', OptionsPage.getTopmostVisiblePage().name); 351 ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false)); 352 assertEquals('settings', OptionsPage.getTopmostVisiblePage().name); 353}); 354 355// Only non-managed users should be able to delete profiles. 356TEST_F('ManageProfileUITest', 'ManagedDelete', function() { 357 ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false)); 358 assertEquals('manageProfile', OptionsPage.getTopmostVisiblePage().name); 359 assertFalse($('manage-profile-overlay-delete').hidden); 360 361 // Clicks the "Delete" button, after overriding chrome.send to record what 362 // messages were sent. 363 function clickAndListen() { 364 var originalChromeSend = chrome.send; 365 var chromeSendMessages = []; 366 chrome.send = function(message) { 367 chromeSendMessages.push(message); 368 }; 369 $('delete-profile-ok').onclick(); 370 // Restore the original function so the test framework can use it. 371 chrome.send = originalChromeSend; 372 return chromeSendMessages; 373 } 374 375 this.setProfileManaged_(false); 376 var messages = clickAndListen(); 377 assertEquals(1, messages.length); 378 assertEquals('deleteProfile', messages[0]); 379 assertEquals('settings', OptionsPage.getTopmostVisiblePage().name); 380 381 ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false)); 382 this.setProfileManaged_(true); 383 messages = clickAndListen(); 384 assertEquals(0, messages.length); 385 assertEquals('settings', OptionsPage.getTopmostVisiblePage().name); 386}); 387 388GEN('#endif // OS_CHROMEOS'); 389