Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / test / data / webui / net_internals / log_util.js
blobb167b81a942155d882c946b5fd7862b06ba5bbed
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 /**
6 * @fileoverview This file contains tests for creating and loading log files.
7 * It also tests the stop capturing button, since it both creates and then loads
8 * a log dump.
9 */
11 // Include test fixture.
12 GEN_INCLUDE(['net_internals_test.js']);
14 // Anonymous namespace
15 (function() {
17 /**
18 * A Task that creates a log dump and then loads it.
19 * @param {string} userComments User comments to copy to the ExportsView before
20 * creating the log dump.
21 * @extends {NetInternalsTest.Task}
23 function CreateAndLoadLogTask(userComments) {
24 this.userComments_ = userComments;
25 NetInternalsTest.Task.call(this);
26 // Not strictly necessary, but since we complete from a call to the observer,
27 // seems reasonable to complete asynchronously.
28 this.setCompleteAsync(true);
31 CreateAndLoadLogTask.prototype = {
32 __proto__: NetInternalsTest.Task.prototype,
34 /**
35 * Start creating the log dump. Use ExportView's function as it supports
36 * both creating completely new logs and using existing logs to create new
37 * ones, depending on whether or not we're currently viewing a log.
39 start: function() {
40 this.initialPrivacyStripping_ =
41 SourceTracker.getInstance().getPrivacyStripping();
42 $(ExportView.USER_COMMENTS_TEXT_AREA_ID).value = this.userComments_;
43 ExportView.getInstance().createLogDump_(this.onLogDumpCreated.bind(this),
44 true);
47 /**
48 * Callback passed to |createLogDumpAsync|. Tries to load the dumped log
49 * text. Checks the status bar and user comments after loading the log.
50 * @param {string} logDumpText Log dump, as a string.
52 onLogDumpCreated: function(logDumpText) {
53 expectEquals(this.initialPrivacyStripping_,
54 SourceTracker.getInstance().getPrivacyStripping());
55 expectEquals('Log loaded.', log_util.loadLogFile(logDumpText, 'log.txt'));
56 expectFalse(SourceTracker.getInstance().getPrivacyStripping());
58 NetInternalsTest.expectStatusViewNodeVisible(LoadedStatusView.MAIN_BOX_ID);
60 expectEquals(this.userComments_,
61 $(ExportView.USER_COMMENTS_TEXT_AREA_ID).value);
62 // Make sure the DIV on the import tab containing the comments is visible
63 // before checking the displayed text.
64 expectTrue(NetInternalsTest.nodeIsVisible($(ImportView.LOADED_DIV_ID)));
65 expectEquals(this.userComments_,
66 $(ImportView.LOADED_INFO_USER_COMMENTS_ID).innerText);
68 this.onTaskDone();
72 /**
73 * A Task that waits until constants are received, completing asynchronously
74 * once they are.
75 * @extends {NetInternalsTest.Task}
77 function WaitForConstantsTask() {
78 NetInternalsTest.Task.call(this);
79 this.setCompleteAsync(true);
82 WaitForConstantsTask.prototype = {
83 __proto__: NetInternalsTest.Task.prototype,
85 /**
86 * Starts watching for constants.
88 start: function() {
89 g_browser.addConstantsObserver(this);
92 /**
93 * Resumes the test once we receive constants.
95 onReceivedConstants: function() {
96 if (!this.isDone())
97 this.onTaskDone();
102 * A Task that creates a log dump in the browser process via
103 * WriteToFileNetLogObserver, waits to receive it via IPC, and and then loads
104 * it as a string.
105 * @param {integer} truncate The number of bytes to truncate from the end of
106 * the string, if any, to simulate a truncated log due to crash, or
107 * quitting without properly shutting down a WriteToFileNetLogObserver.
108 * @extends {NetInternalsTest.Task}
110 function GetNetLogFileContentsAndLoadLogTask(truncate) {
111 NetInternalsTest.Task.call(this);
112 this.setCompleteAsync(true);
113 this.truncate_ = truncate;
116 GetNetLogFileContentsAndLoadLogTask.prototype = {
117 __proto__: NetInternalsTest.Task.prototype,
120 * Sets |NetInternals.callback|, and sends the request to the browser process.
122 start: function() {
123 NetInternalsTest.setCallback(this.onLogReceived_.bind(this));
124 chrome.send('getNetLogFileContents');
128 * Loads the received log and completes the Task.
129 * @param {string} logDumpText Log received from the browser process.
131 onLogReceived_: function(logDumpText) {
132 assertEquals('string', typeof logDumpText);
133 expectTrue(SourceTracker.getInstance().getPrivacyStripping());
135 var expectedResult = 'The log file is missing clientInfo.numericDate.\n' +
136 'Synthesizing export date as time of last event captured.\n' +
137 'Log loaded.';
139 if (this.truncate_) {
140 expectedResult =
141 'Log file truncated. Events may be missing.\n' + expectedResult;
144 logDumpText = logDumpText.substring(0, logDumpText.length - this.truncate_);
145 expectEquals(expectedResult, log_util.loadLogFile(logDumpText, 'log.txt'));
146 expectFalse(SourceTracker.getInstance().getPrivacyStripping());
148 NetInternalsTest.expectStatusViewNodeVisible(LoadedStatusView.MAIN_BOX_ID);
150 // Make sure the DIV on the import tab containing the comments is visible
151 // before checking the displayed text.
152 expectTrue(NetInternalsTest.nodeIsVisible($(ImportView.LOADED_DIV_ID)));
153 expectEquals('', $(ImportView.LOADED_INFO_USER_COMMENTS_ID).innerText);
155 this.onTaskDone();
160 * Checks the visibility of each view after loading a freshly created log dump.
161 * Also checks that the BrowserBridge is disabled.
163 function checkViewsAfterLogLoaded() {
164 expectTrue(g_browser.isDisabled());
165 var tabVisibilityState = {
166 capture: false,
167 export: true,
168 import: true,
169 proxy: true,
170 events: true,
171 waterfall: true,
172 timeline: true,
173 dns: true,
174 sockets: true,
175 http2: true,
176 quic: true,
177 sdch: true,
178 httpCache: true,
179 modules: true,
180 hsts: false,
181 prerender: true,
182 bandwidth: true,
183 chromeos: false
185 NetInternalsTest.checkTabHandleVisibility(tabVisibilityState, false);
189 * Checks the visibility of each view after loading a log dump created by the
190 * WriteToFileNetLogObserver. Also checks that the BrowserBridge is disabled.
192 function checkViewsAfterNetLogFileLoaded() {
193 expectTrue(g_browser.isDisabled());
194 var tabVisibilityState = {
195 capture: false,
196 export: true,
197 import: true,
198 proxy: false,
199 events: true,
200 waterfall: true,
201 timeline: true,
202 dns: false,
203 sockets: false,
204 http2: false,
205 quic: false,
206 sdch: false,
207 httpCache: false,
208 modules: false,
209 hsts: false,
210 prerender: false,
211 bandwidth: false,
212 chromeos: false
214 NetInternalsTest.checkTabHandleVisibility(tabVisibilityState, false);
217 function checkPrivacyStripping(expectedValue) {
218 expectEquals(expectedValue,
219 SourceTracker.getInstance().getPrivacyStripping());
223 * Checks the currently active view.
224 * @param {string} id ID of the view that should be active.
226 function checkActiveView(id) {
227 expectEquals(id, NetInternalsTest.getActiveTabId());
231 * Exports a log dump to a string and loads it. Makes sure no errors occur,
232 * and checks visibility of tabs aftwards. Does not actually save the log to a
233 * file.
234 * TODO(mmenke): Add some checks for the import view.
236 TEST_F('NetInternalsTest', 'netInternalsLogUtilExportImport', function() {
237 expectFalse(g_browser.isDisabled());
238 expectTrue(SourceTracker.getInstance().getPrivacyStripping());
239 NetInternalsTest.expectStatusViewNodeVisible(CaptureStatusView.MAIN_BOX_ID);
241 var taskQueue = new NetInternalsTest.TaskQueue(true);
242 taskQueue.addTask(new CreateAndLoadLogTask('Detailed explanation.'));
243 taskQueue.addFunctionTask(checkViewsAfterLogLoaded);
244 taskQueue.run(true);
248 * Exports a log dump by using a WriteToFileNetLogObserver and attempts to load
249 * it from a string. The string is passed to Javascript via an IPC rather than
250 * drag and drop.
252 TEST_F('NetInternalsTest',
253 'netInternalsLogUtilImportNetLogFile',
254 function() {
255 var taskQueue = new NetInternalsTest.TaskQueue(true);
256 taskQueue.addTask(new GetNetLogFileContentsAndLoadLogTask(0));
257 taskQueue.addFunctionTask(checkViewsAfterNetLogFileLoaded);
258 taskQueue.run(true);
262 * Same as above, but it truncates the log to simulate the case of a crash when
263 * creating a log.
265 TEST_F('NetInternalsTest', 'netInternalsLogUtilImportNetLogFileTruncated',
266 function() {
267 var taskQueue = new NetInternalsTest.TaskQueue(true);
268 taskQueue.addTask(new GetNetLogFileContentsAndLoadLogTask(20));
269 taskQueue.addFunctionTask(checkViewsAfterNetLogFileLoaded);
270 taskQueue.run(true);
274 * Exports a log dump to a string and loads it, and then repeats, making sure
275 * we can export loaded logs.
277 TEST_F('NetInternalsTest',
278 'netInternalsLogUtilExportImportExportImport',
279 function() {
280 var taskQueue = new NetInternalsTest.TaskQueue(true);
281 taskQueue.addTask(new CreateAndLoadLogTask('Random comment on the weather.'));
282 taskQueue.addFunctionTask(checkViewsAfterLogLoaded);
283 taskQueue.addTask(new CreateAndLoadLogTask('Detailed explanation.'));
284 taskQueue.addFunctionTask(checkViewsAfterLogLoaded);
285 taskQueue.run(true);
289 * Checks pressing the stop capturing button.
291 TEST_F('NetInternalsTest', 'netInternalsLogUtilStopCapturing', function() {
292 var taskQueue = new NetInternalsTest.TaskQueue(true);
293 // Switching to stop capturing mode will load a log dump, which will update
294 // the constants.
295 taskQueue.addTask(new WaitForConstantsTask());
297 taskQueue.addFunctionTask(
298 NetInternalsTest.expectStatusViewNodeVisible.bind(
299 null, HaltedStatusView.MAIN_BOX_ID));
300 taskQueue.addFunctionTask(checkViewsAfterLogLoaded);
301 taskQueue.addFunctionTask(checkPrivacyStripping.bind(null, true));
302 taskQueue.addFunctionTask(checkActiveView.bind(null, ExportView.TAB_ID));
303 taskQueue.run();
305 // Simulate a click on the stop capturing button
306 $(CaptureView.STOP_BUTTON_ID).click();
310 * Switches to stop capturing mode, then exports and imports a log dump.
312 TEST_F('NetInternalsTest',
313 'netInternalsLogUtilStopCapturingExportImport',
314 function() {
315 var taskQueue = new NetInternalsTest.TaskQueue(true);
316 // Switching to stop capturing mode will load a log dump, which will update
317 // the constants.
318 taskQueue.addTask(new WaitForConstantsTask());
320 taskQueue.addFunctionTask(
321 NetInternalsTest.expectStatusViewNodeVisible.bind(
322 null, HaltedStatusView.MAIN_BOX_ID));
323 taskQueue.addFunctionTask(checkViewsAfterLogLoaded);
324 taskQueue.addFunctionTask(checkPrivacyStripping.bind(null, true));
325 taskQueue.addTask(new CreateAndLoadLogTask('Detailed explanation.'));
326 taskQueue.addFunctionTask(checkViewsAfterLogLoaded);
327 taskQueue.run();
329 // Simulate clicking the stop button.
330 $(CaptureView.STOP_BUTTON_ID).click();
333 })(); // Anonymous namespace