forked from OpenGeoscience/geojs
-
Notifications
You must be signed in to change notification settings - Fork 5
/
karma-base.js
370 lines (359 loc) · 12.8 KB
/
karma-base.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
var webpack_config = require('./webpack.base.config');
var fs = require('fs');
var path = require('path');
var image_path = process.env.TEST_IMAGE_PATH || path.resolve('_build/images');
var test_case = process.env.GEOJS_TEST_CASE || 'tests/all.js';
var getRawBody = require('raw-body');
// Create the images directory, if it doesn't exist.
if (!fs.existsSync(image_path)) {
fs.mkdirSync(image_path, {recursive: true});
}
/**
* This function returns true if (1) there is an environment variable set
* called "TEST_SAVE_IMAGE", and either (2a) the name of the test appears in
* the value of the variable when treated as a comma separated list of strings,
* or (2b) the variable reads "all".
*/
function doSaveImage(name) {
var saveList = process.env.TEST_SAVE_IMAGE;
var result;
if (saveList === 'all' || !saveList) {
result = !!saveList;
} else {
result = saveList.split(',').indexOf(name) >= 0;
if (name.indexOf('-') >= 0) {
result = result || saveList.split(',').indexOf(name.split('-')[1]) >= 0;
}
}
return result;
}
/* Save an image. The image is expected to be a base64 encoded string, with or
* without a mime type specifier.
*
* @param {string} name: base name for the image.
* @param {string} image: a base64 encoded png image.
* @param {boolean} always: if true, always save the image regardless of
* environment settings.
*/
function saveImage(name, image, always) {
if (always || doSaveImage(name)) {
if (image.indexOf(',') >= 0) {
image = image.split(',')[1];
}
var dest = path.resolve(image_path, name + '.png');
fs.writeFileSync(dest, image, 'base64');
}
}
/* Use ImageMagick's import tool to get a portion of the screen. The caller is
* responsible for identifying the useful portion of the screen.
*
* @param {string} name: base name for the image.
* @param {number} left: left screen coordinate
* @param {number} top: top screen coordinate
* @param {number} width: width in pixels of area to fetch.
* @param {number} height: height in pixels of area to fetch.
* @returns: a base64-encoded image.
*/
function getScreenImage(name, left, top, width, height) {
var child_process = require('child_process');
var dest = path.resolve(image_path, name + '-screen.png');
child_process.execSync(
'import -window root ' +
'-crop ' + width + 'x' + height + (left >= 0 ? '+' : '') + left +
(top >= 0 ? '+' : '') + top + ' +repage ' +
'\'' + dest.replace(/'/g, "'\\''") + '\'');
var xvfbImage = Buffer.from(fs.readFileSync(dest)).toString('base64');
xvfbImage = 'data:image/png;base64,' + xvfbImage;
return xvfbImage;
}
/* Compare an image to a base image. If it violates a threshold, save the
* image and a diff between it and the base image. Returns the resemble
* results.
*
* @param {string} name: base name for the image.
* @param {string} image: a base64 encoded png image.
* @param {number} threshold: allowed difference between this image and the
* base image.
* @param {function} callback: a function to call when complete.
*/
function compareImage(name, image, threshold, callback) {
var resemble = require('resemblejs');
var src = path.resolve('dist/data/base-images', name + '.png');
if (!fs.existsSync(src)) {
src = path.resolve(image_path, name + '.png');
}
var refImage = Buffer.from(fs.readFileSync(src)).toString('base64');
refImage = 'data:image/png;base64,' + refImage;
resemble(image)
.compareTo(refImage)
.ignoreAntialiasing()
.onComplete(function (results) {
console.log('Image comparison: ' + name + ', delta: ' +
Number(results.misMatchPercentage) * 0.01);
var passed = (Number(results.misMatchPercentage) <= threshold * 100);
saveImage(name + '-base', refImage, !passed);
saveImage(name + '-test', image, !passed);
saveImage(name + '-diff', results.getImageDataUrl(), !passed);
results.passed = passed;
if (callback) {
callback(results);
}
});
}
/**
* Express style middleware to handle REST requests to `/testImage` on the test
* server.
*/
var testimage_middleware = function (config) {
return function (request, response, next) {
const requestURL = new URL(request.url, 'http://nowhere.com');
const parsed = {
pathname: requestURL.pathname,
query: Object.fromEntries(requestURL.searchParams)
};
var query = (parsed.query || {});
if (parsed.pathname === '/testImage') {
if (request.method === 'PUT') {
return getRawBody(request).then(function (body) {
var name = query.name;
var image;
if (query.screen === 'true') {
image = getScreenImage(name, query.left, query.top,
query.width, query.height);
} else {
image = '' + body;
}
saveImage(name, image);
if (query.compare === 'true') {
compareImage(name, image, query.threshold, function (results) {
response.writeHead(200);
return response.end(JSON.stringify(results));
});
} else {
response.writeHead(200);
return response.end('{}');
}
}).catch(function (err) {
response.writeHead(500);
response.end(err.message);
});
} else if (request.method === 'GET') {
var src = path.resolve(image_path, query.name + '.png');
var img = Buffer.from(fs.readFileSync(src)).toString('base64');
img = 'data:image/png;base64,' + img;
response.writeHead(200);
return response.end(img);
}
}
next();
};
};
/**
* Express style middleware to handle REST requests for OSM tiles on the test
* server.
*/
var osmtiles_middleware = function (config) {
return function (request, response, next) {
var match = request.url.match(/.*https?:\/\/([a-c]\.tile.openstreetmap.org|.*-[a-d]\.a\.ssl\.fastly\.net\/[a-z-]+)\/([0-9]+\/[0-9]+\/[0-9]+.png)$/);
/* Serve tiles if they have been proxied */
if (match && request.method === 'GET') {
var imagePath = 'dist/data/tiles/' + match[2];
var img = Buffer.from(fs.readFileSync(imagePath));
response.setHeader('Content-Type', 'image/png');
response.setHeader('Content-Length', img.length);
response.setHeader('Access-Control-Allow-Origin', '*');
response.writeHead(200);
return response.end(img);
}
next();
};
};
/* Reduce the number of external connections Chrome makes. */
var ChromeFlags = [
'--no-sandbox', // necessary to run tests in a docker
'--no-pings', // no auditing pings
'--force-color-profile=srgb', // for consistent tests
'--disable-background-networking',
'--disable-component-extensions-with-background-pages',
'--translate-script-url=""'
];
/* By default, when Firefox starts it makes many connections to sites like
* mozilla.org, yahoo.com, google.com, etc. This limits these connections (but
* doesn't eliminate them completely, especially when accessing the 2d canvas).
* See
* https://support.mozilla.org/en-US/kb/how-stop-firefox-making-automatic-connections
*/
var FirefoxPrefs = {
'browser.aboutHomeSnippets.updateUrl': '',
'browser.casting.enabled': false,
'browser.library.activity-stream.enabled': false,
'browser.newtabpage.activity-stream.enabled': false,
'browser.search.geoip.url': '',
'browser.selfsupport.enabled': false,
'browser.selfsupport.url': '',
'browser.startup.homepage_override.mstone': 'ignore',
'extensions.getAddons.cache.enabled': false,
'extensions.pocket.enabled': false,
'extensions.update.enabled': false,
'network.captive-portal-service.enabled': false,
'network.dns.disablePrefetch': true,
'network.http.speculative-parallel-limit': 0,
'network.prefetch-next': false,
// these further limit firefox connections
'browser.safebrowsing.provider.mozilla.gethashURL': '',
'browser.safebrowsing.provider.mozilla.updateURL': '',
'browser.safebrowsing.provider.google.gethashURL': '',
'browser.safebrowsing.provider.google.updateURL': '',
'browser.safebrowsing.provider.google4.dataSharingURL': '',
'browser.safebrowsing.provider.google4.gethashURL': '',
'browser.safebrowsing.provider.google4.updateURL': '',
'datareporting.healthreport.uploadEnabled': false,
'datareporting.policy.dataSubmissionEnabled': false,
'media.gmp-gmpopenh264.autoupdate': false,
'media.gmp-manager.url': ''
};
/* If webpack of a test fails, stop rather than run some tests */
class KarmaWarningsToErrorsWebpackPlugin {
apply(compiler) {
compiler.hooks.done.tap('KarmaWarningsToErrorsWebpackPlugin', (stats) => {
if (stats.compilation.warnings.length) {
// Log each of the warnings
stats.compilation.warnings.forEach(function (warning) {
console.log(warning.message || warning);
});
// Pretend no assets were generated. This prevents the tests
// from running making it clear that there were warnings.
stats.stats = [{
toJson: function () {
return this;
},
assets: []
}];
}
});
}
}
module.exports = function (config) {
/* If webpack of a test fails, stop rather than run some tests */
webpack_config.plugins.push(new KarmaWarningsToErrorsWebpackPlugin());
var newConfig = {
autoWatch: false,
files: [
test_case,
{pattern: 'tests/data/**/*', included: false},
{pattern: 'tests/cases/**/*.js', included: false, served: false, watched: true},
{pattern: 'tests/gl-cases/**/*.js', included: false, served: false, watched: true},
{pattern: 'tests/headed-cases/**/*.js', included: false, served: false, watched: true},
{pattern: 'dist/data/**/*', included: false},
{pattern: 'dist/examples/**/*', included: false},
{pattern: 'dist/tutorials/**/*', included: false},
{pattern: 'dist/built/**/*', included: false}
],
proxies: {
'/testdata/': '/base/tests/data/',
'/data/': '/base/dist/data/',
'/examples/': '/base/dist/examples/',
'/tutorials/': '/base/dist/tutorials/',
'/built/': '/base/dist/built/'
},
browsers: [
'ChromeHeadlessTouch'
],
customLaunchers: {
ChromeHeadlessTouch: {
base: 'ChromeHeadless',
flags: ChromeFlags.concat([
'--touch-events'
])
},
ChromeFull: {
base: 'Chrome',
flags: ChromeFlags.concat([
'--device-scale-factor=1',
'--window-position=0,0',
'--start-fullscreen',
'--kiosk',
'--incognito'
])
},
ChromeWithProxy: {
// inheriting from ChromeFull ignores the flags in this entry, so we
// need to inherit from Chrome
base: 'Chrome',
flags: ChromeFlags.concat([
'--device-scale-factor=1',
'--window-position=0,0',
'--start-fullscreen',
'--kiosk',
'--incognito',
'--proxy-pac-url=' + config.protocol + '//' + (config.hostname || '127.0.0.1') + ':' + config.port + '/testdata/proxy-for-tests.pac'
])
},
FirefoxHeadlessTouch: {
base: 'FirefoxHeadless',
prefs: Object.assign({
// enable touch
'dom.w3c_touch_events.enabled': 1
}, FirefoxPrefs)
},
FirefoxWithProxy: {
base: 'Firefox',
prefs: Object.assign({
// enable proxy
'network.proxy.type': 2,
'network.proxy.autoconfig_url': config.protocol + '//' + (config.hostname || '127.0.0.1') + ':' + config.port + '/testdata/proxy-for-tests.pac',
// enable touch
'dom.w3c_touch_events.enabled': 1
}, FirefoxPrefs)
}
},
browserDisconnectTimeout: 30000,
browserDisconnectTolerance: 3,
browserNoActivityTimeout: 300000,
reporters: [
'spec', // we had used the 'progress' reporter in the past.
'kjhtml'
],
/* enable for testing */
// logLevel: config.LOG_DEBUG,
/* We could suppress passing results */
// specReporter = {suppressPassed: true, suppressSkipped: true},
middleware: [
'testimage',
'osmtiles'
],
plugins: [
{'middleware:testimage': ['factory', testimage_middleware]},
{'middleware:osmtiles': ['factory', osmtiles_middleware]},
'karma-*'
],
preprocessors: {},
frameworks: [
'jasmine', 'sinon', 'webpack'
],
client: {
jasmine: {
random: false,
timeoutInterval: 30000
}
},
webpack: {
mode: 'development',
performance: {hints: false},
cache: true,
devtool: 'inline-source-map',
module: {
rules: webpack_config.module.rules
},
resolve: webpack_config.resolve,
plugins: webpack_config.plugins
},
webpackMiddleware: {
stats: 'errors-only'
}
};
/* Suppress a babel warning */
newConfig.webpack.module.rules[0].use[0].options.compact = false;
newConfig.preprocessors[test_case] = ['webpack', 'sourcemap'];
return newConfig;
};