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 "extension_base.h"
21 #undef private
22 #undef protected
23
24 #include "ability_handler.h"
25 #include "ability_transaction_callback_info.h"
26 #include "configuration.h"
27 #include "hilog_tag_wrapper.h"
28 #include "iremote_object.h"
29 #include "mock_ability_token.h"
30 #include "ohos_application.h"
31 #include "extension_context.h"
32 #include "js_runtime.h"
33 #include "js_extension_common.h"
34 #include "want.h"
35
36 using namespace testing::ext;
37 using OHOS::AppExecFwk::ElementName;
38
39 namespace OHOS {
40 namespace AbilityRuntime {
41 class AbilityExtensionBaseTest : public testing::Test {
42 public:
43 static void SetUpTestCase();
44 static void TearDownTestCase();
45 void SetUp() override;
46 void TearDown() override;
47 };
48
SetUpTestCase(void)49 void AbilityExtensionBaseTest::SetUpTestCase(void)
50 {}
51
TearDownTestCase(void)52 void AbilityExtensionBaseTest::TearDownTestCase(void)
53 {}
54
SetUp()55 void AbilityExtensionBaseTest::SetUp()
56 {}
57
TearDown()58 void AbilityExtensionBaseTest::TearDown()
59 {}
60
61 /**
62 * @tc.name: Init_0100
63 * @tc.desc: basic function test.
64 * @tc.type: FUNC
65 */
66 HWTEST_F(AbilityExtensionBaseTest, Init_0100, TestSize.Level1)
67 {
68 TAG_LOGI(AAFwkTag::TEST, "Init start");
69
70 std::shared_ptr<AppExecFwk::AbilityLocalRecord> record = nullptr;
71 std::shared_ptr<AppExecFwk::OHOSApplication> application = std::make_shared<AppExecFwk::OHOSApplication>();
72 std::shared_ptr<AppExecFwk::AbilityHandler> handler = std::make_shared<AppExecFwk::AbilityHandler>(nullptr);
73 sptr<IRemoteObject> token = nullptr;
74
75 std::shared_ptr<AbilityRuntime::ContextImpl> contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
76 std::shared_ptr<AbilityRuntime::ApplicationContext> applicationContext =
77 AbilityRuntime::ApplicationContext::GetInstance();
78 applicationContext->AttachContextImpl(contextImpl);
79 application->SetApplicationContext(applicationContext);
80
81 ExtensionBase<ExtensionContext> extensionBase;
82 extensionBase.Init(record, application, handler, token);
83 EXPECT_TRUE(true);
84
85 TAG_LOGI(AAFwkTag::TEST, "Init end");
86 }
87
88 /**
89 * @tc.name: CreateAndInitContext_0100
90 * @tc.desc: basic function test.
91 * @tc.type: FUNC
92 */
93 HWTEST_F(AbilityExtensionBaseTest, CreateAndInitContext_0100, TestSize.Level1)
94 {
95 TAG_LOGI(AAFwkTag::TEST, "CreateAndInitContext start");
96
97 std::shared_ptr<AppExecFwk::AbilityInfo> info = std::make_shared<AppExecFwk::AbilityInfo>();
98 info->name = "ExtensionBaseTest";
99 std::shared_ptr<AppExecFwk::AbilityLocalRecord> record =
100 std::make_shared<AppExecFwk::AbilityLocalRecord>(info, nullptr);
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 std::shared_ptr<AbilityRuntime::ContextImpl> contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
106 std::shared_ptr<AbilityRuntime::ApplicationContext> applicationContext =
107 AbilityRuntime::ApplicationContext::GetInstance();
108 applicationContext->AttachContextImpl(contextImpl);
109 application->SetApplicationContext(applicationContext);
110
111 ExtensionBase<ExtensionContext> extensionBase;
112 extensionBase.Init(record, application, handler, token);
113 std::shared_ptr<ExtensionContext> context = extensionBase.CreateAndInitContext(record, application, handler, token);
114 EXPECT_STREQ(context->GetAbilityInfo()->name.c_str(), "ExtensionBaseTest");
115 EXPECT_TRUE(true);
116
117 TAG_LOGI(AAFwkTag::TEST, "CreateAndInitContext end");
118 }
119
120 /**
121 * @tc.name: GetContext_0100
122 * @tc.desc: basic function test.
123 * @tc.type: FUNC
124 */
125 HWTEST_F(AbilityExtensionBaseTest, GetContext_0100, TestSize.Level1)
126 {
127 TAG_LOGI(AAFwkTag::TEST, "GetContext start");
128
129 std::shared_ptr<AppExecFwk::AbilityInfo> info = std::make_shared<AppExecFwk::AbilityInfo>();
130 info->name = "ExtensionBaseTest";
131 std::shared_ptr<AppExecFwk::AbilityLocalRecord> record =
132 std::make_shared<AppExecFwk::AbilityLocalRecord>(info, nullptr);
133 std::shared_ptr<AppExecFwk::OHOSApplication> application = std::make_shared<AppExecFwk::OHOSApplication>();
134 std::shared_ptr<AppExecFwk::AbilityHandler> handler = std::make_shared<AppExecFwk::AbilityHandler>(nullptr);
135 sptr<IRemoteObject> token = new AppExecFwk::MockAbilityToken();
136
137 std::shared_ptr<AbilityRuntime::ContextImpl> contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
138 std::shared_ptr<AbilityRuntime::ApplicationContext> applicationContext =
139 AbilityRuntime::ApplicationContext::GetInstance();
140 applicationContext->AttachContextImpl(contextImpl);
141 application->SetApplicationContext(applicationContext);
142
143 ExtensionBase<ExtensionContext> extensionBase;
144 extensionBase.Init(record, application, handler, token);
145 std::shared_ptr<ExtensionContext> context = extensionBase.GetContext();
146 EXPECT_STREQ(context->GetAbilityInfo()->name.c_str(), "ExtensionBaseTest");
147 EXPECT_TRUE(true);
148
149 TAG_LOGI(AAFwkTag::TEST, "GetContext end");
150 }
151
152 /**
153 * @tc.name: OnConfigurationUpdated_0100
154 * @tc.desc: basic function test.
155 * @tc.type: FUNC
156 */
157 HWTEST_F(AbilityExtensionBaseTest, OnConfigurationUpdated_0100, TestSize.Level1)
158 {
159 TAG_LOGI(AAFwkTag::TEST, "OnConfigurationUpdated start");
160
161 std::shared_ptr<AppExecFwk::AbilityInfo> info = std::make_shared<AppExecFwk::AbilityInfo>();
162 std::shared_ptr<AppExecFwk::AbilityLocalRecord> record =
163 std::make_shared<AppExecFwk::AbilityLocalRecord>(info, nullptr);
164 std::shared_ptr<AppExecFwk::OHOSApplication> application = std::make_shared<AppExecFwk::OHOSApplication>();
165 std::shared_ptr<AppExecFwk::AbilityHandler> handler = std::make_shared<AppExecFwk::AbilityHandler>(nullptr);
166 sptr<IRemoteObject> token = new AppExecFwk::MockAbilityToken();
167
168 std::shared_ptr<AbilityRuntime::ContextImpl> contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
169 std::shared_ptr<AbilityRuntime::ApplicationContext> applicationContext =
170 AbilityRuntime::ApplicationContext::GetInstance();
171 applicationContext->AttachContextImpl(contextImpl);
172 application->SetApplicationContext(applicationContext);
173
174 AppExecFwk::Configuration configuration;
175 application->SetConfiguration(configuration);
176
177 ExtensionBase<ExtensionContext> extensionBase;
178 extensionBase.Init(record, application, handler, token);
179 extensionBase.OnConfigurationUpdated(configuration);
180 EXPECT_TRUE(true);
181
182 TAG_LOGI(AAFwkTag::TEST, "OnConfigurationUpdated end");
183 }
184
185 /**
186 * @tc.name: OnMemoryLevel_0100
187 * @tc.desc: basic function test.
188 * @tc.type: FUNC
189 */
190 HWTEST_F(AbilityExtensionBaseTest, OnMemoryLevel_0100, TestSize.Level1)
191 {
192 TAG_LOGI(AAFwkTag::TEST, "OnMemoryLevel start");
193
194 std::shared_ptr<AppExecFwk::AbilityInfo> info = std::make_shared<AppExecFwk::AbilityInfo>();
195 std::shared_ptr<AppExecFwk::AbilityLocalRecord> record =
196 std::make_shared<AppExecFwk::AbilityLocalRecord>(info, nullptr);
197 std::shared_ptr<AppExecFwk::OHOSApplication> application = std::make_shared<AppExecFwk::OHOSApplication>();
198 std::shared_ptr<AppExecFwk::AbilityHandler> handler = std::make_shared<AppExecFwk::AbilityHandler>(nullptr);
199 sptr<IRemoteObject> token = new AppExecFwk::MockAbilityToken();
200
201 std::shared_ptr<AbilityRuntime::ContextImpl> contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
202 std::shared_ptr<AbilityRuntime::ApplicationContext> applicationContext =
203 AbilityRuntime::ApplicationContext::GetInstance();
204 applicationContext->AttachContextImpl(contextImpl);
205 application->SetApplicationContext(applicationContext);
206
207 ExtensionBase<ExtensionContext> extensionBase;
208 extensionBase.Init(record, application, handler, token);
209
210 int level = 0;
211 extensionBase.OnMemoryLevel(level);
212 EXPECT_TRUE(true);
213
214 TAG_LOGI(AAFwkTag::TEST, "OnMemoryLevel end");
215 }
216
217 /**
218 * @tc.name: SetExtensionCommon_0100
219 * @tc.desc: basic function test.
220 * @tc.type: FUNC
221 */
222 HWTEST_F(AbilityExtensionBaseTest, SetExtensionCommon_0100, TestSize.Level1)
223 {
224 TAG_LOGI(AAFwkTag::TEST, "SetExtensionCommon start");
225
226 std::shared_ptr<AppExecFwk::AbilityInfo> info = std::make_shared<AppExecFwk::AbilityInfo>();
227 std::shared_ptr<AppExecFwk::AbilityLocalRecord> record =
228 std::make_shared<AppExecFwk::AbilityLocalRecord>(info, nullptr);
229 std::shared_ptr<AppExecFwk::OHOSApplication> application = std::make_shared<AppExecFwk::OHOSApplication>();
230 std::shared_ptr<AppExecFwk::AbilityHandler> handler = std::make_shared<AppExecFwk::AbilityHandler>(nullptr);
231 sptr<IRemoteObject> token = new AppExecFwk::MockAbilityToken();
232
233 std::shared_ptr<AbilityRuntime::ContextImpl> contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
234 std::shared_ptr<AbilityRuntime::ApplicationContext> applicationContext =
235 AbilityRuntime::ApplicationContext::GetInstance();
236 applicationContext->AttachContextImpl(contextImpl);
237 application->SetApplicationContext(applicationContext);
238
239 ExtensionBase<ExtensionContext> extensionBase;
240 extensionBase.Init(record, application, handler, token);
241
242 Runtime::Options options;
243 std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options);
244 std::unique_ptr<NativeReference> jsObj;
245 extensionBase.SetExtensionCommon(JsExtensionCommon::Create(
246 static_cast<JsRuntime&>(*jsRuntime), static_cast<NativeReference&>(*jsObj), nullptr));
247 EXPECT_NE(extensionBase.extensionCommon_, nullptr);
248 EXPECT_TRUE(true);
249
250 TAG_LOGI(AAFwkTag::TEST, "SetExtensionCommon end");
251 }
252
253 } // namespace AbilityRuntime
254 } // namespace OHOS
255