add first render test

This commit is contained in:
Konstantin Käfer
2015-02-04 20:09:02 +01:00
parent c4c45a5ff8
commit 66ca191ab2
8 changed files with 102 additions and 30 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
/build
/lib
/node_modules
/test/results

View File

@@ -11,6 +11,10 @@ HOST ?= osx
endif
HOST ?= linux
# Explicitly disable the default FileSource implementation
ASSET = none
HTTP = none
CACHE = none
include $(MBGL)/config/defaults.mk

View File

@@ -3,6 +3,7 @@
"version": "0.0.1",
"main": "lib/mbgl.node",
"dependencies": {
"mkdirp": "^0.5.0",
"mocha": "^2.1.0",
"nan": "^1.4.1",
"request": "^2.51.0"

View File

@@ -16,7 +16,6 @@ NodeMap::RenderWorker::~RenderWorker() {
}
void NodeMap::RenderWorker::Execute() {
fprintf(stderr, "executing render worker\n");
try {
nodeMap->view.resize(options->width, options->height, options->ratio);
nodeMap->map.setAccessToken(options->accessToken);
@@ -28,12 +27,11 @@ void NodeMap::RenderWorker::Execute() {
// Run the loop. It will terminate when we don't have any further listeners.
nodeMap->map.run();
fprintf(stderr, "run completed\n");
const unsigned int width = options->width * options->ratio;
const unsigned int height = options->height * options->ratio;
image = mbgl::util::compress_png(width, height, nodeMap->view.readPixels().get());
fprintf(stderr, "png compressed\n");
} catch (const std::exception &ex) {
auto pixels = nodeMap->view.readPixels();
image = mbgl::util::compress_png(width, height, pixels.get());
} catch (mbgl::exception &ex) {
SetErrorMessage(ex.what());
}
}

13
test/fixtures/tiles.tilejson vendored Normal file
View File

@@ -0,0 +1,13 @@
{
"bounds": [ -180, -85.0511, 180, 85.0511 ],
"center": [ 0, 0, 0 ],
"format": "pbf",
"id": "mapbox.mapbox-streets-v6-dev",
"maskLevel": 8,
"maxzoom": 15,
"minzoom": 0,
"name": "Mapbox Streets V6",
"scheme": "xyz",
"tilejson": "2.0.0",
"tiles": [ "./fixtures/tiles/{z}-{x}-{y}.vector.pbf" ]
}

BIN
test/fixtures/tiles/0-0-0.vector.pbf vendored Normal file

Binary file not shown.

View File

@@ -4,6 +4,38 @@
var assert = require('assert');
var mbgl = require('..');
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
mkdirp.sync('test/results');
var style = {
'version': 7,
'name': 'Empty',
'sources': {
'mapbox': {
'type': 'vector',
'url': './fixtures/tiles.tilejson',
'maxzoom': 15
}
},
'layers': [{
'id': 'background',
'type': 'background',
'paint': {
'background-color': 'white'
}
}, {
'id': 'water',
'type': 'fill',
'source': 'mapbox',
'source-layer': 'water',
'paint': {
'fill-color': 'blue'
}
}]
};
describe('Map', function() {
@@ -29,30 +61,30 @@ describe('Map', function() {
});
it('should require the FileSource object to have request and cancel methods', function() {
var fs = new mbgl.FileSource();
var fileSource = new mbgl.FileSource();
assert.throws(function() {
new mbgl.Map(fs);
new mbgl.Map(fileSource);
}, /FileSource must have a request member function/);
fs.request = 'test';
fileSource.request = 'test';
assert.throws(function() {
new mbgl.Map(fs);
new mbgl.Map(fileSource);
}, /FileSource must have a request member function/);
fs.request = function() {};
fileSource.request = function() {};
assert.throws(function() {
new mbgl.Map(fs);
new mbgl.Map(fileSource);
}, /FileSource must have a cancel member function/);
fs.cancel = 'test';
fileSource.cancel = 'test';
assert.throws(function() {
new mbgl.Map(fs);
new mbgl.Map(fileSource);
}, /FileSource must have a cancel member function/);
fs.cancel = function() {};
fileSource.cancel = function() {};
assert.doesNotThrow(function() {
new mbgl.Map(fs);
new mbgl.Map(fileSource);
});
});
@@ -61,11 +93,12 @@ describe('Map', function() {
describe('load styles', function() {
var map;
var fileSource = new mbgl.FileSource();
fileSource.request = function() {};
fileSource.cancel = function() {};
beforeEach(function() {
var fs = new mbgl.FileSource();
fs.request = function() {};
fs.cancel = function() {};
map = new mbgl.Map(fs);
map = new mbgl.Map(fileSource);
});
afterEach(function() {
@@ -82,28 +115,40 @@ describe('Map', function() {
}, /Expect either an object or array at root/);
});
it('accepts a stylesheet string', function() {
it('accepts an empty stylesheet string', function() {
map.load('{}');
});
it('accepts a JSON stylesheet', function() {
map.load(style);
});
it('accepts a stringified stylesheet', function() {
map.load(JSON.stringify(style));
});
});
describe('render arguments', function() {
describe('render argument requirements', function() {
var map;
var fileSource = new mbgl.FileSource();
fileSource.request = function(req) {
fs.readFile(path.join('test', req.url), function(err, data) {
req.respond(err, { data: data });
assert.ifError(err);
});
};
fileSource.cancel = function() {};
beforeEach(function() {
var fs = new mbgl.FileSource();
fs.request = function() {};
fs.cancel = function() {};
map = new mbgl.Map(fs);
map = new mbgl.Map(fileSource);
});
afterEach(function() {
map = null;
});
it('requires a string or object as the first parameter', function() {
it('requires an object as the first parameter', function() {
assert.throws(function() {
map.render();
}, /First argument must be an options object/);
@@ -123,12 +168,22 @@ describe('Map', function() {
}, /Second argument must be a callback function/);
});
it('requires a callback as the second parameter', function(done) {
it('requires a style to be set', function(done) {
map.render({}, function(err) {
done(err);
assert.ok(err);
assert.equal(err.message, 'Style is not set');
done();
});
});
it('returns an image', function(done) {
map.load(style);
map.render({}, function(err, data) {
assert.ifError(err);
fs.writeFileSync('test/results/image.png', data);
done();
});
});
});
});

2
vendor/mbgl vendored