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.
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
11 // Include test fixture.
12 GEN_INCLUDE(['net_internals_test.js']);
14 // Anonymous namespace
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,
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.
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),
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);
73 * A Task that waits until constants are received, completing asynchronously
75 * @extends {NetInternalsTest.Task}
77 function WaitForConstantsTask() {
78 NetInternalsTest.Task.call(this);
79 this.setCompleteAsync(true);
82 WaitForConstantsTask.prototype = {
83 __proto__: NetInternalsTest.Task.prototype,
86 * Starts watching for constants.
89 g_browser.addConstantsObserver(this);
93 * Resumes the test once we receive constants.
95 onReceivedConstants: function() {
102 * A Task that creates a log dump in the browser process via NetLogLogger,
103 * waits to receive it via IPC, and and then loads it as a string.
104 * @param {integer} truncate The number of bytes to truncate from the end of
105 * the string, if any, to simulate a truncated log due to crash, or
106 * quitting without properly shutting down a NetLogLogger.
107 * @extends {NetInternalsTest.Task}
109 function GetNetLogLoggerStringAndLoadLogTask(truncate) {
110 NetInternalsTest.Task.call(this);
111 this.setCompleteAsync(true);
112 this.truncate_ = truncate;
115 GetNetLogLoggerStringAndLoadLogTask.prototype = {
116 __proto__: NetInternalsTest.Task.prototype,
119 * Sets |NetInternals.callback|, and sends the request to the browser process.
122 NetInternalsTest.setCallback(this.onLogReceived_.bind(this));
123 chrome.send('getNetLogLoggerLog');
127 * Loads the received log and completes the Task.
128 * @param {string} logDumpText Log received from the browser process.
130 onLogReceived_: function(logDumpText) {
131 assertEquals('string', typeof logDumpText);
132 expectTrue(SourceTracker.getInstance().getPrivacyStripping());
134 var expectedResult = 'The log file is missing clientInfo.numericDate.\n' +
135 'Synthesizing export date as time of last event captured.\n' +
138 if (this.truncate_) {
140 'Log file truncated. Events may be missing.\n' + expectedResult;
143 logDumpText = logDumpText.substring(0, logDumpText.length - this.truncate_);
144 expectEquals(expectedResult, log_util.loadLogFile(logDumpText, 'log.txt'));
145 expectFalse(SourceTracker.getInstance().getPrivacyStripping());
147 NetInternalsTest.expectStatusViewNodeVisible(LoadedStatusView.MAIN_BOX_ID);
149 // Make sure the DIV on the import tab containing the comments is visible
150 // before checking the displayed text.
151 expectTrue(NetInternalsTest.nodeIsVisible($(ImportView.LOADED_DIV_ID)));
152 expectEquals('', $(ImportView.LOADED_INFO_USER_COMMENTS_ID).innerText);
159 * Checks the visibility of each view after loading a freshly created log dump.
160 * Also checks that the BrowserBridge is disabled.
162 function checkViewsAfterLogLoaded() {
163 expectTrue(g_browser.isDisabled());
164 var tabVisibilityState = {
184 NetInternalsTest.checkTabHandleVisibility(tabVisibilityState, false);
188 * Checks the visibility of each view after loading a log dump created by the
189 * NetLogLogger. Also checks that the BrowserBridge is disabled.
191 function checkViewsAfterNetLogLoggerLogLoaded() {
192 expectTrue(g_browser.isDisabled());
193 var tabVisibilityState = {
213 NetInternalsTest.checkTabHandleVisibility(tabVisibilityState, false);
216 function checkPrivacyStripping(expectedValue) {
217 expectEquals(expectedValue,
218 SourceTracker.getInstance().getPrivacyStripping());
222 * Checks the currently active view.
223 * @param {string} id ID of the view that should be active.
225 function checkActiveView(id) {
226 expectEquals(id, NetInternalsTest.getActiveTabId());
230 * Exports a log dump to a string and loads it. Makes sure no errors occur,
231 * and checks visibility of tabs aftwards. Does not actually save the log to a
233 * TODO(mmenke): Add some checks for the import view.
235 TEST_F('NetInternalsTest', 'netInternalsLogUtilExportImport', function() {
236 expectFalse(g_browser.isDisabled());
237 expectTrue(SourceTracker.getInstance().getPrivacyStripping());
238 NetInternalsTest.expectStatusViewNodeVisible(CaptureStatusView.MAIN_BOX_ID);
240 var taskQueue = new NetInternalsTest.TaskQueue(true);
241 taskQueue.addTask(new CreateAndLoadLogTask('Detailed explanation.'));
242 taskQueue.addFunctionTask(checkViewsAfterLogLoaded);
247 * Exports a log dump by using a NetLogLogger and attempts to load it from a
248 * string. The string is passed to Javascript via an IPC rather than drag and
251 TEST_F('NetInternalsTest',
252 'netInternalsLogUtilImportNetLogLoggerDump',
254 var taskQueue = new NetInternalsTest.TaskQueue(true);
255 taskQueue.addTask(new GetNetLogLoggerStringAndLoadLogTask(0));
256 taskQueue.addFunctionTask(checkViewsAfterNetLogLoggerLogLoaded);
261 * Same as above, but it truncates the log to simulate the case of a crash when
264 TEST_F('NetInternalsTest',
265 'netInternalsLogUtilImportNetLogLoggerDumpTruncated',
267 var taskQueue = new NetInternalsTest.TaskQueue(true);
268 taskQueue.addTask(new GetNetLogLoggerStringAndLoadLogTask(20));
269 taskQueue.addFunctionTask(checkViewsAfterNetLogLoggerLogLoaded);
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',
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);
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
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));
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',
315 var taskQueue = new NetInternalsTest.TaskQueue(true);
316 // Switching to stop capturing mode will load a log dump, which will update
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);
329 // Simulate clicking the stop button.
330 $(CaptureView.STOP_BUTTON_ID).click();
333 })(); // Anonymous namespace