Home | History | Annotate | Download | only in options
      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.
      6 GEN('#if !defined(OS_CHROMEOS)');
      7 
      8 GEN('#include "base/command_line.h"');
      9 GEN('#include "chrome/common/chrome_switches.h"');
     10 
     11 /**
     12  * TestFixture for ManageProfileOverlay and CreateProfileOverlay WebUI testing.
     13  * @extends {testing.Test}
     14  * @constructor
     15  */
     16 function ManageProfileUITest() {}
     17 
     18 ManageProfileUITest.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.
     70 TEST_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.
     87 TEST_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.
     99 TEST_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 (a] 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.
    124 TEST_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.
    137 TEST_F('ManageProfileUITest', 'ShowCreateConfirmation', function() {
    138   var testProfile = this.testProfileInfo_(true);
    139   testProfile.custodianEmail = 'foo (a] 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.
    149 TEST_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.
    157 TEST_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.
    171 TEST_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.onLocalError();
    178   assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name);
    179   assertFalse(errorBubble.hidden);
    180 
    181   errorBubble.hidden = true;
    182   CreateProfileOverlay.onRemoteError();
    183   assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name);
    184   assertFalse(errorBubble.hidden);
    185 });
    186 
    187 // The name and email sould be inserted into the confirmation dialog.
    188 TEST_F('ManageProfileUITest', 'CreateConfirmationText', function () {
    189   var self = this;
    190   var custodianEmail = 'foo (a] example.com';
    191 
    192   // Checks the strings in the confirmation dialog. If |expectedNameText| is
    193   // given, it should be present in the dialog's textContent; otherwise the name
    194   // is expected. If |expectedNameHtml| is given, it should be present in the
    195   // dialog's innerHTML; otherwise the expected text is expected in the HTML
    196   // too.
    197   function checkDialog(name, expectedNameText, expectedNameHtml) {
    198     var expectedText = expectedNameText || name;
    199     var expectedHtml = expectedNameHtml || expectedText;
    200 
    201     // Configure the test profile and show the confirmation dialog.
    202     var testProfile = self.testProfileInfo_(true);
    203     testProfile.name = name;
    204     CreateProfileOverlay.onSuccess(testProfile);
    205     assertEquals('managedUserCreateConfirm',
    206                  OptionsPage.getTopmostVisiblePage().name);
    207 
    208     // Check for the presence of the name and email in the UI, without depending
    209     // on the details of the messsages.
    210     assertNotEquals(-1,
    211         $('managed-user-created-title').textContent.indexOf(expectedText));
    212     assertNotEquals(-1,
    213         $('managed-user-created-switch').textContent.indexOf(expectedText));
    214     var message = $('managed-user-created-text');
    215     assertNotEquals(-1, message.textContent.indexOf(expectedText));
    216     assertNotEquals(-1, message.textContent.indexOf(custodianEmail));
    217 
    218     // The name should be properly HTML-escaped.
    219     assertNotEquals(-1, message.innerHTML.indexOf(expectedHtml));
    220 
    221     OptionsPage.closeOverlay();
    222     assertEquals('settings', OptionsPage.getTopmostVisiblePage().name, name);
    223   }
    224 
    225   // Show and configure the create-profile dialog.
    226   OptionsPage.showPageByName('createProfile');
    227   CreateProfileOverlay.updateSignedInStatus(custodianEmail);
    228   assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name);
    229 
    230   checkDialog('OneWord');
    231   checkDialog('Multiple Words');
    232   checkDialog('It\'s "<HTML> injection" & more!',
    233               'It\'s "<HTML> injection" & more!',
    234               // The innerHTML getter doesn't escape quotation marks,
    235               // independent of whether they were escaped in the setter.
    236               'It\'s "&lt;HTML&gt; injection" &amp; more!');
    237 
    238   // Test elision. MAX_LENGTH = 50, minus 3 for the ellipsis.
    239   var name47Characters = '01234567890123456789012345678901234567890123456';
    240   var name60Characters = name47Characters + '0123456789012';
    241   checkDialog(name60Characters, name47Characters + '...');
    242 
    243   // Test both elision and HTML escaping. The allowed string length is the
    244   // visible length, not the length including the entity names.
    245   name47Characters = name47Characters.replace('0', '&').replace('1', '>');
    246   name60Characters = name60Characters.replace('0', '&').replace('1', '>');
    247   var escaped = name47Characters.replace('&', '&amp;').replace('>', '&gt;');
    248   checkDialog(name60Characters, name47Characters + '...', escaped + '...');
    249 });
    250 
    251 // An additional warning should be shown when deleting a managed user.
    252 TEST_F('ManageProfileUITest', 'DeleteManagedUserWarning', function() {
    253   var addendum = $('delete-managed-profile-addendum');
    254 
    255   ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(true));
    256   assertFalse(addendum.hidden);
    257 
    258   ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
    259   assertTrue(addendum.hidden);
    260 });
    261 
    262 // The policy prohibiting managed users should update the UI dynamically.
    263 TEST_F('ManageProfileUITest', 'PolicyDynamicRefresh', function() {
    264   ManageProfileOverlay.getInstance().initializePage();
    265 
    266   var custodianEmail = 'chrome.playpen.test (a] gmail.com';
    267   CreateProfileOverlay.updateSignedInStatus(custodianEmail);
    268   CreateProfileOverlay.updateManagedUsersAllowed(true);
    269   var checkbox = $('create-profile-managed');
    270   var link = $('create-profile-managed-not-signed-in-link');
    271   var indicator = $('create-profile-managed-indicator');
    272 
    273   assertFalse(checkbox.disabled, 'allowed and signed in');
    274   assertFalse(link.hidden, 'allowed and signed in');
    275   assertEquals('none', window.getComputedStyle(indicator, null).display,
    276                'allowed and signed in');
    277 
    278   CreateProfileOverlay.updateSignedInStatus('');
    279   CreateProfileOverlay.updateManagedUsersAllowed(true);
    280   assertTrue(checkbox.disabled, 'allowed, not signed in');
    281   assertFalse(link.hidden, 'allowed, not signed in');
    282   assertEquals('none', window.getComputedStyle(indicator, null).display,
    283                'allowed, not signed in');
    284 
    285   CreateProfileOverlay.updateSignedInStatus('');
    286   CreateProfileOverlay.updateManagedUsersAllowed(false);
    287   assertTrue(checkbox.disabled, 'disallowed, not signed in');
    288   assertTrue(link.hidden, 'disallowed, not signed in');
    289   assertEquals('inline-block', window.getComputedStyle(indicator, null).display,
    290                'disallowed, not signed in');
    291   assertEquals('policy', indicator.getAttribute('controlled-by'));
    292 
    293   CreateProfileOverlay.updateSignedInStatus(custodianEmail);
    294   CreateProfileOverlay.updateManagedUsersAllowed(false);
    295   assertTrue(checkbox.disabled, 'disallowed, signed in');
    296   assertTrue(link.hidden, 'disallowed, signed in');
    297   assertEquals('inline-block', window.getComputedStyle(indicator, null).display,
    298                'disallowed, signed in');
    299   assertEquals('policy', indicator.getAttribute('controlled-by'));
    300 
    301   CreateProfileOverlay.updateSignedInStatus(custodianEmail);
    302   CreateProfileOverlay.updateManagedUsersAllowed(true);
    303   assertFalse(checkbox.disabled, 're-allowed and signed in');
    304   assertFalse(link.hidden, 're-allowed and signed in');
    305   assertEquals('none', window.getComputedStyle(indicator, null).display,
    306                're-allowed and signed in');
    307 });
    308 
    309 // Managed users shouldn't be able to open the delete or create dialogs.
    310 TEST_F('ManageProfileUITest', 'ManagedShowDeleteAndCreate', function() {
    311   this.setProfileManaged_(false);
    312 
    313   ManageProfileOverlay.showCreateDialog();
    314   assertEquals('createProfile', OptionsPage.getTopmostVisiblePage().name);
    315   OptionsPage.closeOverlay();
    316   assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);
    317   ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
    318   assertEquals('manageProfile', OptionsPage.getTopmostVisiblePage().name);
    319   assertFalse($('manage-profile-overlay-delete').hidden);
    320   OptionsPage.closeOverlay();
    321   assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);
    322 
    323   this.setProfileManaged_(true);
    324   ManageProfileOverlay.showCreateDialog();
    325   assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);
    326   ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
    327   assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);
    328 });
    329 
    330 // Only non-managed users should be able to delete profiles.
    331 TEST_F('ManageProfileUITest', 'ManagedDelete', function() {
    332   ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
    333   assertEquals('manageProfile', OptionsPage.getTopmostVisiblePage().name);
    334   assertFalse($('manage-profile-overlay-delete').hidden);
    335 
    336   // Clicks the "Delete" button, after overriding chrome.send to record what
    337   // messages were sent.
    338   function clickAndListen() {
    339     var originalChromeSend = chrome.send;
    340     var chromeSendMessages = [];
    341     chrome.send = function(message) {
    342       chromeSendMessages.push(message);
    343     };
    344     $('delete-profile-ok').onclick();
    345     // Restore the original function so the test framework can use it.
    346     chrome.send = originalChromeSend;
    347     return chromeSendMessages;
    348   }
    349 
    350   this.setProfileManaged_(false);
    351   var messages = clickAndListen();
    352   assertEquals(1, messages.length);
    353   assertEquals('deleteProfile', messages[0]);
    354   assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);
    355 
    356   ManageProfileOverlay.showDeleteDialog(this.testProfileInfo_(false));
    357   this.setProfileManaged_(true);
    358   messages = clickAndListen();
    359   assertEquals(0, messages.length);
    360   assertEquals('settings', OptionsPage.getTopmostVisiblePage().name);
    361 });
    362 
    363 GEN('#endif  // OS_CHROMEOS');
    364