Skip to content

Commit

Permalink
add more tests for loader cases
Browse files Browse the repository at this point in the history
  • Loading branch information
englercj committed Apr 24, 2015
1 parent 715dd22 commit 77cf854
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 26 deletions.
Empty file removed test/fixtures/.gitkeep
Empty file.
92 changes: 92 additions & 0 deletions test/fixtures/hud.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{"frames": {

"0.png":
{
"frame": {"x":14,"y":28,"w":14,"h":14},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":14,"h":14},
"sourceSize": {"w":14,"h":14}
},
"1.png":
{
"frame": {"x":14,"y":42,"w":12,"h":14},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":12,"h":14},
"sourceSize": {"w":12,"h":14}
},
"2.png":
{
"frame": {"x":14,"y":14,"w":14,"h":14},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":14,"h":14},
"sourceSize": {"w":14,"h":14}
},
"3.png":
{
"frame": {"x":42,"y":0,"w":14,"h":14},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":14,"h":14},
"sourceSize": {"w":14,"h":14}
},
"4.png":
{
"frame": {"x":28,"y":0,"w":14,"h":14},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":14,"h":14},
"sourceSize": {"w":14,"h":14}
},
"5.png":
{
"frame": {"x":14,"y":0,"w":14,"h":14},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":14,"h":14},
"sourceSize": {"w":14,"h":14}
},
"6.png":
{
"frame": {"x":0,"y":42,"w":14,"h":14},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":14,"h":14},
"sourceSize": {"w":14,"h":14}
},
"7.png":
{
"frame": {"x":0,"y":28,"w":14,"h":14},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":14,"h":14},
"sourceSize": {"w":14,"h":14}
},
"8.png":
{
"frame": {"x":0,"y":14,"w":14,"h":14},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":14,"h":14},
"sourceSize": {"w":14,"h":14}
},
"9.png":
{
"frame": {"x":0,"y":0,"w":14,"h":14},
"rotated": false,
"trimmed": false,
"spriteSourceSize": {"x":0,"y":0,"w":14,"h":14},
"sourceSize": {"w":14,"h":14}
}},
"meta": {
"app": "http://www.texturepacker.com",
"version": "1.0",
"image": "hud.png",
"format": "RGBA8888",
"size": {"w":64,"h":64},
"scale": "1",
"smartupdate": "$TexturePacker:SmartUpdate:47025c98c8b10634b75172d4ed7e7edc$"
}
}
Binary file added test/fixtures/hud.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/hud2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions test/fixtures/spritesheetMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function spritesheetMiddleware()
{
return function (resource, next)
{
// skip if no data, its not json, or it isn't spritesheet data
if (!resource.data || !resource.isJson || !resource.data.frames)
{
return next();
}

var loadOptions = {
crossOrigin: resource.crossOrigin,
loadType: Resource.LOAD_TYPE.IMAGE
};

var route = dirname(resource.url.replace(this.baseUrl, ''));

// load the image for this sheet
this.add(resource.name + '_image', route + '/' + resource.data.meta.image, loadOptions, function (res)
{
next();
});
};
};


function dirname(path) {
var result = posixSplitPath(path),
root = result[0],
dir = result[1];

if (!root && !dir) {
// No dirname whatsoever
return '.';
}

if (dir) {
// It has a dirname, strip trailing slash
dir = dir.substr(0, dir.length - 1);
}

return root + dir;
}

var splitPathRe = /^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/;

function posixSplitPath(filename) {
return splitPathRe.exec(filename).slice(1);
}
87 changes: 81 additions & 6 deletions test/unit/Loader.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
var loader = null;
var loader = null,
baseUrl = '/fixtures';

describe('Loader', function () {
beforeEach(function () {
loader = new Loader();
loader = new Loader(baseUrl);
});

it('should have correct properties', function () {
expect(loader).to.have.property('baseUrl', '');
expect(loader).to.have.property('baseUrl', baseUrl);
expect(loader).to.have.property('progress', 0);
});

Expand Down Expand Up @@ -231,8 +232,10 @@ describe('Loader', function () {
});

describe('#reset', function () {
it('should reset the loading state of the loader');
it('should reset the progress of the loader');
it('should reset the queue of the loader');
it('should reset the queue/buffer of the loader');
it('should reset the resources of the loader');
});

describe('#load', function () {
Expand All @@ -241,13 +244,22 @@ describe('Loader', function () {
it('should properly load the resource');
});

describe('#loadResource', function () {
describe('#_loadResource', function () {
it('should run the before middleware before loading the resource');
it('should load a resource passed into it');
});

describe('#_onComplete', function () {
it('should emit the `complete` event');
it('should emit the `complete` event', function (done) {
loader.on('complete', function (_l, resources) {
expect(_l).to.equal(loader);
expect(resources).to.equal(loader.resources);

done();
});

loader._onComplete();
});
});

describe('#_onLoad', function () {
Expand All @@ -260,4 +272,67 @@ describe('Loader', function () {
describe('#_runMiddleware', function () {
it('should run each middleware function');
});

describe('events', function () {
it('should call progress for each loaded asset', function (done) {
loader.add([
{ name: 'hud', url: 'hud.png' },
{ name: 'hud2', url: 'hud2.png' }
]);

var spy = sinon.spy();

loader.on('progress', spy);

loader.load(function () {
expect(spy).to.have.been.calledTwice;
done();
});

});

it('progress should be 100% on complete', function (done) {
loader.add([
{ name: 'hud', url: 'hud.png' },
{ name: 'hud2', url: 'hud2.png' }
]);

loader.load(function () {
expect(loader).to.have.property('progress', 100);
done();
});
});

it('should call progress for each loaded asset, even when a middleware adds more resources', function (done) {
loader.add([
{ name: 'hud2', url: 'hud2.png' },
{ name: 'hud_atlas', url: 'hud.json' }
]);

loader.use(spritesheetMiddleware());

var spy = sinon.spy();

loader.on('progress', spy);

loader.load(function () {
expect(spy).to.have.been.calledThrice;
done();
});
});

it('progress should be 100% on complete, even when a middleware adds more resources', function (done) {
loader.add([
{ name: 'hud2', url: 'hud2.png' },
{ name: 'hud_atlas', url: 'hud.json' }
]);

loader.use(spritesheetMiddleware());

loader.load(function () {
expect(loader).to.have.property('progress', 100);
done();
});
});
})
});
18 changes: 9 additions & 9 deletions test/unit/Resource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ var request,
headers = { 'Content-Type': 'application/json' },
json = '[{ "id": 12, "comment": "Hey there" }]';

before(function () {
xhr = sinon.useFakeXMLHttpRequest();
xhr.onCreate = function (req) { request = req; };
});
describe('Resource', function () {
before(function () {
xhr = sinon.useFakeXMLHttpRequest();
xhr.onCreate = function (req) { request = req; };
});

after(function () {
xhr.restore();
});
after(function () {
xhr.restore();
});

describe('Resource', function () {
beforeEach(function () {
res = new Resource(name, url);
request = null;
Expand Down Expand Up @@ -149,7 +149,7 @@ describe('Resource', function () {

expect(res).to.have.property('data').instanceOf(Image)
.and.is.an.instanceOf(HTMLImageElement)
.and.has.property('src', url);
.and.have.property('src', url);
});

it('should load using Audio', function () {
Expand Down
31 changes: 20 additions & 11 deletions testem.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
{
"framework" : "mocha",
"launch_in_dev" : ["Chrome"],
"launch_in_ci" : ["Firefox"],
"src_files" : [
"node_modules/chai/chai.js",
"node_modules/sinon/pkg/sinon.js",
"node_modules/sinon-chai/lib/sinon-chai.js",
"framework": "mocha+chai",
"launch_in_dev": ["Chrome"],
"launch_in_ci": ["Firefox"],
"src_files": [
"dist/resource-loader.js",
"test/unit/**/*.test.js"
],
"serve_files": [
"node_modules/sinon/pkg/sinon.js",
"node_modules/sinon-chai/lib/sinon-chai.js",

"dist/asset-loader.js",
"test/setup.js",
"test/unit/**/*.test.js"
]
"dist/resource-loader.js",

"test/setup.js",
"test/fixtures/spritesheetMiddleware.js",

"test/unit/**/*.test.js"
],
"routes": {
"/fixtures": "test/fixtures"
}
}

0 comments on commit 77cf854

Please sign in to comment.