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 #include "ability.h"
19 #include "ability_handler.h"
20 #include "ohos_application.h"
21 #include "mock_ability_token.h"
22 #include "runtime.h"
23 #include "app_service_extension.h"
24
25 #include "iremote_object.h"
26
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace AbilityRuntime {
31 class AppServiceExtensionTest : public testing::Test {
32 public:
33 static void SetUpTestCase();
34 static void TearDownTestCase();
35 void SetUp() override;
36 void TearDown() override;
37 };
38
SetUpTestCase(void)39 void AppServiceExtensionTest::SetUpTestCase(void)
40 {}
41
TearDownTestCase(void)42 void AppServiceExtensionTest::TearDownTestCase(void)
43 {}
44
SetUp()45 void AppServiceExtensionTest::SetUp()
46 {}
47
TearDown()48 void AppServiceExtensionTest::TearDown()
49 {}
50
51 /*
52 * Feature: AppServiceExtension
53 * Function: Create
54 * SubFunction: NA
55 * FunctionPoints: Create
56 * EnvConditions: NA
57 * CaseDescription: Test the function of Create to create a AppServiceExtension instance.
58 */
59 HWTEST_F(AppServiceExtensionTest, Create_0100, TestSize.Level1)
60 {
61 AppServiceExtension* appServiceExtension = AppServiceExtension::Create(nullptr);
62 EXPECT_NE(appServiceExtension, nullptr);
63 }
64
65 /*
66 * Feature: AppServiceExtension
67 * Function: CreateAndInitContext
68 * SubFunction: NA
69 * FunctionPoints: CreateAndInitContext
70 * EnvConditions: NA
71 * CaseDescription: Test the function of CreateAndInitContext.
72 */
73 HWTEST_F(AppServiceExtensionTest, CreateAndInitContext_0100, TestSize.Level1)
74 {
75 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
76 sptr<IRemoteObject> token(new (std::nothrow) MockAbilityToken());
77
78 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
79 std::shared_ptr<AbilityRuntime::ContextImpl> contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
80 std::shared_ptr<AbilityRuntime::ApplicationContext> applicationContext =
81 AbilityRuntime::ApplicationContext::GetInstance();
82 applicationContext->AttachContextImpl(contextImpl);
83 application->SetApplicationContext(applicationContext);
84
85 auto record = std::make_shared<AbilityLocalRecord>(abilityInfo, token, nullptr, 0);
86 std::shared_ptr<AbilityHandler> handler = std::make_shared<AbilityHandler>(nullptr);
87
88 AppServiceExtension* appServiceExtension = AppServiceExtension::Create(nullptr);
89 EXPECT_NE(appServiceExtension, nullptr);
90
91 appServiceExtension->Init(record, application, handler, token);
92
93 std::shared_ptr<AppServiceExtensionContext> context =
94 appServiceExtension->CreateAndInitContext(record, application, handler, token);
95 EXPECT_NE(context, nullptr);
96 }
97
98 /*
99 * Feature: AppServiceExtension
100 * Function: Init
101 * SubFunction: NA
102 * FunctionPoints: Init
103 * EnvConditions: NA
104 * CaseDescription: Test the function of Init.
105 */
106 HWTEST_F(AppServiceExtensionTest, Init_0100, TestSize.Level1)
107 {
108 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
109 sptr<IRemoteObject> token(new (std::nothrow) MockAbilityToken());
110
111 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
112 std::shared_ptr<AbilityRuntime::ContextImpl> contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
113 std::shared_ptr<AbilityRuntime::ApplicationContext> applicationContext =
114 AbilityRuntime::ApplicationContext::GetInstance();
115 applicationContext->AttachContextImpl(contextImpl);
116 application->SetApplicationContext(applicationContext);
117
118 auto record = std::make_shared<AbilityLocalRecord>(abilityInfo, token, nullptr, 0);
119 std::shared_ptr<AbilityHandler> handler = std::make_shared<AbilityHandler>(nullptr);
120
121 AppServiceExtension* appServiceExtension = AppServiceExtension::Create(nullptr);
122 EXPECT_NE(appServiceExtension, nullptr);
123
124 GTEST_LOG_(INFO) << "app service extension Init start";
125
126 appServiceExtension->Init(record, application, handler, token);
127
128 GTEST_LOG_(INFO) << "app service extension Init end";
129 }
130
131 /*
132 * Feature: AppServiceExtension
133 * Function: Init
134 * SubFunction: NA
135 * FunctionPoints: OnConfigurationUpdated
136 * EnvConditions: NA
137 * CaseDescription: Test the function of OnConfigurationUpdated.
138 */
139 HWTEST_F(AppServiceExtensionTest, OnConfigurationUpdated_0100, TestSize.Level1)
140 {
141 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
142 sptr<IRemoteObject> token(new (std::nothrow) MockAbilityToken());
143
144 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
145 std::shared_ptr<AbilityRuntime::ContextImpl> contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
146 std::shared_ptr<AbilityRuntime::ApplicationContext> applicationContext =
147 AbilityRuntime::ApplicationContext::GetInstance();
148 applicationContext->AttachContextImpl(contextImpl);
149 application->SetApplicationContext(applicationContext);
150
151 AppServiceExtension* appServiceExtension = AppServiceExtension::Create(nullptr);
152 EXPECT_NE(appServiceExtension, nullptr);
153
154 GTEST_LOG_(INFO) << "app service extension OnConfigurationUpdated start";
155
156 AppExecFwk::Configuration configuration;
157 appServiceExtension->OnConfigurationUpdated(configuration);
158
159 GTEST_LOG_(INFO) << "app service extension OnConfigurationUpdated end";
160 }
161 } // namespace AbilityRuntime
162 } // namespace OHOS