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">
<name>ScreenOrientationDemo</name>
<description>
A sample Apache Cordova application that showcases the Screenorientation plugin.
A sample Apache Cordova application that showcases the ScreenOrientation plugin.
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<content src="index.html" />
<plugin name="cordova-plugin-whitelist" spec="1" />
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />

View File

@ -22,52 +22,101 @@
/* jshint jasmine: true */
exports.defineAutoTests = function() {
describe('Orientation Information (window.screen.orientation)', function () {
it('should exist', function() {
if (window.screen) {
expect(window.screen.orientation).toBeDefined();
} else {
fail('No screen object is present');
}
});
it('should contain a platform specification that is a string', function() {
if (window.screen && window.screen.orientation) {
expect((String(window.screen.orientation.type)).length > 0).toBe(true);
} else {
fail('No orientation object is present');
}
describe('window.screen', function() {
it('should be defined', function() {
expect(window.screen).toBeDefined();
});
});
describe('Screen functions', function () {
it('should successfully lock and unlock screen orientation', function (done) {
describe('window.screen.orientation', function() {
it('should be defined', function() {
expect(window.screen.orientation).toBeDefined();
});
it('should have a `lock` function', function() {
expect(window.screen.orientation.lock).toBeDefined();
expect(typeof window.screen.orientation.lock).toBe('function');
});
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('OrientationType', function() {
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 {
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();
done();
}, function (err) {
fail('Promise was rejected: ' + err);
}, function(err) {
expect(err).toBeDefined();
fail(err);
done();
});
} catch (e) {
fail(e);
} catch (err) {
fail(err);
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,81 +20,81 @@
*/
var screenOrientation = {};
if (!window.OrientationType) {
window.OrientationType = {
'0': 'portrait-primary',
'180': 'portrait-secondary',
'90': 'landscape-primary',
'-90': 'landscape-secondary'
};
var screenOrientation = {};
if (!window.OrientationType) {
window.OrientationType = {
'0': 'portrait-primary',
'180': 'portrait-secondary',
'90': 'landscape-primary',
'-90': 'landscape-secondary'
};
}
if (!window.OrientationLockType) {
window.OrientationLockType = {
'portrait-primary': 1,
'portrait-secondary': 2,
'landscape-primary': 4,
'landscape-secondary': 8,
'portrait': (1 & 2), // either portrait-primary or portrait-secondary.
'landscape': ( 4 & 8 ), // either landscape-primary or landscape-secondary.
'any': ( 1 & 2 & 4 & 8 ) // All orientations are supported (unlocked orientation)
};
}
var orientationMask = 1;
screenOrientation.setOrientation = function(orientation) {
orientationMask = window.OrientationLockType[orientation];
cordova.exec(null, null, "CDVOrientation", "screenOrientation", [orientationMask, orientation]);
};
function addScreenOrientationApi(screenObject) {
if (screenObject.unlock || screenObject.lock) {
screenObject.nativeLock = screenObject.lock;
}
if (!window.OrientationLockType) {
window.OrientationLockType = {
'portrait-primary': 1,
'portrait-secondary': 2,
'landscape-primary': 4,
'landscape-secondary': 8,
'portrait': 3, // either portrait-primary or portrait-secondary.
'landscape': 12, // either landscape-primary or landscape-secondary.
'any': 15 // All orientations are supported (unlocked orientation)
};
screenObject.lock = function(orientation) {
var promiseLock;
var p = new Promise(function(resolve, reject) {
if (screenObject.nativeLock != null) {
promiseLock = screenObject.nativeLock(orientation);
promiseLock.then(function success(res) {
resolve();
}, function error(err) {
screenObject.nativeLock = null;
resolveOrientation(orientation, resolve, reject);
});
} else {
resolveOrientation(orientation, resolve, reject);
}
})
return p;
}
var orientationMask = 1;
screenOrientation.setOrientation = function(orientation) {
orientationMask = window.OrientationLockType[orientation];
cordova.exec(null, null, "CDVOrientation", "screenOrientation", [orientationMask, orientation]);
screenObject.unlock = function() {
screenOrientation.setOrientation('any');
};
function addScreenOrientationApi(screenObject) {
if (screenObject.unlock || screenObject.lock) {
screenObject.nativeLock = screenObject.lock;
}
screenObject.lock = function(orientation) {
var promiseLock;
var p = new Promise(function(resolve, reject) {
if (screenObject.nativeLock != null) {
promiseLock = screenObject.nativeLock(orientation);
promiseLock.then(function success(res) {
resolve();
}, function error(err) {
screenObject.nativeLock = null;
resolveOrientation(orientation, resolve, reject);
});
} else {
resolveOrientation(orientation, resolve, reject);
}
})
return p;
}
screenObject.unlock = function() {
screenOrientation.setOrientation('any');
};
}
function resolveOrientation(orientation, resolve, reject) {
if (!OrientationLockType.hasOwnProperty(orientation)) {
var err = new Error();
err.name = "NotSupportedError";
reject(err); //"cannot change orientation");
} else {
//screenOrientation.currOrientation = screenObject.orientation = orientation;
screenOrientation.setOrientation(orientation);
resolve("Orientation set"); // orientation change successful
}
function resolveOrientation(orientation, resolve, reject) {
if (!OrientationLockType.hasOwnProperty(orientation)) {
var err = new Error();
err.name = "NotSupportedError";
reject(err); //"cannot change orientation");
} else {
//screenOrientation.currOrientation = screenObject.orientation = orientation;
screenOrientation.setOrientation(orientation);
resolve("Orientation set"); // orientation change successful
}
}
if (!screen.orientation) {
screen.orientation = {};
}
addScreenOrientationApi(screen.orientation);
orientationChange();
}
if (!screen.orientation) {
screen.orientation = {};
}
addScreenOrientationApi(screen.orientation);
orientationChange();
function orientationChange() {
screen.orientation.type = window.OrientationType[window.orientation];
}
window.addEventListener("orientationchange", orientationChange, true);
module.exports = screenOrientation;
function orientationChange() {
screen.orientation.type = window.OrientationType[window.orientation];
}
window.addEventListener("orientationchange", orientationChange, true);
module.exports = screenOrientation;