Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | |
|
eroman
2017/03/28 22:41:59
nit: remove this change
mgersh
2017/03/29 18:15:25
Done.
| |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 // 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 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 4 // found in the LICENSE file. |
| 4 | 5 |
| 5 // Include test fixture. | 6 // Include test fixture. |
| 6 GEN_INCLUDE(['net_internals_test.js']); | 7 GEN_INCLUDE(['net_internals_test.js']); |
| 7 | 8 |
| 8 // Anonymous namespace | 9 // Anonymous namespace |
| 9 (function() { | 10 (function() { |
| 10 | 11 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 94 this.expired_ ? -2 : 2 | 95 this.expired_ ? -2 : 2 |
| 95 ]; | 96 ]; |
| 96 chrome.send('addCacheEntry', addCacheEntryParams); | 97 chrome.send('addCacheEntry', addCacheEntryParams); |
| 97 g_browser.addHostResolverInfoObserver(this, false); | 98 g_browser.addHostResolverInfoObserver(this, false); |
| 98 }, | 99 }, |
| 99 | 100 |
| 100 /** | 101 /** |
| 101 * Callback from the BrowserBridge. Checks if |hostResolverInfo| has the | 102 * Callback from the BrowserBridge. Checks if |hostResolverInfo| has the |
| 102 * DNS entry specified on creation. If so, validates it and completes the | 103 * DNS entry specified on creation. If so, validates it and completes the |
| 103 * task. If not, continues running. | 104 * task. If not, continues running. |
| 104 * @param {object} hostResolverInfo Results a host resolver info query. | 105 * @param {object} hostResolverInfo Results of a host resolver info query. |
| 105 */ | 106 */ |
| 106 onHostResolverInfoChanged: function(hostResolverInfo) { | 107 onHostResolverInfoChanged: function(hostResolverInfo) { |
| 107 if (!this.isDone()) { | 108 if (!this.isDone()) { |
| 108 checkDisplay(hostResolverInfo); | 109 checkDisplay(hostResolverInfo); |
| 109 | 110 |
| 110 var index = findEntry(hostResolverInfo, this.hostname_); | 111 var index = findEntry(hostResolverInfo, this.hostname_); |
| 111 if (index >= 0) { | 112 if (index >= 0) { |
| 112 var entry = hostResolverInfo.cache.entries[index]; | 113 var entry = hostResolverInfo.cache.entries[index]; |
| 113 if (this.netError_) { | 114 if (this.netError_) { |
| 114 this.checkError_(entry); | 115 this.checkError_(entry); |
| 115 } else { | 116 } else { |
| 116 this.checkSuccess_(entry); | 117 this.checkSuccess_(entry); |
| 117 } | 118 } |
| 118 var expirationDate = timeutil.convertTimeTicksToDate(entry.expiration); | 119 var expirationDate = timeutil.convertTimeTicksToDate(entry.expiration); |
| 119 expectEquals(this.expired_, expirationDate < new Date()); | 120 expectEquals(this.expired_, expirationDate < new Date()); |
| 120 | 121 |
| 121 // Expect at least one active or expired entry, depending on |expired_|. | 122 // 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 // To avoid any chance of a race, exact values are not tested. |
| 123 var activeMin = this.expired_ ? 0 : 1; | 124 var activeMin = this.expired_ ? 0 : 1; |
| 124 var expiredMin = this.expired_ ? 1 : 0; | 125 var expiredMin = this.expired_ ? 1 : 0; |
| 125 expectLE(activeMin, parseInt($(DnsView.ACTIVE_SPAN_ID).innerText)); | 126 expectLE(activeMin, parseInt($(DnsView.ACTIVE_SPAN_ID).innerText)); |
| 126 expectLE(expiredMin, parseInt($(DnsView.EXPIRED_SPAN_ID).innerText)); | 127 expectLE(expiredMin, parseInt($(DnsView.EXPIRED_SPAN_ID).innerText)); |
| 127 | 128 |
| 128 // Text for the expiration time of the entry should contain 'Expired' | 129 // Text for the expiration time of the entry should contain 'Expired' |
| 129 // only if |expired_| is true. Only checked for entries we add | 130 // only if |expired_| is true. Only checked for entries we add |
| 130 // ourselves to avoid any expiration time race. | 131 // ourselves to avoid any expiration time race. |
| 131 var expirationText = | 132 var expirationText = |
| 132 NetInternalsTest.getTbodyText(DnsView.CACHE_TBODY_ID, index, 3); | 133 NetInternalsTest.getTbodyText(DnsView.CACHE_TBODY_ID, index, 4); |
| 133 expectEquals(this.expired_, /expired/i.test(expirationText)); | 134 expectEquals(this.expired_, /expired/i.test(expirationText)); |
| 134 | 135 |
| 135 this.onTaskDone(); | 136 this.onTaskDone(); |
| 136 } | 137 } |
| 137 } | 138 } |
| 138 }, | 139 }, |
| 139 | 140 |
| 140 checkError_: function(entry) { | 141 checkError_: function(entry) { |
| 141 expectEquals(this.netError_, entry.error); | 142 expectEquals(this.netError_, entry.error); |
| 142 }, | 143 }, |
| 143 | 144 |
| 144 checkSuccess_: function(entry) { | 145 checkSuccess_: function(entry) { |
| 145 expectEquals(undefined, entry.error); | 146 expectEquals(undefined, entry.error); |
| 146 expectEquals(1, entry.addresses.length); | 147 expectEquals(1, entry.addresses.length); |
| 147 expectEquals(0, entry.addresses[0].search(this.ipAddress_)); | 148 expectEquals(0, entry.addresses[0].search(this.ipAddress_)); |
| 148 } | 149 } |
| 149 }; | 150 }; |
| 150 | 151 |
| 151 /** | 152 /** |
| 152 * A Task clears the cache by simulating a button click. | 153 * A Task that simulates a network change and checks that cache entries are |
|
eroman
2017/03/28 22:41:59
nice job adding tests!
mgersh
2017/03/29 18:15:25
thanks :)
| |
| 154 * expired. | |
| 155 * @extends {NetInternalsTest.Task} | |
| 156 */ | |
| 157 function NetworkChangeTask() { | |
| 158 NetInternalsTest.Task.call(this); | |
| 159 } | |
| 160 | |
| 161 NetworkChangeTask.prototype = { | |
| 162 __proto__: NetInternalsTest.Task.prototype, | |
| 163 | |
| 164 start: function() { | |
| 165 chrome.send('changeNetwork'); | |
| 166 g_browser.addHostResolverInfoObserver(this, false); | |
| 167 }, | |
| 168 | |
| 169 /** | |
| 170 * Callback from the BrowserBridge. Checks if the entry has been expired. | |
| 171 * If so, the task completes. | |
| 172 * @param {object} hostResolverInfo Results of a host resolver info query. | |
| 173 */ | |
| 174 onHostResolverInfoChanged: function(hostResolverInfo) { | |
| 175 if (!this.isDone()) { | |
| 176 checkDisplay(hostResolverInfo); | |
| 177 | |
| 178 var entries = hostResolverInfo.cache.entries; | |
| 179 var tableId = DnsView.CACHE_TBODY_ID; | |
| 180 var foundExpired = false; | |
| 181 | |
| 182 // Look for an entry that's expired due to a network change. | |
| 183 for (var row = 0; row < entries.length; ++row) { | |
| 184 var text = NetInternalsTest.getTbodyText(tableId, row, 5); | |
| 185 if (/expired/i.test(text)) { | |
| 186 foundExpired = true; | |
| 187 }; | |
| 188 } | |
| 189 | |
| 190 if (foundExpired) { | |
| 191 // Expect at least one expired entry and at least one network change. | |
| 192 // To avoid any chance of a race, exact values are not tested. | |
| 193 expectLE(0, parseInt($(DnsView.ACTIVE_SPAN_ID).innerText)); | |
| 194 expectLE(1, parseInt($(DnsView.EXPIRED_SPAN_ID).innerText)); | |
| 195 expectLE(1, parseInt($(DnsView.NETWORK_SPAN_ID).innerText)); | |
| 196 this.onTaskDone(); | |
| 197 } | |
| 198 } | |
| 199 } | |
| 200 }; | |
| 201 | |
| 202 /** | |
| 203 * A Task that clears the cache by simulating a button click. | |
| 153 * @extends {NetInternalsTest.Task} | 204 * @extends {NetInternalsTest.Task} |
| 154 */ | 205 */ |
| 155 function ClearCacheTask() { | 206 function ClearCacheTask() { |
| 156 NetInternalsTest.Task.call(this); | 207 NetInternalsTest.Task.call(this); |
| 157 } | 208 } |
| 158 | 209 |
| 159 ClearCacheTask.prototype = { | 210 ClearCacheTask.prototype = { |
| 160 __proto__: NetInternalsTest.Task.prototype, | 211 __proto__: NetInternalsTest.Task.prototype, |
| 161 | 212 |
| 162 start: function() { | 213 start: function() { |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 268 NetInternalsTest.switchToView('dns'); | 319 NetInternalsTest.switchToView('dns'); |
| 269 var taskQueue = new NetInternalsTest.TaskQueue(true); | 320 var taskQueue = new NetInternalsTest.TaskQueue(true); |
| 270 taskQueue.addTask(new NetInternalsTest.CreateIncognitoBrowserTask()); | 321 taskQueue.addTask(new NetInternalsTest.CreateIncognitoBrowserTask()); |
| 271 taskQueue.addTask(new AddCacheEntryTask( | 322 taskQueue.addTask(new AddCacheEntryTask( |
| 272 'somewhere.com', '1.2.3.4', 0, true)); | 323 'somewhere.com', '1.2.3.4', 0, true)); |
| 273 taskQueue.addTask(NetInternalsTest.getCloseIncognitoBrowserTask()); | 324 taskQueue.addTask(NetInternalsTest.getCloseIncognitoBrowserTask()); |
| 274 taskQueue.addTask(new WaitForEntryDestructionTask('somewhere.com')); | 325 taskQueue.addTask(new WaitForEntryDestructionTask('somewhere.com')); |
| 275 taskQueue.run(true); | 326 taskQueue.run(true); |
| 276 }); | 327 }); |
| 277 | 328 |
| 329 /** | |
| 330 * Adds a successful lookup to the DNS cache, then simulates a network change | |
| 331 * and checks that the entry expires. | |
| 332 */ | |
| 333 TEST_F('NetInternalsTest', 'netInternalsDnsViewNetworkChanged', function() { | |
| 334 NetInternalsTest.switchToView('dns'); | |
| 335 var taskQueue = new NetInternalsTest.TaskQueue(true); | |
| 336 taskQueue.addTask(new AddCacheEntryTask( | |
| 337 'somewhere.com', '1.2.3.4', 0, false)); | |
| 338 taskQueue.addTask(new NetworkChangeTask()); | |
| 339 taskQueue.addTask(new ClearCacheTask()); | |
| 340 taskQueue.addTask(new WaitForEntryDestructionTask('somewhere.com')); | |
| 341 taskQueue.run(true); | |
| 342 }); | |
| 343 | |
| 278 })(); // Anonymous namespace | 344 })(); // Anonymous namespace |
| OLD | NEW |