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 #include "UTTest_dm_account_common_event.h"
16 
17 #include "common_event_support.h"
18 #include "dm_account_common_event.h"
19 #include "dm_constants.h"
20 #include "matching_skills.h"
21 #include "system_ability_definition.h"
22 
23 namespace OHOS {
24 namespace DistributedHardware {
SetUp()25 void DmAccountCommonEventManagerTest::SetUp()
26 {
27 }
28 
TearDown()29 void DmAccountCommonEventManagerTest::TearDown()
30 {
31 }
32 
SetUpTestCase()33 void DmAccountCommonEventManagerTest::SetUpTestCase()
34 {
35 }
36 
TearDownTestCase()37 void DmAccountCommonEventManagerTest::TearDownTestCase()
38 {
39 }
40 
41 namespace {
42 
HWTEST_F(DmAccountCommonEventManagerTest, SubscribeAccountCommonEvent_001, testing::ext::TestSize.Level0)43 HWTEST_F(DmAccountCommonEventManagerTest, SubscribeAccountCommonEvent_001, testing::ext::TestSize.Level0)
44 {
45     std::shared_ptr<DmAccountCommonEventManager> accountCommonEventManager
46         = std::make_shared<DmAccountCommonEventManager>();
47     std::vector<std::string> commonEventVec;
48     bool ret = accountCommonEventManager->SubscribeAccountCommonEvent(commonEventVec, nullptr);
49     ASSERT_EQ(ret, false);
50     commonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
51     ret = accountCommonEventManager->SubscribeAccountCommonEvent(commonEventVec, nullptr);
52     ASSERT_EQ(ret, false);
53 }
54 
HWTEST_F(DmAccountCommonEventManagerTest, UnSubscribeAccountCommonEvent_001, testing::ext::TestSize.Level0)55 HWTEST_F(DmAccountCommonEventManagerTest, UnSubscribeAccountCommonEvent_001, testing::ext::TestSize.Level0)
56 {
57     std::shared_ptr<DmAccountCommonEventManager> accountCommonEventManager
58         = std::make_shared<DmAccountCommonEventManager>();
59     bool ret = accountCommonEventManager->UnsubscribeAccountCommonEvent();
60     ASSERT_EQ(ret, false);
61     accountCommonEventManager->eventValidFlag_ = true;
62     ret = accountCommonEventManager->UnsubscribeAccountCommonEvent();
63     ASSERT_EQ(ret, true);
64 }
65 
HWTEST_F(DmAccountCommonEventManagerTest, OnReceiveEvent_001, testing::ext::TestSize.Level0)66 HWTEST_F(DmAccountCommonEventManagerTest, OnReceiveEvent_001, testing::ext::TestSize.Level0)
67 {
68     AAFwk::Want want;
69     EventFwk::CommonEventData data;
70     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
71     data.SetWant(want);
72     data.SetCode(0);
73 
74     std::vector<std::string> changeEventVec;
75     changeEventVec.push_back("changeEvent");
76     std::string strEvent = "test";
77     std::vector<std::string> strEventVec;
78     strEventVec.push_back(strEvent);
79     AccountEventCallback callback = nullptr;
80     EventFwk::MatchingSkills matchingSkills;
81     matchingSkills.AddEvent(strEvent);
82     CommonEventSubscribeInfo subscriberInfo(matchingSkills);
83 
84     std::shared_ptr<DmAccountCommonEventManager> accountCommonEventManager
85         = std::make_shared<DmAccountCommonEventManager>();
86     accountCommonEventManager->subscriber_
87         = std::make_shared<DmAccountEventSubscriber>(subscriberInfo, callback, strEventVec);
88     accountCommonEventManager->subscriber_->OnReceiveEvent(data);
89 
90     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
91     data.SetWant(want);
92     data.SetCode(1);
93     accountCommonEventManager->subscriber_->OnReceiveEvent(data);
94 
95     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT);
96     data.SetWant(want);
97     accountCommonEventManager->subscriber_->OnReceiveEvent(data);
98 
99     want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN);
100     data.SetWant(want);
101     accountCommonEventManager->subscriber_->OnReceiveEvent(data);
102     EXPECT_NE(accountCommonEventManager->subscriber_->GetSubscriberEventNameVec(), changeEventVec);
103 }
104 }
105 } // namespace DistributedHardware
106 } // namespace OHOS
107