1 /*
2 * Copyright (c) 2024 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 <gtest/hwext/gtest-multithread.h>
18 #include <gmock/gmock.h>
19
20 #define private public
21 #define protected public
22 #include "js_ui_service_proxy.h"
23 #include "js_ui_service_extension.h"
24 #include "ui_service_extension_module_loader.h"
25 #include "js_ui_service_extension_context.cpp"
26 #undef private
27 #undef protected
28
29 #include "js_runtime_lite.h"
30 #include "mock_ability_token.h"
31 #include "ability_handler.h"
32 #include "ohos_application.h"
33 #include "runtime.h"
34 #include "ui_service_extension_connection_constants.h"
35 #include "ui_service_host_stub.h"
36
37
38 using namespace testing::ext;
39
40 namespace OHOS {
41 namespace AbilityRuntime {
42
43 class MockWindow : public Rosen::Window {
44 public:
GetUIContent() const45 virtual Ace::UIContent* GetUIContent() const
46 {
47 return uiContent_.get();
48 }
49
50 std::unique_ptr<Ace::UIContent> uiContent_ = Ace::UIContent::Create(nullptr, nullptr);
51 };
52
53 class NativeReferenceMock : public NativeReference {
54 public:
55 NativeReferenceMock() = default;
56 virtual ~NativeReferenceMock() = default;
57 MOCK_METHOD0(Ref, uint32_t());
58 MOCK_METHOD0(Unref, uint32_t());
59 MOCK_METHOD0(Get, napi_value());
60 MOCK_METHOD0(GetData, void*());
operator napi_value()61 virtual operator napi_value() override
62 {
63 return reinterpret_cast<napi_value>(this);
64 }
65 MOCK_METHOD0(SetDeleteSelf, void());
66 MOCK_METHOD0(GetRefCount, uint32_t());
67 MOCK_METHOD0(GetFinalRun, bool());
GetNapiValue()68 napi_value GetNapiValue() override
69 {
70 napi_env env{nullptr};
71 napi_value object = AppExecFwk::CreateJSObject(env);
72 return object;
73 }
74 };
75
76 class JsUIServiceProxyTest : public testing::Test {
77 public:
78 static void SetUpTestCase();
79 static void TearDownTestCase();
80 void SetUp() override;
81 void TearDown() override;
82 napi_env env_ = nullptr;
83 std::unique_ptr<Runtime> runtime;
84 std::shared_ptr<JsUIServiceExtension> jsUIServiceExtension;
85 };
86
SetUpTestCase()87 void JsUIServiceProxyTest::SetUpTestCase()
88 {}
89
TearDownTestCase()90 void JsUIServiceProxyTest::TearDownTestCase()
91 {}
92
SetUp()93 void JsUIServiceProxyTest::SetUp()
94 {}
95
TearDown()96 void JsUIServiceProxyTest::TearDown()
97 {}
98
99 class IRemoteObjectMocker : public IRemoteObject {
100 public:
IRemoteObjectMocker()101 IRemoteObjectMocker() : IRemoteObject {u"IRemoteObjectMocker"}
102 {
103 }
104
~IRemoteObjectMocker()105 ~IRemoteObjectMocker()
106 {
107 }
108
GetObjectRefCount()109 int32_t GetObjectRefCount()
110 {
111 return 0;
112 }
113
SendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)114 int SendRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
115 {
116 return 0;
117 }
118
IsProxyObject() const119 bool IsProxyObject() const
120 {
121 return true;
122 }
123
CheckObjectLegality() const124 bool CheckObjectLegality() const
125 {
126 return true;
127 }
128
AddDeathRecipient(const sptr<DeathRecipient> & recipient)129 bool AddDeathRecipient(const sptr<DeathRecipient>& recipient)
130 {
131 return true;
132 }
133
RemoveDeathRecipient(const sptr<DeathRecipient> & recipient)134 bool RemoveDeathRecipient(const sptr<DeathRecipient>& recipient)
135 {
136 return true;
137 }
138
AsInterface()139 sptr<IRemoteBroker> AsInterface()
140 {
141 return nullptr;
142 }
143
Dump(int fd,const std::vector<std::u16string> & args)144 int Dump(int fd, const std::vector<std::u16string>& args)
145 {
146 return 0;
147 }
148 };
149
150 /**
151 * @tc.number: CreateJsUIServiceProxy_0100
152 * @tc.name: CreateJsUIServiceProxy
153 * @tc.desc: SystemAbilityStatusChangeListener CreateJsUIServiceProxy
154 */
155 HWTEST_F(JsUIServiceProxyTest, CreateJsUIServiceProxy_0100, TestSize.Level1)
156 {
157 TAG_LOGI(AAFwkTag::TEST, "CreateJsUIServiceProxy_0100 start");
158 sptr<IRemoteObject> impl = nullptr;
159 sptr<IRemoteObject> hostProxy = nullptr;
160 auto infos = std::make_shared<AAFwk::JsUIServiceProxy>(impl, hostProxy);
161 int64_t connectionId = 1;
162 napi_value result = infos->CreateJsUIServiceProxy(env_, impl, connectionId, hostProxy);
163 EXPECT_EQ(result, nullptr);
164 TAG_LOGI(AAFwkTag::TEST, "CreateJsUIServiceProxy_0100 end");
165 }
166
167 /**
168 * @tc.number: CreateJsUIServiceProxy_0200
169 * @tc.name: CreateJsUIServiceProxy
170 * @tc.desc: SystemAbilityStatusChangeListener CreateJsUIServiceProxy
171 */
172 HWTEST_F(JsUIServiceProxyTest, CreateJsUIServiceProxy_0200, TestSize.Level1)
173 {
174 TAG_LOGI(AAFwkTag::TEST, "CreateJsUIServiceProxy_0200 start");
175 sptr<IRemoteObject> impl = new IRemoteObjectMocker();
176 sptr<IRemoteObject> hostProxy = new IRemoteObjectMocker();
177 auto infos = std::make_shared<AAFwk::JsUIServiceProxy>(impl, hostProxy);
178 OHOS::AbilityRuntime::Runtime::Options options;
179 std::shared_ptr<OHOS::JsEnv::JsEnvironment> jsEnv = nullptr;
180 auto err = JsRuntimeLite::GetInstance().CreateJsEnv(options, jsEnv);
181 EXPECT_EQ(err, napi_status::napi_ok);
182 napi_env env = reinterpret_cast<napi_env>(jsEnv->GetNativeEngine());
183 int64_t connectionId = 1;
184 napi_value result = infos->CreateJsUIServiceProxy(env, impl, connectionId, hostProxy);
185 EXPECT_NE(result, nullptr);
186 TAG_LOGI(AAFwkTag::TEST, "CreateJsUIServiceProxy_0200 end");
187 }
188
189 /**
190 * @tc.number: Finalizer_0100
191 * @tc.name: Finalizer_0100
192 * @tc.desc: SystemAbilityStatusChangeListener Finalizer_0100
193 */
194 HWTEST_F(JsUIServiceProxyTest, Finalizer_0100, TestSize.Level1)
195 {
196 TAG_LOGI(AAFwkTag::TEST, "Finalizer_0100 start");
197 sptr<IRemoteObject> impl = nullptr;
198 sptr<IRemoteObject> hostProxy = nullptr;
199 auto infos = std::make_shared<AAFwk::JsUIServiceProxy>(impl, hostProxy);
200 infos->Finalizer(env_, nullptr, nullptr);
201 EXPECT_NE(infos, nullptr);
202 TAG_LOGI(AAFwkTag::TEST, "Finalizer_0100 end");
203 }
204
205 /**
206 * @tc.number: SendData_0100
207 * @tc.name: SendData_0100
208 * @tc.desc: SystemAbilityStatusChangeListener SendData_0100
209 */
210 HWTEST_F(JsUIServiceProxyTest, SendData_0100, TestSize.Level1)
211 {
212 TAG_LOGI(AAFwkTag::TEST, "SendData_0100 start");
213 sptr<IRemoteObject> impl = nullptr;
214 sptr<IRemoteObject> hostProxy = nullptr;
215 auto infos = std::make_shared<AAFwk::JsUIServiceProxy>(impl, hostProxy);
216 napi_callback_info info{nullptr};
217 napi_value result = infos->SendData(env_, info);
218 EXPECT_EQ(result, nullptr);
219 TAG_LOGI(AAFwkTag::TEST, "SendData_0100 end");
220 }
221
222 /**
223 * @tc.number: OnSendData_0100
224 * @tc.name: OnSendData_0100
225 * @tc.desc: SystemAbilityStatusChangeListener OnSendData_0100
226 */
227 HWTEST_F(JsUIServiceProxyTest, OnSendData_0100, TestSize.Level1)
228 {
229 TAG_LOGI(AAFwkTag::TEST, "OnSendData_0100 start");
230 sptr<IRemoteObject> impl = new IRemoteObjectMocker();;
231 sptr<IRemoteObject> hostProxy = new IRemoteObjectMocker();;
232 auto infos = std::make_shared<AAFwk::JsUIServiceProxy>(impl, hostProxy);
233 infos->hostProxy_ = nullptr;
234 infos->proxy_ = nullptr;
235 NapiCallbackInfo info{0};
236 napi_value result = infos->OnSendData(env_, info);
237 EXPECT_EQ(result, nullptr);
238 TAG_LOGI(AAFwkTag::TEST, "OnSendData_0100 end");
239 }
240
241 /**
242 * @tc.number: OnSendData_0200
243 * @tc.name: OnSendData_0200
244 * @tc.desc: SystemAbilityStatusChangeListener OnSendData_0200
245 */
246
247 HWTEST_F(JsUIServiceProxyTest, OnSendData_0200, TestSize.Level1)
248 {
249 TAG_LOGI(AAFwkTag::TEST, "OnSendData_0200 start");
250 sptr<IRemoteObject> impl = new IRemoteObjectMocker();;
251 sptr<IRemoteObject> hostProxy = new IRemoteObjectMocker();;
252 auto infos = std::make_shared<AAFwk::JsUIServiceProxy>(impl, hostProxy);
253 NapiCallbackInfo info{0};
254 info.argv[0] = AppExecFwk::CreateJSObject(env_);
255 napi_value result = infos->OnSendData(env_, info);
256 EXPECT_EQ(result, nullptr);
257 TAG_LOGI(AAFwkTag::TEST, "OnSendData_0200 end");
258 }
259
260 /**
261 * @tc.number: OnSendData_0300
262 * @tc.name: OnSendData_0300
263 * @tc.desc: SystemAbilityStatusChangeListener OnSendData_0300
264 */
265
266 HWTEST_F(JsUIServiceProxyTest, OnSendData_0300, TestSize.Level1)
267 {
268 TAG_LOGI(AAFwkTag::TEST, "OnSendData_0300 start");
269 sptr<IRemoteObject> impl = new IRemoteObjectMocker();;
270 sptr<IRemoteObject> hostProxy = new IRemoteObjectMocker();;
271 auto infos = std::make_shared<AAFwk::JsUIServiceProxy>(impl, hostProxy);
272 NapiCallbackInfo info{1};
273 napi_value result = infos->OnSendData(env_, info);
274 EXPECT_EQ(result, nullptr);
275 TAG_LOGI(AAFwkTag::TEST, "OnSendData_0300 end");
276 }
277
278 } // namespace AbilityRuntime
279 } // namespace OHOS
280