gpii-windows
Version:
Components of the GPII personalization infrastructure for use on Microsoft's "Windows" ™
231 lines (198 loc) • 8.17 kB
JavaScript
/*
* Windows Display Settings Handler Tests
*
* Copyright 2016 Raising the Floor - US
*
* Licensed under the New BSD license. You may not use this file except in
* compliance with this License.
*
* 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");
fluid.registerNamespace("gpii.tests.displaySettings");
require("../../WindowsUtilities/WindowsUtilities.js");
require("../src/displaySettingsHandler.js");
/**
* Tests for the DPI settings.
*/
gpii.tests.displaySettings.dpiTestDefs = fluid.freezeRecursive([
{
// 100%
input: 0,
expected: {
actual: 0,
configured: 0,
minimum: "$minimum",
maximum: "$maximum"
}
},
{
// 125%
input: 1,
expected: {
actual: 1,
configured: 1,
minimum: "$minimum",
maximum: "$maximum"
}
},
{
// < 100%
input: -1,
expected: {
actual: -1,
configured: -1,
minimum: "$minimum",
maximum: "$maximum"
}
},
{
// very low
input: -10,
expected: {
actual: -3,
configured: -3,
minimum: "$minimum",
maximum: "$maximum"
}
},
{
// > max
input: "$overMaximum",
expected: {
actual: "$maximum",
configured: "$overMaximum",
minimum: "$minimum",
maximum: "$maximum"
}
}
]);
/**
* Extracts the test items from model, performing look-ups from it ("$like.this")
*
* @param {Object} model The object to resolve from.
* @param {Object} value The value (or values) to resolve.
* @return {Array} The test items.
*/
gpii.tests.displaySettings.resolveReferences = function (model, value) {
if (typeof(value) === "string") {
return value.charAt(0) === "$" ? fluid.get(model, value.substring(1)) : value;
} else {
return fluid.transform(value, function (obj) {
return gpii.tests.displaySettings.resolveReferences(model, obj);
});
}
};
var teardowns = [];
jqUnit.module("Windows Display Settings Handler Tests", {
teardown: function () {
while (teardowns.length) {
teardowns.pop()();
}
}
});
jqUnit.test("Testing GetScreenResolution", function () {
jqUnit.expect(3);
var screenRes = gpii.windows.display.getScreenResolution();
jqUnit.assertDeepEq("getScreenResolution returns an object", "object", typeof(screenRes));
jqUnit.assertDeepEq("value for width is a number", "number", typeof(screenRes.width));
jqUnit.assertDeepEq("value for height is a number", "number", typeof(screenRes.height));
});
jqUnit.test("Testing setScreenResolution ", function () {
var oldRes = gpii.windows.display.getScreenResolution();
// We can change resolution
// Not such a good unit test as depends on available modes and current screen resolution
jqUnit.expect(2);
var targetRes = { width: 800, height: 600 };
jqUnit.assertTrue("We can call to setScreenResolution, it returns 'true'", gpii.windows.display.setScreenResolution(targetRes));
var newRes = gpii.windows.display.getScreenResolution();
jqUnit.assertDeepEq("New resolution is set", targetRes, newRes);
// Restore old resolution
jqUnit.expect(2);
jqUnit.assertTrue("Resetting to old resolution", gpii.windows.display.setScreenResolution(oldRes));
var restoredRes = gpii.windows.display.getScreenResolution();
jqUnit.assertDeepEq("Old resolution appear to be restored", oldRes, restoredRes);
// test can't change to invalid resolution
var badRes = { width: -123, height: -123 };
jqUnit.expectFrameworkDiagnostic("setScreenResolution fails when receives an invalid resolution such as {w: -123, h: -123}", function () {
gpii.windows.display.setScreenResolution(badRes);
}, "invalid screen resolution");
// test that setScreenResolution fails when it receives a faulty screen resolution
var faulty1 = 2;
jqUnit.expectFrameworkDiagnostic("setScreenResolution fails when receives a non-object parameter", function () {
gpii.windows.display.setScreenResolution(faulty1);
}, "invalid screen resolution");
var faulty2 = { w: 0, h: 1 };
jqUnit.expectFrameworkDiagnostic("setScreenResolution fails when receives a wrong object", function () {
gpii.windows.display.setScreenResolution(faulty2);
}, ["invalid screen resolution"]);
var faulty3 = { width: 0, height: "" };
jqUnit.expectFrameworkDiagnostic("setScreenResolution fails when receives one string as value for height", function () {
gpii.windows.display.setScreenResolution(faulty3);
}, "invalid screen resolution");
});
// This test is only able to test the implementation for the version of Windows it's running on.
jqUnit.test("Testing getScreenDpi ", function () {
var dpi = gpii.windows.display.getScreenDpi();
jqUnit.assertDeepEq("actual DPI is a number", "number", typeof(dpi.actual));
jqUnit.assertDeepEq("configured DPI is a number", "number", typeof(dpi.configured));
jqUnit.assertDeepEq("maximum DPI is a number", "number", typeof(dpi.maximum));
jqUnit.assert("actual DPI should at least 1", dpi.actual >= 1);
jqUnit.assert("actual DPI should not be more than maximum DPI", dpi.actual <= dpi.maximum);
jqUnit.assert("actual DPI should not be more than configured DPI", dpi.actual <= dpi.configured);
});
// This test is only able to test the implementation for the version of Windows it's running on.
jqUnit.asyncTest("Testing setScreenDpi ", function () {
var resolutionPromise;
// Make sure the resolution is good enough to have DPI above 100%. 1024x768 provides up to 125%.
var minRes = {
width: 1024,
height: 768
};
var res = gpii.windows.display.getScreenResolution();
fluid.log("Current resolution:", res);
if (res.width < minRes.width || res.height < minRes.height) {
fluid.log("Increasing resolution for DPI tests.");
teardowns.push(function () {
gpii.windows.display.setScreenResolution(res);
});
gpii.windows.display.setScreenResolution(minRes);
// Wait for the change to be applied.
resolutionPromise = gpii.windows.waitForCondition(function () {
var current = gpii.windows.display.getScreenResolution();
return current.width === minRes.width && current.height === minRes.height;
}, {timeout:10000, dontReject:true});
} else {
resolutionPromise = fluid.promise().resolve();
}
resolutionPromise.then(function (value) {
if (value === "timeout") {
fluid.log("Timed out waiting for resolution change.");
}
// Restore the original setting at the end.
var originalDpi = gpii.windows.display.getScreenDpi();
teardowns.push(function () {
gpii.windows.display.setScreenDpi(originalDpi.configured);
});
fluid.log("Start dpi: ", originalDpi);
// Set it to the current setting
var dpi = gpii.windows.display.setScreenDpi(originalDpi.configured);
var currentDpi = gpii.windows.display.getScreenDpi();
jqUnit.assertDeepEq("get/setScreenDpi should return the same", dpi, currentDpi);
var tests = gpii.tests.displaySettings.resolveReferences({
minimum: originalDpi.minimum,
maximum: originalDpi.maximum,
overMaximum: Math.ceil(originalDpi.maximum + 1)
}, gpii.tests.displaySettings.dpiTestDefs);
for (var index = 0; index < tests.length; index++) {
var testData = tests[index];
var actual = gpii.windows.display.setScreenDpi(testData.input);
jqUnit.assertDeepEq("setScreenDpi " + testData.input, testData.expected, actual);
}
jqUnit.start();
});
});