1 /*
2  * Copyright (c) 2022 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 
18 #define private public
19 #include "connection_observer_controller.h"
20 #undef private
21 #include "connection_observer_errors.h"
22 
23 using namespace testing::ext;
24 using namespace OHOS::AbilityRuntime;
25 
26 namespace OHOS {
27 namespace AAFwk {
28 class ConnectionObserverControllerTest : public testing::Test {
29 public:
30     static void SetUpTestCase(void);
31     static void TearDownTestCase(void);
32     void SetUp();
33     void TearDown();
34 };
35 
SetUpTestCase(void)36 void ConnectionObserverControllerTest::SetUpTestCase(void)
37 {}
TearDownTestCase(void)38 void ConnectionObserverControllerTest::TearDownTestCase(void)
39 {}
SetUp(void)40 void ConnectionObserverControllerTest::SetUp(void)
41 {}
TearDown(void)42 void ConnectionObserverControllerTest::TearDown(void)
43 {}
44 
45 /*
46  * Feature: ConnectionObserverController
47  * Function: AddObserver
48  * SubFunction: NA
49  * FunctionPoints: ConnectionObserverController AddObserver
50  * EnvConditions: NA
51  * CaseDescription: Verify AddObserver
52  */
53 HWTEST_F(ConnectionObserverControllerTest, AddObserver_001, TestSize.Level1)
54 {
55     auto connectionObserverController = std::make_shared<ConnectionObserverController>();
56     sptr<IConnectionObserver> observer = nullptr;
57     auto res = connectionObserverController->AddObserver(observer);
58     EXPECT_EQ(res, ERR_INVALID_OBSERVER);
59 }
60 
61 /*
62  * Feature: ConnectionObserverController
63  * Function: RemoveObserver
64  * SubFunction: NA
65  * FunctionPoints: ConnectionObserverController RemoveObserver
66  * EnvConditions: NA
67  * CaseDescription: Verify RemoveObserver
68  */
69 HWTEST_F(ConnectionObserverControllerTest, RemoveObserver_001, TestSize.Level1)
70 {
71     auto connectionObserverController = std::make_shared<ConnectionObserverController>();
72     ASSERT_NE(connectionObserverController, nullptr);
73     sptr<IConnectionObserver> observer = nullptr;
74     connectionObserverController->RemoveObserver(observer);
75 }
76 
77 /*
78  * Feature: ConnectionObserverController
79  * Function: NotifyExtensionConnected
80  * SubFunction: NA
81  * FunctionPoints: ConnectionObserverController NotifyExtensionConnected
82  * EnvConditions: NA
83  * CaseDescription: Verify NotifyExtensionConnected
84  */
85 HWTEST_F(ConnectionObserverControllerTest, NotifyExtensionConnected_001, TestSize.Level1)
86 {
87     auto connectionObserverController = std::make_shared<ConnectionObserverController>();
88     ASSERT_NE(connectionObserverController, nullptr);
89     ConnectionData data;
90     connectionObserverController->NotifyExtensionConnected(data);
91 }
92 
93 /*
94  * Feature: ConnectionObserverController
95  * Function: NotifyExtensionDisconnected
96  * SubFunction: NA
97  * FunctionPoints: ConnectionObserverController NotifyExtensionDisconnected
98  * EnvConditions: NA
99  * CaseDescription: Verify NotifyExtensionDisconnected
100  */
101 HWTEST_F(ConnectionObserverControllerTest, NotifyExtensionDisconnected_001, TestSize.Level1)
102 {
103     auto connectionObserverController = std::make_shared<ConnectionObserverController>();
104     ASSERT_NE(connectionObserverController, nullptr);
105     ConnectionData data;
106     connectionObserverController->NotifyExtensionDisconnected(data);
107 }
108 
109 #ifdef WITH_DLP
110 /*
111  * Feature: ConnectionObserverController
112  * Function: NotifyDlpAbilityOpened
113  * SubFunction: NA
114  * FunctionPoints: ConnectionObserverController NotifyDlpAbilityOpened
115  * EnvConditions: NA
116  * CaseDescription: Verify NotifyDlpAbilityOpened
117  */
118 HWTEST_F(ConnectionObserverControllerTest, NotifyDlpAbilityOpened_001, TestSize.Level1)
119 {
120     auto connectionObserverController = std::make_shared<ConnectionObserverController>();
121     ASSERT_NE(connectionObserverController, nullptr);
122     DlpStateData data;
123     connectionObserverController->NotifyDlpAbilityOpened(data);
124 }
125 
126 /*
127  * Feature: ConnectionObserverController
128  * Function: NotifyDlpAbilityClosed
129  * SubFunction: NA
130  * FunctionPoints: ConnectionObserverController NotifyDlpAbilityClosed
131  * EnvConditions: NA
132  * CaseDescription: Verify NotifyDlpAbilityClosed
133  */
134 HWTEST_F(ConnectionObserverControllerTest, NotifyDlpAbilityClosed_001, TestSize.Level1)
135 {
136     auto connectionObserverController = std::make_shared<ConnectionObserverController>();
137     ASSERT_NE(connectionObserverController, nullptr);
138     DlpStateData data;
139     connectionObserverController->NotifyDlpAbilityClosed(data);
140 }
141 #endif // WITH_DLP
142 
143 /*
144  * Feature: ConnectionObserverController
145  * Function: GetObservers
146  * SubFunction: NA
147  * FunctionPoints: ConnectionObserverController GetObservers
148  * EnvConditions: NA
149  * CaseDescription: Verify GetObservers
150  */
151 HWTEST_F(ConnectionObserverControllerTest, GetObservers_001, TestSize.Level1)
152 {
153     auto connectionObserverController = std::make_shared<ConnectionObserverController>();
154     std::vector<sptr<AbilityRuntime::IConnectionObserver>> observers;
155     auto res = connectionObserverController->GetObservers();
156     EXPECT_EQ(res, observers);
157 }
158 
159 /*
160  * Feature: ConnectionObserverController
161  * Function: HandleRemoteDied
162  * SubFunction: NA
163  * FunctionPoints: ConnectionObserverController HandleRemoteDied
164  * EnvConditions: NA
165  * CaseDescription: Verify HandleRemoteDied
166  */
167 HWTEST_F(ConnectionObserverControllerTest, HandleRemoteDied_001, TestSize.Level1)
168 {
169     auto connectionObserverController = std::make_shared<ConnectionObserverController>();
170     ASSERT_NE(connectionObserverController, nullptr);
171     wptr<IRemoteObject> remote;
172     connectionObserverController->HandleRemoteDied(remote);
173 }
174 
175 /*
176  * Feature: ObserverDeathRecipient
177  * Function: OnRemoteDied
178  * SubFunction: NA
179  * FunctionPoints: ConnectionObserverController OnRemoteDied
180  * EnvConditions: NA
181  * CaseDescription: Verify OnRemoteDied
182  */
183 HWTEST_F(ConnectionObserverControllerTest, OnRemoteDied_001, TestSize.Level1)
184 {
185     auto connectionObserverController = std::make_shared<ConnectionObserverController>();
186     ASSERT_NE(connectionObserverController, nullptr);
187     ConnectionObserverController::ObserverDeathRecipient::ObserverDeathHandler handler;
188     auto observerDeathRecipient = std::make_shared<ConnectionObserverController::ObserverDeathRecipient>(handler);
189     wptr<IRemoteObject> remote;
190     observerDeathRecipient->OnRemoteDied(remote);
191 }
192 }  // namespace AAFwk
193 }  // namespace OHOS
194