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