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