Home | History | Annotate | Download | only in webui
      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 // Test UI when removing entries is allowed.
     87 TEST_F('BaseDownloadsWebUITest', 'deleteAllowed', function() {
     88   // "Clear all" should be a link.
     89   var clearAllHolder = document.querySelectorAll('#clear-all-holder > a');
     90   expectEquals(clearAllHolder.length, 1);
     91 
     92   // All the "Remove from list" items should be links.
     93   var removeLinks = document.querySelectorAll(
     94       '.controls > a.control-remove-link');
     95   expectEquals(removeLinks.length, TOTAL_RESULT_COUNT);
     96 
     97   // There should be no disabled text "links".
     98   var disabledLinks = document.querySelectorAll('.disabled-link');
     99   expectEquals(disabledLinks.length, 0);
    100 
    101   // The model is updated synchronously, even though the actual back-end removal
    102   // (tested elsewhere) is asynchronous.
    103   clearAll();
    104   expectEquals(downloads.size(), 0);
    105 
    106   // TODO(pamg): Mock out the back-end calls, so we can also test removing a
    107   // single item.
    108   testDone();
    109 });
    110 
    111 /**
    112  * Fixture for Downloads WebUI testing when deletions are prohibited.
    113  * @extends {BaseDownloadsWebUITest}
    114  * @constructor
    115  */
    116 function DownloadsWebUIDeleteProhibitedTest() {}
    117 
    118 DownloadsWebUIDeleteProhibitedTest.prototype = {
    119   __proto__: BaseDownloadsWebUITest.prototype,
    120 
    121   /** @override */
    122   testGenPreamble: function() {
    123     GEN('  SetDeleteAllowed(false);');
    124   },
    125 };
    126 
    127 // Test UI when removing entries is prohibited.
    128 TEST_F('DownloadsWebUIDeleteProhibitedTest', 'deleteProhibited', function() {
    129   // "Clear all" should not be a link.
    130   var clearAllText = document.querySelectorAll(
    131       '#clear-all-holder.disabled-link');
    132   expectEquals(clearAllText.length, 1);
    133   expectEquals(clearAllText[0].nodeName, 'SPAN');
    134 
    135   // There should be no "Clear all" link.
    136   var clearAllLinks = document.querySelectorAll('clear-all-link');
    137   expectEquals(clearAllLinks.length, 0);
    138 
    139   // All the "Remove from list" items should be text. Check only one, to avoid
    140   // spam in case of failure.
    141   var removeTexts = document.querySelectorAll('.controls > .disabled-link');
    142   expectEquals(removeTexts.length, TOTAL_RESULT_COUNT);
    143   expectEquals(removeTexts[0].nodeName, 'SPAN');
    144 
    145   // There should be no "Remove from list" links.
    146   var removeLinks = document.querySelectorAll('control-remove-link');
    147   expectEquals(removeLinks.length, 0);
    148 
    149   // Attempting to remove items anyway should fail.
    150   // The model would have been cleared synchronously, even though the actual
    151   // back-end removal (also disabled, but tested elsewhere) is asynchronous.
    152   clearAll();
    153   expectEquals(downloads.size(), TOTAL_RESULT_COUNT);
    154 
    155   // TODO(pamg): Mock out the back-end calls, so we can also test removing a
    156   // single item.
    157   testDone();
    158 });
    159