Commit 0be8a1d2 authored by Paul Lewis's avatar Paul Lewis Committed by Commit Bot

[DevTools] Adds Karma tests for common/Console

Bug: 1009443
Change-Id: Iaf4ed0b2f02e016a665c42da2082c207e2a2ee6f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1829710Reviewed-by: default avatarYang Guo <yangguo@chromium.org>
Reviewed-by: default avatarMathias Bynens <mathias@chromium.org>
Commit-Queue: Paul Lewis <aerotwist@chromium.org>
Cr-Commit-Position: refs/heads/master@{#701271}
parent 39db9396
// Copyright 2014 The Chromium Authors. All rights reserved. // Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import ObjectWrapper from './Object.js';
/** /**
* @unrestricted * @unrestricted
*/ */
export default class Console extends Common.Object { export default class Console extends ObjectWrapper {
constructor() { constructor() {
super(); super();
/** @type {!Array.<!Common.Console.Message>} */ /** @type {!Array.<!Message>} */
this._messages = []; this._messages = [];
} }
/** /**
* @param {string} text * @param {string} text
* @param {!Common.Console.MessageLevel} level * @param {!MessageLevel} level
* @param {boolean=} show * @param {boolean=} show
*/ */
addMessage(text, level, show) { addMessage(text, level, show) {
const message = const message = new Message(text, level || MessageLevel.Info, Date.now(), show || false);
new Common.Console.Message(text, level || Common.Console.MessageLevel.Info, Date.now(), show || false);
this._messages.push(message); this._messages.push(message);
this.dispatchEventToListeners(Common.Console.Events.MessageAdded, message); this.dispatchEventToListeners(Common.Console.Events.MessageAdded, message);
} }
...@@ -27,25 +29,25 @@ export default class Console extends Common.Object { ...@@ -27,25 +29,25 @@ export default class Console extends Common.Object {
* @param {string} text * @param {string} text
*/ */
log(text) { log(text) {
this.addMessage(text, Common.Console.MessageLevel.Info); this.addMessage(text, MessageLevel.Info);
} }
/** /**
* @param {string} text * @param {string} text
*/ */
warn(text) { warn(text) {
this.addMessage(text, Common.Console.MessageLevel.Warning); this.addMessage(text, MessageLevel.Warning);
} }
/** /**
* @param {string} text * @param {string} text
*/ */
error(text) { error(text) {
this.addMessage(text, Common.Console.MessageLevel.Error, true); this.addMessage(text, MessageLevel.Error, true);
} }
/** /**
* @return {!Array.<!Common.Console.Message>} * @return {!Array.<!Message>}
*/ */
messages() { messages() {
return this._messages; return this._messages;
...@@ -83,7 +85,7 @@ export const MessageLevel = { ...@@ -83,7 +85,7 @@ export const MessageLevel = {
export class Message { export class Message {
/** /**
* @param {string} text * @param {string} text
* @param {!Common.Console.MessageLevel} level * @param {!MessageLevel} level
* @param {number} timestamp * @param {number} timestamp
* @param {boolean} show * @param {boolean} show
*/ */
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
const { assert } = chai;
import { default as Console, Events, MessageLevel } from '../../../front_end/common/Console.js';
describe('Console', () => {
let consoleImpl: Console;
beforeEach(() => {
consoleImpl = new Console();
});
it('adds messages', () => {
consoleImpl.addMessage('Foo', MessageLevel.Info, true);
const messages = consoleImpl.messages();
assert.equal(messages.length, 1);
assert.equal(messages[0].text, 'Foo');
});
it('adds handles messages of all types', () => {
const messageTypes = new Map<string, string>([
['Info', 'log'],
['Warning', 'warn'],
['Error', 'error']
]);
for (const [type, method] of messageTypes) {
consoleImpl = new Console();
// Dispatch the message of the appropriate type.
consoleImpl[method](type);
// Now read the message back and check it.
const messages = consoleImpl.messages();
assert.equal(messages.length, 1);
assert.equal(messages[0].text, type);
assert.equal(messages[0].level, MessageLevel[type]);
}
});
it('stores messages', () => {
consoleImpl.addMessage('Foo', MessageLevel.Info, true);
consoleImpl.addMessage('Baz', MessageLevel.Warning, true);
consoleImpl.addMessage('Bar', MessageLevel.Error, true);
consoleImpl.addMessage('Donkey', MessageLevel.Info, true);
const messages = consoleImpl.messages();
assert.equal(messages.length, 4);
});
it('dispatches events to listeners', (done) => {
consoleImpl.addEventListener(Events.MessageAdded, ({ data }) => {
assert.equal(data.text, 'Foo');
done();
});
consoleImpl.addMessage('Foo', MessageLevel.Info, true);
});
});
{ {
"compilerOptions": { "compilerOptions": {
"module": "esnext",
"target": "esnext",
"typeRoots": ["../../../devtools-node-modules/third_party/node_modules/@types"] "typeRoots": ["../../../devtools-node-modules/third_party/node_modules/@types"]
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment