-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update integration test suite implementation
- Loading branch information
Anand Thakker
committed
Mar 12, 2018
1 parent
835a9a9
commit 97b2496
Showing
6 changed files
with
137 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters