openseadragon/test/modules/utils.js

142 lines
5.1 KiB
JavaScript
Raw Normal View History

/* global QUnit, Util */
2013-05-14 06:24:04 +04:00
(function() {
2013-05-21 21:39:23 +04:00
QUnit.module("utils");
2013-05-14 06:24:04 +04:00
2013-05-21 21:39:23 +04:00
// ----------
QUnit.test("addRemoveClass", function(assert) {
2013-05-14 06:24:04 +04:00
var div = OpenSeadragon.makeNeutralElement('div');
assert.strictEqual(div.className, '',
2013-05-14 06:24:04 +04:00
"makeNeutralElement set no classes");
OpenSeadragon.addClass(div, 'foo');
assert.strictEqual(div.className, 'foo',
2013-05-14 06:24:04 +04:00
"Added first class");
OpenSeadragon.addClass(div, 'bar');
assert.strictEqual(div.className, 'foo bar',
2013-05-14 06:24:04 +04:00
"Added second class");
OpenSeadragon.addClass(div, 'baz');
assert.strictEqual(div.className, 'foo bar baz',
2013-05-14 06:24:04 +04:00
"Added third class");
OpenSeadragon.addClass(div, 'plugh');
assert.strictEqual(div.className, 'foo bar baz plugh',
2013-05-14 06:24:04 +04:00
"Added fourth class");
OpenSeadragon.addClass(div, 'foo');
assert.strictEqual(div.className, 'foo bar baz plugh',
2013-05-14 06:24:04 +04:00
"Re-added first class");
OpenSeadragon.addClass(div, 'bar');
assert.strictEqual(div.className, 'foo bar baz plugh',
2013-05-14 06:24:04 +04:00
"Re-added middle class");
OpenSeadragon.addClass(div, 'plugh');
assert.strictEqual(div.className, 'foo bar baz plugh',
2013-05-14 06:24:04 +04:00
"Re-added last class");
OpenSeadragon.removeClass(div, 'xyzzy');
assert.strictEqual(div.className, 'foo bar baz plugh',
2013-05-14 06:24:04 +04:00
"Removed nonexistent class");
OpenSeadragon.removeClass(div, 'ba');
assert.strictEqual(div.className, 'foo bar baz plugh',
2013-05-14 06:24:04 +04:00
"Removed nonexistent class with existent substring");
OpenSeadragon.removeClass(div, 'bar');
assert.strictEqual(div.className, 'foo baz plugh',
2013-05-14 06:24:04 +04:00
"Removed middle class");
OpenSeadragon.removeClass(div, 'plugh');
assert.strictEqual(div.className, 'foo baz',
2013-05-14 06:24:04 +04:00
"Removed last class");
OpenSeadragon.removeClass(div, 'foo');
assert.strictEqual(div.className, 'baz',
2013-05-14 06:24:04 +04:00
"Removed first class");
OpenSeadragon.removeClass(div, 'baz');
assert.strictEqual(div.className, '',
2013-05-14 06:24:04 +04:00
"Removed only class");
});
2013-05-21 21:39:23 +04:00
// ----------
QUnit.test("makeAjaxRequest", function(assert) {
var timeWatcher = Util.timeWatcher(assert);
2013-05-21 21:39:23 +04:00
OpenSeadragon.makeAjaxRequest('data/testpattern.dzi',
function(xhr) {
assert.equal(xhr.status, 200, 'Success callback called for HTTP 200');
assert.ok(/deepzoom/.test(xhr.responseText), 'Success function called');
timeWatcher.done();
},
function(xhr) {
assert.ok(false, 'Error callback should not be called');
timeWatcher.done();
}
);
});
QUnit.test("makeAjaxRequest for invalid file", function(assert) {
var timeWatcher = Util.timeWatcher(assert);
OpenSeadragon.makeAjaxRequest('not-a-real-dzi-file',
function(xhr) {
assert.ok(false, 'Success function should not be called for errors');
timeWatcher.done();
},
function(xhr) {
assert.equal(xhr.status, 404, 'Error callback called for HTTP 404');
assert.ok(true, 'Error function should be called for errors');
timeWatcher.done();
}
);
2013-05-21 21:39:23 +04:00
});
QUnit.test("getUrlProtocol", function(assert) {
2014-04-15 20:55:32 +04:00
assert.equal(OpenSeadragon.getUrlProtocol("test"), window.location.protocol,
2014-04-15 20:55:32 +04:00
"'test' url protocol should be window.location.protocol");
assert.equal(OpenSeadragon.getUrlProtocol("/test"), window.location.protocol,
2014-04-15 20:55:32 +04:00
"'/test' url protocol should be window.location.protocol");
assert.equal(OpenSeadragon.getUrlProtocol("//test"), window.location.protocol,
2014-04-15 20:55:32 +04:00
"'//test' url protocol should be window.location.protocol");
assert.equal(OpenSeadragon.getUrlProtocol("http://test"), "http:",
2014-04-15 20:55:32 +04:00
"'http://test' url protocol should be http:");
assert.equal(OpenSeadragon.getUrlProtocol("https://test"), "https:",
2014-04-15 20:55:32 +04:00
"'https://test' url protocol should be https:");
assert.equal(OpenSeadragon.getUrlProtocol("file://test"), "file:",
2014-04-15 20:55:32 +04:00
"'file://test' url protocol should be file:");
assert.equal(OpenSeadragon.getUrlProtocol("FTP://test"), "ftp:",
2014-04-15 20:55:32 +04:00
"'FTP://test' url protocol should be ftp:");
});
// ----------
QUnit.test("requestAnimationFrame", function(assert) {
var timeWatcher = Util.timeWatcher(assert);
OpenSeadragon.requestAnimationFrame(function() {
assert.ok(true, 'frame fired');
timeWatcher.done();
});
});
// ----------
QUnit.test("cancelAnimationFrame", function(assert) {
var done = assert.async();
var frameFired = false;
setTimeout(function() {
assert.strictEqual(frameFired, false, 'the frame never fired');
done();
}, 150);
var frameId = OpenSeadragon.requestAnimationFrame(function() {
frameFired = true;
});
OpenSeadragon.cancelAnimationFrame(frameId);
});
2013-05-14 06:24:04 +04:00
})();