You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by an...@apache.org on 2014/09/05 19:59:07 UTC
[1/6] git commit: Add support for the browser
Repository: cordova-plugin-device-orientation
Updated Branches:
refs/heads/master 56654b0d8 -> 9dd4a5ab7
Add support for the browser
Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/commit/8baf7301
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/tree/8baf7301
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/diff/8baf7301
Branch: refs/heads/master
Commit: 8baf73010e3aa4699241711411dbbc2f156c3e3f
Parents: d77710f
Author: Suraj Pindoria <su...@yahoo.com>
Authored: Fri Aug 29 16:07:06 2014 -0700
Committer: Suraj Pindoria <su...@yahoo.com>
Committed: Fri Aug 29 16:07:06 2014 -0700
----------------------------------------------------------------------
plugin.xml | 11 ++++
src/browser/CompassProxy.js | 45 ++++++++++++++++
src/browser/compass.js | 109 +++++++++++++++++++++++++++++++++++++++
3 files changed, 165 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/blob/8baf7301/plugin.xml
----------------------------------------------------------------------
diff --git a/plugin.xml b/plugin.xml
index 7e989a5..0fff6df 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -163,4 +163,15 @@
<runs/>
</js-module>
</platform>
+
+ <!-- browser -->
+ <platform name="browser">
+ <js-module src="src/browser/CompassProxy.js" name="CompassProxy">
+ <runs/>
+ </js-module>
+
+ <js-module src="src/browser/compass.js" name="compass">
+ <merges target="navigator.compass" />
+ </js-module>
+ </platform>
</plugin>
http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/blob/8baf7301/src/browser/CompassProxy.js
----------------------------------------------------------------------
diff --git a/src/browser/CompassProxy.js b/src/browser/CompassProxy.js
new file mode 100644
index 0000000..1b22111
--- /dev/null
+++ b/src/browser/CompassProxy.js
@@ -0,0 +1,45 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+
+var Compass = {
+ getHeading: function(success, error) {
+ var listener = function() {
+ var orient = {};
+ var heading = (Math.round((Math.random() * 360) * 100) / 100);
+
+ orient.trueHeading = heading;
+ orient.magneticHeading = heading;
+ orient.headingAccuracy = 0;
+ orient.timestamp = new Date().getTime();
+
+ success(orient);
+
+ window.removeEventListener('deviceorientation', listener, false);
+ };
+
+ return window.addEventListener('deviceorientation', listener, false);
+ }
+};
+
+var browser = require('cordova/platform');
+
+module.exports = Compass;
+require('cordova/exec/proxy').add('Compass', Compass);
http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/blob/8baf7301/src/browser/compass.js
----------------------------------------------------------------------
diff --git a/src/browser/compass.js b/src/browser/compass.js
new file mode 100644
index 0000000..e705d77
--- /dev/null
+++ b/src/browser/compass.js
@@ -0,0 +1,109 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+*/
+
+var argscheck = require('cordova/argscheck'),
+ exec = require('cordova/exec'),
+ utils = require('cordova/utils'),
+ CompassHeading = require('./CompassHeading'),
+ CompassError = require('./CompassError'),
+
+ timers = {},
+ compass = {
+ /**
+ * Asynchronously acquires the current heading.
+ * @param {Function} successCallback The function to call when the heading
+ * data is available
+ * @param {Function} errorCallback The function to call when there is an error
+ * getting the heading data.
+ * @param {CompassOptions} options The options for getting the heading data (not used).
+ */
+ getCurrentHeading:function(successCallback, errorCallback, options) {
+ argscheck.checkArgs('fFO', 'compass.getCurrentHeading', arguments);
+
+ var win = function(result) {
+ var ch = new CompassHeading(result.magneticHeading, result.trueHeading, result.headingAccuracy, result.timestamp);
+ successCallback(ch);
+ };
+ var fail = errorCallback && function(code) {
+ var ce = new CompassError(code);
+ errorCallback(ce);
+ };
+
+ // Get heading
+ exec(win, fail, "Compass", "getHeading", [options]);
+ },
+
+ /**
+ * Asynchronously acquires the heading repeatedly at a given interval.
+ * @param {Function} successCallback The function to call each time the heading
+ * data is available
+ * @param {Function} errorCallback The function to call when there is an error
+ * getting the heading data.
+ * @param {HeadingOptions} options The options for getting the heading data
+ * such as timeout and the frequency of the watch. For iOS, filter parameter
+ * specifies to watch via a distance filter rather than time.
+ */
+ watchHeading:function(successCallback, errorCallback, options) {
+ argscheck.checkArgs('fFO', 'compass.watchHeading', arguments);
+ // Default interval (100 msec)
+ var frequency = (options !== undefined && options.frequency !== undefined) ? options.frequency : 100;
+ var filter = (options !== undefined && options.filter !== undefined) ? options.filter : 0;
+
+ var id = utils.createUUID();
+ if (filter > 0) {
+ // is an iOS request for watch by filter, no timer needed
+ timers[id] = "iOS";
+ compass.getCurrentHeading(successCallback, errorCallback, options);
+ } else {
+ // Start watch timer to get headings
+ timers[id] = window.setInterval(function() {
+ compass.getCurrentHeading(successCallback, errorCallback);
+ }, frequency);
+ }
+
+ // Continuously calls deviceorientation listener and gets new heading values
+ deviceorientation = new Event('deviceorientation');
+ window.setInterval(function() {
+ window.dispatchEvent(deviceorientation);
+ }, 200);
+
+ return id;
+ },
+
+ /**
+ * Clears the specified heading watch.
+ * @param {String} watchId The ID of the watch returned from #watchHeading.
+ */
+ clearWatch:function(id) {
+ // Stop javascript timer & remove from timer list
+ if (id && timers[id]) {
+ if (timers[id] != "iOS") {
+ clearInterval(timers[id]);
+ } else {
+ // is iOS watch by filter so call into device to stop
+ exec(null, null, "Compass", "stopHeading", []);
+ }
+ delete timers[id];
+ }
+ }
+ };
+
+module.exports = compass;
[6/6] git commit: Merge branch 'master' of
https://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation
Posted by an...@apache.org.
Merge branch 'master' of https://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation
Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/commit/9dd4a5ab
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/tree/9dd4a5ab
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/diff/9dd4a5ab
Branch: refs/heads/master
Commit: 9dd4a5ab7489b1cfe5f5ecfa9e7eba57d1f0d347
Parents: ce6ab12 56654b0
Author: Anis Kadri <an...@apache.org>
Authored: Fri Sep 5 10:59:02 2014 -0700
Committer: Anis Kadri <an...@apache.org>
Committed: Fri Sep 5 10:59:02 2014 -0700
----------------------------------------------------------------------
----------------------------------------------------------------------
[4/6] git commit: added documentation for manual tests
Posted by an...@apache.org.
added documentation for manual tests
Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/commit/c1d79099
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/tree/c1d79099
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/diff/c1d79099
Branch: refs/heads/master
Commit: c1d79099d91561130f7bc9a1880d7634d73dc54d
Parents: e483ccc
Author: Edna Morales <ed...@ednas-mbp-2.raleigh.ibm.com>
Authored: Thu Jul 24 13:42:36 2014 -0400
Committer: Anis Kadri <an...@apache.org>
Committed: Fri Sep 5 10:58:56 2014 -0700
----------------------------------------------------------------------
test/tests.js | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/blob/c1d79099/test/tests.js
----------------------------------------------------------------------
diff --git a/test/tests.js b/test/tests.js
index 50f97d9..54b529a 100644
--- a/test/tests.js
+++ b/test/tests.js
@@ -215,22 +215,30 @@ exports.defineManualTests = function (contentEl, createActionButton) {
/******************************************************************************/
+ var orientation_tests = '<h3>iOS devices may bring up a calibration screen when initiating these tests</h3>' +
+ '<div id="getCompass"></div>' +
+ 'Expected result: Will update the status box with current heading. Status will read "Stopped"' +
+ '<p/> <div id="watchCompass"></div>' +
+ 'Expected result: When pressed, will start a watch on the compass and update the heading value when heading reading changes. Status will read "Running"' +
+ '<p/> <div id="stopCompass"></div>' +
+ 'Expected result: Will clear the compass watch, so heading value will no longer be updated. Status will read "Stopped"';
+
contentEl.innerHTML = '<div id="info"><b>Status: </b>' +
'<span id="compass_status">Stopped</span>' +
'<table width="100%"><tr>' +
'<td width="33%">Heading: <span id="compassHeading"></span>' +
'</td></tr></table></div>' +
- '<div id="actions"></div>';
+ orientation_tests;
createActionButton('Get Compass', function () {
getCompass();
- }, 'actions');
+ }, 'getCompass');
createActionButton('Start Watching Compass', function () {
watchCompass();
- }, 'actions');
+ }, 'watchCompass');
createActionButton('Stop Watching Compass', function () {
stopCompass();
- }, 'actions');
+ }, 'stopCompass');
};
[2/6] git commit: Updated docs for browser
Posted by an...@apache.org.
Updated docs for browser
Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/commit/e483ccc4
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/tree/e483ccc4
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/diff/e483ccc4
Branch: refs/heads/master
Commit: e483ccc41bcc21c8756b757c397ffad240540596
Parents: 8baf730
Author: Suraj Pindoria <su...@yahoo.com>
Authored: Fri Sep 5 09:56:27 2014 -0700
Committer: Suraj Pindoria <su...@yahoo.com>
Committed: Fri Sep 5 09:56:27 2014 -0700
----------------------------------------------------------------------
doc/index.md | 5 +++++
1 file changed, 5 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/blob/e483ccc4/doc/index.md
----------------------------------------------------------------------
diff --git a/doc/index.md b/doc/index.md
index 37d2f28..b2474e7 100644
--- a/doc/index.md
+++ b/doc/index.md
@@ -33,6 +33,7 @@ from the top of the device. It measures the heading in degrees from 0 to
- Amazon Fire OS
- Android
- BlackBerry 10
+- Browser
- Firefox OS
- iOS
- Tizen
@@ -97,6 +98,10 @@ ID can be used with `navigator.compass.clearWatch` to stop watching the navigato
var watchID = navigator.compass.watchHeading(onSuccess, onError, options);
+### Browser Quirks
+
+Values for current heading are randomly generated in order to simulate the compass.
+
### iOS Quirks
Only one `watchHeading` can be in effect at one time in iOS. If a
[5/6] git commit: Fixed problem with watchCompass if pressed twice
Posted by an...@apache.org.
Fixed problem with watchCompass if pressed twice
Stop any existing watch before starting a new one.
Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/commit/ce6ab122
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/tree/ce6ab122
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/diff/ce6ab122
Branch: refs/heads/master
Commit: ce6ab122d337ad468715e5df27d67b3e443c2ade
Parents: fc3837d
Author: Staci Cooper <sm...@us.ibm.com>
Authored: Wed Sep 3 11:19:28 2014 -0400
Committer: Anis Kadri <an...@apache.org>
Committed: Fri Sep 5 10:58:56 2014 -0700
----------------------------------------------------------------------
tests/tests.js | 3 +++
1 file changed, 3 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/blob/ce6ab122/tests/tests.js
----------------------------------------------------------------------
diff --git a/tests/tests.js b/tests/tests.js
index 4893ea9..c5ddc59 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -162,6 +162,9 @@ exports.defineManualTests = function (contentEl, createActionButton) {
setCompassStatus(e);
};
+ // Stop compass if running
+ stopCompass();
+
// Update heading every 1 sec
var opt = {};
opt.frequency = 1000;
[3/6] git commit: CB-7086 Renamed dir, added nested plugin.xml
Posted by an...@apache.org.
CB-7086 Renamed dir, added nested plugin.xml
Not sure where these changes got lost
github: close #14
Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/commit/fc3837d7
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/tree/fc3837d7
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/diff/fc3837d7
Branch: refs/heads/master
Commit: fc3837d74c6f7c92d3b7a53cd518888b031bfee1
Parents: c1d7909
Author: Staci Cooper <sm...@us.ibm.com>
Authored: Wed Sep 3 11:11:31 2014 -0400
Committer: Anis Kadri <an...@apache.org>
Committed: Fri Sep 5 10:58:56 2014 -0700
----------------------------------------------------------------------
test/tests.js | 244 --------------------------------------------------
tests/plugin.xml | 30 +++++++
tests/tests.js | 244 ++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 274 insertions(+), 244 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/blob/fc3837d7/test/tests.js
----------------------------------------------------------------------
diff --git a/test/tests.js b/test/tests.js
deleted file mode 100644
index 54b529a..0000000
--- a/test/tests.js
+++ /dev/null
@@ -1,244 +0,0 @@
-/*
-*
-* Licensed to the Apache Software Foundation (ASF) under one
-* or more contributor license agreements. See the NOTICE file
-* distributed with this work for additional information
-* regarding copyright ownership. The ASF licenses this file
-* to you under the Apache License, Version 2.0 (the
-* "License"); you may not use this file except in compliance
-* with the License. You may obtain a copy of the License at
-*
-* http://www.apache.org/licenses/LICENSE-2.0
-*
-* Unless required by applicable law or agreed to in writing,
-* software distributed under the License is distributed on an
-* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-* KIND, either express or implied. See the License for the
-* specific language governing permissions and limitations
-* under the License.
-*
-*/
-
-exports.defineAutoTests = function () {
- var fail = function (done, message) {
- message = (typeof message !== 'string') ? "Forced failure: wrong callback called" : message;
- expect(true).toFailWithMessage(message);
- done();
- },
- unexpectedSuccess = "Forced failure: success callback should not have been called",
- unexpectedFailure = "Forced failure: error callback should not have been called";
-
- describe('Compass (navigator.compass)', function () {
- var hardwarefailure = false;
- beforeEach(function () {
- jasmine.Expectation.addMatchers({
- toFailWithMessage: function () {
- return {
- compare: function (actual, customMessage) {
- var pass = false;
- if (customMessage === undefined) {
- customMessage = "Forced failure: wrong callback called";
- }
- return {
- pass: pass,
- message: customMessage
- };
- }
- };
- }
- });
- });
-
- it("compass.hardwarecheck is compass supported", function (done) {
- navigator.compass.getCurrentHeading(function onSuccess() { done(); },
- function onError(error) {
- if (error.code == CompassError.COMPASS_NOT_SUPPORTED) {
- hardwarefailure = true;
- expect(this).toFailWithMessage("The device does not have compass support. Any tests relying on support will be marked pending.");
- }
- done();
- });
- });
-
- it("compass.spec.1 should exist", function () {
- console.log("In spec 1");
- expect(navigator.compass).toBeDefined();
- });
-
- it("compass.spec.2 should contain a getCurrentHeading function", function () {
- expect(navigator.compass.getCurrentHeading).toBeDefined();
- expect(typeof navigator.compass.getCurrentHeading == 'function').toBe(true);
- });
-
- it("compass.spec.3 getCurrentHeading success callback should be called with a Heading object", function (done) {
- if (hardwarefailure) {
- pending();
- }
- navigator.compass.getCurrentHeading(function (a) {
- expect(a instanceof CompassHeading).toBe(true);
- expect(a.magneticHeading).toBeDefined();
- expect(typeof a.magneticHeading == 'number').toBe(true);
- expect(a.trueHeading).not.toBe(undefined);
- expect(typeof a.trueHeading == 'number' || a.trueHeading === null).toBe(true);
- expect(a.headingAccuracy).not.toBe(undefined);
- expect(typeof a.headingAccuracy == 'number' || a.headingAccuracy === null).toBe(true);
- expect(typeof a.timestamp == 'number').toBe(true);
- done();
- }, fail.bind(null, done, unexpectedFailure));
- });
-
- it("compass.spec.4 should contain a watchHeading function", function () {
- expect(navigator.compass.watchHeading).toBeDefined();
- expect(typeof navigator.compass.watchHeading == 'function').toBe(true);
- });
-
- it("compass.spec.5 should contain a clearWatch function", function () {
- expect(navigator.compass.clearWatch).toBeDefined();
- expect(typeof navigator.compass.clearWatch == 'function').toBe(true);
- });
-
- describe('Compass Constants (window.CompassError)', function () {
- it("compass.spec.1 should exist", function () {
- expect(window.CompassError).toBeDefined();
- expect(window.CompassError.COMPASS_INTERNAL_ERR).toBe(0);
- expect(window.CompassError.COMPASS_NOT_SUPPORTED).toBe(20);
- });
- });
-
- describe('Compass Heading model (CompassHeading)', function () {
- it("compass.spec.1 should exist", function () {
- expect(CompassHeading).toBeDefined();
- });
-
- it("compass.spec.8 should be able to create a new CompassHeading instance with no parameters", function () {
- var h = new CompassHeading();
- expect(h).toBeDefined();
- expect(h.magneticHeading).toBeUndefined();
- expect(h.trueHeading).toBeUndefined();
- expect(h.headingAccuracy).toBeUndefined();
- expect(typeof h.timestamp == 'number').toBe(true);
- });
-
- it("compass.spec.9 should be able to create a new CompassHeading instance with parameters", function () {
- var h = new CompassHeading(1, 2, 3, 4);
- expect(h.magneticHeading).toBe(1);
- expect(h.trueHeading).toBe(2);
- expect(h.headingAccuracy).toBe(3);
- expect(h.timestamp.valueOf()).toBe(4);
- expect(typeof h.timestamp == 'number').toBe(true);
- });
- });
- });
-};
-
-/******************************************************************************/
-/******************************************************************************/
-/******************************************************************************/
-
-exports.defineManualTests = function (contentEl, createActionButton) {
- function roundNumber(num) {
- var dec = 3;
- var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
- return result;
- }
-
- var watchCompassId = null;
-
- /**
- * Start watching compass
- */
- var watchCompass = function () {
- console.log("watchCompass()");
-
- // Success callback
- var success = function (a) {
- document.getElementById('compassHeading').innerHTML = roundNumber(a.magneticHeading);
- };
-
- // Fail callback
- var fail = function (e) {
- console.log("watchCompass fail callback with error code " + e);
- stopCompass();
- setCompassStatus(e);
- };
-
- // Update heading every 1 sec
- var opt = {};
- opt.frequency = 1000;
- watchCompassId = navigator.compass.watchHeading(success, fail, opt);
-
- setCompassStatus("Running");
- };
-
- /**
- * Stop watching the acceleration
- */
- var stopCompass = function () {
- setCompassStatus("Stopped");
- if (watchCompassId) {
- navigator.compass.clearWatch(watchCompassId);
- watchCompassId = null;
- }
- };
-
- /**
- * Get current compass
- */
- var getCompass = function () {
- console.log("getCompass()");
-
- // Stop compass if running
- stopCompass();
-
- // Success callback
- var success = function (a) {
- document.getElementById('compassHeading').innerHTML = roundNumber(a.magneticHeading);
- };
-
- // Fail callback
- var fail = function (e) {
- console.log("getCompass fail callback with error code " + e.toString);
- setCompassStatus(e);
- };
-
- // Make call
- var opt = {};
- navigator.compass.getCurrentHeading(success, fail, opt);
- };
-
- /**
- * Set compass status
- */
- var setCompassStatus = function (status) {
- document.getElementById('compass_status').innerHTML = status;
- };
-
- /******************************************************************************/
-
- var orientation_tests = '<h3>iOS devices may bring up a calibration screen when initiating these tests</h3>' +
- '<div id="getCompass"></div>' +
- 'Expected result: Will update the status box with current heading. Status will read "Stopped"' +
- '<p/> <div id="watchCompass"></div>' +
- 'Expected result: When pressed, will start a watch on the compass and update the heading value when heading reading changes. Status will read "Running"' +
- '<p/> <div id="stopCompass"></div>' +
- 'Expected result: Will clear the compass watch, so heading value will no longer be updated. Status will read "Stopped"';
-
- contentEl.innerHTML = '<div id="info"><b>Status: </b>' +
- '<span id="compass_status">Stopped</span>' +
- '<table width="100%"><tr>' +
- '<td width="33%">Heading: <span id="compassHeading"></span>' +
- '</td></tr></table></div>' +
- orientation_tests;
-
- createActionButton('Get Compass', function () {
- getCompass();
- }, 'getCompass');
-
- createActionButton('Start Watching Compass', function () {
- watchCompass();
- }, 'watchCompass');
-
- createActionButton('Stop Watching Compass', function () {
- stopCompass();
- }, 'stopCompass');
-};
http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/blob/fc3837d7/tests/plugin.xml
----------------------------------------------------------------------
diff --git a/tests/plugin.xml b/tests/plugin.xml
new file mode 100644
index 0000000..be078bd
--- /dev/null
+++ b/tests/plugin.xml
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+
+<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ id="org.apache.cordova.device-orientation.tests"
+ version="0.3.8-dev">
+ <name>Cordova Device Orientation Plugin Tests</name>
+ <license>Apache 2.0</license>
+
+ <js-module src="tests.js" name="tests">
+ </js-module>
+</plugin>
http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/blob/fc3837d7/tests/tests.js
----------------------------------------------------------------------
diff --git a/tests/tests.js b/tests/tests.js
new file mode 100644
index 0000000..4893ea9
--- /dev/null
+++ b/tests/tests.js
@@ -0,0 +1,244 @@
+/*
+*
+* Licensed to the Apache Software Foundation (ASF) under one
+* or more contributor license agreements. See the NOTICE file
+* distributed with this work for additional information
+* regarding copyright ownership. The ASF licenses this file
+* to you under the Apache License, Version 2.0 (the
+* "License"); you may not use this file except in compliance
+* with the License. You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing,
+* software distributed under the License is distributed on an
+* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+* KIND, either express or implied. See the License for the
+* specific language governing permissions and limitations
+* under the License.
+*
+*/
+
+exports.defineAutoTests = function () {
+ var fail = function (done, message) {
+ message = (typeof message !== 'string') ? "Forced failure: wrong callback called" : message;
+ expect(true).toFailWithMessage(message);
+ done();
+ },
+ unexpectedSuccess = "Forced failure: success callback should not have been called",
+ unexpectedFailure = "Forced failure: error callback should not have been called";
+
+ describe('Compass (navigator.compass)', function () {
+ var hardwarefailure = false;
+ beforeEach(function () {
+ jasmine.Expectation.addMatchers({
+ toFailWithMessage: function () {
+ return {
+ compare: function (actual, customMessage) {
+ var pass = false;
+ if (customMessage === undefined) {
+ customMessage = "Forced failure: wrong callback called";
+ }
+ return {
+ pass: pass,
+ message: customMessage
+ };
+ }
+ };
+ }
+ });
+ });
+
+ it("compass.hardwarecheck is compass supported", function (done) {
+ navigator.compass.getCurrentHeading(function onSuccess() { done(); },
+ function onError(error) {
+ if (error.code == CompassError.COMPASS_NOT_SUPPORTED) {
+ hardwarefailure = true;
+ expect(this).toFailWithMessage("The device does not have compass support. Any tests relying on support will be marked pending.");
+ }
+ done();
+ });
+ });
+
+ it("compass.spec.1 should exist", function () {
+ console.log("In spec 1");
+ expect(navigator.compass).toBeDefined();
+ });
+
+ it("compass.spec.2 should contain a getCurrentHeading function", function () {
+ expect(navigator.compass.getCurrentHeading).toBeDefined();
+ expect(typeof navigator.compass.getCurrentHeading == 'function').toBe(true);
+ });
+
+ it("compass.spec.3 getCurrentHeading success callback should be called with a Heading object", function (done) {
+ if (hardwarefailure) {
+ pending();
+ }
+ navigator.compass.getCurrentHeading(function (a) {
+ expect(a instanceof CompassHeading).toBe(true);
+ expect(a.magneticHeading).toBeDefined();
+ expect(typeof a.magneticHeading == 'number').toBe(true);
+ expect(a.trueHeading).not.toBe(undefined);
+ expect(typeof a.trueHeading == 'number' || a.trueHeading === null).toBe(true);
+ expect(a.headingAccuracy).not.toBe(undefined);
+ expect(typeof a.headingAccuracy == 'number' || a.headingAccuracy === null).toBe(true);
+ expect(typeof a.timestamp == 'number').toBe(true);
+ done();
+ }, fail.bind(null, done, unexpectedFailure));
+ });
+
+ it("compass.spec.4 should contain a watchHeading function", function () {
+ expect(navigator.compass.watchHeading).toBeDefined();
+ expect(typeof navigator.compass.watchHeading == 'function').toBe(true);
+ });
+
+ it("compass.spec.5 should contain a clearWatch function", function () {
+ expect(navigator.compass.clearWatch).toBeDefined();
+ expect(typeof navigator.compass.clearWatch == 'function').toBe(true);
+ });
+
+ describe('Compass Constants (window.CompassError)', function () {
+ it("compass.spec.1 should exist", function () {
+ expect(window.CompassError).toBeDefined();
+ expect(window.CompassError.COMPASS_INTERNAL_ERR).toBe(0);
+ expect(window.CompassError.COMPASS_NOT_SUPPORTED).toBe(20);
+ });
+ });
+
+ describe('Compass Heading model (CompassHeading)', function () {
+ it("compass.spec.1 should exist", function () {
+ expect(CompassHeading).toBeDefined();
+ });
+
+ it("compass.spec.8 should be able to create a new CompassHeading instance with no parameters", function () {
+ var h = new CompassHeading();
+ expect(h).toBeDefined();
+ expect(h.magneticHeading).toBeUndefined();
+ expect(h.trueHeading).toBeUndefined();
+ expect(h.headingAccuracy).toBeUndefined();
+ expect(typeof h.timestamp == 'number').toBe(true);
+ });
+
+ it("compass.spec.9 should be able to create a new CompassHeading instance with parameters", function () {
+ var h = new CompassHeading(1, 2, 3, 4);
+ expect(h.magneticHeading).toBe(1);
+ expect(h.trueHeading).toBe(2);
+ expect(h.headingAccuracy).toBe(3);
+ expect(h.timestamp.valueOf()).toBe(4);
+ expect(typeof h.timestamp == 'number').toBe(true);
+ });
+ });
+ });
+};
+
+/******************************************************************************/
+/******************************************************************************/
+/******************************************************************************/
+
+exports.defineManualTests = function (contentEl, createActionButton) {
+ function roundNumber(num) {
+ var dec = 3;
+ var result = Math.round(num * Math.pow(10, dec)) / Math.pow(10, dec);
+ return result;
+ }
+
+ var watchCompassId = null;
+
+ /**
+ * Start watching compass
+ */
+ var watchCompass = function () {
+ console.log("watchCompass()");
+
+ // Success callback
+ var success = function (a) {
+ document.getElementById('compassHeading').innerHTML = roundNumber(a.magneticHeading);
+ };
+
+ // Fail callback
+ var fail = function (e) {
+ console.log("watchCompass fail callback with error code " + e);
+ stopCompass();
+ setCompassStatus(e);
+ };
+
+ // Update heading every 1 sec
+ var opt = {};
+ opt.frequency = 1000;
+ watchCompassId = navigator.compass.watchHeading(success, fail, opt);
+
+ setCompassStatus("Running");
+ };
+
+ /**
+ * Stop watching the acceleration
+ */
+ var stopCompass = function () {
+ setCompassStatus("Stopped");
+ if (watchCompassId) {
+ navigator.compass.clearWatch(watchCompassId);
+ watchCompassId = null;
+ }
+ };
+
+ /**
+ * Get current compass
+ */
+ var getCompass = function () {
+ console.log("getCompass()");
+
+ // Stop compass if running
+ stopCompass();
+
+ // Success callback
+ var success = function (a) {
+ document.getElementById('compassHeading').innerHTML = roundNumber(a.magneticHeading);
+ };
+
+ // Fail callback
+ var fail = function (e) {
+ console.log("getCompass fail callback with error code " + e.toString);
+ setCompassStatus(e);
+ };
+
+ // Make call
+ var opt = {};
+ navigator.compass.getCurrentHeading(success, fail, opt);
+ };
+
+ /**
+ * Set compass status
+ */
+ var setCompassStatus = function (status) {
+ document.getElementById('compass_status').innerHTML = status;
+ };
+
+ /******************************************************************************/
+
+ var orientation_tests = '<h3>iOS devices may bring up a calibration screen when initiating these tests</h3>' +
+ '<div id="getCompass"></div>' +
+ 'Expected result: Will update the status box with current heading. Status will read "Stopped"' +
+ '<p/> <div id="watchCompass"></div>' +
+ 'Expected result: When pressed, will start a watch on the compass and update the heading value when heading changes. Status will read "Running"' +
+ '<p/> <div id="stopCompass"></div>' +
+ 'Expected result: Will clear the compass watch, so heading value will no longer be updated. Status will read "Stopped"';
+
+ contentEl.innerHTML = '<div id="info"><b>Status: </b>' +
+ '<span id="compass_status">Stopped</span>' +
+ '<table width="100%"><tr>' +
+ '<td width="33%">Heading: <span id="compassHeading"></span>' +
+ '</td></tr></table></div>' +
+ orientation_tests;
+
+ createActionButton('Get Compass', function () {
+ getCompass();
+ }, 'getCompass');
+
+ createActionButton('Start Watching Compass', function () {
+ watchCompass();
+ }, 'watchCompass');
+
+ createActionButton('Stop Watching Compass', function () {
+ stopCompass();
+ }, 'stopCompass');
+};