1c29fa5a6Sopenharmony_ci/*
2c29fa5a6Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3c29fa5a6Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c29fa5a6Sopenharmony_ci * you may not use this file except in compliance with the License.
5c29fa5a6Sopenharmony_ci * You may obtain a copy of the License at
6c29fa5a6Sopenharmony_ci *
7c29fa5a6Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8c29fa5a6Sopenharmony_ci *
9c29fa5a6Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c29fa5a6Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c29fa5a6Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c29fa5a6Sopenharmony_ci * See the License for the specific language governing permissions and
13c29fa5a6Sopenharmony_ci * limitations under the License.
14c29fa5a6Sopenharmony_ci */
15c29fa5a6Sopenharmony_ci
16c29fa5a6Sopenharmony_ci
17c29fa5a6Sopenharmony_ci#include <fstream>
18c29fa5a6Sopenharmony_ci#include <gtest/gtest.h>
19c29fa5a6Sopenharmony_ci
20c29fa5a6Sopenharmony_ci#include "input_event_handler.h"
21c29fa5a6Sopenharmony_ci#include "mmi_log.h"
22c29fa5a6Sopenharmony_ci#include "switch_subscriber_handler.h"
23c29fa5a6Sopenharmony_ci#include "uds_server.h"
24c29fa5a6Sopenharmony_ci
25c29fa5a6Sopenharmony_ci#undef MMI_LOG_TAG
26c29fa5a6Sopenharmony_ci#define MMI_LOG_TAG "SwitchSubscriberHandlerTest"
27c29fa5a6Sopenharmony_ci
28c29fa5a6Sopenharmony_cinamespace OHOS {
29c29fa5a6Sopenharmony_cinamespace MMI {
30c29fa5a6Sopenharmony_cinamespace {
31c29fa5a6Sopenharmony_ciusing namespace testing::ext;
32c29fa5a6Sopenharmony_ciconstexpr int32_t INVAID_VALUE = -1;
33c29fa5a6Sopenharmony_ciconstexpr int32_t SUBSCRIBER_ID = 0;
34c29fa5a6Sopenharmony_ciconstexpr int32_t POINTER_EVENT_TYPE = 3;
35c29fa5a6Sopenharmony_ciconstexpr int32_t SESSION_MODULE_TYPE = 3;
36c29fa5a6Sopenharmony_ciconstexpr int32_t SESSION_FD = -1;
37c29fa5a6Sopenharmony_ciconstexpr int32_t SESSION_UID = 0;
38c29fa5a6Sopenharmony_ciconstexpr int32_t SESSION_PID = 0;
39c29fa5a6Sopenharmony_ci} // namespace
40c29fa5a6Sopenharmony_ci
41c29fa5a6Sopenharmony_ciclass SwitchSubscriberHandlerTest : public testing::Test {
42c29fa5a6Sopenharmony_cipublic:
43c29fa5a6Sopenharmony_ci    static void SetUpTestCase(void) {}
44c29fa5a6Sopenharmony_ci    static void TearDownTestCase(void) {}
45c29fa5a6Sopenharmony_ci};
46c29fa5a6Sopenharmony_ci
47c29fa5a6Sopenharmony_ci/**
48c29fa5a6Sopenharmony_ci * @tc.name: SwitchSubscriberHandlerTest_HandleKeyEvent_001
49c29fa5a6Sopenharmony_ci * @tc.desc: Test HandleKeyEvent
50c29fa5a6Sopenharmony_ci * @tc.type: FUNC
51c29fa5a6Sopenharmony_ci * @tc.require:
52c29fa5a6Sopenharmony_ci */
53c29fa5a6Sopenharmony_ciHWTEST_F(SwitchSubscriberHandlerTest, SwitchSubscriberHandlerTest_HandleKeyEvent_001, TestSize.Level1)
54c29fa5a6Sopenharmony_ci{
55c29fa5a6Sopenharmony_ci    auto keyEvent = KeyEvent::Create();
56c29fa5a6Sopenharmony_ci    ASSERT_NE(keyEvent, nullptr);
57c29fa5a6Sopenharmony_ci    auto switchSubscriberHandler = std::make_shared<SwitchSubscriberHandler>();
58c29fa5a6Sopenharmony_ci    ASSERT_NE(switchSubscriberHandler, nullptr);
59c29fa5a6Sopenharmony_ci    auto nextSwitchSubscriberHandler = std::make_shared<SwitchSubscriberHandler>();
60c29fa5a6Sopenharmony_ci    ASSERT_NE(nextSwitchSubscriberHandler, nullptr);
61c29fa5a6Sopenharmony_ci
62c29fa5a6Sopenharmony_ci    switchSubscriberHandler->HandleKeyEvent(nullptr);
63c29fa5a6Sopenharmony_ci    switchSubscriberHandler->HandleKeyEvent(keyEvent);
64c29fa5a6Sopenharmony_ci    switchSubscriberHandler->nextHandler_ = nextSwitchSubscriberHandler;
65c29fa5a6Sopenharmony_ci    switchSubscriberHandler->HandleKeyEvent(keyEvent);
66c29fa5a6Sopenharmony_ci}
67c29fa5a6Sopenharmony_ci
68c29fa5a6Sopenharmony_ci/**
69c29fa5a6Sopenharmony_ci * @tc.name: SwitchSubscriberHandlerTest_HandlePointerEvent_001
70c29fa5a6Sopenharmony_ci * @tc.desc: Test HandlePointerEvent
71c29fa5a6Sopenharmony_ci * @tc.type: FUNC
72c29fa5a6Sopenharmony_ci * @tc.require:
73c29fa5a6Sopenharmony_ci */
74c29fa5a6Sopenharmony_ciHWTEST_F(SwitchSubscriberHandlerTest, SwitchSubscriberHandlerTest_HandlePointerEvent_001, TestSize.Level1)
75c29fa5a6Sopenharmony_ci{
76c29fa5a6Sopenharmony_ci    auto pointerEvent = std::make_shared<PointerEvent>(POINTER_EVENT_TYPE);
77c29fa5a6Sopenharmony_ci    ASSERT_NE(pointerEvent, nullptr);
78c29fa5a6Sopenharmony_ci    auto switchSubscriberHandler = std::make_shared<SwitchSubscriberHandler>();
79c29fa5a6Sopenharmony_ci    ASSERT_NE(switchSubscriberHandler, nullptr);
80c29fa5a6Sopenharmony_ci    auto nextSwitchSubscriberHandler = std::make_shared<SwitchSubscriberHandler>();
81c29fa5a6Sopenharmony_ci    ASSERT_NE(nextSwitchSubscriberHandler, nullptr);
82c29fa5a6Sopenharmony_ci
83c29fa5a6Sopenharmony_ci    switchSubscriberHandler->HandlePointerEvent(nullptr);
84c29fa5a6Sopenharmony_ci    switchSubscriberHandler->HandlePointerEvent(pointerEvent);
85c29fa5a6Sopenharmony_ci    switchSubscriberHandler->nextHandler_ = nextSwitchSubscriberHandler;
86c29fa5a6Sopenharmony_ci    switchSubscriberHandler->HandlePointerEvent(pointerEvent);
87c29fa5a6Sopenharmony_ci}
88c29fa5a6Sopenharmony_ci
89c29fa5a6Sopenharmony_ci/**
90c29fa5a6Sopenharmony_ci * @tc.name: SwitchSubscriberHandlerTest_HandleTouchEvent_001
91c29fa5a6Sopenharmony_ci * @tc.desc: Test HandleTouchEvent
92c29fa5a6Sopenharmony_ci * @tc.type: FUNC
93c29fa5a6Sopenharmony_ci * @tc.require:
94c29fa5a6Sopenharmony_ci */
95c29fa5a6Sopenharmony_ciHWTEST_F(SwitchSubscriberHandlerTest, SwitchSubscriberHandlerTest_HandleTouchEvent_001, TestSize.Level1)
96c29fa5a6Sopenharmony_ci{
97c29fa5a6Sopenharmony_ci    auto pointerEvent = std::make_shared<PointerEvent>(POINTER_EVENT_TYPE);
98c29fa5a6Sopenharmony_ci    ASSERT_NE(pointerEvent, nullptr);
99c29fa5a6Sopenharmony_ci    auto switchSubscriberHandler = std::make_shared<SwitchSubscriberHandler>();
100c29fa5a6Sopenharmony_ci    ASSERT_NE(switchSubscriberHandler, nullptr);
101c29fa5a6Sopenharmony_ci    auto nextSwitchSubscriberHandler = std::make_shared<SwitchSubscriberHandler>();
102c29fa5a6Sopenharmony_ci    ASSERT_NE(nextSwitchSubscriberHandler, nullptr);
103c29fa5a6Sopenharmony_ci
104c29fa5a6Sopenharmony_ci    switchSubscriberHandler->HandleTouchEvent(nullptr);
105c29fa5a6Sopenharmony_ci    switchSubscriberHandler->HandleTouchEvent(pointerEvent);
106c29fa5a6Sopenharmony_ci    switchSubscriberHandler->nextHandler_ = nextSwitchSubscriberHandler;
107c29fa5a6Sopenharmony_ci    switchSubscriberHandler->HandleTouchEvent(pointerEvent);
108c29fa5a6Sopenharmony_ci}
109c29fa5a6Sopenharmony_ci
110c29fa5a6Sopenharmony_ci/**
111c29fa5a6Sopenharmony_ci * @tc.name: SwitchSubscriberHandlerTest_HandleSwitchEvent_001
112c29fa5a6Sopenharmony_ci * @tc.desc: Test HandleSwitchEvent
113c29fa5a6Sopenharmony_ci * @tc.type: FUNC
114c29fa5a6Sopenharmony_ci * @tc.require:
115c29fa5a6Sopenharmony_ci */
116c29fa5a6Sopenharmony_ciHWTEST_F(SwitchSubscriberHandlerTest, SwitchSubscriberHandlerTest_HandleSwitchEvent_001, TestSize.Level1)
117c29fa5a6Sopenharmony_ci{
118c29fa5a6Sopenharmony_ci    auto switchEvent = std::make_shared<SwitchEvent>(INVAID_VALUE);
119c29fa5a6Sopenharmony_ci    ASSERT_NE(switchEvent, nullptr);
120c29fa5a6Sopenharmony_ci    auto switchSubscriberHandler = std::make_shared<SwitchSubscriberHandler>();
121c29fa5a6Sopenharmony_ci    ASSERT_NE(switchSubscriberHandler, nullptr);
122c29fa5a6Sopenharmony_ci    auto nextSwitchSubscriberHandler = std::make_shared<SwitchSubscriberHandler>();
123c29fa5a6Sopenharmony_ci    ASSERT_NE(nextSwitchSubscriberHandler, nullptr);
124c29fa5a6Sopenharmony_ci
125c29fa5a6Sopenharmony_ci    switchSubscriberHandler->HandleSwitchEvent(nullptr);
126c29fa5a6Sopenharmony_ci    switchEvent->SetSwitchType(SwitchEvent::SwitchType::SWITCH_PRIVACY);
127c29fa5a6Sopenharmony_ci    switchEvent->SetSwitchValue(SwitchEvent::SWITCH_ON);
128c29fa5a6Sopenharmony_ci    switchSubscriberHandler->HandleSwitchEvent(switchEvent);
129c29fa5a6Sopenharmony_ci    switchSubscriberHandler->nextHandler_ = nextSwitchSubscriberHandler;
130c29fa5a6Sopenharmony_ci    switchSubscriberHandler->HandleSwitchEvent(switchEvent);
131c29fa5a6Sopenharmony_ci
132c29fa5a6Sopenharmony_ci    auto sess = std::make_shared<UDSSession>(
133c29fa5a6Sopenharmony_ci        "switch_subscriber_handler_test", SESSION_MODULE_TYPE, SESSION_FD, SESSION_UID, SESSION_PID);
134c29fa5a6Sopenharmony_ci    ASSERT_NE(sess, nullptr);
135c29fa5a6Sopenharmony_ci    auto subscriber = std::make_shared<SwitchSubscriberHandler::Subscriber>(
136c29fa5a6Sopenharmony_ci        SUBSCRIBER_ID, sess, SwitchEvent::SwitchType::SWITCH_PRIVACY);
137c29fa5a6Sopenharmony_ci    ASSERT_NE(subscriber, nullptr);
138c29fa5a6Sopenharmony_ci
139c29fa5a6Sopenharmony_ci    switchSubscriberHandler->InsertSubScriber(subscriber);
140c29fa5a6Sopenharmony_ci    switchSubscriberHandler->HandleSwitchEvent(switchEvent);
141c29fa5a6Sopenharmony_ci}
142c29fa5a6Sopenharmony_ci
143c29fa5a6Sopenharmony_ci/**
144c29fa5a6Sopenharmony_ci * @tc.name: SwitchSubscriberHandlerTest_SubscribeSwitchEvent_001
145c29fa5a6Sopenharmony_ci * @tc.desc: Test SubscribeSwitchEvent
146c29fa5a6Sopenharmony_ci * @tc.type: FUNC
147c29fa5a6Sopenharmony_ci * @tc.require:
148c29fa5a6Sopenharmony_ci */
149c29fa5a6Sopenharmony_ciHWTEST_F(SwitchSubscriberHandlerTest, SwitchSubscriberHandlerTest_SubscribeSwitchEvent_001, TestSize.Level1)
150c29fa5a6Sopenharmony_ci{
151c29fa5a6Sopenharmony_ci    auto switchSubscriberHandler = std::make_shared<SwitchSubscriberHandler>();
152c29fa5a6Sopenharmony_ci    ASSERT_NE(switchSubscriberHandler, nullptr);
153c29fa5a6Sopenharmony_ci
154c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->SubscribeSwitchEvent(
155c29fa5a6Sopenharmony_ci        nullptr, INVAID_VALUE, SwitchEvent::SwitchType::SWITCH_DEFAULT), RET_ERR);
156c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->SubscribeSwitchEvent(nullptr, SUBSCRIBER_ID, INVAID_VALUE), RET_ERR);
157c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->SubscribeSwitchEvent(
158c29fa5a6Sopenharmony_ci        nullptr, SUBSCRIBER_ID, SwitchEvent::SwitchType::SWITCH_DEFAULT), ERROR_NULL_POINTER);
159c29fa5a6Sopenharmony_ci    auto sess = std::make_shared<UDSSession>(
160c29fa5a6Sopenharmony_ci        "switch_subscriber_handler_test", SESSION_MODULE_TYPE, SESSION_FD, SESSION_UID, SESSION_PID);
161c29fa5a6Sopenharmony_ci    ASSERT_NE(sess, nullptr);
162c29fa5a6Sopenharmony_ci
163c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->SubscribeSwitchEvent(
164c29fa5a6Sopenharmony_ci        sess, SUBSCRIBER_ID, SwitchEvent::SwitchType::SWITCH_PRIVACY), RET_OK);
165c29fa5a6Sopenharmony_ci}
166c29fa5a6Sopenharmony_ci
167c29fa5a6Sopenharmony_ci/**
168c29fa5a6Sopenharmony_ci * @tc.name: SwitchSubscriberHandlerTest_UnsubscribeSwitchEvent_001
169c29fa5a6Sopenharmony_ci * @tc.desc: Test UnsubscribeSwitchEvent
170c29fa5a6Sopenharmony_ci * @tc.type: FUNC
171c29fa5a6Sopenharmony_ci * @tc.require:
172c29fa5a6Sopenharmony_ci */
173c29fa5a6Sopenharmony_ciHWTEST_F(SwitchSubscriberHandlerTest, SwitchSubscriberHandlerTest_UnsubscribeSwitchEvent_001, TestSize.Level1)
174c29fa5a6Sopenharmony_ci{
175c29fa5a6Sopenharmony_ci    auto firstSess = std::make_shared<UDSSession>(
176c29fa5a6Sopenharmony_ci        "switch_subscriber_handler_test", SESSION_MODULE_TYPE, SESSION_FD, SESSION_UID, SESSION_PID);
177c29fa5a6Sopenharmony_ci    ASSERT_NE(firstSess, nullptr);
178c29fa5a6Sopenharmony_ci    auto secondSess = std::make_shared<UDSSession>(
179c29fa5a6Sopenharmony_ci        "switch_subscriber_handler_test", SESSION_MODULE_TYPE, SESSION_FD, SESSION_UID, SESSION_PID);
180c29fa5a6Sopenharmony_ci    ASSERT_NE(secondSess, nullptr);
181c29fa5a6Sopenharmony_ci    auto switchEvent = std::make_shared<SwitchEvent>(SwitchEvent::SwitchType::SWITCH_DEFAULT);
182c29fa5a6Sopenharmony_ci    ASSERT_NE(switchEvent, nullptr);
183c29fa5a6Sopenharmony_ci    auto switchSubscriberHandler = std::make_shared<SwitchSubscriberHandler>();
184c29fa5a6Sopenharmony_ci    ASSERT_NE(switchSubscriberHandler, nullptr);
185c29fa5a6Sopenharmony_ci
186c29fa5a6Sopenharmony_ci    switchEvent->SetSwitchType(SwitchEvent::SwitchType::SWITCH_LID);
187c29fa5a6Sopenharmony_ci    switchEvent->SetSwitchValue(SwitchEvent::SWITCH_ON);
188c29fa5a6Sopenharmony_ci
189c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->UnsubscribeSwitchEvent(firstSess, SUBSCRIBER_ID), RET_ERR);
190c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->SubscribeSwitchEvent(firstSess, SUBSCRIBER_ID,
191c29fa5a6Sopenharmony_ci        SwitchEvent::SwitchType::SWITCH_DEFAULT), RET_OK);
192c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->SubscribeSwitchEvent(secondSess, SUBSCRIBER_ID,
193c29fa5a6Sopenharmony_ci        SwitchEvent::SwitchType::SWITCH_DEFAULT), RET_OK);
194c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->UnsubscribeSwitchEvent(firstSess, SUBSCRIBER_ID), RET_OK);
195c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->UnsubscribeSwitchEvent(firstSess, SUBSCRIBER_ID), RET_ERR);
196c29fa5a6Sopenharmony_ci}
197c29fa5a6Sopenharmony_ci
198c29fa5a6Sopenharmony_ci/**
199c29fa5a6Sopenharmony_ci * @tc.name: SwitchSubscriberHandlerTest_OnSubscribeSwitchEvent_001
200c29fa5a6Sopenharmony_ci * @tc.desc: Test OnSubscribeSwitchEvent
201c29fa5a6Sopenharmony_ci * @tc.type: FUNC
202c29fa5a6Sopenharmony_ci * @tc.require:
203c29fa5a6Sopenharmony_ci */
204c29fa5a6Sopenharmony_ciHWTEST_F(SwitchSubscriberHandlerTest, SwitchSubscriberHandlerTest_OnSubscribeSwitchEvent_001, TestSize.Level1)
205c29fa5a6Sopenharmony_ci{
206c29fa5a6Sopenharmony_ci    auto sess = std::make_shared<UDSSession>(
207c29fa5a6Sopenharmony_ci        "switch_subscriber_handler_test", SESSION_MODULE_TYPE, SESSION_FD, SESSION_UID, SESSION_PID);
208c29fa5a6Sopenharmony_ci    ASSERT_NE(sess, nullptr);
209c29fa5a6Sopenharmony_ci    auto switchEvent = std::make_shared<SwitchEvent>(SwitchEvent::SwitchType::SWITCH_DEFAULT);
210c29fa5a6Sopenharmony_ci    ASSERT_NE(switchEvent, nullptr);
211c29fa5a6Sopenharmony_ci    auto switchSubscriberHandler = std::make_shared<SwitchSubscriberHandler>();
212c29fa5a6Sopenharmony_ci    ASSERT_NE(switchSubscriberHandler, nullptr);
213c29fa5a6Sopenharmony_ci
214c29fa5a6Sopenharmony_ci    switchEvent->SetSwitchType(SwitchEvent::SwitchType::SWITCH_PRIVACY);
215c29fa5a6Sopenharmony_ci    switchEvent->SetSwitchValue(SwitchEvent::SWITCH_ON);
216c29fa5a6Sopenharmony_ci    switchEvent->SetSwitchMask(INVAID_VALUE);
217c29fa5a6Sopenharmony_ci    switchEvent->GetSwitchMask();
218c29fa5a6Sopenharmony_ci
219c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->OnSubscribeSwitchEvent(nullptr), false);
220c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->SubscribeSwitchEvent(sess, SUBSCRIBER_ID,
221c29fa5a6Sopenharmony_ci        SwitchEvent::SwitchType::SWITCH_PRIVACY), RET_OK);
222c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->OnSubscribeSwitchEvent(switchEvent), true);
223c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->UnsubscribeSwitchEvent(sess, SUBSCRIBER_ID), RET_OK);
224c29fa5a6Sopenharmony_ci}
225c29fa5a6Sopenharmony_ci
226c29fa5a6Sopenharmony_ci/**
227c29fa5a6Sopenharmony_ci * @tc.name: SwitchSubscriberHandlerTest_OnSubscribeSwitchEvent_002
228c29fa5a6Sopenharmony_ci * @tc.desc: Test OnSubscribeSwitchEvent
229c29fa5a6Sopenharmony_ci * @tc.type: FUNC
230c29fa5a6Sopenharmony_ci * @tc.require:
231c29fa5a6Sopenharmony_ci */
232c29fa5a6Sopenharmony_ciHWTEST_F(SwitchSubscriberHandlerTest, SwitchSubscriberHandlerTest_OnSubscribeSwitchEvent_002, TestSize.Level1)
233c29fa5a6Sopenharmony_ci{
234c29fa5a6Sopenharmony_ci    auto sess = std::make_shared<UDSSession>(
235c29fa5a6Sopenharmony_ci        "switch_subscriber_handler_test", SESSION_MODULE_TYPE, SESSION_FD, SESSION_UID, SESSION_PID);
236c29fa5a6Sopenharmony_ci    ASSERT_NE(sess, nullptr);
237c29fa5a6Sopenharmony_ci    auto switchEvent = std::make_shared<SwitchEvent>(SwitchEvent::SwitchType::SWITCH_DEFAULT);
238c29fa5a6Sopenharmony_ci    ASSERT_NE(switchEvent, nullptr);
239c29fa5a6Sopenharmony_ci    auto switchSubscriberHandler = std::make_shared<SwitchSubscriberHandler>();
240c29fa5a6Sopenharmony_ci    ASSERT_NE(switchSubscriberHandler, nullptr);
241c29fa5a6Sopenharmony_ci
242c29fa5a6Sopenharmony_ci    switchEvent->SetSwitchType(SwitchEvent::SwitchType::SWITCH_LID);
243c29fa5a6Sopenharmony_ci    switchEvent->SetSwitchValue(SwitchEvent::SWITCH_ON);
244c29fa5a6Sopenharmony_ci
245c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->SubscribeSwitchEvent(sess, SUBSCRIBER_ID,
246c29fa5a6Sopenharmony_ci        SwitchEvent::SwitchType::SWITCH_DEFAULT), RET_OK);
247c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->OnSubscribeSwitchEvent(switchEvent), true);
248c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->UnsubscribeSwitchEvent(sess, SUBSCRIBER_ID), RET_OK);
249c29fa5a6Sopenharmony_ci}
250c29fa5a6Sopenharmony_ci
251c29fa5a6Sopenharmony_ci/**
252c29fa5a6Sopenharmony_ci * @tc.name: SwitchSubscriberHandlerTest_OnSubscribeSwitchEvent_003
253c29fa5a6Sopenharmony_ci * @tc.desc: Test OnSubscribeSwitchEvent
254c29fa5a6Sopenharmony_ci * @tc.type: FUNC
255c29fa5a6Sopenharmony_ci * @tc.require:
256c29fa5a6Sopenharmony_ci */
257c29fa5a6Sopenharmony_ciHWTEST_F(SwitchSubscriberHandlerTest, SwitchSubscriberHandlerTest_OnSubscribeSwitchEvent_003, TestSize.Level1)
258c29fa5a6Sopenharmony_ci{
259c29fa5a6Sopenharmony_ci    auto sess = std::make_shared<UDSSession>(
260c29fa5a6Sopenharmony_ci        "switch_subscriber_handler_test", SESSION_MODULE_TYPE, SESSION_FD, SESSION_UID, SESSION_PID);
261c29fa5a6Sopenharmony_ci    ASSERT_NE(sess, nullptr);
262c29fa5a6Sopenharmony_ci    auto switchEvent = std::make_shared<SwitchEvent>(SwitchEvent::SwitchType::SWITCH_DEFAULT);
263c29fa5a6Sopenharmony_ci    ASSERT_NE(switchEvent, nullptr);
264c29fa5a6Sopenharmony_ci    auto switchSubscriberHandler = std::make_shared<SwitchSubscriberHandler>();
265c29fa5a6Sopenharmony_ci    ASSERT_NE(switchSubscriberHandler, nullptr);
266c29fa5a6Sopenharmony_ci
267c29fa5a6Sopenharmony_ci    switchEvent->SetSwitchType(SwitchEvent::SwitchType::SWITCH_PRIVACY);
268c29fa5a6Sopenharmony_ci    switchEvent->SetSwitchValue(SwitchEvent::SWITCH_ON);
269c29fa5a6Sopenharmony_ci
270c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->SubscribeSwitchEvent(sess, SUBSCRIBER_ID,
271c29fa5a6Sopenharmony_ci        SwitchEvent::SwitchType::SWITCH_DEFAULT), RET_OK);
272c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->OnSubscribeSwitchEvent(switchEvent), false);
273c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->UnsubscribeSwitchEvent(sess, SUBSCRIBER_ID), RET_OK);
274c29fa5a6Sopenharmony_ci}
275c29fa5a6Sopenharmony_ci
276c29fa5a6Sopenharmony_ci/**
277c29fa5a6Sopenharmony_ci * @tc.name: SwitchSubscriberHandlerTest_InsertSubScriber_001
278c29fa5a6Sopenharmony_ci * @tc.desc: Test InsertSubScriber
279c29fa5a6Sopenharmony_ci * @tc.type: FUNC
280c29fa5a6Sopenharmony_ci * @tc.require:
281c29fa5a6Sopenharmony_ci */
282c29fa5a6Sopenharmony_ciHWTEST_F(SwitchSubscriberHandlerTest, SwitchSubscriberHandlerTest_InsertSubScriber_001, TestSize.Level1)
283c29fa5a6Sopenharmony_ci{
284c29fa5a6Sopenharmony_ci    auto sess = std::make_shared<UDSSession>(
285c29fa5a6Sopenharmony_ci        "switch_subscriber_handler_test", SESSION_MODULE_TYPE, SESSION_FD, SESSION_UID, SESSION_PID);
286c29fa5a6Sopenharmony_ci    ASSERT_NE(sess, nullptr);
287c29fa5a6Sopenharmony_ci    auto switchSubscriberHandler = std::make_shared<SwitchSubscriberHandler>();
288c29fa5a6Sopenharmony_ci    ASSERT_NE(switchSubscriberHandler, nullptr);
289c29fa5a6Sopenharmony_ci    auto subscriber = std::make_shared<SwitchSubscriberHandler::Subscriber>(
290c29fa5a6Sopenharmony_ci        SUBSCRIBER_ID, sess, SwitchEvent::SwitchType::SWITCH_PRIVACY);
291c29fa5a6Sopenharmony_ci    ASSERT_NE(subscriber, nullptr);
292c29fa5a6Sopenharmony_ci    auto repeatSubscriber = std::make_shared<SwitchSubscriberHandler::Subscriber>(
293c29fa5a6Sopenharmony_ci        SUBSCRIBER_ID, sess, SwitchEvent::SwitchType::SWITCH_PRIVACY);
294c29fa5a6Sopenharmony_ci    ASSERT_NE(repeatSubscriber, nullptr);
295c29fa5a6Sopenharmony_ci
296c29fa5a6Sopenharmony_ci    switchSubscriberHandler->InsertSubScriber(nullptr);
297c29fa5a6Sopenharmony_ci    switchSubscriberHandler->InsertSubScriber(subscriber);
298c29fa5a6Sopenharmony_ci    switchSubscriberHandler->InsertSubScriber(repeatSubscriber);
299c29fa5a6Sopenharmony_ci    repeatSubscriber->sess_ = nullptr;
300c29fa5a6Sopenharmony_ci    switchSubscriberHandler->InsertSubScriber(repeatSubscriber);
301c29fa5a6Sopenharmony_ci}
302c29fa5a6Sopenharmony_ci
303c29fa5a6Sopenharmony_ci/**
304c29fa5a6Sopenharmony_ci * @tc.name: SwitchSubscriberHandlerTest_OnSessionDelete_001
305c29fa5a6Sopenharmony_ci * @tc.desc: Test OnSessionDelete
306c29fa5a6Sopenharmony_ci * @tc.type: FUNC
307c29fa5a6Sopenharmony_ci * @tc.require:
308c29fa5a6Sopenharmony_ci */
309c29fa5a6Sopenharmony_ciHWTEST_F(SwitchSubscriberHandlerTest, SwitchSubscriberHandlerTest_OnSessionDelete_001, TestSize.Level1)
310c29fa5a6Sopenharmony_ci{
311c29fa5a6Sopenharmony_ci    auto firstSess = std::make_shared<UDSSession>(
312c29fa5a6Sopenharmony_ci        "switch_subscriber_handler_test", SESSION_MODULE_TYPE, SESSION_FD, SESSION_UID, SESSION_PID);
313c29fa5a6Sopenharmony_ci    ASSERT_NE(firstSess, nullptr);
314c29fa5a6Sopenharmony_ci    auto secondSess = std::make_shared<UDSSession>(
315c29fa5a6Sopenharmony_ci        "switch_subscriber_handler_test", SESSION_MODULE_TYPE, SESSION_FD, SESSION_UID, SESSION_PID);
316c29fa5a6Sopenharmony_ci    ASSERT_NE(secondSess, nullptr);
317c29fa5a6Sopenharmony_ci    auto switchSubscriberHandler = std::make_shared<SwitchSubscriberHandler>();
318c29fa5a6Sopenharmony_ci    ASSERT_NE(switchSubscriberHandler, nullptr);
319c29fa5a6Sopenharmony_ci    auto firstSubscriber = std::make_shared<SwitchSubscriberHandler::Subscriber>(
320c29fa5a6Sopenharmony_ci        SUBSCRIBER_ID, firstSess, SwitchEvent::SwitchType::SWITCH_PRIVACY);
321c29fa5a6Sopenharmony_ci    ASSERT_NE(firstSubscriber, nullptr);
322c29fa5a6Sopenharmony_ci    auto secondSubscriber = std::make_shared<SwitchSubscriberHandler::Subscriber>(
323c29fa5a6Sopenharmony_ci        SUBSCRIBER_ID, secondSess, SwitchEvent::SwitchType::SWITCH_PRIVACY);
324c29fa5a6Sopenharmony_ci    ASSERT_NE(secondSubscriber, nullptr);
325c29fa5a6Sopenharmony_ci
326c29fa5a6Sopenharmony_ci    switchSubscriberHandler->OnSessionDelete(firstSess);
327c29fa5a6Sopenharmony_ci    switchSubscriberHandler->InsertSubScriber(firstSubscriber);
328c29fa5a6Sopenharmony_ci    switchSubscriberHandler->InsertSubScriber(secondSubscriber);
329c29fa5a6Sopenharmony_ci    switchSubscriberHandler->OnSessionDelete(nullptr);
330c29fa5a6Sopenharmony_ci    switchSubscriberHandler->OnSessionDelete(secondSess);
331c29fa5a6Sopenharmony_ci}
332c29fa5a6Sopenharmony_ci
333c29fa5a6Sopenharmony_ci/**
334c29fa5a6Sopenharmony_ci * @tc.name: SwitchSubscriberHandlerTest_NotifySubscriber_001
335c29fa5a6Sopenharmony_ci * @tc.desc: Test NotifySubscriber
336c29fa5a6Sopenharmony_ci * @tc.type: FUNC
337c29fa5a6Sopenharmony_ci * @tc.require:
338c29fa5a6Sopenharmony_ci */
339c29fa5a6Sopenharmony_ciHWTEST_F(SwitchSubscriberHandlerTest, SwitchSubscriberHandlerTest_NotifySubscriber_001, TestSize.Level1)
340c29fa5a6Sopenharmony_ci{
341c29fa5a6Sopenharmony_ci    auto switchEvent = std::make_shared<SwitchEvent>(SwitchEvent::SwitchType::SWITCH_DEFAULT);
342c29fa5a6Sopenharmony_ci    ASSERT_NE(switchEvent, nullptr);
343c29fa5a6Sopenharmony_ci    auto sess = std::make_shared<UDSSession>(
344c29fa5a6Sopenharmony_ci        "switch_subscriber_handler_test", SESSION_MODULE_TYPE, SESSION_FD, SESSION_UID, SESSION_PID);
345c29fa5a6Sopenharmony_ci    ASSERT_NE(sess, nullptr);
346c29fa5a6Sopenharmony_ci    auto subscriber = std::make_shared<SwitchSubscriberHandler::Subscriber>(
347c29fa5a6Sopenharmony_ci        SUBSCRIBER_ID, sess, SwitchEvent::SwitchType::SWITCH_PRIVACY);
348c29fa5a6Sopenharmony_ci    ASSERT_NE(subscriber, nullptr);
349c29fa5a6Sopenharmony_ci    auto switchSubscriberHandler = std::make_shared<SwitchSubscriberHandler>();
350c29fa5a6Sopenharmony_ci    ASSERT_NE(switchSubscriberHandler, nullptr);
351c29fa5a6Sopenharmony_ci
352c29fa5a6Sopenharmony_ci    switchSubscriberHandler->NotifySubscriber(nullptr, subscriber);
353c29fa5a6Sopenharmony_ci    switchSubscriberHandler->NotifySubscriber(switchEvent, nullptr);
354c29fa5a6Sopenharmony_ci    switchSubscriberHandler->NotifySubscriber(switchEvent, subscriber);
355c29fa5a6Sopenharmony_ci
356c29fa5a6Sopenharmony_ci    UDSServer udsServer;
357c29fa5a6Sopenharmony_ci    InputHandler->udsServer_ = &udsServer;
358c29fa5a6Sopenharmony_ci    switchSubscriberHandler->NotifySubscriber(switchEvent, subscriber);
359c29fa5a6Sopenharmony_ci    subscriber->sess_ = nullptr;
360c29fa5a6Sopenharmony_ci    switchSubscriberHandler->NotifySubscriber(switchEvent, subscriber);
361c29fa5a6Sopenharmony_ci    InputHandler->udsServer_ = nullptr;
362c29fa5a6Sopenharmony_ci}
363c29fa5a6Sopenharmony_ci
364c29fa5a6Sopenharmony_ci/**
365c29fa5a6Sopenharmony_ci * @tc.name: SwitchSubscriberHandlerTest_InitSessionDeleteCallback_001
366c29fa5a6Sopenharmony_ci * @tc.desc: Test InitSessionDeleteCallback
367c29fa5a6Sopenharmony_ci * @tc.type: FUNC
368c29fa5a6Sopenharmony_ci * @tc.require:
369c29fa5a6Sopenharmony_ci */
370c29fa5a6Sopenharmony_ciHWTEST_F(SwitchSubscriberHandlerTest, SwitchSubscriberHandlerTest_InitSessionDeleteCallback_001, TestSize.Level1)
371c29fa5a6Sopenharmony_ci{
372c29fa5a6Sopenharmony_ci    auto switchSubscriberHandler = std::make_shared<SwitchSubscriberHandler>();
373c29fa5a6Sopenharmony_ci    ASSERT_NE(switchSubscriberHandler, nullptr);
374c29fa5a6Sopenharmony_ci
375c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->InitSessionDeleteCallback(), false);
376c29fa5a6Sopenharmony_ci
377c29fa5a6Sopenharmony_ci    UDSServer udsServer;
378c29fa5a6Sopenharmony_ci    InputHandler->udsServer_ = &udsServer;
379c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->InitSessionDeleteCallback(), true);
380c29fa5a6Sopenharmony_ci    ASSERT_EQ(switchSubscriberHandler->InitSessionDeleteCallback(), true);
381c29fa5a6Sopenharmony_ci    InputHandler->udsServer_ = nullptr;
382c29fa5a6Sopenharmony_ci}
383c29fa5a6Sopenharmony_ci
384c29fa5a6Sopenharmony_ci/**
385c29fa5a6Sopenharmony_ci * @tc.name: SwitchSubscriberHandlerTest_Dump_001
386c29fa5a6Sopenharmony_ci * @tc.desc: Test Dump
387c29fa5a6Sopenharmony_ci * @tc.type: FUNC
388c29fa5a6Sopenharmony_ci * @tc.require:
389c29fa5a6Sopenharmony_ci */
390c29fa5a6Sopenharmony_ciHWTEST_F(SwitchSubscriberHandlerTest, SwitchSubscriberHandlerTest_Dump_001, TestSize.Level1)
391c29fa5a6Sopenharmony_ci{
392c29fa5a6Sopenharmony_ci    auto sess = std::make_shared<UDSSession>(
393c29fa5a6Sopenharmony_ci        "switch_subscriber_handler_test", SESSION_MODULE_TYPE, SESSION_FD, SESSION_UID, SESSION_PID);
394c29fa5a6Sopenharmony_ci    ASSERT_NE(sess, nullptr);
395c29fa5a6Sopenharmony_ci    auto subscriber = std::make_shared<SwitchSubscriberHandler::Subscriber>(
396c29fa5a6Sopenharmony_ci        SUBSCRIBER_ID, sess, SwitchEvent::SwitchType::SWITCH_PRIVACY);
397c29fa5a6Sopenharmony_ci    ASSERT_NE(subscriber, nullptr);
398c29fa5a6Sopenharmony_ci    auto switchSubscriberHandler = std::make_shared<SwitchSubscriberHandler>();
399c29fa5a6Sopenharmony_ci    ASSERT_NE(switchSubscriberHandler, nullptr);
400c29fa5a6Sopenharmony_ci
401c29fa5a6Sopenharmony_ci    switchSubscriberHandler->InsertSubScriber(subscriber);
402c29fa5a6Sopenharmony_ci
403c29fa5a6Sopenharmony_ci    std::vector<std::string> args;
404c29fa5a6Sopenharmony_ci    args.push_back("args0");
405c29fa5a6Sopenharmony_ci    args.push_back("args1");
406c29fa5a6Sopenharmony_ci
407c29fa5a6Sopenharmony_ci    switchSubscriberHandler->Dump(INVAID_VALUE, args);
408c29fa5a6Sopenharmony_ci}
409c29fa5a6Sopenharmony_ci
410c29fa5a6Sopenharmony_ci} // namespace MMI
411c29fa5a6Sopenharmony_ci} // namespace OHOS
412