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:49:07 UTC

[2/3] git commit: CB-6960 Added manual tests

CB-6960 Added 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/7457cba1
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/tree/7457cba1
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/diff/7457cba1

Branch: refs/heads/master
Commit: 7457cba1c08cf2727f9c03eba5e376098be3c071
Parents: 6e44a4a
Author: Staci Cooper <sm...@us.ibm.com>
Authored: Thu Jul 17 16:20:25 2014 -0400
Committer: Staci Cooper <sm...@us.ibm.com>
Committed: Thu Jul 17 16:20:25 2014 -0400

----------------------------------------------------------------------
 test/tests.js | 222 ++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 159 insertions(+), 63 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-orientation/blob/7457cba1/test/tests.js
----------------------------------------------------------------------
diff --git a/test/tests.js b/test/tests.js
index 3da81f2..50f97d9 100644
--- a/test/tests.js
+++ b/test/tests.js
@@ -1,48 +1,47 @@
 /*
- *
- * 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.
- *
+*
+* 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) {
-        expect(true).toBe(false);
+exports.defineAutoTests = function () {
+    var fail = function (done, message) {
+        message = (typeof message !== 'string') ? "Forced failure: wrong callback called" : message;
+        expect(true).toFailWithMessage(message);
         done();
     },
-    succeed = function (done) {
-        expect(true).toBe(true);
-        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 () {             
+    describe('Compass (navigator.compass)', function () {
         var hardwarefailure = false;
-        var failedSpec = 0;
-        beforeEach(function() {
+        beforeEach(function () {
             jasmine.Expectation.addMatchers({
-                // check to see if the device has a compass, if it doesn't fail gracefully 
-                hasHardware: function() {
+                toFailWithMessage: function () {
                     return {
-                        compare: function (actual) {
-                            var pass = actual;
-                            hardwarefailure = pass ? false : true;
+                        compare: function (actual, customMessage) {
+                            var pass = false;
+                            if (customMessage === undefined) {
+                                customMessage = "Forced failure: wrong callback called";
+                            }
                             return {
                                 pass: pass,
-                                message: "The device does not have compass support.  The remaining compass tests will be ignored."
+                                message: customMessage
                             };
                         }
                     };
@@ -50,37 +49,31 @@ exports.defineAutoTests = function() {
             });
         });
 
-        afterEach(function () {
-            jasmine.Spec.prototype.status = function() {
-                if (this.result.failedExpectations.length > 0) {
-                    failedSpec++;
-                    // We want to gracefully fail if there is a hardware failure
-                    if(failedSpec > 0 && hardwarefailure == true){
-                        // There is an error in the code - stop running tests!
-                        jasmine.Queue.prototype.next_ = function () { this.onComplete();}
+        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.");
                     }
-                    return 'failed';
-                } else {
-                    return 'passed';
-                }
-            };
-        });
-
-        it("compass.hardwarecheck is compass supported", function() {
-            var f = function(){navigator.compass.getCurrentHeading(function onSuccess(){}, function onError(error) {})};
-            expect(f).hasHardware();
+                    done();
+                });
         });
 
-        it("compass.spec.1 should exist", function() {
+        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() {
+        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();
@@ -91,22 +84,21 @@ exports.defineAutoTests = function() {
                 expect(typeof a.headingAccuracy == 'number' || a.headingAccuracy === null).toBe(true);
                 expect(typeof a.timestamp == 'number').toBe(true);
                 done();
-            },
-            fail.bind(null, done));
+            }, fail.bind(null, done, unexpectedFailure));
         });
 
-        it("compass.spec.4 should contain a watchHeading function", function() {
+        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() {
+        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() {
+            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);
@@ -114,11 +106,11 @@ exports.defineAutoTests = function() {
         });
 
         describe('Compass Heading model (CompassHeading)', function () {
-            it("compass.spec.1 should exist", 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() {
+            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();
@@ -127,8 +119,8 @@ exports.defineAutoTests = function() {
                 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);
+            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);
@@ -138,3 +130,107 @@ 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 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;
+    };
+
+    /******************************************************************************/
+
+    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>';
+
+    createActionButton('Get Compass', function () {
+        getCompass();
+    }, 'actions');
+
+    createActionButton('Start Watching Compass', function () {
+        watchCompass();
+    }, 'actions');
+
+    createActionButton('Stop Watching Compass', function () {
+        stopCompass();
+    }, 'actions');
+};