move node-mbgl to platform/node/ directory

This commit is contained in:
Mike Morris
2015-08-21 15:53:24 -04:00
parent a8d9b921d7
commit 6e6387e089
39 changed files with 0 additions and 4 deletions

View File

@@ -0,0 +1,27 @@
var spawn = require('child_process').spawn;
module.exports = function compare(actual, expected, diff, t, callback) {
var compare = spawn('compare', ['-metric', 'MAE', actual, expected, diff]);
var error = '';
compare.stderr.on('data', function(data) {
error += data.toString();
});
compare.on('error', function(err) {
t.error(err);
});
compare.on('exit', function(code) {
// The compare program returns 2 on error otherwise 0 if the images are similar or 1 if they are dissimilar.
if (code === 2) {
callback(error.trim(), Infinity);
} else {
var match = error.match(/^\d+(?:\.\d+)?\s+\(([^\)]+)\)\s*$/);
var difference = match ? parseFloat(match[1]) : Infinity;
callback(match ? '' : error, difference);
}
});
compare.stdin.end();
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

26
platform/node/test/fixtures/style.json vendored Normal file
View File

@@ -0,0 +1,26 @@
{
"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"
}
}]
}

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" ]
}

Binary file not shown.

View File

@@ -0,0 +1,83 @@
'use strict';
/* jshint node:true */
var test = require('tape');
var mbgl = require('../..');
var fs = require('fs');
var path = require('path');
var suitePath = path.dirname(require.resolve('mapbox-gl-test-suite/package.json'));
function renderTest(style, info, dir, key) {
return function (t) {
var completed = 0;
var remaining = 10;
var start = +new Date;
var options = {};
options.request = function(req) {
fs.readFile(path.join(suitePath, decodeURIComponent(req.url)), function(err, data) {
req.respond(err, { data: data });
t.error(err);
});
};
options.cancel = function() {};
options.ratio = 1.0;
var map = new mbgl.Map(options);
map.load(style);
function render() {
map.render(info[key], function(err, image) {
t.error(err);
t.ok(true, 'render @ ' + ((+new Date) - start) + 'ms');
if (++completed === remaining) {
map.release();
t.end();
} else {
render();
}
});
}
render();
};
}
function rewriteLocalSchema(uri) {
return uri.replace(/^local:\/\//, '');
}
test('Consecutive', function(t) {
var dir = 'line-join';
var k = 'round';
var style = require(path.join(suitePath, 'tests', dir, 'style.json')),
info = require(path.join(suitePath, 'tests', dir, 'info.json'));
for (var k in style.sources) {
var source = style.sources[k];
if (source.tiles) {
source.tiles = source.tiles.map(rewriteLocalSchema);
}
if (source.url) {
source.url = rewriteLocalSchema(source.url);
}
}
if (style.sprite) style.sprite = rewriteLocalSchema(style.sprite);
if (style.glyphs) style.glyphs = rewriteLocalSchema(style.glyphs);
style = JSON.stringify(style);
for (k in info) {
t.test(dir + ' ' + k, renderTest(style, info, dir, k));
}
t.end();
});

View File

@@ -0,0 +1,126 @@
'use strict';
/* jshint node: true */
var test = require('tape').test;
var mbgl = require('../..');
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var http = require('http');
var request = require('request');
var st = require('st');
var style = require('../../test/fixtures/style.json');
var PNG = require('pngjs').PNG;
var compare = require('../compare.js');
var dirPath = path.join(path.dirname(require.resolve('../../package.json')), 'test');
var server = http.createServer(st({ path: dirPath }));
server.listen(0);
function filePath(name) {
return ['expected', 'actual', 'diff'].reduce(function(prev, key) {
var dir = path.join('test', key, 'gzip');
mkdirp.sync(dir);
prev[key] = path.join(dir, name);
return prev;
}, {});
}
function setup(options, callback) {
callback(new mbgl.Map(options));
}
function getOptions(gzip, t) {
return {
request: function(req) {
var parts = req.url.split('.');
var filetype = parts[parts.length - 1];
request({
url: 'http://localhost:' + server.address().port + path.join('/', req.url),
encoding: null,
gzip: filetype === 'pbf' ? gzip : true,
headers: {
'Accept-Encoding': 'gzip'
}
}, function (err, res, body) {
t.error(err);
var response = {};
response.data = res.body;
req.respond(null, response);
});
},
ratio: 1.0
};
}
test('gzip', function(t) {
t.test('success', function(t) {
mbgl.on('message', function(msg) {
if (msg.severity == 'ERROR') t.error(msg);
});
setup(getOptions(true, t), function(map) {
map.load(style);
map.render({}, function(err, data) {
mbgl.removeAllListeners('message');
map.release();
t.error(err);
var filename = filePath('success.png');
var png = new PNG({
width: data.width,
height: data.height
});
png.data = data.pixels;
if (process.env.UPDATE) {
png.pack()
.pipe(fs.createWriteStream(filename.expected))
.on('finish', t.end);
} else {
png.pack()
.pipe(fs.createWriteStream(filename.actual))
.on('finish', function() {
compare(filename.actual, filename.expected, filename.diff, t, function(err, diff) {
t.error(err);
t.ok(diff <= 0.01, 'actual matches expected');
t.end();
});
});
}
});
});
});
t.test('unhandled', function(t) {
mbgl.once('message', function(msg) {
if (msg.severity == 'ERROR') {
t.ok(msg, 'emits error');
t.equal(msg.class, 'Style');
t.equal(msg.severity, 'ERROR');
t.ok(msg.text.match(/pbf unknown field type exception/), 'error text matches');
}
});
setup(getOptions(false, t), function(map) {
map.load(style);
map.render({}, function(err, data) {
map.release();
t.ok(err, 'returns error');
t.ok(err.message.match(/Failed to parse/), 'error text matches');
t.end();
});
});
});
t.test('teardown', function(t) {
server.close(t.end);
});
});

View File

@@ -0,0 +1,293 @@
'use strict';
/* jshint node: true */
var test = require('tape');
var mbgl = require('../..');
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var style = require('../fixtures/style.json');
var PNG = require('pngjs').PNG;
var compare = require('../compare.js');
function filePath(name) {
return ['expected', 'actual', 'diff'].reduce(function(prev, key) {
var dir = path.join('test', key, 'map');
mkdirp.sync(dir);
prev[key] = path.join(dir, name);
return prev;
}, {});
}
function setup(options, callback) {
callback(new mbgl.Map(options));
}
test('Map', function(t) {
t.test('constructor', function(t) {
t.test('must be called with new', function(t) {
t.throws(function() {
mbgl.Map();
}, /Use the new operator to create new Map objects/);
t.end();
});
t.test('should require an options object as first parameter', function(t) {
t.throws(function() {
new mbgl.Map();
}, /Requires an options object as first argument/);
t.throws(function() {
new mbgl.Map('options');
}, /Requires an options object as first argument/);
t.end();
});
t.test('should require then options object to have request and cancel methods', function(t) {
var options = {};
t.throws(function() {
new mbgl.Map(options);
}, /Options object must have a 'request' method/);
options.request = 'test';
t.throws(function() {
new mbgl.Map(options);
}, /Options object must have a 'request' method/);
options.request = function() {};
options.cancel = 'test';
t.throws(function() {
new mbgl.Map(options);
}, /Options object 'cancel' property must be a function/);
options.cancel = function() {};
t.throws(function() {
new mbgl.Map(options);
}, /Options object must have a numerical 'ratio' property/);
options.ratio = 'test';
t.throws(function() {
new mbgl.Map(options);
}, /Options object must have a numerical 'ratio' property/);
options.ratio = 1.0;
t.doesNotThrow(function() {
new mbgl.Map(options);
});
t.end();
});
t.end();
});
t.test('load styles', function(t) {
var options = {};
options.request = function() {};
options.cancel = function() {};
options.ratio = 1.0;
t.test('requires a string or object as the first parameter', function(t) {
t.test('requires a map style as first argument', function(t) {
setup(options, function(map) {
t.throws(function() {
map.load();
}, /Requires a map style as first argument/);
map.release();
t.end();
});
});
t.test('expect either an object or array at root', { timeout: 1000 }, function(t) {
setup(options, function(map) {
mbgl.once('message', function(msg) {
t.equal(msg.severity, 'ERROR');
t.equal(msg.class, 'ParseStyle');
t.ok(msg.text.match(/Expect either an object or array at root/));
map.release();
t.end();
});
map.load('invalid');
});
});
t.end();
});
t.test('accepts an empty stylesheet string', function(t) {
setup(options, function(map) {
t.doesNotThrow(function() {
map.load('{}');
});
map.release();
t.end();
});
});
t.test('accepts a JSON stylesheet', { timeout: 1000 }, function(t) {
setup(options, function(map) {
t.doesNotThrow(function() {
map.load(style);
});
map.release();
t.end();
});
});
t.test('accepts a stringified stylesheet', { timeout: 1000 }, function(t) {
setup(options, function(map) {
t.doesNotThrow(function() {
map.load(JSON.stringify(style));
});
map.release();
t.end();
});
});
t.end();
});
t.test('render argument requirements', function(t) {
var options = {};
options.request = function(req) {
fs.readFile(path.join('test', req.url), function(err, data) {
req.respond(err, { data: data });
});
};
options.cancel = function() {};
options.ratio = 1.0;
t.test('requires an object as the first parameter', function(t) {
setup(options, function(map) {
t.throws(function() {
map.render();
}, /First argument must be an options object/);
t.throws(function() {
map.render('invalid');
}, /First argument must be an options object/);
map.release();
t.end();
});
});
t.test('requires a callback as the second parameter', function(t) {
setup(options, function(map) {
t.throws(function() {
map.render({});
}, /Second argument must be a callback function/);
t.throws(function() {
map.render({}, 'invalid');
}, /Second argument must be a callback function/);
map.release();
t.end();
});
});
t.test('requires a style to be set', function(t) {
setup(options, function(map) {
t.throws(function() {
map.render({}, function() {});
}, /Style is not loaded/);
map.release();
t.end();
});
});
t.test('returns an error', function(t) {
mbgl.on('message', function(msg) {
t.ok(msg, 'emits error');
t.equal(msg.class, 'Style');
t.equal(msg.severity, 'ERROR');
t.ok(msg.text.match(/Failed to load/), 'error text matches');
});
setup(options, function(map) {
map.load(style);
map.render({ zoom: 1 }, function(err, data) {
mbgl.removeAllListeners('message');
map.release();
t.ok(err, 'returns error');
t.ok(err.message.match(/Failed to load/), 'error text matches');
t.end();
});
});
});
t.test('double release', function(t) {
setup(options, function(map) {
map.release();
t.throws(function() {
map.release();
}, /Map resources have already been released/);
t.end();
});
});
t.test('returns an image', function(t) {
setup(options, function(map) {
map.load(style);
map.render({}, function(err, data) {
t.error(err);
map.release();
var filename = filePath('image.png');
var png = new PNG({
width: data.width,
height: data.height
});
png.data = data.pixels;
if (process.env.UPDATE) {
png.pack()
.pipe(fs.createWriteStream(filename.expected))
.on('finish', t.end);
} else {
png.pack()
.pipe(fs.createWriteStream(filename.actual))
.on('finish', function() {
compare(filename.actual, filename.expected, filename.diff, t, function(err, diff) {
t.error(err);
t.ok(diff <= 0.01, 'actual matches expected');
t.end();
});
});
}
});
});
});
t.end();
});
t.end();
});

View File

@@ -0,0 +1,160 @@
'use strict';
/* jshint node:true */
var test = require('tape');
var mbgl = require('..');
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var PNG = require('pngjs').PNG;
var compare = require('./compare.js');
var suitePath = path.dirname(require.resolve('mapbox-gl-test-suite/package.json'));
function template(name) {
return fs.readFileSync(require.resolve('mapbox-gl-test-suite/templates/' + name + '.html.tmpl')).toString();
}
var results = '';
var resultTemplate = template('result');
function format(tmpl, kwargs) {
return tmpl.replace(/\{\{|\}\}|\{([^}]+)\}/g, function(match, key) {
if (match === '{{') return '{';
if (match === '}}') return '}';
return kwargs[key];
});
}
function renderTest(style, info, base, key) {
var dir = path.join(suitePath, 'tests', base, key);
mkdirp.sync(dir);
return function(t) {
var watchdog = setTimeout(function() {
t.fail('timed out after 20 seconds');
}, 20000);
t.once('end', function() {
clearTimeout(watchdog);
});
var options = {};
options.request = function(req) {
fs.readFile(path.join(suitePath, decodeURIComponent(req.url)), function(err, data) {
req.respond(err, { data: data });
t.error(err);
});
};
options.cancel = function() {};
options.ratio = 1.0;
var map = new mbgl.Map(options);
map.load(style);
map.render(info[key], function(err, data) {
t.error(err);
map.release();
var expected = path.join(dir, 'expected.png');
var actual = path.join(dir, 'actual.png');
var diff = path.join(dir, 'diff.png');
var png = new PNG({
width: data.width,
height: data.height
});
png.data = data.pixels;
if (process.env.UPDATE) {
png.pack()
.pipe(fs.createWriteStream(expected))
.on('finish', t.end);
} else {
png.pack()
.pipe(fs.createWriteStream(actual))
.on('finish', function() {
compare(actual, expected, diff, t, function(err, diff) {
t.error(err);
var allowed = ('diff' in info) ? info.diff : 0.001;
var color = diff <= allowed ? 'green' : 'red';
results += format(resultTemplate, {
name: base,
key: key,
color: color,
error: err ? '<p>' + err + '</p>' : '',
difference: diff,
zoom: info.zoom || 0,
center: info.center || [0, 0],
bearing: info.bearing || 0,
width: info.width || 512,
height: info.height || 512
});
t.ok(diff <= allowed, 'actual matches expected');
t.end();
});
});
}
});
};
}
function rewriteLocalSchema(url) {
var regex = /^local:\/\//;
if (url instanceof Array) {
return url.map(function(str) {
return str.replace(regex, '');
});
} else if (typeof url === "string") {
return url.replace(regex, '');
}
}
var tests;
if (process.argv[1] === __filename) {
tests = process.argv.slice(2);
}
test('Render', function(t) {
fs.readdirSync(path.join(suitePath, 'tests')).forEach(function(dir) {
if (dir === 'index.html' || dir[0] === '.') return;
if (tests && tests.length && tests.indexOf(dir) < 0) return;
var style = require(path.join(suitePath, 'tests', dir, 'style.json')),
info = require(path.join(suitePath, 'tests', dir, 'info.json'));
for (var k in style.sources) {
var source = style.sources[k];
if (source.tiles) {
source.tiles = source.tiles.map(rewriteLocalSchema);
}
if (source.url) {
source.url = rewriteLocalSchema(source.url);
}
}
if (style.sprite) style.sprite = rewriteLocalSchema(style.sprite);
if (style.glyphs) style.glyphs = rewriteLocalSchema(style.glyphs);
style = JSON.stringify(style);
for (k in info) {
(info[k].native === false ? t.skip : t.test)(dir + ' ' + k, renderTest(style, info, dir, k));
}
});
t.test('results', function(t) {
var p = path.join(suitePath, 'tests', 'index.html');
fs.writeFileSync(p, format(template('results'), {results: results}));
console.warn('Results at: ' + p);
t.end();
});
});