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 // Include test fixture.
6 GEN_INCLUDE(['net_internals_test.js']);
11 // Path to the page containing iframe. Iframe is used to load sdch-related
12 // content from the different origin. Otherwise favicon requests for the main
13 // page domain would spoil SDCH blacklists counters making test behavior hardly
15 var BASE_PATH = 'files/sdch/base-page.html?iframe_url=';
18 * Checks the display on the SDCH tab against the information it should be
20 * @param {object} sdchInfo Results from a sdch manager info query.
22 function checkDisplay(sdchInfo) {
23 expectEquals(sdchInfo.sdch_enabled,
24 $(SdchView.SDCH_ENABLED_SPAN_ID).innerText === 'true');
25 NetInternalsTest.checkTbodyRows(SdchView.BLACKLIST_TBODY_ID,
26 sdchInfo.blacklisted.length);
27 NetInternalsTest.checkTbodyRows(SdchView.DICTIONARIES_TBODY_ID,
28 sdchInfo.dictionaries.length);
30 // Rather than check the exact string in every position, just make sure every
31 // entry does not have 'undefined' anywhere and certain entries are not empty,
32 // which should find a fair number of potential output errors.
33 for (var row = 0; row < sdchInfo.blacklisted.length; ++row) {
34 for (var column = 0; column < 3; ++column) {
35 var text = NetInternalsTest.getTbodyText(
36 SdchView.BLACKLIST_TBODY_ID, row, column);
37 expectNotEquals(text, '');
38 expectFalse(/undefined/i.test(text));
43 for (var row = 0; row < sdchInfo.dictionaries.length; ++row) {
44 for (var column = 0; column < 6; ++column) {
45 var text = NetInternalsTest.getTbodyText(
46 SdchView.DICTIONARIES_TBODY_ID, row, column);
47 expectFalse(/undefined/i.test(text));
49 // At least Domain cell should not be empty.
50 expectNotEquals(text, '');
57 * A Task that loads provided page and waits for the SDCH dictionary to be
58 * downloaded. The page response headers should provide Get-Dictionary header.
59 * @extends {NetInternalsTest.Task}
61 function LoadSdchDictionaryTask() {
62 NetInternalsTest.Task.call(this);
65 LoadSdchDictionaryTask.prototype = {
66 __proto__: NetInternalsTest.Task.prototype,
69 * Navigates to the page and starts waiting to receive the results from
70 * the browser process.
72 start: function(url) {
73 g_browser.addSdchInfoObserver(this, false)
74 NetInternalsTest.switchToView('sdch');
75 // 127.0.0.1 is not allowed to be an SDCH domain, use test domain.
76 url = url.replace('127.0.0.1', 'testdomain.com');
78 chrome.send('loadPage', [url]);
82 * Callback from the BrowserBridge. Checks if |sdchInfo| has the SDCH
83 * dictionary info for the dictionary the page has advertised. If so,
84 * validates it and completes the task. If not, continues running.
85 * @param {object} sdchInfo Results of a SDCH manager info query.
87 onSdchInfoChanged: function(sdchInfo) {
91 checkDisplay(sdchInfo);
93 if (sdchInfo.dictionaries.length > 0) {
94 var testDict = sdchInfo.dictionaries.filter(function(dictionary) {
95 return dictionary.domain === 'sub.testdomain.com';
97 if (testDict.length === 0)
100 expectEquals(1, testDict.length);
101 var dict = testDict[0];
102 expectEquals('/', dict.path);
103 expectTrue(dict.url.indexOf('/files/sdch/dict') !== -1);
105 var tableId = SdchView.DICTIONARIES_TBODY_ID;
106 var domain = NetInternalsTest.getTbodyText(tableId, 0, 0);
107 var path = NetInternalsTest.getTbodyText(tableId, 0, 1);
108 var url = NetInternalsTest.getTbodyText(tableId, 0, 5);
110 expectEquals(dict.domain, domain);
111 expectEquals(dict.path, path);
112 expectEquals(dict.url, url);
114 this.onTaskDone(this.url_);
120 * A Task that loads provided page and waits for its domain to appear in SDCH
121 * blacklist with the specified reason.
122 * @param {string} reason Blacklist reason we're waiting for.
123 * @extends {NetInternalsTest.Task}
125 function LoadPageWithDecodeErrorTask(reason) {
126 NetInternalsTest.Task.call(this);
127 this.reason_ = reason;
130 LoadPageWithDecodeErrorTask.prototype = {
131 __proto__: NetInternalsTest.Task.prototype,
134 * Navigates to the page and starts waiting to receive the results from
135 * the browser process.
137 start: function(url) {
138 g_browser.addSdchInfoObserver(this, false)
139 NetInternalsTest.switchToView('sdch');
140 // 127.0.0.1 is not allowed to be an SDCH domain, so we need another one.
141 url = url.replace('127.0.0.1', 'testdomain.com');
142 chrome.send('loadPage', [url]);
146 * Callback from the BrowserBridge. Checks if |sdchInfo.blacklisted| contains
147 * the test domain with the reason specified on creation. If so, validates it
148 * and completes the task. If not, continues running.
149 * @param {object} sdchInfo Results of SDCH manager info query.
151 onSdchInfoChanged: function(sdchInfo) {
155 checkDisplay(sdchInfo);
157 if (sdchInfo.blacklisted.length > 0) {
158 var testDomains = sdchInfo.blacklisted.filter(function(entry) {
159 return entry.domain === 'sub.testdomain.com';
161 if (testDomains.length === 0)
164 expectEquals(1, testDomains.length);
165 var entry = testDomains[0];
166 expectEquals(this.reason_, sdchProblemCodeToString(entry.reason));
167 var tableId = SdchView.BLACKLIST_TBODY_ID;
168 var domain = NetInternalsTest.getTbodyText(tableId, 0, 0);
169 var reason = NetInternalsTest.getTbodyText(tableId, 0, 1);
170 expectEquals(entry.domain, domain);
171 expectEquals(this.reason_, reason);
178 * Load a page, which results in downloading a SDCH dictionary. Make sure its
181 TEST_F('NetInternalsTest', 'netInternalsSdchViewFetchDictionary', function() {
182 var taskQueue = new NetInternalsTest.TaskQueue(true);
184 new NetInternalsTest.GetTestServerURLTask(
185 BASE_PATH + encodeURI('/files/sdch/page.html')));
186 taskQueue.addTask(new LoadSdchDictionaryTask());
191 * Load a page, get the dictionary for it, and get decoding error to see
192 * the blacklist in action.
194 TEST_F('NetInternalsTest', 'netInternalsSdchViewBlacklistMeta', function() {
195 var taskQueue = new NetInternalsTest.TaskQueue(true);
197 new NetInternalsTest.GetTestServerURLTask(
198 BASE_PATH + encodeURI('/files/sdch/page.html')));
199 taskQueue.addTask(new LoadSdchDictionaryTask());
201 new NetInternalsTest.GetTestServerURLTask(
202 BASE_PATH + encodeURI('/files/sdch/non-html')));
204 new LoadPageWithDecodeErrorTask('META_REFRESH_UNSUPPORTED'));
209 * Load a page, which is said to be SDCH-encoded, though we don't expect it.
211 TEST_F('NetInternalsTest', 'netInternalsSdchViewBlacklistNonSdch', function() {
212 var taskQueue = new NetInternalsTest.TaskQueue(true);
214 new NetInternalsTest.GetTestServerURLTask(
215 BASE_PATH + encodeURI('/files/sdch/non-sdch.html')));
217 new LoadPageWithDecodeErrorTask('PASSING_THROUGH_NON_SDCH'));
221 })(); // Anonymous namespace