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  /**
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.
43   * @override
44   */
45  setUp: function() {
46    // The entries will begin at 1:00 AM on Sept 2, 2008, and will be spaced
47    // two minutes apart.
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.
52    }
53    expectEquals(downloads.size(), TOTAL_RESULT_COUNT);
54  },
55
56  /**
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.
61   * @return {!Object} A fake download object.
62   * @private
63   */
64  createDownload_: function(id, timestamp) {
65    return {
66      id: id,
67      started: timestamp,
68      otr: false,
69      state: Download.States.COMPLETE,
70      retry: false,
71      file_path: '/path/to/file',
72      file_url: 'http://google.com/' + timestamp,
73      file_name: 'download_' + timestamp,
74      url: 'http://google.com/' + timestamp,
75      file_externally_removed: false,
76      danger_type: Download.DangerType.NOT_DANGEROUS,
77      last_reason_text: '',
78      since_string: 'today',
79      date_string: 'today',
80      percent: 100,
81      progress_status_text: 'done',
82      received: 128,
83    };
84  },
85
86  /**
87   * Asserts the correctness of the state of the UI elements
88   * that delete the download history.
89   * @param {boolean} allowDelete True if download history deletion is
90   *     allowed and false otherwise.
91   * @param {boolean} expectControlsHidden True if the controls to delete
92   *     download history are expected to be hidden and false otherwise.
93   */
94  testHelper: function(allowDelete, expectControlsHidden) {
95    var clearAllElements = document.getElementsByClassName('clear-all-link');
96    var disabledElements = document.getElementsByClassName('disabled-link');
97    var removeLinkElements =
98        document.getElementsByClassName('control-remove-link');
99
100    // "Clear all" should be a link only when deletions are allowed.
101    expectEquals(allowDelete ? 1 : 0, clearAllElements.length);
102
103    // There should be no disabled links when deletions are allowed.
104    // On the other hand, when deletions are not allowed, "Clear All"
105    // and all "Remove from list" links should be disabled.
106    expectEquals(allowDelete ? 0 : TOTAL_RESULT_COUNT + 1,
107        disabledElements.length);
108
109    // All "Remove from list" items should be links when deletions are allowed.
110    // On the other hand, when deletions are not allowed, all
111    // "Remove from list" items should be text.
112    expectEquals(allowDelete ? TOTAL_RESULT_COUNT : 0,
113        removeLinkElements.length);
114
115    if (allowDelete) {
116      // "Clear all" should not be hidden.
117      expectFalse(clearAllElements[0].hidden);
118
119      // No "Remove from list" items should be hidden.
120      expectFalse(removeLinkElements[0].hidden);
121    } else {
122      expectEquals(expectControlsHidden, disabledElements[0].hidden);
123    }
124
125    // The model is updated synchronously, even though the actual
126    // back-end removal (tested elsewhere) is asynchronous.
127    clearAll();
128    expectEquals(allowDelete ? 0 : TOTAL_RESULT_COUNT, downloads.size());
129  },
130};
131
132// Test UI when removing entries is allowed.
133TEST_F('BaseDownloadsWebUITest', 'DeleteAllowed', function() {
134  this.testHelper(true, false);
135  // TODO(pamg): Mock out the back-end calls, so we can also test removing a
136  // single item.
137  testDone();
138});
139
140/**
141 * Fixture for Downloads WebUI testing when deletions are prohibited.
142 * @extends {BaseDownloadsWebUITest}
143 * @constructor
144 */
145function DownloadsWebUIDeleteProhibitedTest() {}
146
147DownloadsWebUIDeleteProhibitedTest.prototype = {
148  __proto__: BaseDownloadsWebUITest.prototype,
149
150  /** @override */
151  testGenPreamble: function() {
152    GEN('  SetDeleteAllowed(false);');
153  },
154};
155
156// Test UI when removing entries is prohibited.
157TEST_F('DownloadsWebUIDeleteProhibitedTest', 'DeleteProhibited', function() {
158  this.testHelper(false, false);
159  // TODO(pamg): Mock out the back-end calls, so we can also test removing a
160  // single item.
161  testDone();
162});
163
164/**
165 * Fixture for Downloads WebUI testing for a supervised user.
166 * @extends {BaseDownloadsWebUITest}
167 * @constructor
168 */
169function DownloadsWebUIForSupervisedUsersTest() {}
170
171DownloadsWebUIForSupervisedUsersTest.prototype = {
172  __proto__: BaseDownloadsWebUITest.prototype,
173
174  /** @override */
175  typedefCppFixture: 'DownloadsWebUIForSupervisedUsersTest',
176};
177
178// Test UI for supervised users, removing entries should be disabled
179// and removal controls should be hidden.
180TEST_F('DownloadsWebUIForSupervisedUsersTest', 'SupervisedUsers', function() {
181  this.testHelper(false, true);
182  testDone();
183});
184