• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 */
15
16import hilog from '@ohos.hilog';
17import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect, TestType, Size, Level } from '@ohos/hypium';
18import oaid from '@ohos.identifier.oaid';
19import { BY, UiDriver } from '@ohos.UiTest';
20import abilityAccessCtrl from "@ohos.abilityAccessCtrl";
21
22const ERR_INVALID_PARAM: number = 401;
23const TAG: string = 'OAIDTestTag';
24const Domain: number = 0x0000;
25
26export default function OAIDTest(abilityContext) {
27  describe('ActsOAIDTest', function () {
28
29    function sleep(ms) {
30      return new Promise(resolve => setTimeout(resolve, ms));
31    }
32
33    beforeAll(async function () {
34      console.log(TAG, `beforeAll case`);
35
36      let atManager = abilityAccessCtrl.createAtManager();
37      let permissions = ["ohos.permission.APP_TRACKING_CONSENT"];
38
39      try {
40        atManager.requestPermissionsFromUser(abilityContext, permissions, (err, data) => {
41          console.log(TAG, `getPermission requestPermissionsFromUser ${JSON.stringify(data)}`);
42        });
43      } catch (err) {
44        console.log(TAG, `get permission catch err -> ${JSON.stringify(err)}`);
45      }
46      await sleep(1000);
47
48      let driver = UiDriver.create();
49      await sleep(2000);
50
51      let button = await driver.findComponent(BY.text("允许"));
52      await button.click();
53      await sleep(2000);
54
55    });
56
57    /**
58     * @tc.number: SUB_CLOUD_OAID_GETOAID_0100
59     * @tc.name: testGetOAIDByPromise
60     * @tc.desc: Get oaid by promise is not null.
61     * @tc.size: LargeTest
62     * @tc.type: Function
63     * @tc.level: Level0
64     */
65    it("testGetOAIDByPromise", TestType.FUNCTION | Size.LARGETEST | Level.LEVEL0, async function () {
66      let oaidResult: string = '';
67      try {
68        oaidResult = await oaid.getOAID();
69      } catch (e) {
70        hilog.error(Domain, TAG, 'Promise GetOAID error, error code is %{public}d, error message is %{public}s.', e.code, e.message);
71        hilog.error(Domain, TAG, `Promise1 GetOAID error, error code is: ${e.code}, error message is: ${e.message}`);
72        expect().assertFail();
73      }
74      expect(oaidResult.length > 0).assertEqual(true);
75    })
76
77    /**
78     * @tc.number: SUB_CLOUD_OAID_GETOAID_0200
79     * @tc.name: testGetOAIDByCallback
80     * @tc.desc: Get oaid by callback is not null.
81     * @tc.size: LargeTest
82     * @tc.type: Function
83     * @tc.level: Level0
84     */
85    it("testGetOAIDByCallback", TestType.FUNCTION | Size.LARGETEST | Level.LEVEL0, async function (done) {
86      try {
87        oaid.getOAID((error, result) => {
88          expect(result.length > 0).assertEqual(true);
89          done();
90        });
91      } catch (e) {
92        hilog.error(Domain, TAG, 'Callback GetOAID error, error code is %{public}d, error message is %{public}s.', e.code, e.message);
93        expect().assertFail();
94        done();
95      }
96    })
97
98    /**
99     * @tc.number: SUB_CLOUD_OAID_GETOAID_0300
100     * @tc.name: testRepeatGetOAID
101     * @tc.desc: Repeat get oaid, result is equal.
102     * @tc.size: LargeTest
103     * @tc.type: Function
104     * @tc.level: Level1
105     */
106    it("testRepeatGetOAID", TestType.FUNCTION | Size.LARGETEST | Level.LEVEL1, async function () {
107      try {
108        let oaidResult1 = await oaid.getOAID();
109        let oaidResult2 = await oaid.getOAID();
110        expect(oaidResult1).assertEqual(oaidResult2);
111      } catch (e) {
112        hilog.error(Domain, TAG, 'RepeatGetOAID error, error code is %{public}d, error message is %{public}s.', e.code, e.message);
113        expect(e.code).assertEqual(ERR_INVALID_PARAM);
114      }
115    })
116
117    /**
118     * @tc.number: SUB_CLOUD_OAID_RESETOAID_0400
119     * @tc.name: testResetOAID
120     * @tc.desc: Reset OAID.
121     * @tc.size: LargeTest
122     * @tc.type: Function
123     * @tc.level: Level1
124     */
125    it("testResetOAID", TestType.FUNCTION | Size.LARGETEST | Level.LEVEL1, function () {
126      try {
127        oaid.resetOAID();
128      } catch (e) {
129        hilog.error(Domain, TAG, 'ResetOAID error, error code is %{public}d, error message is %{public}s.', e.code, e.message);
130        expect().assertFail();
131      }
132    })
133
134  })
135}