summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/webui/downloads_ui_browsertest_base.js
blob: 4af9e7e6a568e9b6e1105efdfeca88a1c9743f3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/** @const */ var TOTAL_RESULT_COUNT = 25;

/**
 * Test C++ fixture for downloads WebUI testing.
 * @constructor
 * @extends {testing.Test}
 */
function DownloadsUIBrowserTest() {}

/**
 * Base fixture for Downloads WebUI testing.
 * @extends {testing.Test}
 * @constructor
 */
function BaseDownloadsWebUITest() {}

BaseDownloadsWebUITest.prototype = {
  __proto__: testing.Test.prototype,

  /**
   * Browse to the downloads page & call our preLoad().
   */
  browsePreload: 'chrome://downloads/',

  /** @override */
  typedefCppFixture: 'DownloadsUIBrowserTest',

  /** @override */
  testGenPreamble: function() {
    GEN('  SetDeleteAllowed(true);');
  },

  /** @override */
  runAccessibilityChecks: true,

  /** @override */
  accessibilityIssuesAreErrors: true,

  /**
   * Sends TOTAL_RESULT_COUNT fake downloads to the page. This can't be called
   * in the preLoad, because it requires the global Download object to have
   * been created by the page.
   * @override
   */
  setUp: function() {
    this.createdDownloads = [];

    // The entries will begin at 1:00 AM on Sept 2, 2008, and will be spaced
    // two minutes apart.
    var timestamp = new Date(2008, 9, 2, 1, 0).getTime();
    for (var i = 0; i < TOTAL_RESULT_COUNT; ++i) {
      this.createDownload(i, timestamp);
      timestamp += 2 * 60 * 1000;  // Next visit is two minutes later.
    }
    downloads.Manager.updateAll(this.createdDownloads);
    expectEquals(downloads.Manager.size(), TOTAL_RESULT_COUNT);
  },

  /**
   * Creates a download object to be passed to the page, following the expected
   * backend format (see downloads_dom_handler.cc).
   * @param {number} id A unique ID for the download.
   * @param {number} timestamp The time the download purportedly started.
   * @return {!Object} A fake download object.
   */
  createDownload: function(id, timestamp) {
    this.createdDownloads.unshift({
      id: id,
      started: timestamp,
      otr: false,
      state: downloads.States.COMPLETE,
      retry: false,
      file_path: '/path/to/file',
      file_url: 'http://google.com/' + timestamp,
      file_name: 'download_' + timestamp,
      url: 'http://google.com/' + timestamp,
      file_externally_removed: false,
      danger_type: downloads.DangerType.NOT_DANGEROUS,
      last_reason_text: '',
      since_string: 'today',
      date_string: 'today',
      percent: 100,
      progress_status_text: 'done',
      received: 128,
    });
    return this.createdDownloads[0];
  },

  /**
   * Creates a dangerous download object. See downloads_dom_handler.cc.
   * @param {number} id The ID of the download.
   * @param {number} timestamp The time this download started.
   * @return {!Object} A fake, dangerous download object.
   */
  createDangerousDownload: function(id, timestamp) {
    this.createdDownloads.unshift({
      id: id,
      started: timestamp,
      otr: false,
      state: downloads.States.DANGEROUS,
      retry: false,
      file_path: '/oh/noes.jpg.exe',
      file_url: 'http://evil.com/cute/kittens' + timestamp,
      file_name: 'evil.' + timestamp + '.jar',
      file_url: 'http://evil.com/cute/kittens' + timestamp,
      file_externally_removed: false,
      danger_type: downloads.DangerType.DANGEROUS_FILE,
      last_reason_text: '',
      since_string: 'today',
      date_string: 'today',
      percent: 0,
      progress_status_text: '',
      received: 128,
    });
    return this.createdDownloads[0];
  },

  /**
   * Simulates getting no results from C++.
   */
  sendEmptyList: function() {
    downloads.Manager.updateAll([]);
    assertEquals(0, downloads.Manager.size());
  },

  /**
   * Check that |element| is showing and contains |text|.
   * @param {Element} element
   * @param {string} text
   */
  checkShowing: function(element, text) {
    expectFalse(element.hidden);
    expectNotEquals(-1, element.textContent.indexOf(text));
  },

  /**
   * Asserts the correctness of the state of the UI elements that delete the
   * download history.
   * @param {boolean} visible True if download deletion UI should be visible.
   */
  expectDeleteControlsVisible: function(visible) {
    // "Clear all" should only be showing when deletions are allowed.
    expectEquals(!visible, $('clear-all').hidden);

    // "Remove from list" links should only exist when deletions are allowed.
    var query = '#downloads-display .safe .remove';
    if (!visible)
      query += '[hidden]';
    expectEquals(TOTAL_RESULT_COUNT, document.querySelectorAll(query).length);
  },
};