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;
32 BandwidthTask
.prototype = {
33 __proto__
: NetInternalsTest
.Task
.prototype,
36 * Switches to the bandwidth tab, loads a page in the background, and waits
37 * for the arrival of network statistics.
39 * @param {string} url URL to be fetched.
41 start: function(url
) {
42 assertEquals('string', typeof url
);
44 g_browser
.addSessionNetworkStatsObserver(this, true);
45 g_browser
.addHistoricNetworkStatsObserver(this, false);
46 NetInternalsTest
.switchToView('bandwidth');
47 chrome
.send('loadPage', [this.url_
]);
51 * Returns the float value the specified cell of the bandwidth table.
53 getBandwidthTableCell_: function(row
, col
) {
54 return parseFloat(NetInternalsTest
.getTbodyText(
55 BandwidthView
.MAIN_BOX_ID
, row
, col
));
59 * Confirms that the bandwidth usage table displays the expected values.
60 * Does not check exact displayed values, to avoid races, but makes sure
61 * values are high enough after an event of interest.
63 * @param {number} col The column of the table to validate, either 1 or 2.
64 * @param {number} expectedReceived Expected received content length.
65 * @param {number} expectedOriginal Expected original content length.
67 validateBandwidthTableColumn_: function(
68 col
, expectedReceived
, expectedOriginal
) {
69 var row1
= this.getBandwidthTableCell_(0, col
);
70 var row2
= this.getBandwidthTableCell_(1, col
);
71 var row3
= this.getBandwidthTableCell_(2, col
);
72 var row4
= this.getBandwidthTableCell_(3, col
);
74 var expectedReceivedKB
= (expectedReceived
/ 1024).toFixed(1);
75 var expectedOriginalKB
= (expectedOriginal
/ 1024).toFixed(1);
77 expectLE(expectedOriginalKB
, row1
);
78 expectLE(expectedReceivedKB
, row2
);
79 expectFalse(isNaN(row3
));
80 expectFalse(isNaN(row4
));
84 * A task is complete only when session and historic counters have been
85 * verified to reflect the expected number of bytes received.
87 completeIfDone: function() {
88 if (this.historicVerified
&& this.sessionVerified
) {
89 // Check number of rows in the table.
90 NetInternalsTest
.checkTbodyRows(BandwidthView
.MAIN_BOX_ID
, 4);
96 * SessionNetworkStatsObserver function. Sanity checks the received data
97 * and constructed table.
99 * @param {object} networkStats State of the network session.
101 onSessionNetworkStatsChanged: function(networkStats
) {
102 if (this.isDone() || this.sessionVerified
)
104 // Wait until the received content length is at least the size of
105 // our test page and favicon.
106 var expectedLength
= this.expectedLength_
+ this.faviconLength_
;
107 if (networkStats
.session_received_content_length
>= expectedLength
) {
108 expectLE(expectedLength
, networkStats
.session_original_content_length
);
109 // Column 1 contains session information.
110 this.validateBandwidthTableColumn_(1, expectedLength
, expectedLength
);
111 this.sessionVerified
= true;
112 this.completeIfDone();
117 * HistoricNetworkStatsObserver function. Sanity checks the received data
118 * and constructed table.
120 * @param {object} networkStats State of the network session.
122 onHistoricNetworkStatsChanged: function(networkStats
) {
123 if (this.isDone() || this.historicVerified
)
125 // The received content length should be zero since the historic
126 // information only updates every hour.
127 var expectedLength
= 0;
128 // Wait until the table has changed, otherwise the columns will be NaN
129 if (!isNaN(this.getBandwidthTableCell_(0, 2))) {
130 expectLE(expectedLength
, networkStats
.historic_original_content_length
);
131 // Column 2 contains historic information.
132 this.validateBandwidthTableColumn_(2, expectedLength
, expectedLength
);
133 this.historicVerified
= true;
134 this.completeIfDone();
140 * Loads a page and checks bandwidth statistics.
142 TEST_F('NetInternalsTest', 'netInternalsSessionBandwidthSucceed', function() {
143 var taskQueue
= new NetInternalsTest
.TaskQueue(true);
145 new NetInternalsTest
.GetTestServerURLTask('files/title1.html'));
146 // Load a page with a content length of 66 bytes and a 45-byte favicon.
147 taskQueue
.addTask(new BandwidthTask(66, 45));
151 })(); // Anonymous namespace