Path: blob/trunk/javascript/selenium-webdriver/test/lib/logging_test.js
2885 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'1819const assert = require('node:assert')20const sinon = require('sinon')21const logging = require('selenium-webdriver/lib/logging')2223describe('logging', function () {24let mgr, root, clock2526beforeEach(function setUp() {27mgr = new logging.LogManager()28root = mgr.getLogger('')2930clock = sinon.useFakeTimers()31})3233afterEach(function tearDown() {34clock.restore()35})3637describe('LogManager', function () {38describe('getLogger()', function () {39it('handles falsey input', function () {40assert.strictEqual(root, mgr.getLogger())41assert.strictEqual(root, mgr.getLogger(''))42assert.strictEqual(root, mgr.getLogger(null))43assert.strictEqual(root, mgr.getLogger(0))44})4546it('creates parent loggers', function () {47let logger = mgr.getLogger('foo.bar.baz')48assert.strictEqual(logger.parent_, mgr.getLogger('foo.bar'))4950logger = logger.parent_51assert.strictEqual(logger.parent_, mgr.getLogger('foo'))5253logger = logger.parent_54assert.strictEqual(logger.parent_, mgr.getLogger(''))5556assert.strictEqual(logger.parent_.parent_, null)57})58})59})6061describe('Logger', function () {62describe('getEffectiveLevel()', function () {63it('defaults to OFF', function () {64assert.strictEqual(root.getLevel(), logging.Level.OFF)65assert.strictEqual(root.getEffectiveLevel(), logging.Level.OFF)6667root.setLevel(null)68assert.strictEqual(root.getLevel(), null)69assert.strictEqual(root.getEffectiveLevel(), logging.Level.OFF)70})7172it('uses own level if set', function () {73let logger = mgr.getLogger('foo.bar.baz')74assert.strictEqual(logger.getLevel(), null)75assert.strictEqual(logger.getEffectiveLevel(), logging.Level.OFF)7677logger.setLevel(logging.Level.INFO)78assert.strictEqual(logger.getLevel(), logging.Level.INFO)79assert.strictEqual(logger.getEffectiveLevel(), logging.Level.INFO)80})8182it('uses level from set on nearest parent', function () {83let ancestor = mgr.getLogger('foo')84ancestor.setLevel(logging.Level.SEVERE)8586let logger = mgr.getLogger('foo.bar.baz')87assert.strictEqual(logger.getLevel(), null)88assert.strictEqual(logger.getEffectiveLevel(), logging.Level.SEVERE)89})90})9192describe('isLoggable()', function () {93it("compares level against logger's effective level", function () {94const log1 = mgr.getLogger('foo')95log1.setLevel(logging.Level.WARNING)9697const log2 = mgr.getLogger('foo.bar.baz')9899assert(!log2.isLoggable(logging.Level.FINEST))100assert(!log2.isLoggable(logging.Level.INFO))101assert(log2.isLoggable(logging.Level.WARNING))102assert(log2.isLoggable(logging.Level.SEVERE))103104log2.setLevel(logging.Level.INFO)105106assert(!log2.isLoggable(logging.Level.FINEST))107assert(log2.isLoggable(logging.Level.INFO))108assert(log2.isLoggable(logging.Level.WARNING))109assert(log2.isLoggable(logging.Level.SEVERE))110111log2.setLevel(logging.Level.ALL)112113assert(log2.isLoggable(logging.Level.FINEST))114assert(log2.isLoggable(logging.Level.INFO))115assert(log2.isLoggable(logging.Level.WARNING))116assert(log2.isLoggable(logging.Level.SEVERE))117})118119it('Level.OFF is never loggable', function () {120function test(level) {121root.setLevel(level)122assert(!root.isLoggable(logging.Level.OFF), 'OFF should not be loggable at ' + level)123}124125test(logging.Level.ALL)126test(logging.Level.INFO)127test(logging.Level.OFF)128})129})130131describe('log()', function () {132it('does not invoke loggable if message is not loggable', function () {133const log = mgr.getLogger('foo')134log.setLevel(logging.Level.OFF)135136let callback = sinon.spy()137log.addHandler(callback)138root.addHandler(callback)139140assert(!callback.called)141})142143it('invokes handlers for each parent logger', function () {144const cb1 = sinon.spy()145const cb2 = sinon.spy()146const cb3 = sinon.spy()147148const log1 = mgr.getLogger('foo')149const log2 = mgr.getLogger('foo.bar')150const log3 = mgr.getLogger('foo.bar.baz')151const log4 = mgr.getLogger('foo.bar.baz.quot')152153log1.addHandler(cb1)154log1.setLevel(logging.Level.INFO)155156log2.addHandler(cb2)157log2.setLevel(logging.Level.WARNING)158159log3.addHandler(cb3)160log3.setLevel(logging.Level.FINER)161162clock.tick(123456)163164log4.finest('this is the finest message')165log4.finer('this is a finer message')166log4.info('this is an info message')167log4.warning('this is a warning message')168log4.severe('this is a severe message')169170assert.strictEqual(4, cb1.callCount)171assert.strictEqual(4, cb2.callCount)172assert.strictEqual(4, cb3.callCount)173174const entry1 = new logging.Entry(logging.Level.FINER, '[foo.bar.baz.quot] this is a finer message', 123456)175const entry2 = new logging.Entry(logging.Level.INFO, '[foo.bar.baz.quot] this is an info message', 123456)176const entry3 = new logging.Entry(logging.Level.WARNING, '[foo.bar.baz.quot] this is a warning message', 123456)177const entry4 = new logging.Entry(logging.Level.SEVERE, '[foo.bar.baz.quot] this is a severe message', 123456)178179check(cb1.getCall(0).args[0], entry1)180check(cb1.getCall(1).args[0], entry2)181check(cb1.getCall(2).args[0], entry3)182check(cb1.getCall(3).args[0], entry4)183184check(cb2.getCall(0).args[0], entry1)185check(cb2.getCall(1).args[0], entry2)186check(cb2.getCall(2).args[0], entry3)187check(cb2.getCall(3).args[0], entry4)188189check(cb3.getCall(0).args[0], entry1)190check(cb3.getCall(1).args[0], entry2)191check(cb3.getCall(2).args[0], entry3)192check(cb3.getCall(3).args[0], entry4)193194function check(entry, expected) {195assert.strictEqual(entry.level, expected.level, 'wrong level')196assert.strictEqual(entry.message, expected.message, 'wrong message')197assert.strictEqual(entry.timestamp, expected.timestamp, 'wrong time')198}199})200201it('does not invoke removed handler', function () {202root.setLevel(logging.Level.INFO)203const cb = sinon.spy()204205root.addHandler(cb)206root.info('hi')207assert.strictEqual(1, cb.callCount)208209assert(root.removeHandler(cb))210root.info('bye')211assert.strictEqual(1, cb.callCount)212213assert(!root.removeHandler(cb))214})215})216})217218describe('getLevel()', function () {219it('converts named levels', function () {220assert.strictEqual(logging.Level.DEBUG, logging.getLevel('DEBUG'))221assert.strictEqual(logging.Level.ALL, logging.getLevel('FAKE'))222})223224it('converts numeric levels', function () {225assert.strictEqual(logging.Level.DEBUG, logging.getLevel(logging.Level.DEBUG.value))226})227228it('normalizes numeric levels', function () {229assert.strictEqual(logging.Level.OFF, logging.getLevel(logging.Level.OFF.value * 2))230231let diff = logging.Level.SEVERE.value - logging.Level.WARNING.value232assert.strictEqual(logging.Level.WARNING, logging.getLevel(logging.Level.WARNING.value + diff * 0.5))233234assert.strictEqual(logging.Level.ALL, logging.getLevel(0))235assert.strictEqual(logging.Level.ALL, logging.getLevel(-1))236})237})238239describe('Preferences', function () {240it('can be converted to JSON', function () {241let prefs = new logging.Preferences()242assert.strictEqual('{}', JSON.stringify(prefs))243244prefs.setLevel('foo', logging.Level.DEBUG)245assert.strictEqual('{"foo":"DEBUG"}', JSON.stringify(prefs))246247prefs.setLevel(logging.Type.BROWSER, logging.Level.FINE)248assert.strictEqual('{"foo":"DEBUG","browser":"FINE"}', JSON.stringify(prefs))249})250})251})252253254