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 #include <iostream>
16
17 #include <gtest/gtest.h>
18
19 #include "file_util.h"
20 #include "hiappevent_base.h"
21 #include "hiappevent_config.h"
22 #include "hiappevent_read.h"
23 #include "hiappevent_write.h"
24 #include "time_util.h"
25
26 using namespace testing::ext;
27 using namespace OHOS::HiviewDFX;
28
29 namespace {
30 const std::string TEST_NAME = "test_name";
31 constexpr unsigned int TEST_TYPE = 1;
32 const std::string TEST_PATH = "/data/test/hiappevent/";
33 const std::string TEST_EVENT = R"~({"domain_":"hiappevent", "name_":"testEvent"})~";
34 constexpr int64_t INVALID_TIME = -1;
35 constexpr int64_t INVALID_COUNT = -1;
36 }
37
38 class HiAppEventReadTest : public testing::Test {
39 public:
40 void SetUp();
41 void TearDown();
42 };
43
SetUp()44 void HiAppEventReadTest::SetUp()
45 {}
46
TearDown()47 void HiAppEventReadTest::TearDown()
48 {}
49
50 /**
51 * @tc.name: HiAppEventReadTest001
52 * @tc.desc: Check the real time log update function.
53 * @tc.type: FUNC
54 * @tc.require: issueI621G6
55 */
56 HWTEST_F(HiAppEventReadTest, HiAppEventReadTest001, TestSize.Level3)
57 {
58 /**
59 * @tc.steps: step1. Regist an event log real time listener.
60 * @tc.steps: step2. Update an event.
61 * @tc.steps: step3. Remove event log listeners.
62 */
63 std::cout << "HiAppEventReadTest001 start" << std::endl;
64
__anon65565eb60202(const std::string& log) 65 auto testListener = [](const std::string& log) {
66 std::cout << log << std::endl;
67 ASSERT_EQ(log, TEST_EVENT);
68 };
69 RegRealTimeAppLogListener(testListener);
70 RealTimeAppLogUpdate(TEST_EVENT);
71 RemoveAllListeners();
72
73 std::cout << "HiAppEventReadTest001 end" << std::endl;
74 }
75
76 /**
77 * @tc.name: HiAppEventReadTest002
78 * @tc.desc: Check the history log update function.
79 * @tc.type: FUNC
80 * @tc.require: issueI621G6
81 */
82 HWTEST_F(HiAppEventReadTest, HiAppEventReadTest002, TestSize.Level3)
83 {
84 /**
85 * @tc.steps: step1. Regist an event log history listener.
86 * @tc.steps: step2. Persist a self-constructed event log to local file.
87 * @tc.steps: step3. Remove event log listeners.
88 */
89 std::cout << "HiAppEventReadTest002 start" << std::endl;
90
__anon65565eb60302(const std::vector<std::string>& logs) 91 auto testListener = [](const std::vector<std::string>& logs) {
92 std::for_each(logs.cbegin(), logs.cend(), [](const std::string& log) {
93 std::cout << log << std::endl;
94 });
95 ASSERT_GT(logs.size(), 0);
96 };
97 RegHistoryAppLogListener(testListener);
98
99 HiAppEventConfig::GetInstance().SetStorageDir(TEST_PATH);
100 UpdateHiAppEventLogDir("/data/test");
101 WriteEvent(std::make_shared<AppEventPack>(TEST_NAME, TEST_TYPE));
102
103 // read all files
104 PullEventHistoryLog(INVALID_TIME, INVALID_TIME, INVALID_COUNT);
105
106 // read files up to current time
107 int64_t curTime = static_cast<int64_t>(TimeUtil::GetMilliseconds());
108 constexpr int eventCount = 1;
109 PullEventHistoryLog(INVALID_TIME, curTime, eventCount);
110
111 // read files greater than 0 but less than current time
112 PullEventHistoryLog(0, curTime, eventCount);
113
114 // read files greater than 0
115 PullEventHistoryLog(0, INVALID_TIME, eventCount);
116
117 RemoveAllListeners();
118 (void)FileUtil::ForceRemoveDirectory(TEST_PATH, true);
119 std::cout << "HiAppEventReadTest002 end" << std::endl;
120 }
121