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 <gtest/gtest.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <string>
20 #include <vector>
21 #include <iostream>
22
23 #include "distributed_device_profile_service_new.h"
24 #include "multi_user_manager.h"
25 #include "common_event_subscribe_info.h"
26 #include "common_event_support.h"
27 #include "matching_skills.h"
28 #include "system_ability_status_change_stub.h"
29
30 namespace OHOS {
31 namespace DistributedDeviceProfile {
32 using namespace testing::ext;
33 using namespace std;
34 namespace {
35 const std::string TAG = "AccountCommonEventTest";
36 }
37 class AccountCommonEventTest : public testing::Test {
38 public:
39 static void SetUpTestCase();
40 static void TearDownTestCase();
41 void SetUp();
42 void TearDown();
43 };
44
SetUpTestCase()45 void AccountCommonEventTest::SetUpTestCase()
46 {
47 }
48
TearDownTestCase()49 void AccountCommonEventTest::TearDownTestCase()
50 {
51 }
52
SetUp()53 void AccountCommonEventTest::SetUp()
54 {
55 }
56
TearDown()57 void AccountCommonEventTest::TearDown()
58 {
59 }
60
61 /*
62 * @tc.name: SubscribeAccountCommonEvent
63 * @tc.desc: Normal testCase of AccountCommonEventTest for CRUD
64 * @tc.type: FUNC
65 */
66 HWTEST_F(AccountCommonEventTest, SubscribeAccountCommonEvent_001, TestSize.Level1)
67 {
68 std::shared_ptr<DpAccountCommonEventManager> accountCommonEventManager;
69 accountCommonEventManager = std::make_shared<DpAccountCommonEventManager>();
__anonb51446270202(const auto &arg1, const auto &arg2) 70 AccountEventCallback callback = [=](const auto &arg1, const auto &arg2) {
71 DistributedDeviceProfileServiceNew::GetInstance().AccountCommonEventCallback(arg1, arg2);
72 };
73 std::vector<std::string> accountCommonEventVec;
74 accountCommonEventManager->eventValidFlag_ = true;
75 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
76 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
77 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT);
78 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN);
79 bool ret = accountCommonEventManager->SubscribeAccountCommonEvent(accountCommonEventVec, callback);
80 EXPECT_EQ(ret, false);
81 }
82
83 /*
84 * @tc.name: SubscribeAccountCommonEvent
85 * @tc.desc: Normal testCase of AccountCommonEventTest for CRUD
86 * @tc.type: FUNC
87 */
88 HWTEST_F(AccountCommonEventTest, SubscribeAccountCommonEvent_002, TestSize.Level1)
89 {
90 std::shared_ptr<DpAccountCommonEventManager> accountCommonEventManager;
91 accountCommonEventManager = std::make_shared<DpAccountCommonEventManager>();
__anonb51446270302(const auto &arg1, const auto &arg2) 92 AccountEventCallback callback = [=](const auto &arg1, const auto &arg2) {
93 DistributedDeviceProfileServiceNew::GetInstance().AccountCommonEventCallback(arg1, arg2);
94 };
95 std::vector<std::string> accountCommonEventVec;
96 accountCommonEventManager->eventValidFlag_ = false;
97 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
98 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
99 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT);
100 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN);
101 bool ret = accountCommonEventManager->SubscribeAccountCommonEvent(accountCommonEventVec, callback);
102 EXPECT_EQ(ret, true);
103 }
104
105 /*
106 * @tc.name: SubscribeAccountCommonEvent
107 * @tc.desc: Normal testCase of AccountCommonEventTest for CRUD
108 * @tc.type: FUNC
109 */
110 HWTEST_F(AccountCommonEventTest, SubscribeAccountCommonEvent_003, TestSize.Level1)
111 {
112 std::shared_ptr<DpAccountCommonEventManager> accountCommonEventManager;
113 accountCommonEventManager = std::make_shared<DpAccountCommonEventManager>();
__anonb51446270402(const auto &arg1, const auto &arg2) 114 AccountEventCallback callback = [=](const auto &arg1, const auto &arg2) {
115 DistributedDeviceProfileServiceNew::GetInstance().AccountCommonEventCallback(arg1, arg2);
116 };
117 std::vector<std::string> accountCommonEventVec;
118 bool ret = accountCommonEventManager->SubscribeAccountCommonEvent(accountCommonEventVec, callback);
119 EXPECT_EQ(ret, false);
120 }
121
122 /*
123 * @tc.name: SubscribeAccountCommonEvent
124 * @tc.desc: Normal testCase of AccountCommonEventTest for CRUD
125 * @tc.type: FUNC
126 */
127 HWTEST_F(AccountCommonEventTest, SubscribeAccountCommonEvent_004, TestSize.Level1)
128 {
129 std::shared_ptr<DpAccountCommonEventManager> accountCommonEventManager;
130 accountCommonEventManager = std::make_shared<DpAccountCommonEventManager>();
131 AccountEventCallback callback = nullptr;
132 std::vector<std::string> accountCommonEventVec;
133 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
134 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
135 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT);
136 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN);
137 bool ret = accountCommonEventManager->SubscribeAccountCommonEvent(accountCommonEventVec, callback);
138 EXPECT_EQ(ret, false);
139 }
140
141 /*
142 * @tc.name: SubscribeAccountCommonEvent
143 * @tc.desc: Normal testCase of AccountCommonEventTest for CRUD
144 * @tc.type: FUNC
145 */
146 HWTEST_F(AccountCommonEventTest, SubscribeAccountCommonEvent_005, TestSize.Level1)
147 {
148 std::shared_ptr<DpAccountCommonEventManager> accountCommonEventManager;
149 accountCommonEventManager = std::make_shared<DpAccountCommonEventManager>();
150 AccountEventCallback callback = nullptr;
151 std::vector<std::string> accountCommonEventVec;
152 accountCommonEventVec.emplace_back("11111");
153 bool ret = accountCommonEventManager->SubscribeAccountCommonEvent(accountCommonEventVec, callback);
154 EXPECT_EQ(ret, false);
155 }
156
157 /*
158 * @tc.name: UnsubscribeAccountCommonEvent
159 * @tc.desc: Normal testCase of AccountCommonEventTest for CRUD
160 * @tc.type: FUNC
161 */
162 HWTEST_F(AccountCommonEventTest, UnsubscribeAccountCommonEvent_001, TestSize.Level1)
163 {
164 std::shared_ptr<DpAccountCommonEventManager> accountCommonEventManager;
165 accountCommonEventManager = std::make_shared<DpAccountCommonEventManager>();
166 accountCommonEventManager->eventValidFlag_ = false;
167 bool ret = accountCommonEventManager->UnsubscribeAccountCommonEvent();
168 EXPECT_EQ(ret, false);
169 }
170
171 /*
172 * @tc.name: UnsubscribeAccountCommonEvent
173 * @tc.desc: Normal testCase of AccountCommonEventTest for CRUD
174 * @tc.type: FUNC
175 */
176 HWTEST_F(AccountCommonEventTest, UnsubscribeAccountCommonEvent_002, TestSize.Level1)
177 {
178 std::shared_ptr<DpAccountCommonEventManager> accountCommonEventManager;
179 accountCommonEventManager = std::make_shared<DpAccountCommonEventManager>();
__anonb51446270502(const auto &arg1, const auto &arg2) 180 AccountEventCallback callback = [=](const auto &arg1, const auto &arg2) {
181 DistributedDeviceProfileServiceNew::GetInstance().AccountCommonEventCallback(arg1, arg2);
182 };
183 std::vector<std::string> accountCommonEventVec;
184 accountCommonEventManager->eventValidFlag_ = false;
185 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
186 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
187 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT);
188 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN);
189 accountCommonEventManager->SubscribeAccountCommonEvent(accountCommonEventVec, callback);
190 accountCommonEventManager->subscriber_ = nullptr;
191 bool ret = accountCommonEventManager->UnsubscribeAccountCommonEvent();
192 EXPECT_EQ(ret, true);
193 }
194
195 /*
196 * @tc.name: UnsubscribeAccountCommonEvent
197 * @tc.desc: Normal testCase of AccountCommonEventTest for CRUD
198 * @tc.type: FUNC
199 */
200 HWTEST_F(AccountCommonEventTest, UnsubscribeAccountCommonEvent_003, TestSize.Level1)
201 {
202 std::shared_ptr<DpAccountCommonEventManager> accountCommonEventManager;
203 accountCommonEventManager = std::make_shared<DpAccountCommonEventManager>();
204 accountCommonEventManager->eventValidFlag_ = true;
__anonb51446270602(const auto &arg1, const auto &arg2) 205 AccountEventCallback callback = [=](const auto &arg1, const auto &arg2) {
206 DistributedDeviceProfileServiceNew::GetInstance().AccountCommonEventCallback(arg1, arg2);
207 };
208 std::vector<std::string> accountCommonEventVec;
209 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
210 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
211 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT);
212 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN);
213 EventFwk::MatchingSkills matchingSkills;
214 for (auto &item : accountCommonEventVec) {
215 matchingSkills.AddEvent(item);
216 }
217 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
218 accountCommonEventManager->subscriber_ =
219 std::make_shared<DpAccountEventSubscriber>(subscriberInfo, callback, accountCommonEventVec);
220 accountCommonEventManager->statusChangeListener_ = nullptr;
221 bool ret = accountCommonEventManager->UnsubscribeAccountCommonEvent();
222 EXPECT_EQ(ret, true);
223 }
224
225 /*
226 * @tc.name: UnsubscribeAccountCommonEvent
227 * @tc.desc: Normal testCase of AccountCommonEventTest for CRUD
228 * @tc.type: FUNC
229 */
230 HWTEST_F(AccountCommonEventTest, UnsubscribeAccountCommonEvent_004, TestSize.Level1)
231 {
232 std::shared_ptr<DpAccountCommonEventManager> accountCommonEventManager;
233 accountCommonEventManager = std::make_shared<DpAccountCommonEventManager>();
234 accountCommonEventManager->eventValidFlag_ = true;
__anonb51446270702(const auto &arg1, const auto &arg2) 235 AccountEventCallback callback = [=](const auto &arg1, const auto &arg2) {
236 DistributedDeviceProfileServiceNew::GetInstance().AccountCommonEventCallback(arg1, arg2);
237 };
238 std::vector<std::string> accountCommonEventVec;
239 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
240 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
241 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT);
242 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN);
243 EventFwk::MatchingSkills matchingSkills;
244 for (auto &item : accountCommonEventVec) {
245 matchingSkills.AddEvent(item);
246 }
247 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
248 accountCommonEventManager->subscriber_ =
249 std::make_shared<DpAccountEventSubscriber>(subscriberInfo, callback, accountCommonEventVec);
250 accountCommonEventManager->statusChangeListener_ =
251 new (std::nothrow) DpAccountCommonEventManager::
252 SystemAbilityStatusChangeListener(accountCommonEventManager->subscriber_);
253 bool ret = accountCommonEventManager->UnsubscribeAccountCommonEvent();
254 EXPECT_EQ(ret, true);
255 }
256
257 /*
258 * @tc.name: UnsubscribeAccountCommonEvent
259 * @tc.desc: Normal testCase of AccountCommonEventTest for CRUD
260 * @tc.type: FUNC
261 */
262 HWTEST_F(AccountCommonEventTest, UnsubscribeAccountCommonEvent_005, TestSize.Level1)
263 {
264 std::shared_ptr<DpAccountCommonEventManager> accountCommonEventManager;
265 accountCommonEventManager = std::make_shared<DpAccountCommonEventManager>();
__anonb51446270802(const auto &arg1, const auto &arg2) 266 AccountEventCallback callback = [=](const auto &arg1, const auto &arg2) {
267 DistributedDeviceProfileServiceNew::GetInstance().AccountCommonEventCallback(arg1, arg2);
268 };
269 std::vector<std::string> accountCommonEventVec;
270 accountCommonEventManager->eventValidFlag_ = false;
271 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
272 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED);
273 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGOUT);
274 accountCommonEventVec.emplace_back(EventFwk::CommonEventSupport::COMMON_EVENT_HWID_LOGIN);
275 accountCommonEventManager->SubscribeAccountCommonEvent(accountCommonEventVec, callback);
276 bool ret = accountCommonEventManager->UnsubscribeAccountCommonEvent();
277 EXPECT_EQ(ret, true);
278 }
279 } // namespace DistributedDeviceProfile
280 } // namespace OHOS