ChangeOnOff.js 22.1 KB
Newer Older
1
15829070344 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import account from '@ohos.account.appAccount'
import commonevent from '@ohos.commonEvent'
import featureAbility from '@ohos.ability.featureAbility'
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'

const TIMEOUT = 5000;
export default function ActsAccountOnOff() {
    describe('ActsAccountOnOff', async function () {

        function sleep(delay) {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    resolve()
                }, delay)
            }).then(() => {
                console.info(`sleep #{time} over ...`)
            })
        }
        beforeAll(async function (done) {            
            done();
        });

        async function testInit(){
            console.debug("====>startAbility start====");
            await featureAbility.startAbility(
                {
                    want:
                    {
                        deviceId: "",
                        bundleName: "com.example.actsaccountsceneonoff",
                        abilityName: "com.example.actsaccountsceneonoff.MainAbility",
                        action: "action1",
                        parameters:
                        {},
                    },
                },
            );
            await sleep(TIMEOUT);
        }

        /*
        * @tc.number    : ActsAccountOnOff_0300
        * @tc.name      : Subscribe/unsubscribe to the change event of application
        * @tc.desc      : Received the account information change to the authorized account of the subscription to change
        *                 the credential
        */
        it('ActsAccountOnOff_0300', 0, async function (done) {
            testInit();
            console.debug("====>ActsAccountOnOff_0300 start====");
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add account ActsAccountOnOff_0300 start");
            await appAccountManager.createAccount("onoff_setcredential");
            console.debug("====>enableAppAccess ActsAccountOnOff_0300 start");
            await appAccountManager.setAppAccess("onoff_setcredential", "com.example.actsaccountsceneonoff", true);
            function unSubscriberCallback(err){
                console.debug("====>unsubscribe 0300 err:" + JSON.stringify(err));
            }
            function removeAccountCallback(err){
                console.debug("====>delete account 0300 err:" + JSON.stringify(err));
                expect(err).assertEqual(null);
                done();
            }
            function subscriberCallback(err, data){
                console.debug("====>subscriberCallback 0300 data:" + JSON.stringify(data));
                expect(data.event).assertEqual("account_on_change_setcredential");
                expect(data.data).assertEqual("SUCCESS");
                commonevent.unsubscribe(subscriber, unSubscriberCallback);
                appAccountManager.removeAccount("onoff_setcredential", removeAccountCallback);
            }
            function publishCallback(err){
                console.debug("====>publish call back err:" + JSON.stringify(err));
                setTimeout(async function (){
                    console.debug("====>setCredential start====");
                    try{
                        await appAccountManager.setCredential("onoff_setcredential", "credentialType", "credential");
                    }
                    catch(err){
                        console.error("====>setCredential fail err:" + JSON.stringify(err));
                        expect().assertFail();
                        done();
                    }
                    console.debug("====>setCredential finish====");
                }, 500)
            }
            var commonEventSubscribeInfo = {
                events: ["account_on_change_setcredential"]
            }
            var subscriber
            commonevent.createSubscriber(commonEventSubscribeInfo).then(function (data){
                subscriber = data;
                commonevent.subscribe(subscriber, subscriberCallback);
                console.debug("====>subscribe ActsAccountOnOff_0300 finish====")
            });

            var commonEventPublishData = {
                code: 9
            }
            setTimeout(function (){
                console.debug("====>publish event account_on_change 0300====");
                commonevent.publish("account_on_change", commonEventPublishData, publishCallback);
            }, 1000);
        });

        /*
        * @tc.number    : ActsAccountOnOff_0400
        * @tc.name      : Subscribe/unsubscribe to the change event of application
        * @tc.desc      : Received the account information change to the authorized account of the subscription to delete
        *                 authorized account
        */
        it('ActsAccountOnOff_0400', 0, async function (done) {
            testInit();
            console.debug("====>ActsAccountOnOff_0400 start====");
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add first account ActsAccountOnOff_0400 start");
            await appAccountManager.createAccount("onoff_removeFir");
            console.debug("====>add second account ActsAccountOnOff_0400 start");
            await appAccountManager.createAccount("onoff_removeSec");
            console.debug("====>enableAppAccess first ActsAccountOnOff_0400 start");
            await appAccountManager.setAppAccess("onoff_removeFir", "com.example.actsaccountsceneonoff", true);
            console.debug("====>enableAppAccess second ActsAccountOnOff_0400 start");
            await appAccountManager.setAppAccess("onoff_removeSec", "com.example.actsaccountsceneonoff", true);
            function unSubscriberCallback(err){
                console.debug("====>unsubscribe 0400 err:" + JSON.stringify(err));
            }
            function removeAccountCallback(err){
                console.debug("====>remove account 0400 err:" + JSON.stringify(err));
                expect(err).assertEqual(null);
                done();
            }
            function subscriberCallback(err, data){
                console.debug("====>subscriberCallback 0400 data:" + JSON.stringify(data));
                expect(data.event).assertEqual("account_on_remove_another");
                expect(data.data).assertEqual("SUCCESS");
                commonevent.unsubscribe(subscriber, unSubscriberCallback);
                appAccountManager.removeAccount("onoff_removeFir", removeAccountCallback);
            }
            function publishCallback(err){
                console.debug("====>publish call back err:" + JSON.stringify(err));
                setTimeout(async function (){
                    console.debug("====>removeAccount start====");
                    try{
                        await appAccountManager.removeAccount("onoff_removeSec");
                    }
                    catch(err){
                        console.error("====>removeAccount fail err:" + JSON.stringify(err));
                        expect().assertFail();
                        done();
                    }
                    console.debug("====>removeAccount finish====");
                }, 500)
            }
            var commonEventSubscribeInfo = {
                events: ["account_on_remove_another"]
            }
            var subscriber
            commonevent.createSubscriber(commonEventSubscribeInfo).then(function (data){
                subscriber = data;
                commonevent.subscribe(subscriber, subscriberCallback);
                console.debug("====>subscribe ActsAccountOnOff_0400 finish====")
            });

            var commonEventPublishData = {
                code: 10
            }
            setTimeout(function (){
                console.debug("====>publish event account_on_change 0400====");
                commonevent.publish("account_on_change", commonEventPublishData, publishCallback);
            }, 1000);
        });

        /*
        * @tc.number    : ActsAccountOnOff_0500
        * @tc.name      : Subscribe/unsubscribe to the change event of application
        * @tc.desc      : Received the account information change to the authorized account of the subscription to delete
        *                 the only authorized account
        */
        it('ActsAccountOnOff_0500', 0, async function (done) {
            testInit();
            console.debug("====>ActsAccountOnOff_0500 start====");
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add account ActsAccountOnOff_0500 start");
            await appAccountManager.createAccount("onoff_remove");
            console.debug("====>enableAppAccess ActsAccountOnOff_0500 start");
            await appAccountManager.setAppAccess("onoff_remove", "com.example.actsaccountsceneonoff", true);
            function unSubscriberCallback(err){
                console.debug("====>unsubscribe 0500 err:" + JSON.stringify(err));
            }
            function subscriberCallback(err, data){
                console.debug("====>subscriberCallback 0500 data:" + JSON.stringify(data));
                expect(data.event).assertEqual("account_on_change_remove");
                expect(data.data).assertEqual("SUCCESS");
                commonevent.unsubscribe(subscriber, unSubscriberCallback);
                done();
            }
            function publishCallback(err){
                console.debug("====>publish call back err:" + JSON.stringify(err));
                setTimeout(async function (){
                    console.debug("====>removeAccount start====");
                    try{
                        await appAccountManager.removeAccount("onoff_remove");
                    }
                    catch(err){
                        console.error("====>removeAccount fail err:" + JSON.stringify(err));
                        expect().assertFail();
                        done();
                    }
                    console.debug("====>removeAccount finish====");
                }, 500)
            }
            var commonEventSubscribeInfo = {
                events: ["account_on_change_remove"]
            }
            var subscriber
            commonevent.createSubscriber(commonEventSubscribeInfo).then(function (data){
                subscriber = data;
                commonevent.subscribe(subscriber, subscriberCallback);
                console.debug("====>subscribe ActsAccountOnOff_0500 finish====")
            });

            var commonEventPublishData = {
                code: 11
            }
            setTimeout(function (){
                console.debug("====>publish event account_on_change 0500====");
                commonevent.publish("account_on_change", commonEventPublishData, publishCallback);
            }, 1000);
        });

        /*
        * @tc.number    : ActsAccountOnOff_0600
        * @tc.name      : Subscribe/unsubscribe to the change event of application
        * @tc.desc      : Received the account information change to the authorized account of the subscription to cancel
        *                 authorized account   
        */
        it('ActsAccountOnOff_0600', 0, async function (done) {
            testInit();
            console.debug("====>ActsAccountOnOff_0600 start====");
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add first account ActsAccountOnOff_0600 start");
            await appAccountManager.createAccount("onoff_disableFir");
            console.debug("====>add second account ActsAccountOnOff_0600 start");
            await appAccountManager.createAccount("onoff_disableSec");
            console.debug("====>enableAppAccess first ActsAccountOnOff_0600 start");
            await appAccountManager.setAppAccess("onoff_disableFir", "com.example.actsaccountsceneonoff", true);
            console.debug("====>enableAppAccess second ActsAccountOnOff_0600 start");
            await appAccountManager.setAppAccess("onoff_disableSec", "com.example.actsaccountsceneonoff", true);
            function unSubscriberCallback(err){
                console.debug("====>unsubscribe 0600 err:" + JSON.stringify(err));
            }
            function removeAccountCallback(err){
                console.debug("====>remove first account 0600 err:" + JSON.stringify(err));
                expect(err).assertEqual(null);
                appAccountManager.removeAccount("onoff_disableSec", (err)=>{
                    console.debug("====>remove second account 0600 err:" + JSON.stringify(err));
                    expect(err).assertEqual(null);
                    done();
                });
            }
            function subscriberCallback(err, data){
                console.debug("====>subscriberCallback 0600 data:" + JSON.stringify(data));
                expect(data.event).assertEqual("account_on_set_another_disable");
                expect(data.data).assertEqual("SUCCESS");
                commonevent.unsubscribe(subscriber, unSubscriberCallback);
                appAccountManager.removeAccount("onoff_disableFir", removeAccountCallback);
            }
            function publishCallback(err){
                console.debug("====>publish call back err:" + JSON.stringify(err));
                setTimeout(async function (){
                    console.debug("====>disableAppAccess start====");
                    try{
                        await appAccountManager.setAppAccess("onoff_disableSec", "com.example.actsaccountsceneonoff", false);
                    }
                    catch(err){
                        console.error("====>disableAppAccess fail err:" + JSON.stringify(err));
                        expect().assertFail();
                        done();
                    }
                    console.debug("====>disableAppAccess finish====");
                }, 500)
            }
            var commonEventSubscribeInfo = {
                events: ["account_on_set_another_disable"]
            }
            var subscriber
            commonevent.createSubscriber(commonEventSubscribeInfo).then(function (data){
                subscriber = data;
                commonevent.subscribe(subscriber, subscriberCallback);
                console.debug("====>subscribe ActsAccountOnOff_0600 finish====")
            });

            var commonEventPublishData = {
                code: 12
            }
            setTimeout(function (){
                console.debug("====>publish event account_on_change 0600====");
                commonevent.publish("account_on_change", commonEventPublishData, publishCallback);
            }, 1000);
        });

        /*
        * @tc.number    : ActsAccountOnOff_0700
        * @tc.name      : Subscribe/unsubscribe to the change event of application
        * @tc.desc      : Received the account information change to the authorized account of the subscription to cancel
        *                 the only authorized account
        */
        it('ActsAccountOnOff_0700', 0, async function (done) {
            testInit();
            console.debug("====>ActsAccountOnOff_0700 start====");
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add account ActsAccountOnOff_0700 start");
            await appAccountManager.createAccount("onoff_disable");
            console.debug("====>enableAppAccess ActsAccountOnOff_0700 start");
            await appAccountManager.setAppAccess("onoff_disable", "com.example.actsaccountsceneonoff", true);
            function unSubscriberCallback(err){
                console.debug("====>unsubscribe 0700 err:" + JSON.stringify(err));
            }
            function removeAccountCallback(err){
                console.debug("====>delete account 0700 err:" + JSON.stringify(err));
                expect(err).assertEqual(null);
                done();
            }
            function subscriberCallback(err, data){
                console.debug("====>subscriberCallback 0700 data:" + JSON.stringify(data));
                expect(data.event).assertEqual("account_on_set_disable");
                expect(data.data).assertEqual("SUCCESS");
                commonevent.unsubscribe(subscriber, unSubscriberCallback);
                appAccountManager.removeAccount("onoff_disable", removeAccountCallback);
            }
            function publishCallback(err){
                console.debug("====>publish call back err:" + JSON.stringify(err));
                setTimeout(async function (){
                    console.debug("====>disableAppAccess start====");
                    try{
                        await appAccountManager.setAccess("onoff_disable", "com.example.actsaccountsceneonoff", false);
                    }
                    catch(err){
                        console.error("====>disableAppAccess fail err:" + JSON.stringify(err));
                        expect().assertFail();
                        done();
                    }
                    console.debug("====>disableAppAccess finish====");
                }, 500)
            }
            var commonEventSubscribeInfo = {
                events: ["account_on_set_disable"]
            }
            var subscriber
            commonevent.createSubscriber(commonEventSubscribeInfo).then(function (data){
                subscriber = data;
                commonevent.subscribe(subscriber, subscriberCallback);
                console.debug("====>subscribe ActsAccountOnOff_0700 finish====")
            });

            var commonEventPublishData = {
                code: 13
            }
            setTimeout(function (){
                console.debug("====>publish event account_on_change 0700====");
                commonevent.publish("account_on_change", commonEventPublishData, publishCallback);
            }, 1000);
        });

 
        /*
        * @tc.number    : ActsAccountOnOff_0900
        * @tc.name      : Subscribe/unsubscribe to the change event of application
        * @tc.desc      : Receiving account information after adding two accounts to the app, subscribing to itself,
        *                 and changing one of the associatal data
        */
        it('ActsAccountOnOff_0900', 0, async function (done) {
            console.debug("====>ActsAccountOnOff_0900 start====");
            let dataMap = new Map();
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add first account ActsAccountOnOff_0900 start");
            await appAccountManager.createAccount("onoff_self_first");
            console.debug("====>add second account ActsAccountOnOff_0900 start");
            await appAccountManager.createAccount("onoff_self_second");
            console.debug("====>on ActsAccountOnOff_0900 start");
            function changeOnCallback(data){
                console.debug("====>receive change 0900 data:" + JSON.stringify(data));
                console.debug("====>data.length:" + data.length);
                for (let i = 0, len = data.length; i < len; i++) {
                    dataMap.set(data[i].name, data[i].owner)
                }
                expect(dataMap.has("onoff_self_first")).assertTrue();
                if (dataMap.has("onoff_self_first")) {
                    let data = dataMap.get("onoff_self_first");
                    console.debug("====>the account owner is: " + data);
                    expect(data).assertEqual("com.example.actsaccountoperatetest");
                }
                expect(dataMap.has("onoff_self_second")).assertTrue();
                if (dataMap.has("onoff_self_second")) {
                    let data = dataMap.get("onoff_self_second");
                    console.debug("====>the account owner is: " + data);
                    expect(data).assertEqual("com.example.actsaccountoperatetest");
                }
                appAccountManager.off('accountChange', function (){
                    console.debug("====>off ActsAccountOnOff_0900 finish====");
                    appAccountManager.removeAccount("onoff_self_first", (err)=>{
                        console.debug("====>delete first account 0900 err:" + JSON.stringify(err));
                        expect(err).assertEqual(null);
                        appAccountManager.removeAccount("onoff_self_second", (err)=>{
                            console.debug("====>delete second account 0900 err:" + JSON.stringify(err));
                            expect(err).assertEqual(null);
                            console.debug("====>ActsAccountOnOff_0900 end====");
                            done();
                        });
                    });
                });
            }
            try{
                appAccountManager.on('accountChange', ["com.example.actsaccountoperatetest"], changeOnCallback);
            }
            catch(err){
                console.error("====>on ActsAccountOnOff_0900 err:" + JSON.stringify(err));
                expect().assertFail();
                done();
            }
            console.debug("====>setCustomData start====");
            try{
                await appAccountManager.setCustomData("onoff_self_second", "change_key", "change_value");
            }
            catch(err){
                console.error("====>setCustomData fail err:" + JSON.stringify(err));
                expect().assertFail();
                done();
            }
        });       
    })
}