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 "window_extension_proxy.h"
18 #include "window_extension_client_proxy.h"
19 #include "window_extension_stub.h"
20 #include "window_extension_stub_impl.h"
21 #include "window_extension_client_interface.h"
22 #include "window_extension_client_stub_impl.h"
23 #include "mock_message_parcel.h"
24 #include "iremote_object_mocker.h"
25 
26 using namespace testing;
27 using namespace testing::ext;
28 
29 namespace OHOS {
30 namespace Rosen {
31 class WindowExtensionProxyTest : public testing::Test {
32 public:
33     static void SetUpTestCase();
34     static void TearDownTestCase();
35     void SetUp() override;
36     void TearDown() override;
37     sptr<WindowExtensionStub> mockWindowExtensionStub_;
38     sptr<WindowExtensionProxy> windowExtensionProxy_;
39     sptr<IRemoteObject> impl_;
40     sptr<WindowExtensionClientProxy> windowExtensionClientProxy_;
41 };
42 
SetUpTestCase()43 void WindowExtensionProxyTest::SetUpTestCase()
44 {
45 }
46 
TearDownTestCase()47 void WindowExtensionProxyTest::TearDownTestCase()
48 {
49 }
50 
SetUp()51 void WindowExtensionProxyTest::SetUp()
52 {
53     mockWindowExtensionStub_ = new WindowExtensionStubImpl("name");
54     windowExtensionProxy_ = new WindowExtensionProxy(mockWindowExtensionStub_);
55     impl_ = new IRemoteObjectMocker();
56     ASSERT_NE(nullptr, impl_);
57     windowExtensionClientProxy_ = new WindowExtensionClientProxy(impl_);
58 }
59 
TearDown()60 void WindowExtensionProxyTest::TearDown()
61 {
62     MockMessageParcel::ClearAllErrorFlag();
63 }
64 
65 namespace {
66 /**
67  * @tc.name: OnWindowReady
68  * @tc.desc: test success
69  * @tc.type: FUNC
70  */
71 HWTEST_F(WindowExtensionProxyTest, OnWindowReady, Function | SmallTest | Level2)
72 {
73     ASSERT_NE(nullptr, windowExtensionClientProxy_);
74     MockMessageParcel::SetWriteInterfaceTokenErrorFlag(true);
75     windowExtensionClientProxy_->OnWindowReady(nullptr);
76 
77     MockMessageParcel::SetWriteInterfaceTokenErrorFlag(false);
78     windowExtensionClientProxy_->OnWindowReady(nullptr);
79 
80     struct RSSurfaceNodeConfig config;
81     auto surfaceNode = RSSurfaceNode::Create(config);
82     windowExtensionClientProxy_->OnWindowReady(surfaceNode);
83 }
84 
85 /**
86  * @tc.name: OnBackPress
87  * @tc.desc: test success
88  * @tc.type: FUNC
89  */
90 HWTEST_F(WindowExtensionProxyTest, OnBackPress, Function | SmallTest | Level2)
91 {
92     ASSERT_NE(nullptr, windowExtensionClientProxy_);
93     MockMessageParcel::SetWriteInterfaceTokenErrorFlag(true);
94     windowExtensionClientProxy_->OnBackPress();
95     MockMessageParcel::SetWriteInterfaceTokenErrorFlag(false);
96     windowExtensionClientProxy_->OnBackPress();
97 }
98 
99 /**
100  * @tc.name: OnKeyEvent
101  * @tc.desc: test success
102  * @tc.type: FUNC
103  */
104 HWTEST_F(WindowExtensionProxyTest, OnKeyEvent, Function | SmallTest | Level2)
105 {
106     ASSERT_NE(nullptr, windowExtensionClientProxy_);
107     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
108     ASSERT_NE(nullptr, keyEvent);
109     MockMessageParcel::SetWriteInterfaceTokenErrorFlag(true);
110     windowExtensionClientProxy_->OnKeyEvent(keyEvent);
111 
112     MockMessageParcel::SetWriteInterfaceTokenErrorFlag(false);
113     windowExtensionClientProxy_->OnKeyEvent(keyEvent);
114 }
115 
116 /**
117  * @tc.name: OnPointerEvent
118  * @tc.desc: test success
119  * @tc.type: FUNC
120  */
121 HWTEST_F(WindowExtensionProxyTest, OnPointerEvent, Function | SmallTest | Level2)
122 {
123     ASSERT_NE(nullptr, windowExtensionClientProxy_);
124     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
125     ASSERT_NE(nullptr, pointerEvent);
126     MockMessageParcel::SetWriteInterfaceTokenErrorFlag(true);
127     windowExtensionClientProxy_->OnPointerEvent(pointerEvent);
128 
129     MockMessageParcel::SetWriteInterfaceTokenErrorFlag(false);
130     windowExtensionClientProxy_->OnPointerEvent(pointerEvent);
131 }
132 
133 /**
134  * @tc.name: OnRemoteRequest
135  * @tc.desc: test success
136  * @tc.type: FUNC
137  */
138 HWTEST_F(WindowExtensionProxyTest, OnRemoteRequest, Function | SmallTest | Level2)
139 {
140     uint32_t code = 1000;
141     MessageParcel data = {};
142     MessageParcel reply = {};
143     MessageOption option = {MessageOption::TF_SYNC};
144     ASSERT_NE(-1, mockWindowExtensionStub_->OnRemoteRequest(code, data, reply, option));
145 }
146 }
147 }
148 }