Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / test / data / webui / net_internals / bandwidth_view.js
blob38ceafbf7edd7dc719db373076f0018f7681ad58
1 // Copyright (c) 2012 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.
5 // Include test fixture.
6 GEN_INCLUDE(['net_internals_test.js']);
8 // Anonymous namespace
9 (function() {
11 /**
12  * The task fetches a page as a way of determining if network statistics
13  * (such as the aggregate received content length) change accordingly.
14  * The URL is received from the previous task.
15  *
16  * Checks that we see all relevant events and update the corresponding table.
17  *
18  * @param {int} expectedLength The length of the content being loaded.
19  * @param {int} faviconLength The length a favicon that is received.
20  * @extends {NetInternalsTest.Task}
21  * @constructor
22  */
23 function BandwidthTask(expectedLength, faviconLength) {
24   NetInternalsTest.Task.call(this);
25   this.url_ = null;
26   this.expectedLength_ = expectedLength;
27   this.faviconLength_ = faviconLength;
28   this.sessionVerified = false;
29   this.historicVerified = false;
32 /**
33  * The task loads data reduction proxy info.
34  *
35  * Checks that we see all relevant events and update the corresponding elements.
36  *
37  * @param {boolean} isEnabled The desired state of the data reduction proxy.
38  * @extends {NetInternalsTest.Task}
39  * @constructor
40  */
41 function DataReductionProxyTask(isEnabled) {
42   NetInternalsTest.Task.call(this);
43   this.enabled_ = isEnabled;
44   this.dataReductionProxyInfoVerified_ = false;
45   this.proxySettingsReceived_ = false;
46   this.badProxyChangesReceived_ = false;
49 BandwidthTask.prototype = {
50   __proto__: NetInternalsTest.Task.prototype,
52   /**
53    * Switches to the bandwidth tab, loads a page in the background, and waits
54    * for the arrival of network statistics.
55    *
56    * @param {string} url URL to be fetched.
57    */
58   start: function(url) {
59     assertEquals('string', typeof url);
60     this.url_ = url;
61     g_browser.addSessionNetworkStatsObserver(this, true);
62     g_browser.addHistoricNetworkStatsObserver(this, true);
63     NetInternalsTest.switchToView('bandwidth');
64     chrome.send('loadPage', [this.url_]);
65   },
67   /**
68    * Returns the float value the specified cell of the bandwidth table.
69    */
70   getBandwidthTableCell_: function(row, col) {
71     return parseFloat(NetInternalsTest.getTbodyText(
72         BandwidthView.STATS_BOX_ID, row, col));
73   },
75   /**
76    * Confirms that the bandwidth usage table displays the expected values.
77    * Does not check exact displayed values, to avoid races, but makes sure
78    * values are high enough after an event of interest.
79    *
80    * @param {number} col The column of the table to validate, either 1 or 2.
81    * @param {number} expectedReceived Expected received content length.
82    * @param {number} expectedOriginal Expected original content length.
83    */
84   validateBandwidthTableColumn_: function(
85       col, expectedReceived, expectedOriginal) {
86     var row1 = this.getBandwidthTableCell_(0, col);
87     var row2 = this.getBandwidthTableCell_(1, col);
88     var row3 = this.getBandwidthTableCell_(2, col);
89     var row4 = this.getBandwidthTableCell_(3, col);
91     var expectedReceivedKB = (expectedReceived / 1024).toFixed(1);
92     var expectedOriginalKB = (expectedOriginal / 1024).toFixed(1);
94     expectLE(expectedOriginalKB, row1);
95     expectLE(expectedReceivedKB, row2);
96     expectFalse(isNaN(row3));
97     expectFalse(isNaN(row4));
98   },
100   /**
101    * A task is complete only when session and historic counters have been
102    * verified to reflect the expected number of bytes received.
103    */
104   completeIfDone: function() {
105     if (this.historicVerified && this.sessionVerified) {
106       // Check number of rows in the table.
107       NetInternalsTest.checkTbodyRows(BandwidthView.STATS_BOX_ID, 4);
108       this.onTaskDone();
109     }
110   },
112   /**
113    * SessionNetworkStatsObserver function.  Sanity checks the received data
114    * and constructed table.
116    * @param {object} networkStats State of the network session.
117    */
118   onSessionNetworkStatsChanged: function(networkStats) {
119     if (this.isDone())
120       return;
121     // Wait until the received content length is at least the size of
122     // our test page and favicon.
123     var expectedLength = this.expectedLength_ + this.faviconLength_;
124     if (networkStats.session_received_content_length >= expectedLength) {
125       expectLE(expectedLength, networkStats.session_original_content_length);
126       // Column 1 contains session information.
127       this.validateBandwidthTableColumn_(1, expectedLength, expectedLength);
128       this.sessionVerified = true;
129       this.completeIfDone();
130     }
131   },
133   /**
134    * HistoricNetworkStatsObserver function.  Sanity checks the received data
135    * and constructed table.
137    * @param {object} networkStats State of the network session.
138    */
139   onHistoricNetworkStatsChanged: function(networkStats) {
140     if (this.isDone())
141       return;
142     // Wait until the received content length is at least the size of
143     // our test page and favicon.
144     var expectedLength = this.expectedLength_ + this.faviconLength_;
145     if (networkStats.historic_received_content_length >= expectedLength) {
146       expectLE(expectedLength, networkStats.historic_original_content_length);
147       // Column 2 contains historic information. The expected length should
148       // only be what has been collected in this session, because previously
149       // there was no history
150       this.validateBandwidthTableColumn_(2, expectedLength, expectedLength);
151       this.historicVerified = true;
152       this.completeIfDone();
153     }
154   }
157 DataReductionProxyTask.prototype = {
158   __proto__: NetInternalsTest.Task.prototype,
160   /**
161    * Switches to the bandwidth tab and waits for arrival of data reduction
162    * proxy information.
163    */
164   start: function() {
165     chrome.send('enableDataReductionProxy', [this.enabled_]);
166     g_browser.addDataReductionProxyInfoObserver(this, true);
167     g_browser.addProxySettingsObserver(this, true);
168     g_browser.addBadProxiesObserver(this, true);
169     NetInternalsTest.switchToView('bandwidth');
170   },
172   /**
173    * A task is complete only when session and historic counters have been
174    * verified to reflect the expected number of bytes received.
175    */
176   completeIfDone: function() {
177     if (this.dataReductionProxyInfoVerified_) {
178       this.onTaskDone();
179     }
180   },
182   /**
183    * ProxySettingsObserver function.
184    *
185    * @param {object} proxySettings Proxy settings.
186    */
187   onProxySettingsChanged: function(proxySettings) {
188     if (this.isDone() || this.proxySettingsReceived_)
189       return;
191     this.proxySettingsReceived_ = true;
192   },
194   /**
195    * BadProxiesObserver function.
196    *
197    * @param {object} badProxies Bad proxies.
198    */
199   onBadProxiesChanged: function(badProxies) {
200     if (this.isDone() || this.badProxyChangesReceived_)
201       return;
203     this.badProxyChangesReceived_ = true;
204   },
206   /**
207    * DataReductionProxyInfoObserver function.  Sanity checks the received data
208    * and constructed table.
210    * @param {object} info State of the data reduction proxy.
211    */
212   onDataReductionProxyInfoChanged: function(info) {
213     if (this.isDone() ||
214         this.dataReductionProxyInfoVerified_ ||
215         !this.proxySettingsReceived_) {
216       return;
217     }
219     if (info) {
220       expectEquals(this.enabled_, info.enabled);
221       if (this.enabled_) {
222         expectEquals("Enabled", $(BandwidthView.ENABLED_ID).innerText);
223         expectNotEquals('', $(BandwidthView.PRIMARY_PROXY_ID).innerText);
224         expectNotEquals('', $(BandwidthView.SECONDARY_PROXY_ID).innerText);
225       } else {
226         expectEquals("Disabled", $(BandwidthView.ENABLED_ID).innerText);
227         expectEquals('', $(BandwidthView.PRIMARY_PROXY_ID).innerText);
228         expectEquals('', $(BandwidthView.SECONDARY_PROXY_ID).innerText);
229         // Each event results in 2 rows, and we get 2 events since the startup
230         // event starts as disabled, and we subsequently manually set it to the
231         // disabled state.
232         expectEquals(
233             4, NetInternalsTest.getTbodyNumRows(BandwidthView.EVENTS_TBODY_ID));
234       }
236       this.dataReductionProxyInfoVerified_ = true;
237       this.completeIfDone();
238     }
239   }
243  * Loads a page and checks bandwidth statistics.
244  */
245 TEST_F('NetInternalsTest', 'netInternalsSessionBandwidthSucceed', function() {
246   var taskQueue = new NetInternalsTest.TaskQueue(true);
247   taskQueue.addTask(
248       new NetInternalsTest.GetTestServerURLTask('files/title1.html'));
249   // Load a page with a content length of 66 bytes and a 45-byte favicon.
250   taskQueue.addTask(new BandwidthTask(66, 45));
251   taskQueue.run();
255  * Checks data reduction proxy info when it is enabled.
256  */
257 TEST_F('NetInternalsTest',
258        'DISABLED_netInternalsDataReductionProxyEnabled',
259        function() {
260   var taskQueue = new NetInternalsTest.TaskQueue(true);
261   taskQueue.addTask(new DataReductionProxyTask(true));
262   taskQueue.run();
266  * Checks data reduction proxy info when it is disabled.
267  */
268 TEST_F('NetInternalsTest',
269        'DISABLED_netInternalsDataReductionProxyDisabled',
270        function() {
271   var taskQueue = new NetInternalsTest.TaskQueue(true);
272   taskQueue.addTask(new DataReductionProxyTask(false));
273   taskQueue.run();
276 })();  // Anonymous namespace