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']);
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.
16 * Checks that we see all relevant events and update the corresponding table.
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}
23 function BandwidthTask(expectedLength, faviconLength) {
24 NetInternalsTest.Task.call(this);
26 this.expectedLength_ = expectedLength;
27 this.faviconLength_ = faviconLength;
28 this.sessionVerified = false;
29 this.historicVerified = false;
33 * The task loads data reduction proxy info.
35 * Checks that we see all relevant events and update the corresponding elements.
37 * @param {boolean} isEnabled The desired state of the data reduction proxy.
38 * @extends {NetInternalsTest.Task}
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,
53 * Switches to the bandwidth tab, loads a page in the background, and waits
54 * for the arrival of network statistics.
56 * @param {string} url URL to be fetched.
58 start: function(url) {
59 assertEquals('string', typeof url);
61 g_browser.addSessionNetworkStatsObserver(this, true);
62 g_browser.addHistoricNetworkStatsObserver(this, true);
63 NetInternalsTest.switchToView('bandwidth');
64 chrome.send('loadPage', [this.url_]);
68 * Returns the float value the specified cell of the bandwidth table.
70 getBandwidthTableCell_: function(row, col) {
71 return parseFloat(NetInternalsTest.getTbodyText(
72 BandwidthView.STATS_BOX_ID, row, col));
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.
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.
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));
101 * A task is complete only when session and historic counters have been
102 * verified to reflect the expected number of bytes received.
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);
113 * SessionNetworkStatsObserver function. Sanity checks the received data
114 * and constructed table.
116 * @param {object} networkStats State of the network session.
118 onSessionNetworkStatsChanged: function(networkStats) {
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();
134 * HistoricNetworkStatsObserver function. Sanity checks the received data
135 * and constructed table.
137 * @param {object} networkStats State of the network session.
139 onHistoricNetworkStatsChanged: function(networkStats) {
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();
157 DataReductionProxyTask.prototype = {
158 __proto__: NetInternalsTest.Task.prototype,
161 * Switches to the bandwidth tab and waits for arrival of data reduction
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');
173 * A task is complete only when session and historic counters have been
174 * verified to reflect the expected number of bytes received.
176 completeIfDone: function() {
177 if (this.dataReductionProxyInfoVerified_) {
183 * ProxySettingsObserver function.
185 * @param {object} proxySettings Proxy settings.
187 onProxySettingsChanged: function(proxySettings) {
188 if (this.isDone() || this.proxySettingsReceived_)
191 this.proxySettingsReceived_ = true;
195 * BadProxiesObserver function.
197 * @param {object} badProxies Bad proxies.
199 onBadProxiesChanged: function(badProxies) {
200 if (this.isDone() || this.badProxyChangesReceived_)
203 this.badProxyChangesReceived_ = true;
207 * DataReductionProxyInfoObserver function. Sanity checks the received data
208 * and constructed table.
210 * @param {object} info State of the data reduction proxy.
212 onDataReductionProxyInfoChanged: function(info) {
214 this.dataReductionProxyInfoVerified_ ||
215 !this.proxySettingsReceived_) {
220 expectEquals(this.enabled_, info.enabled);
222 expectEquals("Enabled", $(BandwidthView.ENABLED_ID).innerText);
223 expectNotEquals('', $(BandwidthView.PRIMARY_PROXY_ID).innerText);
224 expectNotEquals('', $(BandwidthView.SECONDARY_PROXY_ID).innerText);
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
233 4, NetInternalsTest.getTbodyNumRows(BandwidthView.EVENTS_TBODY_ID));
236 this.dataReductionProxyInfoVerified_ = true;
237 this.completeIfDone();
243 * Loads a page and checks bandwidth statistics.
245 TEST_F('NetInternalsTest', 'netInternalsSessionBandwidthSucceed', function() {
246 var taskQueue = new NetInternalsTest.TaskQueue(true);
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));
255 * Checks data reduction proxy info when it is enabled.
257 TEST_F('NetInternalsTest',
258 'DISABLED_netInternalsDataReductionProxyEnabled',
260 var taskQueue = new NetInternalsTest.TaskQueue(true);
261 taskQueue.addTask(new DataReductionProxyTask(true));
266 * Checks data reduction proxy info when it is disabled.
268 TEST_F('NetInternalsTest',
269 'DISABLED_netInternalsDataReductionProxyDisabled',
271 var taskQueue = new NetInternalsTest.TaskQueue(true);
272 taskQueue.addTask(new DataReductionProxyTask(false));
276 })(); // Anonymous namespace