Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / downloads_ui_browsertest_base.js
1 // Copyright 2014 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 /** @const */ var TOTAL_RESULT_COUNT = 25;
6
7 /**
8  * Test C++ fixture for downloads WebUI testing.
9  * @constructor
10  * @extends {testing.Test}
11  */
12 function DownloadsUIBrowserTest() {}
13
14 /**
15  * Base fixture for Downloads WebUI testing.
16  * @extends {testing.Test}
17  * @constructor
18  */
19 function BaseDownloadsWebUITest() {}
20
21 BaseDownloadsWebUITest.prototype = {
22   __proto__: testing.Test.prototype,
23
24   /**
25    * Browse to the downloads page & call our preLoad().
26    */
27   browsePreload: 'chrome://downloads/',
28
29   /** @override */
30   typedefCppFixture: 'DownloadsUIBrowserTest',
31
32   /** @override */
33   testGenPreamble: function() {
34     GEN('  SetDeleteAllowed(true);');
35   },
36
37   /** @override */
38   runAccessibilityChecks: true,
39
40   /** @override */
41   accessibilityIssuesAreErrors: true,
42
43   /**
44    * Sends TOTAL_RESULT_COUNT fake downloads to the page. This can't be called
45    * in the preLoad, because it requires the global Download object to have
46    * been created by the page.
47    * @override
48    */
49   setUp: function() {
50     // The entries will begin at 1:00 AM on Sept 2, 2008, and will be spaced
51     // two minutes apart.
52     var timestamp = new Date(2008, 9, 2, 1, 0).getTime();
53     for (var i = 0; i < TOTAL_RESULT_COUNT; ++i) {
54       downloads.updated(this.createDownload_(i, timestamp));
55       timestamp += 2 * 60 * 1000;  // Next visit is two minutes later.
56     }
57     expectEquals(downloads.size(), TOTAL_RESULT_COUNT);
58   },
59
60   /**
61    * Creates a download object to be passed to the page, following the expected
62    * backend format (see downloads_dom_handler.cc).
63    * @param {number} A unique ID for the download.
64    * @param {number} The time the download purportedly started.
65    * @return {!Object} A fake download object.
66    * @private
67    */
68   createDownload_: function(id, timestamp) {
69     return {
70       id: id,
71       started: timestamp,
72       otr: false,
73       state: Download.States.COMPLETE,
74       retry: false,
75       file_path: '/path/to/file',
76       file_url: 'http://google.com/' + timestamp,
77       file_name: 'download_' + timestamp,
78       url: 'http://google.com/' + timestamp,
79       file_externally_removed: false,
80       danger_type: Download.DangerType.NOT_DANGEROUS,
81       last_reason_text: '',
82       since_string: 'today',
83       date_string: 'today',
84       percent: 100,
85       progress_status_text: 'done',
86       received: 128,
87     };
88   },
89
90   /**
91    * Asserts the correctness of the state of the UI elements
92    * that delete the download history.
93    * @param {boolean} allowDelete True if download history deletion is
94    *     allowed and false otherwise.
95    * @param {boolean} expectControlsHidden True if the controls to delete
96    *     download history are expected to be hidden and false otherwise.
97    */
98   testHelper: function(allowDelete, expectControlsHidden) {
99     var clearAllElements = document.getElementsByClassName('clear-all-link');
100     var disabledElements = document.getElementsByClassName('disabled-link');
101     var removeLinkElements =
102         document.getElementsByClassName('control-remove-link');
103
104     // "Clear all" should be a link only when deletions are allowed.
105     expectEquals(allowDelete ? 1 : 0, clearAllElements.length);
106
107     // There should be no disabled links when deletions are allowed.
108     // On the other hand, when deletions are not allowed, "Clear All"
109     // and all "Remove from list" links should be disabled.
110     expectEquals(allowDelete ? 0 : TOTAL_RESULT_COUNT + 1,
111         disabledElements.length);
112
113     // All "Remove from list" items should be links when deletions are allowed.
114     // On the other hand, when deletions are not allowed, all
115     // "Remove from list" items should be text.
116     expectEquals(allowDelete ? TOTAL_RESULT_COUNT : 0,
117         removeLinkElements.length);
118
119     if (allowDelete) {
120       // "Clear all" should not be hidden.
121       expectFalse(clearAllElements[0].hidden);
122
123       // No "Remove from list" items should be hidden.
124       expectFalse(removeLinkElements[0].hidden);
125     } else {
126       expectEquals(expectControlsHidden, disabledElements[0].hidden);
127     }
128
129     // The model is updated synchronously, even though the actual
130     // back-end removal (tested elsewhere) is asynchronous.
131     clearAll();
132     expectEquals(allowDelete ? 0 : TOTAL_RESULT_COUNT, downloads.size());
133   },
134 };