SystemLocation.test.js 8.0 KB
Newer Older
Q
q00313334 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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';
import geolocations from '@system.geolocation';
import abilityAccessCtrl from '@ohos.abilityAccessCtrl'
import bundle from '@ohos.bundle'
19
import osaccount from '@ohos.account.osAccount'
Q
quanli 已提交
20
import {describe, beforeAll, beforeEach, afterEach, afterAll, it, expect} from '@ohos/hypium'
Q
q00313334 已提交
21 22 23

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
Q
quanli 已提交
24
}
Q
q00313334 已提交
25

26
async function changedLocationMode(){
Q
q00313334 已提交
27
    await geolocation.isLocationEnabled().then(async(result) => {
Q
quanli 已提交
28 29 30 31 32 33 34 35 36 37 38 39
        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) => {
Q
quanli 已提交
40
        console.info('[lbs_js] check LocationSwitchState result: ' + JSON.stringify(result));
Q
quanli 已提交
41
    });
Q
q00313334 已提交
42 43 44
}

async function applyPermission() {
Q
quanli 已提交
45
    let osAccountManager = osaccount.getAccountManager();
Q
quanli 已提交
46
    console.info("=== getAccountManager finish");
Q
quanli 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    let localId = await osAccountManager.getOsAccountLocalIdFromProcess();
    console.info("LocalId is :" + 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 :' + JSON.stringify(result));
        }).catch((err) => {
            console.info('[permission] case grantUserGrantedPermission failed :' + JSON.stringify(err));
        });
        await atManager.grantUserGrantedPermission(tokenID, permissionName2, 1).then((result) => {
            console.info('[permission] case grantUserGrantedPermission success :' + JSON.stringify(result));
        }).catch((err) => {
            console.info('[permission] case grantUserGrantedPermission failed :' + JSON.stringify(err));
        });
    } else {
        console.info('[permission] case apply permission failed, createAtManager failed');
    }
Q
q00313334 已提交
69
}
Q
quanli 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
export default function geolocationTest_geo2() {

    describe('geolocationTest_geo2', function () {
        beforeAll(async function (done) {
            console.info('beforeAll case');
            await applyPermission();
            done();
        })
        beforeEach(async function (done) {
            console.info('beforeEach case');
            await changedLocationMode();
            done()
        })
        afterEach(function () {
        })
Q
q00313334 已提交
85

Q
quanli 已提交
86

Q
q00313334 已提交
87
    /**
Q
quanli 已提交
88
     * @tc.number SUB_HSS_LocationSystem_systemapi_0100
Q
quanli 已提交
89 90
     * @tc.name Test getLocation
     * @tc.desc Obtains the geographical location of a device..
Q
q00313334 已提交
91 92 93 94
     * @tc.size MEDIUM
     * @tc.type Function
     * @tc.level Level 2
    */
Q
quanli 已提交
95
     it('SUB_HSS_LocationSystem_systemapi_0100', 0, async function (done) {
Q
q00313334 已提交
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
        geolocations.getLocation({
            timeout:30000,
            coordType:'wgs84',
            success: function(geolocationResponse) {
                console.log('lbs_js [GetLocation-success], result' + JSON.stringify(geolocationResponse));
                expect(true).assertEqual(geolocationResponse.length !=0);
                console.info('[lbs_js] getLocation latitude: ' + geolocationResponse.latitude +
                ' longitude: ' + geolocationResponse.longitude +' altitude: ' + geolocationResponse.altitude
                +' accuracy: ' + geolocationResponse.accuracy +'time: ' + geolocationResponse.time);
            },
            fail: function(data, code) {
                switch(code){
                    case 601:
                        console.info("获取定位权限失败/失败原因/用户拒绝: "+ err);
                        break;
                    case 602:
                        console.info("权限未声明: "+ err);
                        break;
                    case 800:
                        console.info("超时失败原因/网络状况不佳或GPS不可用: "+ err);
                        break;
                    case 801:
                        console.info("系统位置开关未打开: "+ err);
                        break;
                    case 802:
                        console.info("该次调用结果未返回前接口又被重新调用/该次调用失败返回错误码: "+ err);
                        break;
                    default:
Q
quanli 已提交
124
                        console.log('lbs_js [GetLocation-fail] data:' + data + ', code:' + code);
Q
q00313334 已提交
125 126 127 128 129
                }
            },
        });
        done();
    })
Q
quanli 已提交
130

Q
q00313334 已提交
131
    /**
Q
quanli 已提交
132
     * @tc.number SUB_HSS_LocationSystem_systemapi_0200
Q
quanli 已提交
133
     * @tc.name Test subscribe and unsubscribe
Q
q00313334 已提交
134 135 136 137 138
     * @tc.desc Test subscribe api .
     * @tc.size MEDIUM
     * @tc.type Function
     * @tc.level Level 2
    */
Q
quanli 已提交
139
    it('SUB_HSS_LocationSystem_systemapi_0200', 0,  async function (done) {
Q
q00313334 已提交
140 141 142 143 144 145 146 147 148 149 150
        geolocations.subscribe({
            coordType:'wgs84',
            success: function(data) {
                console.log('lbs_js [GetLocation-success], result' + JSON.stringify(data));
                expect(true).assertEqual(data !=null);
            },
            fail: function(data, code) {
                console.log('lbs_js [Subscribe-fail] code:' + code + ', data:' + data);
                expect().assertFail();
            },
        });
Q
quanli 已提交
151 152
        geolocation.unsubscribe();
        console.info("[lbs_js] unsubscribe called")
Q
q00313334 已提交
153 154 155 156
        done();
    })

    /**
Q
quanli 已提交
157
      * @tc.number SUB_HSS_LocationSystem_systemapi_0300
Q
quanli 已提交
158 159 160 161 162 163
      * @tc.name test getLocationType
      * @tc.desc Subscribing to geographical location information .
      * @tc.size MEDIUM
      * @tc.type Function
      * @tc.level Level 2
      */
Q
quanli 已提交
164
    it('SUB_HSS_LocationSystem_systemapi_0300', 0,  async function (done) {
Q
quanli 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
        geolocations.getLocationType({
            success: function(data) {
                console.log('success get location type:' + JSON.stringify(data));
                expect(true).assertEqual(data.types.length !=0);
                done()
            },
            fail: function(data, code) {
                console.log('fail to get location. code:' + code + ', data:' + JSON.stringify(data));
                expect().assertFail();
                done()
            },
            complete: function(result) {
                console.log('get location end' + JSON.stringify(result));
            },
        });
    })

    /**
Q
quanli 已提交
183
      * @tc.number SUB_HSS_LocationSystem_systemapi_0400
Q
quanli 已提交
184 185 186 187 188 189
      * @tc.name Test getSupportedCoordTypes
      * @tc.desc Obtains the geographical location of a device..
      * @tc.size MEDIUM
      * @tc.type Function
      * @tc.level Level 2
      */
Q
quanli 已提交
190
    it('SUB_HSS_LocationSystem_systemapi_0400', 0,  function () {
Q
quanli 已提交
191 192 193 194
        let types = geolocations.getSupportedCoordTypes();
        console.info('[lbs_js] getSupportedCoordTypes result: ' + JSON.stringify(types));
        expect(true).assertEqual(types.length !=0);

Q
q00313334 已提交
195
    })
Q
quanli 已提交
196 197 198 199

    })
}

Q
q00313334 已提交
200 201


202

Q
quanli 已提交
203