17405867cSopenharmony_ci/* 27405867cSopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd. 37405867cSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 47405867cSopenharmony_ci * you may not use this file except in compliance with the License. 57405867cSopenharmony_ci * You may obtain a copy of the License at 67405867cSopenharmony_ci * 77405867cSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 87405867cSopenharmony_ci * 97405867cSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 107405867cSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 117405867cSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 127405867cSopenharmony_ci * See the License for the specific language governing permissions and 137405867cSopenharmony_ci * limitations under the License. 147405867cSopenharmony_ci */ 157405867cSopenharmony_ci#include <iostream> 167405867cSopenharmony_ci#include <map> 177405867cSopenharmony_ci 187405867cSopenharmony_ci#include <gtest/gtest.h> 197405867cSopenharmony_ci 207405867cSopenharmony_ci#include "app_event_store.h" 217405867cSopenharmony_ci#include "app_event_watcher.h" 227405867cSopenharmony_ci#include "app_event_observer_mgr.h" 237405867cSopenharmony_ci#include "hiappevent_base.h" 247405867cSopenharmony_ci#include "hiappevent_config.h" 257405867cSopenharmony_ci 267405867cSopenharmony_ciusing namespace testing::ext; 277405867cSopenharmony_ciusing namespace OHOS::HiviewDFX; 287405867cSopenharmony_ciusing namespace OHOS::HiviewDFX::HiAppEvent; 297405867cSopenharmony_ci 307405867cSopenharmony_cinamespace { 317405867cSopenharmony_ciconst std::string TEST_DIR = "/data/test/hiappevent/"; 327405867cSopenharmony_ciconst std::string TEST_WATCHER = "test_watcher"; 337405867cSopenharmony_ciconst std::string TEST_WATCHER_ROW = "watcher_row"; 347405867cSopenharmony_ciconst std::string TEST_WATCHER_SIZE = "watcher_size"; 357405867cSopenharmony_ciconst std::string TEST_WATCHER_TIMEOUT = "watcher_time"; 367405867cSopenharmony_ciconst std::string TEST_DOMAIN = "test_domain"; 377405867cSopenharmony_ciconst std::string TEST_NAME = "test_name"; 387405867cSopenharmony_ciconstexpr unsigned int TEST_TYPE = 1; 397405867cSopenharmony_ciconst std::string TEST_EVENT = R"~({"domain_":"hiappevent", "name_":"testEvent"})~"; 407405867cSopenharmony_ci 417405867cSopenharmony_cistd::shared_ptr<AppEventPack> CreateAppEventPack(const std::string& domain = TEST_DOMAIN) 427405867cSopenharmony_ci{ 437405867cSopenharmony_ci return std::make_shared<AppEventPack>(domain, TEST_NAME, TEST_TYPE); 447405867cSopenharmony_ci} 457405867cSopenharmony_ci 467405867cSopenharmony_ciclass HiAppEventWatcherTest : public testing::Test { 477405867cSopenharmony_cipublic: 487405867cSopenharmony_ci void SetUp(); 497405867cSopenharmony_ci void TearDown(); 507405867cSopenharmony_ci}; 517405867cSopenharmony_ci 527405867cSopenharmony_civoid HiAppEventWatcherTest::SetUp() 537405867cSopenharmony_ci{ 547405867cSopenharmony_ci HiAppEventConfig::GetInstance().SetStorageDir(TEST_DIR); 557405867cSopenharmony_ci (void)AppEventStore::GetInstance().InitDbStore(); 567405867cSopenharmony_ci AppEventObserverMgr::GetInstance().CreateEventHandler(); 577405867cSopenharmony_ci} 587405867cSopenharmony_ci 597405867cSopenharmony_civoid HiAppEventWatcherTest::TearDown() 607405867cSopenharmony_ci{ 617405867cSopenharmony_ci (void)AppEventStore::GetInstance().DestroyDbStore(); 627405867cSopenharmony_ci AppEventObserverMgr::GetInstance().DestroyEventHandler(); 637405867cSopenharmony_ci} 647405867cSopenharmony_ci 657405867cSopenharmony_ciclass AppEventWatcherTest : public AppEventWatcher { 667405867cSopenharmony_cipublic: 677405867cSopenharmony_ci AppEventWatcherTest( 687405867cSopenharmony_ci const std::string& name, 697405867cSopenharmony_ci const std::vector<AppEventFilter>& filters, 707405867cSopenharmony_ci TriggerCondition cond) 717405867cSopenharmony_ci : AppEventWatcher(name, filters, cond) {} 727405867cSopenharmony_ci 737405867cSopenharmony_ci void OnTrigger(const TriggerCondition& triggerCond) override 747405867cSopenharmony_ci { 757405867cSopenharmony_ci std::cout << GetName() << " onTrigger, row=" << triggerCond.row << ", size=" << triggerCond.size << std::endl; 767405867cSopenharmony_ci triggerTimes++; 777405867cSopenharmony_ci } 787405867cSopenharmony_ci 797405867cSopenharmony_ci int GetTriggerTimes() 807405867cSopenharmony_ci { 817405867cSopenharmony_ci return triggerTimes; 827405867cSopenharmony_ci } 837405867cSopenharmony_ci 847405867cSopenharmony_ci void OnEvents(const std::vector<std::shared_ptr<AppEventPack>>& events) override 857405867cSopenharmony_ci { 867405867cSopenharmony_ci std::cout << GetName() << " OnEvents size=" << events.size() << std::endl; 877405867cSopenharmony_ci for (const auto& event : events) { 887405867cSopenharmony_ci std::cout << "domain=" << event->GetDomain() << ", eventName=" << event->GetName() 897405867cSopenharmony_ci << ", eventType=" << event->GetType() << std::endl; 907405867cSopenharmony_ci std::cout << "params=" << event->GetParamStr() << std::endl; 917405867cSopenharmony_ci } 927405867cSopenharmony_ci } 937405867cSopenharmony_ci 947405867cSopenharmony_ciprivate: 957405867cSopenharmony_ci int triggerTimes = 0; 967405867cSopenharmony_ci}; 977405867cSopenharmony_ci 987405867cSopenharmony_civoid BuildSimpleFilters(std::vector<AppEventFilter>& filters) 997405867cSopenharmony_ci{ 1007405867cSopenharmony_ci filters.emplace_back(AppEventFilter(TEST_DOMAIN, 0xff)); // 0xff means all types 1017405867cSopenharmony_ci} 1027405867cSopenharmony_ci 1037405867cSopenharmony_civoid BuildSimpleOsFilters(std::vector<AppEventFilter>& filters) 1047405867cSopenharmony_ci{ 1057405867cSopenharmony_ci filters.emplace_back(AppEventFilter("OS", {"APP_CRASH"})); 1067405867cSopenharmony_ci} 1077405867cSopenharmony_ci 1087405867cSopenharmony_ciTriggerCondition BuildCondition(int row, int size, int timeout) 1097405867cSopenharmony_ci{ 1107405867cSopenharmony_ci TriggerCondition cond = { 1117405867cSopenharmony_ci .row = row, 1127405867cSopenharmony_ci .size = size, 1137405867cSopenharmony_ci .timeout = timeout, 1147405867cSopenharmony_ci }; 1157405867cSopenharmony_ci return cond; 1167405867cSopenharmony_ci} 1177405867cSopenharmony_ci 1187405867cSopenharmony_cistd::shared_ptr<AppEventWatcherTest> BuildSimpleWatcher() 1197405867cSopenharmony_ci{ 1207405867cSopenharmony_ci std::vector<AppEventFilter> filters; 1217405867cSopenharmony_ci TriggerCondition cond = BuildCondition(0, 0, 0); 1227405867cSopenharmony_ci return std::make_shared<AppEventWatcherTest>(TEST_WATCHER, filters, cond); 1237405867cSopenharmony_ci} 1247405867cSopenharmony_ci 1257405867cSopenharmony_cistd::shared_ptr<AppEventWatcherTest> BuildWatcherWithRow() 1267405867cSopenharmony_ci{ 1277405867cSopenharmony_ci std::vector<AppEventFilter> filters; 1287405867cSopenharmony_ci BuildSimpleFilters(filters); 1297405867cSopenharmony_ci TriggerCondition cond = BuildCondition(1, 0, 0); // row is 1 1307405867cSopenharmony_ci return std::make_shared<AppEventWatcherTest>(TEST_WATCHER_ROW, filters, cond); 1317405867cSopenharmony_ci} 1327405867cSopenharmony_ci 1337405867cSopenharmony_cistd::shared_ptr<AppEventWatcherTest> BuildWatcherWithSize() 1347405867cSopenharmony_ci{ 1357405867cSopenharmony_ci std::vector<AppEventFilter> filters; 1367405867cSopenharmony_ci BuildSimpleFilters(filters); 1377405867cSopenharmony_ci TriggerCondition cond = BuildCondition(0, 10, 0); // size is 10 byte 1387405867cSopenharmony_ci return std::make_shared<AppEventWatcherTest>(TEST_WATCHER_SIZE, filters, cond); 1397405867cSopenharmony_ci} 1407405867cSopenharmony_ci 1417405867cSopenharmony_cistd::shared_ptr<AppEventWatcherTest> BuildWatcherWithTimeout() 1427405867cSopenharmony_ci{ 1437405867cSopenharmony_ci std::vector<AppEventFilter> filters; 1447405867cSopenharmony_ci BuildSimpleFilters(filters); 1457405867cSopenharmony_ci TriggerCondition cond = BuildCondition(0, 0, 1); // timeout is 1 1467405867cSopenharmony_ci return std::make_shared<AppEventWatcherTest>(TEST_WATCHER_TIMEOUT, filters, cond); 1477405867cSopenharmony_ci} 1487405867cSopenharmony_ci 1497405867cSopenharmony_cistd::shared_ptr<AppEventWatcherTest> BuildWatcherWithTimeout2() 1507405867cSopenharmony_ci{ 1517405867cSopenharmony_ci std::vector<AppEventFilter> filters; 1527405867cSopenharmony_ci BuildSimpleFilters(filters); 1537405867cSopenharmony_ci TriggerCondition cond = BuildCondition(0, 0, 1); // timeout is 1 1547405867cSopenharmony_ci return std::make_shared<AppEventWatcherTest>("watcher_time2", filters, cond); 1557405867cSopenharmony_ci} 1567405867cSopenharmony_ci 1577405867cSopenharmony_cistd::shared_ptr<AppEventWatcherTest> BuildSimpleOsWatcher() 1587405867cSopenharmony_ci{ 1597405867cSopenharmony_ci std::vector<AppEventFilter> filters; 1607405867cSopenharmony_ci BuildSimpleOsFilters(filters); 1617405867cSopenharmony_ci TriggerCondition cond = BuildCondition(0, 0, 0); 1627405867cSopenharmony_ci return std::make_shared<AppEventWatcherTest>(TEST_WATCHER, filters, cond); 1637405867cSopenharmony_ci} 1647405867cSopenharmony_ci} 1657405867cSopenharmony_ci 1667405867cSopenharmony_ci/** 1677405867cSopenharmony_ci * @tc.name: HiAppEventWatcherTest001 1687405867cSopenharmony_ci * @tc.desc: Test to add watcher with no condition. 1697405867cSopenharmony_ci * @tc.type: FUNC 1707405867cSopenharmony_ci * @tc.require: issueI5LB4N 1717405867cSopenharmony_ci */ 1727405867cSopenharmony_ciHWTEST_F(HiAppEventWatcherTest, HiAppEventWatcherTest001, TestSize.Level3) 1737405867cSopenharmony_ci{ 1747405867cSopenharmony_ci /** 1757405867cSopenharmony_ci * @tc.steps: step1. create AppEventWatcher object. 1767405867cSopenharmony_ci * @tc.steps: step2. add the watcher to AppEventObserverMgr. 1777405867cSopenharmony_ci */ 1787405867cSopenharmony_ci std::cout << "HiAppEventWatcherTest001 start" << std::endl; 1797405867cSopenharmony_ci 1807405867cSopenharmony_ci auto watcher1 = BuildSimpleWatcher(); 1817405867cSopenharmony_ci AppEventObserverMgr::GetInstance().RegisterObserver(watcher1); 1827405867cSopenharmony_ci auto watcher2 = BuildWatcherWithRow(); 1837405867cSopenharmony_ci AppEventObserverMgr::GetInstance().RegisterObserver(watcher2); 1847405867cSopenharmony_ci auto watcher3 = BuildWatcherWithSize(); 1857405867cSopenharmony_ci AppEventObserverMgr::GetInstance().RegisterObserver(watcher3); 1867405867cSopenharmony_ci auto watcher4 = BuildWatcherWithTimeout(); 1877405867cSopenharmony_ci AppEventObserverMgr::GetInstance().RegisterObserver(watcher4); 1887405867cSopenharmony_ci auto watcher5 = BuildWatcherWithTimeout2(); 1897405867cSopenharmony_ci AppEventObserverMgr::GetInstance().RegisterObserver(watcher5); 1907405867cSopenharmony_ci 1917405867cSopenharmony_ci std::vector<std::shared_ptr<AppEventPack>> events; 1927405867cSopenharmony_ci events.emplace_back(CreateAppEventPack()); 1937405867cSopenharmony_ci AppEventObserverMgr::GetInstance().HandleEvents(events); 1947405867cSopenharmony_ci ASSERT_EQ(watcher1->GetTriggerTimes(), 0); 1957405867cSopenharmony_ci ASSERT_EQ(watcher2->GetTriggerTimes(), 1); 1967405867cSopenharmony_ci ASSERT_EQ(watcher3->GetTriggerTimes(), 1); 1977405867cSopenharmony_ci ASSERT_EQ(watcher4->GetTriggerTimes(), 0); 1987405867cSopenharmony_ci 1997405867cSopenharmony_ci events.clear(); 2007405867cSopenharmony_ci events.emplace_back(CreateAppEventPack("invalid_domain")); 2017405867cSopenharmony_ci AppEventObserverMgr::GetInstance().HandleEvents(events); 2027405867cSopenharmony_ci ASSERT_EQ(watcher1->GetTriggerTimes(), 0); 2037405867cSopenharmony_ci ASSERT_EQ(watcher2->GetTriggerTimes(), 1); 2047405867cSopenharmony_ci ASSERT_EQ(watcher3->GetTriggerTimes(), 1); 2057405867cSopenharmony_ci ASSERT_EQ(watcher4->GetTriggerTimes(), 0); 2067405867cSopenharmony_ci 2077405867cSopenharmony_ci AppEventObserverMgr::GetInstance().HandleTimeout(); 2087405867cSopenharmony_ci ASSERT_EQ(watcher4->GetTriggerTimes(), 1); 2097405867cSopenharmony_ci ASSERT_EQ(watcher5->GetTriggerTimes(), 1); 2107405867cSopenharmony_ci 2117405867cSopenharmony_ci AppEventObserverMgr::GetInstance().UnregisterObserver(watcher1->GetName()); 2127405867cSopenharmony_ci AppEventObserverMgr::GetInstance().UnregisterObserver(watcher2->GetName()); 2137405867cSopenharmony_ci AppEventObserverMgr::GetInstance().UnregisterObserver(watcher3->GetName()); 2147405867cSopenharmony_ci AppEventObserverMgr::GetInstance().UnregisterObserver(watcher4->GetName()); 2157405867cSopenharmony_ci AppEventObserverMgr::GetInstance().UnregisterObserver(watcher5->GetName()); 2167405867cSopenharmony_ci std::cout << "HiAppEventWatcherTest001 end" << std::endl; 2177405867cSopenharmony_ci} 2187405867cSopenharmony_ci 2197405867cSopenharmony_ci/** 2207405867cSopenharmony_ci * @tc.name: HiAppEventWatcherTest002 2217405867cSopenharmony_ci * @tc.desc: Test failed to add watcher. 2227405867cSopenharmony_ci * @tc.type: FUNC 2237405867cSopenharmony_ci * @tc.require: issueI5LB4N 2247405867cSopenharmony_ci */ 2257405867cSopenharmony_ciHWTEST_F(HiAppEventWatcherTest, HiAppEventWatcherTest002, TestSize.Level3) 2267405867cSopenharmony_ci{ 2277405867cSopenharmony_ci /** 2287405867cSopenharmony_ci * @tc.steps: step1. create AppEventWatcher object. 2297405867cSopenharmony_ci * @tc.steps: step2. add the watcher to AppEventObserverMgr. 2307405867cSopenharmony_ci */ 2317405867cSopenharmony_ci std::cout << "HiAppEventWatcherTest002 start" << std::endl; 2327405867cSopenharmony_ci (void)AppEventStore::GetInstance().DestroyDbStore(); 2337405867cSopenharmony_ci 2347405867cSopenharmony_ci auto watcher = BuildSimpleWatcher(); 2357405867cSopenharmony_ci AppEventObserverMgr::GetInstance().RegisterObserver(watcher); 2367405867cSopenharmony_ci std::vector<std::shared_ptr<AppEventPack>> events; 2377405867cSopenharmony_ci events.emplace_back(CreateAppEventPack()); 2387405867cSopenharmony_ci AppEventObserverMgr::GetInstance().HandleEvents(events); 2397405867cSopenharmony_ci ASSERT_EQ(watcher->GetTriggerTimes(), 0); 2407405867cSopenharmony_ci 2417405867cSopenharmony_ci AppEventObserverMgr::GetInstance().UnregisterObserver(watcher->GetName()); 2427405867cSopenharmony_ci std::cout << "HiAppEventWatcherTest002 end" << std::endl; 2437405867cSopenharmony_ci} 2447405867cSopenharmony_ci 2457405867cSopenharmony_ci/** 2467405867cSopenharmony_ci * @tc.name: HiAppEventWatcherTest003 2477405867cSopenharmony_ci * @tc.desc: Test to add watcher repeatedly. 2487405867cSopenharmony_ci * @tc.type: FUNC 2497405867cSopenharmony_ci * @tc.require: issueI5LB4N 2507405867cSopenharmony_ci */ 2517405867cSopenharmony_ciHWTEST_F(HiAppEventWatcherTest, HiAppEventWatcherTest003, TestSize.Level3) 2527405867cSopenharmony_ci{ 2537405867cSopenharmony_ci /** 2547405867cSopenharmony_ci * @tc.steps: step1. create AppEventWatcher object. 2557405867cSopenharmony_ci * @tc.steps: step2. add the watcher to AppEventObserverMgr. 2567405867cSopenharmony_ci */ 2577405867cSopenharmony_ci std::cout << "HiAppEventWatcherTest003 start" << std::endl; 2587405867cSopenharmony_ci 2597405867cSopenharmony_ci auto watcher1 = BuildWatcherWithRow(); 2607405867cSopenharmony_ci AppEventObserverMgr::GetInstance().RegisterObserver(watcher1); 2617405867cSopenharmony_ci auto watcher2 = BuildWatcherWithRow(); 2627405867cSopenharmony_ci AppEventObserverMgr::GetInstance().RegisterObserver(watcher2); 2637405867cSopenharmony_ci 2647405867cSopenharmony_ci std::vector<std::shared_ptr<AppEventPack>> events; 2657405867cSopenharmony_ci events.emplace_back(CreateAppEventPack()); 2667405867cSopenharmony_ci AppEventObserverMgr::GetInstance().HandleEvents(events); 2677405867cSopenharmony_ci ASSERT_EQ(watcher1->GetTriggerTimes(), 0); 2687405867cSopenharmony_ci ASSERT_EQ(watcher2->GetTriggerTimes(), 1); 2697405867cSopenharmony_ci 2707405867cSopenharmony_ci AppEventObserverMgr::GetInstance().UnregisterObserver(watcher1->GetName()); 2717405867cSopenharmony_ci AppEventObserverMgr::GetInstance().UnregisterObserver(watcher2->GetName()); 2727405867cSopenharmony_ci std::cout << "HiAppEventWatcherTest003 end" << std::endl; 2737405867cSopenharmony_ci} 2747405867cSopenharmony_ci 2757405867cSopenharmony_ci/** 2767405867cSopenharmony_ci * @tc.name: HiAppEventWatcherTest004 2777405867cSopenharmony_ci * @tc.desc: Test to add watcher onReceive. 2787405867cSopenharmony_ci * @tc.type: FUNC 2797405867cSopenharmony_ci * @tc.require: issueI5LB4N 2807405867cSopenharmony_ci */ 2817405867cSopenharmony_ciHWTEST_F(HiAppEventWatcherTest, HiAppEventWatcherTest004, TestSize.Level3) 2827405867cSopenharmony_ci{ 2837405867cSopenharmony_ci /** 2847405867cSopenharmony_ci * @tc.steps: step1. create AppEventWatcher object. 2857405867cSopenharmony_ci * @tc.steps: step2. add the watcher to AppEventObserverMgr. 2867405867cSopenharmony_ci */ 2877405867cSopenharmony_ci std::cout << "HiAppEventWatcherTest004 start" << std::endl; 2887405867cSopenharmony_ci 2897405867cSopenharmony_ci auto watcher = BuildSimpleOsWatcher(); 2907405867cSopenharmony_ci AppEventObserverMgr::GetInstance().RegisterObserver(watcher); 2917405867cSopenharmony_ci std::vector<std::shared_ptr<AppEventPack>> events; 2927405867cSopenharmony_ci events.emplace_back(std::make_shared<AppEventPack>("OS", "APP_CRASH", TEST_TYPE)); 2937405867cSopenharmony_ci AppEventObserverMgr::GetInstance().HandleEvents(events); 2947405867cSopenharmony_ci ASSERT_EQ(watcher->GetTriggerTimes(), 0); 2957405867cSopenharmony_ci 2967405867cSopenharmony_ci AppEventObserverMgr::GetInstance().UnregisterObserver(watcher->GetName()); 2977405867cSopenharmony_ci std::cout << "HiAppEventWatcherTest004 end" << std::endl; 2987405867cSopenharmony_ci} 299