GetCountryCode.test.js 7.0 KB
Newer Older
Q
quanli 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (C) 2022 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 geolocation from '@ohos.geolocation';
16
import geolocationm from '@ohos.geoLocationManager';
Q
quanli 已提交
17 18 19
import abilityAccessCtrl from '@ohos.abilityAccessCtrl'
import bundle from '@ohos.bundle'
import osaccount from '@ohos.account.osAccount'
Q
quanli 已提交
20
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium'
Q
quanli 已提交
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

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
    console.info('[lbs_js]sleep function');
}

async function changedLocationMode(){
    await geolocation.isLocationEnabled().then(async(result) => {
        console.info('[lbs_js] getLocationSwitchState result: ' + JSON.stringify(result));
        if(!result){
            await geolocation.requestEnableLocation().then(async(result) => {
                await sleep(3000);
                console.info('[lbs_js] test requestEnableLocation promise result: ' + JSON.stringify(result));
            }).catch((error) => {
                console.info("[lbs_js] promise then error." + JSON.stringify(error));
                expect().assertFail();
            });
        }
    });
    await geolocation.isLocationEnabled().then(async(result) => {
        console.info('[lbs_js] check LocationSwitchState result: ' + JSON.stringify(result));
    });
}

async function applyPermission() {
    let osAccountManager = osaccount.getAccountManager();
    console.info("====>testgetuserid get AccountManager finish====");
    let localId = await osAccountManager.getOsAccountLocalIdFromProcess();
    console.info("====>testgetuserid localId obtained by process:" + localId);
    let appInfo = await bundle.getApplicationInfo('ohos.acts.location.geolocation.function', 0, localId);
    let atManager = abilityAccessCtrl.createAtManager();
    if (atManager != null) {
        let tokenID = appInfo.accessTokenId;
        console.info('[permission] case accessTokenID is ' + tokenID);
        let permissionName1 = 'ohos.permission.LOCATION';
        let permissionName2 = 'ohos.permission.LOCATION_IN_BACKGROUND';
        await atManager.grantUserGrantedPermission(tokenID, permissionName1, 1).then((result) => {
            console.info('[permission] case grantUserGrantedPermission success :' + result);
        }).catch((err) => {
            console.info('[permission] case grantUserGrantedPermission failed :' + err);
        });
        await atManager.grantUserGrantedPermission(tokenID, permissionName2, 1).then((result) => {
            console.info('[permission] case grantUserGrantedPermission success :' + result);
        }).catch((err) => {
            console.info('[permission] case grantUserGrantedPermission failed :' + err);
        });
    } else {
        console.info('[permission] case apply permission failed, createAtManager failed');
    }
}

let CountryCodeType = {
        COUNTRY_CODE_FROM_LOCALE : 1,
        COUNTRY_CODE_FROM_SIM:2,
        COUNTRY_CODE_FROM_LOCATION:3,
        COUNTRY_CODE_FROM_NETWORK:4,
    }

Q
quanli 已提交
79
export default function geolocationTest_4() {
Q
quanli 已提交
80

Q
quanli 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94

    describe('geolocationTest_4', function () {
        beforeAll(async function (done) {
            console.info('beforeAll case');
            await applyPermission();
            done();
        })
    
        beforeEach(async function (done) {
            console.info('beforeEach case');
            await changedLocationMode();
            done();
        })
    
Q
quanli 已提交
95 96
    
    /**
Q
quanli 已提交
97
     * @tc.number SUB_HSS_LocationSystem_CountryCode_0100
Q
quanli 已提交
98 99 100 101 102
     * @tc.name Test getCountryCode
     * @tc.desc Obtaining Country Code Information
     * @tc.type Function
     * @tc.level since 9
     */
Q
quanli 已提交
103
     it('SUB_HSS_LocationSystem_CountryCode_0100', 0, async function (done) {
104
        await geolocationm.getCountryCode().then((result) => {
Q
quanli 已提交
105 106 107
            console.info("[lbs_js] getCountryCode promise result: " + JSON.stringify(result));
            console.info("[lbs_js] country :" + result.country);
            console.info("[lbs_js] type: " + result.type);
Q
quanli 已提交
108 109 110 111
            expect(true).assertEqual(JSON.stringify(result.type)==CountryCodeType.COUNTRY_CODE_FROM_LOCALE);
            expect(true).assertEqual(JSON.stringify(result.type)!=CountryCodeType.COUNTRY_CODE_FROM_SIM);
            expect(true).assertEqual(JSON.stringify(result.type)!=CountryCodeType.COUNTRY_CODE_FROM_LOCATION);
            expect(true).assertEqual(JSON.stringify(result.type)!=CountryCodeType.COUNTRY_CODE_FROM_NETWORK);
Q
quanli 已提交
112 113 114 115 116 117 118 119
        }).catch((error) => {
            console.info("[lbs_js] getCountryCode promise then error."  + JSON.stringify(error));
            expect().assertFail();
        });
        done();
    })

    /**
Q
quanli 已提交
120
     * @tc.number SUB_HSS_LocationSystem_CountryCode_0200
Q
quanli 已提交
121 122 123 124 125
     * @tc.name Test getCountryCode
     * @tc.desc Obtaining Country Code Information
     * @tc.type Function
     * @tc.level since 9
     */
Q
quanli 已提交
126
    it('SUB_HSS_LocationSystem_CountryCode_0200', 0, async function (done) {
Q
quanli 已提交
127 128
        function getCountryCodeCallback() {
            return new Promise((resolve, reject) => {
129
                geolocationm.getCountryCode((err,data) => {
Q
quanli 已提交
130 131 132
                    if (err) {
                        return console.info("getCountryCode callback err:  " + JSON.stringify(err));
                    } else {
Q
quanli 已提交
133 134
                        console.info("getCountryCode callback success"+ JSON.stringify(data));
			expect(true).assertEqual(data != null);
Q
quanli 已提交
135 136 137 138 139 140 141 142 143 144
                    }
                    resolve();
                })
            })
        }
        await getCountryCodeCallback();
        done();
    })

    /**
Q
quanli 已提交
145
     * @tc.number SUB_HSS_LocationSystem_CountryCode_0300
Q
quanli 已提交
146 147 148 149 150
     * @tc.name getCountryCode_on_off
     * @tc.desc The interception country code is changed.
     * @tc.type Function
     * @tc.level since 9
     */
Q
quanli 已提交
151
    it('SUB_HSS_LocationSystem_CountryCode_0300', 0, async function (done) {
Q
quanli 已提交
152
        console.info("[lbs_js]countryCodeChange");
153
        geolocationm.on('countryCodeChange', function (data) {
Q
quanli 已提交
154 155
            console.info('[lbs_js] countryCodeChange' +JSON.stringify(data) );
        });
156
        await geolocationm.getCountryCode().then((result) => {
Q
quanli 已提交
157 158 159 160 161 162
            console.info("[lbs_js] getCountryCode promise result: " + JSON.stringify(result));
            expect(true).assertTrue(JSON.stringify(result)!=null);
        }).catch((error) => {
            console.info("[lbs_js] getCountryCode promise then error."  + JSON.stringify(error));
            expect().assertFail();
        });
163
        geolocationm.off('countryCodeChange', function (data) {
Q
quanli 已提交
164 165 166 167 168
            console.info('[lbs_js] countryCodeChange' + JSON.stringify(data));
            done();
        })
        done();
    })
Q
quanli 已提交
169 170 171
    
    })
}
Q
quanli 已提交
172