<commit_before>/*
 * Tests for Windows Language
 *
 * Copyright 2015 Raising the Floor - International
 *
 * Licensed under the New BSD license. You may not use this file except in
 * compliance with this License.
 *
 * The research leading to these results has received funding from the European Union's
 * Seventh Framework Programme (FP7/2007-2013)
 * under grant agreement no. 289016.
 *
 * You may obtain a copy of the License at
 * https://github.com/GPII/universal/blob/master/LICENSE.txt
 */

"use strict";

var fluid = require("gpii-universal");

var jqUnit = fluid.require("node-jqunit");
var gpii = fluid.registerNamespace("gpii");

var ffi = require("ffi-napi");

require("../../../../index.js");
fluid.require("%gpii-windows");

require("../src/language.js");

fluid.registerNamespace("gpii.tests.windows.language");

var teardowns = [];
jqUnit.module("gpii.tests.windows.language", {
    setup: function () {
    },
    teardown: function () {
        while (teardowns.length) {
            teardowns.pop()();
        }
    }
});

// Tests for getLanguageNames();
gpii.tests.windows.language.nameTests = fluid.freezeRecursive({
    "en": {
        "english": "English",
        "local": "English",
        "native": "English"
    },
    "en-US": {
        "english": "English",
        "local": "English (United States)",
        "native": "English (United States)"
    },
    "eN-Gb": {
        "english": "English",
        "local": "English (United Kingdom)",
        "native": "English (United Kingdom)"
    },
    "es-ES": {
        "english": "Spanish",
        "local": "Spanish (Spain)",
        "native": "Español (España)"
    },
    "es-MX": {
        "english": "Spanish",
        "local": "Spanish (Mexico)",
        "native": "Español (México)"
    },
    "fr": {
        "english": "French",
        "local": "French",
        "native": "français"
    },
    "ta-LK": {
        "english": "Tamil",
        "local": "Tamil (Sri Lanka)",
        "native": "தமிழ் (இலங்கை)"
    },

    // Unknown combination
    "zh-DE": {
        "english": "Chinese (zh-DE)",
        "local": "",
        "native": ""
    },
    "en-xyz": {
        "english": "English (en-xyz)",
        "local": "English (en-xyz)",
        "native": ""
    },

    // Valid, but unknown
    "zz-DE": { "english": "", "local": "", "native": "" },
    "zz-AA": { "english": "", "local": "", "native": "" },
    "zz": { "english": "", "local": "", "native": "" },

    // Invalid
    "": { "english": null, "local": null, "native": null },
    1: { "english": null, "local": null, "native": null },
    "-": { "english": null, "local": null, "native": null },
    "a": { "english": null, "local": null, "native": null }
});

var kernel32 = ffi.Library("kernel32", {
    // https://docs.microsoft.com/windows/desktop/api/winnls/nf-winnls-getthreaduilanguage
    "GetThreadUILanguage": [
        "uint16", []
    ]
});

// If the GPII_IGNORE_LANGTEST environment variable is set, then ignore the test results that would be affected by the
// current language (but still run the code).
gpii.tests.windows.language.ignoreResults = !!process.env.GPII_IGNORE_LANGTEST;
if (gpii.tests.windows.language.ignoreResults) {
    fluid.log("SOFT-FAIL: GPII_IGNORE_LANGTEST is set - ignoring the results.");
}

jqUnit.test("Checking current language", function () {
    jqUnit.expect(gpii.tests.windows.language.ignoreResults ? 0 : 1);

    // Windows language identifier for en-US
    var englishUS = 1033;

    if (!gpii.tests.windows.language.ignoreResults) {
        // The tests expect the current language to be en-US. Fail in an obvious manner before attempting the tests.
        // Those brave enough to run the tests on a non-vagrant box are free to ignore these tests.
        jqUnit.assertEquals("The current language must be English (US) [en-US]. " +
            "Set GPII_IGNORE_LANGTEST to ignore the language failures.", englishUS, kernel32.GetThreadUILanguage());
    }
});

jqUnit.test("Test getting language names", function () {
    var tests = gpii.tests.windows.language.nameTests;

    jqUnit.expect(gpii.tests.windows.language.ignoreResults ? 0 : Object.keys(tests).length);
    var language = gpii.windows.language();

    fluid.each(tests, function (expect, code) {
        var result = language.getLanguageNames(code);
        if (!gpii.tests.windows.language.ignoreResults) {
            jqUnit.assertDeepEq("Language names for '" + code + "' should match the expected", expect, result);
        }
    });

    language.destroy();
});

jqUnit.test("Test getting installed languages", function () {
    jqUnit.expect(4);

    // The expected value for the en-US field of the installed languages.
    var expect = {
        english: "English",
        local: "English (United States)",
        native: "English (United States)",
        code: "en-US",
        current: true,
        order: 0
    };

    var language = gpii.windows.language();

    // Check the model update
    language.model.installedLanguages = {};
    language.applier.modelChanged.addListener("installedLanguages", function (newValue) {
        if (gpii.tests.windows.language.ignoreResults) {
            jqUnit.assert("installedLanguages changed");
        } else {
            jqUnit.assertDeepEq("Model ", expect, newValue["en-US"]);
        }
    });

    var result = language.getInstalledLanguages();

    jqUnit.assertTrue("getInstalledLanguages should return an object", fluid.isPlainObject(result, true));
    jqUnit.assertTrue("getInstalledLanguages result should have fields", Object.keys(result).length > 0);

    if (!gpii.tests.windows.language.ignoreResults) {
        var enUS = result["en-US"];
        jqUnit.assertDeepEq("getInstalledLanguages should return en-US", expect, enUS);
    }
    language.destroy();
});
<commit_msg>GPII-3544: Fixed test for CI<commit_after>/*
 * Tests for Windows Language
 *
 * Copyright 2015 Raising the Floor - International
 *
 * Licensed under the New BSD license. You may not use this file except in
 * compliance with this License.
 *
 * The research leading to these results has received funding from the European Union's
 * Seventh Framework Programme (FP7/2007-2013)
 * under grant agreement no. 289016.
 *
 * You may obtain a copy of the License at
 * https://github.com/GPII/universal/blob/master/LICENSE.txt
 */

"use strict";

var fluid = require("gpii-universal");

var jqUnit = fluid.require("node-jqunit");
var gpii = fluid.registerNamespace("gpii");

var ffi = require("ffi-napi");

require("../../../../index.js");
fluid.require("%gpii-windows");

require("../src/language.js");

fluid.registerNamespace("gpii.tests.windows.language");

var teardowns = [];
jqUnit.module("gpii.tests.windows.language", {
    setup: function () {
    },
    teardown: function () {
        while (teardowns.length) {
            teardowns.pop()();
        }
    }
});

// Tests for getLanguageNames();
gpii.tests.windows.language.nameTests = fluid.freezeRecursive({
    "en": {
        "english": "English",
        "local": "English",
        "native": "English"
    },
    "en-US": {
        "english": "English",
        "local": "English (United States)",
        "native": "English (United States)"
    },
    "eN-Gb": {
        "english": "English",
        "local": "English (United Kingdom)",
        "native": "English (United Kingdom)"
    },
    "es-ES": {
        "english": "Spanish",
        "local": "Spanish (Spain)",
        "native": "Español (España)"
    },
    "es-MX": {
        "english": "Spanish",
        "local": "Spanish (Mexico)",
        "native": "Español (México)"
    },
    "fr": {
        "english": "French",
        "local": "French",
        "native": "français"
    },
    "ta-LK": {
        "english": "Tamil",
        "local": "Tamil (Sri Lanka)",
        "native": "தமிழ் (இலங்கை)"
    },

    // Unknown combination
    "zh-DE": {
        "english": "Chinese (zh-DE)",
        "local": "",
        "native": ""
    },
    "en-xyz": {
        "english": "English (en-xyz)",
        "local": "English (en-xyz)",
        "native": ""
    },

    // Valid, but unknown
    "zz-DE": { "english": "", "local": "", "native": "" },
    "zz-AA": { "english": "", "local": "", "native": "" },
    "zz": { "english": "", "local": "", "native": "" },

    // Invalid
    "": { "english": null, "local": null, "native": null },
    1: { "english": null, "local": null, "native": null },
    "-": { "english": null, "local": null, "native": null },
    "a": { "english": null, "local": null, "native": null }
});

var kernel32 = ffi.Library("kernel32", {
    // https://docs.microsoft.com/windows/desktop/api/winnls/nf-winnls-getthreaduilanguage
    "GetThreadUILanguage": [
        "uint16", []
    ]
});

// If the GPII_IGNORE_LANGTEST environment variable is set, then ignore the test results that would be affected by the
// current language (but still run the code).
gpii.tests.windows.language.ignoreResults = !!process.env.GPII_IGNORE_LANGTEST;
if (gpii.tests.windows.language.ignoreResults) {
    fluid.log("SOFT-FAIL: GPII_IGNORE_LANGTEST is set - ignoring the results.");
}

jqUnit.test("Checking current language", function () {
    jqUnit.expect(gpii.tests.windows.language.ignoreResults ? 0 : 1);

    // Windows language identifier for en-US
    var englishUS = 1033;

    if (!gpii.tests.windows.language.ignoreResults) {
        // The tests expect the current language to be en-US. Fail in an obvious manner before attempting the tests.
        // Those brave enough to run the tests on a non-vagrant box are free to ignore these tests.
        jqUnit.assertEquals("The current language must be English (US) [en-US]. " +
            "Set GPII_IGNORE_LANGTEST to ignore the language failures.", englishUS, kernel32.GetThreadUILanguage());
    }
});

jqUnit.test("Test getting language names", function () {
    var tests = gpii.tests.windows.language.nameTests;

    jqUnit.expect(gpii.tests.windows.language.ignoreResults ? 0 : Object.keys(tests).length);
    var language = gpii.windows.language();

    fluid.each(tests, function (expect, code) {
        var result = language.getLanguageNames(code);
        if (!gpii.tests.windows.language.ignoreResults) {
            jqUnit.assertDeepEq("Language names for '" + code + "' should match the expected", expect, result);
        }
    });

    language.destroy();
});

jqUnit.test("Test getting installed languages", function () {
    jqUnit.expect(4);

    var currentLanguage = gpii.windows.language.getDisplayLanguage();
    fluid.log("currentLanguage:" + currentLanguage);
    jqUnit.assertNotNull("getDisplayLanguage should return something", currentLanguage);


    // The expected value for the en-US field of the installed languages.
    var expect = {
        english: "English",
        local: "English (United States)",
        native: "English (United States)",
        code: "en-US",
        current: currentLanguage === "en-US",
        order: 0
    };

    var language = gpii.windows.language();

    // Check the model update
    language.model.installedLanguages = {};
    language.applier.modelChanged.addListener("installedLanguages", function (newValue) {
        if (gpii.tests.windows.language.ignoreResults) {
            jqUnit.assert("installedLanguages changed");
        } else {
            jqUnit.assertDeepEq("Model ", expect, newValue["en-US"]);
        }
    });

    var result = language.getInstalledLanguages();

    jqUnit.assertTrue("getInstalledLanguages should return an object", fluid.isPlainObject(result, true));
    jqUnit.assertTrue("getInstalledLanguages result should have fields", Object.keys(result).length > 0);

    if (!gpii.tests.windows.language.ignoreResults) {
        var enUS = result["en-US"];
        jqUnit.assertDeepEq("getInstalledLanguages should return en-US", expect, enUS);
    }
    language.destroy();
});

{'fraction_non_alphanumeric': 0.15983193277310925, 'fraction_numerical': 0.0067226890756302525, 'mean_word_length': 2.8740234375, 'pattern_counts': {'":': 128, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6342509', 'n_tokens_mistral': 3814, 'n_tokens_neox': 3624, 'n_words': 903}