diff --git a/apps/android/test.js b/apps/android/test.js deleted file mode 100644 index 007b06fc7..000000000 --- a/apps/android/test.js +++ /dev/null @@ -1,153 +0,0 @@ -let result = true; - -function assertTrue(condition, text) { - if (!condition) { - result = false; - print("FAILURE: " + text); - } else print("OK: " + text); -} - -function assertFalse(condition, text) { - assertTrue(!condition, text); -} - -function assertUndefinedOrEmpty(array, text) { - assertTrue(!array || array.length == 0, text); -} - -function assertNotEmpty(array, text) { - assertTrue(array && array.length > 0, text); -} - -let internalOn = () => { - return getPinMode((process.env.HWVERSION==2)?D30:D26) == "input"; -}; - -let sec = { - connected: false -}; - -NRF.getSecurityStatus = () => sec; -// add an empty starting point to make the asserts work -Bangle._PWR={}; - -let teststeps = []; - -teststeps.push(()=>{ - print("Not connected, should use internal GPS"); - assertTrue(!NRF.getSecurityStatus().connected, "Not connected"); - - assertUndefinedOrEmpty(Bangle._PWR.GPS, "No GPS"); - assertFalse(Bangle.isGPSOn(), "isGPSOn"); - - assertTrue(Bangle.setGPSPower(1, "test"), "Switch GPS on"); - - assertNotEmpty(Bangle._PWR.GPS, "GPS"); - assertTrue(Bangle.isGPSOn(), "isGPSOn"); - assertTrue(internalOn(), "Internal GPS on"); - - assertFalse(Bangle.setGPSPower(0, "test"), "Switch GPS off"); - - assertUndefinedOrEmpty(Bangle._PWR.GPS, "No GPS"); - assertFalse(Bangle.isGPSOn(), "isGPSOn"); - assertFalse(internalOn(), "Internal GPS off"); - -}); - -teststeps.push(()=>{ - print("Connected, should use GB GPS"); - sec.connected = true; - - assertTrue(NRF.getSecurityStatus().connected, "Connected"); - - assertUndefinedOrEmpty(Bangle._PWR.GPS, "No GPS"); - assertFalse(Bangle.isGPSOn(), "isGPSOn"); - assertFalse(internalOn(), "Internal GPS off"); - - - print("Internal GPS stays on until the first GadgetBridge event arrives"); - assertTrue(Bangle.setGPSPower(1, "test"), "Switch GPS on"); - - assertNotEmpty(Bangle._PWR.GPS, "GPS"); - assertTrue(Bangle.isGPSOn(), "isGPSOn"); - assertTrue(internalOn(), "Internal GPS on"); - - print("Send minimal GadgetBridge GPS event to trigger switch"); - GB({t:"gps"}); -}); - -teststeps.push(()=>{ - print("GPS should be on, internal off"); - - assertNotEmpty(Bangle._PWR.GPS, "GPS"); - assertTrue(Bangle.isGPSOn(), "isGPSOn"); - assertFalse(internalOn(), "Internal GPS off"); -}); - -teststeps.push(()=>{ - print("Switching GPS off turns both GadgetBridge as well as internal off"); - assertFalse(Bangle.setGPSPower(0, "test"), "Switch GPS off"); - - assertUndefinedOrEmpty(Bangle._PWR.GPS, "No GPS"); - assertFalse(Bangle.isGPSOn(), "isGPSOn"); - assertFalse(internalOn(), "Internal GPS off"); -}); - -teststeps.push(()=>{ - print("Wait for all timeouts to run out"); - return 12000; -}); - -teststeps.push(()=>{ - print("Check auto switch when no GPS event arrives"); - - assertTrue(Bangle.setGPSPower(1, "test"), "Switch GPS on"); - - assertNotEmpty(Bangle._PWR.GPS, "GPS"); - assertTrue(Bangle.isGPSOn(), "isGPSOn"); - assertTrue(internalOn(), "Internal GPS on"); - - print("Send minimal GadgetBridge GPS event to trigger switch"); - GB({t:"gps"}); - - print("Internal should be switched off now"); - - assertNotEmpty(Bangle._PWR.GPS, "GPS"); - assertTrue(Bangle.isGPSOn(), "isGPSOn"); - assertFalse(internalOn(), "Internal GPS off"); - - //wait on next test - return 12000; -}); - -teststeps.push(()=>{ - print("Check state and disable GPS, internal should be on"); - - assertNotEmpty(Bangle._PWR.GPS, "GPS"); - assertTrue(Bangle.isGPSOn(), "isGPSOn"); - assertTrue(internalOn(), "Internal GPS on"); - - assertFalse(Bangle.setGPSPower(0, "test"), "Switch GPS off"); -}); - -teststeps.push(()=>{ - print("Result Overall is " + (result ? "OK" : "FAIL")); -}); - -let wrap = (functions) => { - if (functions.length > 0) { - setTimeout(()=>{ - let waitingTime = functions.shift()(); - if (waitingTime){ - print("WAITING: ", waitingTime); - setTimeout(()=>{wrap(functions);}, waitingTime); - } else - wrap(functions); - },0); - } -}; - -setTimeout(()=>{ - wrap(teststeps); -}, 5000); - diff --git a/apps/android/test.json b/apps/android/test.json index 809fd012c..74d88a1e6 100644 --- a/apps/android/test.json +++ b/apps/android/test.json @@ -5,13 +5,22 @@ "steps" : [ {"t":"cmd", "js": "Bangle.setGPSPower=(isOn, appID)=>{if (!appID) appID='?';if (!Bangle._PWR) Bangle._PWR={};if (!Bangle._PWR.GPS) Bangle._PWR.GPS=[];if (isOn && !Bangle._PWR.GPS.includes(appID)) Bangle._PWR.GPS.push(appID);if (!isOn && Bangle._PWR.GPS.includes(appID)) Bangle._PWR.GPS.splice(Bangle._PWR.GPS.indexOf(appID),1);return Bangle._PWR.GPS.length>0;};", "text": "Fake the setGPSPower"}, {"t":"wrap", "fn": "Bangle.setGPSPower", "id": "gpspower"}, - {"t":"cmd", "js": "NRF.getSecurityStatus = () => { return { connected: false };}", "text": "Control the security status"}, {"t":"cmd", "js": "Serial1.println = () => { }", "text": "Fake the serial port println"}, {"t":"cmd", "js": "Bluetooth.println = () => { }", "text": "Fake the Bluetooth println"}, {"t":"cmd", "js": "Bangle._PWR={}", "text": "Prepare an empty _PWR for following asserts"}, {"t":"cmd", "js": "require('Storage').writeJSON('android.settings.json', {overwriteGps: true})", "text": "Enable GPS overwrite"}, {"t":"cmd", "js": "eval(require('Storage').read('android.boot.js'))", "text": "Load the boot code"} ] + },{ + "id": "connected", + "steps" : [ + {"t":"cmd", "js": "NRF.getSecurityStatus = () => { return { connected: true };}", "text": "Control the security status to be connected"} + ] + },{ + "id": "disconnected", + "steps" : [ + {"t":"cmd", "js": "NRF.getSecurityStatus = () => { return { connected: false };}", "text": "Control the security status to be disconnected"} + ] }], "tests" : [{ "description": "Check setGPSPower is replaced", @@ -26,6 +35,7 @@ "description": "Test switching hardware GPS on and off", "steps" : [ {"t":"setup", "id": "default"}, + {"t":"setup", "id": "connected"}, {"t":"assertArray", "js": "Bangle._PWR.GPS", "is":"undefinedOrEmpty", "text": "No GPS clients"}, {"t":"assert", "js": "Bangle.isGPSOn()", "is":"falsy", "text": "isGPSOn shows GPS as off"}, {"t":"assert", "js": "Bangle.setGPSPower(1, 'test')", "is":"truthy", "text": "setGPSPower returns truthy when switching on"}, @@ -37,5 +47,52 @@ {"t":"assert", "js": "Bangle.isGPSOn()", "is":"falsy", "text": "isGPSOn shows GPS as off"}, {"t":"assertCall", "id": "gpspower", "count": 2, "argAsserts": [ { "t": "assert", "arg": "0", "is": "equal", "to": 0 } ] , "text": "internal GPS switched off"} ] + },{ + "description": "Test switching when GB GPS is available, internal GPS active until GB GPS event arrives", + "steps" : [ + {"t":"setup", "id": "default"}, + {"t":"setup", "id": "connected"}, + {"t":"assertArray", "js": "Bangle._PWR.GPS", "is":"undefinedOrEmpty", "text": "No GPS clients"}, + {"t":"assert", "js": "Bangle.isGPSOn()", "is":"falsy", "text": "isGPSOn shows GPS as off"}, + + {"t":"assert", "js": "Bangle.setGPSPower(1, 'test')", "is":"truthy", "text": "setGPSPower returns truthy when switching on"}, + {"t":"assertArray", "js": "Bangle._PWR.GPS", "is":"notEmpty", "text": "GPS clients"}, + {"t":"assert", "js": "Bangle.isGPSOn()", "is":"truthy", "text": "isGPSOn shows GPS as on"}, + {"t":"assertCall", "id": "gpspower", "count": 1, "argAsserts": [ { "t": "assert", "arg": "0", "is": "equal", "to": 1 } ], "text": "internal GPS switched on"}, + + {"t":"gb", "obj":{"t":"gps"}}, + {"t":"assertArray", "js": "Bangle._PWR.GPS", "is":"notEmpty", "text": "GPS clients still there"}, + {"t":"assert", "js": "Bangle.isGPSOn()", "is":"truthy", "text": "isGPSOn still shows GPS as on"}, + {"t":"assertCall", "id": "gpspower", "count": 2, "argAsserts": [ { "t": "assert", "arg": "0", "is": "equal", "to": 0 } ], "text": "internal GPS switched off"} + ] + },{ + "description": "Test switching when GB GPS is available, internal stays off", + "steps" : [ + {"t":"setup", "id": "default"}, + {"t":"setup", "id": "connected"}, + + {"t":"assert", "js": "Bangle.setGPSPower(1, 'test')", "is":"truthy", "text": "setGPSPower returns truthy when switching on"}, + + {"t":"gb", "obj":{"t":"gps"}}, + {"t":"assertCall", "id": "gpspower", "argAsserts": [ { "t": "assert", "arg": "0", "is": "equal", "to": 0 } ], "text": "internal GPS switched off"}, + + {"t":"assert", "js": "Bangle.setGPSPower(0, 'test')", "is":"falsy", "text": "setGPSPower returns truthy when switching on"}, + {"t":"assertCall", "id": "gpspower", "argAsserts": [ { "t": "assert", "arg": "0", "is": "equal", "to": 0 } ], "text": "internal GPS still switched off"} + ] + },{ + "description": "Test switching when GB GPS is available, but no event arrives", + "steps" : [ + {"t":"setup", "id": "default"}, + {"t":"setup", "id": "connected"}, + + {"t":"assert", "js": "Bangle.setGPSPower(1, 'test')", "is":"truthy", "text": "setGPSPower returns truthy when switching on"}, + + {"t":"gb", "obj":{"t":"gps"}, "text": "trigger switch"}, + {"t":"assertCall", "id": "gpspower", "argAsserts": [ { "t": "assert", "arg": "0", "is": "equal", "to": 0 } ], "text": "internal GPS switched off"}, + + {"t":"sleep", "ms":"12000", "text": "wait for fallback"}, + + {"t":"assertCall", "id": "gpspower", "argAsserts": [ { "t": "assert", "arg": "0", "is": "equal", "to": 1 } ], "text": "internal GPS switched on caused by missing GB event"} + ] }] }