added missing tests, fixed indentation of js file

This commit is contained in:
Jesse MacFadyen 2017-02-22 11:34:21 -08:00
parent 972a082203
commit f8207c4164
3 changed files with 156 additions and 108 deletions

View File

@ -2,13 +2,12 @@
<widget id="com.cordova.screenorientationdemo" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0"> <widget id="com.cordova.screenorientationdemo" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>ScreenOrientationDemo</name> <name>ScreenOrientationDemo</name>
<description> <description>
A sample Apache Cordova application that showcases the Screenorientation plugin. A sample Apache Cordova application that showcases the ScreenOrientation plugin.
</description> </description>
<author email="dev@cordova.apache.org" href="http://cordova.io"> <author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team Apache Cordova Team
</author> </author>
<content src="index.html" /> <content src="index.html" />
<plugin name="cordova-plugin-whitelist" spec="1" />
<access origin="*" /> <access origin="*" />
<allow-intent href="http://*/*" /> <allow-intent href="http://*/*" />
<allow-intent href="https://*/*" /> <allow-intent href="https://*/*" />

View File

@ -22,52 +22,101 @@
/* jshint jasmine: true */ /* jshint jasmine: true */
exports.defineAutoTests = function() { exports.defineAutoTests = function() {
describe('Orientation Information (window.screen.orientation)', function () {
it('should exist', function() { describe('window.screen', function() {
if (window.screen) {
it('should be defined', function() {
expect(window.screen).toBeDefined();
});
});
describe('window.screen.orientation', function() {
it('should be defined', function() {
expect(window.screen.orientation).toBeDefined(); expect(window.screen.orientation).toBeDefined();
} else {
fail('No screen object is present');
}
}); });
it('should contain a platform specification that is a string', function() { it('should have a `lock` function', function() {
if (window.screen && window.screen.orientation) { expect(window.screen.orientation.lock).toBeDefined();
expect((String(window.screen.orientation.type)).length > 0).toBe(true); expect(typeof window.screen.orientation.lock).toBe('function');
} else { });
fail('No orientation object is present');
} it('should have an `unlock` function', function() {
expect(window.screen.orientation.unlock).toBeDefined();
expect(typeof window.screen.orientation.unlock).toBe('function');
});
it('should have a `type` property (string)', function() {
expect(window.screen.orientation.type).toBeDefined();
expect(typeof window.screen.orientation.type).toBe('string');
});
xit('should have an `angle` property (number)', function() {
expect(window.screen.orientation.angle).toBeDefined();
expect(typeof window.screen.orientation.angle).toBe('number');
});
xit('should have an `onchange` property (function)', function() {
expect(window.screen.orientation.onchange).toBeDefined();
expect(typeof window.screen.orientation.onchange).toBe('function');
}); });
}); });
describe('Screen functions', function () { describe('OrientationType', function() {
it('should successfully lock and unlock screen orientation', function (done) {
it("should be defined", function() {
expect(window.OrientationType).toBeDefined();
});
xit("should have defined types", function(){
expect(window.OrientationType['portrait-primary']).toBeDefined();
expect(window.OrientationType['portrait-secondary']).toBeDefined();
expect(window.OrientationType['landscape-primary']).toBeDefined();
expect(window.OrientationType['landscape-secondary']).toBeDefined();
});
});
describe('OrientationLockType', function() {
it("should be defined", function() {
expect(window.OrientationLockType).toBeDefined();
});
it("should have defined types", function(){
expect(window.OrientationLockType['portrait-primary']).toBeDefined();
expect(window.OrientationLockType['portrait-secondary']).toBeDefined();
expect(window.OrientationLockType['landscape-primary']).toBeDefined();
expect(window.OrientationLockType['landscape-secondary']).toBeDefined();
expect(window.OrientationLockType['portrait']).toBeDefined();
expect(window.OrientationLockType['landscape']).toBeDefined();
expect(window.OrientationLockType['any']).toBeDefined();
});
});
// TODO:
// test addEventListener('change') works
// test onchange works
describe('window.screen.orientation', function() {
xit('should successfully lock and unlock screen orientation, lock should return a promise', function(done) {
try { try {
window.screen.orientation.lock('landscape').then(function () { var promise = window.screen.orientation.lock('landscape');
expect(promise).toBeDefined();
expect(typeof promise).toBe('promise');
promise.then(function() {
expect(window.screen.orientation.unlock).not.toThrow(); expect(window.screen.orientation.unlock).not.toThrow();
done(); done();
}, function (err) { }, function(err) {
fail('Promise was rejected: ' + err); expect(err).toBeDefined();
fail(err);
done(); done();
}); });
} catch (e) { } catch (err) {
fail(e); fail(err);
done(); done();
} }
}); });
}); });
describe('OrientationLockType should have one of seven values', function () {
it("should have one of seven orientation lock types", function(){
expect(window.OrientationLockType['portrait-primary']).toBe(1);
expect(window.OrientationLockType['portrait-secondary']).toBe(2);
expect(window.OrientationLockType['landscape-primary']).toBe(4);
expect(window.OrientationLockType['landscape-secondary']).toBe(8);
expect(window.OrientationLockType['portrait']).toBe(3);
expect(window.OrientationLockType['landscape']).toBe(12);
expect(window.OrientationLockType['any']).toBe(15);
});
});
}; };

View File

@ -20,33 +20,33 @@
*/ */
var screenOrientation = {}; var screenOrientation = {};
if (!window.OrientationType) { if (!window.OrientationType) {
window.OrientationType = { window.OrientationType = {
'0': 'portrait-primary', '0': 'portrait-primary',
'180': 'portrait-secondary', '180': 'portrait-secondary',
'90': 'landscape-primary', '90': 'landscape-primary',
'-90': 'landscape-secondary' '-90': 'landscape-secondary'
}; };
} }
if (!window.OrientationLockType) { if (!window.OrientationLockType) {
window.OrientationLockType = { window.OrientationLockType = {
'portrait-primary': 1, 'portrait-primary': 1,
'portrait-secondary': 2, 'portrait-secondary': 2,
'landscape-primary': 4, 'landscape-primary': 4,
'landscape-secondary': 8, 'landscape-secondary': 8,
'portrait': 3, // either portrait-primary or portrait-secondary. 'portrait': (1 & 2), // either portrait-primary or portrait-secondary.
'landscape': 12, // either landscape-primary or landscape-secondary. 'landscape': ( 4 & 8 ), // either landscape-primary or landscape-secondary.
'any': 15 // All orientations are supported (unlocked orientation) 'any': ( 1 & 2 & 4 & 8 ) // All orientations are supported (unlocked orientation)
}; };
} }
var orientationMask = 1; var orientationMask = 1;
screenOrientation.setOrientation = function(orientation) { screenOrientation.setOrientation = function(orientation) {
orientationMask = window.OrientationLockType[orientation]; orientationMask = window.OrientationLockType[orientation];
cordova.exec(null, null, "CDVOrientation", "screenOrientation", [orientationMask, orientation]); cordova.exec(null, null, "CDVOrientation", "screenOrientation", [orientationMask, orientation]);
}; };
function addScreenOrientationApi(screenObject) { function addScreenOrientationApi(screenObject) {
if (screenObject.unlock || screenObject.lock) { if (screenObject.unlock || screenObject.lock) {
screenObject.nativeLock = screenObject.lock; screenObject.nativeLock = screenObject.lock;
} }
@ -73,8 +73,8 @@
screenOrientation.setOrientation('any'); screenOrientation.setOrientation('any');
}; };
} }
function resolveOrientation(orientation, resolve, reject) { function resolveOrientation(orientation, resolve, reject) {
if (!OrientationLockType.hasOwnProperty(orientation)) { if (!OrientationLockType.hasOwnProperty(orientation)) {
var err = new Error(); var err = new Error();
err.name = "NotSupportedError"; err.name = "NotSupportedError";
@ -85,16 +85,16 @@
resolve("Orientation set"); // orientation change successful resolve("Orientation set"); // orientation change successful
} }
} }
if (!screen.orientation) { if (!screen.orientation) {
screen.orientation = {}; screen.orientation = {};
} }
addScreenOrientationApi(screen.orientation); addScreenOrientationApi(screen.orientation);
orientationChange(); orientationChange();
function orientationChange() { function orientationChange() {
screen.orientation.type = window.OrientationType[window.orientation]; screen.orientation.type = window.OrientationType[window.orientation];
} }
window.addEventListener("orientationchange", orientationChange, true); window.addEventListener("orientationchange", orientationChange, true);
module.exports = screenOrientation; module.exports = screenOrientation;