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 5GEN('#include "chrome/browser/ui/webui/downloads_ui_browsertest.h"'); 6 7/** @const */ var TOTAL_RESULT_COUNT = 25; 8 9/** 10 * Test C++ fixture for downloads WebUI testing. 11 * @constructor 12 * @extends {testing.Test} 13 */ 14function DownloadsUIBrowserTest() {} 15 16/** 17 * Base fixture for Downloads WebUI testing. 18 * @extends {testing.Test} 19 * @constructor 20 */ 21function BaseDownloadsWebUITest() {} 22 23BaseDownloadsWebUITest.prototype = { 24 __proto__: testing.Test.prototype, 25 26 /** 27 * Browse to the downloads page & call our preLoad(). 28 */ 29 browsePreload: 'chrome://downloads/', 30 31 /** @override */ 32 typedefCppFixture: 'DownloadsUIBrowserTest', 33 34 /** @override */ 35 testGenPreamble: function() { 36 GEN(' SetDeleteAllowed(true);'); 37 }, 38 39 /** @override */ 40 runAccessibilityChecks: true, 41 42 /** @override */ 43 accessibilityIssuesAreErrors: true, 44 45 /** 46 * Sends TOTAL_RESULT_COUNT fake downloads to the page. This can't be called 47 * in the preLoad, because it requires the global Download object to have 48 * been created by the page. 49 * @override 50 */ 51 setUp: function() { 52 // The entries will begin at 1:00 AM on Sept 2, 2008, and will be spaced 53 // two minutes apart. 54 var timestamp = new Date(2008, 9, 2, 1, 0).getTime(); 55 for (var i = 0; i < TOTAL_RESULT_COUNT; ++i) { 56 downloads.updated(this.createDownload_(i, timestamp)); 57 timestamp += 2 * 60 * 1000; // Next visit is two minutes later. 58 } 59 expectEquals(downloads.size(), TOTAL_RESULT_COUNT); 60 }, 61 62 /** 63 * Creates a download object to be passed to the page, following the expected 64 * backend format (see downloads_dom_handler.cc). 65 * @param {number} A unique ID for the download. 66 * @param {number} The time the download purportedly started. 67 * @return {!Object} A fake download object. 68 * @private 69 */ 70 createDownload_: function(id, timestamp) { 71 return { 72 id: id, 73 started: timestamp, 74 otr: false, 75 state: Download.States.COMPLETE, 76 retry: false, 77 file_path: '/path/to/file', 78 file_url: 'http://google.com/' + timestamp, 79 file_name: 'download_' + timestamp, 80 url: 'http://google.com/' + timestamp, 81 file_externally_removed: false, 82 danger_type: Download.DangerType.NOT_DANGEROUS, 83 last_reason_text: '', 84 since_string: 'today', 85 date_string: 'today', 86 percent: 100, 87 progress_status_text: 'done', 88 received: 128, 89 }; 90 }, 91 92 /** 93 * Asserts the correctness of the state of the UI elements 94 * that delete the download history. 95 * @param {boolean} allowDelete True if download history deletion is 96 * allowed and false otherwise. 97 * @param {boolean} expectControlsHidden True if the controls to delete 98 * download history are expected to be hidden and false otherwise. 99 */ 100 testHelper: function(allowDelete, expectControlsHidden) { 101 var clearAllElements = document.getElementsByClassName('clear-all-link'); 102 var disabledElements = document.getElementsByClassName('disabled-link'); 103 var removeLinkElements = 104 document.getElementsByClassName('control-remove-link'); 105 106 // "Clear all" should be a link only when deletions are allowed. 107 expectEquals(allowDelete ? 1 : 0, clearAllElements.length); 108 109 // There should be no disabled links when deletions are allowed. 110 // On the other hand, when deletions are not allowed, "Clear All" 111 // and all "Remove from list" links should be disabled. 112 expectEquals(allowDelete ? 0 : TOTAL_RESULT_COUNT + 1, 113 disabledElements.length); 114 115 // All "Remove from list" items should be links when deletions are allowed. 116 // On the other hand, when deletions are not allowed, all 117 // "Remove from list" items should be text. 118 expectEquals(allowDelete ? TOTAL_RESULT_COUNT : 0, 119 removeLinkElements.length); 120 121 if (allowDelete) { 122 // "Clear all" should not be hidden. 123 expectFalse(clearAllElements[0].hidden); 124 125 // No "Remove from list" items should be hidden. 126 expectFalse(removeLinkElements[0].hidden); 127 } else { 128 expectEquals(expectControlsHidden, disabledElements[0].hidden); 129 } 130 131 // The model is updated synchronously, even though the actual 132 // back-end removal (tested elsewhere) is asynchronous. 133 clearAll(); 134 expectEquals(allowDelete ? 0 : TOTAL_RESULT_COUNT, downloads.size()); 135 }, 136}; 137 138// Test UI when removing entries is allowed. 139TEST_F('BaseDownloadsWebUITest', 'DeleteAllowed', function() { 140 this.testHelper(true, false); 141 // TODO(pamg): Mock out the back-end calls, so we can also test removing a 142 // single item. 143 testDone(); 144}); 145 146// Test that clicking <a href=#> doesn't actually go to #. 147TEST_F('BaseDownloadsWebUITest', 'PoundLinkClicksDontChangeUrl', function() { 148 assertEquals(this.browsePreload, document.location.href); 149 document.querySelector('.clear-all-link').click(); 150 assertEquals(this.browsePreload, document.location.href); 151 testDone(); 152}); 153 154/** 155 * Fixture for Downloads WebUI testing when deletions are prohibited. 156 * @extends {BaseDownloadsWebUITest} 157 * @constructor 158 */ 159function DownloadsWebUIDeleteProhibitedTest() {} 160 161DownloadsWebUIDeleteProhibitedTest.prototype = { 162 __proto__: BaseDownloadsWebUITest.prototype, 163 164 /** @override */ 165 testGenPreamble: function() { 166 GEN(' SetDeleteAllowed(false);'); 167 }, 168}; 169 170// Test UI when removing entries is prohibited. 171TEST_F('DownloadsWebUIDeleteProhibitedTest', 'DeleteProhibited', function() { 172 this.testHelper(false, false); 173 // TODO(pamg): Mock out the back-end calls, so we can also test removing a 174 // single item. 175 testDone(); 176}); 177 178/** 179 * Fixture for Downloads WebUI testing for a supervised user. 180 * @extends {BaseDownloadsWebUITest} 181 * @constructor 182 */ 183function DownloadsWebUIForSupervisedUsersTest() {} 184 185DownloadsWebUIForSupervisedUsersTest.prototype = { 186 __proto__: BaseDownloadsWebUITest.prototype, 187 188 /** @override */ 189 typedefCppFixture: 'DownloadsWebUIForSupervisedUsersTest', 190}; 191 192// Test UI for supervised users, removing entries should be disabled 193// and removal controls should be hidden. 194TEST_F('DownloadsWebUIForSupervisedUsersTest', 'SupervisedUsers', function() { 195 this.testHelper(false, true); 196 testDone(); 197}); 198