You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by pu...@apache.org on 2014/08/07 21:21:23 UTC

[08/12] git commit: CB-7160 added manual tests

CB-7160 added manual tests


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/commit/85db0d93
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/tree/85db0d93
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/diff/85db0d93

Branch: refs/heads/master
Commit: 85db0d938a26f2032d1a086cc5a5f1448d2b2281
Parents: 1954d48
Author: Staci Cooper <sm...@us.ibm.com>
Authored: Thu Jul 17 15:46:14 2014 -0400
Committer: Staci Cooper <sm...@us.ibm.com>
Committed: Wed Jul 30 17:00:48 2014 -0400

----------------------------------------------------------------------
 test/tests.js | 135 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 134 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/blob/85db0d93/test/tests.js
----------------------------------------------------------------------
diff --git a/test/tests.js b/test/tests.js
index f85e695..75016d0 100644
--- a/test/tests.js
+++ b/test/tests.js
@@ -1,4 +1,25 @@
-exports.defineAutoTests = function() {
+/*
+ *
+ * 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 () {
   describe('Accelerometer (navigator.accelerometer)', function () {
     var fail = function(done) {
       expect(true).toBe(false);
@@ -152,3 +173,115 @@ exports.defineAutoTests = function() {
     });
   });
 };
+
+/******************************************************************************/
+/******************************************************************************/
+/******************************************************************************/
+
+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 watchAccelId = null;
+
+    /**
+     * Start watching acceleration
+     */
+    var watchAccel = function () {
+        console.log("watchAccel()");
+
+        // Success callback
+        var success = function (a) {
+            document.getElementById('x').innerHTML = roundNumber(a.x);
+            document.getElementById('y').innerHTML = roundNumber(a.y);
+            document.getElementById('z').innerHTML = roundNumber(a.z);
+        };
+
+        // Fail callback
+        var fail = function (e) {
+            console.log("watchAccel fail callback with error code " + e);
+            stopAccel();
+            setAccelStatus(Accelerometer.ERROR_MSG[e]);
+        };
+
+        // Update acceleration every 1 sec
+        var opt = {};
+        opt.frequency = 1000;
+        watchAccelId = navigator.accelerometer.watchAcceleration(success, fail, opt);
+
+        setAccelStatus("Running");
+    };
+
+    /**
+     * Stop watching the acceleration
+     */
+    var stopAccel = function () {
+        console.log("stopAccel()");
+        setAccelStatus("Stopped");
+        if (watchAccelId) {
+            navigator.accelerometer.clearWatch(watchAccelId);
+            watchAccelId = null;
+        }
+    };
+
+    /**
+     * Get current acceleration
+     */
+    var getAccel = function () {
+        console.log("getAccel()");
+
+        // Stop accel if running
+        stopAccel();
+
+        // Success callback
+        var success = function (a) {
+            document.getElementById('x').innerHTML = roundNumber(a.x);
+            document.getElementById('y').innerHTML = roundNumber(a.y);
+            document.getElementById('z').innerHTML = roundNumber(a.z);
+            console.log("getAccel success callback");
+        };
+
+        // Fail callback
+        var fail = function (e) {
+            console.log("getAccel fail callback with error code " + e);
+            setAccelStatus(Accelerometer.ERROR_MSG[e]);
+        };
+
+        // Make call
+        var opt = {};
+        navigator.accelerometer.getCurrentAcceleration(success, fail, opt);
+    };
+
+    /**
+     * Set accelerometer status
+     */
+    var setAccelStatus = function (status) {
+        document.getElementById('accel_status').innerHTML = status;
+    };
+
+    /******************************************************************************/
+
+    contentEl.innerHTML = '<div id="info">' +
+        'Status: <span id="accel_status">Stopped</span>' +
+        '<table width="100%">' +
+        '<tr><td width="20%">X:</td><td id="x"> </td></tr>' +
+        '<tr><td width="20%">Y:</td><td id="y"> </td></tr>' +
+        '<tr><td width="20%">Z:</td><td id="z"> </td></tr>' +
+        '</table></div>' +
+        '<div id="actions"></div>';
+
+    createActionButton('Get Acceleration', function () {
+        getAccel();
+    }, 'actions');
+
+    createActionButton('Start Watch', function () {
+        watchAccel();
+    }, 'actions');
+
+    createActionButton('Clear Watch', function () {
+        stopAccel();
+    }, 'actions');
+};