Reland "Non-SFI mode: Switch to newlib. (patchset #4 id:60001 of https://codereview...
[chromium-blink-merge.git] / chrome / test / data / webui / net_internals / dns_view.js
bloba2bab3f52e2e4bcf609a597cedc0650744ff9b7b
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 * Checks the display on the DNS tab against the information it should be
13 * displaying.
14 * @param {object} hostResolverInfo Results from a host resolver info query.
16 function checkDisplay(hostResolverInfo) {
17 expectEquals(hostResolverInfo.cache.capacity,
18 parseInt($(DnsView.CAPACITY_SPAN_ID).innerText));
20 var entries = hostResolverInfo.cache.entries;
22 // Don't check exact displayed values, to avoid any races, but make sure
23 // values are non-negative and have the correct sum.
24 var active = parseInt($(DnsView.ACTIVE_SPAN_ID).innerText);
25 var expired = parseInt($(DnsView.EXPIRED_SPAN_ID).innerText);
26 expectLE(0, active);
27 expectLE(0, expired);
28 expectEquals(entries.length, active + expired);
30 var tableId = DnsView.CACHE_TBODY_ID;
31 NetInternalsTest.checkTbodyRows(tableId, entries.length);
33 // Rather than check the exact string in every position, just make sure every
34 // entry is not empty, and does not have 'undefined' anywhere, which should
35 // find a fair number of potential output errors, without duplicating the
36 // entire corresponding function of DnsView.
37 for (var row = 0; row < entries.length; ++row) {
38 for (column = 0; column < 4; ++column) {
39 var text = NetInternalsTest.getTbodyText(tableId, row, column);
40 expectNotEquals(text, '');
41 expectFalse(/undefined/i.test(text));
46 /**
47 * Finds an entry with the specified host name in the |hostResolverInfo| cache,
48 * and returns its index.
49 * @param {object} hostResolverInfo Results to search.
50 * @param {object} hostname The host name to find.
51 * @return {int} Index of the specified host. -1 if not found.
53 function findEntry(hostResolverInfo, hostname) {
54 var entries = hostResolverInfo.cache.entries;
55 for (var i = 0; i < entries.length; ++i) {
56 if (entries[i].hostname == hostname)
57 return i;
59 return -1;
62 /**
63 * A Task that adds a hostname to the cache and waits for it to appear in the
64 * data we receive from the cache.
65 * @param {string} hostname Name of host address we're waiting for.
66 * @param {string} ipAddress IP address we expect it to have. Null if we expect
67 * a net error other than OK.
68 * @param {int} netError The expected network error code.
69 * @param {bool} expired True if we expect the entry to be expired. The added
70 * entry will have an expiration time far enough away from the current time
71 * that there will be no chance of any races.
72 * @extends {NetInternalsTest.Task}
74 function AddCacheEntryTask(hostname, ipAddress, netError, expired) {
75 this.hostname_ = hostname;
76 this.ipAddress_ = ipAddress;
77 this.netError_ = netError;
78 this.expired_ = expired;
79 NetInternalsTest.Task.call(this);
82 AddCacheEntryTask.prototype = {
83 __proto__: NetInternalsTest.Task.prototype,
85 /**
86 * Adds an entry to the cache and starts waiting to received the results from
87 * the browser process.
89 start: function() {
90 var addCacheEntryParams = [
91 this.hostname_,
92 this.ipAddress_,
93 this.netError_,
94 this.expired_ ? -2 : 2
96 chrome.send('addCacheEntry', addCacheEntryParams);
97 g_browser.addHostResolverInfoObserver(this, false);
101 * Callback from the BrowserBridge. Checks if |hostResolverInfo| has the
102 * DNS entry specified on creation. If so, validates it and completes the
103 * task. If not, continues running.
104 * @param {object} hostResolverInfo Results a host resolver info query.
106 onHostResolverInfoChanged: function(hostResolverInfo) {
107 if (!this.isDone()) {
108 checkDisplay(hostResolverInfo);
110 var index = findEntry(hostResolverInfo, this.hostname_);
111 if (index >= 0) {
112 var entry = hostResolverInfo.cache.entries[index];
113 if (this.netError_) {
114 this.checkError_(entry);
115 } else {
116 this.checkSuccess_(entry);
118 var expirationDate = timeutil.convertTimeTicksToDate(entry.expiration);
119 expectEquals(this.expired_, expirationDate < new Date());
121 // Expect at least one active or expired entry, depending on |expired_|.
122 // To avoid any chance of a race, exact values are not tested.
123 var activeMin = this.expired_ ? 0 : 1;
124 var expiredMin = this.expired_ ? 1 : 0;
125 expectLE(activeMin, parseInt($(DnsView.ACTIVE_SPAN_ID).innerText));
126 expectLE(expiredMin, parseInt($(DnsView.EXPIRED_SPAN_ID).innerText));
128 // Text for the expiration time of the entry should contain 'Expired'
129 // only if |expired_| is true. Only checked for entries we add
130 // ourselves to avoid any expiration time race.
131 var expirationText =
132 NetInternalsTest.getTbodyText(DnsView.CACHE_TBODY_ID, index, 3);
133 expectEquals(this.expired_, /expired/i.test(expirationText));
135 this.onTaskDone();
140 checkError_: function(entry) {
141 expectEquals(this.netError_, entry.error);
144 checkSuccess_: function(entry) {
145 expectEquals(undefined, entry.error);
146 expectEquals(1, entry.addresses.length);
147 expectEquals(0, entry.addresses[0].search(this.ipAddress_));
152 * A Task clears the cache by simulating a button click.
153 * @extends {NetInternalsTest.Task}
155 function ClearCacheTask() {
156 NetInternalsTest.Task.call(this);
159 ClearCacheTask.prototype = {
160 __proto__: NetInternalsTest.Task.prototype,
162 start: function() {
163 $(DnsView.CLEAR_CACHE_BUTTON_ID).onclick();
164 this.onTaskDone();
169 * A Task that waits for the specified hostname entry to disappear from the
170 * cache.
171 * @param {string} hostname Name of host we're waiting to be removed.
172 * @extends {NetInternalsTest.Task}
174 function WaitForEntryDestructionTask(hostname) {
175 this.hostname_ = hostname;
176 NetInternalsTest.Task.call(this);
179 WaitForEntryDestructionTask.prototype = {
180 __proto__: NetInternalsTest.Task.prototype,
183 * Starts waiting to received the results from the browser process.
185 start: function() {
186 g_browser.addHostResolverInfoObserver(this, false);
190 * Callback from the BrowserBridge. Checks if the entry has been removed.
191 * If so, the task completes.
192 * @param {object} hostResolverInfo Results a host resolver info query.
194 onHostResolverInfoChanged: function(hostResolverInfo) {
195 if (!this.isDone()) {
196 checkDisplay(hostResolverInfo);
198 var entry = findEntry(hostResolverInfo, this.hostname_);
199 if (entry == -1)
200 this.onTaskDone();
206 * Adds a successful lookup to the DNS cache, then clears the cache.
208 TEST_F('NetInternalsTest', 'netInternalsDnsViewSuccess', function() {
209 NetInternalsTest.switchToView('dns');
210 var taskQueue = new NetInternalsTest.TaskQueue(true);
211 taskQueue.addTask(new AddCacheEntryTask(
212 'somewhere.com', '1.2.3.4', 0, false));
213 taskQueue.addTask(new ClearCacheTask());
214 taskQueue.addTask(new WaitForEntryDestructionTask('somewhere.com'));
215 taskQueue.run(true);
219 * Adds a failed lookup to the DNS cache, then clears the cache.
221 TEST_F('NetInternalsTest', 'netInternalsDnsViewFail', function() {
222 NetInternalsTest.switchToView('dns');
223 var taskQueue = new NetInternalsTest.TaskQueue(true);
224 taskQueue.addTask(new AddCacheEntryTask(
225 'nowhere.com', '', NetError.ERR_NAME_NOT_RESOLVED, false));
226 taskQueue.addTask(new ClearCacheTask());
227 taskQueue.addTask(new WaitForEntryDestructionTask('nowhere.com'));
228 taskQueue.run(true);
232 * Adds an expired successful lookup to the DNS cache, then clears the cache.
234 TEST_F('NetInternalsTest', 'netInternalsDnsViewExpired', function() {
235 NetInternalsTest.switchToView('dns');
236 var taskQueue = new NetInternalsTest.TaskQueue(true);
237 taskQueue.addTask(new AddCacheEntryTask(
238 'somewhere.com', '1.2.3.4', 0, true));
239 taskQueue.addTask(new ClearCacheTask());
240 taskQueue.addTask(new WaitForEntryDestructionTask('somewhere.com'));
241 taskQueue.run(true);
245 * Adds two entries to the DNS cache, clears the cache, and then repeats.
247 TEST_F('NetInternalsTest', 'netInternalsDnsViewAddTwoTwice', function() {
248 NetInternalsTest.switchToView('dns');
249 var taskQueue = new NetInternalsTest.TaskQueue(true);
250 for (var i = 0; i < 2; ++i) {
251 taskQueue.addTask(new AddCacheEntryTask(
252 'somewhere.com', '1.2.3.4', 0, false));
253 taskQueue.addTask(new AddCacheEntryTask(
254 'nowhere.com', '', NetError.ERR_NAME_NOT_RESOLVED, true));
255 taskQueue.addTask(new ClearCacheTask());
256 taskQueue.addTask(new WaitForEntryDestructionTask('somewhere.com'));
257 taskQueue.addTask(new WaitForEntryDestructionTask('nowhere.com'));
259 taskQueue.run(true);
263 * Makes sure that openning and then closing an incognito window clears the
264 * DNS cache. To keep things simple, we add a fake cache entry ourselves,
265 * rather than having the incognito browser create one.
267 TEST_F('NetInternalsTest', 'netInternalsDnsViewIncognitoClears', function() {
268 NetInternalsTest.switchToView('dns');
269 var taskQueue = new NetInternalsTest.TaskQueue(true);
270 taskQueue.addTask(new NetInternalsTest.CreateIncognitoBrowserTask());
271 taskQueue.addTask(new AddCacheEntryTask(
272 'somewhere.com', '1.2.3.4', 0, true));
273 taskQueue.addTask(NetInternalsTest.getCloseIncognitoBrowserTask());
274 taskQueue.addTask(new WaitForEntryDestructionTask('somewhere.com'));
275 taskQueue.run(true);
278 })(); // Anonymous namespace