obj2gltf/specs/lib/obj2gltfSpec.js

117 lines
4.1 KiB
JavaScript
Raw Normal View History

2016-07-11 12:27:15 -04:00
'use strict';
2017-05-04 17:58:13 -04:00
var Cesium = require('Cesium');
2017-04-25 13:02:14 -04:00
var fsExtra = require('fs-extra');
2016-07-11 12:27:15 -04:00
var path = require('path');
2017-04-25 13:02:14 -04:00
var Promise = require('bluebird');
2017-04-12 16:55:03 -04:00
var obj2gltf = require('../../lib/obj2gltf');
2016-07-11 12:27:15 -04:00
2017-05-04 17:58:13 -04:00
var RuntimeError = Cesium.RuntimeError;
2017-03-13 15:28:51 -04:00
var objPath = 'specs/data/box-textured/box-textured.obj';
var gltfPath = 'specs/data/box-textured/box-textured.gltf';
var glbPath = 'specs/data/box-textured/box-textured.glb';
var objPathNonExistent = 'specs/data/non-existent.obj';
2017-04-04 17:21:10 -04:00
2017-04-12 16:55:03 -04:00
describe('obj2gltf', function() {
2017-04-25 13:02:14 -04:00
beforeEach(function() {
spyOn(fsExtra, 'outputJson').and.returnValue(Promise.resolve());
spyOn(fsExtra, 'outputFile').and.returnValue(Promise.resolve());
2017-04-25 13:02:14 -04:00
});
it('converts obj to gltf', function(done) {
2017-04-12 16:55:03 -04:00
expect(obj2gltf(objPath, gltfPath)
2016-07-22 14:09:13 -04:00
.then(function() {
var args = fsExtra.outputJson.calls.first().args;
var outputPath = args[0];
var gltf = args[1];
2017-03-13 15:28:51 -04:00
expect(path.normalize(outputPath)).toEqual(path.normalize(gltfPath));
expect(gltf).toBeDefined();
2017-05-04 17:58:13 -04:00
expect(gltf.images.length).toBe(1);
2017-03-13 15:28:51 -04:00
}), done).toResolve();
});
it('converts obj to glb', function(done) {
2017-03-13 15:28:51 -04:00
var options = {
binary : true
2017-03-13 15:28:51 -04:00
};
2017-04-12 16:55:03 -04:00
expect(obj2gltf(objPath, gltfPath, options)
2017-03-13 15:28:51 -04:00
.then(function() {
var args = fsExtra.outputFile.calls.first().args;
var outputPath = args[0];
var glb = args[1];
expect(path.extname(outputPath)).toBe('.glb');
var magic = glb.toString('utf8', 0, 4);
expect(magic).toBe('glTF');
2017-03-13 15:28:51 -04:00
}), done).toResolve();
});
it('converts obj to glb when gltfPath has a .glb extension', function(done) {
2017-04-12 16:55:03 -04:00
expect(obj2gltf(objPath, glbPath)
2017-03-13 15:28:51 -04:00
.then(function() {
var args = fsExtra.outputFile.calls.first().args;
var outputPath = args[0];
var glb = args[1];
expect(path.extname(outputPath)).toBe('.glb');
var magic = glb.toString('utf8', 0, 4);
expect(magic).toBe('glTF');
2017-03-13 15:28:51 -04:00
}), done).toResolve();
});
it('writes out separate resources', function(done) {
2017-03-13 15:28:51 -04:00
var options = {
separate : true,
separateTextures : true
2017-03-13 15:28:51 -04:00
};
2017-04-12 16:55:03 -04:00
expect(obj2gltf(objPath, gltfPath, options)
2017-03-13 15:28:51 -04:00
.then(function() {
expect(fsExtra.outputFile.calls.count()).toBe(2); // Saves out .png and .bin
expect(fsExtra.outputJson.calls.count()).toBe(1); // Saves out .gltf
2017-03-13 15:28:51 -04:00
}), done).toResolve();
});
2017-04-10 17:57:56 -04:00
it('rejects if obj path does not exist', function(done) {
2017-04-12 16:55:03 -04:00
expect(obj2gltf(objPathNonExistent, gltfPath), done).toRejectWith(Error);
});
2017-04-10 17:57:56 -04:00
it('throws if objPath is undefined', function() {
expect(function() {
2017-04-12 16:55:03 -04:00
obj2gltf(undefined, gltfPath);
2017-04-10 17:57:56 -04:00
}).toThrowDeveloperError();
2017-03-13 15:28:51 -04:00
});
2017-05-04 17:58:13 -04:00
it('throws if gltfPath is undefined', function() {
2017-04-10 17:57:56 -04:00
expect(function() {
2017-04-12 16:55:03 -04:00
obj2gltf(objPath, undefined);
2017-04-10 17:57:56 -04:00
}).toThrowDeveloperError();
2017-03-13 15:28:51 -04:00
});
2017-05-04 17:58:13 -04:00
2017-07-24 18:21:01 -04:00
it('throws if more than one material type is set', function() {
2017-05-04 17:58:13 -04:00
var options = {
metallicRoughness : true,
specularGlossiness : true
};
2017-07-24 18:21:01 -04:00
expect(function() {
obj2gltf(objPath, gltfPath, options);
}).toThrowDeveloperError();
});
it('throws if occlusionTexture is defined and specularGlossinessTexture is undefined', function() {
var options = {
occlusionTexture : 'path/to/occlusion/texture'
};
expect(function() {
obj2gltf(objPath, gltfPath, options);
}).toThrowDeveloperError();
});
it('throws if metallicRoughnessOcclusionTexture and specularGlossinessTexture are both defined', function() {
var options = {
metallicRoughnessOcclusionTexture : 'path/to/metallic-roughness-occlusion/texture',
specularGlossinessTexture : 'path/to/specular-glossiness/texture'
};
expect(function() {
obj2gltf(objPath, gltfPath, options);
}).toThrowDeveloperError();
2017-05-04 17:58:13 -04:00
});
2016-07-11 12:27:15 -04:00
});