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
103 * WriteToFileNetLogObserver, waits to receive it via IPC, and and then loads
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.
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' +
139 if (this.truncate_
) {
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
);
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
= {
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
= {
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
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
);
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
252 TEST_F('NetInternalsTest',
253 'netInternalsLogUtilImportNetLogFile',
255 var taskQueue
= new NetInternalsTest
.TaskQueue(true);
256 taskQueue
.addTask(new GetNetLogFileContentsAndLoadLogTask(0));
257 taskQueue
.addFunctionTask(checkViewsAfterNetLogFileLoaded
);
262 * Same as above, but it truncates the log to simulate the case of a crash when
265 TEST_F('NetInternalsTest', 'netInternalsLogUtilImportNetLogFileTruncated',
267 var taskQueue
= new NetInternalsTest
.TaskQueue(true);
268 taskQueue
.addTask(new GetNetLogFileContentsAndLoadLogTask(20));
269 taskQueue
.addFunctionTask(checkViewsAfterNetLogFileLoaded
);
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