-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathtestDisplaySettingsHandler.js
More file actions
310 lines (277 loc) · 11.2 KB
/
testDisplaySettingsHandler.js
File metadata and controls
310 lines (277 loc) · 11.2 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* 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");
var windowsDisplay = gpii.windowsDisplay();
/**
* Tests for the DPI settings.
*/
gpii.tests.displaySettings.dpiTestDefs = fluid.freezeRecursive([
{
// 100%
input: 1,
expected: {
actual: 1,
configured: 1,
maximum: "$maximum"
}
},
{
// 125%
input: 1.25,
expected: {
actual: 1.25,
configured: 1.25,
maximum: "$maximum"
}
},
{
// > max
input: "$overMaximum",
expected: {
actual: "$maximum",
configured: "$overMaximum",
maximum: "$maximum"
}
},
{
// 50% (not possible)
input: 0.5,
expected: {
actual: 1,
configured: 1,
maximum: "$maximum"
}
},
{
// 0% (not possible)
input: 0,
expected: {
actual: 1,
configured: 1,
maximum: "$maximum"
}
},
{
// < 0% (not possible)
input: -1,
expected: {
actual: 1,
configured: 1,
maximum: "$maximum"
}
},
{
// 120% (should round to 125%)
input: 1.2,
expected: {
actual: 1.25,
configured: 1.25,
maximum: "$maximum"
}
}
]);
/**
* Extracts the test items from model, performing look-ups from it ("$like.this")
*
* @param model {Object} The object to resolve from.
* @param value {Object} 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 = windowsDisplay.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("Get available screen resolutions", function () {
var availableReesolutions = windowsDisplay.getAvailableResolutions();
jqUnit.assertNotNull("Available resoiultions array", availableReesolutions);
jqUnit.assertNotEquals("Number of availableReesolutions",
0, availableReesolutions.length);
});
jqUnit.test("Get all screen resolutions", function () {
var current = windowsDisplay.getScreenResolution();
var available = windowsDisplay.getAvailableResolutions();
var all = windowsDisplay.getAllScreenResolutions();
jqUnit.assertDeepEq("All screen resolutions contains current resolution",
current, all["screen-resolution"]);
// Revisit use of "dmPels" prefix as returnsd by getAvailalbeResolutions().
// Can't do a deep equals because the property names are different.
var allAvailable = all["available-resolutions"];
jqUnit.assertEquals("All screen resolutions contain available resolutions",
available.length, allAvailable.length);
fluid.each (available, function (expected, index) {
var actual = allAvailable[index];
jqUnit.assertEquals("Available resolution width in 'all' structure vs available",
expected.dmPelsWidth, actual.width);
jqUnit.assertEquals("Available resolution height in 'all' structure vs available",
expected.dmPelsHeight, actual.height);
});
});
jqUnit.test("Valid and invalid screen resolutions", function () {
var currentResolution = windowsDisplay.getScreenResolution();
jqUnit.assertFalse(
"Valid screen resolution",
windowsDisplay.isScreenResolutionInvalid(currentResolution)
);
var invalidResolution = { width: -999, height: -666 };
jqUnit.assertTrue(
"Valid screen resolution",
windowsDisplay.isScreenResolutionInvalid(invalidResolution)
);
});
jqUnit.test("Testing setScreenResolution ", function () {
var oldRes = windowsDisplay.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'",
windowsDisplay.setScreenResolution(targetRes));
var newRes = windowsDisplay.getScreenResolution();
jqUnit.assertDeepEq("New resolution is set", targetRes, newRes);
// Restore old resolution
jqUnit.expect(2);
jqUnit.assertTrue("Resetting to old resolution", windowsDisplay.setScreenResolution(oldRes));
var restoredRes = windowsDisplay.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 () {
windowsDisplay.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 () {
windowsDisplay.setScreenResolution(faulty1);
}, "invalid screen resolution");
var faulty2 = { w: 0, h: 1 };
jqUnit.expectFrameworkDiagnostic("setScreenResolution fails when receives a wrong object", function () {
windowsDisplay.setScreenResolution(faulty2);
}, ["invalid screen resolution"]);
var faulty3 = { width: 0, height: "" };
jqUnit.expectFrameworkDiagnostic("setScreenResolution fails when receives one string as value for height", function () {
windowsDisplay.setScreenResolution(faulty3);
}, "invalid screen resolution");
});
jqUnit.test("Windows display settings handlerr: getImpl()", function () {
var actualSettings = windowsDisplay.getImpl();
var expectedSettings = windowsDisplay.getAllScreenResolutions();
jqUnit.assertDeepEq("Return value of getImpl() with no input",
expectedSettings["screen-resolution"],
actualSettings["screen-resolution"]);
});
jqUnit.test("Windows display settings handler: setImpl()", function () {
var allRez = windowsDisplay.getAllScreenResolutions();
var newRez = fluid.find(allRez["available-resolutions"], function (aRez) {
if (aRez.width !== allRez["screen-resolution"].width ||
aRez.height !== allRez["screen-resolution"].height) {
return aRez;
}
}, null);
if (newRez !== null) {
var rezToSet = {};
rezToSet.width = newRez.width;
rezToSet.height = newRez.height;
var screenResolution = {};
fluid.set(screenResolution, "screen-resolution", rezToSet);
var payload = {};
payload.settings = screenResolution;
var oldRez = windowsDisplay.setImpl(payload);
jqUnit.assertDeepEq(
"New resolution via setImpl()",
windowsDisplay.getScreenResolution(),
rezToSet
);
jqUnit.assertDeepEq(
"Old resolution from setImpl()",
allRez["screen-resolution"],
oldRez["screen-resolution"].oldValue
);
// Revert to the previous resolution.
payload.settings["screen-resolution"] = allRez["screen-resolution"];
oldRez = windowsDisplay.setImpl(payload);
jqUnit.assertDeepEq(
"Revert to old resolution using setImpl()",
windowsDisplay.getScreenResolution(),
allRez["screen-resolution"]
);
jqUnit.assertDeepEq(
"Old resolution returned during revert via setImpl()",
rezToSet,
oldRez["screen-resolution"].oldValue
);
}
});
// 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.test("Testing setScreenDpi ", function () {
// Make sure the resolution is good enough to have DPI above 100%. 1024x768 provides up to 125%.
var res = gpii.windows.display.getScreenResolution();
if (res.width < 1024 || res.height < 768) {
teardowns.push(function () {
windowsDisplay.setScreenResolution(res);
});
windowsDisplay.setScreenResolution({width: 1024, height: 768});
}
// Restore the original setting at the end.
var originalDpi = gpii.windows.display.getScreenDpi();
teardowns.push(function () {
gpii.windows.display.setScreenDpi(originalDpi.configured);
});
// 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({
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 * 100) + "%", testData.expected, actual);
}
});