Skip to content

Commit

Permalink
Update integration test suite implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Thakker committed Mar 12, 2018
1 parent 835a9a9 commit 97b2496
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 95 deletions.
106 changes: 106 additions & 0 deletions test/ajax_stubs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@

import {PNG} from 'pngjs';
import request from 'request';
// we're using a require hook to load this file instead of src/util/ajax.js,
// so we import browser module as if it were in an adjacent file
import browser from './browser';
const cache = {};

/**
* The type of a resource.
* @private
* @readonly
* @enum {string}
*/
const ResourceType = {
Unknown: 'Unknown',
Style: 'Style',
Source: 'Source',
Tile: 'Tile',
Glyphs: 'Glyphs',
SpriteImage: 'SpriteImage',
SpriteJSON: 'SpriteJSON',
Image: 'Image'
};
export { ResourceType };

if (typeof Object.freeze == 'function') {
Object.freeze(ResourceType);
}

function cached(data, callback) {
setImmediate(() => {
callback(null, data);
});
}

export const getJSON = function({ url }, callback) {
if (cache[url]) return cached(cache[url], callback);
return request(url, (error, response, body) => {
if (!error && response.statusCode >= 200 && response.statusCode < 300) {
let data;
try {
data = JSON.parse(body);
} catch (err) {
return callback(err);
}
cache[url] = data;
callback(null, data);
} else {
callback(error || new Error(response.statusCode));
}
});
};

export const getArrayBuffer = function({ url }, callback) {
if (cache[url]) return cached(cache[url], callback);
return request({ url, encoding: null }, (error, response, body) => {
if (!error && response.statusCode >= 200 && response.statusCode < 300) {
cache[url] = {data: body};
callback(null, {data: body});
} else {
callback(error || new Error(response.statusCode));
}
});
};

export const getImage = function({ url }, callback) {
if (cache[url]) return cached(cache[url], callback);
return request({ url, encoding: null }, (error, response, body) => {
if (!error && response.statusCode >= 200 && response.statusCode < 300) {
new PNG().parse(body, (err, png) => {
if (err) return callback(err);
cache[url] = png;
callback(null, png);
});
} else {
callback(error || {status: response.statusCode});
}
});
};

browser.getImageData = function({width, height, data}) {
return {width, height, data: new Uint8Array(data)};
};

// Hack: since node doesn't have any good video codec modules, just grab a png with
// the first frame and fake the video API.
export const getVideo = function(urls, callback) {
return request({ url: urls[0], encoding: null }, (error, response, body) => {
if (!error && response.statusCode >= 200 && response.statusCode < 300) {
new PNG().parse(body, (err, png) => {
if (err) return callback(err);
callback(null, {
readyState: 4, // HAVE_ENOUGH_DATA
addEventListener: function() {},
play: function() {},
width: png.width,
height: png.height,
data: png.data
});
});
} else {
callback(error || new Error(response.statusCode));
}
});
};
4 changes: 3 additions & 1 deletion test/expression.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

require('flow-remove-types/register');
require('../build/flow-remove-types.js');
require = require("@std/esm")(module, true);

const expressionSuite = require('./integration').expression;
const { createPropertyExpression } = require('../src/style-spec/expression');
const { toString } = require('../src/style-spec/expression/types');
Expand Down
4 changes: 3 additions & 1 deletion test/query.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

require('flow-remove-types/register');
require('./stub_loader');
require('../build/flow-remove-types.js');
require = require("@std/esm")(module, true);

const querySuite = require('./integration').query;
const suiteImplementation = require('./suite_implementation');
Expand Down
4 changes: 3 additions & 1 deletion test/render.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

require('flow-remove-types/register');
require('./stub_loader');
require('../build/flow-remove-types.js');
require = require("@std/esm")(module, true);

const suite = require('./integration').render;
const suiteImplementation = require('./suite_implementation');
Expand Down
13 changes: 13 additions & 0 deletions test/stub_loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Load our stubbed ajax module for the integration suite implementation
const fs = require('fs');
const assert = require('assert');
const pirates = require('pirates');

pirates.addHook((code, filename) => {
assert(filename.endsWith('/ajax.js'));
return fs.readFileSync(`${__dirname}/ajax_stubs.js`, 'utf-8');
}, {
exts: ['.js'],
matcher: filename => filename.endsWith('/ajax.js')
});

101 changes: 9 additions & 92 deletions test/suite_implementation.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
'use strict';

const ajax = require('../src/util/ajax');
const request = require('request');
const PNG = require('pngjs').PNG;
const Map = require('../src/ui/map');
const config = require('../src/util/config');
const window = require('../src/util/window');
const browser = require('../src/util/browser');
const rtlTextPlugin = require('../src/source/rtl_text_plugin');
const rtlText = require('@mapbox/mapbox-gl-rtl-text');
const fs = require('fs');
const path = require('path');
import {PNG} from 'pngjs';
import Map from '../src/ui/map';
import config from '../src/util/config';
import window from '../src/util/window';
import browser from '../src/util/browser';
import {plugin as rtlTextPlugin} from '../src/source/rtl_text_plugin';
import rtlText from '@mapbox/mapbox-gl-rtl-text';
import fs from 'fs';
import path from 'path';

rtlTextPlugin['applyArabicShaping'] = rtlText.applyArabicShaping;
rtlTextPlugin['processBidirectionalText'] = rtlText.processBidirectionalText;
Expand Down Expand Up @@ -150,82 +146,3 @@ module.exports = function(style, options, _callback) {
}
}
};

const cache = {};

function cached(data, callback) {
setImmediate(() => {
callback(null, data);
});
}

ajax.getJSON = function({ url }, callback) {
if (cache[url]) return cached(cache[url], callback);
return request(url, (error, response, body) => {
if (!error && response.statusCode >= 200 && response.statusCode < 300) {
let data;
try {
data = JSON.parse(body);
} catch (err) {
return callback(err);
}
cache[url] = data;
callback(null, data);
} else {
callback(error || new Error(response.statusCode));
}
});
};

ajax.getArrayBuffer = function({ url }, callback) {
if (cache[url]) return cached(cache[url], callback);
return request({ url, encoding: null }, (error, response, body) => {
if (!error && response.statusCode >= 200 && response.statusCode < 300) {
cache[url] = {data: body};
callback(null, {data: body});
} else {
callback(error || new Error(response.statusCode));
}
});
};

ajax.getImage = function({ url }, callback) {
if (cache[url]) return cached(cache[url], callback);
return request({ url, encoding: null }, (error, response, body) => {
if (!error && response.statusCode >= 200 && response.statusCode < 300) {
new PNG().parse(body, (err, png) => {
if (err) return callback(err);
cache[url] = png;
callback(null, png);
});
} else {
callback(error || {status: response.statusCode});
}
});
};

browser.getImageData = function({width, height, data}) {
return {width, height, data: new Uint8Array(data)};
};

// Hack: since node doesn't have any good video codec modules, just grab a png with
// the first frame and fake the video API.
ajax.getVideo = function(urls, callback) {
return request({ url: urls[0], encoding: null }, (error, response, body) => {
if (!error && response.statusCode >= 200 && response.statusCode < 300) {
new PNG().parse(body, (err, png) => {
if (err) return callback(err);
callback(null, {
readyState: 4, // HAVE_ENOUGH_DATA
addEventListener: function() {},
play: function() {},
width: png.width,
height: png.height,
data: png.data
});
});
} else {
callback(error || new Error(response.statusCode));
}
});
};

0 comments on commit 97b2496

Please sign in to comment.