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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18
19 #include "iremote_object_mocker.h"
20 #include "window_extension_connection.cpp"
21 #include "window_extension_connection.h"
22 #include "wm_common.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace Rosen {
29
30 class MockWindowExtensionProxy : public IRemoteProxy<IWindowExtension> {
31 public:
MockWindowExtensionProxy(const sptr<IRemoteObject> & impl)32 explicit MockWindowExtensionProxy(const sptr<IRemoteObject>& impl) : IRemoteProxy<IWindowExtension>(impl) {};
~MockWindowExtensionProxy()33 ~MockWindowExtensionProxy() {};
34
35 MOCK_METHOD(void, SetBounds, (const Rect& rect), (override));
36 MOCK_METHOD(void, Hide, (), (override));
37 MOCK_METHOD(void, Show, (), (override));
38 MOCK_METHOD(void, RequestFocus, (), (override));
39 MOCK_METHOD(void, GetExtensionWindow, (sptr<IWindowExtensionClient> & token), (override));
40 };
41
42 class ExtensionCallback : public Rosen::IWindowExtensionCallback {
43 public:
44 ExtensionCallback() = default;
45 ~ExtensionCallback() = default;
46 void OnWindowReady(const std::shared_ptr<Rosen::RSSurfaceNode>& rsSurfaceNode) override;
47 void OnExtensionDisconnected() override;
48 void OnKeyEvent(const std::shared_ptr<MMI::KeyEvent>& event) override;
49 void OnPointerEvent(const std::shared_ptr<MMI::PointerEvent>& event) override;
50 void OnBackPress() override;
51 bool isWindowReady_ = false;
52 };
53
OnWindowReady(const std::shared_ptr<Rosen::RSSurfaceNode> & rsSurfaceNode)54 void ExtensionCallback::OnWindowReady(const std::shared_ptr<Rosen::RSSurfaceNode>& rsSurfaceNode)
55 {
56 isWindowReady_ = true;
57 }
58
OnExtensionDisconnected()59 void ExtensionCallback::OnExtensionDisconnected() {}
60
OnKeyEvent(const std::shared_ptr<MMI::KeyEvent> & event)61 void ExtensionCallback::OnKeyEvent(const std::shared_ptr<MMI::KeyEvent>& event) {}
62
OnPointerEvent(const std::shared_ptr<MMI::PointerEvent> & event)63 void ExtensionCallback::OnPointerEvent(const std::shared_ptr<MMI::PointerEvent>& event) {}
64
OnBackPress()65 void ExtensionCallback::OnBackPress() {}
66
67 class ExtensionConnectionTest : public testing::Test {
68 public:
69 static void SetUpTestCase();
70 static void TearDownTestCase();
71 virtual void SetUp() override;
72 virtual void TearDown() override;
73
74 private:
75 sptr<WindowExtensionConnection> connection_ = nullptr;
76 };
77
SetUpTestCase()78 void ExtensionConnectionTest::SetUpTestCase() {}
79
TearDownTestCase()80 void ExtensionConnectionTest::TearDownTestCase() {}
81
SetUp()82 void ExtensionConnectionTest::SetUp()
83 {
84 connection_ = new (std::nothrow) WindowExtensionConnection();
85 ASSERT_NE(nullptr, connection_);
86 }
87
TearDown()88 void ExtensionConnectionTest::TearDown()
89 {
90 connection_ = nullptr;
91 }
92
93 namespace {
94 /**
95 * @tc.name: WindowExtensionConnection01
96 * @tc.desc: connect window extension
97 * @tc.type: FUNC
98 */
99 HWTEST_F(ExtensionConnectionTest, WindowExtensionConnection01, TestSize.Level1)
100 {
101 AppExecFwk::ElementName element;
102 element.SetBundleName("com.test.windowextension");
103 element.SetAbilityName("WindowExtAbility");
104 Rosen::Rect rect{ 100, 100, 60, 60 };
105 ASSERT_TRUE(connection_->ConnectExtension(element, rect, 100, INVALID_WINDOW_ID, nullptr) != ERR_OK);
106 connection_->Show();
107 connection_->RequestFocus();
108 connection_->SetBounds(rect);
109 connection_->Hide();
110 }
111
112 /**
113 * @tc.name: Show
114 * @tc.desc: Show Test
115 * @tc.type: FUNC
116 */
117 HWTEST_F(ExtensionConnectionTest, Show, TestSize.Level1)
118 {
119 sptr<IRemoteObject> remoteObject = new (std::nothrow) IRemoteObjectMocker();
120 ASSERT_NE(nullptr, remoteObject);
121 auto mockProxy = new (std::nothrow) MockWindowExtensionProxy(remoteObject);
122 ASSERT_NE(nullptr, mockProxy);
123 ASSERT_NE(nullptr, connection_->pImpl_);
124 connection_->pImpl_->proxy_ = mockProxy;
125 EXPECT_CALL(*mockProxy, Show());
126 connection_->Show();
127 }
128
129 /**
130 * @tc.name: Hide
131 * @tc.desc: Hide Test
132 * @tc.type: FUNC
133 */
134 HWTEST_F(ExtensionConnectionTest, Hide, TestSize.Level1)
135 {
136 sptr<IRemoteObject> remoteObject = new (std::nothrow) IRemoteObjectMocker();
137 ASSERT_NE(nullptr, remoteObject);
138 auto mockProxy = new (std::nothrow) MockWindowExtensionProxy(remoteObject);
139 ASSERT_NE(nullptr, mockProxy);
140 ASSERT_NE(nullptr, connection_->pImpl_);
141 connection_->pImpl_->proxy_ = mockProxy;
142 EXPECT_CALL(*mockProxy, Hide());
143 connection_->Hide();
144 }
145
146 /**
147 * @tc.name: RequestFocus
148 * @tc.desc: RequestFocus Test
149 * @tc.type: FUNC
150 */
151 HWTEST_F(ExtensionConnectionTest, RequestFocus, TestSize.Level1)
152 {
153 sptr<IRemoteObject> remoteObject = new (std::nothrow) IRemoteObjectMocker();
154 ASSERT_NE(nullptr, remoteObject);
155 auto mockProxy = new (std::nothrow) MockWindowExtensionProxy(remoteObject);
156 ASSERT_NE(nullptr, mockProxy);
157 ASSERT_NE(nullptr, connection_->pImpl_);
158 connection_->pImpl_->proxy_ = mockProxy;
159 EXPECT_CALL(*mockProxy, RequestFocus());
160 connection_->RequestFocus();
161 }
162
163 /**
164 * @tc.name: SetBounds
165 * @tc.desc: SetBounds Test
166 * @tc.type: FUNC
167 */
168 HWTEST_F(ExtensionConnectionTest, SetBounds, TestSize.Level1)
169 {
170 sptr<IRemoteObject> remoteObject = new (std::nothrow) IRemoteObjectMocker();
171 ASSERT_NE(nullptr, remoteObject);
172 auto mockProxy = new (std::nothrow) MockWindowExtensionProxy(remoteObject);
173 ASSERT_NE(nullptr, mockProxy);
174 ASSERT_NE(nullptr, connection_->pImpl_);
175 connection_->pImpl_->proxy_ = mockProxy;
176 Rect rect;
177 EXPECT_CALL(*mockProxy, SetBounds(rect));
178 connection_->SetBounds(rect);
179 }
180
181 /**
182 * @tc.name: OnRemoteDied01
183 * @tc.desc: OnRemoteDied Test
184 * @tc.type: FUNC
185 */
186 HWTEST_F(ExtensionConnectionTest, OnRemoteDied01, TestSize.Level1)
187 {
188 sptr<IRemoteObject> remoteObject = nullptr;
189 ASSERT_NE(nullptr, connection_->pImpl_);
190 connection_->pImpl_->deathRecipient_ =
191 new (std::nothrow) WindowExtensionConnection::Impl::WindowExtensionClientRecipient(nullptr);
192 ASSERT_NE(nullptr, connection_->pImpl_->deathRecipient_);
193 connection_->pImpl_->deathRecipient_->OnRemoteDied(remoteObject);
194 }
195
196 /**
197 * @tc.name: OnRemoteDied02
198 * @tc.desc: OnRemoteDied Test
199 * @tc.type: FUNC
200 */
201 HWTEST_F(ExtensionConnectionTest, OnRemoteDied02, TestSize.Level1)
202 {
203 sptr<IRemoteObject> remoteObject = new (std::nothrow) IRemoteObjectMocker();
204 ASSERT_NE(nullptr, remoteObject);
205 ASSERT_NE(nullptr, connection_->pImpl_);
206 connection_->pImpl_->deathRecipient_ =
207 new (std::nothrow) WindowExtensionConnection::Impl::WindowExtensionClientRecipient(nullptr);
208 ASSERT_NE(nullptr, connection_->pImpl_->deathRecipient_);
209 connection_->pImpl_->deathRecipient_->OnRemoteDied(remoteObject);
210 }
211
212 /**
213 * @tc.name: OnRemoteDied03
214 * @tc.desc: OnRemoteDied Test
215 * @tc.type: FUNC
216 */
217 HWTEST_F(ExtensionConnectionTest, OnRemoteDied03, TestSize.Level1)
218 {
219 sptr<IRemoteObject> remoteObject = new (std::nothrow) IRemoteObjectMocker();
220 ASSERT_NE(nullptr, remoteObject);
221 ASSERT_NE(nullptr, connection_->pImpl_);
222 connection_->pImpl_->deathRecipient_ =
223 new (std::nothrow) WindowExtensionConnection::Impl::WindowExtensionClientRecipient(nullptr);
224 ASSERT_NE(nullptr, connection_->pImpl_->deathRecipient_);
225 connection_->pImpl_->deathRecipient_->callback_ = new (std::nothrow) ExtensionCallback();
226 ASSERT_NE(nullptr, connection_->pImpl_->deathRecipient_->callback_);
227 connection_->pImpl_->deathRecipient_->OnRemoteDied(remoteObject);
228 }
229
230 /**
231 * @tc.name: OnAbilityConnectDone01
232 * @tc.desc: OnAbilityConnectDone Test
233 * @tc.type: FUNC
234 */
235 HWTEST_F(ExtensionConnectionTest, OnAbilityConnectDone01, TestSize.Level1)
236 {
237 AppExecFwk::ElementName element;
238 sptr<IRemoteObject> remoteObject = nullptr;
239 int resultCode = 0;
240 ASSERT_NE(nullptr, connection_->pImpl_);
241 connection_->pImpl_->OnAbilityConnectDone(element, remoteObject, resultCode);
242 }
243
244 /**
245 * @tc.name: OnAbilityConnectDone02
246 * @tc.desc: OnAbilityConnectDone Test
247 * @tc.type: FUNC
248 */
249 HWTEST_F(ExtensionConnectionTest, OnAbilityConnectDone02, TestSize.Level1)
250 {
251 AppExecFwk::ElementName element;
252 sptr<IRemoteObject> remoteObject = new (std::nothrow) IRemoteObjectMocker();
253 ASSERT_NE(nullptr, remoteObject);
254 int resultCode = 0;
255 ASSERT_NE(nullptr, connection_->pImpl_);
256 connection_->pImpl_->OnAbilityConnectDone(element, remoteObject, resultCode);
257 }
258
259 /**
260 * @tc.name: OnAbilityConnectDone03
261 * @tc.desc: OnAbilityConnectDone Test
262 * @tc.type: FUNC
263 */
264 HWTEST_F(ExtensionConnectionTest, OnAbilityConnectDone03, TestSize.Level1)
265 {
266 AppExecFwk::ElementName element;
267 sptr<IRemoteObject> remoteObject = new (std::nothrow) IRemoteObjectMocker();
268 ASSERT_NE(nullptr, remoteObject);
269 auto mockProxy = new (std::nothrow) MockWindowExtensionProxy(remoteObject);
270 ASSERT_NE(nullptr, mockProxy);
271 ASSERT_NE(nullptr, connection_->pImpl_);
272 connection_->pImpl_->deathRecipient_ =
273 new (std::nothrow) WindowExtensionConnection::Impl::WindowExtensionClientRecipient(nullptr);
274 ASSERT_NE(nullptr, connection_->pImpl_->deathRecipient_);
275 connection_->pImpl_->proxy_ = mockProxy;
276 int resultCode = 0;
277 connection_->pImpl_->OnAbilityConnectDone(element, remoteObject, resultCode);
278 }
279
280 /**
281 * @tc.name: OnAbilityDisconnectDone01
282 * @tc.desc: OnAbilityConnectDone Test
283 * @tc.type: FUNC
284 */
285 HWTEST_F(ExtensionConnectionTest, OnAbilityDisconnectDone01, TestSize.Level1)
286 {
287 AppExecFwk::ElementName element;
288 int resultCode = 0;
289 ASSERT_NE(nullptr, connection_->pImpl_);
290 connection_->pImpl_->OnAbilityDisconnectDone(element, resultCode);
291 }
292
293 /**
294 * @tc.name: OnAbilityDisconnectDone02
295 * @tc.desc: OnAbilityConnectDone Test
296 * @tc.type: FUNC
297 */
298 HWTEST_F(ExtensionConnectionTest, OnAbilityDisconnectDone02, TestSize.Level1)
299 {
300 AppExecFwk::ElementName element;
301 int resultCode = 0;
302 ASSERT_NE(nullptr, connection_->pImpl_);
303 connection_->pImpl_->componentCallback_ = new (std::nothrow) ExtensionCallback();
304 ASSERT_NE(nullptr, connection_->pImpl_->componentCallback_);
305 connection_->pImpl_->OnAbilityDisconnectDone(element, resultCode);
306 }
307 } // namespace
308 } // namespace Rosen
309 } // namespace OHOS