GetCountryCode.test.js 7.9 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';
Q
quanli 已提交
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

    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 已提交
94 95
    
    /**
Q
quanli 已提交
96
     * @tc.number SUB_HSS_LocationSystem_CountryCode_0100
Q
quanli 已提交
97 98 99 100 101
     * @tc.name Test getCountryCode
     * @tc.desc Obtaining Country Code Information
     * @tc.type Function
     * @tc.level since 9
     */
Q
quanli 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
     it('SUB_HSS_LocationSystem_CountryCode_0100', 0, async function (done) {
        try {
            await geolocationm.getCountryCode().then((result) => {
                console.info("[lbs_js] getCountryCode promise result: " + JSON.stringify(result));
                console.info("[lbs_js] country :" + result.country);
                console.info("[lbs_js] type: " + result.type);
                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);
            }).catch((error) => {
                console.info("[lbs_js] getCountryCode promise then error."  + JSON.stringify(error));
                expect().assertFail();
            });
        } catch (error) {
            console.info("[lbs_js] getCountryCode promise try err."  + JSON.stringify(error));
Q
quanli 已提交
118
            expect().assertFail();
Q
quanli 已提交
119
        }
Q
quanli 已提交
120 121 122 123
        done();
    })

    /**
Q
quanli 已提交
124
     * @tc.number SUB_HSS_LocationSystem_CountryCode_0200
Q
quanli 已提交
125 126 127 128 129
     * @tc.name Test getCountryCode
     * @tc.desc Obtaining Country Code Information
     * @tc.type Function
     * @tc.level since 9
     */
Q
quanli 已提交
130
     it('SUB_HSS_LocationSystem_CountryCode_0200', 0, async function (done) {
Q
quanli 已提交
131 132 133 134 135 136 137 138 139 140 141 142
        try {
            geolocationm.getCountryCode((err,data) => {
                if (err) {
                    return console.info("getCountryCode callback err:  " + JSON.stringify(err));
                } else {
                    console.info("getCountryCode callback success"+ JSON.stringify(data));
                    expect(true).assertEqual(data != null);
                }
            });
        } catch (error) {
            console.info("[lbs_js] getCountryCode callback try err."  + JSON.stringify(error));
            expect().assertFail();
Q
quanli 已提交
143
        }
Q
quanli 已提交
144
        await sleep(1000);
Q
quanli 已提交
145 146 147 148
        done();
    })

    /**
Q
quanli 已提交
149
     * @tc.number SUB_HSS_LocationSystem_CountryCode_0300
Q
quanli 已提交
150 151 152 153 154
     * @tc.name getCountryCode_on_off
     * @tc.desc The interception country code is changed.
     * @tc.type Function
     * @tc.level since 9
     */
Q
quanli 已提交
155
    it('SUB_HSS_LocationSystem_CountryCode_0300', 0, async function (done) {
Q
quanli 已提交
156
        console.info("[lbs_js] countryCodeChange");
Q
quanli 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
        try {
            geolocationm.on('countryCodeChange', function (data) {
                console.info('[lbs_js] countryCodeChange' +JSON.stringify(data) );
            });
        } catch (error) {
            console.info("[lbs_js] countryCodeChangeOn try err."  + JSON.stringify(error));
            expect(true).assertEqual(JSON.stringify(error)!=null);
        }
        try {
            await geolocationm.getCountryCode().then((result) => {
                console.info("[lbs_js] getCountryCode promise result: " + JSON.stringify(result));
                expect(true).assertEqual(JSON.stringify(result)!=null);
            }).catch((error) => {
                console.info("[lbs_js] getCountryCode promise then error."  + JSON.stringify(error));
                expect().assertFail();
            });
        } catch (error) {
            console.info("[lbs_js] getCountryCode callback try err."  + JSON.stringify(error));
Q
quanli 已提交
175
            expect().assertFail();
Q
quanli 已提交
176 177 178 179 180 181 182 183 184
        }
        try {
            geolocationm.off('countryCodeChange', function (data) {
                console.info('[lbs_js] countryCodeChange' + JSON.stringify(data));
            })
        } catch (error) {
            console.info("[lbs_js] countryCodeChangeOff try err."  + JSON.stringify(error));
            expect(true).assertEqual(JSON.stringify(error)!=null);
        }
Q
quanli 已提交
185 186
        done();
    })
Q
quanli 已提交
187 188 189
    
    })
}
Q
quanli 已提交
190