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.
5 GEN('#include "chrome/browser/ui/webui/downloads_ui_browsertest.h"');
7 /** @const */ var TOTAL_RESULT_COUNT = 25;
10 * Test C++ fixture for downloads WebUI testing.
12 * @extends {testing.Test}
14 function DownloadsUIBrowserTest() {}
17 * Base fixture for Downloads WebUI testing.
18 * @extends {testing.Test}
21 function BaseDownloadsWebUITest() {}
23 BaseDownloadsWebUITest.prototype = {
24 __proto__: testing.Test.prototype,
27 * Browse to the downloads page & call our preLoad().
29 browsePreload: 'chrome://downloads/',
32 typedefCppFixture: 'DownloadsUIBrowserTest',
35 testGenPreamble: function() {
36 GEN(' SetDeleteAllowed(true);');
40 runAccessibilityChecks: true,
43 accessibilityIssuesAreErrors: true,
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.
52 // The entries will begin at 1:00 AM on Sept 2, 2008, and will be spaced
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.
59 expectEquals(downloads.size(), TOTAL_RESULT_COUNT);
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.
70 createDownload_: function(id, timestamp) {
75 state: Download.States.COMPLETE,
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,
84 since_string: 'today',
87 progress_status_text: 'done',
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.
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');
106 // "Clear all" should be a link only when deletions are allowed.
107 expectEquals(allowDelete ? 1 : 0, clearAllElements.length);
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);
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);
122 // "Clear all" should not be hidden.
123 expectFalse(clearAllElements[0].hidden);
125 // No "Remove from list" items should be hidden.
126 expectFalse(removeLinkElements[0].hidden);
128 expectEquals(expectControlsHidden, disabledElements[0].hidden);
131 // The model is updated synchronously, even though the actual
132 // back-end removal (tested elsewhere) is asynchronous.
134 expectEquals(allowDelete ? 0 : TOTAL_RESULT_COUNT, downloads.size());
138 // Test UI when removing entries is allowed.
139 TEST_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
146 // Test that clicking <a href=#> doesn't actually go to #.
147 TEST_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);
155 * Fixture for Downloads WebUI testing when deletions are prohibited.
156 * @extends {BaseDownloadsWebUITest}
159 function DownloadsWebUIDeleteProhibitedTest() {}
161 DownloadsWebUIDeleteProhibitedTest.prototype = {
162 __proto__: BaseDownloadsWebUITest.prototype,
165 testGenPreamble: function() {
166 GEN(' SetDeleteAllowed(false);');
170 // Test UI when removing entries is prohibited.
171 TEST_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
179 * Fixture for Downloads WebUI testing for a supervised user.
180 * @extends {BaseDownloadsWebUITest}
183 function DownloadsWebUIForSupervisedUsersTest() {}
185 DownloadsWebUIForSupervisedUsersTest.prototype = {
186 __proto__: BaseDownloadsWebUITest.prototype,
189 typedefCppFixture: 'DownloadsWebUIForSupervisedUsersTest',
192 // Test UI for supervised users, removing entries should be disabled
193 // and removal controls should be hidden.
194 TEST_F('DownloadsWebUIForSupervisedUsersTest', 'SupervisedUsers', function() {
195 this.testHelper(false, true);