summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKRenderTest/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'platform/android/MapboxGLAndroidSDKRenderTest/scripts')
-rw-r--r--platform/android/MapboxGLAndroidSDKRenderTest/scripts/definitions/globals.js37
-rw-r--r--platform/android/MapboxGLAndroidSDKRenderTest/scripts/generators.js17
-rw-r--r--platform/android/MapboxGLAndroidSDKRenderTest/scripts/generators/java.js126
-rw-r--r--platform/android/MapboxGLAndroidSDKRenderTest/scripts/index.js2
-rw-r--r--platform/android/MapboxGLAndroidSDKRenderTest/scripts/main.js18
-rw-r--r--platform/android/MapboxGLAndroidSDKRenderTest/scripts/pixelmatch.js18
6 files changed, 218 insertions, 0 deletions
diff --git a/platform/android/MapboxGLAndroidSDKRenderTest/scripts/definitions/globals.js b/platform/android/MapboxGLAndroidSDKRenderTest/scripts/definitions/globals.js
new file mode 100644
index 0000000000..6b215ea84d
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDKRenderTest/scripts/definitions/globals.js
@@ -0,0 +1,37 @@
+import fs from 'fs';
+import path from 'path';
+
+// Write out a list of files that this script is modifying so that we can check
+var files = [];
+process.on('exit', function() {
+ const list = path.join(path.dirname(process.argv[1]), path.basename(process.argv[1], '.js') + '.list');
+ fs.writeFileSync(list, files.join('\n'));
+});
+
+export function writeIfModified(filename, newContent) {
+ ensureDirectoryExistence(filename);
+ files.push(filename);
+ try {
+ const oldContent = fs.readFileSync(filename, 'utf8');
+ if (oldContent == newContent) {
+ console.warn(`* Skipping file '${filename}' because it is up-to-date`);
+ return;
+ }
+ } catch(err) {
+ console.log(err);
+ }
+
+ if (['0', 'false'].indexOf(process.env.DRY_RUN || '0') !== -1) {
+ fs.writeFileSync(filename, newContent);
+ }
+ console.warn(`* Updating outdated file '${filename}'`);
+}
+
+function ensureDirectoryExistence(filePath) {
+ var dirname = path.dirname(filePath);
+ if (fs.existsSync(dirname)) {
+ return true;
+ }
+ ensureDirectoryExistence(dirname);
+ fs.mkdirSync(dirname);
+} \ No newline at end of file
diff --git a/platform/android/MapboxGLAndroidSDKRenderTest/scripts/generators.js b/platform/android/MapboxGLAndroidSDKRenderTest/scripts/generators.js
new file mode 100644
index 0000000000..f66aa2e332
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDKRenderTest/scripts/generators.js
@@ -0,0 +1,17 @@
+import java from './generators/java';
+
+export function generators(type) {
+ let fs = require('fs');
+
+ console.log('Generating tests for '+ type + ':');
+ var api = {
+ 'operations' : JSON.parse(fs.readFileSync('config/supported-operations.json', 'utf8')),
+ 'properties' : JSON.parse(fs.readFileSync('config/supported-properties.json', 'utf8')),
+ 'java-ignores' : JSON.parse(fs.readFileSync('config/ignores.json', 'utf8')),
+ 'node-ignores' : JSON.parse(fs.readFileSync('../../node/test/ignores.json', 'utf8'))
+ };
+ if (type === 'android') {
+ java(api, type);
+ }
+ console.log('');
+}
diff --git a/platform/android/MapboxGLAndroidSDKRenderTest/scripts/generators/java.js b/platform/android/MapboxGLAndroidSDKRenderTest/scripts/generators/java.js
new file mode 100644
index 0000000000..285bdbbee8
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDKRenderTest/scripts/generators/java.js
@@ -0,0 +1,126 @@
+import { writeIfModified } from '../definitions/globals.js';
+
+let fs = require('fs');
+let ejs = require('ejs');
+let path = require('path');
+
+var totalCount = 0;
+var javaIgnoreCount = 0;
+var nodeIgnoreCount = 0;
+var relativePath = '';
+var pathToFile = '';
+
+export default function(api, type) {
+ // Copy gl-js integration test folder to android assets
+ copyFolderSync('../../../mapbox-gl-js/test/integration/', 'src/main/assets/', 'node_modules');
+
+ // Walk through render-test directory and generate tests
+ recursiveDirectoryWalk('../../../mapbox-gl-js/test/integration/render-tests/', type, api['java-ignores'], api['node-ignores']);
+
+ // Print code gen result output
+ console.log('\nFinished generating render tests for ' + type + ': \ngenerated: ' + totalCount + '\njava-ignored: ' + javaIgnoreCount+ '\nnode-ignored: ' + nodeIgnoreCount);
+ return {};
+}
+
+function recursiveDirectoryWalk(dir, type, javaIgnores, nodeIgnores) {
+ let testSubDir = "test/integration/render-tests/";
+ const files = fs.readdirSync(dir);
+ for (const file of files) {
+ pathToFile = path.join(dir, file);
+ const isDirectory = fs.statSync(pathToFile).isDirectory();
+ if (isDirectory) {
+ recursiveDirectoryWalk(pathToFile, type, javaIgnores, nodeIgnores);
+ } else if (file.endsWith('style.json')) {
+ relativePath = pathToFile.split(testSubDir)[1].slice(0, -11);
+ let testPath = 'render-tests/' + relativePath;
+ if (javaIgnores.hasOwnProperty(testPath)) {
+ // This test is ignored due to the ignores files for android
+ javaIgnoreCount++;
+ continue;
+ }
+
+ if (nodeIgnores.hasOwnProperty(testPath)) {
+ // This test is ignored due to the ignores files for node
+ nodeIgnoreCount++;
+ continue;
+ }
+
+ // TODO add ignore for unsupported operations and properties
+
+ totalCount++;
+
+ generateTestFiles(dir);
+ }
+ }
+}
+
+function generateTestFiles(dir) {
+ // read style definition, optimise test metadata
+ const style_json = JSON.parse(fs.readFileSync(pathToFile, 'utf8'));
+ const test_metadata = createTestMetadata(style_json, pathToFile, relativePath);
+
+ // generate test
+ const template = ejs.compile(fs.readFileSync('templates/render-test.java.ejs', 'utf8'), {strict: true});
+ writeIfModified('src/androidTest/java/com/mapbox/mapboxsdk/test/render/' + test_metadata['test']['package'] + '/'+ test_metadata['test']['name'] + '.java', template(test_metadata));
+}
+
+function createTestMetadata(style_json) {
+ var test_metadata = style_json['metadata'];
+
+ // ensure metadata availability
+ if (!test_metadata) {
+ test_metadata = {};
+ }
+ if (!test_metadata['test']) {
+ test_metadata['test'] = {};
+ }
+
+ // optimize test data for java language conventions
+ const pathElements = santizePathForJavaConventions(pathToFile).split('/');
+ const javaPath = pathElements[pathElements.length - 3].replace(/-/g,'_');
+ test_metadata['test']['package'] = javaPath;
+ test_metadata['test']['asset_path'] = 'render-tests/' + relativePath;
+ test_metadata['test']['result'] = relativePath;
+ var testName = camelize(relativePath.split("/")[1]).replace("-","");
+ test_metadata['test']['name'] = testName;
+ return test_metadata;
+}
+
+function copyFolderSync(from, to, ignoreDir) {
+ try {
+ ensureDirSync(to)
+ } catch (err) {
+ }
+ fs.readdirSync(from).forEach(element => {
+ if (fs.lstatSync(path.join(from, element)).isFile()) {
+ fs.copyFileSync(path.join(from, element), path.join(to, element));
+ } else {
+ if (!from.endsWith(ignoreDir)) {
+ copyFolderSync(path.join(from, element), path.join(to, element), ignoreDir);
+ }
+ }
+ });
+}
+
+function ensureDirSync (dirpath) {
+ try {
+ fs.mkdirSync(dirpath, { recursive: true })
+ } catch (err) {
+ if (err.code !== 'EEXIST') throw err
+ }
+}
+
+global.camelize = function (str) {
+ return str.replace(/(?:^|-)(.)/g, function (_, x) {
+ return x.toUpperCase();
+ });
+}
+
+global.santizePathForJavaConventions = function (str) {
+ return str.replace(/\/$/, '')
+ .replace('default','defaux')
+ .replace('#',"_")
+ .replace('@','at')
+ .replace('false', 'faux');
+}
+
diff --git a/platform/android/MapboxGLAndroidSDKRenderTest/scripts/index.js b/platform/android/MapboxGLAndroidSDKRenderTest/scripts/index.js
new file mode 100644
index 0000000000..e17577982e
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDKRenderTest/scripts/index.js
@@ -0,0 +1,2 @@
+require = require("esm")(module)
+module.exports = require("./src/main.js)") \ No newline at end of file
diff --git a/platform/android/MapboxGLAndroidSDKRenderTest/scripts/main.js b/platform/android/MapboxGLAndroidSDKRenderTest/scripts/main.js
new file mode 100644
index 0000000000..293a5305a9
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDKRenderTest/scripts/main.js
@@ -0,0 +1,18 @@
+import yargs from 'yargs';
+import { generators } from './generators';
+
+function parser(language) {
+ generators(language)
+}
+
+function builder(yargs) {
+ return yargs.boolean('list');
+}
+
+yargs
+ .command('[android]', 'generate Android render tests', builder, parser('android'))
+ .parse();
+
+
+
+
diff --git a/platform/android/MapboxGLAndroidSDKRenderTest/scripts/pixelmatch.js b/platform/android/MapboxGLAndroidSDKRenderTest/scripts/pixelmatch.js
new file mode 100644
index 0000000000..21758b99b2
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDKRenderTest/scripts/pixelmatch.js
@@ -0,0 +1,18 @@
+// TODO: support multiple test cases
+var testCase = 'background-color/function'
+
+var fs = require('fs'),
+ PNG = require('pngjs').PNG,
+ pixelmatch = require('pixelmatch');
+
+var img1 = fs.createReadStream('build/outputs/' + testCase + '/expected.png').pipe(new PNG()).on('parsed', doneReading),
+ img2 = fs.createReadStream('build/outputs/' + testCase + '/actual.png').pipe(new PNG()).on('parsed', doneReading),
+ filesRead = 0;
+
+function doneReading() {
+ if (++filesRead < 2) return;
+ var diff = new PNG({width: img1.width, height: img1.height});
+ var numberMismatchPixels = pixelmatch(img1.data, img2.data, diff.data, img1.width, img1.height, {threshold: 0.1});
+ console.log("PixelMatching: " + testCase + ": " + numberMismatchPixels + " mismatched pixels.");
+ diff.pack().pipe(fs.createWriteStream('build/outputs/' + testCase + '/diff.png'));
+} \ No newline at end of file