1 /*
2 * Copyright (c) 2024 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("AAFWK", "APP_FOREGROUND", SysEventCreator::BEHAVIOR);
188 sysEventCreator1.SetKeyValue("BUNDLE_NAME", "test_bundle");
189 sysEventCreator1.SetKeyValue("VERSION_NAME", "1");
190 sysEventCreator1.SetKeyValue("time_", 123);
191
192 auto sysEvent1 = std::make_shared<SysEvent>("test", nullptr, sysEventCreator1);
193 cacher.ProcessEvent(sysEvent1);
194
195 sysEventCreator1.SetKeyValue("BUNDLE_NAME", FoldAppUsageEventSpace::SCENEBOARD_BUNDLE_NAME);
196 auto sysEvent2 = std::make_shared<SysEvent>("test", nullptr, sysEventCreator1);
197 cacher.ProcessEvent(sysEvent2);
198
199 FoldAppUsageDbHelper dbHelper("/data/test/");
200 int index1 = dbHelper.QueryRawEventIndex("test_bundle", FoldEventId::EVENT_APP_START);
201 ASSERT_TRUE(index1 != 0);
202
203 SysEventCreator sysEventCreator2("WINDOWMANAGER", "NOTIFY_FOLD_STATE_CHANGE", SysEventCreator::BEHAVIOR);
204 sysEventCreator2.SetKeyValue("CURRENT_FOLD_STATUS", 1);
205 sysEventCreator2.SetKeyValue("NEXT_FOLD_STATUS", 2);
206 sysEventCreator2.SetKeyValue("time_", 333);
207 auto sysEvent3 = std::make_shared<SysEvent>("test", nullptr, sysEventCreator2);
208 cacher.ProcessEvent(sysEvent3);
209 int index2 = dbHelper.QueryRawEventIndex("test_bundle", FoldEventId::EVENT_SCREEN_STATUS_CHANGED);
210 ASSERT_TRUE(index2 != 0);
211
212 SysEventCreator sysEventCreator3("WINDOWMANAGER", "VH_MODE", SysEventCreator::BEHAVIOR);
213 sysEventCreator3.SetKeyValue("MODE", 1);
214 sysEventCreator3.SetKeyValue("time_", 444);
215 auto sysEvent4 = std::make_shared<SysEvent>("test", nullptr, sysEventCreator3);
216 cacher.ProcessEvent(sysEvent4);
217 int index3 = dbHelper.QueryRawEventIndex("test_bundle", FoldEventId::EVENT_SCREEN_STATUS_CHANGED);
218 ASSERT_TRUE(index3 != 0);
219
220 ASSERT_TRUE(index3 != index2);
221 }
222
223 /**
224 * @tc.name: FoldAppUsageTest005
225 * @tc.desc: add app exit event to db.
226 * @tc.type: FUNC
227 */
228 HWTEST_F(FoldAppUsageTest, FoldAppUsageTest005, TestSize.Level1)
229 {
230 SysEventCreator sysEventCreator("WINDOWMANAGER", "NOTIFY_FOLD_STATE_CHANGE", SysEventCreator::BEHAVIOR);
231 sysEventCreator.SetKeyValue("CURRENT_FOLD_STATUS", 0);
232 sysEventCreator.SetKeyValue("NEXT_FOLD_STATUS", 1);
233 sysEventCreator.SetKeyValue("time_", 111);
234
235 auto sysEvent = std::make_shared<SysEvent>("test", nullptr, sysEventCreator);
236 FoldEventCacher cacher("/data/test/");
237 cacher.ProcessEvent(sysEvent);
238
239 SysEventCreator sysEventCreator1("AAFWK", "APP_BACKGROUND", SysEventCreator::BEHAVIOR);
240 sysEventCreator1.SetKeyValue("BUNDLE_NAME", "test_bundle");
241 sysEventCreator1.SetKeyValue("VERSION_NAME", "1");
242 sysEventCreator1.SetKeyValue("time_", 456);
243
244 auto sysEvent1 = std::make_shared<SysEvent>("test", nullptr, sysEventCreator1);
245 cacher.ProcessEvent(sysEvent1);
246
247 sysEventCreator1.SetKeyValue("BUNDLE_NAME", FoldAppUsageEventSpace::SCENEBOARD_BUNDLE_NAME);
248 auto sysEvent2 = std::make_shared<SysEvent>("test", nullptr, sysEventCreator1);
249 cacher.ProcessEvent(sysEvent2);
250
251 FoldAppUsageDbHelper dbHelper("/data/test/");
252 int index = dbHelper.QueryRawEventIndex("test_bundle", FoldEventId::EVENT_APP_EXIT);
253 ASSERT_TRUE(index != 0);
254
255 cacher.TimeOut();
256 index = dbHelper.QueryRawEventIndex("test_bundle", FoldEventId::EVENT_COUNT_DURATION);
257 ASSERT_TRUE(index != 0);
258 }
259
260 /**
261 * @tc.name: FoldAppUsageTest006
262 * @tc.desc: report fold app usage events.
263 * @tc.type: FUNC
264 */
265 HWTEST_F(FoldAppUsageTest, FoldAppUsageTest006, TestSize.Level1)
266 {
267 AppEventRecord record{1104, 1000, "test_bundle", 11, 11, "1", g_endTime, 1, 2, 3, 4};
268 FoldAppUsageDbHelper dbHelper("/data/test/");
269 ASSERT_TRUE(dbHelper.AddAppEvent(record) == 0);
270
271 std::vector<std::unique_ptr<LoggerEvent>> foldAppUsageEvents;
272 FoldAppUsageEventFactory factory("/data/test/");
273 factory.Create(foldAppUsageEvents);
274 ASSERT_TRUE(foldAppUsageEvents.size() != 0);
275
276 for (size_t i = 0; i < foldAppUsageEvents.size(); ++i) {
277 foldAppUsageEvents[i]->Report();
278 }
279 FileUtil::ForceRemoveDirectory("/data/test/sys_event_logger/", true);
280 ASSERT_TRUE(!FileUtil::FileExists("/data/test/sys_event_logger/"));
281 }
282
283 /**
284 * @tc.name: FoldAppUsageTest007
285 * @tc.desc: report fold app usage events.
286 * @tc.type: FUNC
287 */
288 HWTEST_F(FoldAppUsageTest, FoldAppUsageTest007, TestSize.Level1)
289 {
290 EventDbHelper dbHelper("/data/test/sys_event_logger");
291 ASSERT_TRUE(FileUtil::FileExists("/data/test/sys_event_logger/"));
292
293 std::shared_ptr<LoggerEvent> event = nullptr;
294 ASSERT_EQ(dbHelper.InsertPluginStatsEvent(event), -1);
295 ASSERT_EQ(dbHelper.InsertSysUsageEvent(event, SysUsageDbSpace::LAST_SYS_USAGE_TABLE), -1);
296 ASSERT_EQ(dbHelper.QuerySysUsageEvent(event, SysUsageDbSpace::LAST_SYS_USAGE_TABLE), -1);
297 }
298
299 /**
300 * @tc.name: FoldAppUsageTest008
301 * @tc.desc: check json strings.
302 * @tc.type: FUNC
303 */
304 HWTEST_F(FoldAppUsageTest, FoldAppUsageTest008, TestSize.Level1)
305 {
306 Json::Value object;
307 std::string jsonStr = R"~({"param":"str1"})~";
308 JsonParser::ParseJsonString(object, jsonStr);
309 std::vector<std::string> fields;
310 fields.emplace_back("key");
311 ASSERT_FALSE(JsonParser::CheckJsonValue(object, fields));
312 JsonParser::ParseStringVec(object, fields);
313 std::vector<uint32_t> vec;
314 JsonParser::ParseUInt32Vec(object, vec);
315 std::shared_ptr<LoggerEvent> event = nullptr;
316 ASSERT_FALSE(JsonParser::ParsePluginStatsEvent(event, jsonStr));
317 ASSERT_FALSE(JsonParser::ParseSysUsageEvent(event, jsonStr));
318 }
319 } // namespace HiviewDFX
320 } // namespace OHOS
321