tdd用例修复

Signed-off-by: z30025928 <734222381@qq.com>
This commit is contained in:
z30025928 2023-02-28 09:42:40 +08:00
parent 8ef52c87a7
commit 481d7a6f0c
6 changed files with 99 additions and 44 deletions

View File

@ -27,6 +27,7 @@ public:
void MockNativePermission();
void MockNativeApproxiPermission();
void MockNativeAccurateLocation();
void LoadSystemAbility();
uint64_t tokenId_;
uint64_t tokenIdForApproxi_;

View File

@ -26,6 +26,7 @@
#include "common_utils.h"
#include "location_log.h"
#include "location_sa_load_manager.h"
using namespace testing::ext;
namespace OHOS {
@ -39,6 +40,7 @@ const double NUM_ACC_E6 = 1.000001;
const double NUM_ACC_E7 = 1.0000001;
void CommonUtilsTest::SetUp()
{
LoadSystemAbility();
}
void CommonUtilsTest::TearDown()
@ -108,6 +110,23 @@ void CommonUtilsTest::MockNativeAccurateLocation()
Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
}
void CommonUtilsTest::LoadSystemAbility()
{
LocationSaLoadManager::GetInstance().LoadLocationSa(LOCATION_LOCATOR_SA_ID);
#ifdef FEATURE_GNSS_SUPPORT
LocationSaLoadManager::GetInstance().LoadLocationSa(LOCATION_GNSS_SA_ID);
#endif
#ifdef FEATURE_PASSIVE_SUPPORT
LocationSaLoadManager::GetInstance().LoadLocationSa(LOCATION_NOPOWER_LOCATING_SA_ID);
#endif
#ifdef FEATURE_NETWORK_SUPPORT
LocationSaLoadManager::GetInstance().LoadLocationSa(LOCATION_NETWORK_LOCATING_SA_ID);
#endif
#ifdef FEATURE_GEOCODE_SUPPORT
LocationSaLoadManager::GetInstance().LoadLocationSa(LOCATION_GEO_CONVERT_SA_ID);
#endif
}
HWTEST_F(CommonUtilsTest, AbilityConvertToIdTest001, TestSize.Level1)
{
GTEST_LOG_(INFO)

View File

@ -75,28 +75,28 @@ void LocationCommonTest::TearDown()
void LocationCommonTest::SetGeoAddress(std::unique_ptr<GeoAddress>& geoAddress)
{
MessageParcel parcel;
parcel.WriteString("localeLanguage");
parcel.WriteString("localeCountry");
parcel.WriteString16(u"localeLanguage");
parcel.WriteString16(u"localeCountry");
parcel.WriteInt32(1); // size
parcel.WriteInt32(0); // line
parcel.WriteString("line");
parcel.WriteString("placeName");
parcel.WriteString("administrativeArea");
parcel.WriteString("subAdministrativeArea");
parcel.WriteString("locality");
parcel.WriteString("subLocality");
parcel.WriteString("roadName");
parcel.WriteString("subRoadName");
parcel.WriteString("premises");
parcel.WriteString("postalCode");
parcel.WriteString("countryCode");
parcel.WriteString("countryName");
parcel.WriteString16(u"line");
parcel.WriteString16(u"placeName");
parcel.WriteString16(u"administrativeArea");
parcel.WriteString16(u"subAdministrativeArea");
parcel.WriteString16(u"locality");
parcel.WriteString16(u"subLocality");
parcel.WriteString16(u"roadName");
parcel.WriteString16(u"subRoadName");
parcel.WriteString16(u"premises");
parcel.WriteString16(u"postalCode");
parcel.WriteString16(u"countryCode");
parcel.WriteString16(u"countryName");
parcel.WriteInt32(1); // hasLatitude
parcel.WriteDouble(MOCK_LATITUDE); // latitude
parcel.WriteInt32(1); // hasLongitude
parcel.WriteDouble(MOCK_LONGITUDE); // longitude
parcel.WriteString("phoneNumber");
parcel.WriteString("addressUrl");
parcel.WriteString16(u"phoneNumber");
parcel.WriteString16(u"addressUrl");
parcel.WriteBool(true);
geoAddress->ReadFromParcel(parcel);
}
@ -136,28 +136,28 @@ void LocationCommonTest::VerifyGeoAddressReadFromParcel(std::unique_ptr<GeoAddre
#ifdef FEATURE_GEOCODE_SUPPORT
void LocationCommonTest::VerifyGeoAddressMarshalling(MessageParcel& newParcel)
{
EXPECT_EQ("localeLanguage", newParcel.ReadString());
EXPECT_EQ("localeCountry", newParcel.ReadString());
EXPECT_EQ("localeLanguage", Str16ToStr8(newParcel.ReadString16()));
EXPECT_EQ("localeCountry", Str16ToStr8(newParcel.ReadString16()));
EXPECT_EQ(1, newParcel.ReadInt32());
EXPECT_EQ(0, newParcel.ReadInt32());
EXPECT_EQ("line", newParcel.ReadString());
EXPECT_EQ("placeName", newParcel.ReadString());
EXPECT_EQ("administrativeArea", newParcel.ReadString());
EXPECT_EQ("subAdministrativeArea", newParcel.ReadString());
EXPECT_EQ("locality", newParcel.ReadString());
EXPECT_EQ("subLocality", newParcel.ReadString());
EXPECT_EQ("roadName", newParcel.ReadString());
EXPECT_EQ("subRoadName", newParcel.ReadString());
EXPECT_EQ("premises", newParcel.ReadString());
EXPECT_EQ("postalCode", newParcel.ReadString());
EXPECT_EQ("countryCode", newParcel.ReadString());
EXPECT_EQ("countryName", newParcel.ReadString());
EXPECT_EQ("line", Str16ToStr8(newParcel.ReadString16()));
EXPECT_EQ("placeName", Str16ToStr8(newParcel.ReadString16()));
EXPECT_EQ("administrativeArea", Str16ToStr8(newParcel.ReadString16()));
EXPECT_EQ("subAdministrativeArea", Str16ToStr8(newParcel.ReadString16()));
EXPECT_EQ("locality", Str16ToStr8(newParcel.ReadString16()));
EXPECT_EQ("subLocality", Str16ToStr8(newParcel.ReadString16()));
EXPECT_EQ("roadName", Str16ToStr8(newParcel.ReadString16()));
EXPECT_EQ("subRoadName", Str16ToStr8(newParcel.ReadString16()));
EXPECT_EQ("premises", Str16ToStr8(newParcel.ReadString16()));
EXPECT_EQ("postalCode", Str16ToStr8(newParcel.ReadString16()));
EXPECT_EQ("countryCode", Str16ToStr8(newParcel.ReadString16()));
EXPECT_EQ("countryName", Str16ToStr8(newParcel.ReadString16()));
EXPECT_EQ(1, newParcel.ReadInt32());
EXPECT_EQ(MOCK_LATITUDE, newParcel.ReadDouble());
EXPECT_EQ(1, newParcel.ReadInt32());
EXPECT_EQ(MOCK_LONGITUDE, newParcel.ReadDouble());
EXPECT_EQ("phoneNumber", newParcel.ReadString());
EXPECT_EQ("addressUrl", newParcel.ReadString());
EXPECT_EQ("phoneNumber", Str16ToStr8(newParcel.ReadString16()));
EXPECT_EQ("addressUrl", Str16ToStr8(newParcel.ReadString16()));
EXPECT_EQ(true, newParcel.ReadBool());
}
#endif
@ -172,7 +172,7 @@ void LocationCommonTest::VerifyLocationMarshalling(MessageParcel& newParcel)
EXPECT_EQ(VERIFY_LOCATION_DIRECTION, newParcel.ReadDouble()); // direction
EXPECT_EQ(VERIFY_LOCATION_TIME, newParcel.ReadInt64()); // timeStamp
EXPECT_EQ(VERIFY_LOCATION_TIMESINCEBOOT, newParcel.ReadInt64()); // timeSinceBoot
EXPECT_EQ(u"additions", newParcel.ReadString16()); // additions
EXPECT_EQ("additions", Str16ToStr8(newParcel.ReadString16())); // additions
EXPECT_EQ(1, newParcel.ReadInt64()); // additionSize
EXPECT_EQ(true, newParcel.ReadBool()); // isFromMock
EXPECT_EQ(1, newParcel.ReadInt32()); // sourceType

View File

@ -125,6 +125,8 @@ HWTEST_F(LocationWithoutPermissionTest, LocatorWithoutLocationPermission002, Tes
LBSLOGI(LOCATOR, "[LocationWithoutPermissionTest] LocatorWithoutLocationPermission002 begin");
std::unique_ptr<Locator> locatorImpl = Locator::GetInstance();
EXPECT_NE(nullptr, locatorImpl);
bool state = false;
EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl->IsLocationEnabledV9(state));
#ifdef FEATURE_GNSS_SUPPORT
auto gnssCallbackHost =
sptr<GnssStatusCallbackHost>(new (std::nothrow) GnssStatusCallbackHost());
@ -141,7 +143,11 @@ HWTEST_F(LocationWithoutPermissionTest, LocatorWithoutLocationPermission002, Tes
EXPECT_EQ(false, locatorImpl->IsLocationPrivacyConfirmed(1));
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->SetLocationPrivacyConfirmStatus(1, true));
#ifdef FEATURE_GNSS_SUPPORT
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->FlushCachedGnssLocations());
if (state) {
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->FlushCachedGnssLocations());
} else {
EXPECT_EQ(ERRCODE_SWITCH_OFF, locatorImpl->FlushCachedGnssLocations());
}
#endif
EXPECT_EQ(false, locatorImpl->ProxyUidForFreeze(1000, false));
@ -205,14 +211,25 @@ HWTEST_F(LocationWithoutPermissionTest, LocatorWithoutLocationPermissionV9001, T
std::unique_ptr<Locator> locatorImpl = Locator::GetInstance();
EXPECT_NE(nullptr, locatorImpl);
bool state = false;
EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl->IsLocationEnabledV9(state));
std::unique_ptr<RequestConfig> requestConfig = std::make_unique<RequestConfig>();
requestConfig->SetPriority(PRIORITY_ACCURACY);
sptr<ILocatorCallback> callbackStub = new (std::nothrow) LocatorCallbackStub();
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->StartLocatingV9(requestConfig, callbackStub));
if (state) {
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->StartLocatingV9(requestConfig, callbackStub));
} else {
EXPECT_EQ(ERRCODE_SWITCH_OFF, locatorImpl->StartLocatingV9(requestConfig, callbackStub));
}
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->StopLocatingV9(callbackStub));
std::unique_ptr loc = std::make_unique<Location>();
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->GetCachedLocationV9(loc));
if (state) {
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->GetCachedLocationV9(loc));
} else {
EXPECT_EQ(ERRCODE_SWITCH_OFF, locatorImpl->GetCachedLocationV9(loc));
}
EXPECT_EQ(nullptr, loc);
LBSLOGI(LOCATOR, "[LocationWithoutPermissionTest] LocatorWithoutLocationPermissionV9001 end");
}
@ -224,6 +241,9 @@ HWTEST_F(LocationWithoutPermissionTest, LocatorWithoutLocationPermissionV9002, T
LBSLOGI(LOCATOR, "[LocationWithoutPermissionTest] LocatorWithoutLocationPermissionV9002 begin");
std::unique_ptr<Locator> locatorImpl = Locator::GetInstance();
EXPECT_NE(nullptr, locatorImpl);
bool state = false;
EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl->IsLocationEnabledV9(state));
#ifdef FEATURE_GNSS_SUPPORT
auto gnssCallbackHost =
sptr<GnssStatusCallbackHost>(new (std::nothrow) GnssStatusCallbackHost());
@ -236,8 +256,11 @@ HWTEST_F(LocationWithoutPermissionTest, LocatorWithoutLocationPermissionV9002, T
EXPECT_NE(nullptr, nmeaCallbackHost);
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->RegisterNmeaMessageCallbackV9(nmeaCallbackHost->AsObject()));
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->UnregisterNmeaMessageCallbackV9(nmeaCallbackHost->AsObject()));
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->FlushCachedGnssLocationsV9());
if (state) {
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->FlushCachedGnssLocationsV9());
} else {
EXPECT_EQ(ERRCODE_SWITCH_OFF, locatorImpl->FlushCachedGnssLocationsV9());
}
#endif
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->ProxyUidForFreezeV9(1000, false));
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->ResetAllProxyV9());
@ -251,6 +274,9 @@ HWTEST_F(LocationWithoutPermissionTest, LocatorWithoutLocationPermissionV9003, T
LBSLOGI(LOCATOR, "[LocationWithoutPermissionTest] LocatorWithoutLocationPermissionV9003 begin");
std::unique_ptr<Locator> locatorImpl = Locator::GetInstance();
EXPECT_NE(nullptr, locatorImpl);
bool state = false;
EXPECT_EQ(ERRCODE_SUCCESS, locatorImpl->IsLocationEnabledV9(state));
#ifdef FEATURE_GNSS_SUPPORT
std::unique_ptr<GeofenceRequest> fenceRequest = std::make_unique<GeofenceRequest>();
fenceRequest->scenario = SCENE_NAVIGATION;
@ -260,11 +286,20 @@ HWTEST_F(LocationWithoutPermissionTest, LocatorWithoutLocationPermissionV9003, T
geofence.radius = 3.0;
geofence.expiration = 4.0;
fenceRequest->geofence = geofence;
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->AddFenceV9(fenceRequest));
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->RemoveFenceV9(fenceRequest));
if (state) {
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->AddFenceV9(fenceRequest));
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->RemoveFenceV9(fenceRequest));
} else {
EXPECT_EQ(ERRCODE_SWITCH_OFF, locatorImpl->AddFenceV9(fenceRequest));
EXPECT_EQ(ERRCODE_SWITCH_OFF, locatorImpl->RemoveFenceV9(fenceRequest));
}
int size = -1;
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->GetCachedGnssLocationsSizeV9(size));
if (state) {
EXPECT_EQ(ERRCODE_PERMISSION_DENIED, locatorImpl->GetCachedGnssLocationsSizeV9(size));
} else {
EXPECT_EQ(ERRCODE_SWITCH_OFF, locatorImpl->GetCachedGnssLocationsSizeV9(size));
}
EXPECT_EQ(0, size);
auto cachedLocationsCallbackHost =

View File

@ -60,8 +60,8 @@ const double MOCK_LONGITUDE = 100.0;
#endif
void LocatorImplTest::SetUp()
{
LoadSystemAbility();
MockNativePermission();
LoadSystemAbility();
locatorImpl_ = Locator::GetInstance();
ASSERT_TRUE(locatorImpl_ != nullptr);
callbackStub_ = new (std::nothrow) LocatorCallbackStub();

View File

@ -1451,7 +1451,7 @@ HWTEST_F(LocatorServiceTest, locatorImpl001, TestSize.Level1)
locatorImpl->EnableAbility(true);
EXPECT_EQ(true, locatorImpl->IsLocationEnabled());
EXPECT_NE(nullptr, locatorImpl->GetCachedLocation());
EXPECT_EQ(nullptr, locatorImpl->GetCachedLocation());
locatorImpl->SetLocationPrivacyConfirmStatus(1, true);
EXPECT_EQ(true, locatorImpl->IsLocationPrivacyConfirmed(1));