• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                     .SetImeCbTime(rand())
511                     .Build();
512     ImaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::IME_START_INPUT, *info);
513 }
514 
TestBaseTextOperation(const uint8_t * data,size_t size)515 void ImfHiSysEventReporterTest::TestBaseTextOperation(const uint8_t *data, size_t size)
516 {
517     if (data == nullptr || size == 0) {
518         return;
519     }
520     std::string testString(reinterpret_cast<const char *>(data), size);
521     auto paramPeerName = testString;
522     auto paramPeerPid = static_cast<int64_t>(size);
523     auto errCode = static_cast<int32_t>(size);
524     auto paramIEventCode = static_cast<int32_t>(size);
525     auto info = HiSysOriginalInfo::Builder()
526                     .SetPeerName(paramPeerName)
527                     .SetPeerPid(paramPeerPid)
528                     .SetClientType(PARAM_CLIENT_TYPE)
529                     .SetEventCode(paramIEventCode)
530                     .SetErrCode(errCode)
531                     .SetBaseTextOperatorTime(rand())
532                     .Build();
533     ImaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::BASE_TEXT_OPERATOR, *info);
534 }
535 
TestStatisticsEventConcurrent()536 void ImfHiSysEventReporterTest::TestStatisticsEventConcurrent()
537 {
538     for (int32_t i = 0; i < EACH_THREAD_CIRCULATION_TIME; i++) {
539         size_t size = static_cast<size_t>(rand() % 100); // A remainder of 100 can generate a random number from 0-99
540         uint8_t *data = static_cast<uint8_t *>(malloc(size * sizeof(uint8_t)));
541         if (data == nullptr) {
542             return;
543         }
544         for (auto j = 0; j < size; j++) {
545             data[j] = static_cast<uint8_t>(rand() % 100); // A remainder of 100 can generate a random number from 0-99
546         }
547 
548         TestClientAttach(data, size);
549         TestClientShow(data, size);
550         TestStartInput(data, size);
551         TestBaseTextOperation(data, size);
552         ImaHiSysEventReporter::GetInstance().TimerCallback();
553         ImsaHiSysEventReporter::GetInstance().TimerCallback();
554         free(data);
555         multiThreadExecTotalNum_++;
556     }
557 }
558 
559 std::mutex ImfHiSysEventReporterTest::cvMutex_;
560 std::condition_variable ImfHiSysEventReporterTest::watcherCv_;
561 std::shared_ptr<HiSysOriginalInfo> ImfHiSysEventReporterTest::faultInfo_ = HiSysOriginalInfo::Builder().Build();
562 std::string ImfHiSysEventReporterTest::eventName_;
563 ClientAttachStatistics ImfHiSysEventReporterTest::clientAttachStatistics_;
564 ClientShowStatistics ImfHiSysEventReporterTest::clientShowStatistics_;
565 ImeStartInputStatistics ImfHiSysEventReporterTest::imeStartInputStatistics_;
566 BaseTextOperationStatistics ImfHiSysEventReporterTest::baseTextOperationStatistics_;
567 int32_t ImfHiSysEventReporterTest::multiThreadExecTotalNum_{ 0 };
568 
569 /**
570  * @tc.name: ImcClientAttachFailed_001
571  * @tc.desc: imc client attach failed, report fault, report fault again within 5 miniutes
572  * @tc.type: FUNC
573  * @tc.require:
574  * @tc.author: chenyu
575  */
576 HWTEST_F(ImfHiSysEventReporterTest, ImcClientAttachFailed_001, TestSize.Level1)
577 {
578     IMSA_HILOGI("ImcClientAttachFailed_001");
579     int32_t errCode = ErrorCode::ERROR_CLIENT_NOT_EDITABLE;
580     auto info = HiSysOriginalInfo::Builder()
581                     .SetErrCode(errCode)
582                     .SetInputPattern(PARAM_INPUT_PATTERN)
583                     .SetIsShowKeyboard(PARAM_ISSHOWKEYBOARD)
584                     .SetClientType(PARAM_CLIENT_TYPE)
585                     .Build();
586     ImcHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_ATTACH, *info);
587     EXPECT_TRUE(ImfHiSysEventReporterTest::WaitFaultEvent(EVENT_NAME_CLIENT_ATTACH_FAILED, *info));
588 
589     ImfHiSysEventReporterTest::faultInfo_ = HiSysOriginalInfo::Builder().Build();
590     ImfHiSysEventReporterTest::eventName_.clear();
591     // report again
592     ImcHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_ATTACH, *info);
593     EXPECT_FALSE(ImfHiSysEventReporterTest::WaitFaultEvent(EVENT_NAME_CLIENT_ATTACH_FAILED, *info));
594     auto key = ImcHiSysEventReporter::GetInstance().GenerateFaultEventKey(ImfFaultEvent::CLIENT_ATTACH_FAILED, *info);
595     auto it = ImcHiSysEventReporter::GetInstance().faultEventRecords_.find(key);
596     ASSERT_NE(it, ImcHiSysEventReporter::GetInstance().faultEventRecords_.end());
597     EXPECT_EQ(it->second.second, 2);
598     ImcHiSysEventReporter::GetInstance().faultEventRecords_.clear();
599 }
600 /**
601  * @tc.name: ImsaClientAttachFailed_002
602  * @tc.desc: imsa client attach failed, report fault
603  * @tc.type: FUNC
604  * @tc.require:
605  * @tc.author: chenyu
606  */
607 HWTEST_F(ImfHiSysEventReporterTest, ImsaClientAttachFailed_002, TestSize.Level1)
608 {
609     IMSA_HILOGI("ImsaClientAttachFailed_002");
610     int32_t errCode = ErrorCode::ERROR_IMSA_USER_SESSION_NOT_FOUND;
611     auto info = HiSysOriginalInfo::Builder()
612                     .SetPeerName(PARAM_PEER_NAME)
613                     .SetPeerPid(PARAM_PEER_PID)
614                     .SetPeerUserId(PARAM_PEER_USERID)
615                     .SetClientType(PARAM_CLIENT_TYPE)
616                     .SetInputPattern(PARAM_INPUT_PATTERN)
617                     .SetIsShowKeyboard(PARAM_ISSHOWKEYBOARD)
618                     .SetImeName(PARAM_IME_NAME)
619                     .SetErrCode(errCode)
620                     .Build();
621     ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_ATTACH, *info);
622     EXPECT_TRUE(ImfHiSysEventReporterTest::WaitFaultEvent(EVENT_NAME_CLIENT_ATTACH_FAILED, *info));
623     auto key = ImsaHiSysEventReporter::GetInstance().GenerateFaultEventKey(ImfFaultEvent::CLIENT_ATTACH_FAILED, *info);
624     auto it = ImsaHiSysEventReporter::GetInstance().faultEventRecords_.find(key);
625     ASSERT_NE(it, ImsaHiSysEventReporter::GetInstance().faultEventRecords_.end());
626     EXPECT_EQ(it->second.second, 1);
627     ImsaHiSysEventReporter::GetInstance().faultEventRecords_.clear();
628 }
629 /**
630  * @tc.name: ImsaClientAttachFailed_003
631  * @tc.desc: imsa client attach failed, err but not fault, not report
632  * @tc.type: FUNC
633  * @tc.require:
634  * @tc.author: chenyu
635  */
636 HWTEST_F(ImfHiSysEventReporterTest, ImsaClientAttachFailed_003, TestSize.Level1)
637 {
638     IMSA_HILOGI("ImsaClientAttachFailed_003");
639     int32_t errCode = ErrorCode::ERROR_STATUS_PERMISSION_DENIED;
640     auto info = HiSysOriginalInfo::Builder()
641                     .SetPeerName(PARAM_PEER_NAME)
642                     .SetPeerPid(PARAM_PEER_PID)
643                     .SetPeerUserId(PARAM_PEER_USERID)
644                     .SetClientType(PARAM_CLIENT_TYPE)
645                     .SetInputPattern(PARAM_INPUT_PATTERN)
646                     .SetIsShowKeyboard(PARAM_ISSHOWKEYBOARD)
647                     .SetImeName(PARAM_IME_NAME)
648                     .SetErrCode(errCode)
649                     .Build();
650     ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_ATTACH, *info);
651     EXPECT_FALSE(ImfHiSysEventReporterTest::WaitFaultEvent(EVENT_NAME_CLIENT_ATTACH_FAILED, *info));
652     auto key = ImsaHiSysEventReporter::GetInstance().GenerateFaultEventKey(ImfFaultEvent::CLIENT_ATTACH_FAILED, *info);
653     auto it = ImsaHiSysEventReporter::GetInstance().faultEventRecords_.find(key);
654     ASSERT_EQ(it, ImsaHiSysEventReporter::GetInstance().faultEventRecords_.end());
655 }
656 /**
657  * @tc.name: ImcClientShowFailed_004
658  * @tc.desc: imc client show failed, report fault
659  * @tc.type: FUNC
660  * @tc.require:
661  * @tc.author: chenyu
662  */
663 HWTEST_F(ImfHiSysEventReporterTest, ImcClientShowFailed_004, TestSize.Level1)
664 {
665     IMSA_HILOGI("ImcClientShowFailed_004");
666     int32_t errCode = ErrorCode::ERROR_CLIENT_NOT_EDITABLE;
667     auto info = HiSysOriginalInfo::Builder()
668                     .SetPeerName(PARAM_PEER_NAME)
669                     .SetPeerPid(PARAM_PEER_PID)
670                     .SetPeerUserId(PARAM_PEER_USERID)
671                     .SetClientType(PARAM_CLIENT_TYPE)
672                     .SetImeName(PARAM_IME_NAME)
673                     .SetEventCode(PARAM_EVENT_CODE)
674                     .SetErrCode(errCode)
675                     .Build();
676     ImcHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_SHOW, *info);
677     EXPECT_TRUE(ImfHiSysEventReporterTest::WaitFaultEvent(EVENT_NAME_CLIENT_SHOW_FAILED, *info));
678     auto key = ImcHiSysEventReporter::GetInstance().GenerateFaultEventKey(ImfFaultEvent::CLIENT_SHOW_FAILED, *info);
679     auto it = ImcHiSysEventReporter::GetInstance().faultEventRecords_.find(key);
680     ASSERT_NE(it, ImcHiSysEventReporter::GetInstance().faultEventRecords_.end());
681     EXPECT_EQ(it->second.second, 1);
682     ImcHiSysEventReporter::GetInstance().faultEventRecords_.clear();
683 }
684 
685 /**
686  * @tc.name: ImsaClientShowFailed_005
687  * @tc.desc: imsa client show failed, report fault
688  * @tc.type: FUNC
689  * @tc.require:
690  * @tc.author: chenyu
691  */
692 HWTEST_F(ImfHiSysEventReporterTest, ImsaClientShowFailed_005, TestSize.Level1)
693 {
694     IMSA_HILOGI("ImsaClientShowFailed_005");
695     int32_t errCode = ErrorCode::ERROR_CLIENT_NULL_POINTER;
696     auto info = HiSysOriginalInfo::Builder()
697                     .SetPeerName(PARAM_PEER_NAME)
698                     .SetPeerPid(PARAM_PEER_PID)
699                     .SetPeerUserId(PARAM_PEER_USERID)
700                     .SetClientType(PARAM_CLIENT_TYPE)
701                     .SetImeName(PARAM_IME_NAME)
702                     .SetEventCode(PARAM_EVENT_CODE)
703                     .SetErrCode(errCode)
704                     .Build();
705     ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_SHOW, *info);
706     EXPECT_TRUE(ImfHiSysEventReporterTest::WaitFaultEvent(EVENT_NAME_CLIENT_SHOW_FAILED, *info));
707     auto key = ImsaHiSysEventReporter::GetInstance().GenerateFaultEventKey(ImfFaultEvent::CLIENT_SHOW_FAILED, *info);
708     auto it = ImsaHiSysEventReporter::GetInstance().faultEventRecords_.find(key);
709     ASSERT_NE(it, ImsaHiSysEventReporter::GetInstance().faultEventRecords_.end());
710     EXPECT_EQ(it->second.second, 1);
711     ImsaHiSysEventReporter::GetInstance().faultEventRecords_.clear();
712 }
713 
714 /**
715  * @tc.name: ImcClientShowFailed_006
716  * @tc.desc: imc client show failed, report fault, but errCode not imc, not report
717  * @tc.type: FUNC
718  * @tc.require:
719  * @tc.author: chenyu
720  */
721 HWTEST_F(ImfHiSysEventReporterTest, ImcClientShowFailed_006, TestSize.Level1)
722 {
723     IMSA_HILOGI("ImcClientShowFailed_006");
724     int32_t errCode = ErrorCode::ERROR_CLIENT_NULL_POINTER;
725     auto info = HiSysOriginalInfo::Builder()
726                     .SetPeerName(PARAM_PEER_NAME)
727                     .SetPeerPid(PARAM_PEER_PID)
728                     .SetPeerUserId(PARAM_PEER_USERID)
729                     .SetClientType(PARAM_CLIENT_TYPE)
730                     .SetImeName(PARAM_IME_NAME)
731                     .SetEventCode(PARAM_EVENT_CODE)
732                     .SetErrCode(errCode)
733                     .Build();
734     ImcHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_SHOW, *info);
735     EXPECT_FALSE(ImfHiSysEventReporterTest::WaitFaultEvent(EVENT_NAME_CLIENT_SHOW_FAILED, *info));
736     auto key = ImcHiSysEventReporter::GetInstance().GenerateFaultEventKey(ImfFaultEvent::CLIENT_SHOW_FAILED, *info);
737     auto it = ImcHiSysEventReporter::GetInstance().faultEventRecords_.find(key);
738     ASSERT_EQ(it, ImcHiSysEventReporter::GetInstance().faultEventRecords_.end());
739 }
740 
741 /**
742  * @tc.name: ImeStartInputFailed_007
743  * @tc.desc: imc start input failed, report fault
744  * @tc.type: FUNC
745  * @tc.require:
746  * @tc.author: chenyu
747  */
748 HWTEST_F(ImfHiSysEventReporterTest, ImeStartInputFailed_007, TestSize.Level1)
749 {
750     IMSA_HILOGI("ImeStartInputFailed_007");
751     int32_t errCode = ErrorCode::ERROR_IME;
752     auto info = HiSysOriginalInfo::Builder()
753                     .SetPeerName(PARAM_PEER_NAME)
754                     .SetPeerPid(PARAM_PEER_PID)
755                     .SetIsShowKeyboard(PARAM_ISSHOWKEYBOARD)
756                     .SetEventCode(PARAM_EVENT_CODE)
757                     .SetErrCode(errCode)
758                     .Build();
759     ImaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::IME_START_INPUT, *info);
760     EXPECT_TRUE(ImfHiSysEventReporterTest::WaitFaultEvent(EVENT_NAME_IME_START_INPUT_FAILED, *info));
761     auto key = ImaHiSysEventReporter::GetInstance().GenerateFaultEventKey(ImfFaultEvent::IME_START_INPUT_FAILED, *info);
762     auto it = ImaHiSysEventReporter::GetInstance().faultEventRecords_.find(key);
763     ASSERT_NE(it, ImaHiSysEventReporter::GetInstance().faultEventRecords_.end());
764     EXPECT_EQ(it->second.second, 1);
765     ImaHiSysEventReporter::GetInstance().faultEventRecords_.clear();
766 }
767 
768 /**
769  * @tc.name: BaseTextOperationFailed_008
770  * @tc.desc: base text operation failed, report fault
771  * @tc.type: FUNC
772  * @tc.require:
773  * @tc.author: chenyu
774  */
775 HWTEST_F(ImfHiSysEventReporterTest, BaseTextOperationFailed_008, TestSize.Level1)
776 {
777     IMSA_HILOGI("BaseTextOperationFailed_008");
778     int32_t errCode = ErrorCode::ERROR_IMA_CHANNEL_NULLPTR;
779     auto info = HiSysOriginalInfo::Builder()
780                     .SetPeerName(PARAM_PEER_NAME)
781                     .SetPeerPid(PARAM_PEER_PID)
782                     .SetClientType(PARAM_CLIENT_TYPE)
783                     .SetEventCode(PARAM_EVENT_CODE)
784                     .SetErrCode(errCode)
785                     .Build();
786     ImaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::BASE_TEXT_OPERATOR, *info);
787     EXPECT_TRUE(ImfHiSysEventReporterTest::WaitFaultEvent(EVENT_NAME_BASIC_TEXT_OPERATION_FAILED, *info));
788     auto key =
789         ImaHiSysEventReporter::GetInstance().GenerateFaultEventKey(ImfFaultEvent::BASE_TEXT_OPERATION_FAILED, *info);
790     auto it = ImaHiSysEventReporter::GetInstance().faultEventRecords_.find(key);
791     ASSERT_NE(it, ImaHiSysEventReporter::GetInstance().faultEventRecords_.end());
792     EXPECT_EQ(it->second.second, 1);
793     ImaHiSysEventReporter::GetInstance().faultEventRecords_.clear();
794 }
795 
796 /**
797  * @tc.name: ClientAttachStatistic_009
798  * @tc.desc: imsa report statistic that contain two different succeed and two different failed event
799  * @tc.type: FUNC
800  * @tc.require:
801  * @tc.author: chenyu
802  */
803 HWTEST_F(ImfHiSysEventReporterTest, ClientAttachStatistic_009, TestSize.Level1)
804 {
805     IMSA_HILOGI("ClientAttachStatistic_009");
806     int32_t errCode1 = ErrorCode::ERROR_CLIENT_NULL_POINTER;
807     int32_t errCode2 = ErrorCode::ERROR_CLIENT_ADD_FAILED;
808     auto info = HiSysOriginalInfo::Builder()
809                     .SetPeerName(PARAM_PEER_NAME)
810                     .SetPeerPid(PARAM_PEER_PID)
811                     .SetPeerUserId(PARAM_PEER_USERID)
812                     .SetClientType(PARAM_CLIENT_TYPE)
813                     .SetImeName(PARAM_IME_NAME)
814                     .SetErrCode(errCode1)
815                     .Build();
816     ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_ATTACH, *info);
817     info->errCode = errCode2;
818     info->imeName = PARAM_IME_NAME1;
819     ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_ATTACH, *info);
820     info->errCode = ErrorCode::NO_ERROR;
821     ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_ATTACH, *info);
822     info->errCode = ErrorCode::NO_ERROR;
823     info->peerName = PARAM_PEER_NAME1;
824     ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_ATTACH, *info);
825     auto index = ImsaHiSysEventReporter::GetInstance().GetStatisticalIntervalIndex();
826     ASSERT_TRUE(index < ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
827     ClientAttachStatistics statistics;
828     statistics.imeNames.emplace_back(PARAM_IME_NAME);
829     statistics.imeNames.emplace_back(PARAM_IME_NAME1);
830     statistics.appNames.emplace_back("*");
831     statistics.succeedInfo.count = 2;
832     statistics.succeedInfo.info.resize(ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
833     statistics.succeedInfo.info[index] = { "0/2" };
834     statistics.failedInfo.count = 2;
835     statistics.failedInfo.info.resize(ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
836     std::string failedStr1("0");
837     failedStr1.append("/").append("0").append("/").append(std::to_string(PARAM_CLIENT_TYPE)).append("/")
838         .append(std::to_string(errCode1)).append("/").append("1");
839     std::string failedStr2("0");
840     failedStr2.append("/").append("1").append("/").append(std::to_string(PARAM_CLIENT_TYPE)).append("/")
841         .append(std::to_string(errCode2)).append("/").append("1");
842     statistics.failedInfo.info[index] = { failedStr1, failedStr2 };
843     ImsaHiSysEventReporter::GetInstance().TimerCallback();
844     EXPECT_TRUE(ImfHiSysEventReporterTest::WaitClientAttachStatistic(statistics));
845     EXPECT_TRUE(ImsaHiSysEventReporter::GetInstance().clientAttachInfo_.appNames.empty());
846 }
847 /**
848  * @tc.name: ClientShowStatistic_010
849  * @tc.desc: imsa report statistic that contain two same failed event
850  * @tc.type: FUNC
851  * @tc.require:
852  * @tc.author: chenyu
853  */
854 HWTEST_F(ImfHiSysEventReporterTest, ClientShowStatistic_010, TestSize.Level1)
855 {
856     IMSA_HILOGI("ClientShowStatistic_010");
857     int32_t errCode = ErrorCode::ERROR_STATUS_PERMISSION_DENIED;
858     auto info = HiSysOriginalInfo::Builder()
859                     .SetPeerName(PARAM_PEER_NAME)
860                     .SetPeerPid(PARAM_PEER_PID)
861                     .SetPeerUserId(PARAM_PEER_USERID)
862                     .SetClientType(PARAM_CLIENT_TYPE)
863                     .SetImeName(PARAM_IME_NAME)
864                     .SetEventCode(PARAM_EVENT_CODE)
865                     .SetErrCode(errCode)
866                     .Build();
867     ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_SHOW, *info);
868     ImsaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::CLIENT_SHOW, *info);
869     auto index = ImsaHiSysEventReporter::GetInstance().GetStatisticalIntervalIndex();
870     ASSERT_TRUE(index < ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
871     ClientShowStatistics statistics;
872     statistics.imeNames.emplace_back(PARAM_IME_NAME);
873     statistics.appNames.emplace_back("*");
874     statistics.succeedInfo.count = 0;
875     statistics.succeedInfo.info.resize(ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
876     statistics.failedInfo.count = 2;
877     statistics.failedInfo.info.resize(ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
878     std::string failedStr("0");
879     failedStr.append("/")
880         .append("0")
881         .append("/")
882         .append(std::to_string(PARAM_CLIENT_TYPE))
883         .append("/")
884         .append(std::to_string(PARAM_EVENT_CODE))
885         .append("/")
886         .append(std::to_string(errCode))
887         .append("/")
888         .append("2");
889     statistics.failedInfo.info[index] = { failedStr };
890     ImsaHiSysEventReporter::GetInstance().TimerCallback();
891     EXPECT_TRUE(ImfHiSysEventReporterTest::WaitClientShowStatistic(statistics));
892     EXPECT_TRUE(ImsaHiSysEventReporter::GetInstance().clientShowInfo_.appNames.empty());
893 }
894 /**
895  * @tc.name: ImeStartInputStatistic_011
896  * @tc.desc: imsa report statistic that contain one succeed event and one failed event(not contain cbTime)
897  * @tc.type: FUNC
898  * @tc.require:
899  * @tc.author: chenyu
900  */
901 HWTEST_F(ImfHiSysEventReporterTest, ImeStartInputStatistic_011, TestSize.Level1)
902 {
903     IMSA_HILOGI("ImeStartInputStatistic_011");
904     int32_t errCode = ErrorCode::ERROR_IME;
905     auto info = HiSysOriginalInfo::Builder()
906                     .SetPeerName(PARAM_PEER_NAME)
907                     .SetPeerPid(PARAM_PEER_PID)
908                     .SetIsShowKeyboard(true)
909                     .SetEventCode(PARAM_EVENT_CODE)
910                     .SetErrCode(errCode)
911                     .Build();
912     ImaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::IME_START_INPUT, *info);
913     info->errCode = ErrorCode::NO_ERROR;
914     info->imeCbTime = 40;
915     ImaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::IME_START_INPUT, *info);
916     auto index = ImaHiSysEventReporter::GetInstance().GetStatisticalIntervalIndex();
917     ASSERT_TRUE(index < ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
918     ImeStartInputStatistics statistics;
919     statistics.appNames.emplace_back("*");
920     statistics.succeedInfo.count = 1;
921     statistics.succeedInfo.info.resize(ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
922     std::string succeedStr("0");
923     succeedStr.append("/").append(std::to_string(true)).append("/").append("1");
924     statistics.succeedInfo.info[index] = { succeedStr };
925     statistics.failedInfo.count = 1;
926     statistics.failedInfo.info.resize(ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
927     std::string failedStr("0");
928     failedStr.append("/")
929         .append(std::to_string(PARAM_EVENT_CODE))
930         .append("/")
931         .append(std::to_string(errCode))
932         .append("/")
933         .append("1");
934     statistics.failedInfo.info[index] = { failedStr };
935     statistics.cbTimeConsume.count = 1;
936     statistics.cbTimeConsume.timeConsumeStatistics = "0/1/0/0/0/0";
937     ImaHiSysEventReporter::GetInstance().TimerCallback();
938     EXPECT_TRUE(ImfHiSysEventReporterTest::WaitImeStartInputStatistic(statistics));
939     EXPECT_TRUE(ImaHiSysEventReporter::GetInstance().imeStartInputAllInfo_.appNames.empty());
940 }
941 
942 /**
943  * @tc.name: BaseTextOperationStatistic_012
944  * @tc.desc: ima report statistic that contain one succeed event and one failed event
945  * @tc.type: FUNC
946  * @tc.require:
947  * @tc.author: chenyu
948  */
949 HWTEST_F(ImfHiSysEventReporterTest, BaseTextOperationStatistic_012, TestSize.Level1)
950 {
951     IMSA_HILOGI("BaseTextOperationStatistic_012");
952     int32_t failedConsumeTime = 60;
953     int32_t suceedConsumeTime = 10;
954     int32_t errCode = ErrorCode::ERROR_CLIENT_NULL_POINTER;
955     auto info = HiSysOriginalInfo::Builder()
956                     .SetPeerName(PARAM_PEER_NAME)
957                     .SetPeerPid(PARAM_PEER_PID)
958                     .SetClientType(PARAM_CLIENT_TYPE)
959                     .SetEventCode(PARAM_EVENT_CODE)
960                     .SetErrCode(errCode)
961                     .SetBaseTextOperatorTime(failedConsumeTime)
962                     .Build();
963     ImaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::BASE_TEXT_OPERATOR, *info);
964     info->errCode = ErrorCode::NO_ERROR;
965     info->baseTextOperationTime = suceedConsumeTime;
966     ImaHiSysEventReporter::GetInstance().ReportEvent(ImfEventType::BASE_TEXT_OPERATOR, *info);
967 
968     BaseTextOperationStatistics statistics;
969     statistics.appNames.emplace_back("*");
970     statistics.succeedInfo.count = 1;
971     statistics.succeedInfo.info.resize(ImaHiSysEventReporter::BASE_TEXT_OPERATION_TIME_INTERVAL.size());
972     auto succeedIndex =
973         ImaHiSysEventReporter::GetInstance().GetBaseTextOperationSucceedIntervalIndex(suceedConsumeTime);
974     ASSERT_TRUE(succeedIndex < ImaHiSysEventReporter::BASE_TEXT_OPERATION_TIME_INTERVAL.size());
975     std::string succeedStr("0");
976     succeedStr.append("/").append(std::to_string(PARAM_EVENT_CODE)).append("/").append("1");
977     statistics.succeedInfo.info[succeedIndex] = { succeedStr };
978     auto failedIndex = ImaHiSysEventReporter::GetInstance().GetStatisticalIntervalIndex();
979     ASSERT_TRUE(failedIndex < ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
980     statistics.failedInfo.count = 1;
981     statistics.failedInfo.info.resize(ImfHiSysEventReporter::COUNT_STATISTICS_INTERVAL_NUM);
982     std::string failedStr("0");
983     failedStr.append("/")
984         .append(std::to_string(PARAM_CLIENT_TYPE))
985         .append("/")
986         .append(std::to_string(PARAM_EVENT_CODE))
987         .append("/")
988         .append(std::to_string(errCode))
989         .append("/")
990         .append("1");
991     statistics.failedInfo.info[failedIndex] = { failedStr };
992     ImaHiSysEventReporter::GetInstance().TimerCallback();
993     EXPECT_TRUE(ImfHiSysEventReporterTest::WaitBaseTextOperatorStatistic(statistics));
994     EXPECT_TRUE(ImaHiSysEventReporter::GetInstance().baseTextOperationAllInfo_.appNames.empty());
995 }
996 
997 /**
998  * @tc.name: StatisticsEventConcurrentTest_013
999  * @tc.desc:
1000  * @tc.type: FUNC
1001  * @tc.require:
1002  * @tc.author: chenyu
1003  */
1004 HWTEST_F(ImfHiSysEventReporterTest, StatisticsEventConcurrentTest_013, TestSize.Level1)
1005 {
1006     IMSA_HILOGI("StatisticsEventConcurrentTest_013");
1007     SET_THREAD_NUM(THREAD_NUM);
1008     GTEST_RUN_TASK(TestStatisticsEventConcurrent);
1009     EXPECT_EQ(multiThreadExecTotalNum_, THREAD_NUM * EACH_THREAD_CIRCULATION_TIME);
1010 }
1011 } // namespace MiscServices
1012 } // namespace OHOS