1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "UTTest_dm_publish_common_event.h"
17
18 #include "bluetooth_def.h"
19 #include "common_event_support.h"
20 #include "dm_constants.h"
21 #include "dm_publish_common_event.h"
22 #include "matching_skills.h"
23 #include "system_ability_definition.h"
24 #include "wifi_msg.h"
25
26 namespace OHOS {
27 namespace DistributedHardware {
SetUp()28 void DmPublishCommonEventManagerTest::SetUp()
29 {
30 }
31
TearDown()32 void DmPublishCommonEventManagerTest::TearDown()
33 {
34 }
35
SetUpTestCase()36 void DmPublishCommonEventManagerTest::SetUpTestCase()
37 {
38 }
39
TearDownTestCase()40 void DmPublishCommonEventManagerTest::TearDownTestCase()
41 {
42 }
43
44 namespace {
45
46 HWTEST_F(DmPublishCommonEventManagerTest, SubscribePublishCommonEvent_001, testing::ext::TestSize.Level0)
47 {
48 std::shared_ptr<DmPublishCommonEventManager> publshCommonEventManager
49 = std::make_shared<DmPublishCommonEventManager>();
50 std::vector<std::string> publishCommonEventVec;
51 bool ret = publshCommonEventManager->SubscribePublishCommonEvent(publishCommonEventVec, nullptr);
52 ASSERT_EQ(ret, false);
53 publishCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
54 ret = publshCommonEventManager->SubscribePublishCommonEvent(publishCommonEventVec, nullptr);
55 ASSERT_EQ(ret, false);
56 }
57
58 HWTEST_F(DmPublishCommonEventManagerTest, UnsubscribePublishCommonEvent_001, testing::ext::TestSize.Level0)
59 {
60 std::shared_ptr<DmPublishCommonEventManager> publshCommonEventManager
61 = std::make_shared<DmPublishCommonEventManager>();
62 bool ret = publshCommonEventManager->UnsubscribePublishCommonEvent();
63 ASSERT_EQ(ret, false);
64 publshCommonEventManager->eventValidFlag_ = true;
65 ret = publshCommonEventManager->UnsubscribePublishCommonEvent();
66 ASSERT_EQ(ret, true);
67 }
68
PublishCommonEventCallback(int32_t bluetoothState,int32_t wifiState,int32_t screenState)69 void PublishCommonEventCallback(int32_t bluetoothState, int32_t wifiState, int32_t screenState)
70 {
71 (void)bluetoothState;
72 (void)wifiState;
73 (void)screenState;
74 }
75
76 HWTEST_F(DmPublishCommonEventManagerTest, OnReceiveEvent_001, testing::ext::TestSize.Level0)
77 {
78 AAFwk::Want want;
79 EventFwk::CommonEventData data;
80 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
81 data.SetWant(want);
82 data.SetCode(0);
83 std::vector<std::string> changeEventVec;
84 changeEventVec.push_back(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
85 std::string strEvent = "test";
86 std::vector<std::string> strEventVec;
87 strEventVec.push_back(strEvent);
__anon627847870202(const auto &arg1, const auto &arg2, const auto &arg3) 88 PublishEventCallback callback = [=](const auto &arg1, const auto &arg2, const auto &arg3) {
89 PublishCommonEventCallback(arg1, arg2, arg3);
90 };
91 EventFwk::MatchingSkills matchingSkills;
92 matchingSkills.AddEvent(strEvent);
93 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
94
95 std::shared_ptr<DmPublishCommonEventManager> publshCommonEventManager
96 = std::make_shared<DmPublishCommonEventManager>();
97 publshCommonEventManager->subscriber_
98 = std::make_shared<DmPublishEventSubscriber>(subscriberInfo, callback, strEventVec);
99 publshCommonEventManager->subscriber_->OnReceiveEvent(data);
100
101 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_BLUETOOTH_HOST_STATE_UPDATE);
102 data.SetWant(want);
103 data.SetCode(static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_ON));
104 publshCommonEventManager->subscriber_->OnReceiveEvent(data);
105
106 data.SetCode(static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_OFF));
107 publshCommonEventManager->subscriber_->OnReceiveEvent(data);
108
109 data.SetCode(-1);
110 publshCommonEventManager->subscriber_->OnReceiveEvent(data);
111
112 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_WIFI_POWER_STATE);
113 data.SetWant(want);
114 data.SetCode(static_cast<int32_t>(OHOS::Wifi::WifiState::ENABLED));
115 publshCommonEventManager->subscriber_->OnReceiveEvent(data);
116
117 data.SetCode(static_cast<int32_t>(OHOS::Wifi::WifiState::DISABLED));
118 publshCommonEventManager->subscriber_->OnReceiveEvent(data);
119
120 EXPECT_NE(publshCommonEventManager->subscriber_->GetSubscriberEventNameVec(), changeEventVec);
121 }
122 }
123 } // namespace DistributedHardware
124 } // namespace OHOS
125