1/* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import sensor from '@ohos.sensor' 16 17import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' 18 19describe("SensorJsTest", function () { 20 function callback(data) { 21 console.info("callback" + JSON.stringify(data)); 22 expect(typeof(data.x)).assertEqual("number"); 23 expect(typeof(data.y)).assertEqual("number"); 24 expect(typeof(data.z)).assertEqual("number"); 25 } 26 27 function callback2(data) { 28 console.info("callback2" + JSON.stringify(data)); 29 expect(typeof(data.x)).assertEqual("number"); 30 expect(typeof(data.y)).assertEqual("number"); 31 expect(typeof(data.z)).assertEqual("number"); 32 } 33 beforeAll(function() { 34 /* 35 * @tc.setup: setup invoked before all testcases 36 */ 37 console.info('beforeAll caled') 38 }) 39 40 afterAll(function() { 41 /* 42 * @tc.teardown: teardown invoked after all testcases 43 */ 44 console.info('afterAll caled') 45 }) 46 47 beforeEach(function() { 48 /* 49 * @tc.setup: setup invoked before each testcases 50 */ 51 console.info('beforeEach caled') 52 }) 53 54 afterEach(function() { 55 /* 56 * @tc.teardown: teardown invoked after each testcases 57 */ 58 console.info('afterEach caled') 59 }) 60 61 /* 62 * @tc.name:SensorJsTest001 63 * @tc.desc:verify app info is not null 64 * @tc.type: FUNC 65 * @tc.require: Issue Number 66 */ 67 it("SensorJsTest001", 0, async function (done) { 68 console.info('----------------------SensorJsTest001---------------------------'); 69 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback); 70 setTimeout(()=>{ 71 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER); 72 done(); 73 }, 500); 74 }) 75 76 /* 77 * @tc.name:SensorJsTest002 78 * @tc.desc:verify app info is not null 79 * @tc.type: FUNC 80 * @tc.require: Issue Number 81 */ 82 it("SensorJsTest002", 0, async function (done) { 83 console.info('----------------------SensorJsTest002---------------------------'); 84 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback, {'interval': 100000000}); 85 setTimeout(()=>{ 86 console.info('----------------------SensorJsTest002 off in---------------------------'); 87 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER); 88 console.info('----------------------SensorJsTest002 off end---------------------------'); 89 done(); 90 }, 500); 91 }) 92 93 /* 94 * @tc.name:SensorJsTest003 95 * @tc.desc:verify app info is not null 96 * @tc.type: FUNC 97 * @tc.require: Issue Number 98 */ 99 it("SensorJsTest003", 0, async function (done) { 100 console.info('----------------------SensorJsTest003---------------------------'); 101 function onSensorCallback(data) { 102 console.info('SensorJsTest003 on error'); 103 expect(false).assertTrue(); 104 done(); 105 } 106 try { 107 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, onSensorCallback, {'interval': 100000000}, 5); 108 } catch (error) { 109 console.info(error); 110 expect(true).assertTrue(); 111 done(); 112 } 113 }) 114 115 /* 116 * @tc.name:SensorJsTest004 117 * @tc.desc:verify app info is not null 118 * @tc.type: FUNC 119 * @tc.require: Issue Number 120 */ 121 it("SensorJsTest004", 0, async function (done) { 122 sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback); 123 setTimeout(()=>{ 124 expect(true).assertTrue(); 125 done(); 126 }, 500); 127 }) 128 129 /* 130 * @tc.name:SensorJsTest005 131 * @tc.desc:verify app info is not null 132 * @tc.type: FUNC 133 * @tc.require: Issue Number 134 */ 135 it("SensorJsTest005", 0, async function (done) { 136 function onceSensorCallback(data) { 137 console.info('SensorJsTest005 on error'); 138 expect(false).assertTrue(); 139 done(); 140 } 141 try{ 142 sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, onceSensorCallback, 5); 143 } catch (error) { 144 console.info(error); 145 expect(true).assertTrue(); 146 done(); 147 } 148 }) 149 150 /* 151 * @tc.name:SensorJsTest006 152 * @tc.desc:verify app info is not null 153 * @tc.type: FUNC 154 * @tc.require: Issue Number 155 */ 156 it("SensorJsTest006", 0, async function (done) { 157 try { 158 sensor.off(string, ""); 159 } catch (error) { 160 console.info(error); 161 expect(true).assertTrue(); 162 done(); 163 } 164 }) 165 166 /* 167 * @tc.name:SensorJsTest007 168 * @tc.desc:verify app info is not null 169 * @tc.type: FUNC 170 * @tc.require: Issue Number 171 */ 172 it("SensorJsTest007", 0, async function (done) { 173 function onSensorCallback(data) { 174 console.info('SensorJsTest007 on error'); 175 expect(false).assertTrue(); 176 done(); 177 } 178 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, onSensorCallback); 179 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, onSensorCallback); 180 setTimeout(()=>{ 181 expect(true).assertTrue(); 182 done(); 183 }, 500); 184 }) 185 186 /* 187 * @tc.name:SensorJsTest008 188 * @tc.desc:verify app info is not null 189 * @tc.type: FUNC 190 * @tc.require: Issue Number 191 */ 192 it("SensorJsTest008", 0, async function (done) { 193 function onSensorCallback(data) { 194 console.info('SensorJsTest008 on error'); 195 expect(false).assertTrue(); 196 done(); 197 } 198 try { 199 sensor.off(1000000, onSensorCallback); 200 } catch (error) { 201 console.info(error); 202 expect(true).assertTrue(); 203 done(); 204 } 205 }) 206 207 /* 208 * @tc.name:SensorJsTest009 209 * @tc.desc:verify app info is not null 210 * @tc.type: FUNC 211 * @tc.require: Issue Number 212 */ 213 it("SensorJsTest009", 0, async function (done) { 214 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback); 215 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback2); 216 setTimeout(()=>{ 217 console.info('----------------------SensorJsTest009 off in---------------------------'); 218 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER); 219 console.info('----------------------SensorJsTest009 off end---------------------------'); 220 done(); 221 }, 1000); 222 }) 223 224 /* 225 * @tc.name:SensorJsTest010 226 * @tc.desc:verify app info is not null 227 * @tc.type: FUNC 228 * @tc.require: Issue Number 229 */ 230 it("SensorJsTest010", 0, async function (done) { 231 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback); 232 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback2); 233 setTimeout(()=>{ 234 console.info('----------------------SensorJsTest010 off in---------------------------'); 235 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback); 236 console.info('----------------------SensorJsTest010 off end---------------------------'); 237 }, 500); 238 setTimeout(()=>{ 239 console.info('----------------------SensorJsTest010 off in---------------------------'); 240 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback2); 241 console.info('----------------------SensorJsTest010 off end---------------------------'); 242 done(); 243 }, 1000); 244 }) 245 246 /* 247 * @tc.name:SensorJsTest011 248 * @tc.desc:verify app info is not null 249 * @tc.type: FUNC 250 * @tc.require: Issue Number 251 */ 252 it("SensorJsTest011", 0, async function (done) { 253 console.info('----------------------SensorJsTest011---------------------------'); 254 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback, {'interval': 100000000}); 255 sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback2); 256 setTimeout(()=>{ 257 console.info('----------------------SensorJsTest011 off in---------------------------'); 258 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER); 259 console.info('----------------------SensorJsTest011 off end---------------------------'); 260 done(); 261 }, 1000); 262 }) 263 264 /* 265 * @tc.name:SensorJsTest012 266 * @tc.desc:verify app info is not null 267 * @tc.type: FUNC 268 * @tc.require: Issue Number 269 */ 270 it("SensorJsTest012", 0, async function (done) { 271 console.info('----------------------SensorJsTest012---------------------------'); 272 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback, {'interval': 100000000}); 273 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback2, {'interval': 100000000}); 274 setTimeout(()=>{ 275 console.info('----------------------SensorJsTest012 off in---------------------------'); 276 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback); 277 console.info('----------------------SensorJsTest012 off end---------------------------'); 278 }, 500); 279 setTimeout(()=>{ 280 console.info('----------------------SensorJsTest012 off in---------------------------'); 281 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback2); 282 console.info('----------------------SensorJsTest012 off end---------------------------'); 283 done(); 284 }, 1000); 285 }) 286 287 /* 288 * @tc.name:SensorJsTest013 289 * @tc.desc:verify app info is not null 290 * @tc.type: FUNC 291 * @tc.require: Issue Number 292 */ 293 it("SensorJsTest013", 0, async function (done) { 294 console.info('----------------------SensorJsTest013---------------------------'); 295 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback, {'interval': 100000000}); 296 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER, callback2, {'interval': 100000000}); 297 setTimeout(()=>{ 298 console.info('----------------------SensorJsTest013 off in---------------------------'); 299 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ACCELEROMETER); 300 console.info('----------------------SensorJsTest013 off end---------------------------'); 301 done(); 302 }, 1000); 303 }) 304})