summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKRenderTest/scripts/generators/java.js
blob: 285bdbbee8f667e30971d351921ff933eb2ba19a (plain)
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
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');
}