1 // Copyright 2014 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 var googleResponseReceived = false;
6 var googleRequestSent = false;
7 var nonGoogleResponseReceived = false;
8 var nonGoogleRequestSent = false;
10 function initGlobals() {
11 googleResponseReceived = false;
12 googleRequestSent = false;
13 nonGoogleResponseReceived = false;
14 nonGoogleRequestSent = false;
17 // Starts XHR requests - one for google.com and one kater for non-google.
18 function startXHRRequests(googlePageUrl, googlePageCheck,
19 nonGooglePageUrl, nonGooglePageCheck) {
20 // Kick off google XHR first.
21 var xhr = new XMLHttpRequest();
22 xhr.onreadystatechange = function() {
23 console.warn("xhr.onreadystatechange: " + xhr.readyState);
24 if (xhr.readyState == 1) {
25 startNonGoogleXHRRequests(nonGooglePageUrl, nonGooglePageCheck);
26 } else if (xhr.readyState == 4) { // done
27 if (xhr.status == 200 &&
28 xhr.responseText.indexOf('Hello Google') != -1) {
29 googleResponseReceived = true;
34 xhr.open("GET", googlePageUrl, true);
36 googleRequestSent = true;
40 function startNonGoogleXHRRequests(nonGooglePageUrl, nonGooglePageCheck) {
41 // Kick off non-google XHR next.
42 var xhr2 = new XMLHttpRequest();
43 xhr2.onreadystatechange = function() {
44 console.warn("xhr2.onreadystatechange: " + xhr2.readyState);
45 if (xhr2.readyState == 4) { // done
46 if (xhr2.status == 200 &&
47 xhr2.responseText.indexOf('SomethingElse') != -1) {
48 // Google response should not have been received before non-google
49 // XHR since it must be blocked by the throttle side right now.
50 chrome.test.sendMessage('non-google-xhr-received');
51 nonGoogleResponseReceived = true;
56 xhr2.open("GET", nonGooglePageUrl, true);
58 nonGoogleRequestSent = true;
62 function googlePageThrottleCheck() {
63 // Google response should not have been received before non-google
64 // XHR since it must be blocked by the throttle side right now.
65 if (nonGoogleResponseReceived)
66 chrome.test.succeed();
71 function nonGooglePageThrottleCheck() {
73 // Performs test that will verify if XHR request had completed prematurely.
74 function startThrottledTests(googlePageUrl,nonGooglePageUrl) {
75 chrome.test.runTests([function testXHRThrottle() {
77 startXHRRequests(googlePageUrl, googlePageThrottleCheck,
78 nonGooglePageUrl, nonGooglePageThrottleCheck);
83 function googlePageNoThrottleCheck() {
84 console.warn("googlePageNoThrottleCheck: " + googleResponseReceived);
87 function nonGooglePageNoThrottleCheck() {
88 // Non-google response should be received only after google request
89 // since there is no throttle now.
90 if (googleResponseReceived) {
91 chrome.test.succeed();
93 console.warn("nonGooglePageNoThrottleCheck: " + googleResponseReceived);
98 // Performs test that will verify that XHR will complete in order they were
99 // issued since there is no throttle to change that since this test is running
100 // after merge session is completed.
101 function startNonThrottledTests(googlePageUrl, nonGooglePageUrl) {
102 chrome.test.runTests([function testWithNoXHRThrottle() {
104 startXHRRequests(googlePageUrl, googlePageNoThrottleCheck,
105 nonGooglePageUrl, nonGooglePageNoThrottleCheck);