• 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 #include "account_subscriber_unittest.h"
17 
18 using namespace std;
19 using namespace testing;
20 using namespace testing::ext;
21 using namespace OHOS::Media;
22 
23 const static int32_t TIME_ONE = 1;
24 const static int32_t TEST_USER_ID = 1;
25 const static int32_t TEST_INV_USER_ID = -1;
26 const static std::string TEST_ACTION = "test_action";
27 
28 namespace OHOS {
29 namespace Media {
SetUpTestCase(void)30 void AccountSubscriberUnitTest::SetUpTestCase(void) {}
31 
TearDownTestCase(void)32 void AccountSubscriberUnitTest::TearDownTestCase(void) {}
33 
SetUp(void)34 void AccountSubscriberUnitTest::SetUp(void)
35 {
36     accountSubscriber_ = AccountSubscriber::GetInstance();
37 }
38 
TearDown(void)39 void AccountSubscriberUnitTest::TearDown(void)
40 {
41     accountSubscriber_ = nullptr;
42 }
43 
44 /**
45  * @tc.name  : Test DispatchEvent
46  * @tc.number: DispatchEvent_001
47  * @tc.desc  : Test DispatchEvent mapIt == userMap_.end()
48  *             Test DispatchEvent mapIt != userMap_.end()
49  */
50 HWTEST_F(AccountSubscriberUnitTest, DispatchEvent_001, TestSize.Level1)
51 {
52     int32_t userId = TEST_USER_ID;
53     std::string expectedAction = TEST_ACTION;
54 
55     // Test DispatchEvent mapIt == userMap_.end()
56     accountSubscriber_->userMap_.clear();
57     accountSubscriber_->DispatchEvent(userId, expectedAction);
58 
59     // Test DispatchEvent mapIt != userMap_.end()
60     accountSubscriber_->userMap_[TEST_USER_ID] = { std::make_shared<MockCommonEventReceiver>() };
61     EXPECT_CALL(*std::static_pointer_cast<MockCommonEventReceiver>(accountSubscriber_->userMap_[TEST_USER_ID][0]),
62                 OnCommonEventReceived(_))
63         .Times(TIME_ONE);
64     accountSubscriber_->DispatchEvent(userId, expectedAction);
65 }
66 
67 /**
68  * @tc.name  : Test DispatchEvent
69  * @tc.number: DispatchEvent_002
70  * @tc.desc  : Test DispatchEvent receiver == nullptr
71  *             Test DispatchEvent receiver != nullptr
72  */
73 HWTEST_F(AccountSubscriberUnitTest, DispatchEvent_002, TestSize.Level1)
74 {
75     int32_t userId = TEST_USER_ID;
76     std::string expectedAction = TEST_ACTION;
77 
78     // Test DispatchEvent receiver == nullptr
79     accountSubscriber_->userMap_[TEST_USER_ID] = { nullptr };
80     accountSubscriber_->DispatchEvent(userId, expectedAction);
81 
82     // Test DispatchEvent receiver != nullptr
83     accountSubscriber_->userMap_[TEST_USER_ID] = { std::make_shared<MockCommonEventReceiver>() };
84     EXPECT_CALL(*std::static_pointer_cast<MockCommonEventReceiver>(accountSubscriber_->userMap_[TEST_USER_ID][0]),
85                 OnCommonEventReceived(_))
86         .Times(TIME_ONE);
87     accountSubscriber_->DispatchEvent(userId, expectedAction);
88 }
89 
90 /**
91  * @tc.name  : Test RegisterCommonEventReceiver
92  * @tc.number: RegisterCommonEventReceiver_001
93  * @tc.desc  : Test RegisterCommonEventReceiver receiver == nullptr
94  *             Test RegisterCommonEventReceiver userId < 0
95  *             Test RegisterCommonEventReceiver userMap_.empty() == false
96  *             Test RegisterCommonEventReceiver mapIt != userMap_.end()
97  */
98 HWTEST_F(AccountSubscriberUnitTest, RegisterCommonEventReceiver_001, TestSize.Level1)
99 {
100     // Test RegisterCommonEventReceiver receiver == nullptr
101     int32_t userId = TEST_USER_ID;
102     std::shared_ptr<CommonEventReceiver> receiver = nullptr;
103     accountSubscriber_->RegisterCommonEventReceiver(userId, receiver);
104 
105     // Test RegisterCommonEventReceiver userId < 0
106     userId = TEST_INV_USER_ID;
107     receiver = std::make_shared<MockCommonEventReceiver>();
108     accountSubscriber_->RegisterCommonEventReceiver(userId, receiver);
109 
110     // Test RegisterCommonEventReceiver userMap_.empty() == false
111     // Test RegisterCommonEventReceiver mapIt != userMap_.end()
112     userId = TEST_USER_ID;
113     accountSubscriber_->userMap_.insert({TEST_USER_ID, { receiver }});
114     accountSubscriber_->RegisterCommonEventReceiver(userId, receiver);
115     EXPECT_EQ(accountSubscriber_->userMap_.size(), 1);
116 }
117 
118 /**
119  * @tc.name  : Test RegisterCommonEventReceiver
120  * @tc.number: RegisterCommonEventReceiver_002
121  * @tc.desc  : Test RegisterCommonEventReceiver receiverIt == mapIt->second.end()
122  *             Test RegisterCommonEventReceiver receiverIt != mapIt->second.end()
123  */
124 HWTEST_F(AccountSubscriberUnitTest, RegisterCommonEventReceiver_002, TestSize.Level1)
125 {
126     int32_t userId = TEST_USER_ID;
127     auto receiver1 = std::make_shared<MockCommonEventReceiver>();
128     auto receiver2 = std::make_shared<MockCommonEventReceiver>();
129     accountSubscriber_->userMap_.insert({TEST_USER_ID, { receiver1 }});
130 
131     // Test RegisterCommonEventReceiver receiverIt != mapIt->second.end()
132     accountSubscriber_->RegisterCommonEventReceiver(userId, receiver1);
133 
134     // Test RegisterCommonEventReceiver receiver == nullptr
135     accountSubscriber_->RegisterCommonEventReceiver(userId, receiver2);
136     EXPECT_TRUE(!accountSubscriber_->userMap_[TEST_USER_ID].empty());
137 }
138 
139 /**
140  * @tc.name  : Test UnregisterCommonEventReceiver
141  * @tc.number: UnregisterCommonEventReceiver_001
142  * @tc.desc  : Test UnregisterCommonEventReceiver receiver == nullptr
143  *             Test UnregisterCommonEventReceiver mapIt == userMap_.end()
144  */
145 HWTEST_F(AccountSubscriberUnitTest, UnregisterCommonEventReceiver_001, TestSize.Level1)
146 {
147     // Test UnregisterCommonEventReceiver receiver == nullptr
148     int32_t userId = TEST_USER_ID;
149     std::shared_ptr<CommonEventReceiver> receiver = nullptr;
150     accountSubscriber_->UnregisterCommonEventReceiver(userId, receiver);
151 
152     // Test UnregisterCommonEventReceiver mapIt == userMap_.end()
153     userId = TEST_USER_ID;
154     receiver = std::make_shared<MockCommonEventReceiver>();
155     accountSubscriber_->userMap_.clear();
156     accountSubscriber_->UnregisterCommonEventReceiver(userId, receiver);
157     EXPECT_TRUE(accountSubscriber_->userMap_.empty());
158 }
159 
160 /**
161  * @tc.name  : Test UnregisterCommonEventReceiver
162  * @tc.number: UnregisterCommonEventReceiver_002
163  * @tc.desc  : Test UnregisterCommonEventReceiver receiverIt == mapIt->second.end()
164  */
165 HWTEST_F(AccountSubscriberUnitTest, UnregisterCommonEventReceiver_002, TestSize.Level1)
166 {
167     int32_t userId = TEST_USER_ID;
168     auto receiver1 = std::make_shared<MockCommonEventReceiver>();
169     auto receiver2 = std::make_shared<MockCommonEventReceiver>();
170     accountSubscriber_->userMap_[TEST_USER_ID] = { receiver1 };
171     accountSubscriber_->UnregisterCommonEventReceiver(userId, receiver2);
172     EXPECT_EQ(accountSubscriber_->userMap_[TEST_USER_ID].size(), 1);
173 }
174 
175 /**
176  * @tc.name  : Test UnregisterCommonEventReceiver
177  * @tc.number: UnregisterCommonEventReceiver_003
178  * @tc.desc  : Test UnregisterCommonEventReceiver mapIt->second.empty() == false
179  * @tc.desc  : Test UnregisterCommonEventReceiver userMap_.empty() == false
180  */
181 HWTEST_F(AccountSubscriberUnitTest, UnregisterCommonEventReceiver_003, TestSize.Level1)
182 {
183     int32_t userId = TEST_USER_ID;
184     auto receiver1 = std::make_shared<MockCommonEventReceiver>();
185     auto receiver2 = std::make_shared<MockCommonEventReceiver>();
186     accountSubscriber_->userMap_[TEST_USER_ID] = { receiver1, receiver2 };
187     accountSubscriber_->UnregisterCommonEventReceiver(userId, receiver2);
188     EXPECT_TRUE(!accountSubscriber_->userMap_.empty());
189 }
190 } // namespace Media
191 } // namespace OHOS
192