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 */
15
16 #include "geo_convert_service_test.h"
17 #include <string>
18 #include "common_utils.h"
19 #include "geo_convert_service.h"
20 #include "geo_convert_skeleton.h"
21 #include "if_system_ability_manager.h"
22 #include "ipc_skeleton.h"
23 #include "iservice_registry.h"
24 #include "location_log.h"
25 #include "parameters.h"
26 #include "string_ex.h"
27 #include "system_ability_definition.h"
28
29 using namespace testing::ext;
30 using namespace OHOS;
31 using namespace OHOS::Location;
32
SetUp()33 void GeoConvertServiceTest::SetUp()
34 {
35 /*
36 * @tc.setup: Get system ability's pointer and get sa proxy object.
37 */
38 sptr<ISystemAbilityManager> systemAbilityManager =
39 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
40 EXPECT_NE(nullptr, systemAbilityManager);
41 sptr<IRemoteObject> object = systemAbilityManager->GetSystemAbility(LOCATION_GEO_CONVERT_SA_ID);
42 EXPECT_NE(nullptr, object);
43 proxy_ = new (std::nothrow) GeoConvertProxy(object);
44 EXPECT_NE(nullptr, proxy_);
45 available_ = Available();
46 }
47
TearDown()48 void GeoConvertServiceTest::TearDown()
49 {
50 /*
51 * @tc.teardown: release memory.
52 */
53 proxy_ = nullptr;
54 }
55
Available()56 bool GeoConvertServiceTest::Available()
57 {
58 MessageParcel dataParcel;
59 MessageParcel replyParcel;
60 if (proxy_ != nullptr) {
61 proxy_->IsGeoConvertAvailable(dataParcel, replyParcel);
62 }
63 replyParcel.ReadInt32();
64 int temp = replyParcel.ReadInt32();
65 bool result = (temp != 0);
66 return result;
67 }
68
69 /*
70 * @tc.name: GeoConvertAvailable001
71 * @tc.desc: Check location system ability whether available.
72 * @tc.type: FUNC
73 */
74 HWTEST_F(GeoConvertServiceTest, GeoConvertAvailable001, TestSize.Level1)
75 {
76 if (!available_) {
77 return;
78 }
79
80 /*
81 * @tc.steps: step1. Call system ability and check whether available.
82 * @tc.expected: step1. system ability is available.
83 */
84 bool result = Available();
85 EXPECT_EQ(true, result);
86 }
87
88 /*
89 * @tc.name: GeoAddressByCoordinate001
90 * @tc.desc: Test get address from system ability by coordinate.
91 * @tc.type: FUNC
92 */
93 HWTEST_F(GeoConvertServiceTest, GetAddressByCoordinate001, TestSize.Level1)
94 {
95 if (!available_) {
96 return;
97 }
98
99 /*
100 * @tc.steps: step1.read test data.
101 */
102 MessageParcel dataParcel;
103 MessageParcel replyParcel;
104 dataParcel.WriteDouble(39.92879); // latitude
105 dataParcel.WriteDouble(116.3709); // longitude
106 dataParcel.WriteInt32(5); // maxItem
107 dataParcel.WriteInt32(1); // geocoder param object tag
108 dataParcel.WriteString16(Str8ToStr16("ZH")); // language
109 dataParcel.WriteString16(Str8ToStr16("cn")); // country
110 dataParcel.WriteString16(Str8ToStr16("")); // description
111 dataParcel.WriteString16(Str8ToStr16("test")); // package name
112
113 /*
114 * @tc.steps: step2. test get address by coordinate.
115 * @tc.expected: step2. no exception head info.
116 */
117 proxy_->GetAddressByCoordinate(dataParcel, replyParcel);
118 bool ret = false;
119 int exceptionHeader = replyParcel.ReadInt32();
120 if (exceptionHeader == REPLY_NO_EXCEPTION) {
121 ret = true;
122 }
123 EXPECT_TRUE(ret);
124 }
125
126 /*
127 * @tc.name: GetAddressByLocationName001
128 * @tc.desc: Test get address from system ability by location name.
129 * @tc.type: FUNC
130 */
131 HWTEST_F(GeoConvertServiceTest, GetAddressByLocationName001, TestSize.Level1)
132 {
133 if (!available_) {
134 return;
135 }
136
137 /*
138 * @tc.steps: step1.read test data.
139 */
140 MessageParcel dataParcel;
141 MessageParcel replyParcel;
142 dataParcel.WriteString16(Str8ToStr16("北京")); // input description of a location
143 dataParcel.WriteDouble(0.0); // minLatitude
144 dataParcel.WriteDouble(0.0); // minLongitude
145 dataParcel.WriteDouble(0.0); // maxLatitude
146 dataParcel.WriteDouble(0.0); // maxLongitude
147 dataParcel.WriteInt32(5); // maxItem
148 dataParcel.WriteInt32(1); // description
149 dataParcel.WriteString16(Str8ToStr16("ZH")); // language
150 dataParcel.WriteString16(Str8ToStr16("cn")); // country
151 dataParcel.WriteString16(Str8ToStr16("")); // description
152 dataParcel.WriteString16(u"ohos"); // package name
153
154 /*
155 * @tc.steps: step2. test get address by location's name.
156 * @tc.expected: step2. no exception head info.
157 */
158 proxy_->GetAddressByLocationName(dataParcel, replyParcel);
159 bool ret = false;
160 int exceptionHeader = replyParcel.ReadInt32();
161 if (exceptionHeader == REPLY_NO_EXCEPTION) {
162 ret = true;
163 }
164 EXPECT_TRUE(ret);
165 }
166