• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 <cJSON.h>
17 #include "poi_info_manager_test.h"
18 #include "common_utils.h"
19 
20 using namespace testing::ext;
21 namespace OHOS {
22 namespace Location {
23 
SetUp()24 void PoiInfoManagerTest::SetUp()
25 {
26     poiInfoManager_ = PoiInfoManager::GetInstance();
27     EXPECT_NE(nullptr, poiInfoManager_);
28 }
29 
TearDown()30 void PoiInfoManagerTest::TearDown()
31 {
32     poiInfoManager_ = nullptr;
33 }
34 
MockLocation()35 std::unique_ptr<Location> PoiInfoManagerTest::MockLocation()
36 {
37     std::unique_ptr<Location> location = std::make_unique<Location>();
38     MessageParcel parcel;
39     parcel.WriteDouble(12.0); // latitude
40     parcel.WriteDouble(13.0); // longitude
41     parcel.WriteDouble(14.0); // altitude
42     parcel.WriteDouble(1000.0); // accuracy
43     parcel.WriteDouble(10.0); // speed
44     parcel.WriteDouble(90.0); // direction
45     parcel.WriteInt64(1000000000); // timeStamp
46     parcel.WriteInt64(1000000100); // timeSinceBoot
47     parcel.WriteString16(u"additions"); // additions
48     parcel.WriteInt64(1); // additionSize
49     parcel.WriteInt32(1); // isFromMock
50     location->ReadFromParcel(parcel);
51     std::vector<std::string> additionMap = location->GetAdditions();
52     return location;
53 }
54 
MockPoiString(uint64_t delayTimeMilSec)55 std::string PoiInfoManagerTest::MockPoiString(uint64_t delayTimeMilSec)
56 {
57     // 创建根对象
58     cJSON *root = cJSON_CreateObject();
59     // 创建 pois 数组
60     cJSON *poisArray = cJSON_CreateArray();
61     // 创建第一个 poi 对象
62     cJSON *poi1 = cJSON_CreateObject();
63     cJSON_AddStringToObject(poi1, "id", "42345678");
64     cJSON_AddNumberToObject(poi1, "confidence", 1);
65     cJSON_AddStringToObject(poi1, "name", "静安嘉里中心NB1-03A/NAB1-03C号单元");
66     cJSON_AddNumberToObject(poi1, "lon", 31.111);
67     cJSON_AddNumberToObject(poi1, "lat", 124.111);
68     cJSON_AddStringToObject(poi1, "address", "南京西路街道南京西路1563号");
69     cJSON_AddStringToObject(poi1, "formatAddress", "上海市静安区南京西路街道南京西路1563号静安嘉里中心NB1-03A/NAB1-03C号单元");
70     // 创建第二个 poi 对象
71     cJSON *poi2 = cJSON_CreateObject();
72     cJSON_AddStringToObject(poi2, "id", "4537867");
73     cJSON_AddNumberToObject(poi2, "confidence", 1);
74     cJSON_AddStringToObject(poi2, "name", "静安嘉里中心NB1-03A/NAB1-03C号单元");
75     cJSON_AddNumberToObject(poi2, "lon", 31.111);
76     cJSON_AddNumberToObject(poi2, "lat", 124.111);
77     cJSON_AddStringToObject(poi2, "address", "南京西路街道南京西路1563号");
78     cJSON_AddStringToObject(poi2, "formatAddress", "上海市静安区南京西路街道南京西路1563号静安嘉里中心NB1-03A/NAB1-03C号单元");
79     // 添加 poi 对象到数组中
80     cJSON_AddItemToArray(poisArray, poi1);
81     cJSON_AddItemToArray(poisArray, poi2);
82     // 添加 pois 数组到根对象中
83     cJSON_AddItemToObject(root, "pois", poisArray);
84     // 添加其他字段到根对象
85     cJSON_AddStringToObject(root, "semanticDescription", "Wagas沃歌斯(静安嘉里中心店)附近");
86     cJSON_AddNumberToObject(root, "time", CommonUtils::GetCurrentTimeMilSec() - delayTimeMilSec);
87     // 转换为字符串
88     char *jsonStr = cJSON_PrintUnformatted(root);
89     // 将字符串保存为 std::string,并释放 cJSON 字符串和对象
90     std::string jsonString(jsonStr);
91     cJSON_free(jsonStr);
92     cJSON_Delete(root);
93     return jsonString;
94 }
95 
96 HWTEST_F(PoiInfoManagerTest, PoiInfoManagerTest001, TestSize.Level1)
97 {
98     GTEST_LOG_(INFO)
99         << "PoiInfoManagerTest, PoiInfoManagerTest001, TestSize.Level1";
100     LBSLOGI(REPORT_MANAGER, "[PoiInfoManagerTest] PoiInfoManagerTest001 begin");
101     std::unique_ptr<Location> location = MockLocation();
102     ASSERT_TRUE(poiInfoManager_ != nullptr);
103     poiInfoManager_->UpdateCachedPoiInfo(location);
104     poiInfoManager_->UpdateLocationPoiInfo(location);
105 
106     std::string mockPoiInfo = "poiInfos:" + MockPoiString(0);
107     std::u16string mockPoiInfoStr16 = Str8ToStr16(mockPoiInfo);
108     std::u16string mockStr16 = Str8ToStr16("test1:{empty}");
109     std::vector<std::u16string> mockAdditionsStr16;
110     mockAdditionsStr16.push_back(mockPoiInfoStr16);
111     mockAdditionsStr16.push_back(mockStr16);
112     location->VectorString16ToVectorString8(mockAdditionsStr16);
113 
114     poiInfoManager_->UpdateCachedPoiInfo(location);
115     poiInfoManager_->SetLatestPoiInfoTime(CommonUtils::GetCurrentTimeMilSec());
116     poiInfoManager_->UpdateLocationPoiInfo(location);
117 
118     std::unique_ptr<Location> location1 = MockLocation();
119     poiInfoManager_->UpdateLocationPoiInfo(location1);
120     poiInfoManager_->ClearPoiInfos(location1);
121     LBSLOGI(REPORT_MANAGER, "[PoiInfoManagerTest] PoiInfoManagerTest001 end");
122 }
123 
124 HWTEST_F(PoiInfoManagerTest, PoiInfoManagerTest002, TestSize.Level1)
125 {
126     GTEST_LOG_(INFO)
127         << "PoiInfoManagerTest, PoiInfoManagerTest002, TestSize.Level1";
128     LBSLOGI(REPORT_MANAGER, "[PoiInfoManagerTest] PoiInfoManagerTest002 begin");
129     ASSERT_TRUE(poiInfoManager_ != nullptr);
130     std::unique_ptr<Location> location = MockLocation();
131     poiInfoManager_->UpdateCachedPoiInfo(location);
132     poiInfoManager_->UpdateLocationPoiInfo(location);
133 
134     std::string mockPoiInfo = "poiInfos:" + MockPoiString(60000);
135     std::u16string mockPoiInfoStr16 = Str8ToStr16(mockPoiInfo);
136     std::u16string mockStr16 = Str8ToStr16("test1:{empty}");
137     std::vector<std::u16string> mockAdditionsStr16;
138     mockAdditionsStr16.push_back(mockPoiInfoStr16);
139     mockAdditionsStr16.push_back(mockStr16);
140     location->VectorString16ToVectorString8(mockAdditionsStr16);
141 
142     poiInfoManager_->UpdateCachedPoiInfo(location);
143     poiInfoManager_->SetLatestPoiInfoTime(CommonUtils::GetCurrentTimeMilSec() - 60000);
144     poiInfoManager_->UpdateLocationPoiInfo(location);
145     LBSLOGI(REPORT_MANAGER, "[PoiInfoManagerTest] PoiInfoManagerTest002 end");
146 }
147 
148 }  // namespace Location
149 }  // namespace OHOS