1 /*
2 * Copyright (c) 2024-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 "fold_app_usage_test.h"
17
18 #include "event_db_helper.h"
19 #include "file_util.h"
20 #include "fold_app_usage_db_helper.h"
21 #include "fold_app_usage_event_factory.h"
22 #include "fold_event_cacher.h"
23 #include "json_parser.h"
24 #include "rdb_errno.h"
25 #include "rdb_helper.h"
26 #include "rdb_store.h"
27 #include "sql_util.h"
28 #include "sys_event.h"
29 #include "time_util.h"
30 #include "usage_event_common.h"
31
32 using namespace std;
33 using namespace testing::ext;
34
35 namespace OHOS {
36 namespace HiviewDFX {
37 namespace {
38 const std::string LOG_DB_TABLE_NAME = "app_events";
39
40 int64_t g_today0Time = 0;
41 int64_t g_dayGapTime = 0;
42 int64_t g_hourGapTime = 0;
43 int64_t g_startTime = 0;
44 int64_t g_endTime = 0;
45 int g_screenStat = 0;
46 }
47
SetUpTestCase(void)48 void FoldAppUsageTest::SetUpTestCase(void) {}
49
TearDownTestCase(void)50 void FoldAppUsageTest::TearDownTestCase(void) {}
51
SetUp(void)52 void FoldAppUsageTest::SetUp(void)
53 {
54 if (!FileUtil::FileExists("/data/test/sys_event_logger/") &&
55 !FileUtil::ForceCreateDirectory("/data/test/sys_event_logger/")) {
56 return;
57 }
58
59 g_today0Time = TimeUtil::Get0ClockStampMs();
60 g_dayGapTime = static_cast<int64_t>(TimeUtil::MILLISECS_PER_DAY);
61 g_hourGapTime = static_cast<int64_t>(TimeUtil::SECONDS_PER_HOUR * TimeUtil::SEC_TO_MILLISEC);
62 g_startTime = g_today0Time - g_dayGapTime;
63 g_endTime = g_today0Time - 1;
64 }
65
TearDown(void)66 void FoldAppUsageTest::TearDown(void) {}
67
68 /**
69 * @tc.name: FoldAppUsageTest001
70 * @tc.desc: check fold app usage func get 1104 data from db.
71 * @tc.type: FUNC
72 */
73 HWTEST_F(FoldAppUsageTest, FoldAppUsageTest001, TestSize.Level1)
74 {
75 FoldAppUsageDbHelper dbHelper("/data/test/");
76 AppEventRecord record1{1104, 1000, "app1", 11, 11, "55", g_today0Time - 5 * g_dayGapTime, 1, 2, 3, 4};
77
78 AppEventRecord record2{1104, 1000, "app1", 11, 11, "55", g_startTime - 5 * g_hourGapTime, 1, 2, 3, 4};
79
80 AppEventRecord record3{1104, 1000, "app1", 11, 11, "55", g_startTime + 7 * g_hourGapTime, 1, 2, 3, 4};
81
82 AppEventRecord record4{1104, 1000, "app2", 11, 11, "55", g_startTime + 8 * g_hourGapTime, 2, 2, 2, 2};
83
84 AppEventRecord record5{1104, 1000, "app2", 11, 11, "55", g_startTime + 9 * g_hourGapTime, 3, 3, 3, 3};
85
86 EXPECT_EQ(dbHelper.AddAppEvent(record1), 0);
87 EXPECT_EQ(dbHelper.AddAppEvent(record2), 0);
88 EXPECT_EQ(dbHelper.AddAppEvent(record3), 0);
89 EXPECT_EQ(dbHelper.AddAppEvent(record4), 0);
90 EXPECT_EQ(dbHelper.AddAppEvent(record5), 0);
91
92 std::unordered_map<std::string, FoldAppUsageInfo> all1104Infos;
93 dbHelper.QueryStatisticEventsInPeriod(g_startTime, g_endTime, all1104Infos);
94 EXPECT_EQ(all1104Infos.size(), 2);
95 EXPECT_EQ(all1104Infos["app155"].package, "app1");
96 EXPECT_EQ(all1104Infos["app155"].version, "55");
97 EXPECT_EQ(all1104Infos["app155"].foldVer, 1);
98 EXPECT_EQ(all1104Infos["app155"].foldHor, 2);
99 EXPECT_EQ(all1104Infos["app155"].expdVer, 3);
100 EXPECT_EQ(all1104Infos["app155"].expdHor, 4);
101 EXPECT_EQ(all1104Infos["app255"].package, "app2");
102 EXPECT_EQ(all1104Infos["app255"].version, "55");
103 EXPECT_EQ(all1104Infos["app255"].foldVer, 5);
104 EXPECT_EQ(all1104Infos["app255"].foldHor, 5);
105 EXPECT_EQ(all1104Infos["app255"].expdVer, 5);
106 EXPECT_EQ(all1104Infos["app255"].expdHor, 5);
107 }
108
109 /**
110 * @tc.name: FoldAppUsageTest002
111 * @tc.desc: check fold app usage func get 1103, 1101 data from db.
112 * @tc.type: FUNC
113 */
114 HWTEST_F(FoldAppUsageTest, FoldAppUsageTest002, TestSize.Level1)
115 {
116 FoldAppUsageDbHelper dbHelper("/data/test/");
117 AppEventRecord record6{1101, 4000, "app3", 11, 12, "55", g_startTime + 10 * g_hourGapTime, 0, 0, 0, 0};
118
119 AppEventRecord record7{1103, 5000, "app3", 12, 22, "55", g_startTime + 11 * g_hourGapTime, 1, 2, 3, 4};
120
121 AppEventRecord record8{1103, 6000, "app3", 22, 21, "55", g_startTime + 12 * g_hourGapTime, 0, 0, 0, 0};
122
123 AppEventRecord record9{1103, 7000, "app4", 11, 21, "55", g_startTime + 15 * g_hourGapTime, 0, 0, 0, 0};
124
125 AppEventRecord record10{1103, 8000, "app4", 21, 22, "55", g_startTime + 16 * g_hourGapTime, 0, 0, 0, 0};
126
127 AppEventRecord record11{1102, 9000, "app4", 22, 12, "55", g_today0Time + 2 * g_hourGapTime, 0, 0, 0, 0};
128
129 EXPECT_EQ(dbHelper.AddAppEvent(record6), 0);
130 EXPECT_EQ(dbHelper.AddAppEvent(record7), 0);
131 EXPECT_EQ(dbHelper.AddAppEvent(record8), 0);
132 EXPECT_EQ(dbHelper.AddAppEvent(record9), 0);
133 EXPECT_EQ(dbHelper.AddAppEvent(record10), 0);
134 EXPECT_EQ(dbHelper.AddAppEvent(record11), 0);
135
136 FoldAppUsageInfo app3Info;
137 app3Info.package = "app3";
138 dbHelper.QueryForegroundAppsInfo(g_startTime, g_endTime, g_screenStat, app3Info);
139 EXPECT_EQ(app3Info.version, "");
140 EXPECT_EQ(app3Info.foldVer, 43199999); // from 12:00:00 to 23:59:59
141 EXPECT_EQ(app3Info.foldHor, 1000); // from ts, 5000 to 6000
142 EXPECT_EQ(app3Info.expdVer, 0);
143 EXPECT_EQ(app3Info.expdHor, 1000); // from ts, 4000 to 5000
144 FoldAppUsageInfo app4Info;
145 app4Info.package = "app4";
146 dbHelper.QueryForegroundAppsInfo(g_startTime, g_endTime, g_screenStat, app4Info);
147 EXPECT_EQ(app4Info.version, "");
148 EXPECT_EQ(app4Info.foldVer, 1000); // from ts, 7000 to 8000
149 EXPECT_EQ(app4Info.foldHor, 28799999); // from 16:00:00 to 23:59:59
150 EXPECT_EQ(app4Info.expdVer, 54000000); // from 00:00:00 to 15:00:00
151 EXPECT_EQ(app4Info.expdHor, 0);
152 }
153
154 /**
155 * @tc.name: FoldAppUsageTest003
156 * @tc.desc: check fold app usage func remove data and get creenState from db.
157 * @tc.type: FUNC
158 */
159 HWTEST_F(FoldAppUsageTest, FoldAppUsageTest003, TestSize.Level1)
160 {
161 FoldAppUsageDbHelper dbHelper("/data/test/");
162 g_screenStat = dbHelper.QueryFinalScreenStatus(g_endTime);
163 EXPECT_EQ(g_screenStat, 22);
164
165 int deleteEventNum = dbHelper.DeleteEventsByTime(g_startTime - 2 * g_dayGapTime);
166 EXPECT_EQ(deleteEventNum, 1);
167
168 FileUtil::ForceRemoveDirectory("/data/test/sys_event_logger/", true);
169 }
170
171 /**
172 * @tc.name: FoldAppUsageTest004
173 * @tc.desc: add app start and screen status change events to db.
174 * @tc.type: FUNC
175 */
176 HWTEST_F(FoldAppUsageTest, FoldAppUsageTest004, TestSize.Level1)
177 {
178 SysEventCreator sysEventCreator("WINDOWMANAGER", "NOTIFY_FOLD_STATE_CHANGE", SysEventCreator::BEHAVIOR);
179 sysEventCreator.SetKeyValue("CURRENT_FOLD_STATUS", 0);
180 sysEventCreator.SetKeyValue("NEXT_FOLD_STATUS", 1);
181 sysEventCreator.SetKeyValue("time_", 111);
182
183 auto sysEvent = std::make_shared<SysEvent>("test", nullptr, sysEventCreator);
184 FoldEventCacher cacher("/data/test/");
185 cacher.ProcessEvent(sysEvent);
186
187 SysEventCreator sysEventCreator1("WINDOWMANAGER", "FOCUS_WINDOW", SysEventCreator::BEHAVIOR);
188 sysEventCreator1.SetKeyValue("PID", 1111);
189 sysEventCreator1.SetKeyValue("UID", 20020019);
190 sysEventCreator1.SetKeyValue("BUNDLE_NAME", "test_bundle");
191 sysEventCreator1.SetKeyValue("WINDOW_TYPE", 1);
192 sysEventCreator1.SetKeyValue("time_", 123);
193
194 auto sysEvent1 = std::make_shared<SysEvent>("test", nullptr, sysEventCreator1);
195 cacher.ProcessEvent(sysEvent1);
196
197 FoldAppUsageDbHelper dbHelper("/data/test/");
198 int index1 = dbHelper.QueryRawEventIndex("test_bundle", FoldEventId::EVENT_APP_START);
199 ASSERT_TRUE(index1 != 0);
200
201 SysEventCreator sysEventCreator2("WINDOWMANAGER", "NOTIFY_FOLD_STATE_CHANGE", SysEventCreator::BEHAVIOR);
202 sysEventCreator2.SetKeyValue("CURRENT_FOLD_STATUS", 1);
203 sysEventCreator2.SetKeyValue("NEXT_FOLD_STATUS", 2);
204 sysEventCreator2.SetKeyValue("time_", 333);
205 auto sysEvent2 = std::make_shared<SysEvent>("test", nullptr, sysEventCreator2);
206 cacher.ProcessEvent(sysEvent2);
207 int index2 = dbHelper.QueryRawEventIndex("test_bundle", FoldEventId::EVENT_SCREEN_STATUS_CHANGED);
208 ASSERT_TRUE(index2 != 0);
209
210 SysEventCreator sysEventCreator3("WINDOWMANAGER", "VH_MODE", SysEventCreator::BEHAVIOR);
211 sysEventCreator3.SetKeyValue("MODE", 1);
212 sysEventCreator3.SetKeyValue("time_", 444);
213 auto sysEvent3 = std::make_shared<SysEvent>("test", nullptr, sysEventCreator3);
214 cacher.ProcessEvent(sysEvent3);
215 int index3 = dbHelper.QueryRawEventIndex("test_bundle", FoldEventId::EVENT_SCREEN_STATUS_CHANGED);
216 ASSERT_TRUE(index3 != 0);
217 ASSERT_TRUE(index3 != index2);
218
219 sysEventCreator1.SetKeyValue("BUNDLE_NAME", FoldAppUsageEventSpace::SCENEBOARD_BUNDLE_NAME);
220 sysEventCreator1.SetKeyValue("WINDOW_TYPE", 2001);
221 auto sysEvent4 = std::make_shared<SysEvent>("test", nullptr, sysEventCreator1);
222 cacher.ProcessEvent(sysEvent4);
223 }
224
225 /**
226 * @tc.name: FoldAppUsageTest005
227 * @tc.desc: add app exit event to db.
228 * @tc.type: FUNC
229 */
230 HWTEST_F(FoldAppUsageTest, FoldAppUsageTest005, TestSize.Level1)
231 {
232 SysEventCreator sysEventCreator("WINDOWMANAGER", "NOTIFY_FOLD_STATE_CHANGE", SysEventCreator::BEHAVIOR);
233 sysEventCreator.SetKeyValue("CURRENT_FOLD_STATUS", 0);
234 sysEventCreator.SetKeyValue("NEXT_FOLD_STATUS", 1);
235 sysEventCreator.SetKeyValue("time_", 111);
236
237 auto sysEvent = std::make_shared<SysEvent>("test", nullptr, sysEventCreator);
238 FoldEventCacher cacher("/data/test/");
239 cacher.ProcessEvent(sysEvent);
240
241 SysEventCreator sysEventCreator1("WINDOWMANAGER", "FOCUS_WINDOW", SysEventCreator::BEHAVIOR);
242 sysEventCreator1.SetKeyValue("PID", 2222);
243 sysEventCreator1.SetKeyValue("UID", 20020019);
244 sysEventCreator1.SetKeyValue("BUNDLE_NAME", "test_bundle1");
245 sysEventCreator1.SetKeyValue("WINDOW_TYPE", 1);
246 sysEventCreator1.SetKeyValue("time_", 456);
247
248 auto sysEvent1 = std::make_shared<SysEvent>("test", nullptr, sysEventCreator1);
249 cacher.ProcessEvent(sysEvent1);
250
251 sysEventCreator1.SetKeyValue("BUNDLE_NAME", FoldAppUsageEventSpace::SCENEBOARD_BUNDLE_NAME);
252 sysEventCreator1.SetKeyValue("WINDOW_TYPE", 2001);
253 auto sysEvent2 = std::make_shared<SysEvent>("test", nullptr, sysEventCreator1);
254 cacher.ProcessEvent(sysEvent2);
255
256 FoldAppUsageDbHelper dbHelper("/data/test/");
257 int index = dbHelper.QueryRawEventIndex("test_bundle", FoldEventId::EVENT_APP_EXIT);
258 ASSERT_TRUE(index != 0);
259
260 index = dbHelper.QueryRawEventIndex("test_bundle", FoldEventId::EVENT_COUNT_DURATION);
261 ASSERT_TRUE(index != 0);
262 }
263
264 /**
265 * @tc.name: FoldAppUsageTest006
266 * @tc.desc: report fold app usage events.
267 * @tc.type: FUNC
268 */
269 HWTEST_F(FoldAppUsageTest, FoldAppUsageTest006, TestSize.Level1)
270 {
271 AppEventRecord record{1104, 1000, "test_bundle", 11, 11, "1", g_endTime, 1, 2, 3, 4};
272 FoldAppUsageDbHelper dbHelper("/data/test/");
273 ASSERT_TRUE(dbHelper.AddAppEvent(record) == 0);
274
275 std::vector<std::unique_ptr<LoggerEvent>> foldAppUsageEvents;
276 FoldAppUsageEventFactory factory("/data/test/");
277 factory.Create(foldAppUsageEvents);
278 ASSERT_TRUE(foldAppUsageEvents.size() != 0);
279
280 for (size_t i = 0; i < foldAppUsageEvents.size(); ++i) {
281 foldAppUsageEvents[i]->Report();
282 }
283 FileUtil::ForceRemoveDirectory("/data/test/sys_event_logger/", true);
284 ASSERT_TRUE(!FileUtil::FileExists("/data/test/sys_event_logger/"));
285 }
286
287 /**
288 * @tc.name: FoldAppUsageTest007
289 * @tc.desc: report fold app usage events.
290 * @tc.type: FUNC
291 */
292 HWTEST_F(FoldAppUsageTest, FoldAppUsageTest007, TestSize.Level1)
293 {
294 EventDbHelper dbHelper("/data/test/sys_event_logger");
295 ASSERT_TRUE(FileUtil::FileExists("/data/test/sys_event_logger/"));
296
297 std::shared_ptr<LoggerEvent> event = nullptr;
298 ASSERT_EQ(dbHelper.InsertPluginStatsEvent(event), -1);
299 ASSERT_EQ(dbHelper.InsertSysUsageEvent(event, SysUsageDbSpace::LAST_SYS_USAGE_TABLE), -1);
300 ASSERT_EQ(dbHelper.QuerySysUsageEvent(event, SysUsageDbSpace::LAST_SYS_USAGE_TABLE), -1);
301 }
302
303 /**
304 * @tc.name: FoldAppUsageTest008
305 * @tc.desc: check json strings.
306 * @tc.type: FUNC
307 */
308 HWTEST_F(FoldAppUsageTest, FoldAppUsageTest008, TestSize.Level1)
309 {
310 Json::Value object;
311 std::string jsonStr = R"~({"param":"str1"})~";
312 JsonParser::ParseJsonString(object, jsonStr);
313 std::vector<std::string> fields;
314 fields.emplace_back("key");
315 ASSERT_FALSE(JsonParser::CheckJsonValue(object, fields));
316 JsonParser::ParseStringVec(object, fields);
317 std::vector<uint32_t> vec;
318 JsonParser::ParseUInt32Vec(object, vec);
319 std::shared_ptr<LoggerEvent> event = nullptr;
320 ASSERT_FALSE(JsonParser::ParsePluginStatsEvent(event, jsonStr));
321 ASSERT_FALSE(JsonParser::ParseSysUsageEvent(event, jsonStr));
322 }
323 } // namespace HiviewDFX
324 } // namespace OHOS
325