UNPKG

gpii-windows

Version:

Components of the GPII personalization infrastructure for use on Microsoft's "Windows" ™

404 lines (355 loc) 13.1 kB
/* * WmiSetttingsHandler 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"), jqUnit = fluid.require("node-jqunit"), windows = fluid.registerNamespace("gpii.windows"); require("../src/WmiSettingsHandler.js"); require("../../../../tests/TestWithMocks.js"); jqUnit.module("WmiSettingsHandler Module"); jqUnit.test("Testing 'gpii.windows.wmi.mergeSameKeys'", function () { var settings = { "Brightness": { "value": 100 } }; var options = { "Brightness": { "namespace": "root\\WMI", "get": { "query": "SELECT CurrentBrightness FROM WmiMonitorBrightness" }, "set": { "className": "WmiMonitorBrightnessMethods", "method": "WmiSetBrightness", "params": [ 4294967295, "$value" ], "returnVal": [ "uint", 0 ] } } }; var expected = { "Brightness": { "namespace": "root\\WMI", "get": { "query": "SELECT CurrentBrightness FROM WmiMonitorBrightness" }, "set": { "className": "WmiMonitorBrightnessMethods", "method": "WmiSetBrightness", "params": [ 4294967295, "$value" ], "returnVal": [ "uint", 0 ] }, "value": 100 } }; var mergedKeys = windows.wmi.mergeSameKeys(settings, options); jqUnit.assert("Expected and result payload mismatch", expected, mergedKeys); }); jqUnit.test("Testing 'gpii.windows.wmi.replacePlaceholder'", function () { var options = { "Brightness": { "namespace": "root\\WMI", "get": { "query": "SELECT CurrentBrightness FROM WmiMonitorBrightness" }, "set": { "className": "WmiMonitorBrightnessMethods", "method": "WmiSetBrightness", "params": [ 4294967295, "$value" ], "returnVal": [ "uint", 0 ] }, "value": 100 } }; var expected = { "Brightness": { "namespace": "root\\WMI", "get": { "query": "SELECT CurrentBrightness FROM WmiMonitorBrightness" }, "set": { "className": "WmiMonitorBrightnessMethods", "method": "WmiSetBrightness", "params": [ 4294967295, 100 ], "returnVal": [ "uint", 0 ] } } }; var replaced = windows.wmi.replacePlaceholder(options); jqUnit.assertDeepEq("Expected and result payload mismatch", replaced, expected); }); jqUnit.test("Testing 'gpii.windows.wmi.flatSetQuery' and 'gpii.windows.wmi.flatGetQuery'", function () { var settings = { "Brightness": { "value": 100 } }; var options = { "Brightness": { "namespace": "root\\WMI", "get": { "query": "SELECT CurrentBrightness FROM WmiMonitorBrightness" }, "set": { "className": "WmiMonitorBrightnessMethods", "method": "WmiSetBrightness", "params": [ 4294967295, "$value" ], "returnVal": [ "uint", 0 ] } } }; var mergedQueries = windows.wmi.mergeSameKeys(settings, options); var replacedQueries = windows.wmi.replacePlaceholder(mergedQueries); var flattenedSet = windows.wmi.flatSetQuery(replacedQueries.Brightness); var expected = [ "root\\WMI", "WmiMonitorBrightnessMethods", "WmiSetBrightness", [ 4294967295, 100 ], [ "uint", 0 ] ]; jqUnit.assertDeepEq("Expected and result payload mismatch", expected, flattenedSet); var flattenedGet = windows.wmi.flatGetQuery(Object.values(replacedQueries)[0]); var expectedGet = [ "root\\WMI", "SELECT CurrentBrightness FROM WmiMonitorBrightness" ]; jqUnit.assertDeepEq("Expected and result payload mismatch", expectedGet, flattenedGet); }); jqUnit.test("Testing wmiSettingsHandler set new brightness using the setImpl payload", function () { var sampleBrightness = 30; var getPayload = function (desiredBrightness) { return { "settings": { "Brightness": { "value": desiredBrightness } }, "options": { "Brightness": { "namespace": "root\\WMI", "get": { "query": "SELECT CurrentBrightness FROM WmiMonitorBrightness" }, "set": { "className": "WmiMonitorBrightnessMethods", "method": "WmiSetBrightness", "params": [ 4294967295, "$value" ], "returnVal": [ "uint", 0 ] }, "settingType": "uint" } } }; }; var payload = getPayload(sampleBrightness); var oldBrightnessVal = windows.wmi.getImpl(payload).Brightness.value; var supportedBrightness = !Number.isNaN(oldBrightnessVal); if (supportedBrightness) { var brightnessValues = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]; var changeToTargetBrightness = function (desiredBrightness, payload) { var oldBrightnessVal = NaN; try { // Creating new brightness payload var result = windows.wmi.setImpl(payload); // Checking if new brightness has been set oldBrightnessVal = result.Brightness.oldValue.value; var newBrightnessVal = result.Brightness.newValue.value; var inMargin = (desiredBrightness + 5) > newBrightnessVal && (desiredBrightness - 5) < newBrightnessVal; jqUnit.assertTrue("Brightness is the same as the setted new one.", inMargin); } catch (e) { jqUnit.fail("Error while applying the settings with the following exception: " + e); } return oldBrightnessVal; }; var firstBrightnessVal = null; fluid.each(brightnessValues, function (desiredBrightness) { var payload = getPayload(desiredBrightness); var oldBrightnessVal = changeToTargetBrightness(desiredBrightness, payload); if (firstBrightnessVal === null) { firstBrightnessVal = oldBrightnessVal; } }); var oldBrightnessPayload = ["root\\WMI" ,"WmiMonitorBrightnessMethods", "WmiSetBrightness", [0xFFFFFFFF, firstBrightnessVal], ["uint", 0]]; windows.wmi.updateWMISetting(oldBrightnessPayload); } else { fluid.log(fluid.logLevel.WARN, "Expected Error: setScreenBrightnessWithPayload test lacks validity, screenBrightness is not supported on the system"); jqUnit.assert("Test passed without doing meaningfull job, screenBrightness is not supported on the system"); } }); jqUnit.test("Testing wmiSettingsHandler set handles unsupported feature.", function () { var desiredBrightness = 30; var payload = { "settings": { "Brightness": { "value": desiredBrightness } }, "options": { "Brightness": { "namespace": "root\\WMI", "get": { "query": "SELECT CurrentBrightness FROM WmiMonitorBrightness" }, "set": { "className": "WmiMonitorBrightnessMethods", "method": "WmiSetBrightness", "params": [ 4294967295, "$value" ], "returnVal": [ "uint", 0 ] }, "settingType": "uint" } } }; var notSupported = windows.wmi.getImpl(payload).Brightness.value === null; if (notSupported) { var result = windows.wmi.setImpl(payload); var nullOld = result.Brightness.oldValue.value === null; var nullNew = result.Brightness.newValue.value === null; var correct = nullOld && nullNew; jqUnit.assertTrue("The payload should be null when the setting is not supported.", correct); } else { jqUnit.assert("Setting is supported in the system, so test passes with no validity."); } }); jqUnit.test("Testing wmiSettingsHandler set handles null/non null oldValue", function () { var desiredBrightness = 30; var payload = { "settings": { "Brightness": { "value": desiredBrightness } }, "options": { "Brightness": { "namespace": "root\\WMI", "get": { "query": "SELECT CurrentBrightness FROM WmiMonitorBrightness" }, "set": { "className": "WmiMonitorBrightnessMethods", "method": "WmiSetBrightness", "params": [ 4294967295, "$value" ], "returnVal": [ "uint", 0 ] }, "settingType": "uint" } } }; windows.tests.testWithMocks([{ "original": { obj: windows, path: "wmi.getImpl" }, "mock": function (payload) { var settingsOptions = payload.options; var results = {}; fluid.each(settingsOptions, function (settingOptions, settingId) { fluid.set(results, settingId + ".value", 20); }); return results; } }], function () { var result = windows.wmi.setImpl(payload); jqUnit.assertEquals("The handler should attempt to set value when setting is supported.", 20, result.Brightness.oldValue.value); }, function (e) { jqUnit.fail("Error while applying the settings with the following exception: " + e); }); }); jqUnit.test("Testing wmiSettingsHandler set errors on missing placeholder.", function () { var query = { "Brightness": { "namespace": "root\\WMI", "get": { "query": "SELECT CurrentBrightness FROM WmiMonitorBrightness" }, "set": { "className": "WmiMonitorBrightnessMethods", "method": "WmiSetBrightness", "params": [ 4294967295, "" ], "returnVal": [ "uint", 0 ] }, "settingType": "uint" } }; jqUnit.expectFrameworkDiagnostic("Should fail due to missing placeholder", function () { windows.wmi.replacePlaceholder(query); }, "No value placeholder supplied"); }); jqUnit.test("Testing wmiSettingsHandler bad getQuery.", function () { var query = "Not a valid query"; jqUnit.assertNull("Should return null due to bad getQuery", windows.wmi.getQuery(query)); }); jqUnit.test("Testing wmiSettingsHandler updates successfully.", function () { var brightnessVal = 30; var payload = ["root\\WMI" ,"WmiMonitorBrightnessMethods", "WmiSetBrightness", [0xFFFFFFFF, brightnessVal], ["uint", 0]]; windows.wmi.updateWMISetting(payload); jqUnit.assert("Setting is supported in the system, so test passes with no validity."); }); jqUnit.test("Testing wmiSettingsHandler bad updateQuery.", function () { var payload = "invalid value"; jqUnit.expectFrameworkDiagnostic("Should fail due to bad updateQuery", function () { windows.wmi.updateWMISetting(payload); }, "Failed with WMI error msg"); });