OnOff.js 46.3 KB
Newer Older
J
jiyong_sd 已提交
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
/*
 * 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 ActsAccountChangeOnOff() {
    describe('ActsAccountChangeOnOff', async function () {

        function sleep(delay) {
            return new Promise((resolve, reject) => {
                setTimeout(() => {
                    resolve()
                }, delay)
            }).then(() => {
                console.info(`sleep #{time} over ...`)
            })
        }
        beforeAll(async function (done) {
            console.debug("====>startAbility start====");
            await featureAbility.startAbility(
                {
                    want:
                    {
                        deviceId: "",
                        bundleName: "com.example.actsaccountsceneonoff",
                        abilityName: "com.example.actsaccountsceneonoff.MainAbility",
                        action: "action1",
                        parameters:
                        {},
                    },
                },
            );
            await sleep(TIMEOUT);
            done();
        });

        /*
        * @tc.number    : ActsAccountChangeOnOff_0100
        * @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 additional information
        */
        it('ActsAccountChangeOnOff_0100', 0, async function (done) {
            console.debug("====>ActsAccountChangeOnOff_0100 start====");
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add account ActsAccountChangeOnOff_0100 start");
            await appAccountManager.addAccount("changeonoff_extra");
            console.debug("====>enableAppAccess ActsAccountChangeOnOff_0100 start");
            await appAccountManager.enableAppAccess("changeonoff_extra", "com.example.actsaccountsceneonoff");
            function unSubscriberCallback(err){
                console.debug("====>unsubscribe 0100 err:" + JSON.stringify(err));
            }
            function deleteAccountCallback(err){
                console.debug("====>delete account 0100 err:" + JSON.stringify(err));
71
                expect(err).assertEqual(null);
J
jiyong_sd 已提交
72 73 74 75
                done();
            }
            function disCallback(err){
                console.debug("====>delete account 0100 err:" + JSON.stringify(err));
76
                expect(err).assertEqual(null);
J
jiyong_sd 已提交
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
                appAccountManager.deleteAccount("changeonoff_extra", deleteAccountCallback);
            }
            function subscriberCallback(err, data){
                console.debug("====>subscriberCallback 0100 data:" + JSON.stringify(data));
                expect(data.event).assertEqual("account_on_change_extra");
                expect(data.data).assertEqual("SUCCESS");
                commonevent.unsubscribe(subscriber, unSubscriberCallback);
                appAccountManager.disableAppAccess("changeonoff_extra", "com.example.actsaccountsceneonoff", disCallback);
            }
            function publishCallback(err){
                console.debug("====>publish call back err:" + JSON.stringify(err));
                setTimeout(async function (){
                    console.debug("====>setAccountExtraInfo start====");
                    try{
                        await appAccountManager.setAccountExtraInfo("changeonoff_extra", "change_extra");
                    }
                    catch(err){
                        console.error("====>setAccountExtraInfo fail err:" + JSON.stringify(err));
                        expect().assertFail();
                        done();
                    }
                    console.debug("====>setAccountExtraInfo finish====");
                }, 500)
            }
            var commonEventSubscribeInfo = {
                events: ["account_on_change_extra"]
            }
            var subscriber
            commonevent.createSubscriber(commonEventSubscribeInfo).then(function (data){
                subscriber = data;
                commonevent.subscribe(subscriber, subscriberCallback);
                console.debug("====>subscribe ActsAccountChangeOnOff_0100 finish====")
            });

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

        /*
        * @tc.number    : ActsAccountChangeOnOff_0200
        * @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 associatal data
        */
        it('ActsAccountChangeOnOff_0200', 0, async function (done) {
            console.debug("====>ActsAccountChangeOnOff_0200 start====");
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add account ActsAccountChangeOnOff_0200 start");
            await appAccountManager.addAccount("onoff_associatedata");
            console.debug("====>enableAppAccess ActsAccountChangeOnOff_0200 start");
            await appAccountManager.enableAppAccess("onoff_associatedata", "com.example.actsaccountsceneonoff");
            function unSubscriberCallback(err){
                console.debug("====>unsubscribe 0200 err:" + JSON.stringify(err));
            }
            function deleteAccountCallback(err){
                console.debug("====>delete account 0200 err:" + JSON.stringify(err));
139
                expect(err).assertEqual(null);
J
jiyong_sd 已提交
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
                done();
            }
            function subscriberCallback(err, data){
                console.debug("====>subscriberCallback 0200 data:" + JSON.stringify(data));
                expect(data.event).assertEqual("account_on_change_associatedata");
                expect(data.data).assertEqual("SUCCESS");
                commonevent.unsubscribe(subscriber, unSubscriberCallback);
                appAccountManager.deleteAccount("onoff_associatedata", deleteAccountCallback);
            }
            function publishCallback(err){
                console.debug("====>publish call back err:" + JSON.stringify(err));
                setTimeout(async function (){
                    console.debug("====>setAssociatedData start====");
                    try{
                        await appAccountManager.setAssociatedData("onoff_associatedata", "change_key", "change_value");
                    }
                    catch(err){
                        console.error("====>setAssociatedData fail err:" + JSON.stringify(err));
                        expect().assertFail();
                        done();
                    }
                    console.debug("====>setAssociatedData finish====");
                }, 500)
            }
            var commonEventSubscribeInfo = {
                events: ["account_on_change_associatedata"]
            }
            var subscriber
            commonevent.createSubscriber(commonEventSubscribeInfo).then(function (data){
                subscriber = data;
                commonevent.subscribe(subscriber, subscriberCallback);
                console.debug("====>subscribe ActsAccountChangeOnOff_0200 finish====")
            });

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

        /*
        * @tc.number    : ActsAccountChangeOnOff_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('ActsAccountChangeOnOff_0300', 0, async function (done) {
            console.debug("====>ActsAccountChangeOnOff_0300 start====");
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add account ActsAccountChangeOnOff_0300 start");
            await appAccountManager.addAccount("onoff_credential");
            console.debug("====>enableAppAccess ActsAccountChangeOnOff_0300 start");
            await appAccountManager.enableAppAccess("onoff_credential", "com.example.actsaccountsceneonoff");
            function unSubscriberCallback(err){
                console.debug("====>unsubscribe 0300 err:" + JSON.stringify(err));
            }
            function deleteAccountCallback(err){
                console.debug("====>delete account 0300 err:" + JSON.stringify(err));
202
                expect(err).assertEqual(null);
J
jiyong_sd 已提交
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
                done();
            }
            function subscriberCallback(err, data){
                console.debug("====>subscriberCallback 0300 data:" + JSON.stringify(data));
                expect(data.event).assertEqual("account_on_change_credential");
                expect(data.data).assertEqual("SUCCESS");
                commonevent.unsubscribe(subscriber, unSubscriberCallback);
                appAccountManager.deleteAccount("onoff_credential", deleteAccountCallback);
            }
            function publishCallback(err){
                console.debug("====>publish call back err:" + JSON.stringify(err));
                setTimeout(async function (){
                    console.debug("====>setAccountCredential start====");
                    try{
                        await appAccountManager.setAccountCredential("onoff_credential", "credentialType", "credential");
                    }
                    catch(err){
                        console.error("====>setAccountCredential fail err:" + JSON.stringify(err));
                        expect().assertFail();
                        done();
                    }
                    console.debug("====>setAccountCredential finish====");
                }, 500)
            }
            var commonEventSubscribeInfo = {
                events: ["account_on_change_credential"]
            }
            var subscriber
            commonevent.createSubscriber(commonEventSubscribeInfo).then(function (data){
                subscriber = data;
                commonevent.subscribe(subscriber, subscriberCallback);
                console.debug("====>subscribe ActsAccountChangeOnOff_0300 finish====")
            });

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

        /*
        * @tc.number    : ActsAccountChangeOnOff_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('ActsAccountChangeOnOff_0400', 0, async function (done) {
            console.debug("====>ActsAccountChangeOnOff_0400 start====");
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add first account ActsAccountChangeOnOff_0400 start");
            await appAccountManager.addAccount("onoff_deleteFir");
            console.debug("====>add second account ActsAccountChangeOnOff_0400 start");
            await appAccountManager.addAccount("onoff_deleteSec");
            console.debug("====>enableAppAccess first ActsAccountChangeOnOff_0400 start");
            await appAccountManager.enableAppAccess("onoff_deleteFir", "com.example.actsaccountsceneonoff");
            console.debug("====>enableAppAccess second ActsAccountChangeOnOff_0400 start");
            await appAccountManager.enableAppAccess("onoff_deleteSec", "com.example.actsaccountsceneonoff");
            function unSubscriberCallback(err){
                console.debug("====>unsubscribe 0400 err:" + JSON.stringify(err));
            }
            function deleteAccountCallback(err){
                console.debug("====>delete account 0400 err:" + JSON.stringify(err));
269
                expect(err).assertEqual(null);
J
jiyong_sd 已提交
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
                done();
            }
            function subscriberCallback(err, data){
                console.debug("====>subscriberCallback 0400 data:" + JSON.stringify(data));
                expect(data.event).assertEqual("account_on_delete_another");
                expect(data.data).assertEqual("SUCCESS");
                commonevent.unsubscribe(subscriber, unSubscriberCallback);
                appAccountManager.deleteAccount("onoff_deleteFir", deleteAccountCallback);
            }
            function publishCallback(err){
                console.debug("====>publish call back err:" + JSON.stringify(err));
                setTimeout(async function (){
                    console.debug("====>deleteAccount start====");
                    try{
                        await appAccountManager.deleteAccount("onoff_deleteSec");
                    }
                    catch(err){
                        console.error("====>deleteAccount fail err:" + JSON.stringify(err));
                        expect().assertFail();
                        done();
                    }
                    console.debug("====>deleteAccount finish====");
                }, 500)
            }
            var commonEventSubscribeInfo = {
                events: ["account_on_delete_another"]
            }
            var subscriber
            commonevent.createSubscriber(commonEventSubscribeInfo).then(function (data){
                subscriber = data;
                commonevent.subscribe(subscriber, subscriberCallback);
                console.debug("====>subscribe ActsAccountChangeOnOff_0400 finish====")
            });

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

        /*
        * @tc.number    : ActsAccountChangeOnOff_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('ActsAccountChangeOnOff_0500', 0, async function (done) {
            console.debug("====>ActsAccountChangeOnOff_0500 start====");
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add account ActsAccountChangeOnOff_0500 start");
            await appAccountManager.addAccount("onoff_delete");
            console.debug("====>enableAppAccess ActsAccountChangeOnOff_0500 start");
            await appAccountManager.enableAppAccess("onoff_delete", "com.example.actsaccountsceneonoff");
            function unSubscriberCallback(err){
                console.debug("====>unsubscribe 0500 err:" + JSON.stringify(err));
1
15829070344 已提交
329
                expect(err).assertEqual(null);
J
jiyong_sd 已提交
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
                done();
            }
            function subscriberCallback(err, data){
                console.debug("====>subscriberCallback 0500 data:" + JSON.stringify(data));
                expect(data.event).assertEqual("account_on_change_delete");
                expect(data.data).assertEqual("SUCCESS");
                commonevent.unsubscribe(subscriber, unSubscriberCallback);
            }
            function publishCallback(err){
                console.debug("====>publish call back err:" + JSON.stringify(err));
                setTimeout(async function (){
                    console.debug("====>deleteAccount start====");
                    try{
                        await appAccountManager.deleteAccount("onoff_delete");
                    }
                    catch(err){
                        console.error("====>deleteAccount fail err:" + JSON.stringify(err));
                        expect().assertFail();
                        done();
                    }
                    console.debug("====>deleteAccount finish====");
                }, 500)
            }
            var commonEventSubscribeInfo = {
                events: ["account_on_change_delete"]
            }
            var subscriber
            commonevent.createSubscriber(commonEventSubscribeInfo).then(function (data){
                subscriber = data;
                commonevent.subscribe(subscriber, subscriberCallback);
                console.debug("====>subscribe ActsAccountChangeOnOff_0500 finish====")
            });

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

        /*
        * @tc.number    : ActsAccountChangeOnOff_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('ActsAccountChangeOnOff_0600', 0, async function (done) {
            console.debug("====>ActsAccountChangeOnOff_0600 start====");
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add first account ActsAccountChangeOnOff_0600 start");
            await appAccountManager.addAccount("onoff_enableFir");
            console.debug("====>add second account ActsAccountChangeOnOff_0600 start");
            await appAccountManager.addAccount("onoff_enableSec");
            console.debug("====>enableAppAccess first ActsAccountChangeOnOff_0600 start");
            await appAccountManager.enableAppAccess("onoff_enableFir", "com.example.actsaccountsceneonoff");
            console.debug("====>enableAppAccess second ActsAccountChangeOnOff_0600 start");
            await appAccountManager.enableAppAccess("onoff_enableSec", "com.example.actsaccountsceneonoff");
            function unSubscriberCallback(err){
                console.debug("====>unsubscribe 0600 err:" + JSON.stringify(err));
            }
            function deleteAccountCallback(err){
                console.debug("====>delete first account 0600 err:" + JSON.stringify(err));
395
                expect(err).assertEqual(null);
J
jiyong_sd 已提交
396 397
                appAccountManager.deleteAccount("onoff_enableSec", (err)=>{
                    console.debug("====>delete second account 0600 err:" + JSON.stringify(err));
398
                    expect(err).assertEqual(null);
J
jiyong_sd 已提交
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 451 452 453 454 455 456 457 458 459 460 461
                    done();
                });
            }
            function subscriberCallback(err, data){
                console.debug("====>subscriberCallback 0600 data:" + JSON.stringify(data));
                expect(data.event).assertEqual("account_on_disable_another");
                expect(data.data).assertEqual("SUCCESS");
                commonevent.unsubscribe(subscriber, unSubscriberCallback);
                appAccountManager.deleteAccount("onoff_enableFir", deleteAccountCallback);
            }
            function publishCallback(err){
                console.debug("====>publish call back err:" + JSON.stringify(err));
                setTimeout(async function (){
                    console.debug("====>disableAppAccess start====");
                    try{
                        await appAccountManager.disableAppAccess("onoff_enableSec", "com.example.actsaccountsceneonoff");
                    }
                    catch(err){
                        console.error("====>disableAppAccess fail err:" + JSON.stringify(err));
                        expect().assertFail();
                        done();
                    }
                    console.debug("====>disableAppAccess finish====");
                }, 500)
            }
            var commonEventSubscribeInfo = {
                events: ["account_on_disable_another"]
            }
            var subscriber
            commonevent.createSubscriber(commonEventSubscribeInfo).then(function (data){
                subscriber = data;
                commonevent.subscribe(subscriber, subscriberCallback);
                console.debug("====>subscribe ActsAccountChangeOnOff_0600 finish====")
            });

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

        /*
        * @tc.number    : ActsAccountChangeOnOff_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('ActsAccountChangeOnOff_0700', 0, async function (done) {
            console.debug("====>ActsAccountChangeOnOff_0700 start====");
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add account ActsAccountChangeOnOff_0700 start");
            await appAccountManager.addAccount("onoff_disable");
            console.debug("====>enableAppAccess ActsAccountChangeOnOff_0700 start");
            await appAccountManager.enableAppAccess("onoff_disable", "com.example.actsaccountsceneonoff");
            function unSubscriberCallback(err){
                console.debug("====>unsubscribe 0700 err:" + JSON.stringify(err));
            }
            function deleteAccountCallback(err){
                console.debug("====>delete account 0700 err:" + JSON.stringify(err));
462
                expect(err).assertEqual(null);
J
jiyong_sd 已提交
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
                done();
            }
            function subscriberCallback(err, data){
                console.debug("====>subscriberCallback 0700 data:" + JSON.stringify(data));
                expect(data.event).assertEqual("account_on_change_disable");
                expect(data.data).assertEqual("SUCCESS");
                commonevent.unsubscribe(subscriber, unSubscriberCallback);
                appAccountManager.deleteAccount("onoff_disable", deleteAccountCallback);
            }
            function publishCallback(err){
                console.debug("====>publish call back err:" + JSON.stringify(err));
                setTimeout(async function (){
                    console.debug("====>disableAppAccess start====");
                    try{
                        await appAccountManager.disableAppAccess("onoff_disable", "com.example.actsaccountsceneonoff");
                    }
                    catch(err){
                        console.error("====>disableAppAccess fail err:" + JSON.stringify(err));
                        expect().assertFail();
                        done();
                    }
                    console.debug("====>disableAppAccess finish====");
                }, 500)
            }
            var commonEventSubscribeInfo = {
                events: ["account_on_change_disable"]
            }
            var subscriber
            commonevent.createSubscriber(commonEventSubscribeInfo).then(function (data){
                subscriber = data;
                commonevent.subscribe(subscriber, subscriberCallback);
                console.debug("====>subscribe ActsAccountChangeOnOff_0700 finish====")
            });

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

        /*
        * @tc.number    : ActsAccountChangeOnOff_0800
        * @tc.name      : Subscribe/unsubscribe to the change event of application
        * @tc.desc      : Receive account information after the app subscribes to itself and changes additional information
        */
        it('ActsAccountChangeOnOff_0800', 0, async function (done) {
            console.debug("====>ActsAccountChangeOnOff_0800 start====");
            let dataMap = new Map();
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add account ActsAccountChangeOnOff_0800 start");
            await appAccountManager.addAccount("onoff_self");
            console.debug("====>on ActsAccountChangeOnOff_0800 start");
            function changeOnCallback(data){
                console.debug("====>receive change 0800 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")).assertTrue();
                if (dataMap.has("onoff_self")) {
                    let data = dataMap.get("onoff_self");
                    console.debug("====>the account owner is: " + data);
                    expect(data).assertEqual("com.example.actsaccounttest");
                }
                appAccountManager.off('change', function (){
                    console.debug("====>off ActsAccountChangeOnOff_0800 finish====");
                    appAccountManager.deleteAccount("onoff_self", (err)=>{
                        console.debug("====>delete account ActsAccountChangeOnOff_0800 err:" + JSON.stringify(err));
535
                        expect(err).assertEqual(null);
J
jiyong_sd 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
                        console.debug("====>ActsAccountChangeOnOff_0800 end====");
                        done();
                    });
                });
            }
            try{
                appAccountManager.on('change', ["com.example.actsaccounttest"], changeOnCallback);
            }
            catch(err){
                console.error("====>on ActsAccountChangeOnOff_0800 err:" + JSON.stringify(err));
                expect().assertFail();
                done();
            }
            console.debug("====>setAccountExtraInfo start====");
            try{
                await appAccountManager.setAccountExtraInfo("onoff_self", "change_extra");
            }
            catch(err){
                console.error("====>setAccountExtraInfo fail err:" + JSON.stringify(err));
                expect().assertFail();
                done();
            }
        });

        /*
        * @tc.number    : ActsAccountChangeOnOff_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('ActsAccountChangeOnOff_0900', 0, async function (done) {
            console.debug("====>ActsAccountChangeOnOff_0900 start====");
            let dataMap = new Map();
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add first account ActsAccountChangeOnOff_0900 start");
            await appAccountManager.addAccount("onoff_self_first");
            console.debug("====>add second account ActsAccountChangeOnOff_0900 start");
            await appAccountManager.addAccount("onoff_self_second");
            console.debug("====>on ActsAccountChangeOnOff_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.actsaccounttest");
                }
                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.actsaccounttest");
                }
                appAccountManager.off('change', function (){
                    console.debug("====>off ActsAccountChangeOnOff_0900 finish====");
                    appAccountManager.deleteAccount("onoff_self_first", (err)=>{
                        console.debug("====>delete first account 0900 err:" + JSON.stringify(err));
598
                        expect(err).assertEqual(null);
J
jiyong_sd 已提交
599 600
                        appAccountManager.deleteAccount("onoff_self_second", (err)=>{
                            console.debug("====>delete second account 0900 err:" + JSON.stringify(err));
601
                            expect(err).assertEqual(null);
J
jiyong_sd 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
                            console.debug("====>ActsAccountChangeOnOff_0900 end====");
                            done();
                        });
                    });
                });
            }
            try{
                appAccountManager.on('change', ["com.example.actsaccounttest"], changeOnCallback);
            }
            catch(err){
                console.error("====>on ActsAccountChangeOnOff_0900 err:" + JSON.stringify(err));
                expect().assertFail();
                done();
            }
            console.debug("====>setAssociatedData start====");
            try{
                await appAccountManager.setAssociatedData("onoff_self_second", "change_key", "change_value");
            }
            catch(err){
                console.error("====>setAssociatedData fail err:" + JSON.stringify(err));
                expect().assertFail();
                done();
            }
        });

        /*
        * @tc.number    : ActsAccountChangeOnOff_1000
        * @tc.name      : Subscribe/unsubscribe to the change event of application
        * @tc.desc      : Repeat subscription account information changes, the subscription behavior is independent
        */
        it('ActsAccountChangeOnOff_1000', 0, async function (done) {
            console.debug("====>ActsAccountChangeOnOff_1000 start====");
            let dataMapFir = new Map();
            let dataMapSec = new Map();
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add account ActsAccountChangeOnOff_1000 start");
            await appAccountManager.addAccount("onoff_twice");
            console.debug("====>on ActsAccountChangeOnOff_1000 start");
            var twiceSign = 0;
            function changeOnFirstCallback(data){
                console.debug("====>first receive change 1000 data:" + JSON.stringify(data));
                console.debug("====>data.length:" + data.length);
                for (let i = 0, len = data.length; i < len; i++) {
                    dataMapFir.set(data[i].name, data[i].owner)
                }
                expect(dataMapFir.has("onoff_twice")).assertTrue();
                if (dataMapFir.has("onoff_twice")) {
                    let data = dataMapFir.get("onoff_twice");
                    console.debug("====>the account owner is: " + data);
                    expect(data).assertEqual("com.example.actsaccounttest");
                }
                twiceSign = twiceSign + 1;
            }
            function changeOnSecondCallback(data){
                console.debug("====>second receive change 1000 data:" + JSON.stringify(data));
                console.debug("====>data.length:" + data.length);
                for (let i = 0, len = data.length; i < len; i++) {
                    dataMapSec.set(data[i].name, data[i].owner)
                }
                expect(dataMapSec.has("onoff_twice")).assertTrue();
                if (dataMapSec.has("onoff_twice")) {
                    let data = dataMapSec.get("onoff_twice");
                    console.debug("====>the account owner is: " + data);
                    expect(data).assertEqual("com.example.actsaccounttest");
                }
                twiceSign = twiceSign + 1;
                setTimeout(async function (){
                    console.debug("====>off start====");
                    appAccountManager.off('change', function (){
                        console.debug("====>off ActsAccountChangeOnOff_1000 finish====");
                        appAccountManager.deleteAccount("onoff_twice", (err)=>{
                            console.debug("====>delete account ActsAccountChangeOnOff_1000 err:" + JSON.stringify(err));
675
                            expect(err).assertEqual(null);
J
jiyong_sd 已提交
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
                            expect(twiceSign).assertEqual(2);
                            console.debug("====>ActsAccountChangeOnOff_1000 end====");
                            done();
                        });
                    });
                }, 1000)
            }
            try{
                appAccountManager.on('change', ["com.example.actsaccounttest"], changeOnFirstCallback);
                appAccountManager.on('change', ["com.example.actsaccounttest"], changeOnSecondCallback);
            }
            catch(err){
                console.error("====>on ActsAccountChangeOnOff_1000 err:" + JSON.stringify(err));
                expect().assertFail();
                done();
            }
            console.debug("====>setAccountExtraInfo start====");
            try{
                await appAccountManager.setAccountExtraInfo("onoff_twice", "change_extra");
            }
            catch(err){
                console.error("====>setAccountExtraInfo fail err:" + JSON.stringify(err));
                expect().assertFail();
                done();
            }
        });

        /*
        * @tc.number    : ActsAccountChangeOnOff_1100
        * @tc.name      : Subscribe/unsubscribe to the change event of application
        * @tc.desc      : Subscription account information changes, where the bundleName in the parameter owners is
        *                 duplicated
        */
        it('ActsAccountChangeOnOff_1100', 0, async function (done) {
            console.debug("====>ActsAccountChangeOnOff_1100 start====");
            let dataMap = new Map();
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add account ActsAccountChangeOnOff_1100 start");
            await appAccountManager.addAccount("onoff_same");
            console.debug("====>on ActsAccountChangeOnOff_1100 start");
            var sameSign = 0;
            function onCallBack(data){
                console.debug("====>receive change 1100 data:" + JSON.stringify(data));
                sameSign = sameSign + 1;
                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_same")).assertTrue();
                if (dataMap.has("onoff_same")) {
                    let data = dataMap.get("onoff_same");
                    console.debug("====>the account owner is: " + data);
                    expect(data).assertEqual("com.example.actsaccounttest");
                }
            }
            try{
                appAccountManager.on('change', ["com.example.actsaccounttest", "com.example.actsaccounttest"], onCallBack);
            }
            catch(err){
                console.error("====>on ActsAccountChangeOnOff_1100 err:" + JSON.stringify(err));
                expect().assertFail();
                done();
            }
            console.debug("====>setAccountExtraInfo start====");
            try{
                await appAccountManager.setAccountExtraInfo("onoff_same", "change_extra");
            }
            catch(err){
                console.error("====>setAccountExtraInfo fail err:" + JSON.stringify(err));
                expect().assertFail();
                done();
            }
            setTimeout(async function (){
                console.debug("====>off 1100 start====");
                expect(sameSign).assertEqual(1);
                appAccountManager.off('change', function (){
                    console.debug("====>off ActsAccountChangeOnOff_1100 finish====");
                    appAccountManager.deleteAccount("onoff_same", (err)=>{
                        console.debug("====>delete account ActsAccountChangeOnOff_1100 err:" + JSON.stringify(err));
756
                        expect(err).assertEqual(null);
J
jiyong_sd 已提交
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
                        console.debug("====>ActsAccountChangeOnOff_1100 end====");
                        done();
                    });
                });
            }, 1000)
        });

        /*
        * @tc.number    : ActsAccountChangeOnOff_1200
        * @tc.name      : Subscribe/unsubscribe to the change event of application
        * @tc.desc      : Repeatedly cancel the subscription after the subscription account information is changed
        */
        it('ActsAccountChangeOnOff_1200', 0, async function (done) {
            console.debug("====>ActsAccountChangeOnOff_1200 start====");
            let dataMap = new Map();
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add account ActsAccountChangeOnOff_1200 start");
            await appAccountManager.addAccount("onoff_repeatoff");
            console.debug("====>on ActsAccountChangeOnOff_1200 start");
            function changeOnCallback(data){
                console.debug("====>receive change 1200 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_repeatoff")).assertTrue();
                if (dataMap.has("onoff_repeatoff")) {
                    let data = dataMap.get("onoff_repeatoff");
                    console.debug("====>the account owner is: " + data);
                    expect(data).assertEqual("com.example.actsaccounttest");
                }
                appAccountManager.off('change', function (){
                    console.debug("====>first off ActsAccountChangeOnOff_1200 finish====");
                    appAccountManager.off('change', function (){
                        console.debug("====>second off ActsAccountChangeOnOff_1200 finish====");
                        setTimeout(async function (){
                            console.debug("====>delete account 1200 start====");
                            appAccountManager.deleteAccount("onoff_repeatoff", (err)=>{
                                console.debug("====>delete account ActsAccountChangeOnOff_1200 err:" + JSON.stringify(err));
797
                                expect(err).assertEqual(null);
J
jiyong_sd 已提交
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
                                console.debug("====>ActsAccountChangeOnOff_1200 end====");
                                done();
                            });
                        }, 1000)
                    });
                });
            }
            try{
                appAccountManager.on('change', ["com.example.actsaccounttest"], changeOnCallback);
            }
            catch(err){
                console.error("====>on ActsAccountChangeOnOff_1200 err:" + JSON.stringify(err));
                expect().assertFail();
                done();
            }
            console.debug("====>setAccountExtraInfo start====");
            try{
                await appAccountManager.setAccountExtraInfo("onoff_repeatoff", "change_extra");
            }
            catch(err){
                console.error("====>setAccountExtraInfo fail err:" + JSON.stringify(err));
                expect().assertFail();
                done();
            }
        });

        /*
        * @tc.number    : ActsAccountChangeOnOff_1300
        * @tc.name      : Subscribe/unsubscribe to the change event of application
        * @tc.desc      : There is an unauthorized bundleName in the subscription parameter owners array, and the
        *                 subscription fails
        */
        it('ActsAccountChangeOnOff_1300', 0, async function (done) {
            console.debug("====>ActsAccountChangeOnOff_1300 start====");
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add account ActsAccountChangeOnOff_1300 start");
            await appAccountManager.addAccount("onoff_same");
            console.debug("====>on ActsAccountChangeOnOff_1300 start");
            function changeOnCallBack(data){
                console.debug("====>receive change 1300 data:" + JSON.stringify(data));
                expect().assertFail();
                done();
            }
            var unauthorizedBundle = "com.example.actsaccountsceneonoff";
            try{
                appAccountManager.on('change', ["com.example.actsaccounttest", unauthorizedBundle], changeOnCallBack);
            }
            catch(err){
                console.error("====>on ActsAccountChangeOnOff_1300 err:" + JSON.stringify(err));
                expect().assertFail();
                done();
            }
            console.debug("====>setAccountExtraInfo start====");
            try{
                await appAccountManager.setAccountExtraInfo("onoff_same", "change_extra");
            }
            catch(err){
                console.error("====>setAccountExtraInfo fail err:" + JSON.stringify(err));
                expect().assertFail();
                done();
            }
            setTimeout(async function (){
                console.debug("====>delete account 1300 start====");
                appAccountManager.deleteAccount("onoff_same", (err)=>{
                    console.debug("====>delete account ActsAccountChangeOnOff_1300 err:" + JSON.stringify(err));
864
                    expect(err).assertEqual(null);
J
jiyong_sd 已提交
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
                    console.debug("====>ActsAccountChangeOnOff_1300 end====");
                    done();
                });
            }, 1000)
        });

        /*
        * @tc.number    : ActsAccountChangeOnOff_1400
        * @tc.name      : Subscribe/unsubscribe to the change event of application
        * @tc.desc      : There is a bundleName that does not exist in the subscription parameter owners array, and the
        *                 subscription fails
        */
        it('ActsAccountChangeOnOff_1400', 0, async function (done) {
            console.debug("====>ActsAccountChangeOnOff_1400 start====");
            var appAccountManager = account.createAppAccountManager();
            console.debug("====>creat appAccountManager finish");
            console.debug("====>add account ActsAccountChangeOnOff_1400 start");
            await appAccountManager.addAccount("onoff_same");
            console.debug("====>on ActsAccountChangeOnOff_1400 start");
            function changeOnCallBack(data){
                console.debug("====>receive change 1300 data:" + JSON.stringify(data));
                expect().assertFail();
                done();
            }
            var nonexistentBundle = "com.example.actsaccountnoneexistent";
            try{
                appAccountManager.on('change', ["com.example.actsaccounttest", nonexistentBundle], changeOnCallBack);
            }
            catch(err){
                console.error("====>on ActsAccountChangeOnOff_1400 err:" + JSON.stringify(err));
                expect().assertFail();
                done();
            }
            console.debug("====>setAccountExtraInfo start====");
            try{
                await appAccountManager.setAccountExtraInfo("onoff_same", "change_extra");
            }
            catch(err){
                console.error("====>setAccountExtraInfo fail err:" + JSON.stringify(err));
                expect().assertFail();
                done();
            }
            setTimeout(async function (){
                console.debug("====>delete account 1400 start====");
                appAccountManager.deleteAccount("onoff_same", (err)=>{
                    console.debug("====>delete account ActsAccountChangeOnOff_1400 err:" + JSON.stringify(err));
911
                    expect(err).assertEqual(null);
J
jiyong_sd 已提交
912 913 914 915 916 917 918
                    console.debug("====>ActsAccountChangeOnOff_1400 end====");
                    done();
                });
            }, 1000)
        });
    })
}