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: Create_0200
86 * @tc.name: UIServiceExtension Create
87 * @tc.desc: UIServiceExtension Create.
88 */
89 HWTEST_F(UIServiceExtensionTest, Create_0200, TestSize.Level1)
90 {
91 TAG_LOGI(AAFwkTag::TEST, "Create_0200 start");
92
93 Runtime::Options options;
94 options.lang = Runtime::Language::UNKNOWN;
95 auto runtime = Runtime::Create(options);
96 auto uIServiceExtensionPtr = UIServiceExtension::Create(runtime);
97 EXPECT_TRUE(uIServiceExtensionPtr != nullptr);
98
99 TAG_LOGI(AAFwkTag::TEST, "Create_0100 end");
100 }
101
102 /**
103 * @tc.number: CreateAndInitContext_0100
104 * @tc.name: UIServiceExtension CreateAndInitContext
105 * @tc.desc: UIServiceExtension CreateAndInitContext.
106 */
107 HWTEST_F(UIServiceExtensionTest, CreateAndInitContext_0100, TestSize.Level1)
108 {
109 TAG_LOGI(AAFwkTag::TEST, "CreateAndInitContext_0100 start");
110
111 Runtime::Options options;
112 options.lang = Runtime::Language::JS;
113 auto runtime = Runtime::Create(options);
114 auto uIServiceExtensionPtr = UIServiceExtension::Create(runtime);
115
116 std::shared_ptr<AppExecFwk::AbilityInfo> info = std::make_shared<AppExecFwk::AbilityInfo>();
117 info->name = "UIServiceExtensionTest";
118 auto record = std::make_shared<AppExecFwk::AbilityLocalRecord>(info, nullptr, nullptr, 0);
119 std::shared_ptr<AppExecFwk::OHOSApplication> application = std::make_shared<AppExecFwk::OHOSApplication>();
120 std::shared_ptr<AppExecFwk::AbilityHandler> handler = std::make_shared<AppExecFwk::AbilityHandler>(nullptr);
121 sptr<IRemoteObject> token = new AppExecFwk::MockAbilityToken();
122
123 auto result = uIServiceExtensionPtr->CreateAndInitContext(record, application, handler, token);
124 EXPECT_NE(result, nullptr);
125
126 TAG_LOGI(AAFwkTag::TEST, "CreateAndInitContext_0100 end");
127 }
128
129 /**
130 * @tc.number: Init_0100
131 * @tc.name: UIServiceExtension Init
132 * @tc.desc: UIServiceExtension Init.
133 */
134 HWTEST_F(UIServiceExtensionTest, Init_0100, TestSize.Level1)
135 {
136 TAG_LOGI(AAFwkTag::TEST, "Init_0100 start");
137
138 Runtime::Options options;
139 options.lang = Runtime::Language::JS;
140 auto runtime = Runtime::Create(options);
141 auto uIServiceExtensionPtr = UIServiceExtension::Create(runtime);
142
143 std::shared_ptr<AppExecFwk::AbilityInfo> info = std::make_shared<AppExecFwk::AbilityInfo>();
144 info->name = "UIServiceExtensionTest";
145 auto record = std::make_shared<AppExecFwk::AbilityLocalRecord>(info, nullptr, nullptr, 0);
146 std::shared_ptr<AppExecFwk::OHOSApplication> application = std::make_shared<AppExecFwk::OHOSApplication>();
147 std::shared_ptr<AppExecFwk::AbilityHandler> handler = std::make_shared<AppExecFwk::AbilityHandler>(nullptr);
148 sptr<IRemoteObject> token = new AppExecFwk::MockAbilityToken();
149
150 uIServiceExtensionPtr->Init(record, application, handler, token);
151 EXPECT_TRUE(uIServiceExtensionPtr != nullptr);
152
153 TAG_LOGI(AAFwkTag::TEST, "Init_0100 end");
154 }
155
156 /**
157 * @tc.number: StartAbility_0100
158 * @tc.name: UIServiceExtension StartAbility
159 * @tc.desc: UIServiceExtension StartAbility.
160 */
161 HWTEST_F(UIServiceExtensionTest, StartAbility_0100, TestSize.Level1)
162 {
163 TAG_LOGI(AAFwkTag::TEST, "StartAbility_0100 start");
164
165 AAFwk::Want want;
166 AAFwk::StartOptions startOptions;
167
168 sptr<AAFwk::MockAbilityManagerService> porxyNew = new (std::nothrow) AAFwk::MockAbilityManagerService();
169 AbilityManagerClient::GetInstance()->proxy_ = porxyNew;
170
171 UIServiceExtensionContext uiServiceExtensionContext;
172 auto result = uiServiceExtensionContext.StartAbility(want, startOptions);
173 EXPECT_EQ(result, ERR_OK);
174
175 TAG_LOGI(AAFwkTag::TEST, "StartAbility_0100 end");
176 }
177
178 /**
179 * @tc.number: TerminateSelf_0100
180 * @tc.name: UIServiceExtension TerminateSelf
181 * @tc.desc: UIServiceExtension TerminateSelf.
182 */
183 HWTEST_F(UIServiceExtensionTest, TerminateSelf_0100, TestSize.Level1)
184 {
185 TAG_LOGI(AAFwkTag::TEST, "TerminateSelf_0100 start");
186
187 UIServiceExtensionContext uiServiceExtensionContext;
188 auto result = uiServiceExtensionContext.TerminateSelf();
189 EXPECT_EQ(result, ERR_OK);
190
191 TAG_LOGI(AAFwkTag::TEST, "TerminateSelf_0100 end");
192 }
193
194 /**
195 * @tc.number: GetWindow_0100
196 * @tc.name: UIServiceExtension GetWindow
197 * @tc.desc: UIServiceExtension GetWindow.
198 */
199 HWTEST_F(UIServiceExtensionTest, GetWindow_0100, TestSize.Level1)
200 {
201 TAG_LOGI(AAFwkTag::TEST, "GetWindow_0100 start");
202
203 UIServiceExtensionContext uiServiceExtensionContext;
204 auto result = uiServiceExtensionContext.GetWindow();
205 EXPECT_TRUE(result == nullptr);
206
207 TAG_LOGI(AAFwkTag::TEST, "GetWindow_0100 end");
208 }
209
210 /**
211 * @tc.number: GetUIContent_0100
212 * @tc.name: UIServiceExtension GetUIContent
213 * @tc.desc: UIServiceExtension GetUIContent.
214 */
215 HWTEST_F(UIServiceExtensionTest, GetUIContent_0100, TestSize.Level1)
216 {
217 TAG_LOGI(AAFwkTag::TEST, "GetUIContent_0100 start");
218
219 UIServiceExtensionContext uiServiceExtensionContext;
220 uiServiceExtensionContext.SetWindow(nullptr);
221 auto result = uiServiceExtensionContext.GetUIContent();
222 EXPECT_EQ(result, nullptr);
223
224 sptr<Rosen::Window> window = new Rosen::Window();
225 uiServiceExtensionContext.SetWindow(window);
226 uiServiceExtensionContext.GetUIContent();
227
228 TAG_LOGI(AAFwkTag::TEST, "GetUIContent_0100 end");
229 }
230
231 /**
232 * @tc.number: StartAbilityByType_0100
233 * @tc.name: UIServiceExtension StartAbilityByType
234 * @tc.desc: UIServiceExtension StartAbilityByType.
235 */
236 HWTEST_F(UIServiceExtensionTest, StartAbilityByType_0100, TestSize.Level1)
237 {
238 TAG_LOGI(AAFwkTag::TEST, "StartAbilityByType_0100 start");
239
240 std::string type;
241 AAFwk::WantParams wantParam;
242 std::shared_ptr<JsUIExtensionCallback> uiExtensionCallbacks{nullptr};
243
244 UIServiceExtensionContext uiServiceExtensionContext;
245 auto result = uiServiceExtensionContext.StartAbilityByType(type, wantParam, uiExtensionCallbacks);
246 EXPECT_EQ(result, ERR_INVALID_VALUE);
247
248 TAG_LOGI(AAFwkTag::TEST, "StartAbilityByType_0100 end");
249 }
250
251 /**
252 * @tc.number: StartAbilityByType_0200
253 * @tc.name: UIServiceExtension StartAbilityByType
254 * @tc.desc: UIServiceExtension StartAbilityByType.
255 */
256 HWTEST_F(UIServiceExtensionTest, StartAbilityByType_0200, TestSize.Level1)
257 {
258 TAG_LOGI(AAFwkTag::TEST, "StartAbilityByType_0200 start");
259
260 std::string type;
261 AAFwk::WantParams wantParam;
262 napi_env env;
263 std::shared_ptr<JsUIExtensionCallback> uiExtensionCallbacks = std::make_shared<JsUIExtensionCallback>(env);
264
265 UIServiceExtensionContext uiServiceExtensionContext;
266 uiServiceExtensionContext.SetWindow(nullptr);
267 auto result = uiServiceExtensionContext.StartAbilityByType(type, wantParam, uiExtensionCallbacks);
268 EXPECT_EQ(result, ERR_INVALID_VALUE);
269
270 TAG_LOGI(AAFwkTag::TEST, "StartAbilityByType_0200 end");
271 }
272
273 /**
274 * @tc.number: StartAbilityByType_0300
275 * @tc.name: UIServiceExtension StartAbilityByType
276 * @tc.desc: UIServiceExtension StartAbilityByType.
277 */
278 HWTEST_F(UIServiceExtensionTest, StartAbilityByType_0300, TestSize.Level1)
279 {
280 TAG_LOGI(AAFwkTag::TEST, "StartAbilityByType_0300 start");
281
282 std::string type;
283 AAFwk::WantParams wantParam;
284 napi_env env;
285 std::shared_ptr<JsUIExtensionCallback> uiExtensionCallbacks = std::make_shared<JsUIExtensionCallback>(env);
286 sptr<Rosen::Window> window = new MockWindow();
287
288 UIServiceExtensionContext uiServiceExtensionContext;
289 uiServiceExtensionContext.SetWindow(window);
290 auto result = uiServiceExtensionContext.StartAbilityByType(type, wantParam, uiExtensionCallbacks);
291 EXPECT_EQ(result, ERR_INVALID_VALUE);
292
293 TAG_LOGI(AAFwkTag::TEST, "StartAbilityByType_0300 end");
294 }
295
296 /**
297 * @tc.number: StartAbilityByType_0400
298 * @tc.name: UIServiceExtension StartAbilityByType
299 * @tc.desc: UIServiceExtension StartAbilityByType.
300 */
301 HWTEST_F(UIServiceExtensionTest, StartAbilityByType_0400, TestSize.Level1)
302 {
303 TAG_LOGI(AAFwkTag::TEST, "StartAbilityByType_0400 start");
304
305 std::string type;
306 AAFwk::WantParams wantParam;
307 const std::string FLAG_AUTH_READ_URI_PERMISSION = "ability.want.params.uriPermissionFlag";
308 wantParam.SetParam(FLAG_AUTH_READ_URI_PERMISSION, 0);
309 napi_env env;
310 std::shared_ptr<JsUIExtensionCallback> uiExtensionCallbacks = std::make_shared<JsUIExtensionCallback>(env);
311 sptr<Rosen::Window> window = new MockWindow();
312
313 UIServiceExtensionContext uiServiceExtensionContext;
314 uiServiceExtensionContext.SetWindow(window);
315 auto result = uiServiceExtensionContext.StartAbilityByType(type, wantParam, uiExtensionCallbacks);
316 EXPECT_EQ(result, ERR_INVALID_VALUE);
317
318 TAG_LOGI(AAFwkTag::TEST, "StartAbilityByType_0400 end");
319 }
320
321 /**
322 * @tc.number: GetWindowOption_0100
323 * @tc.name: UIServiceExtension GetWindowOption
324 * @tc.desc: UIServiceExtension GetWindowOption.
325 */
326 HWTEST_F(UIServiceExtensionTest, GetWindowOption_0100, TestSize.Level1)
327 {
328 TAG_LOGI(AAFwkTag::TEST, "GetWindowOption_0100 start");
329
330 std::shared_ptr<Rosen::ExtensionWindowConfig> extensionWindowConfig
331 = std::make_shared<Rosen::ExtensionWindowConfig>();
332 extensionWindowConfig->windowAttribute = Rosen::ExtensionWindowAttribute::SUB_WINDOW;
333 extensionWindowConfig->subWindowOptions.isModal = true;
334 extensionWindowConfig->subWindowOptions.isTopmost = true;
335 int32_t hostWindowId{1};
336
337 Runtime::Options options;
338 auto runtime = Runtime::Create(options);
339 auto uIServiceExtensionPtr = UIServiceExtension::Create(runtime);
340 auto result = uIServiceExtensionPtr->GetWindowOption(extensionWindowConfig, hostWindowId);
341 EXPECT_NE(result, nullptr);
342
343 TAG_LOGI(AAFwkTag::TEST, "GetWindowOption_0100 end");
344 }
345
346 /**
347 * @tc.number: GetWindowOption_0200
348 * @tc.name: UIServiceExtension GetWindowOption
349 * @tc.desc: UIServiceExtension GetWindowOption.
350 */
351 HWTEST_F(UIServiceExtensionTest, GetWindowOption_0200, TestSize.Level1)
352 {
353 TAG_LOGI(AAFwkTag::TEST, "GetWindowOption_0200 start");
354
355 std::shared_ptr<Rosen::ExtensionWindowConfig> extensionWindowConfig
356 = std::make_shared<Rosen::ExtensionWindowConfig>();
357 extensionWindowConfig->windowAttribute = Rosen::ExtensionWindowAttribute::SUB_WINDOW;
358 extensionWindowConfig->subWindowOptions.isModal = false;
359 extensionWindowConfig->subWindowOptions.isTopmost = true;
360 int32_t hostWindowId{100};
361
362 Runtime::Options options;
363 auto runtime = Runtime::Create(options);
364 auto uIServiceExtensionPtr = UIServiceExtension::Create(runtime);
365 auto result = uIServiceExtensionPtr->GetWindowOption(extensionWindowConfig, hostWindowId);
366 EXPECT_NE(result, nullptr);
367
368 TAG_LOGI(AAFwkTag::TEST, "GetWindowOption_0200 end");
369 }
370
371 /**
372 * @tc.number: GetWindowOption_0300
373 * @tc.name: UIServiceExtension GetWindowOption
374 * @tc.desc: UIServiceExtension GetWindowOption.
375 */
376 HWTEST_F(UIServiceExtensionTest, GetWindowOption_0300, TestSize.Level1)
377 {
378 TAG_LOGI(AAFwkTag::TEST, "GetWindowOption_0300 start");
379
380 std::shared_ptr<Rosen::ExtensionWindowConfig> extensionWindowConfig
381 = std::make_shared<Rosen::ExtensionWindowConfig>();
382 extensionWindowConfig->windowAttribute = Rosen::ExtensionWindowAttribute::SUB_WINDOW;
383 extensionWindowConfig->subWindowOptions.isModal = true;
384 extensionWindowConfig->subWindowOptions.isTopmost = false;
385 int32_t hostWindowId{100};
386
387 Runtime::Options options;
388 auto runtime = Runtime::Create(options);
389 auto uIServiceExtensionPtr = UIServiceExtension::Create(runtime);
390 auto result = uIServiceExtensionPtr->GetWindowOption(extensionWindowConfig, hostWindowId);
391 EXPECT_NE(result, nullptr);
392
393 TAG_LOGI(AAFwkTag::TEST, "GetWindowOption_0300 end");
394 }
395
396 /**
397 * @tc.number: GetWindowOption_0400
398 * @tc.name: UIServiceExtension GetWindowOption
399 * @tc.desc: UIServiceExtension GetWindowOption.
400 */
401 HWTEST_F(UIServiceExtensionTest, GetWindowOption_0400, TestSize.Level1)
402 {
403 TAG_LOGI(AAFwkTag::TEST, "GetWindowOption_0400 start");
404
405 std::shared_ptr<Rosen::ExtensionWindowConfig> extensionWindowConfig
406 = std::make_shared<Rosen::ExtensionWindowConfig>();
407 extensionWindowConfig->windowAttribute = Rosen::ExtensionWindowAttribute::SYSTEM_WINDOW;
408 int32_t hostWindowId{0};
409
410 Runtime::Options options;
411 auto runtime = Runtime::Create(options);
412 auto uIServiceExtensionPtr = UIServiceExtension::Create(runtime);
413 auto result = uIServiceExtensionPtr->GetWindowOption(extensionWindowConfig, hostWindowId);
414 EXPECT_EQ(result, nullptr);
415
416 TAG_LOGI(AAFwkTag::TEST, "GetWindowOption_0400 end");
417 }
418
419 /**
420 * @tc.number: SetWindow_0100
421 * @tc.name: UIServiceExtension SetWindow
422 * @tc.desc: UIServiceExtension SetWindow.
423 */
424 HWTEST_F(UIServiceExtensionTest, SetWindow_0100, TestSize.Level1)
425 {
426 TAG_LOGI(AAFwkTag::TEST, "SetWindow_0100 start");
427
428 sptr<Rosen::Window> window = new Rosen::Window();
429
430 UIServiceExtensionContext uiServiceExtensionContext;
431 uiServiceExtensionContext.SetWindow(window);
432 EXPECT_TRUE(window != nullptr);
433
434 TAG_LOGI(AAFwkTag::TEST, "SetWindow_0100 end");
435 }
436
437
438 } // namespace AbilityRuntime
439 } // namespace OHOS
440