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
AccountCommonEventCallbacks(const std::string commonEventType,int32_t currentUserId,int32_t beforeUserId)41 void AccountCommonEventCallbacks(const std::string commonEventType, int32_t currentUserId, int32_t beforeUserId)
42 {
43 }
44 namespace {
45 HWTEST_F(DmAccountCommonEventManagerTest, SubscribeAccountCommonEvent_001, testing::ext::TestSize.Level0)
46 {
47 std::shared_ptr<DmAccountCommonEventManager> accountCommonEventManager
48 = std::make_shared<DmAccountCommonEventManager>();
49 std::vector<std::string> commonEventVec;
50 bool ret = accountCommonEventManager->SubscribeAccountCommonEvent(commonEventVec, nullptr);
51 ASSERT_EQ(ret, false);
52 commonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
53 ret = accountCommonEventManager->SubscribeAccountCommonEvent(commonEventVec, nullptr);
54 ASSERT_EQ(ret, false);
55 }
56
57 HWTEST_F(DmAccountCommonEventManagerTest, UnSubscribeAccountCommonEvent_001, testing::ext::TestSize.Level0)
58 {
59 std::shared_ptr<DmAccountCommonEventManager> accountCommonEventManager
60 = std::make_shared<DmAccountCommonEventManager>();
61 bool ret = accountCommonEventManager->UnsubscribeAccountCommonEvent();
62 ASSERT_EQ(ret, false);
63 accountCommonEventManager->eventValidFlag_ = true;
64 ret = accountCommonEventManager->UnsubscribeAccountCommonEvent();
65 ASSERT_EQ(ret, true);
66 }
67
68 HWTEST_F(DmAccountCommonEventManagerTest, OnReceiveEvent_001, testing::ext::TestSize.Level0)
69 {
70 AAFwk::Want want;
71 EventFwk::CommonEventData data;
72 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
73 data.SetWant(want);
74 data.SetCode(0);
75
76 std::vector<std::string> changeEventVec;
77 changeEventVec.push_back("changeEvent");
78 std::string strEvent = "test";
79 std::vector<std::string> strEventVec;
80 strEventVec.push_back(strEvent);
__anonffeb31c50202(const auto &eventType, const auto ¤tUserId, const auto &beforeUserId) 81 AccountEventCallback callback = [=](const auto &eventType, const auto ¤tUserId, const auto &beforeUserId) {
82 AccountCommonEventCallbacks(eventType, currentUserId, beforeUserId);
83 };
84 EventFwk::MatchingSkills matchingSkills;
85 matchingSkills.AddEvent(strEvent);
86 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
87
88 std::shared_ptr<DmAccountCommonEventManager> accountCommonEventManager
89 = std::make_shared<DmAccountCommonEventManager>();
90 accountCommonEventManager->subscriber_
91 = std::make_shared<DmAccountEventSubscriber>(subscriberInfo, callback, strEventVec);
92 accountCommonEventManager->subscriber_->OnReceiveEvent(data);
93
94 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
95 data.SetWant(want);
96 data.SetCode(1);
97 accountCommonEventManager->subscriber_->OnReceiveEvent(data);
98
99 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT);
100 data.SetWant(want);
101 accountCommonEventManager->subscriber_->OnReceiveEvent(data);
102
103 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN);
104 data.SetWant(want);
105 accountCommonEventManager->subscriber_->OnReceiveEvent(data);
106 EXPECT_NE(accountCommonEventManager->subscriber_->GetSubscriberEventNameVec(), changeEventVec);
107 }
108 }
109 } // namespace DistributedHardware
110 } // namespace OHOS
111