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 geolocations from '@system.geolocation'; 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} 25 26async function changedLocationMode(){ 27 await geolocation.isLocationEnabled().then(async(result) => { 28 console.info('[lbs_js] getLocationSwitchState result: ' + JSON.stringify(result)); 29 if(!result){ 30 await geolocation.requestEnableLocation().then(async(result) => { 31 await sleep(3000); 32 console.info('[lbs_js] test requestEnableLocation promise result: ' + JSON.stringify(result)); 33 }).catch((error) => { 34 console.info("[lbs_js] promise then error." + JSON.stringify(error)); 35 expect().assertFail(); 36 }); 37 } 38 }); 39 await geolocation.isLocationEnabled().then(async(result) => { 40 console.info('[lbs_js] check LocationSwitchState result: ' + JSON.stringify(result)); 41 }); 42} 43 44async function applyPermission() { 45 let osAccountManager = osaccount.getAccountManager(); 46 console.info("=== getAccountManager finish"); 47 let localId = await osAccountManager.getOsAccountLocalIdFromProcess(); 48 console.info("LocalId is :" + localId); 49 let appInfo = await bundle.getApplicationInfo('ohos.acts.location.geolocation.function', 0, localId); 50 let atManager = abilityAccessCtrl.createAtManager(); 51 if (atManager != null) { 52 let tokenID = appInfo.accessTokenId; 53 console.info('[permission] case accessTokenID is ' + tokenID); 54 let permissionName1 = 'ohos.permission.LOCATION'; 55 let permissionName2 = 'ohos.permission.LOCATION_IN_BACKGROUND'; 56 await atManager.grantUserGrantedPermission(tokenID, permissionName1, 1).then((result) => { 57 console.info('[permission] case grantUserGrantedPermission success :' + JSON.stringify(result)); 58 }).catch((err) => { 59 console.info('[permission] case grantUserGrantedPermission failed :' + JSON.stringify(err)); 60 }); 61 await atManager.grantUserGrantedPermission(tokenID, permissionName2, 1).then((result) => { 62 console.info('[permission] case grantUserGrantedPermission success :' + JSON.stringify(result)); 63 }).catch((err) => { 64 console.info('[permission] case grantUserGrantedPermission failed :' + JSON.stringify(err)); 65 }); 66 } else { 67 console.info('[permission] case apply permission failed, createAtManager failed'); 68 } 69} 70export default function geolocationTest_geo2() { 71 72 describe('geolocationTest_geo2', function () { 73 beforeAll(async function (done) { 74 console.info('beforeAll case'); 75 await applyPermission(); 76 done(); 77 }) 78 beforeEach(async function (done) { 79 console.info('beforeEach case'); 80 await changedLocationMode(); 81 done() 82 }) 83 afterEach(function () { 84 }) 85 86 87 /** 88 * @tc.number SUB_HSS_LocationSystem_systemapi_0100 89 * @tc.name Test getLocation 90 * @tc.desc Obtains the geographical location of a device.. 91 * @tc.size MEDIUM 92 * @tc.type Function 93 * @tc.level Level 2 94 */ 95 it('SUB_HSS_LocationSystem_systemapi_0100', 0, async function (done) { 96 geolocations.getLocation({ 97 timeout:30000, 98 coordType:'wgs84', 99 success: function(geolocationResponse) { 100 console.log('lbs_js [GetLocation-success], result' + JSON.stringify(geolocationResponse)); 101 expect(true).assertEqual(geolocationResponse.length !=0); 102 console.info('[lbs_js] getLocation latitude: ' + geolocationResponse.latitude + 103 ' longitude: ' + geolocationResponse.longitude +' altitude: ' + geolocationResponse.altitude 104 +' accuracy: ' + geolocationResponse.accuracy +'time: ' + geolocationResponse.time); 105 }, 106 fail: function(data, code) { 107 switch(code){ 108 case 601: 109 console.info("获取定位权限失败/失败原因/用户拒绝: "+ err); 110 break; 111 case 602: 112 console.info("权限未声明: "+ err); 113 break; 114 case 800: 115 console.info("超时失败原因/网络状况不佳或GPS不可用: "+ err); 116 break; 117 case 801: 118 console.info("系统位置开关未打开: "+ err); 119 break; 120 case 802: 121 console.info("该次调用结果未返回前接口又被重新调用/该次调用失败返回错误码: "+ err); 122 break; 123 default: 124 console.log('lbs_js [GetLocation-fail] data:' + data + ', code:' + code); 125 } 126 }, 127 }); 128 done(); 129 }) 130 131 /** 132 * @tc.number SUB_HSS_LocationSystem_systemapi_0200 133 * @tc.name Test subscribe and unsubscribe 134 * @tc.desc Test subscribe api . 135 * @tc.size MEDIUM 136 * @tc.type Function 137 * @tc.level Level 2 138 */ 139 it('SUB_HSS_LocationSystem_systemapi_0200', 0, async function (done) { 140 geolocations.subscribe({ 141 coordType:'wgs84', 142 success: function(data) { 143 console.log('lbs_js [GetLocation-success], result' + JSON.stringify(data)); 144 expect(true).assertEqual(data !=null); 145 }, 146 fail: function(data, code) { 147 console.log('lbs_js [Subscribe-fail] code:' + code + ', data:' + data); 148 expect().assertFail(); 149 }, 150 }); 151 geolocations.unsubscribe(); 152 console.info("[lbs_js] unsubscribe called") 153 done(); 154 }) 155 156 /** 157 * @tc.number SUB_HSS_LocationSystem_systemapi_0300 158 * @tc.name test getLocationType 159 * @tc.desc Subscribing to geographical location information . 160 * @tc.size MEDIUM 161 * @tc.type Function 162 * @tc.level Level 2 163 */ 164 it('SUB_HSS_LocationSystem_systemapi_0300', 0, async function (done) { 165 geolocations.getLocationType({ 166 success: function(data) { 167 console.log('success get location type:' + JSON.stringify(data)); 168 expect(true).assertEqual(data.types.length !=0); 169 done() 170 }, 171 fail: function(data, code) { 172 console.log('fail to get location. code:' + code + ', data:' + JSON.stringify(data)); 173 expect().assertFail(); 174 done() 175 }, 176 complete: function(result) { 177 console.log('get location end' + JSON.stringify(result)); 178 }, 179 }); 180 }) 181 182 /** 183 * @tc.number SUB_HSS_LocationSystem_systemapi_0400 184 * @tc.name Test getSupportedCoordTypes 185 * @tc.desc Obtains the geographical location of a device.. 186 * @tc.size MEDIUM 187 * @tc.type Function 188 * @tc.level Level 2 189 */ 190 it('SUB_HSS_LocationSystem_systemapi_0400', 0, function () { 191 let types = geolocations.getSupportedCoordTypes(); 192 console.info('[lbs_js] getSupportedCoordTypes result: ' + JSON.stringify(types)); 193 expect(true).assertEqual(types.length !=0); 194 195 }) 196 197 }) 198} 199 200 201 202 203 204