gpii-windows
Version:
Components of the GPII personalization infrastructure for use on Microsoft's "Windows" ™
470 lines (438 loc) • 15 kB
JavaScript
/*
* Tests for the SystemSettingsHandler
*
* 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");
require("../src/systemSettingsHandler.js");
fluid.registerNamespace("gpii.tests.windows.systemSettingsHandler");
var teardowns = [];
jqUnit.module("gpii.tests.windows.systemSettingsHandler", {
setup: function () {
},
teardown: function () {
while (teardowns.length) {
teardowns.pop()();
}
}
});
gpii.tests.FALSEY = fluid.makeMarker("FALSEY");
gpii.tests.windows.systemSettingsHandler.executeHelperTests = fluid.freezeRecursive([
{
input: [],
expect: []
},
{
input: [{}],
expect: [{isError: true}]
},
{
input: [{}, {}],
expect: [{isError: true}, {isError: true}]
},
{
input: [{"badField": "hi"}],
expect: [{isError: true}]
},
{
input: [{"settingID": null}],
expect: [{isError: true}]
},
{
input: [{"settingID": "SystemSettings_Accessibility_Keyboard_IsUnderlineShortcutEnabled"}],
expect: [{isError: true}]
},
{
input: [{"method": "unknownMethod"}],
expect: [{isError: true}]
},
{
input: [{"settingID": "unknownValue", "method": "unknownMethod"}],
expect: [{isError: true}]
},
{
input: [
{"settingID": "SystemSettings_Accessibility_Keyboard_WarningEnabled", "method": "badMethod"}
],
expect: [{isError: true}]
},
{
// Get the value.
input: [
{
"settingID": "SystemSettings_Accessibility_Keyboard_WarningEnabled",
"method": "GetValue",
"async": true
}
],
expect: [{returnValue: false, isError: gpii.tests.FALSEY}]
},
{
// Set the value, returns the previous value.
input: [
{
"settingID": "SystemSettings_Accessibility_Keyboard_WarningEnabled",
"method": "SetValue",
"parameters": [true],
"async": true
}
],
expect: [{returnValue: false, isError: gpii.tests.FALSEY}]
},
{
// Get the new value.
input: [
{
"settingID": "SystemSettings_Accessibility_Keyboard_WarningEnabled",
"method": "GetValue",
"async": true
}
],
expect: [{returnValue: true, isError: gpii.tests.FALSEY}]
},
{
// Set the value back.
input: [
{
"settingID": "SystemSettings_Accessibility_Keyboard_WarningEnabled",
"method": "SetValue",
"parameters": [false],
"async": true
}
],
expect: [{returnValue: true, isError: gpii.tests.FALSEY}]
},
{
// Get the value.
input: [
{
"settingID": "SystemSettings_Accessibility_Keyboard_WarningEnabled",
"method": "GetValue",
"async": true
}
],
expect: [{returnValue: false, isError: gpii.tests.FALSEY}]
},
{
// Set it to the same value.
input: [
{
"settingID": "SystemSettings_Accessibility_Keyboard_WarningEnabled",
"method": "SetValue",
"parameters": [false],
"async": true
}
],
expect: [{returnValue: false, isError: gpii.tests.FALSEY}]
},
{
// Get the value.
input: [
{
"settingID": "SystemSettings_Accessibility_Keyboard_WarningEnabled",
"method": "GetValue",
"async": true
}
],
expect: [{returnValue: false, isError: gpii.tests.FALSEY}]
}
]);
gpii.tests.windows.systemSettingsHandler.apiTests = fluid.freezeRecursive([
{
func: "get",
input: {
"test.windows.systemSettingsHandler": [{
settings: {
"SystemSettings_Accessibility_Keyboard_WarningEnabled": {}
},
options: {
Async: true
}
}]
},
expect: {
"test.windows.systemSettingsHandler": [{
settings: {
"SystemSettings_Accessibility_Keyboard_WarningEnabled": {
"value": false
}
}
}]
}
},
{
func: "set",
input: {
"test.windows.systemSettingsHandler": [{
settings: {
"SystemSettings_Accessibility_Keyboard_WarningEnabled": {
"value": true
}
},
options: {
Async: true
}
}]
},
expect: {
"test.windows.systemSettingsHandler": [{
settings: {
"SystemSettings_Accessibility_Keyboard_WarningEnabled": {
"oldValue": {
"value": false
},
"newValue": {
"value": true
}
}
}
}]
}
},
{
func: "get",
input: {
"test.windows.systemSettingsHandler": [{
settings: {
"SystemSettings_Accessibility_Keyboard_WarningEnabled": {},
"SystemSettings_Taskbar_Location": {},
"SystemSettings_Input_Touch_SetActivationTimeout": {}
},
options: {
Async: true
}
}]
},
expect: {
"test.windows.systemSettingsHandler": [{
settings: {
"SystemSettings_Accessibility_Keyboard_WarningEnabled": {
"value": true
},
"SystemSettings_Taskbar_Location": {
"value": 3
},
"SystemSettings_Input_Touch_SetActivationTimeout": {
"value": "Medium sensitivity"
}
}
}]
}
},
{
func: "set",
input: {
"test.windows.systemSettingsHandler": [{
settings: {
"SystemSettings_Accessibility_Keyboard_WarningEnabled": {
"value": false
},
"SystemSettings_Taskbar_Location": {
"value": 3
},
"SystemSettings_Input_Touch_SetActivationTimeout": {
"value": "Low sensitivity"
}
},
options: {
Async: true
}
}]
},
expect: {
"test.windows.systemSettingsHandler": [{
settings: {
"SystemSettings_Accessibility_Keyboard_WarningEnabled": {
"oldValue": {
"value": true
},
"newValue": {
"value": false
}
},
"SystemSettings_Taskbar_Location": {
"oldValue": {
"value": 3
},
"newValue": {
"value": 3
}
},
"SystemSettings_Input_Touch_SetActivationTimeout": {
"oldValue": {
"value": "Medium sensitivity"
},
"newValue": {
"value": "Low sensitivity"
}
}
}
}]
}
},
{
func: "set",
input: {
"test.windows.systemSettingsHandler": [{
settings: {
"SystemSettings_Accessibility_Keyboard_WarningEnabled": {
"value": true
},
"SystemSettings_Input_Touch_SetActivationTimeout": {
"value": "Medium sensitivity"
}
}
}]
},
expect: {
"test.windows.systemSettingsHandler": [{
settings: {
"SystemSettings_Accessibility_Keyboard_WarningEnabled": {
"oldValue": {
"value": false
},
"newValue": {
"value": true
}
},
"SystemSettings_Input_Touch_SetActivationTimeout": {
"oldValue": {
"value": "Low sensitivity"
},
"newValue": {
"value": "Medium sensitivity"
}
}
}
}]
}
}
]);
/**
* Check if all properties of expected are also in subject and are equal, ignoring any extra ones in subject.
*
* A property's value in the expected object can be the expected value, fluid.VALUE to match any value (just check if
* the property exists), or fluid.NO_VALUE to check the property doesn't exist, or FALSEY marker for any false-like
* value.
*
* @param {Object} subject The object to check against
* @param {Object} expected The object containing the values to check for.
* @param {Number} maxDepth [Optional] How deep to check.
* @return {Boolean} true if there's a match.
*/
gpii.tests.windows.systemSettingsHandler.deepMatch = function (subject, expected, maxDepth) {
var match = false;
if (maxDepth < 0) {
return false;
} else if (!maxDepth && maxDepth !== 0) {
maxDepth = fluid.strategyRecursionBailout;
}
if (!subject) {
return subject === expected;
}
if (Array.isArray(subject) && Array.isArray(expected)) {
if (subject.length !== expected.length) {
return false;
} else if (subject.length === 0) {
return true;
}
}
for (var prop in expected) {
if (expected.hasOwnProperty(prop)) {
var exp = expected[prop];
if (fluid.isMarker(exp, fluid.VALUE)) {
// match any value
match = subject.hasOwnProperty(prop);
} else if (fluid.isMarker(exp, fluid.NO_VALUE)) {
// match no value
match = !subject.hasOwnProperty(prop);
} else if (fluid.isMarker(exp, gpii.tests.FALSEY)) {
// match falsey
match = !subject[prop];
} else if (fluid.isPrimitive(exp)) {
match = subject[prop] === exp;
} else {
match = gpii.tests.windows.systemSettingsHandler.deepMatch(subject[prop], exp, maxDepth - 1);
}
if (!match) {
break;
}
}
}
return match;
};
jqUnit.asyncTest("Testing executeHelper", function () {
var tests = gpii.tests.windows.systemSettingsHandler.executeHelperTests;
jqUnit.expect(tests.length * 3);
// Make sure the "SystemSettings_Accessibility_Keyboard_WarningEnabled" setting is in a known state.
// ("Display a warning message when turning a setting on with a shortcut")
var oldValue = gpii.windows.readRegistryKey("HKEY_CURRENT_USER", "Control Panel\\Accessibility",
"Warning Sounds", "REG_DWORD");
if (oldValue.statusCode === 200) {
teardowns.push(function () {
gpii.windows.writeRegistryKey("HKEY_CURRENT_USER", "Control Panel\\Accessibility",
"Warning Sounds", oldValue.value, "REG_DWORD");
});
}
gpii.windows.writeRegistryKey("HKEY_CURRENT_USER", "Control Panel\\Accessibility",
"Warning Sounds", 0, "REG_DWORD");
var runTest = function (testIndex) {
if (testIndex >= tests.length) {
jqUnit.start();
return;
}
var test = tests[testIndex];
fluid.log(test.input);
var promise = gpii.windows.executeHelper(test.input);
jqUnit.assertNotNull("executeHelper shouldn't return null", promise);
jqUnit.assertTrue("executeHelper should return a promise", fluid.isPromise(promise));
promise.then(function (value) {
fluid.log(value);
var match = gpii.tests.windows.systemSettingsHandler.deepMatch(value, test.expect);
jqUnit.assertTrue("resolved value needs to match the expected.", match);
runTest(testIndex + 1);
}, fluid.fail);
};
runTest(0);
});
jqUnit.asyncTest("Testing system settings handler API", function () {
var tests = gpii.tests.windows.systemSettingsHandler.apiTests;
jqUnit.expect(tests.length * 1);
// Make sure the "SystemSettings_Accessibility_Keyboard_WarningEnabled" setting is in a known state.
// ("Display a warning message when turning a setting on with a shortcut")
var oldValue = gpii.windows.readRegistryKey("HKEY_CURRENT_USER", "Control Panel\\Accessibility",
"Warning Sounds", "REG_DWORD");
if (oldValue.statusCode === 200) {
teardowns.push(function () {
gpii.windows.writeRegistryKey("HKEY_CURRENT_USER", "Control Panel\\Accessibility",
"Warning Sounds", oldValue.value, "REG_DWORD");
});
gpii.windows.writeRegistryKey("HKEY_CURRENT_USER", "Control Panel\\Accessibility",
"Warning Sounds", 0, "REG_DWORD");
}
var runTest = function (testIndex) {
if (testIndex >= tests.length) {
jqUnit.start();
return;
}
var test = tests[testIndex];
gpii.windows.systemSettingsHandler[test.func](test.input).then(function (value) {
jqUnit.assertDeepEq("resolve", test.expect, value);
runTest(testIndex + 1);
}, fluid.fail);
};
runTest(0);
});