From ec430779f47c965386012886240a2afe64fdb8c2 Mon Sep 17 00:00:00 2001 From: Jirka Date: Fri, 15 Apr 2022 10:32:08 +0200 Subject: [PATCH] Move open-failed error message to the handler, adjust test for the new message style. Add 'numberOfHandlers()' method for events. --- src/eventsource.js | 12 ++++++++++++ src/tilesource.js | 6 ++++-- test/modules/basic.js | 4 ++-- test/modules/events.js | 30 ++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/eventsource.js b/src/eventsource.js index e3957d7f..c5629234 100644 --- a/src/eventsource.js +++ b/src/eventsource.js @@ -122,6 +122,18 @@ $.EventSource.prototype = { } }, + /** + * Get the amount of handlers registered for a given event. + * @param {String} eventName - Name of event to inspect. + * @return {number} amount of events + */ + numberOfHandlers: function (eventName) { + var events = this.events[ eventName ]; + if ( !events ) { + return 0; + } + return events.length; + }, /** * Remove all event handlers for a given event type. If no type is given all diff --git a/src/tilesource.js b/src/tilesource.js index 48cfe851..c7ab8e56 100644 --- a/src/tilesource.js +++ b/src/tilesource.js @@ -529,7 +529,7 @@ $.TileSource.prototype = { exception rather than the second one raised when we try to access xhr.status */ try { - msg = "HTTP " + xhr.status + " attempting to load TileSource"; + msg = "HTTP " + xhr.status + " attempting to load TileSource: " + url; } catch ( e ) { var formattedExc; if ( typeof ( exc ) === "undefined" || !exc.toString ) { @@ -538,9 +538,11 @@ $.TileSource.prototype = { formattedExc = exc.toString(); } - msg = formattedExc + " attempting to load TileSource"; + msg = formattedExc + " attempting to load TileSource: " + url; } + $.console.error(msg); + /*** * Raised when an error occurs loading a TileSource. * diff --git a/test/modules/basic.js b/test/modules/basic.js index 6cc2ad2b..d5635fcf 100644 --- a/test/modules/basic.js +++ b/test/modules/basic.js @@ -58,8 +58,8 @@ assert.equal($(".openseadragon-message").length, 1, "Open failures should display a message"); - assert.ok(testLog.error.contains('["AJAX request returned %d: %s",404,"/test/data/not-a-real-file"]'), - "AJAX failures should be logged to the console"); + assert.ok(testLog.error.contains('["HTTP 404 attempting to load TileSource: /test/data/not-a-real-file"]'), + "'open-failed' fired after AJAX error handler prints error to the console.'"); done(); }); diff --git a/test/modules/events.js b/test/modules/events.js index 6900b063..f8173976 100644 --- a/test/modules/events.js +++ b/test/modules/events.js @@ -1152,6 +1152,36 @@ }); // ---------- + QUnit.test( 'Viewer: event count test with \'tile-drawing\'', function (assert) { + var done = assert.async(); + assert.ok(viewer.numberOfHandlers('tile-drawing') === 0, + "'tile-drawing' event is empty by default."); + + var tileDrawing = function ( event ) { + viewer.removeHandler( 'tile-drawing', tileDrawing ); + assert.ok(viewer.numberOfHandlers('tile-drawing') === 0, + "'tile-drawing' deleted: count is 0."); + viewer.close(); + done(); + }; + + var tileDrawingDummy = function ( event ) {}; + + viewer.addHandler( 'tile-drawing', tileDrawing ); + assert.ok(viewer.numberOfHandlers('tile-drawing') === 1, + "'tile-drawing' event set to 1."); + + viewer.addHandler( 'tile-drawing', tileDrawingDummy ); + assert.ok(viewer.numberOfHandlers('tile-drawing') === 2, + "'tile-drawing' event set to 2."); + + viewer.removeHandler( 'tile-drawing', tileDrawingDummy ); + assert.ok(viewer.numberOfHandlers('tile-drawing') === 1, + "'tile-drawing' deleted once: count is 1."); + + viewer.open( '/test/data/testpattern.dzi' ); + } ); + QUnit.test( 'Viewer: tile-drawing event', function (assert) { var done = assert.async(); var tileDrawing = function ( event ) {