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 expect(typeof(data.w)).assertEqual("number"); 26 } 27 28 function callback2(data) { 29 console.info("callback2" + JSON.stringify(data)); 30 expect(typeof(data.x)).assertEqual("number"); 31 expect(typeof(data.y)).assertEqual("number"); 32 expect(typeof(data.z)).assertEqual("number"); 33 expect(typeof(data.w)).assertEqual("number"); 34 } 35 36 beforeAll(function() { 37 /* 38 * @tc.setup: setup invoked before all testcases 39 */ 40 console.info('beforeAll caled') 41 }) 42 43 afterAll(function() { 44 /* 45 * @tc.teardown: teardown invoked after all testcases 46 */ 47 console.info('afterAll caled') 48 }) 49 50 beforeEach(function() { 51 /* 52 * @tc.setup: setup invoked before each testcases 53 */ 54 console.info('beforeEach caled') 55 }) 56 57 afterEach(function() { 58 /* 59 * @tc.teardown: teardown invoked after each testcases 60 */ 61 console.info('afterEach caled') 62 }) 63 64 /* 65 * @tc.name:SensorJsTest001 66 * @tc.desc:verify app info is not null 67 * @tc.type: FUNC 68 * @tc.require: Issue Number 69 */ 70 it("SensorJsTest001", 0, async function (done) { 71 console.info('----------------------SensorJsTest001---------------------------'); 72 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback); 73 setTimeout(()=>{ 74 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR); 75 done(); 76 }, 500); 77 }) 78 79 /* 80 * @tc.name:SensorJsTest002 81 * @tc.desc:verify app info is not null 82 * @tc.type: FUNC 83 * @tc.require: Issue Number 84 */ 85 it("SensorJsTest002", 0, async function (done) { 86 console.info('----------------------SensorJsTest002---------------------------'); 87 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback, {'interval': 100000000}); 88 setTimeout(()=>{ 89 console.info('----------------------SensorJsTest002 off in---------------------------'); 90 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR); 91 console.info('----------------------SensorJsTest002 off end---------------------------'); 92 done(); 93 }, 500); 94 }) 95 96 /* 97 * @tc.name:SensorJsTest003 98 * @tc.desc:verify app info is not null 99 * @tc.type: FUNC 100 * @tc.require: Issue Number 101 */ 102 it("SensorJsTest003", 0, function (done) { 103 console.info('----------------------SensorJsTest003---------------------------'); 104 function onSensorCallback(data) { 105 console.info('SensorJsTest003 on error'); 106 expect(false).assertTrue(); 107 done(); 108 } 109 try { 110 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, onSensorCallback, {'interval': 100000000}, 5); 111 } catch (error) { 112 console.info(error); 113 expect(true).assertTrue(); 114 done(); 115 } 116 }) 117 118 /* 119 * @tc.name:SensorJsTest004 120 * @tc.desc:verify app info is not null 121 * @tc.type: FUNC 122 * @tc.require: Issue Number 123 */ 124 it("SensorJsTest004", 0, async function (done) { 125 sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback); 126 setTimeout(()=>{ 127 expect(true).assertTrue(); 128 done(); 129 }, 500); 130 }) 131 132 /* 133 * @tc.name:SensorJsTest005 134 * @tc.desc:verify app info is not null 135 * @tc.type: FUNC 136 * @tc.require: Issue Number 137 */ 138 it("SensorJsTest005", 0, function (done) { 139 function onceSensorCallback(data) { 140 console.info('SensorJsTest005 on error'); 141 expect(false).assertTrue(); 142 done(); 143 } 144 try{ 145 sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, onceSensorCallback, 5); 146 } catch (error) { 147 console.info(error); 148 expect(true).assertTrue(); 149 done(); 150 } 151 }) 152 153 /* 154 * @tc.name:SensorJsTest006 155 * @tc.desc:verify app info is not null 156 * @tc.type: FUNC 157 * @tc.require: Issue Number 158 */ 159 it("SensorJsTest006", 0, async function (done) { 160 try { 161 sensor.off(string, ""); 162 } catch (error) { 163 console.info(error); 164 expect(true).assertTrue(); 165 done(); 166 } 167 }) 168 169 /* 170 * @tc.name:SensorJsTest007 171 * @tc.desc:verify app info is not null 172 * @tc.type: FUNC 173 * @tc.require: Issue Number 174 */ 175 it("SensorJsTest007", 0, async function (done) { 176 function onSensorCallback(data) { 177 console.info('SensorJsTest007 on error'); 178 expect(false).assertTrue(); 179 done(); 180 } 181 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, onSensorCallback); 182 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, onSensorCallback); 183 setTimeout(()=>{ 184 expect(true).assertTrue(); 185 done(); 186 }, 500); 187 }) 188 189 /* 190 * @tc.name:SensorJsTest008 191 * @tc.desc:verify app info is not null 192 * @tc.type: FUNC 193 * @tc.require: Issue Number 194 */ 195 it("SensorJsTest008", 0, async function (done) { 196 function onSensorCallback(data) { 197 console.info('SensorJsTest008 on error'); 198 expect(false).assertTrue(); 199 done(); 200 } 201 try { 202 sensor.off(1000000, onSensorCallback); 203 } catch (error) { 204 console.info(error); 205 expect(true).assertTrue(); 206 done(); 207 } 208 }) 209 210 /* 211 * @tc.name:SensorJsTest009 212 * @tc.desc:verify app info is not null 213 * @tc.type: FUNC 214 * @tc.require: Issue Number 215 */ 216 it("SensorJsTest009", 0, async function (done) { 217 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback); 218 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback2); 219 setTimeout(()=>{ 220 console.info('----------------------SensorJsTest009 off in---------------------------'); 221 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR); 222 console.info('----------------------SensorJsTest009 off end---------------------------'); 223 done(); 224 }, 1000); 225 }) 226 227 /* 228 * @tc.name:SensorJsTest010 229 * @tc.desc:verify app info is not null 230 * @tc.type: FUNC 231 * @tc.require: Issue Number 232 */ 233 it("SensorJsTest010", 0, async function (done) { 234 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback); 235 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback2); 236 setTimeout(()=>{ 237 console.info('----------------------SensorJsTest010 off in---------------------------'); 238 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback); 239 console.info('----------------------SensorJsTest010 off end---------------------------'); 240 }, 500); 241 setTimeout(()=>{ 242 console.info('----------------------SensorJsTest010 off in---------------------------'); 243 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback2); 244 console.info('----------------------SensorJsTest010 off end---------------------------'); 245 done(); 246 }, 1000); 247 }) 248 249 /* 250 * @tc.name:SensorJsTest011 251 * @tc.desc:verify app info is not null 252 * @tc.type: FUNC 253 * @tc.require: Issue Number 254 */ 255 it("SensorJsTest011", 0, async function (done) { 256 console.info('----------------------SensorJsTest011---------------------------'); 257 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback, {'interval': 100000000}); 258 sensor.once(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback2); 259 setTimeout(()=>{ 260 console.info('----------------------SensorJsTest011 off in---------------------------'); 261 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR); 262 console.info('----------------------SensorJsTest011 off end---------------------------'); 263 done(); 264 }, 1000); 265 }) 266 267 /* 268 * @tc.name:SensorJsTest012 269 * @tc.desc:verify app info is not null 270 * @tc.type: FUNC 271 * @tc.require: Issue Number 272 */ 273 it("SensorJsTest012", 0, async function (done) { 274 console.info('----------------------SensorJsTest012---------------------------'); 275 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback, {'interval': 100000000}); 276 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback2, {'interval': 100000000}); 277 setTimeout(()=>{ 278 console.info('----------------------SensorJsTest012 off in---------------------------'); 279 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback); 280 console.info('----------------------SensorJsTest012 off end---------------------------'); 281 }, 500); 282 setTimeout(()=>{ 283 console.info('----------------------SensorJsTest012 off in---------------------------'); 284 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback2); 285 console.info('----------------------SensorJsTest012 off end---------------------------'); 286 done(); 287 }, 1000); 288 }) 289 290 /* 291 * @tc.name:SensorJsTest013 292 * @tc.desc:verify app info is not null 293 * @tc.type: FUNC 294 * @tc.require: Issue Number 295 */ 296 it("SensorJsTest013", 0, async function (done) { 297 console.info('----------------------SensorJsTest013---------------------------'); 298 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback, {'interval': 100000000}); 299 sensor.on(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR, callback2, {'interval': 100000000}); 300 setTimeout(()=>{ 301 console.info('----------------------SensorJsTest013 off in---------------------------'); 302 sensor.off(sensor.SensorType.SENSOR_TYPE_ID_ROTATION_VECTOR); 303 console.info('----------------------SensorJsTest013 off end---------------------------'); 304 done(); 305 }, 1000); 306 }) 307})