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 #define protected public
20 #include "ui_service_extension.h"
21 #include "ui_service_extension_module_loader.h"
22 #include "ui_service_extension_context.h"
23 #include "ability_manager_client.h"
24 #undef private
25 #undef protected
26
27 #include "mock_ability_token.h"
28 #include "mock_ability_manager_service.h"
29 #include "ability_handler.h"
30 #include "ohos_application.h"
31 #include "runtime.h"
32
33 using namespace testing::ext;
34 using namespace OHOS::Rosen;
35
36 namespace OHOS {
37 namespace AbilityRuntime {
38 class MockWindow : public Rosen::Window {
39 public:
GetUIContent() const40 virtual Ace::UIContent* GetUIContent() const
41 {
42 return uiContent_.get();
43 }
44
45 std::unique_ptr<Ace::UIContent> uiContent_ = Ace::UIContent::Create(nullptr, nullptr);
46 };
47
48 class UIServiceExtensionTest : public testing::Test {
49 public:
50 static void SetUpTestCase();
51 static void TearDownTestCase();
52 void SetUp() override;
53 void TearDown() override;
54 };
55
SetUpTestCase(void)56 void UIServiceExtensionTest::SetUpTestCase(void)
57 {}
58
TearDownTestCase(void)59 void UIServiceExtensionTest::TearDownTestCase(void)
60 {}
61
SetUp()62 void UIServiceExtensionTest::SetUp()
63 {}
64
TearDown()65 void UIServiceExtensionTest::TearDown()
66 {}
67
68 /**
69 * @tc.number: Create_0100
70 * @tc.name: UIServiceExtension Create
71 * @tc.desc: UIServiceExtension Create.
72 */
73 HWTEST_F(UIServiceExtensionTest, Create_0100, TestSize.Level1)
74 {
75 TAG_LOGI(AAFwkTag::TEST, "Create_0100 start");
76
77 std::unique_ptr<Runtime> runtime{nullptr};
78 auto uIServiceExtensionPtr = UIServiceExtension::Create(runtime);
79 EXPECT_TRUE(uIServiceExtensionPtr != nullptr);
80
81 TAG_LOGI(AAFwkTag::TEST, "Create_0100 end");
82 }
83
84 /**
85 * @tc.number: CreateAndInitContext_0100
86 * @tc.name: UIServiceExtension CreateAndInitContext
87 * @tc.desc: UIServiceExtension CreateAndInitContext.
88 */
89 HWTEST_F(UIServiceExtensionTest, CreateAndInitContext_0100, TestSize.Level1)
90 {
91 TAG_LOGI(AAFwkTag::TEST, "CreateAndInitContext_0100 start");
92
93 Runtime::Options options;
94 options.lang = Runtime::Language::JS;
95 auto runtime = Runtime::Create(options);
96 auto uIServiceExtensionPtr = UIServiceExtension::Create(runtime);
97
98 std::shared_ptr<AppExecFwk::AbilityInfo> info = std::make_shared<AppExecFwk::AbilityInfo>();
99 info->name = "UIServiceExtensionTest";
100 auto record = std::make_shared<AppExecFwk::AbilityLocalRecord>(info, nullptr, nullptr, 0);
101 std::shared_ptr<AppExecFwk::OHOSApplication> application = std::make_shared<AppExecFwk::OHOSApplication>();
102 std::shared_ptr<AppExecFwk::AbilityHandler> handler = std::make_shared<AppExecFwk::AbilityHandler>(nullptr);
103 sptr<IRemoteObject> token = new AppExecFwk::MockAbilityToken();
104
105 auto result = uIServiceExtensionPtr->CreateAndInitContext(record, application, handler, token);
106 EXPECT_NE(result, nullptr);
107
108 TAG_LOGI(AAFwkTag::TEST, "CreateAndInitContext_0100 end");
109 }
110
111 /**
112 * @tc.number: Init_0100
113 * @tc.name: UIServiceExtension Init
114 * @tc.desc: UIServiceExtension Init.
115 */
116 HWTEST_F(UIServiceExtensionTest, Init_0100, TestSize.Level1)
117 {
118 TAG_LOGI(AAFwkTag::TEST, "Init_0100 start");
119
120 Runtime::Options options;
121 options.lang = Runtime::Language::JS;
122 auto runtime = Runtime::Create(options);
123 auto uIServiceExtensionPtr = UIServiceExtension::Create(runtime);
124
125 std::shared_ptr<AppExecFwk::AbilityInfo> info = std::make_shared<AppExecFwk::AbilityInfo>();
126 info->name = "UIServiceExtensionTest";
127 auto record = std::make_shared<AppExecFwk::AbilityLocalRecord>(info, nullptr, nullptr, 0);
128 std::shared_ptr<AppExecFwk::OHOSApplication> application = std::make_shared<AppExecFwk::OHOSApplication>();
129 std::shared_ptr<AppExecFwk::AbilityHandler> handler = std::make_shared<AppExecFwk::AbilityHandler>(nullptr);
130 sptr<IRemoteObject> token = new AppExecFwk::MockAbilityToken();
131
132 uIServiceExtensionPtr->Init(record, application, handler, token);
133 EXPECT_TRUE(uIServiceExtensionPtr != nullptr);
134
135 TAG_LOGI(AAFwkTag::TEST, "Init_0100 end");
136 }
137
138 /**
139 * @tc.number: StartAbility_0100
140 * @tc.name: UIServiceExtension StartAbility
141 * @tc.desc: UIServiceExtension StartAbility.
142 */
143 HWTEST_F(UIServiceExtensionTest, StartAbility_0100, TestSize.Level1)
144 {
145 TAG_LOGI(AAFwkTag::TEST, "StartAbility_0100 start");
146
147 AAFwk::Want want;
148 AAFwk::StartOptions startOptions;
149
150 sptr<AAFwk::MockAbilityManagerService> porxyNew = new (std::nothrow) AAFwk::MockAbilityManagerService();
151 AbilityManagerClient::GetInstance()->proxy_ = porxyNew;
152
153 UIServiceExtensionContext uiServiceExtensionContext;
154 auto result = uiServiceExtensionContext.StartAbility(want, startOptions);
155 EXPECT_EQ(result, ERR_OK);
156
157 TAG_LOGI(AAFwkTag::TEST, "StartAbility_0100 end");
158 }
159
160 /**
161 * @tc.number: TerminateSelf_0100
162 * @tc.name: UIServiceExtension TerminateSelf
163 * @tc.desc: UIServiceExtension TerminateSelf.
164 */
165 HWTEST_F(UIServiceExtensionTest, TerminateSelf_0100, TestSize.Level1)
166 {
167 TAG_LOGI(AAFwkTag::TEST, "TerminateSelf_0100 start");
168
169 UIServiceExtensionContext uiServiceExtensionContext;
170 auto result = uiServiceExtensionContext.TerminateSelf();
171 EXPECT_EQ(result, ERR_OK);
172
173 TAG_LOGI(AAFwkTag::TEST, "TerminateSelf_0100 end");
174 }
175
176 /**
177 * @tc.number: GetWindow_0100
178 * @tc.name: UIServiceExtension GetWindow
179 * @tc.desc: UIServiceExtension GetWindow.
180 */
181 HWTEST_F(UIServiceExtensionTest, GetWindow_0100, TestSize.Level1)
182 {
183 TAG_LOGI(AAFwkTag::TEST, "GetWindow_0100 start");
184
185 UIServiceExtensionContext uiServiceExtensionContext;
186 auto result = uiServiceExtensionContext.GetWindow();
187 EXPECT_TRUE(result == nullptr);
188
189 TAG_LOGI(AAFwkTag::TEST, "GetWindow_0100 end");
190 }
191
192 /**
193 * @tc.number: GetUIContent_0100
194 * @tc.name: UIServiceExtension GetUIContent
195 * @tc.desc: UIServiceExtension GetUIContent.
196 */
197 HWTEST_F(UIServiceExtensionTest, GetUIContent_0100, TestSize.Level1)
198 {
199 TAG_LOGI(AAFwkTag::TEST, "GetUIContent_0100 start");
200
201 UIServiceExtensionContext uiServiceExtensionContext;
202 uiServiceExtensionContext.SetWindow(nullptr);
203 auto result = uiServiceExtensionContext.GetUIContent();
204 EXPECT_EQ(result, nullptr);
205
206 sptr<Rosen::Window> window = new Rosen::Window();
207 uiServiceExtensionContext.SetWindow(window);
208 uiServiceExtensionContext.GetUIContent();
209
210 TAG_LOGI(AAFwkTag::TEST, "GetUIContent_0100 end");
211 }
212
213 /**
214 * @tc.number: StartAbilityByType_0100
215 * @tc.name: UIServiceExtension StartAbilityByType
216 * @tc.desc: UIServiceExtension StartAbilityByType.
217 */
218 HWTEST_F(UIServiceExtensionTest, StartAbilityByType_0100, TestSize.Level1)
219 {
220 TAG_LOGI(AAFwkTag::TEST, "StartAbilityByType_0100 start");
221
222 std::string type;
223 AAFwk::WantParams wantParam;
224 std::shared_ptr<JsUIExtensionCallback> uiExtensionCallbacks{nullptr};
225
226 UIServiceExtensionContext uiServiceExtensionContext;
227 auto result = uiServiceExtensionContext.StartAbilityByType(type, wantParam, uiExtensionCallbacks);
228 EXPECT_EQ(result, ERR_INVALID_VALUE);
229
230 TAG_LOGI(AAFwkTag::TEST, "StartAbilityByType_0100 end");
231 }
232
233 /**
234 * @tc.number: StartAbilityByType_0200
235 * @tc.name: UIServiceExtension StartAbilityByType
236 * @tc.desc: UIServiceExtension StartAbilityByType.
237 */
238 HWTEST_F(UIServiceExtensionTest, StartAbilityByType_0200, TestSize.Level1)
239 {
240 TAG_LOGI(AAFwkTag::TEST, "StartAbilityByType_0200 start");
241
242 std::string type;
243 AAFwk::WantParams wantParam;
244 napi_env env;
245 std::shared_ptr<JsUIExtensionCallback> uiExtensionCallbacks = std::make_shared<JsUIExtensionCallback>(env);
246
247 UIServiceExtensionContext uiServiceExtensionContext;
248 uiServiceExtensionContext.SetWindow(nullptr);
249 auto result = uiServiceExtensionContext.StartAbilityByType(type, wantParam, uiExtensionCallbacks);
250 EXPECT_EQ(result, ERR_INVALID_VALUE);
251
252 TAG_LOGI(AAFwkTag::TEST, "StartAbilityByType_0200 end");
253 }
254
255 /**
256 * @tc.number: StartAbilityByType_0300
257 * @tc.name: UIServiceExtension StartAbilityByType
258 * @tc.desc: UIServiceExtension StartAbilityByType.
259 */
260 HWTEST_F(UIServiceExtensionTest, StartAbilityByType_0300, TestSize.Level1)
261 {
262 TAG_LOGI(AAFwkTag::TEST, "StartAbilityByType_0300 start");
263
264 std::string type;
265 AAFwk::WantParams wantParam;
266 napi_env env;
267 std::shared_ptr<JsUIExtensionCallback> uiExtensionCallbacks = std::make_shared<JsUIExtensionCallback>(env);
268 sptr<Rosen::Window> window = new MockWindow();
269
270 UIServiceExtensionContext uiServiceExtensionContext;
271 uiServiceExtensionContext.SetWindow(window);
272 auto result = uiServiceExtensionContext.StartAbilityByType(type, wantParam, uiExtensionCallbacks);
273 EXPECT_EQ(result, ERR_INVALID_VALUE);
274
275 TAG_LOGI(AAFwkTag::TEST, "StartAbilityByType_0300 end");
276 }
277
278 /**
279 * @tc.number: StartAbilityByType_0400
280 * @tc.name: UIServiceExtension StartAbilityByType
281 * @tc.desc: UIServiceExtension StartAbilityByType.
282 */
283 HWTEST_F(UIServiceExtensionTest, StartAbilityByType_0400, TestSize.Level1)
284 {
285 TAG_LOGI(AAFwkTag::TEST, "StartAbilityByType_0400 start");
286
287 std::string type;
288 AAFwk::WantParams wantParam;
289 const std::string FLAG_AUTH_READ_URI_PERMISSION = "ability.want.params.uriPermissionFlag";
290 wantParam.SetParam(FLAG_AUTH_READ_URI_PERMISSION, 0);
291 napi_env env;
292 std::shared_ptr<JsUIExtensionCallback> uiExtensionCallbacks = std::make_shared<JsUIExtensionCallback>(env);
293 sptr<Rosen::Window> window = new MockWindow();
294
295 UIServiceExtensionContext uiServiceExtensionContext;
296 uiServiceExtensionContext.SetWindow(window);
297 auto result = uiServiceExtensionContext.StartAbilityByType(type, wantParam, uiExtensionCallbacks);
298 EXPECT_EQ(result, ERR_INVALID_VALUE);
299
300 TAG_LOGI(AAFwkTag::TEST, "StartAbilityByType_0400 end");
301 }
302
303 /**
304 * @tc.number: GetWindowOption_0100
305 * @tc.name: UIServiceExtension GetWindowOption
306 * @tc.desc: UIServiceExtension GetWindowOption.
307 */
308 HWTEST_F(UIServiceExtensionTest, GetWindowOption_0100, TestSize.Level1)
309 {
310 TAG_LOGI(AAFwkTag::TEST, "GetWindowOption_0100 start");
311
312 std::shared_ptr<Rosen::ExtensionWindowConfig> extensionWindowConfig
313 = std::make_shared<Rosen::ExtensionWindowConfig>();
314 extensionWindowConfig->windowAttribute = Rosen::ExtensionWindowAttribute::SUB_WINDOW;
315 extensionWindowConfig->subWindowOptions.isModal = true;
316 extensionWindowConfig->subWindowOptions.isTopmost = true;
317 int32_t hostWindowId{1};
318
319 Runtime::Options options;
320 auto runtime = Runtime::Create(options);
321 auto uIServiceExtensionPtr = UIServiceExtension::Create(runtime);
322 auto result = uIServiceExtensionPtr->GetWindowOption(extensionWindowConfig, hostWindowId);
323 EXPECT_NE(result, nullptr);
324
325 TAG_LOGI(AAFwkTag::TEST, "GetWindowOption_0100 end");
326 }
327
328 /**
329 * @tc.number: GetWindowOption_0200
330 * @tc.name: UIServiceExtension GetWindowOption
331 * @tc.desc: UIServiceExtension GetWindowOption.
332 */
333 HWTEST_F(UIServiceExtensionTest, GetWindowOption_0200, TestSize.Level1)
334 {
335 TAG_LOGI(AAFwkTag::TEST, "GetWindowOption_0200 start");
336
337 std::shared_ptr<Rosen::ExtensionWindowConfig> extensionWindowConfig
338 = std::make_shared<Rosen::ExtensionWindowConfig>();
339 extensionWindowConfig->windowAttribute = Rosen::ExtensionWindowAttribute::SUB_WINDOW;
340 extensionWindowConfig->subWindowOptions.isModal = false;
341 extensionWindowConfig->subWindowOptions.isTopmost = true;
342 int32_t hostWindowId{100};
343
344 Runtime::Options options;
345 auto runtime = Runtime::Create(options);
346 auto uIServiceExtensionPtr = UIServiceExtension::Create(runtime);
347 auto result = uIServiceExtensionPtr->GetWindowOption(extensionWindowConfig, hostWindowId);
348 EXPECT_NE(result, nullptr);
349
350 TAG_LOGI(AAFwkTag::TEST, "GetWindowOption_0200 end");
351 }
352
353 /**
354 * @tc.number: GetWindowOption_0300
355 * @tc.name: UIServiceExtension GetWindowOption
356 * @tc.desc: UIServiceExtension GetWindowOption.
357 */
358 HWTEST_F(UIServiceExtensionTest, GetWindowOption_0300, TestSize.Level1)
359 {
360 TAG_LOGI(AAFwkTag::TEST, "GetWindowOption_0300 start");
361
362 std::shared_ptr<Rosen::ExtensionWindowConfig> extensionWindowConfig
363 = std::make_shared<Rosen::ExtensionWindowConfig>();
364 extensionWindowConfig->windowAttribute = Rosen::ExtensionWindowAttribute::SUB_WINDOW;
365 extensionWindowConfig->subWindowOptions.isModal = true;
366 extensionWindowConfig->subWindowOptions.isTopmost = false;
367 int32_t hostWindowId{100};
368
369 Runtime::Options options;
370 auto runtime = Runtime::Create(options);
371 auto uIServiceExtensionPtr = UIServiceExtension::Create(runtime);
372 auto result = uIServiceExtensionPtr->GetWindowOption(extensionWindowConfig, hostWindowId);
373 EXPECT_NE(result, nullptr);
374
375 TAG_LOGI(AAFwkTag::TEST, "GetWindowOption_0300 end");
376 }
377
378 /**
379 * @tc.number: GetWindowOption_0400
380 * @tc.name: UIServiceExtension GetWindowOption
381 * @tc.desc: UIServiceExtension GetWindowOption.
382 */
383 HWTEST_F(UIServiceExtensionTest, GetWindowOption_0400, TestSize.Level1)
384 {
385 TAG_LOGI(AAFwkTag::TEST, "GetWindowOption_0400 start");
386
387 std::shared_ptr<Rosen::ExtensionWindowConfig> extensionWindowConfig
388 = std::make_shared<Rosen::ExtensionWindowConfig>();
389 extensionWindowConfig->windowAttribute = Rosen::ExtensionWindowAttribute::SYSTEM_WINDOW;
390 int32_t hostWindowId{0};
391
392 Runtime::Options options;
393 auto runtime = Runtime::Create(options);
394 auto uIServiceExtensionPtr = UIServiceExtension::Create(runtime);
395 auto result = uIServiceExtensionPtr->GetWindowOption(extensionWindowConfig, hostWindowId);
396 EXPECT_EQ(result, nullptr);
397
398 TAG_LOGI(AAFwkTag::TEST, "GetWindowOption_0400 end");
399 }
400
401 /**
402 * @tc.number: SetWindow_0100
403 * @tc.name: UIServiceExtension SetWindow
404 * @tc.desc: UIServiceExtension SetWindow.
405 */
406 HWTEST_F(UIServiceExtensionTest, SetWindow_0100, TestSize.Level1)
407 {
408 TAG_LOGI(AAFwkTag::TEST, "SetWindow_0100 start");
409
410 sptr<Rosen::Window> window = new Rosen::Window();
411
412 UIServiceExtensionContext uiServiceExtensionContext;
413 uiServiceExtensionContext.SetWindow(window);
414 EXPECT_TRUE(window != nullptr);
415
416 TAG_LOGI(AAFwkTag::TEST, "SetWindow_0100 end");
417 }
418
419
420 } // namespace AbilityRuntime
421 } // namespace OHOS
422