1/* 2 * Copyright (c) 2022 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 screenLock from '@ohos.screenLock'; 16 17import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from 'deccjsunit/index' 18 19const SLEEP_TIME = 1000; 20const PERMISSION_ERROR = 201; 21const PARAMETER_ERROR = 401; 22const UNFOCUSED_ERROR = 13200003; 23describe("ScreenlockJsTest", function () { 24 beforeAll(function () { 25 // input testsuite setup step,setup invoked before all testcases 26 console.info('beforeAll caled') 27 }) 28 29 afterAll(function () { 30 // input testsuite teardown step,teardown invoked after all testcases 31 console.info('afterAll caled') 32 }) 33 34 beforeEach(function () { 35 // input testcase setup step,setup invoked before each testcases 36 console.info('beforeEach caled') 37 }) 38 39 afterEach(function () { 40 // input testcase teardown step,teardown invoked after each testcases 41 console.info('afterEach caled') 42 }) 43 44 /* 45 * @tc.name:SUB_MISC_THEME_screenLock_API_0001 46 * @tc.number:SUB_MISC_THEME_screenLock_API_0001 47 * @tc.desc: Checks whether the screen lock of the current device is secure. 48 * @tc.type: Function 49 * @tc.require: SR000HHEJQ 50 */ 51 it("SUB_MISC_THEME_screenLock_API_0001", 0, function (done) { 52 try { 53 let ret = screenLock.isLocked(); 54 screenLock.isScreenLocked((err, data) => { 55 console.info("SUB_MISC_THEME_screenLock_API_0001 screen's status is " + data); 56 expect(data === ret).assertTrue(); 57 done(); 58 }); 59 } catch (error) { 60 console.error(`SUB_MISC_THEME_screenLock_API_0001: ${error.code}, ${error.message}`); 61 done(); 62 } 63 }) 64 /* 65 * @tc.name:SUB_MISC_THEME_screenLock_API_0002 66 * @tc.number:SUB_MISC_THEME_screenLock_API_0002 67 * @tc.desc: Checks whether the screen is currently locked. 68 * @tc.type: Function 69 * @tc.require: SR000HHEJQ 70 */ 71 it("SUB_MISC_THEME_screenLock_API_0002", 0, function (done) { 72 try { 73 screenLock.isSecureMode((err, data) => { 74 console.info("SUB_MISC_THEME_screenLock_API_0002 secureMode's result is " + data); 75 expect(data === false).assertTrue(); 76 done(); 77 }); 78 } catch (error) { 79 console.error(`SUB_MISC_THEME_screenLock_API_0002: ${error.code}, ${error.message}`); 80 done(); 81 } 82 }) 83 /* 84 * @tc.name:SUB_MISC_THEME_screenLock_API_0003 85 * @tc.number:SUB_MISC_THEME_screenLock_API_0003 86 * @tc.desc: Unlock the screen 87 * @tc.type: Function 88 * @tc.require: SR000HHEJQ 89 */ 90 it("SUB_MISC_THEME_screenLock_API_0003", 0, function (done) { 91 try { 92 screenLock.unlock((err, data) => { 93 if (err) { 94 console.info("unlock filed: error.code : " + err.code + "error.message :" + err.message); 95 expect(err.code === UNFOCUSED_ERROR).assertTrue(); 96 } else { 97 console.info("SUB_MISC_THEME_screenLock_API_0003 unlock screen success."); 98 let ret = screenLock.isLocked(); 99 expect(ret).assertFalse(); 100 expect(data).assertTrue(); 101 } 102 done(); 103 }); 104 } catch (error) { 105 console.error(`SUB_MISC_THEME_screenLock_API_0003: ${error.code}, ${error.message}`); 106 done(); 107 } 108 }) 109 /* 110 * @tc.name:SUB_MISC_THEME_screenLock_API_0004 111 * @tc.number:SUB_MISC_THEME_screenLock_API_0004 112 * @tc.desc: Unlock the screen 113 * @tc.type: Function 114 * @tc.require: SR000HHEJQ 115 */ 116 it("SUB_MISC_THEME_screenLock_API_0004", 0, function (done) { 117 try { 118 screenLock.unlock().then((data) => { 119 console.info("SUB_MISC_THEME_screenLock_API_0004 unlock screen success."); 120 let ret = screenLock.isLocked(); 121 expect(ret).assertFalse(); 122 expect(data).assertTrue(); 123 done(); 124 }).catch((err) => { 125 console.error(`SUB_MISC_THEME_screenLock_API_0004: ${err.code}, ${err.message}`); 126 expect(err.code === UNFOCUSED_ERROR).assertTrue(); 127 done(); 128 }); 129 } catch (error) { 130 console.error(`SUB_MISC_THEME_screenLock_API_0004: ${error.code}, ${error.message}`); 131 done(); 132 } 133 }) 134 /* 135 * @tc.name:SUB_MISC_THEME_screenLock_API_0005 136 * @tc.number:SUB_MISC_THEME_screenLock_API_0005 137 * @tc.desc: Lock the screen 138 * @tc.type: Function 139 * @tc.require: SR000HHEJQ 140 */ 141 it("SUB_MISC_THEME_screenLock_API_0005", 0, function (done) { 142 try { 143 screenLock.lock((err, data) => { 144 if (err) { 145 console.error(`SUB_MISC_THEME_screenLock_API_0005: ${err.code}, ${err.message}`); 146 expect(err.code === PERMISSION_ERROR).assertTrue(); 147 } else { 148 console.info("SUB_MISC_THEME_screenLock_API_0005 lock screen success."); 149 let ret = screenLock.isLocked(); 150 expect(ret).assertTrue(); 151 expect(data).assertTrue(); 152 } 153 done(); 154 }); 155 } catch (error) { 156 console.error(`SUB_MISC_THEME_screenLock_API_0005: ${error.code}, ${error.message}`); 157 done(); 158 } 159 }) 160 /* 161 * @tc.name:SUB_MISC_THEME_screenLock_API_0006 162 * @tc.number:SUB_MISC_THEME_screenLock_API_0006 163 * @tc.desc: Lock the screen 164 * @tc.type: Function 165 * @tc.require: SR000HHEJQ 166 */ 167 it("SUB_MISC_THEME_screenLock_API_0006", 0, function (done) { 168 try { 169 screenLock.lock().then((data) => { 170 console.info("SUB_MISC_THEME_screenLock_API_0006 lock screen success."); 171 let ret = screenLock.isLocked(); 172 expect(ret).assertTrue(); 173 expect(data).assertTrue(); 174 done(); 175 }).catch((err) => { 176 console.error(`SUB_MISC_THEME_screenLock_API_0006: ${err.code}, ${err.message}`); 177 expect(err.code === PERMISSION_ERROR).assertTrue(); 178 done(); 179 }); 180 181 } catch (error) { 182 console.error(`SUB_MISC_THEME_screenLock_API_0006: ${error.code}, ${error.message}`); 183 done(); 184 } 185 }) 186 /* 187 * @tc.name:SUB_MISC_THEME_screenLock_API_0007 188 * @tc.number:SUB_MISC_THEME_screenLock_API_0007 189 * @tc.desc: Register system event to screenlockSA. 190 * @tc.type: Function 191 * @tc.require: SR000HHEJQ 192 */ 193 it("SUB_MISC_THEME_screenLock_API_0007", 0, function (done) { 194 try { 195 let ret = screenLock.onSystemEvent((data) => {}); 196 expect(ret).assertTrue(); 197 done(); 198 } catch (error) { 199 console.error(`SUB_MISC_THEME_screenLock_API_0007: ${error.code}, ${error.message}`); 200 expect(error.code === PERMISSION_ERROR).assertTrue(); 201 done(); 202 } 203 }) 204 /* 205 * @tc.name:SUB_MISC_THEME_screenLock_API_0008 206 * @tc.number:SUB_MISC_THEME_screenLock_API_0008 207 * @tc.desc: screenlockAPP send event to screenlockSA 208 * @tc.type: Function 209 * @tc.require: SR000HHEJQ 210 */ 211 it("SUB_MISC_THEME_screenLock_API_0008", 0, function (done) { 212 try { 213 screenLock.sendScreenLockEvent('unlockScreenResult', 0, (err, data) => { 214 if (err) { 215 console.error(`SUB_MISC_THEME_screenLock_API_0008: ${err.code}, ${err.message}`); 216 expect(err.code === PERMISSION_ERROR).assertTrue(); 217 } else { 218 console.info("SUB_MISC_THEME_screenLock_API_0008: sendScreenLockEvent success "); 219 } 220 done(); 221 }); 222 } catch (error) { 223 console.error(`SUB_MISC_THEME_screenLock_API_0008: ${error.code}, ${error.message}`); 224 done(); 225 } 226 }) 227 /* 228 * @tc.name:SUB_MISC_THEME_screenLock_API_0009 229 * @tc.number:SUB_MISC_THEME_screenLock_API_0009 230 * @tc.desc: screenlockAPP send event to screenlockSA 231 * @tc.type: Function 232 * @tc.require: SR000HHEJQ 233 */ 234 it("SUB_MISC_THEME_screenLock_API_0009", 0, function (done) { 235 screenLock.sendScreenLockEvent('unlockScreenResult', 0).then((data) => { 236 console.info("SUB_MISC_THEME_screenLock_API_0009: sendScreenLockEvent success "); 237 expect(data).assertTrue(); 238 done(); 239 }).catch((err) => { 240 console.error(`SUB_MISC_THEME_screenLock_API_0009: ${err.code}, ${err.message}`); 241 expect(err.code === PERMISSION_ERROR).assertTrue(); 242 done(); 243 }); 244 }) 245 /* 246 * @tc.name:SUB_MISC_THEME_screenLock_API_0010 247 * @tc.number:SUB_MISC_THEME_screenLock_API_0010 248 * @tc.desc: Checks whether the screen lock of the current device is secure. 249 * @tc.type: Function 250 * @tc.require: SR000HHEJQ 251 */ 252 it("SUB_MISC_THEME_screenLock_API_0010", 0, function (done) { 253 try { 254 let ret = screenLock.isLocked(); 255 screenLock.isScreenLocked().then((result) => { 256 expect(ret === result).assertTrue(); 257 done(); 258 }).catch((error) => { 259 console.error(`SUB_MISC_THEME_screenLock_API_0010: ${error.code}, ${error.message}`); 260 expect(false).assertTrue(); 261 done(); 262 }); 263 } catch (error) { 264 console.error(`SUB_MISC_THEME_screenLock_API_0010: ${error.code}, ${error.message}`); 265 done(); 266 } 267 }) 268 /* 269 * @tc.name:SUB_MISC_THEME_screenLock_API_0011 270 * @tc.number:SUB_MISC_THEME_screenLock_API_0011 271 * @tc.desc: Checks whether the screen is currently locked. 272 * @tc.type: Function 273 * @tc.require: SR000HHEJQ 274 */ 275 it("SUB_MISC_THEME_screenLock_API_0011", 0, function (done) { 276 try { 277 screenLock.isSecureMode().then((result) => { 278 expect(result === false).assertTrue(); 279 done(); 280 }).catch((error) => { 281 console.error(`SUB_MISC_THEME_screenLock_API_0011: ${error.code}, ${error.message}`); 282 expect(false).assertTrue(); 283 done(); 284 }); 285 } catch (error) { 286 console.error(`SUB_MISC_THEME_screenLock_API_0011: ${error.code}, ${error.message}`); 287 done(); 288 } 289 }) 290 /* 291 * @tc.name:SUB_MISC_THEME_screenLock_API_0012 292 * @tc.number:SUB_MISC_THEME_screenLock_API_0012 293 * @tc.desc: Unlock the screen 294 * @tc.type: Function 295 * @tc.require: SR000HHEJQ 296 */ 297 it("SUB_MISC_THEME_screenLock_API_0012", 0, function (done) { 298 try { 299 screenLock.unlockScreen((err, data) => { 300 if (err) { 301 console.error(`SUB_MISC_THEME_screenLock_API_0012: ${err.code}, ${err.message}`); 302 expect(err.code == UNFOCUSED_ERROR).assertTrue(); 303 } else { 304 console.info("SUB_MISC_THEME_screenLock_API_0012: unlock screen success."); 305 let ret = screenLock.isLocked(); 306 expect(ret).assertFalse(); 307 } 308 expect(data == undefined).assertTrue(); 309 done(); 310 }); 311 } catch (error) { 312 console.error(`SUB_MISC_THEME_screenLock_API_0012: ${error.code}, ${error.message}`); 313 done(); 314 } 315 }) 316 /* 317 * @tc.name:SUB_MISC_THEME_screenLock_API_0013 318 * @tc.number:SUB_MISC_THEME_screenLock_API_0013 319 * @tc.desc: Unlock the screen 320 * @tc.type: Function 321 * @tc.require: SR000HHEJQ 322 */ 323 it("SUB_MISC_THEME_screenLock_API_0013", 0, function (done) { 324 try { 325 screenLock.unlockScreen().then((data) => { 326 console.info("SUB_MISC_THEME_screenLock_API_0013: send unlock issue success retCode."); 327 let ret = screenLock.isLocked(); 328 expect(ret).assertFalse(); 329 expect(data == undefined).assertTrue(); 330 done(); 331 }).catch((err) => { 332 console.error(`SUB_MISC_THEME_screenLock_API_0013: ${err.code}, ${err.message}`); 333 expect(err.code == UNFOCUSED_ERROR).assertTrue(); 334 done(); 335 }); 336 } catch (error) { 337 console.error(`SUB_MISC_THEME_screenLock_API_0013: ${error.code}, ${error.message}`); 338 done(); 339 } 340 }) 341 342 /* 343 * @tc.name:SUB_MISC_THEME_screenLock_API_0014 344 * @tc.number:SUB_MISC_THEME_screenLock_API_0014 345 * @tc.desc: screenlockAPP send event to screenlockSA 346 * @tc.type: Function 347 * @tc.require: SR000HHEJQ 348 */ 349 it("SUB_MISC_THEME_screenLock_API_00014", 0, function (done) { 350 try { 351 screenLock.sendScreenLockEvent('testparam', 0, (err, data) => { 352 if (err) { 353 console.error(`SUB_MISC_THEME_screenLock_API_00014: ${err.code}, ${err.message}`); 354 expect(err.code === PARAMETER_ERROR).assertTrue(); 355 } else { 356 console.info("SUB_MISC_THEME_screenLock_API_00014: sendScreenLockEvent success "); 357 } 358 done(); 359 }); 360 } catch (error) { 361 console.error(`SUB_MISC_THEME_screenLock_API_00014: ${error.code}, ${error.message}`); 362 done(); 363 } 364 }) 365 366})