• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "event_export_mgr_test.h"
17 
18 #include "event_expire_task.h"
19 #include "event_export_engine.h"
20 #include "event_export_task.h"
21 #include "event_export_util.h"
22 #include "file_util.h"
23 #include "hiview_global.h"
24 #include "setting_observer_manager.h"
25 #include "time_util.h"
26 
27 namespace OHOS {
28 namespace HiviewDFX {
29 namespace {
30 constexpr char PARAM_NAME[] = "PARAM_NAME";
31 constexpr char DEFAULT_VAL[] = "DEFAULT_VAL";
32 constexpr char TEST_CFG_DIR[] = "/data/test/test_data/cfg/";
33 constexpr char TEST_WORK_DIR[] = "/data/test/test_data/work/";
34 constexpr char TEST_CFG_FILE[] = "/data/test/test_data/cfg/sys_event_export/test3_event_export_config.json";
35 constexpr char TEST_MODULE_NAME[] = "test3";
36 
37 class EventExportMgrTestContext : public HiviewContext {
38 public:
GetHiViewDirectory(DirectoryType type)39     std::string GetHiViewDirectory(DirectoryType type)
40     {
41         if (type == HiviewContext::DirectoryType::WORK_DIRECTORY) {
42             return TEST_WORK_DIR;
43         }
44         return TEST_CFG_DIR;
45     }
46 };
47 }
48 
SetUpTestCase()49 void EventExportMgrTest::SetUpTestCase()
50 {
51 }
52 
TearDownTestCase()53 void EventExportMgrTest::TearDownTestCase()
54 {
55 }
56 
SetUp()57 void EventExportMgrTest::SetUp()
58 {
59 }
60 
TearDown()61 void EventExportMgrTest::TearDown()
62 {
63 }
64 
65 /**
66  * @tc.name: EventExportMgrTest001
67  * @tc.desc: Test apis of SettingObserverManager
68  * @tc.type: FUNC
69  * @tc.require: issueI9GHI8
70  */
71 HWTEST_F(EventExportMgrTest, EventExportMgrTest001, testing::ext::TestSize.Level3)
72 {
73     SettingObserver::ObserverCallback callback =
__anon73d050bb0202(const std::string& paramKey) 74         [] (const std::string& paramKey) {
75             // do nothing
76         };
77     // observer not found
78     ASSERT_TRUE(SettingObserverManager::GetInstance()->UnregisterObserver(PARAM_NAME));
79     ASSERT_TRUE(SettingObserverManager::GetInstance()->RegisterObserver(PARAM_NAME, callback));
80     // observer exit
81     ASSERT_TRUE(SettingObserverManager::GetInstance()->RegisterObserver(PARAM_NAME, callback));
82     ASSERT_TRUE(SettingObserverManager::GetInstance()->UnregisterObserver(PARAM_NAME));
83     auto value = SettingObserverManager::GetInstance()->GetStringValue(PARAM_NAME, DEFAULT_VAL);
84     ASSERT_EQ(value, DEFAULT_VAL);
85 }
86 
87 /**
88  * @tc.name: EventExportMgrTest002
89  * @tc.desc: Test apis of EventExportEngine
90  * @tc.type: FUNC
91  * @tc.require: issueICSFYS
92  */
93 HWTEST_F(EventExportMgrTest, EventExportEngine001, testing::ext::TestSize.Level3)
94 {
95     EventExportMgrTestContext context;
96     HiviewGlobal::CreateInstance(context);
97     auto& eventExportEngine = EventExportEngine::GetInstance();
98     eventExportEngine.SetTaskDelayedSecond(1); // delay 1 second
99     eventExportEngine.Start();
100     TimeUtil::Sleep(5); // sleep 5 seconds
101     eventExportEngine.Stop();
102     std::vector<std::string> eventZipFiles;
103     FileUtil::GetDirFiles(TEST_WORK_DIR, eventZipFiles);
104     ASSERT_FALSE(eventZipFiles.empty());
105 }
106 
107 /**
108  * @tc.name: EventExportMgrTest003
109  * @tc.desc: Test apis of EventExpireTask
110  * @tc.type: FUNC
111  * @tc.require: issueICSFYS
112  */
113 HWTEST_F(EventExportMgrTest, EventExportMgrTest003, testing::ext::TestSize.Level3)
114 {
115     EventExpireTask invalidTask(nullptr);
116     invalidTask.Run();
117     ExportConfigParser parser(TEST_CFG_FILE, TEST_MODULE_NAME);
118     auto exportConfig = parser.Parse();
119     ASSERT_NE(exportConfig, nullptr);
120     EventExpireTask validTask(exportConfig);
121     validTask.Run();
122 }
123 
124 /**
125  * @tc.name: EventExportMgrTest004
126  * @tc.desc: Test apis of EventExportTask
127  * @tc.type: FUNC
128  * @tc.require: issueICSFYS
129  */
130 HWTEST_F(EventExportMgrTest, EventExportMgrTest004, testing::ext::TestSize.Level3)
131 {
132     EventExportTask invalidTask(nullptr);
133     invalidTask.Run();
134     ExportConfigParser parser(TEST_CFG_FILE, TEST_MODULE_NAME);
135     auto exportConfig = parser.Parse();
136     ASSERT_NE(exportConfig, nullptr);
137     EventExportTask validTask(exportConfig);
138     validTask.Run();
139 }
140 
141 /**
142  * @tc.name: EventExportUtilTest001
143  * @tc.desc: Test apis of EventExportUtil
144  * @tc.type: FUNC
145  * @tc.require: issueICSFYS
146  */
147 HWTEST_F(EventExportMgrTest, EventExportUtilTest001, testing::ext::TestSize.Level3)
148 {
149     EventExportMgrTestContext context;
150     HiviewGlobal::CreateInstance(context);
151     ASSERT_FALSE(EventExportUtil::CheckAndPostExportEvent(nullptr));
152     ExportConfigParser parser(TEST_CFG_FILE, TEST_MODULE_NAME);
153     auto exportConfig = parser.Parse();
154     ASSERT_NE(exportConfig, nullptr);
155     exportConfig->exportDir = "/data/test/test_data/cfg/sys_event_export/";
156     ASSERT_TRUE(EventExportUtil::CheckAndPostExportEvent(exportConfig));
157     exportConfig->needPostEvent = false;
158     ASSERT_FALSE(EventExportUtil::CheckAndPostExportEvent(exportConfig));
159     exportConfig->needPostEvent = true;
160     exportConfig->exportDir = "/data/test/test_data/no_exist_dir/";
161     ASSERT_FALSE(EventExportUtil::CheckAndPostExportEvent(exportConfig));
162     exportConfig->exportDir = "/data/test/test_data/cfg/sys_event_export/";
163     exportConfig->taskType = ALL_EVENT_TASK_TYPE;
164     ASSERT_TRUE(EventExportUtil::CheckAndPostExportEvent(exportConfig));
165     exportConfig->taskType = 30; // 30 is a test task type
166     ASSERT_TRUE(EventExportUtil::CheckAndPostExportEvent(exportConfig));
167 }
168 } // namespace HiviewDFX
169 } // namespace OHOS