-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathtestDisplaySettingsHandler.js
More file actions
258 lines (223 loc) · 9.23 KB
/
testDisplaySettingsHandler.js
File metadata and controls
258 lines (223 loc) · 9.23 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
/*
* 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);
var nextTest = function (index) {
var testData = tests[index];
if (testData) {
fluid.log("expecting: ", testData.expected);
var actual = gpii.windows.display.setScreenDpi(testData.input);
var retryPromise;
fluid.log("first result: ", actual);
if (fluid.model.diff(testData.expected, actual)) {
retryPromise = fluid.promise().resolve();
} else {
// [GPII-4279] This test randomly fails in CI. To work around this, keep trying to get the dpi
// (for 5 seconds) until it returns the expected result.
fluid.log("setScreenDpi returned an unexpected result - retrying");
retryPromise = gpii.windows.waitForCondition(function () {
actual = gpii.windows.display.getScreenDpi(testData.input);
fluid.log("retry result: ", actual);
return fluid.model.diff(testData.expected, actual);
}, {dontReject: true, timeout: 5000});
}
retryPromise.then(function () {
jqUnit.assertDeepEq("setScreenDpi " + testData.input, testData.expected, actual);
// Delay the next test
setTimeout(function () {
nextTest(index + 1);
}, 500);
});
} else {
jqUnit.start();
}
};
nextTest(0);
});
});