1 // Copyright 2014 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 /** @suppress {checkTypes|reportUnknownTypes} */
21 var LOCATION_PREFIX = 'console_wrapper_unittest.js:'
23 QUnit.module('console_wrapper', {
24 beforeEach: function() {
26 oldWarn = console.warn;
27 oldError = console.error;
28 oldAssert = console.assert;
31 warnSpy = sinon.spy();
32 errorSpy = sinon.spy();
33 assertSpy = sinon.spy();
36 console.warn = warnSpy;
37 console.error = errorSpy;
38 console.assert = assertSpy;
41 afterEach: function() {
42 remoting.ConsoleWrapper.getInstance().deactivate();
45 console.warn = oldWarn;
46 console.error = oldError;
47 console.assert = oldAssert;
60 QUnit.test('calls to console methods are passed through when wrapped.',
63 remoting.ConsoleWrapper.getInstance().activate(
65 remoting.ConsoleWrapper.LogType.LOG,
66 remoting.ConsoleWrapper.LogType.WARN,
67 remoting.ConsoleWrapper.LogType.ERROR,
68 remoting.ConsoleWrapper.LogType.ASSERT);
70 // Ignore any logging in the ctor or activate() methods.
76 console.log('log', 1, 2, 3);
77 assert.ok(logSpy.calledWith('log', 1, 2, 3));
79 console.warn('warn', 1, 2, 3);
80 assert.ok(warnSpy.calledWith('warn', 1, 2, 3));
82 console.error('error', 1, 2, 3);
83 assert.ok(errorSpy.calledWith('error', 1, 2, 3));
85 console.assert(false, 'assert', 1, 2, 3);
86 assert.ok(assertSpy.calledWith(false, 'assert', 1, 2, 3));
88 // Verify that all the above calls also include the call-site, and validate
89 // it as far as is possible without making the test too flaky.
90 assert.ok(typeof(logSpy.firstCall.args[4]) == 'string');
91 assert.ok(logSpy.firstCall.args[4].startsWith(LOCATION_PREFIX));
93 assert.ok(typeof(warnSpy.firstCall.args[4]) == 'string');
94 assert.ok(warnSpy.firstCall.args[4].startsWith(LOCATION_PREFIX));
96 assert.ok(typeof(errorSpy.firstCall.args[4]) == 'string');
97 assert.ok(errorSpy.firstCall.args[4].startsWith(LOCATION_PREFIX));
99 assert.ok(typeof(assertSpy.firstCall.args[5]) == 'string');
100 assert.ok(assertSpy.firstCall.args[5].startsWith(LOCATION_PREFIX));
102 // Verify that console methods are no longer wrapped after deactivate().
103 remoting.ConsoleWrapper.getInstance().deactivate();
105 console.log('Should not be intercepted.');
106 assert.equal(logSpy.firstCall.args.length, 1);
110 QUnit.test('calls to console methods are saved when wrapped.',
113 remoting.ConsoleWrapper.getInstance().activate(
115 remoting.ConsoleWrapper.LogType.LOG,
116 remoting.ConsoleWrapper.LogType.WARN,
117 remoting.ConsoleWrapper.LogType.ERROR);
119 console.log('first', 1, 2, 3);
120 assert.equal(remoting.ConsoleWrapper.getInstance().getHistory().length, 1);
121 var entry = remoting.ConsoleWrapper.getInstance().getHistory()[0];
122 assert.equal(entry.type,'log');
123 assert.equal(entry.message, '["first",1,2,3]');
124 assert.ok(entry.caller.startsWith(LOCATION_PREFIX));
125 assert.equal(entry.timestamp.toGMTString(), 'Thu, 01 Jan 1970 00:00:00 GMT');
127 this.clock.tick(1000);
128 console.log('second', 1, 2, 3);
129 assert.equal(remoting.ConsoleWrapper.getInstance().getHistory().length, 2);
130 entry = remoting.ConsoleWrapper.getInstance().getHistory()[1];
131 assert.equal(entry.type, 'log');
132 assert.equal(entry.message, '["second",1,2,3]');
133 assert.ok(entry.caller.startsWith(LOCATION_PREFIX));
134 assert.equal(entry.timestamp.toGMTString(), 'Thu, 01 Jan 1970 00:00:01 GMT');
136 this.clock.tick(1000);
137 console.warn('third', 1, 2, 3);
138 assert.equal(remoting.ConsoleWrapper.getInstance().getHistory().length, 3);
139 entry = remoting.ConsoleWrapper.getInstance().getHistory()[2];
140 assert.equal(entry.type, 'warn');
141 assert.equal(entry.message, '["third",1,2,3]');
142 assert.ok(entry.caller.startsWith(LOCATION_PREFIX));
143 assert.equal(entry.timestamp.toGMTString(),'Thu, 01 Jan 1970 00:00:02 GMT');
145 this.clock.tick(1000);
146 console.error('fourth', 1, 2, 3);
147 assert.equal(remoting.ConsoleWrapper.getInstance().getHistory().length, 3);
148 entry = remoting.ConsoleWrapper.getInstance().getHistory()[2];
149 assert.equal(entry.type, 'error');
150 assert.equal(entry.message, '["fourth",1,2,3]');
151 assert.ok(entry.caller.startsWith(LOCATION_PREFIX));
152 assert.equal(entry.timestamp.toGMTString(), 'Thu, 01 Jan 1970 00:00:03 GMT');
156 QUnit.test('calls to assert are saved only for failures.',
159 remoting.ConsoleWrapper.getInstance().activate(
161 remoting.ConsoleWrapper.LogType.ASSERT);
162 console.assert(true, 'first', 1, 2, 3);
163 this.clock.tick(1000);
164 console.assert(false, 'second', 1, 2, 3);
165 assert.equal(remoting.ConsoleWrapper.getInstance().getHistory().length, 1);
166 var entry = remoting.ConsoleWrapper.getInstance().getHistory()[0];
167 assert.equal(entry.type, 'assert');
168 assert.equal(entry.message, '[false,"second",1,2,3]');
169 assert.ok(entry.caller.startsWith(LOCATION_PREFIX));
170 assert.equal(entry.timestamp.toGMTString(), 'Thu, 01 Jan 1970 00:00:01 GMT');