• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 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 geolocation from '@ohos.geolocation';
16import geolocationm from '@ohos.geoLocationManager';
17import abilityAccessCtrl from '@ohos.abilityAccessCtrl'
18import bundle from '@ohos.bundle'
19import osaccount from '@ohos.account.osAccount'
20import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium'
21
22function sleep(ms) {
23    return new Promise(resolve => setTimeout(resolve, ms));
24    console.info('[lbs_js]sleep function');
25}
26
27async function changedLocationMode(){
28    await geolocation.isLocationEnabled().then(async(result) => {
29        console.info('[lbs_js] getLocationSwitchState result: ' + JSON.stringify(result));
30        if(!result){
31            await geolocation.requestEnableLocation().then(async(result) => {
32                await sleep(3000);
33                console.info('[lbs_js] test requestEnableLocation promise result: ' + JSON.stringify(result));
34            }).catch((error) => {
35                console.info("[lbs_js] promise then error." + JSON.stringify(error));
36                expect().assertFail();
37            });
38        }
39    });
40    await geolocation.isLocationEnabled().then(async(result) => {
41        console.info('[lbs_js] check LocationSwitchState result: ' + JSON.stringify(result));
42    });
43}
44
45async function applyPermission() {
46    let osAccountManager = osaccount.getAccountManager();
47    console.info("====>testgetuserid get AccountManager finish====");
48    let localId = await osAccountManager.getOsAccountLocalIdFromProcess();
49    console.info("====>testgetuserid localId obtained by process:" + localId);
50    let appInfo = await bundle.getApplicationInfo('ohos.acts.location.geolocation.function', 0, localId);
51    let atManager = abilityAccessCtrl.createAtManager();
52    if (atManager != null) {
53        let tokenID = appInfo.accessTokenId;
54        console.info('[permission] case accessTokenID is ' + tokenID);
55        let permissionName1 = 'ohos.permission.LOCATION';
56        let permissionName2 = 'ohos.permission.LOCATION_IN_BACKGROUND';
57        await atManager.grantUserGrantedPermission(tokenID, permissionName1, 1).then((result) => {
58            console.info('[permission] case grantUserGrantedPermission success :' + result);
59        }).catch((err) => {
60            console.info('[permission] case grantUserGrantedPermission failed :' + err);
61        });
62        await atManager.grantUserGrantedPermission(tokenID, permissionName2, 1).then((result) => {
63            console.info('[permission] case grantUserGrantedPermission success :' + result);
64        }).catch((err) => {
65            console.info('[permission] case grantUserGrantedPermission failed :' + err);
66        });
67    } else {
68        console.info('[permission] case apply permission failed, createAtManager failed');
69    }
70}
71
72let CountryCodeType = {
73        COUNTRY_CODE_FROM_LOCALE : 1,
74        COUNTRY_CODE_FROM_SIM:2,
75        COUNTRY_CODE_FROM_LOCATION:3,
76        COUNTRY_CODE_FROM_NETWORK:4,
77    }
78
79export default function geolocationTest_4() {
80
81
82    describe('geolocationTest_4', function () {
83        beforeAll(async function (done) {
84            console.info('beforeAll case');
85            await applyPermission();
86            done();
87        })
88
89        beforeEach(async function (done) {
90            console.info('beforeEach case');
91            await changedLocationMode();
92            done();
93        })
94
95    /**
96     * @tc.number SUB_HSS_LocationSystem_CountryCode_0100
97     * @tc.name Test getCountryCode
98     * @tc.desc Obtaining Country Code Information
99     * @tc.type Function
100     * @tc.level since 9
101     */
102     it('SUB_HSS_LocationSystem_CountryCode_0100', 0, async function (done) {
103        try {
104            await geolocationm.getCountryCode().then((result) => {
105                console.info("[lbs_js] getCountryCode promise result: " + JSON.stringify(result));
106                console.info("[lbs_js] country :" + result.country);
107                console.info("[lbs_js] type: " + result.type);
108                expect(true).assertEqual(result!=null);
109                expect(true).assertEqual(JSON.stringify(result.country)!=null);
110                expect(true).assertEqual(JSON.stringify(result.type)!=null);
111            }).catch((error) => {
112                console.info("[lbs_js] getCountryCode promise then error."  + JSON.stringify(error));
113                expect().assertFail();
114            });
115        } catch (error) {
116            console.info("[lbs_js] getCountryCode promise try err."  + JSON.stringify(error));
117            expect().assertFail();
118        }
119        done();
120    })
121
122    /**
123     * @tc.number SUB_HSS_LocationSystem_CountryCode_0200
124     * @tc.name Test getCountryCode
125     * @tc.desc Obtaining Country Code Information
126     * @tc.type Function
127     * @tc.level since 9
128     */
129     it('SUB_HSS_LocationSystem_CountryCode_0200', 0, async function (done) {
130        try {
131            geolocationm.getCountryCode((err,data) => {
132                if (err) {
133                    return console.info("getCountryCode callback err:  " + JSON.stringify(err));
134                } else {
135                    console.info("getCountryCode callback success"+ JSON.stringify(data));
136                    expect(true).assertEqual(data != null);
137                }
138            });
139        } catch (error) {
140            console.info("[lbs_js] getCountryCode callback try err."  + JSON.stringify(error));
141            expect().assertFail();
142        }
143        await sleep(1000);
144        done();
145    })
146
147    /**
148     * @tc.number SUB_HSS_LocationSystem_CountryCode_0300
149     * @tc.name getCountryCode_on_off
150     * @tc.desc The interception country code is changed.
151     * @tc.type Function
152     * @tc.level since 9
153     */
154    it('SUB_HSS_LocationSystem_CountryCode_0300', 0, async function (done) {
155        console.info("[lbs_js] countryCodeChange");
156        try {
157            geolocationm.on('countryCodeChange', function (data) {
158                console.info('[lbs_js] countryCodeChange' +JSON.stringify(data) );
159            });
160        } catch (error) {
161            console.info("[lbs_js] countryCodeChangeOn try err."  + JSON.stringify(error));
162            expect(true).assertEqual(JSON.stringify(error)!=null);
163        }
164        try {
165            await geolocationm.getCountryCode().then((result) => {
166                console.info("[lbs_js] getCountryCode promise result: " + JSON.stringify(result));
167                expect(true).assertEqual(JSON.stringify(result)!=null);
168            }).catch((error) => {
169                console.info("[lbs_js] getCountryCode promise then error."  + JSON.stringify(error));
170                expect().assertFail();
171            });
172        } catch (error) {
173            console.info("[lbs_js] getCountryCode callback try err."  + JSON.stringify(error));
174            expect().assertFail();
175        }
176        try {
177            geolocationm.off('countryCodeChange', function (data) {
178                console.info('[lbs_js] countryCodeChange' + JSON.stringify(data));
179            })
180        } catch (error) {
181            console.info("[lbs_js] countryCodeChangeOff try err."  + JSON.stringify(error));
182            expect(true).assertEqual(JSON.stringify(error)!=null);
183        }
184        done();
185    })
186
187    })
188}
189
190