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 #define private public
17 #define protected public
18 #include "ima_hisysevent_reporter.h"
19 #include "imc_hisysevent_reporter.h"
20 #include "imf_hisysevent_info.h"
21 #include "imf_hisysevent_reporter.h"
22 #include "imf_hisysevent_util.h"
23 #include "imsa_hisysevent_reporter.h"
24 #undef private
25
26 #include <gtest/gtest.h>
27 #include <gtest/hwext/gtest-multithread.h>
28 #include <sys/time.h>
29 #include <unistd.h>
30
31 #include <condition_variable>
32 #include <cstdint>
33 #include <sstream>
34 #include <string>
35
36 #include "global.h"
37 #include "hisysevent_base_manager.h"
38 #include "hisysevent_listener.h"
39 #include "hisysevent_manager.h"
40 #include "hisysevent_query_callback.h"
41 #include "hisysevent_record.h"
42
43 namespace OHOS {
44 namespace MiscServices {
45 using namespace testing::ext;
46 using namespace testing::mt;
47 using namespace OHOS::HiviewDFX;
48 constexpr const char *EVENT_NAME_CLIENT_ATTACH_FAILED = GET_NAME(CLIENT_ATTACH_FAILED);
49 constexpr const char *EVENT_NAME_CLIENT_SHOW_FAILED = GET_NAME(CLIENT_SHOW_FAILED);
50 constexpr const char *EVENT_NAME_IME_START_INPUT_FAILED = GET_NAME(IME_START_INPUT_FAILED);
51 constexpr const char *EVENT_NAME_BASIC_TEXT_OPERATION_FAILED = GET_NAME(BASE_TEXT_OPERATION_FAILED);
52 constexpr const char *EVENT_NAME_CLIENT_ATTACH_STATISTICS = GET_NAME(CLIENT_ATTACH_STATISTICS);
53 constexpr const char *EVENT_NAME_CLIENT_SHOW_STATISTICS = GET_NAME(CLIENT_SHOW_STATISTICS);
54 constexpr const char *EVENT_NAME_IME_START_INPUT_STATISTICS = GET_NAME(IME_START_INPUT_STATISTICS);
55 constexpr const char *EVENT_NAME_BASIC_TEXT_OPERATION_STATISTICS = GET_NAME(BASE_TEXT_OPERATION_STATISTICS);
56 constexpr const char *DOMAIN = "INPUTMETHOD";
57 constexpr const char *PARAM_PEER_NAME = "app";
58 constexpr const char *PARAM_PEER_NAME1 = "app1";
59 constexpr int32_t PARAM_PEER_USERID = 100;
60 constexpr int64_t PARAM_PEER_PID = 4545;
61 constexpr ClientType PARAM_CLIENT_TYPE = ClientType::INNER_KIT;
62 constexpr int32_t PARAM_INPUT_PATTERN = 6;
63 constexpr bool PARAM_ISSHOWKEYBOARD = true;
64 constexpr const char *PARAM_IME_NAME = "ime";
65 constexpr const char *PARAM_IME_NAME1 = "ime1";
66 constexpr int32_t PARAM_EVENT_CODE = 4;
67 constexpr uint32_t WATI_HISYSEVENT_TIMEOUT = 300; //ms
68 struct BaseStatistics : public Serializable {
69 int32_t count{ 0 };
70 std::vector<std::vector<std::string>> info;
operator ==OHOS::MiscServices::BaseStatistics71 bool operator==(const BaseStatistics &baseInfo) const
72 {
73 return count == baseInfo.count && info == baseInfo.info;
74 }
UnmarshalOHOS::MiscServices::BaseStatistics75 bool Unmarshal(cJSON *node) override
76 {
77 auto ret = GetValue(node, GET_NAME(COUNT), count);
78 return GetValue(node, GET_NAME(COUNT_DISTRIBUTION), info) && ret;
79 }
80 };
81
82 struct ClientAttachStatistics : public BaseStatistics {
83 std::vector<std::string> appNames;
84 std::vector<std::string> imeNames;
85 BaseStatistics succeedInfo;
86 BaseStatistics failedInfo;
operator ==OHOS::MiscServices::ClientAttachStatistics87 bool operator==(const ClientAttachStatistics &info) const
88 {
89 return (imeNames == info.imeNames && appNames == info.appNames && succeedInfo == info.succeedInfo
90 && failedInfo == info.failedInfo);
91 }
UnmarshalOHOS::MiscServices::ClientAttachStatistics92 bool Unmarshal(cJSON *node) override
93 {
94 auto ret = GetValue(node, GET_NAME(SUCCEED), succeedInfo);
95 return GetValue(node, GET_NAME(FAILED), failedInfo) && ret;
96 }
97 };
98
99 struct ClientShowStatistics : public BaseStatistics {
100 std::vector<std::string> appNames;
101 std::vector<std::string> imeNames;
102 BaseStatistics succeedInfo;
103 BaseStatistics failedInfo;
operator ==OHOS::MiscServices::ClientShowStatistics104 bool operator==(const ClientShowStatistics &info) const
105 {
106 return (imeNames == info.imeNames && appNames == info.appNames && succeedInfo == info.succeedInfo
107 && failedInfo == info.failedInfo);
108 }
UnmarshalOHOS::MiscServices::ClientShowStatistics109 bool Unmarshal(cJSON *node) override
110 {
111 auto ret = GetValue(node, GET_NAME(SUCCEED), succeedInfo);
112 return GetValue(node, GET_NAME(FAILED), failedInfo) && ret;
113 }
114 };
115
116 struct CbTimeConsume : public Serializable {
117 int32_t count{ 0 };
118 std::string timeConsumeStatistics;
operator ==OHOS::MiscServices::CbTimeConsume119 bool operator==(const CbTimeConsume &info) const
120 {
121 return (count == info.count && timeConsumeStatistics == info.timeConsumeStatistics);
122 }
UnmarshalOHOS::MiscServices::CbTimeConsume123 bool Unmarshal(cJSON *node) override
124 {
125 auto ret = GetValue(node, GET_NAME(COUNT), count);
126 return GetValue(node, GET_NAME(COUNT_DISTRIBUTION), timeConsumeStatistics) && ret;
127 }
128 };
129
130 struct ImeStartInputStatistics : public BaseStatistics {
131 std::vector<std::string> appNames;
132 BaseStatistics succeedInfo;
133 BaseStatistics failedInfo;
134 CbTimeConsume cbTimeConsume;
operator ==OHOS::MiscServices::ImeStartInputStatistics135 bool operator==(const ImeStartInputStatistics &info) const
136 {
137 return (appNames == info.appNames && succeedInfo == info.succeedInfo && failedInfo == info.failedInfo
138 && cbTimeConsume == info.cbTimeConsume);
139 }
UnmarshalOHOS::MiscServices::ImeStartInputStatistics140 bool Unmarshal(cJSON *node) override
141 {
142 auto ret = GetValue(node, GET_NAME(SUCCEED), succeedInfo);
143 ret = GetValue(node, GET_NAME(FAILED), failedInfo) && ret;
144 return GetValue(node, GET_NAME(CB_TIME_CONSUME), cbTimeConsume) && ret;
145 }
146 };
147
148 struct BaseTextOperationStatistics : public BaseStatistics {
149 std::vector<std::string> appNames;
150 BaseStatistics succeedInfo;
151 BaseStatistics failedInfo;
operator ==OHOS::MiscServices::BaseTextOperationStatistics152 bool operator==(const BaseTextOperationStatistics &info) const
153 {
154 return (appNames == info.appNames && succeedInfo == info.succeedInfo && failedInfo == info.failedInfo);
155 }
UnmarshalOHOS::MiscServices::BaseTextOperationStatistics156 bool Unmarshal(cJSON *node) override
157 {
158 auto ret = GetValue(node, GET_NAME(SUCCEED), succeedInfo);
159 return GetValue(node, GET_NAME(FAILED), failedInfo) && ret;
160 }
161 };
162
163 class ImfHiSysEventReporterTest : public testing::Test {
164 public:
165 static constexpr int32_t THREAD_NUM = 5;
166 static constexpr int32_t EACH_THREAD_CIRCULATION_TIME = 100;
167 static void SetUpTestCase(void);
168 static void TearDownTestCase(void);
169 void SetUp();
170 void TearDown();
171 static bool WaitFaultEvent(const std::string &eventName, const HiSysOriginalInfo &info);
172 static bool WaitClientAttachStatistic(const ClientAttachStatistics &statistic);
173 static bool WaitClientShowStatistic(const ClientShowStatistics &statistic);
174 static bool WaitImeStartInputStatistic(const ImeStartInputStatistics &statistic);
175 static bool WaitBaseTextOperatorStatistic(const BaseTextOperationStatistics &statistic);
176 static void TestStatisticsEventConcurrent();
177 static void TestClientAttach(const uint8_t *data, size_t size);
178 static void TestClientShow(const uint8_t *data, size_t size);
179 static void TestStartInput(const uint8_t *data, size_t size);
180 static void TestBaseTextOperation(const uint8_t *data, size_t size);
181 static std::mutex cvMutex_;
182 static std::condition_variable watcherCv_;
183 static std::string eventName_;
184 static std::shared_ptr<HiSysOriginalInfo> faultInfo_;
185 static ClientAttachStatistics clientAttachStatistics_;
186 static ClientShowStatistics clientShowStatistics_;
187 static ImeStartInputStatistics imeStartInputStatistics_;
188 static BaseTextOperationStatistics baseTextOperationStatistics_;
189 static int32_t multiThreadExecTotalNum_;
190
191 private:
192 static void WatchHiSysEvent();
193 static void AddListenerRules(const std::string &eventName, std::vector<ListenerRule> &sysRules);
194 };
195
196 class ImfHiSysEventWatcher : public HiSysEventListener {
197 public:
~ImfHiSysEventWatcher()198 virtual ~ImfHiSysEventWatcher()
199 {
200 }
OnEvent(std::shared_ptr<HiSysEventRecord> sysEvent)201 virtual void OnEvent(std::shared_ptr<HiSysEventRecord> sysEvent) final
202 {
203 std::unique_lock<std::mutex> lock(ImfHiSysEventReporterTest::cvMutex_);
204 if (sysEvent == nullptr) {
205 IMSA_HILOGE("sysEvent is nullptr.");
206 return;
207 }
208 ImfHiSysEventReporterTest::eventName_ = sysEvent->GetEventName();
209 IMSA_HILOGI("HiSysEventWatcher::OnEvent:%{public}s", ImfHiSysEventReporterTest::eventName_.c_str());
210 auto it = TEST_EVENT_MAPPING.find(ImfHiSysEventReporterTest::eventName_);
211 if (it == TEST_EVENT_MAPPING.end()) {
212 return;
213 }
214 if (it->second != nullptr) {
215 it->second(sysEvent);
216 }
217 ImfHiSysEventReporterTest::watcherCv_.notify_one();
218 }
OnServiceDied()219 virtual void OnServiceDied() final
220 {
221 IMSA_HILOGI("HiSysEventWatcher::OnServiceDied");
222 }
223 static void NotifyFaultEvent(std::shared_ptr<HiSysEventRecord> sysEvent);
224 static void NotifyClientAttachStatistic(std::shared_ptr<HiSysEventRecord> sysEvent);
225 static void NotifyClientShowStatistic(std::shared_ptr<HiSysEventRecord> sysEvent);
226 static void NotifyImeStartInputStatistic(std::shared_ptr<HiSysEventRecord> sysEvent);
227 static void NotifyBaseTextOperatorStatistic(std::shared_ptr<HiSysEventRecord> sysEvent);
228
229 private:
230 using EventHandler = void (*)(std::shared_ptr<HiSysEventRecord> sysEvent);
231 const std::unordered_map<std::string, EventHandler> TEST_EVENT_MAPPING = {
232 { EVENT_NAME_CLIENT_ATTACH_FAILED, NotifyFaultEvent },
233 { EVENT_NAME_CLIENT_SHOW_FAILED, NotifyFaultEvent },
234 { EVENT_NAME_IME_START_INPUT_FAILED, NotifyFaultEvent },
235 { EVENT_NAME_BASIC_TEXT_OPERATION_FAILED, NotifyFaultEvent },
236 { EVENT_NAME_CLIENT_ATTACH_STATISTICS, NotifyClientAttachStatistic },
237 { EVENT_NAME_CLIENT_SHOW_STATISTICS, NotifyClientShowStatistic },
238 { EVENT_NAME_IME_START_INPUT_STATISTICS, NotifyImeStartInputStatistic },
239 { EVENT_NAME_BASIC_TEXT_OPERATION_STATISTICS, NotifyBaseTextOperatorStatistic },
240 };
241 };
242
SetUpTestCase(void)243 void ImfHiSysEventReporterTest::SetUpTestCase(void)
244 {
245 IMSA_HILOGI("ImfHiSysEventReporterTest::SetUpTestCase");
246 WatchHiSysEvent();
247 }
248
TearDownTestCase(void)249 void ImfHiSysEventReporterTest::TearDownTestCase(void)
250 {
251 IMSA_HILOGI("ImfHiSysEventReporterTest::TearDownTestCase");
252 }
253
SetUp(void)254 void ImfHiSysEventReporterTest::SetUp(void)
255 {
256 IMSA_HILOGI("ImfHiSysEventReporterTest::SetUp");
257 ImfHiSysEventReporterTest::faultInfo_ = HiSysOriginalInfo::Builder().Build();
258 eventName_.clear();
259 clientAttachStatistics_ = ClientAttachStatistics();
260 clientShowStatistics_ = ClientShowStatistics();
261 imeStartInputStatistics_ = ImeStartInputStatistics();
262 baseTextOperationStatistics_ = BaseTextOperationStatistics();
263 ImsaHiSysEventReporter::GetInstance().clientAttachInfo_ = ClientAttachAllInfo(
264 ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM, ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
265 ImsaHiSysEventReporter::GetInstance().clientShowInfo_ = ClientShowAllInfo(
266 ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM, ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
267 ImaHiSysEventReporter::GetInstance().imeStartInputAllInfo_ =
268 ImeStartInputAllInfo(ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM,
269 ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM, ImaHiSysEventReporter::IME_CB_TIME_INTERVAL.size());
270 ImaHiSysEventReporter::GetInstance().baseTextOperationAllInfo_ =
271 BaseTextOperationAllInfo(ImaHiSysEventReporter::BASE_TEXT_OPERATION_TIME_INTERVAL.size(),
272 ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
273 }
274
TearDown(void)275 void ImfHiSysEventReporterTest::TearDown(void)
276 {
277 IMSA_HILOGI("ImfHiSysEventReporterTest::TearDown");
278 }
WatchHiSysEvent()279 void ImfHiSysEventReporterTest::WatchHiSysEvent()
280 {
281 std::vector<ListenerRule> sysRules;
282 AddListenerRules(EVENT_NAME_CLIENT_ATTACH_FAILED, sysRules);
283 AddListenerRules(EVENT_NAME_CLIENT_SHOW_FAILED, sysRules);
284 AddListenerRules(EVENT_NAME_IME_START_INPUT_FAILED, sysRules);
285 AddListenerRules(EVENT_NAME_BASIC_TEXT_OPERATION_FAILED, sysRules);
286 AddListenerRules(EVENT_NAME_CLIENT_ATTACH_STATISTICS, sysRules);
287 AddListenerRules(EVENT_NAME_CLIENT_SHOW_STATISTICS, sysRules);
288 AddListenerRules(EVENT_NAME_IME_START_INPUT_STATISTICS, sysRules);
289 AddListenerRules(EVENT_NAME_BASIC_TEXT_OPERATION_STATISTICS, sysRules);
290 AddListenerRules(DOMAIN, sysRules);
291 std::shared_ptr<HiSysEventListener> watcher = std::make_shared<ImfHiSysEventWatcher>();
292 auto ret = HiSysEventManager::AddListener(watcher, sysRules);
293 if (ret != SUCCESS) {
294 IMSA_HILOGE("AddListener failed! ret = %{public}d", ret);
295 }
296 }
297
AddListenerRules(const std::string & eventName,std::vector<ListenerRule> & sysRules)298 void ImfHiSysEventReporterTest::AddListenerRules(const std::string &eventName, std::vector<ListenerRule> &sysRules)
299 {
300 ListenerRule listenerRule(DOMAIN, eventName, "", OHOS::HiviewDFX::RuleType::WHOLE_WORD);
301 sysRules.emplace_back(listenerRule);
302 }
303
NotifyFaultEvent(std::shared_ptr<HiSysEventRecord> sysEvent)304 void ImfHiSysEventWatcher::NotifyFaultEvent(std::shared_ptr<HiSysEventRecord> sysEvent)
305 {
306 if (sysEvent == nullptr) {
307 IMSA_HILOGE("sysEvent is nullptr.");
308 return;
309 }
310 sysEvent->GetParamValue(GET_NAME(PEER_NAME), ImfHiSysEventReporterTest::faultInfo_->peerName);
311 int64_t param = 0;
312 sysEvent->GetParamValue(GET_NAME(PEER_USERID), param);
313 ImfHiSysEventReporterTest::faultInfo_->peerUserId = param;
314 sysEvent->GetParamValue(GET_NAME(PEER_PID), ImfHiSysEventReporterTest::faultInfo_->peerPid);
315 param = CLIENT_TYPE_END;
316 sysEvent->GetParamValue(GET_NAME(CLIENT_TYPE), param);
317 ImfHiSysEventReporterTest::faultInfo_->clientType = static_cast<ClientType>(param);
318 param = 0;
319 sysEvent->GetParamValue(GET_NAME(INPUT_PATTERN), param);
320 ImfHiSysEventReporterTest::faultInfo_->inputPattern = param;
321 param = 1;
322 sysEvent->GetParamValue(GET_NAME(ISSHOWKEYBOARD), param);
323 ImfHiSysEventReporterTest::faultInfo_->isShowKeyboard = param;
324 sysEvent->GetParamValue(GET_NAME(IME_NAME), ImfHiSysEventReporterTest::faultInfo_->imeName);
325 param = 0;
326 sysEvent->GetParamValue(GET_NAME(ERR_CODE), param);
327 ImfHiSysEventReporterTest::faultInfo_->errCode = param;
328 param = 0;
329 sysEvent->GetParamValue(GET_NAME(EVENT_CODE), param);
330 ImfHiSysEventReporterTest::faultInfo_->eventCode = param;
331 }
NotifyClientAttachStatistic(std::shared_ptr<HiSysEventRecord> sysEvent)332 void ImfHiSysEventWatcher::NotifyClientAttachStatistic(std::shared_ptr<HiSysEventRecord> sysEvent)
333 {
334 if (sysEvent == nullptr) {
335 IMSA_HILOGE("sysEvent is nullptr.");
336 return;
337 }
338 sysEvent->GetParamValue(GET_NAME(IME_NAME), ImfHiSysEventReporterTest::clientAttachStatistics_.imeNames);
339 sysEvent->GetParamValue(GET_NAME(APP_NAME), ImfHiSysEventReporterTest::clientAttachStatistics_.appNames);
340 std::vector<std::string> infos;
341 sysEvent->GetParamValue(GET_NAME(INFOS), infos);
342 if (infos.size() != 1) {
343 IMSA_HILOGE("infos size is %{public}zu.", infos.size());
344 return;
345 }
346 ImfHiSysEventReporterTest::clientAttachStatistics_.Unmarshall(infos[0]);
347 }
NotifyClientShowStatistic(std::shared_ptr<HiSysEventRecord> sysEvent)348 void ImfHiSysEventWatcher::NotifyClientShowStatistic(std::shared_ptr<HiSysEventRecord> sysEvent)
349 {
350 if (sysEvent == nullptr) {
351 IMSA_HILOGE("sysEvent is nullptr.");
352 return;
353 }
354 sysEvent->GetParamValue(GET_NAME(IME_NAME), ImfHiSysEventReporterTest::clientShowStatistics_.imeNames);
355 sysEvent->GetParamValue(GET_NAME(APP_NAME), ImfHiSysEventReporterTest::clientShowStatistics_.appNames);
356 std::vector<std::string> infos;
357 sysEvent->GetParamValue(GET_NAME(INFOS), infos);
358 if (infos.size() != 1) {
359 IMSA_HILOGE("infos size is %{public}zu.", infos.size());
360 return;
361 }
362 ImfHiSysEventReporterTest::clientShowStatistics_.Unmarshall(infos[0]);
363 }
364
NotifyImeStartInputStatistic(std::shared_ptr<HiSysEventRecord> sysEvent)365 void ImfHiSysEventWatcher::NotifyImeStartInputStatistic(std::shared_ptr<HiSysEventRecord> sysEvent)
366 {
367 if (sysEvent == nullptr) {
368 IMSA_HILOGE("sysEvent is nullptr.");
369 return;
370 }
371 sysEvent->GetParamValue(GET_NAME(APP_NAME), ImfHiSysEventReporterTest::imeStartInputStatistics_.appNames);
372 std::vector<std::string> infos;
373 sysEvent->GetParamValue(GET_NAME(INFOS), infos);
374 if (infos.size() != 1) {
375 IMSA_HILOGE("infos size is %{public}zu.", infos.size());
376 return;
377 }
378 ImfHiSysEventReporterTest::imeStartInputStatistics_.Unmarshall(infos[0]);
379 }
NotifyBaseTextOperatorStatistic(std::shared_ptr<HiSysEventRecord> sysEvent)380 void ImfHiSysEventWatcher::NotifyBaseTextOperatorStatistic(std::shared_ptr<HiSysEventRecord> sysEvent)
381 {
382 if (sysEvent == nullptr) {
383 IMSA_HILOGE("sysEvent is nullptr.");
384 return;
385 }
386 sysEvent->GetParamValue(GET_NAME(APP_NAME), ImfHiSysEventReporterTest::baseTextOperationStatistics_.appNames);
387 std::vector<std::string> infos;
388 sysEvent->GetParamValue(GET_NAME(INFOS), infos);
389 if (infos.size() != 1) {
390 IMSA_HILOGE("infos size is %{public}zu.", infos.size());
391 return;
392 }
393 ImfHiSysEventReporterTest::baseTextOperationStatistics_.Unmarshall(infos[0]);
394 }
395
WaitFaultEvent(const std::string & eventName,const HiSysOriginalInfo & info)396 bool ImfHiSysEventReporterTest::WaitFaultEvent(const std::string &eventName, const HiSysOriginalInfo &info)
397 {
398 std::unique_lock<std::mutex> lock(cvMutex_);
399 watcherCv_.wait_for(lock, std::chrono::milliseconds(WATI_HISYSEVENT_TIMEOUT),
400 [&eventName, &info]() { return eventName_ == eventName && info == *faultInfo_; });
401 IMSA_HILOGI("notify param:[%{public}d, %{public}d, %{public}s, %{public}d, %{public}d, "
402 "%{public}d, "
403 "%{public}d, %{public}s, %{public}d, %{public}d]",
404 faultInfo_->eventCode, faultInfo_->errCode, faultInfo_->peerName.c_str(), faultInfo_->peerUserId,
405 faultInfo_->clientType, faultInfo_->inputPattern, faultInfo_->isShowKeyboard, faultInfo_->imeName.c_str(),
406 faultInfo_->imeCbTime, faultInfo_->baseTextOperationTime);
407 IMSA_HILOGI("expect param:[%{public}d, %{public}d, %{public}s, %{public}d, %{public}d, "
408 "%{public}d, "
409 "%{public}d, %{public}s, %{public}d, %{public}d]",
410 info.eventCode, info.errCode, info.peerName.c_str(), info.peerUserId, info.clientType, info.inputPattern,
411 info.isShowKeyboard, info.imeName.c_str(), info.imeCbTime, info.baseTextOperationTime);
412 return eventName_ == eventName && info == *faultInfo_;
413 }
414
WaitClientAttachStatistic(const ClientAttachStatistics & statistic)415 bool ImfHiSysEventReporterTest::WaitClientAttachStatistic(const ClientAttachStatistics &statistic)
416 {
417 std::unique_lock<std::mutex> lock(cvMutex_);
418 watcherCv_.wait_for(lock, std::chrono::milliseconds(WATI_HISYSEVENT_TIMEOUT),
419 [&statistic]() { return clientAttachStatistics_ == statistic; });
420 return clientAttachStatistics_ == statistic;
421 }
WaitClientShowStatistic(const ClientShowStatistics & statistic)422 bool ImfHiSysEventReporterTest::WaitClientShowStatistic(const ClientShowStatistics &statistic)
423 {
424 std::unique_lock<std::mutex> lock(cvMutex_);
425 watcherCv_.wait_for(lock, std::chrono::milliseconds(WATI_HISYSEVENT_TIMEOUT),
426 [&statistic]() { return clientShowStatistics_ == statistic; });
427 return clientShowStatistics_ == statistic;
428 }
WaitImeStartInputStatistic(const ImeStartInputStatistics & statistic)429 bool ImfHiSysEventReporterTest::WaitImeStartInputStatistic(const ImeStartInputStatistics &statistic)
430 {
431 std::unique_lock<std::mutex> lock(cvMutex_);
432 watcherCv_.wait_for(lock, std::chrono::milliseconds(WATI_HISYSEVENT_TIMEOUT),
433 [&statistic]() { return imeStartInputStatistics_ == statistic; });
434 return imeStartInputStatistics_ == statistic;
435 }
WaitBaseTextOperatorStatistic(const BaseTextOperationStatistics & statistic)436 bool ImfHiSysEventReporterTest::WaitBaseTextOperatorStatistic(const BaseTextOperationStatistics &statistic)
437 {
438 std::unique_lock<std::mutex> lock(cvMutex_);
439 watcherCv_.wait_for(lock, std::chrono::milliseconds(WATI_HISYSEVENT_TIMEOUT),
440 [&statistic]() { return baseTextOperationStatistics_ == statistic; });
441 return baseTextOperationStatistics_ == statistic;
442 }
443
TestClientAttach(const uint8_t * data,size_t size)444 void ImfHiSysEventReporterTest::TestClientAttach(const uint8_t *data, size_t size)
445 {
446 if (data == nullptr || size == 0) {
447 return;
448 }
449 std::string testString(reinterpret_cast<const char *>(data), size);
450 auto paramPeerName = testString;
451 auto paramPeerUserId = static_cast<int32_t>(size);
452 auto paramPeerPid = static_cast<int64_t>(size);
453 auto errCode = static_cast<int32_t>(size);
454 auto paramIsShowkeyboard = static_cast<bool>(data[0] % 2); // remainder 2 can generate a random number of 0 or 1
455 auto paramImeName = testString;
456 auto info = HiSysOriginalInfo::Builder()
457 .SetPeerName(paramPeerName)
458 .SetPeerPid(paramPeerPid)
459 .SetPeerUserId(paramPeerUserId)
460 .SetClientType(PARAM_CLIENT_TYPE)
461 .SetInputPattern(PARAM_INPUT_PATTERN)
462 .SetIsShowKeyboard(paramIsShowkeyboard)
463 .SetImeName(paramImeName)
464 .SetErrCode(errCode)
465 .Build();
466 ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_ATTACH, *info);
467 }
468
TestClientShow(const uint8_t * data,size_t size)469 void ImfHiSysEventReporterTest::TestClientShow(const uint8_t *data, size_t size)
470 {
471 if (data == nullptr || size == 0) {
472 return;
473 }
474 std::string testString(reinterpret_cast<const char *>(data), size);
475 auto paramPeerName = testString;
476 auto paramPeerUserId = static_cast<int32_t>(size);
477 auto paramPeerPid = static_cast<int64_t>(size);
478 auto errCode = static_cast<int32_t>(size);
479 auto paramIEventCode = static_cast<int32_t>(size);
480 auto paramImeName = testString;
481 auto info = HiSysOriginalInfo::Builder()
482 .SetPeerName(paramPeerName)
483 .SetPeerPid(paramPeerPid)
484 .SetPeerUserId(paramPeerUserId)
485 .SetClientType(PARAM_CLIENT_TYPE)
486 .SetImeName(paramImeName)
487 .SetEventCode(paramIEventCode)
488 .SetErrCode(errCode)
489 .Build();
490 ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_SHOW, *info);
491 }
492
TestStartInput(const uint8_t * data,size_t size)493 void ImfHiSysEventReporterTest::TestStartInput(const uint8_t *data, size_t size)
494 {
495 if (data == nullptr || size == 0) {
496 return;
497 }
498 std::string testString(reinterpret_cast<const char *>(data), size);
499 auto paramPeerName = testString;
500 auto paramPeerPid = static_cast<int64_t>(size);
501 auto errCode = static_cast<int32_t>(size);
502 auto paramIsShowkeyboard = static_cast<bool>(data[0] % 2); // remainder 2 can generate a random number of 0 or 1
503 auto paramIEventCode = static_cast<int32_t>(size);
504 auto info = HiSysOriginalInfo::Builder()
505 .SetPeerName(paramPeerName)
506 .SetPeerPid(paramPeerPid)
507 .SetIsShowKeyboard(paramIsShowkeyboard)
508 .SetEventCode(paramIEventCode)
509 .SetErrCode(errCode)
510 .Build();
511 ImaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::IME_START_INPUT, *info);
512 }
513
TestBaseTextOperation(const uint8_t * data,size_t size)514 void ImfHiSysEventReporterTest::TestBaseTextOperation(const uint8_t *data, size_t size)
515 {
516 if (data == nullptr || size == 0) {
517 return;
518 }
519 std::string testString(reinterpret_cast<const char *>(data), size);
520 auto paramPeerName = testString;
521 auto paramPeerPid = static_cast<int64_t>(size);
522 auto errCode = static_cast<int32_t>(size);
523 auto paramIEventCode = static_cast<int32_t>(size);
524 auto info = HiSysOriginalInfo::Builder()
525 .SetPeerName(paramPeerName)
526 .SetPeerPid(paramPeerPid)
527 .SetClientType(PARAM_CLIENT_TYPE)
528 .SetEventCode(paramIEventCode)
529 .SetErrCode(errCode)
530 .Build();
531 ImaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::BASE_TEXT_OPERATOR, *info);
532 }
533
TestStatisticsEventConcurrent()534 void ImfHiSysEventReporterTest::TestStatisticsEventConcurrent()
535 {
536 for (int32_t i = 0; i < EACH_THREAD_CIRCULATION_TIME; i++) {
537 size_t size = static_cast<size_t>(rand() % 100); // A remainder of 100 can generate a random number from 0-99
538 uint8_t *data = static_cast<uint8_t *>(malloc(size * sizeof(uint8_t)));
539 if (data == nullptr) {
540 return;
541 }
542 for (auto j = 0; j < size; j++) {
543 data[j] = static_cast<uint8_t>(rand() % 100); // A remainder of 100 can generate a random number from 0-99
544 }
545
546 TestClientAttach(data, size);
547 TestClientShow(data, size);
548 TestStartInput(data, size);
549 TestBaseTextOperation(data, size);
550 ImaHiSysEventReporter::GetInstance().TimerCallback();
551 ImsaHiSysEventReporter::GetInstance().TimerCallback();
552 free(data);
553 multiThreadExecTotalNum_++;
554 }
555 }
556
557 std::mutex ImfHiSysEventReporterTest::cvMutex_;
558 std::condition_variable ImfHiSysEventReporterTest::watcherCv_;
559 std::shared_ptr<HiSysOriginalInfo> ImfHiSysEventReporterTest::faultInfo_ = HiSysOriginalInfo::Builder().Build();
560 std::string ImfHiSysEventReporterTest::eventName_;
561 ClientAttachStatistics ImfHiSysEventReporterTest::clientAttachStatistics_;
562 ClientShowStatistics ImfHiSysEventReporterTest::clientShowStatistics_;
563 ImeStartInputStatistics ImfHiSysEventReporterTest::imeStartInputStatistics_;
564 BaseTextOperationStatistics ImfHiSysEventReporterTest::baseTextOperationStatistics_;
565 int32_t ImfHiSysEventReporterTest::multiThreadExecTotalNum_{ 0 };
566
567 /**
568 * @tc.name: ImcClientAttachFailed_001
569 * @tc.desc: imc client attach failed, report fault, report fault again within 5 miniutes
570 * @tc.type: FUNC
571 * @tc.require:
572 * @tc.author: chenyu
573 */
574 HWTEST_F(ImfHiSysEventReporterTest, ImcClientAttachFailed_001, TestSize.Level0)
575 {
576 IMSA_HILOGI("ImcClientAttachFailed_001");
577 int32_t errCode = ErrorCode::ERROR_CLIENT_NOT_EDITABLE;
578 auto info = HiSysOriginalInfo::Builder()
579 .SetErrCode(errCode)
580 .SetInputPattern(PARAM_INPUT_PATTERN)
581 .SetIsShowKeyboard(PARAM_ISSHOWKEYBOARD)
582 .SetClientType(PARAM_CLIENT_TYPE)
583 .Build();
584 ImcHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_ATTACH, *info);
585 EXPECT_TRUE(ImfHiSysEventReporterTest::WaitFaultEvent(EVENT_NAME_CLIENT_ATTACH_FAILED, *info));
586
587 ImfHiSysEventReporterTest::faultInfo_ = HiSysOriginalInfo::Builder().Build();
588 ImfHiSysEventReporterTest::eventName_.clear();
589 // report again
590 ImcHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_ATTACH, *info);
591 EXPECT_FALSE(ImfHiSysEventReporterTest::WaitFaultEvent(EVENT_NAME_CLIENT_ATTACH_FAILED, *info));
592 auto key = ImcHiSysEventReporter::GetInstance().GenerateFaultEventKey(ImfFaultEvent::CLIENT_ATTACH_FAILED, *info);
593 auto it = ImcHiSysEventReporter::GetInstance().faultEventRecords_.find(key);
594 ASSERT_NE(it, ImcHiSysEventReporter::GetInstance().faultEventRecords_.end());
595 EXPECT_EQ(it->second.second, 2);
596 ImcHiSysEventReporter::GetInstance().faultEventRecords_.clear();
597 }
598 /**
599 * @tc.name: ImsaClientAttachFailed_002
600 * @tc.desc: imsa client attach failed, report fault
601 * @tc.type: FUNC
602 * @tc.require:
603 * @tc.author: chenyu
604 */
605 HWTEST_F(ImfHiSysEventReporterTest, ImsaClientAttachFailed_002, TestSize.Level0)
606 {
607 IMSA_HILOGI("ImsaClientAttachFailed_002");
608 int32_t errCode = ErrorCode::ERROR_IMSA_USER_SESSION_NOT_FOUND;
609 auto info = HiSysOriginalInfo::Builder()
610 .SetPeerName(PARAM_PEER_NAME)
611 .SetPeerPid(PARAM_PEER_PID)
612 .SetPeerUserId(PARAM_PEER_USERID)
613 .SetClientType(PARAM_CLIENT_TYPE)
614 .SetInputPattern(PARAM_INPUT_PATTERN)
615 .SetIsShowKeyboard(PARAM_ISSHOWKEYBOARD)
616 .SetImeName(PARAM_IME_NAME)
617 .SetErrCode(errCode)
618 .Build();
619 ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_ATTACH, *info);
620 EXPECT_TRUE(ImfHiSysEventReporterTest::WaitFaultEvent(EVENT_NAME_CLIENT_ATTACH_FAILED, *info));
621 auto key = ImsaHiSysEventReporter::GetInstance().GenerateFaultEventKey(ImfFaultEvent::CLIENT_ATTACH_FAILED, *info);
622 auto it = ImsaHiSysEventReporter::GetInstance().faultEventRecords_.find(key);
623 ASSERT_NE(it, ImsaHiSysEventReporter::GetInstance().faultEventRecords_.end());
624 EXPECT_EQ(it->second.second, 1);
625 ImsaHiSysEventReporter::GetInstance().faultEventRecords_.clear();
626 }
627 /**
628 * @tc.name: ImsaClientAttachFailed_003
629 * @tc.desc: imsa client attach failed, err but not fault, not report
630 * @tc.type: FUNC
631 * @tc.require:
632 * @tc.author: chenyu
633 */
634 HWTEST_F(ImfHiSysEventReporterTest, ImsaClientAttachFailed_003, TestSize.Level0)
635 {
636 IMSA_HILOGI("ImsaClientAttachFailed_003");
637 int32_t errCode = ErrorCode::ERROR_STATUS_PERMISSION_DENIED;
638 auto info = HiSysOriginalInfo::Builder()
639 .SetPeerName(PARAM_PEER_NAME)
640 .SetPeerPid(PARAM_PEER_PID)
641 .SetPeerUserId(PARAM_PEER_USERID)
642 .SetClientType(PARAM_CLIENT_TYPE)
643 .SetInputPattern(PARAM_INPUT_PATTERN)
644 .SetIsShowKeyboard(PARAM_ISSHOWKEYBOARD)
645 .SetImeName(PARAM_IME_NAME)
646 .SetErrCode(errCode)
647 .Build();
648 ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_ATTACH, *info);
649 EXPECT_FALSE(ImfHiSysEventReporterTest::WaitFaultEvent(EVENT_NAME_CLIENT_ATTACH_FAILED, *info));
650 auto key = ImsaHiSysEventReporter::GetInstance().GenerateFaultEventKey(ImfFaultEvent::CLIENT_ATTACH_FAILED, *info);
651 auto it = ImsaHiSysEventReporter::GetInstance().faultEventRecords_.find(key);
652 ASSERT_EQ(it, ImsaHiSysEventReporter::GetInstance().faultEventRecords_.end());
653 }
654 /**
655 * @tc.name: ImcClientShowFailed_004
656 * @tc.desc: imc client show failed, report fault
657 * @tc.type: FUNC
658 * @tc.require:
659 * @tc.author: chenyu
660 */
661 HWTEST_F(ImfHiSysEventReporterTest, ImcClientShowFailed_004, TestSize.Level0)
662 {
663 IMSA_HILOGI("ImcClientShowFailed_004");
664 int32_t errCode = ErrorCode::ERROR_CLIENT_NOT_EDITABLE;
665 auto info = HiSysOriginalInfo::Builder()
666 .SetPeerName(PARAM_PEER_NAME)
667 .SetPeerPid(PARAM_PEER_PID)
668 .SetPeerUserId(PARAM_PEER_USERID)
669 .SetClientType(PARAM_CLIENT_TYPE)
670 .SetImeName(PARAM_IME_NAME)
671 .SetEventCode(PARAM_EVENT_CODE)
672 .SetErrCode(errCode)
673 .Build();
674 ImcHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_SHOW, *info);
675 EXPECT_TRUE(ImfHiSysEventReporterTest::WaitFaultEvent(EVENT_NAME_CLIENT_SHOW_FAILED, *info));
676 auto key = ImcHiSysEventReporter::GetInstance().GenerateFaultEventKey(ImfFaultEvent::CLIENT_SHOW_FAILED, *info);
677 auto it = ImcHiSysEventReporter::GetInstance().faultEventRecords_.find(key);
678 ASSERT_NE(it, ImcHiSysEventReporter::GetInstance().faultEventRecords_.end());
679 EXPECT_EQ(it->second.second, 1);
680 ImcHiSysEventReporter::GetInstance().faultEventRecords_.clear();
681 }
682
683 /**
684 * @tc.name: ImsaClientShowFailed_005
685 * @tc.desc: imsa client show failed, report fault
686 * @tc.type: FUNC
687 * @tc.require:
688 * @tc.author: chenyu
689 */
690 HWTEST_F(ImfHiSysEventReporterTest, ImsaClientShowFailed_005, TestSize.Level0)
691 {
692 IMSA_HILOGI("ImsaClientShowFailed_005");
693 int32_t errCode = ErrorCode::ERROR_CLIENT_NULL_POINTER;
694 auto info = HiSysOriginalInfo::Builder()
695 .SetPeerName(PARAM_PEER_NAME)
696 .SetPeerPid(PARAM_PEER_PID)
697 .SetPeerUserId(PARAM_PEER_USERID)
698 .SetClientType(PARAM_CLIENT_TYPE)
699 .SetImeName(PARAM_IME_NAME)
700 .SetEventCode(PARAM_EVENT_CODE)
701 .SetErrCode(errCode)
702 .Build();
703 ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_SHOW, *info);
704 EXPECT_TRUE(ImfHiSysEventReporterTest::WaitFaultEvent(EVENT_NAME_CLIENT_SHOW_FAILED, *info));
705 auto key = ImsaHiSysEventReporter::GetInstance().GenerateFaultEventKey(ImfFaultEvent::CLIENT_SHOW_FAILED, *info);
706 auto it = ImsaHiSysEventReporter::GetInstance().faultEventRecords_.find(key);
707 ASSERT_NE(it, ImsaHiSysEventReporter::GetInstance().faultEventRecords_.end());
708 EXPECT_EQ(it->second.second, 1);
709 ImsaHiSysEventReporter::GetInstance().faultEventRecords_.clear();
710 }
711
712 /**
713 * @tc.name: ImcClientShowFailed_006
714 * @tc.desc: imc client show failed, report fault, but errCode not imc, not report
715 * @tc.type: FUNC
716 * @tc.require:
717 * @tc.author: chenyu
718 */
719 HWTEST_F(ImfHiSysEventReporterTest, ImcClientShowFailed_006, TestSize.Level0)
720 {
721 IMSA_HILOGI("ImcClientShowFailed_006");
722 int32_t errCode = ErrorCode::ERROR_CLIENT_NULL_POINTER;
723 auto info = HiSysOriginalInfo::Builder()
724 .SetPeerName(PARAM_PEER_NAME)
725 .SetPeerPid(PARAM_PEER_PID)
726 .SetPeerUserId(PARAM_PEER_USERID)
727 .SetClientType(PARAM_CLIENT_TYPE)
728 .SetImeName(PARAM_IME_NAME)
729 .SetEventCode(PARAM_EVENT_CODE)
730 .SetErrCode(errCode)
731 .Build();
732 ImcHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_SHOW, *info);
733 EXPECT_FALSE(ImfHiSysEventReporterTest::WaitFaultEvent(EVENT_NAME_CLIENT_SHOW_FAILED, *info));
734 auto key = ImcHiSysEventReporter::GetInstance().GenerateFaultEventKey(ImfFaultEvent::CLIENT_SHOW_FAILED, *info);
735 auto it = ImcHiSysEventReporter::GetInstance().faultEventRecords_.find(key);
736 ASSERT_EQ(it, ImcHiSysEventReporter::GetInstance().faultEventRecords_.end());
737 }
738
739 /**
740 * @tc.name: ImeStartInputFailed_007
741 * @tc.desc: imc start input failed, report fault
742 * @tc.type: FUNC
743 * @tc.require:
744 * @tc.author: chenyu
745 */
746 HWTEST_F(ImfHiSysEventReporterTest, ImeStartInputFailed_007, TestSize.Level0)
747 {
748 IMSA_HILOGI("ImeStartInputFailed_007");
749 int32_t errCode = ErrorCode::ERROR_IME;
750 auto info = HiSysOriginalInfo::Builder()
751 .SetPeerName(PARAM_PEER_NAME)
752 .SetPeerPid(PARAM_PEER_PID)
753 .SetIsShowKeyboard(PARAM_ISSHOWKEYBOARD)
754 .SetEventCode(PARAM_EVENT_CODE)
755 .SetErrCode(errCode)
756 .Build();
757 ImaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::IME_START_INPUT, *info);
758 EXPECT_TRUE(ImfHiSysEventReporterTest::WaitFaultEvent(EVENT_NAME_IME_START_INPUT_FAILED, *info));
759 auto key = ImaHiSysEventReporter::GetInstance().GenerateFaultEventKey(ImfFaultEvent::IME_START_INPUT_FAILED, *info);
760 auto it = ImaHiSysEventReporter::GetInstance().faultEventRecords_.find(key);
761 ASSERT_NE(it, ImaHiSysEventReporter::GetInstance().faultEventRecords_.end());
762 EXPECT_EQ(it->second.second, 1);
763 ImaHiSysEventReporter::GetInstance().faultEventRecords_.clear();
764 }
765
766 /**
767 * @tc.name: BaseTextOperationFailed_008
768 * @tc.desc: base text operation failed, report fault
769 * @tc.type: FUNC
770 * @tc.require:
771 * @tc.author: chenyu
772 */
773 HWTEST_F(ImfHiSysEventReporterTest, BaseTextOperationFailed_008, TestSize.Level0)
774 {
775 IMSA_HILOGI("BaseTextOperationFailed_008");
776 int32_t errCode = ErrorCode::ERROR_IMA_CHANNEL_NULLPTR;
777 auto info = HiSysOriginalInfo::Builder()
778 .SetPeerName(PARAM_PEER_NAME)
779 .SetPeerPid(PARAM_PEER_PID)
780 .SetClientType(PARAM_CLIENT_TYPE)
781 .SetEventCode(PARAM_EVENT_CODE)
782 .SetErrCode(errCode)
783 .Build();
784 ImaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::BASE_TEXT_OPERATOR, *info);
785 EXPECT_TRUE(ImfHiSysEventReporterTest::WaitFaultEvent(EVENT_NAME_BASIC_TEXT_OPERATION_FAILED, *info));
786 auto key =
787 ImaHiSysEventReporter::GetInstance().GenerateFaultEventKey(ImfFaultEvent::BASE_TEXT_OPERATION_FAILED, *info);
788 auto it = ImaHiSysEventReporter::GetInstance().faultEventRecords_.find(key);
789 ASSERT_NE(it, ImaHiSysEventReporter::GetInstance().faultEventRecords_.end());
790 EXPECT_EQ(it->second.second, 1);
791 ImaHiSysEventReporter::GetInstance().faultEventRecords_.clear();
792 }
793
794 /**
795 * @tc.name: ClientAttachStatistic_009
796 * @tc.desc: imsa report statistic that contain two different succeed and two different failed event
797 * @tc.type: FUNC
798 * @tc.require:
799 * @tc.author: chenyu
800 */
801 HWTEST_F(ImfHiSysEventReporterTest, ClientAttachStatistic_009, TestSize.Level0)
802 {
803 IMSA_HILOGI("ClientAttachStatistic_009");
804 int32_t errCode1 = ErrorCode::ERROR_CLIENT_NULL_POINTER;
805 int32_t errCode2 = ErrorCode::ERROR_CLIENT_ADD_FAILED;
806 auto info = HiSysOriginalInfo::Builder()
807 .SetPeerName(PARAM_PEER_NAME)
808 .SetPeerPid(PARAM_PEER_PID)
809 .SetPeerUserId(PARAM_PEER_USERID)
810 .SetClientType(PARAM_CLIENT_TYPE)
811 .SetImeName(PARAM_IME_NAME)
812 .SetErrCode(errCode1)
813 .Build();
814 ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_ATTACH, *info);
815 info->errCode = errCode2;
816 info->imeName = PARAM_IME_NAME1;
817 ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_ATTACH, *info);
818 info->errCode = ErrorCode::NO_ERROR;
819 ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_ATTACH, *info);
820 info->errCode = ErrorCode::NO_ERROR;
821 info->peerName = PARAM_PEER_NAME1;
822 ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_ATTACH, *info);
823 auto index = ImsaHiSysEventReporter::GetInstance().GetStatisticalIntervalIndex();
824 ASSERT_TRUE(index < ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
825 ClientAttachStatistics statistics;
826 statistics.imeNames.emplace_back(PARAM_IME_NAME);
827 statistics.imeNames.emplace_back(PARAM_IME_NAME1);
828 statistics.appNames.emplace_back("*");
829 statistics.succeedInfo.count = 2;
830 statistics.succeedInfo.info.resize(ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
831 statistics.succeedInfo.info[index] = { "0/2" };
832 statistics.failedInfo.count = 2;
833 statistics.failedInfo.info.resize(ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
834 std::string failedStr1("0");
835 failedStr1.append("/").append("0").append("/").append(std::to_string(PARAM_CLIENT_TYPE)).append("/")
836 .append(std::to_string(errCode1)).append("/").append("1");
837 std::string failedStr2("0");
838 failedStr2.append("/").append("1").append("/").append(std::to_string(PARAM_CLIENT_TYPE)).append("/")
839 .append(std::to_string(errCode2)).append("/").append("1");
840 statistics.failedInfo.info[index] = { failedStr1, failedStr2 };
841 ImsaHiSysEventReporter::GetInstance().TimerCallback();
842 EXPECT_TRUE(ImfHiSysEventReporterTest::WaitClientAttachStatistic(statistics));
843 EXPECT_TRUE(ImsaHiSysEventReporter::GetInstance().clientAttachInfo_.appNames.empty());
844 }
845 /**
846 * @tc.name: ClientShowStatistic_010
847 * @tc.desc: imsa report statistic that contain two same failed event
848 * @tc.type: FUNC
849 * @tc.require:
850 * @tc.author: chenyu
851 */
852 HWTEST_F(ImfHiSysEventReporterTest, ClientShowStatistic_010, TestSize.Level0)
853 {
854 IMSA_HILOGI("ClientShowStatistic_010");
855 int32_t errCode = ErrorCode::ERROR_STATUS_PERMISSION_DENIED;
856 auto info = HiSysOriginalInfo::Builder()
857 .SetPeerName(PARAM_PEER_NAME)
858 .SetPeerPid(PARAM_PEER_PID)
859 .SetPeerUserId(PARAM_PEER_USERID)
860 .SetClientType(PARAM_CLIENT_TYPE)
861 .SetImeName(PARAM_IME_NAME)
862 .SetEventCode(PARAM_EVENT_CODE)
863 .SetErrCode(errCode)
864 .Build();
865 ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_SHOW, *info);
866 ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_SHOW, *info);
867 auto index = ImsaHiSysEventReporter::GetInstance().GetStatisticalIntervalIndex();
868 ASSERT_TRUE(index < ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
869 ClientShowStatistics statistics;
870 statistics.imeNames.emplace_back(PARAM_IME_NAME);
871 statistics.appNames.emplace_back("*");
872 statistics.succeedInfo.count = 0;
873 statistics.succeedInfo.info.resize(ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
874 statistics.failedInfo.count = 2;
875 statistics.failedInfo.info.resize(ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
876 std::string failedStr("0");
877 failedStr.append("/")
878 .append("0")
879 .append("/")
880 .append(std::to_string(PARAM_CLIENT_TYPE))
881 .append("/")
882 .append(std::to_string(PARAM_EVENT_CODE))
883 .append("/")
884 .append(std::to_string(errCode))
885 .append("/")
886 .append("2");
887 statistics.failedInfo.info[index] = { failedStr };
888 ImsaHiSysEventReporter::GetInstance().TimerCallback();
889 EXPECT_TRUE(ImfHiSysEventReporterTest::WaitClientShowStatistic(statistics));
890 EXPECT_TRUE(ImsaHiSysEventReporter::GetInstance().clientShowInfo_.appNames.empty());
891 }
892 /**
893 * @tc.name: ImeStartInputStatistic_011
894 * @tc.desc: imsa report statistic that contain one succeed event and one failed event(not contain cbTime)
895 * @tc.type: FUNC
896 * @tc.require:
897 * @tc.author: chenyu
898 */
899 HWTEST_F(ImfHiSysEventReporterTest, ImeStartInputStatistic_011, TestSize.Level0)
900 {
901 IMSA_HILOGI("ImeStartInputStatistic_011");
902 int32_t errCode = ErrorCode::ERROR_IME;
903 auto info = HiSysOriginalInfo::Builder()
904 .SetPeerName(PARAM_PEER_NAME)
905 .SetPeerPid(PARAM_PEER_PID)
906 .SetIsShowKeyboard(true)
907 .SetEventCode(PARAM_EVENT_CODE)
908 .SetErrCode(errCode)
909 .Build();
910 ImaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::IME_START_INPUT, *info);
911 info->errCode = ErrorCode::NO_ERROR;
912 info->imeCbTime = 40;
913 ImaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::IME_START_INPUT, *info);
914 auto index = ImaHiSysEventReporter::GetInstance().GetStatisticalIntervalIndex();
915 ASSERT_TRUE(index < ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
916 ImeStartInputStatistics statistics;
917 statistics.appNames.emplace_back("*");
918 statistics.succeedInfo.count = 1;
919 statistics.succeedInfo.info.resize(ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
920 std::string succeedStr("0");
921 succeedStr.append("/").append(std::to_string(true)).append("/").append("1");
922 statistics.succeedInfo.info[index] = { succeedStr };
923 statistics.failedInfo.count = 1;
924 statistics.failedInfo.info.resize(ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
925 std::string failedStr("0");
926 failedStr.append("/")
927 .append(std::to_string(PARAM_EVENT_CODE))
928 .append("/")
929 .append(std::to_string(errCode))
930 .append("/")
931 .append("1");
932 statistics.failedInfo.info[index] = { failedStr };
933 statistics.cbTimeConsume.count = 1;
934 statistics.cbTimeConsume.timeConsumeStatistics = "0/1/0/0/0/0";
935 ImaHiSysEventReporter::GetInstance().TimerCallback();
936 EXPECT_TRUE(ImfHiSysEventReporterTest::WaitImeStartInputStatistic(statistics));
937 EXPECT_TRUE(ImaHiSysEventReporter::GetInstance().imeStartInputAllInfo_.appNames.empty());
938 }
939
940 /**
941 * @tc.name: BaseTextOperationStatistic_012
942 * @tc.desc: ima report statistic that contain one succeed event and one failed event
943 * @tc.type: FUNC
944 * @tc.require:
945 * @tc.author: chenyu
946 */
947 HWTEST_F(ImfHiSysEventReporterTest, BaseTextOperationStatistic_012, TestSize.Level0)
948 {
949 IMSA_HILOGI("BaseTextOperationStatistic_012");
950 int32_t failedConsumeTime = 60;
951 int32_t suceedConsumeTime = 10;
952 int32_t errCode = ErrorCode::ERROR_CLIENT_NULL_POINTER;
953 auto info = HiSysOriginalInfo::Builder()
954 .SetPeerName(PARAM_PEER_NAME)
955 .SetPeerPid(PARAM_PEER_PID)
956 .SetClientType(PARAM_CLIENT_TYPE)
957 .SetEventCode(PARAM_EVENT_CODE)
958 .SetErrCode(errCode)
959 .SetBaseTextOperatorTime(failedConsumeTime)
960 .Build();
961 ImaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::BASE_TEXT_OPERATOR, *info);
962 info->errCode = ErrorCode::NO_ERROR;
963 info->baseTextOperationTime = suceedConsumeTime;
964 ImaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::BASE_TEXT_OPERATOR, *info);
965
966 BaseTextOperationStatistics statistics;
967 statistics.appNames.emplace_back("*");
968 statistics.succeedInfo.count = 1;
969 statistics.succeedInfo.info.resize(ImaHiSysEventReporter::BASE_TEXT_OPERATION_TIME_INTERVAL.size());
970 auto succeedIndex =
971 ImaHiSysEventReporter::GetInstance().GetBaseTextOperationSucceedIntervalIndex(suceedConsumeTime);
972 ASSERT_TRUE(succeedIndex < ImaHiSysEventReporter::BASE_TEXT_OPERATION_TIME_INTERVAL.size());
973 std::string succeedStr("0");
974 succeedStr.append("/").append(std::to_string(PARAM_EVENT_CODE)).append("/").append("1");
975 statistics.succeedInfo.info[succeedIndex] = { succeedStr };
976 auto failedIndex = ImaHiSysEventReporter::GetInstance().GetStatisticalIntervalIndex();
977 ASSERT_TRUE(failedIndex < ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
978 statistics.failedInfo.count = 1;
979 statistics.failedInfo.info.resize(ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
980 std::string failedStr("0");
981 failedStr.append("/")
982 .append(std::to_string(PARAM_CLIENT_TYPE))
983 .append("/")
984 .append(std::to_string(PARAM_EVENT_CODE))
985 .append("/")
986 .append(std::to_string(errCode))
987 .append("/")
988 .append("1");
989 statistics.failedInfo.info[failedIndex] = { failedStr };
990 ImaHiSysEventReporter::GetInstance().TimerCallback();
991 EXPECT_TRUE(ImfHiSysEventReporterTest::WaitBaseTextOperatorStatistic(statistics));
992 EXPECT_TRUE(ImaHiSysEventReporter::GetInstance().baseTextOperationAllInfo_.appNames.empty());
993 }
994
995 /**
996 * @tc.name: StatisticsEventConcurrentTest_013
997 * @tc.desc:
998 * @tc.type: FUNC
999 * @tc.require:
1000 * @tc.author: chenyu
1001 */
1002 HWTEST_F(ImfHiSysEventReporterTest, StatisticsEventConcurrentTest_013, TestSize.Level0)
1003 {
1004 IMSA_HILOGI("StatisticsEventConcurrentTest_013");
1005 SET_THREAD_NUM(THREAD_NUM);
1006 GTEST_RUN_TASK(TestStatisticsEventConcurrent);
1007 EXPECT_EQ(multiThreadExecTotalNum_, THREAD_NUM * EACH_THREAD_CIRCULATION_TIME);
1008 }
1009 } // namespace MiscServices
1010 } // namespace OHOS
1011