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 * Sends TOTAL_RESULT_COUNT fake downloads to the page. This can't be called
41 * in the preLoad, because it requires the global Download object to have
42 * been created by the page.
46 // The entries will begin at 1:00 AM on Sept 2, 2008, and will be spaced
48 var timestamp = new Date(2008, 9, 2, 1, 0).getTime();
49 for (var i = 0; i < TOTAL_RESULT_COUNT; ++i) {
50 downloads.updated(this.createDownload_(i, timestamp));
51 timestamp += 2 * 60 * 1000; // Next visit is two minutes later.
53 expectEquals(downloads.size(), TOTAL_RESULT_COUNT);
57 * Creates a download object to be passed to the page, following the expected
58 * backend format (see downloads_dom_handler.cc).
59 * @param {Number} A unique ID for the download.
60 * @param {Number} The time the download purportedly started.
62 createDownload_: function(id, timestamp) {
65 download.started = timestamp;
67 download.state = Download.States.COMPLETE;
68 download.retry = false;
69 download.file_path = '/path/to/file';
70 download.file_url = 'http://google.com/' + timestamp;
71 download.file_name = 'download_' + timestamp;
72 download.url = 'http://google.com/' + timestamp;
73 download.file_externally_removed = false;
74 download.danger_type = Download.DangerType.NOT_DANGEROUS;
75 download.last_reason_text = '';
76 download.since_string = 'today';
77 download.date_string = 'today';
78 download.percent = 100;
79 download.progress_status_text = 'done';
80 download.received = 128;
86 * Asserts the correctness of the state of the UI elements
87 * that delete the download history.
88 * @param {boolean} allowDelete True if download history deletion is
89 * allowed and false otherwise.
90 * @param {boolean} expectControlsHidden True if the controls to delete
91 * download history are expected to be hidden and false otherwise.
93 testHelper: function(allowDelete, expectControlsHidden) {
94 var clearAllElements = document.getElementsByClassName('clear-all-link');
95 var disabledElements = document.getElementsByClassName('disabled-link');
96 var removeLinkElements =
97 document.getElementsByClassName('control-remove-link');
99 // "Clear all" should be a link only when deletions are allowed.
100 expectEquals(allowDelete ? 1 : 0, clearAllElements.length);
102 // There should be no disabled links when deletions are allowed.
103 // On the other hand, when deletions are not allowed, "Clear All"
104 // and all "Remove from list" links should be disabled.
105 expectEquals(allowDelete ? 0 : TOTAL_RESULT_COUNT + 1,
106 disabledElements.length);
108 // All "Remove from list" items should be links when deletions are allowed.
109 // On the other hand, when deletions are not allowed, all
110 // "Remove from list" items should be text.
111 expectEquals(allowDelete ? TOTAL_RESULT_COUNT : 0,
112 removeLinkElements.length);
115 // "Clear all" should not be hidden.
116 expectFalse(clearAllElements[0].hidden);
118 // No "Remove from list" items should be hidden.
119 expectFalse(removeLinkElements[0].hidden);
121 expectEquals(expectControlsHidden, disabledElements[0].hidden);
124 // The model is updated synchronously, even though the actual
125 // back-end removal (tested elsewhere) is asynchronous.
127 expectEquals(allowDelete ? 0 : TOTAL_RESULT_COUNT, downloads.size());
131 // Test UI when removing entries is allowed.
132 TEST_F('BaseDownloadsWebUITest', 'DeleteAllowed', function() {
133 this.testHelper(true, false);
134 // TODO(pamg): Mock out the back-end calls, so we can also test removing a
140 * Fixture for Downloads WebUI testing when deletions are prohibited.
141 * @extends {BaseDownloadsWebUITest}
144 function DownloadsWebUIDeleteProhibitedTest() {}
146 DownloadsWebUIDeleteProhibitedTest.prototype = {
147 __proto__: BaseDownloadsWebUITest.prototype,
150 testGenPreamble: function() {
151 GEN(' SetDeleteAllowed(false);');
155 // Test UI when removing entries is prohibited.
156 TEST_F('DownloadsWebUIDeleteProhibitedTest', 'DeleteProhibited', function() {
157 this.testHelper(false, false);
158 // TODO(pamg): Mock out the back-end calls, so we can also test removing a
164 * Fixture for Downloads WebUI testing for a supervised user.
165 * @extends {BaseDownloadsWebUITest}
168 function DownloadsWebUIForSupervisedUsersTest() {}
170 DownloadsWebUIForSupervisedUsersTest.prototype = {
171 __proto__: BaseDownloadsWebUITest.prototype,
174 typedefCppFixture: 'DownloadsWebUIForSupervisedUsersTest',
177 // Test UI for supervised users, removing entries should be disabled
178 // and removal controls should be hidden.
179 TEST_F('DownloadsWebUIForSupervisedUsersTest', 'SupervisedUsers', function() {
180 this.testHelper(false, true);