- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / downloads_ui_browsertest.js
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 GEN('#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  */
14 function DownloadsUIBrowserTest() {}
15
16 /**
17  * Base fixture for Downloads WebUI testing.
18  * @extends {testing.Test}
19  * @constructor
20  */
21 function BaseDownloadsWebUITest() {}
22
23 BaseDownloadsWebUITest.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    */
62   createDownload_: function(id, timestamp) {
63     var download = {};
64     download.id = id;
65     download.started = timestamp;
66     download.otr = false;
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;
81
82     return download;
83   },
84
85   /**
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.
92    */
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');
98
99     // "Clear all" should be a link only when deletions are allowed.
100     expectEquals(allowDelete ? 1 : 0, clearAllElements.length);
101
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);
107
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);
113
114     if (allowDelete) {
115       // "Clear all" should not be hidden.
116       expectFalse(clearAllElements[0].hidden);
117
118       // No "Remove from list" items should be hidden.
119       expectFalse(removeLinkElements[0].hidden);
120     } else {
121       expectEquals(expectControlsHidden, disabledElements[0].hidden);
122     }
123
124     // The model is updated synchronously, even though the actual
125     // back-end removal (tested elsewhere) is asynchronous.
126     clearAll();
127     expectEquals(allowDelete ? 0 : TOTAL_RESULT_COUNT, downloads.size());
128   },
129 };
130
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
135   // single item.
136   testDone();
137 });
138
139 /**
140  * Fixture for Downloads WebUI testing when deletions are prohibited.
141  * @extends {BaseDownloadsWebUITest}
142  * @constructor
143  */
144 function DownloadsWebUIDeleteProhibitedTest() {}
145
146 DownloadsWebUIDeleteProhibitedTest.prototype = {
147   __proto__: BaseDownloadsWebUITest.prototype,
148
149   /** @override */
150   testGenPreamble: function() {
151     GEN('  SetDeleteAllowed(false);');
152   },
153 };
154
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
159   // single item.
160   testDone();
161 });
162
163 /**
164  * Fixture for Downloads WebUI testing for a supervised user.
165  * @extends {BaseDownloadsWebUITest}
166  * @constructor
167  */
168 function DownloadsWebUIForSupervisedUsersTest() {}
169
170 DownloadsWebUIForSupervisedUsersTest.prototype = {
171   __proto__: BaseDownloadsWebUITest.prototype,
172
173   /** @override */
174   typedefCppFixture: 'DownloadsWebUIForSupervisedUsersTest',
175 };
176
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);
181   testDone();
182 });