Skip to content

Commit 98ec1cf

Browse files
authored
fix(chrome): lock test (#105)
1 parent b4030fc commit 98ec1cf

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

tests/tests.js

+29-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@
2020
*/
2121

2222
exports.defineAutoTests = function () {
23+
var isLockable = false;
24+
25+
beforeAll(function () {
26+
// Mobile devices are expected to have a lockable orientation, while
27+
// desktop devices does not
28+
// (Chrome on desktop will return:
29+
// DOMException: screen.orientation.lock() is not available on this device)
30+
// Using an user agent check sucks, but this is a unit test,
31+
// - using browser window size is unreliable cause the browser can be resized.
32+
// - Using max touch support is unreliable cause some laptops will have touch support
33+
// but still lack support for locking the orientation.
34+
isLockable = /Android|iPhone|iPad|iPod/gi.test(window.navigator.userAgent);
35+
});
36+
2337
describe('window.screen', function () {
2438
it('should be defined', function () {
2539
expect(window.screen).toBeDefined();
@@ -108,12 +122,22 @@ exports.defineAutoTests = function () {
108122
// test addEventListener('change') works
109123
// test onchange works
110124
describe('window.screen.orientation', function () {
111-
it('should successfully lock and unlock screen orientation', function () {
112-
return window.screen.orientation.lock('portrait').then(function () {
113-
expect(window.screen.orientation.type).toMatch(/^portrait-/);
114-
expect(window.screen.orientation.unlock).not.toThrow();
125+
if (isLockable) {
126+
it('should successfully lock and unlock screen orientation', function () {
127+
return window.screen.orientation.lock('portrait').then(function () {
128+
expect(window.screen.orientation.type).toMatch(/^portrait-/);
129+
expect(window.screen.orientation.unlock).not.toThrow();
130+
});
115131
});
116-
});
132+
}
133+
// We do not test "not isLockable" states because it isn't testable.
134+
// The error stating it's not supported is not actually passed to the
135+
// promise reject function, so the error is not catchable. The error
136+
// is only ever printed to the JS console if nothing catches errors.
137+
// The promise itself is fulfilled successfully, despite the action
138+
// not doing what is expected.
139+
// I believe this might be a privacy security mechanism to avoid device
140+
// fingerprinting.
117141
});
118142
};
119143
exports.defineManualTests = function (contentEl, createActionButton) {

0 commit comments

Comments
 (0)