1/* 2 * Copyright (c) 2021-2024 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 account from '@ohos.account.appAccount' 16import commonevent from '@ohos.commonEventManager' 17import featureAbility from '@ohos.ability.featureAbility' 18import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect, TestType, Size, Level } from '@ohos/hypium' 19 20export default function ActsAccountOnOff() { 21 describe('ActsAccountOnOff', async function () { 22 async function sleep(delay) { 23 let timeoutId = null; 24 let promise = new Promise((resolve, reject) => { 25 timeoutId = setTimeout(() => resolve("done!"), delay); 26 }); 27 await promise 28 clearTimeout(timeoutId) 29 } 30 31 async function testInit(){ 32 console.info("====>testInit startAbility start===="); 33 featureAbility.startAbilityForResult( 34 { 35 want: 36 { 37 deviceId: "", 38 bundleName: "com.example.actsaccountsceneonoff", 39 abilityName: "com.example.actsaccountsceneonoff.MainAbility", 40 action: "action1", 41 parameters: 42 {}, 43 }, 44 }, 45 ) 46 } 47 48 /* 49 * @tc.number : ActsAccountOnOff_0300 50 * @tc.name : Subscribe/unsubscribe to the change event of application 51 * @tc.desc : Received the account information change to the authorized account of the subscription to change 52 * the credential 53 */ 54 it('ActsAccountOnOff_0300', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 55 await testInit(); 56 await sleep(500) 57 console.info("====>ActsAccountOnOff_0300 start===="); 58 let appAccountManager = account.createAppAccountManager(); 59 console.info("====>creat appAccountManager finish"); 60 console.info("====>add account ActsAccountOnOff_0300 start"); 61 await appAccountManager.createAccount("onoff_setcredential"); 62 console.info("====>enableAppAccess ActsAccountOnOff_0300 start"); 63 await appAccountManager.setAppAccess("onoff_setcredential", "com.example.actsaccountsceneonoff", true); 64 async function removeAccountCallback(err){ 65 console.info("====>subscribe ActsAccountOnOff_0300 finish====") 66 console.info("====>delete account 0300 err:" + JSON.stringify(err)); 67 try { 68 expect(err).assertEqual(null); 69 } catch (err) { 70 console.info('====>Assert err: ' + JSON.stringify(err)); 71 } 72 await sleep(500); 73 done(); 74 } 75 function unSubscriberCallback(err){ 76 console.info("====>unsubscribe 0300 err:" + JSON.stringify(err)); 77 appAccountManager.removeAccount("onoff_setcredential", removeAccountCallback); 78 } 79 async function subscriberCallback(err, data){ 80 console.info("====>subscriberCallback 0300 data:" + JSON.stringify(data)); 81 try { 82 expect(data.event).assertEqual("account_on_change_setcredential"); 83 if (data.data == "ON_SUCCESS") { 84 console.info("====>setCredential start===="); 85 await appAccountManager.setCredential("onoff_setcredential", "credentialType", "credential"); 86 console.info("====>setCredential finish===="); 87 } else { 88 expect(data.data).assertEqual("SUCCESS"); 89 commonevent.unsubscribe(subscriber, unSubscriberCallback); 90 } 91 } catch (err) { 92 console.info('====>subscriberCallback err: ' + JSON.stringify(err)); 93 } 94 } 95 async function publishCallback(err){ 96 console.info("====>main publish call back err:" + JSON.stringify(err)); 97 } 98 let commonEventSubscribeInfo = { 99 events: ["account_on_change_setcredential"] 100 } 101 await sleep(500) 102 let subscriber 103 console.info("====>createSubscriber start"); 104 await commonevent.createSubscriber(commonEventSubscribeInfo).then(function (data){ 105 subscriber = data; 106 console.info("====>subscribe start"); 107 commonevent.subscribe(subscriber, subscriberCallback); 108 }) 109 let commonEventPublishData = { 110 code: 9 111 } 112 await sleep(500) 113 console.info("====>publish event account_on_change 0300===="); 114 commonevent.publish("account_on_change", commonEventPublishData, publishCallback); 115 }); 116 117 /* 118 * @tc.number : ActsAccountOnOff_0400 119 * @tc.name : Subscribe/unsubscribe to the change event of application 120 * @tc.desc : Received the account information change to the authorized account of the subscription to delete 121 * authorized account 122 */ 123 it('ActsAccountOnOff_0400', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL0, async function (done) { 124 await testInit(); 125 await sleep(500) 126 console.info("====>ActsAccountOnOff_0400 start===="); 127 let appAccountManager = account.createAppAccountManager(); 128 console.info("====>creat appAccountManager finish"); 129 console.info("====>add first account ActsAccountOnOff_0400 start"); 130 await appAccountManager.createAccount("onoff_removeFir"); 131 console.info("====>add second account ActsAccountOnOff_0400 start"); 132 await appAccountManager.createAccount("onoff_removeSec"); 133 console.info("====>enableAppAccess first ActsAccountOnOff_0400 start"); 134 await appAccountManager.setAppAccess("onoff_removeFir", "com.example.actsaccountsceneonoff", true); 135 console.info("====>enableAppAccess second ActsAccountOnOff_0400 start"); 136 await appAccountManager.setAppAccess("onoff_removeSec", "com.example.actsaccountsceneonoff", true); 137 async function removeAccountCallback(err){ 138 console.info("====>remove account 0400 err:" + JSON.stringify(err)); 139 try { 140 expect(err).assertEqual(null); 141 } catch (err) { 142 console.info('====>Assert err: ' + JSON.stringify(err)); 143 } 144 await sleep(500); 145 done(); 146 } 147 function unSubscriberCallback(err){ 148 console.info("====>unsubscribe 0400 err:" + JSON.stringify(err)); 149 appAccountManager.removeAccount("onoff_removeFir", removeAccountCallback); 150 console.info("====>subscribe ActsAccountOnOff_0400 finish====") 151 } 152 async function subscriberCallback(err, data){ 153 console.info("====>subscriberCallback 0400 data:" + JSON.stringify(data)); 154 try { 155 expect(data.event).assertEqual("account_on_remove_another"); 156 if (data.data == "ON_SUCCESS") { 157 console.info("====>removeAccount start===="); 158 await appAccountManager.removeAccount("onoff_removeSec"); 159 console.info("====>removeAccount finish===="); 160 } else { 161 expect(data.data).assertEqual("SUCCESS"); 162 commonevent.unsubscribe(subscriber, unSubscriberCallback); 163 } 164 } catch (err) { 165 console.info('====>subscriberCallback err: ' + JSON.stringify(err)); 166 } 167 } 168 async function publishCallback(err){ 169 console.info("====>main publish call back err:" + JSON.stringify(err)); 170 } 171 let commonEventSubscribeInfo = { 172 events: ["account_on_remove_another"] 173 } 174 await sleep(500) 175 let subscriber 176 console.info("====>createSubscriber start"); 177 await commonevent.createSubscriber(commonEventSubscribeInfo).then(function (data){ 178 subscriber = data; 179 console.info("====>subscribe start"); 180 commonevent.subscribe(subscriber, subscriberCallback); 181 }) 182 let commonEventPublishData = { 183 code: 10 184 } 185 await sleep(500) 186 console.info("====>publish event account_on_change 0400===="); 187 commonevent.publish("account_on_change", commonEventPublishData, publishCallback); 188 }); 189 190 /* 191 * @tc.number : ActsAccountOnOff_0500 192 * @tc.name : Subscribe/unsubscribe to the change event of application 193 * @tc.desc : Received the account information change to the authorized account of the subscription to delete 194 * the only authorized account 195 */ 196 it('ActsAccountOnOff_0500', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL4, async function (done) { 197 await testInit(); 198 await sleep(500) 199 console.info("====>ActsAccountOnOff_0500 start===="); 200 let appAccountManager = account.createAppAccountManager(); 201 console.info("====>creat appAccountManager finish"); 202 console.info("====>add account ActsAccountOnOff_0500 start"); 203 await appAccountManager.createAccount("onoff_remove"); 204 console.info("====>enableAppAccess ActsAccountOnOff_0500 start"); 205 await appAccountManager.setAppAccess("onoff_remove", "com.example.actsaccountsceneonoff", true); 206 async function removeAccountCallback(err){ 207 console.info("====>remove account 0500 err:" + JSON.stringify(err)); 208 console.info("====>subscribe ActsAccountOnOff_0500 finish====") 209 await sleep(500); 210 done(); 211 } 212 function unSubscriberCallback(err){ 213 console.info("====>unsubscribe 0500 err:" + JSON.stringify(err)); 214 appAccountManager.removeAccount("onoff_remove", removeAccountCallback); 215 } 216 async function subscriberCallback(err, data){ 217 console.info("====>subscriberCallback 0500 data:" + JSON.stringify(data)); 218 try { 219 expect(data.event).assertEqual("account_on_change_remove"); 220 if (data.data == "ON_SUCCESS") { 221 console.info("====>removeAccount start===="); 222 await appAccountManager.removeAccount("onoff_remove"); 223 console.info("====>removeAccount finish===="); 224 } else { 225 expect(data.data).assertEqual("SUCCESS"); 226 commonevent.unsubscribe(subscriber, unSubscriberCallback); 227 } 228 } catch (err) { 229 console.info('====>subscriberCallback err: ' + JSON.stringify(err)); 230 } 231 } 232 async function publishCallback(err){ 233 console.info("====>main publish call back err:" + JSON.stringify(err)); 234 } 235 let commonEventSubscribeInfo = { 236 events: ["account_on_change_remove"] 237 } 238 await sleep(500) 239 let subscriber 240 console.info("====>createSubscriber start"); 241 await commonevent.createSubscriber(commonEventSubscribeInfo).then(function (data){ 242 subscriber = data; 243 console.info("====>subscribe start"); 244 commonevent.subscribe(subscriber, subscriberCallback); 245 }) 246 let commonEventPublishData = { 247 code: 11 248 } 249 await sleep(500) 250 console.info("====>publish event account_on_change 0500===="); 251 commonevent.publish("account_on_change", commonEventPublishData, publishCallback); 252 }); 253 254 /* 255 * @tc.number : ActsAccountOnOff_0600 256 * @tc.name : Subscribe/unsubscribe to the change event of application 257 * @tc.desc : Received the account information change to the authorized account of the subscription to cancel 258 * authorized account 259 */ 260 it('ActsAccountOnOff_0600', TestType.FUNCTION | Size.MEDIUMTEST | Level.LEVEL4, async function (done) { 261 await testInit(); 262 await sleep(500) 263 console.info("====>ActsAccountOnOff_0600 start===="); 264 let appAccountManager = account.createAppAccountManager(); 265 console.info("====>creat appAccountManager finish"); 266 console.info("====>add first account ActsAccountOnOff_0600 start"); 267 await appAccountManager.createAccount("onoff_disableFir"); 268 console.info("====>add second account ActsAccountOnOff_0600 start"); 269 await appAccountManager.createAccount("onoff_disableSec"); 270 console.info("====>enableAppAccess first ActsAccountOnOff_0600 start"); 271 await appAccountManager.setAppAccess("onoff_disableFir", "com.example.actsaccountsceneonoff", true); 272 console.info("====>enableAppAccess second ActsAccountOnOff_0600 start"); 273 await appAccountManager.setAppAccess("onoff_disableSec", "com.example.actsaccountsceneonoff", true); 274 function removeAccountCallback(err){ 275 console.info("====>remove first account 0600 err:" + JSON.stringify(err)); 276 try { 277 expect(err).assertEqual(null); 278 } catch (err) { 279 console.info('====>Assert err: ' + JSON.stringify(err)); 280 } 281 appAccountManager.removeAccount("onoff_disableSec", async (err)=>{ 282 console.info("====>remove second account 0600 err:" + JSON.stringify(err)); 283 console.info("====>subscribe ActsAccountOnOff_0600 finish====") 284 try { 285 expect(err).assertEqual(null); 286 } catch (err) { 287 console.info('====>Assert err: ' + JSON.stringify(err)); 288 } 289 await sleep(500); 290 done(); 291 }); 292 } 293 function unSubscriberCallback(err){ 294 console.info("====>unsubscribe 0600 err:" + JSON.stringify(err)); 295 appAccountManager.removeAccount("onoff_disableFir", removeAccountCallback); 296 } 297 async function subscriberCallback(err, data){ 298 console.info("====>subscriberCallback 0600 data:" + JSON.stringify(data)); 299 try { 300 expect(data.event).assertEqual("account_on_set_another_disable"); 301 if (data.data == "ON_SUCCESS") { 302 console.info("====>disableAppAccess start===="); 303 await appAccountManager.setAppAccess("onoff_disableSec", "com.example.actsaccountsceneonoff", false); 304 console.info("====>disableAppAccess finish===="); 305 } else { 306 expect(data.data).assertEqual("SUCCESS"); 307 commonevent.unsubscribe(subscriber, unSubscriberCallback); 308 } 309 } catch (err) { 310 console.info('====>subscriberCallback err: ' + JSON.stringify(err)); 311 } 312 } 313 async function publishCallback(err){ 314 console.info("====>main publish call back err:" + JSON.stringify(err)); 315 } 316 let commonEventSubscribeInfo = { 317 events: ["account_on_set_another_disable"] 318 } 319 await sleep(500) 320 let subscriber 321 console.info("====>createSubscriber start"); 322 await commonevent.createSubscriber(commonEventSubscribeInfo).then(function (data){ 323 subscriber = data; 324 console.info("====>subscribe start"); 325 commonevent.subscribe(subscriber, subscriberCallback); 326 }) 327 let commonEventPublishData = { 328 code: 12 329 } 330 await sleep(500) 331 console.info("====>publish event account_on_change 0600===="); 332 commonevent.publish("account_on_change", commonEventPublishData, publishCallback); 333 }); 334 335 }) 336}