• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_EQ(extensionBase.extensionCommon_, nullptr);
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     auto record = std::make_shared<AppExecFwk::AbilityLocalRecord>(info, nullptr, nullptr, 0);
100     std::shared_ptr<AppExecFwk::OHOSApplication> application = std::make_shared<AppExecFwk::OHOSApplication>();
101     std::shared_ptr<AppExecFwk::AbilityHandler> handler = std::make_shared<AppExecFwk::AbilityHandler>(nullptr);
102     sptr<IRemoteObject> token = new AppExecFwk::MockAbilityToken();
103 
104     std::shared_ptr<AbilityRuntime::ContextImpl> contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
105     std::shared_ptr<AbilityRuntime::ApplicationContext> applicationContext =
106         AbilityRuntime::ApplicationContext::GetInstance();
107     applicationContext->AttachContextImpl(contextImpl);
108     application->SetApplicationContext(applicationContext);
109 
110     ExtensionBase<ExtensionContext> extensionBase;
111     extensionBase.Init(record, application, handler, token);
112     std::shared_ptr<ExtensionContext> context = extensionBase.CreateAndInitContext(record, application, handler, token);
113     EXPECT_STREQ(context->GetAbilityInfo()->name.c_str(), "ExtensionBaseTest");
114 
115     TAG_LOGI(AAFwkTag::TEST, "CreateAndInitContext end");
116 }
117 
118 /**
119  * @tc.name: GetContext_0100
120  * @tc.desc: basic function test.
121  * @tc.type: FUNC
122  */
123 HWTEST_F(AbilityExtensionBaseTest, GetContext_0100, TestSize.Level1)
124 {
125     TAG_LOGI(AAFwkTag::TEST, "GetContext start");
126 
127     std::shared_ptr<AppExecFwk::AbilityInfo> info = std::make_shared<AppExecFwk::AbilityInfo>();
128     info->name = "ExtensionBaseTest";
129     auto record = std::make_shared<AppExecFwk::AbilityLocalRecord>(info, nullptr, nullptr, 0);
130     std::shared_ptr<AppExecFwk::OHOSApplication> application = std::make_shared<AppExecFwk::OHOSApplication>();
131     std::shared_ptr<AppExecFwk::AbilityHandler> handler = std::make_shared<AppExecFwk::AbilityHandler>(nullptr);
132     sptr<IRemoteObject> token = new AppExecFwk::MockAbilityToken();
133 
134     std::shared_ptr<AbilityRuntime::ContextImpl> contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
135     std::shared_ptr<AbilityRuntime::ApplicationContext> applicationContext =
136         AbilityRuntime::ApplicationContext::GetInstance();
137     applicationContext->AttachContextImpl(contextImpl);
138     application->SetApplicationContext(applicationContext);
139 
140     ExtensionBase<ExtensionContext> extensionBase;
141     extensionBase.Init(record, application, handler, token);
142     std::shared_ptr<ExtensionContext> context = extensionBase.GetContext();
143     EXPECT_STREQ(context->GetAbilityInfo()->name.c_str(), "ExtensionBaseTest");
144 
145     TAG_LOGI(AAFwkTag::TEST, "GetContext end");
146 }
147 
148 /**
149  * @tc.name: OnConfigurationUpdated_0100
150  * @tc.desc: basic function test.
151  * @tc.type: FUNC
152  */
153 HWTEST_F(AbilityExtensionBaseTest, OnConfigurationUpdated_0100, TestSize.Level1)
154 {
155     TAG_LOGI(AAFwkTag::TEST, "OnConfigurationUpdated start");
156 
157     std::shared_ptr<AppExecFwk::AbilityInfo> info = std::make_shared<AppExecFwk::AbilityInfo>();
158     auto record = std::make_shared<AppExecFwk::AbilityLocalRecord>(info, nullptr, nullptr, 0);
159     std::shared_ptr<AppExecFwk::OHOSApplication> application = std::make_shared<AppExecFwk::OHOSApplication>();
160     std::shared_ptr<AppExecFwk::AbilityHandler> handler = std::make_shared<AppExecFwk::AbilityHandler>(nullptr);
161     sptr<IRemoteObject> token = new AppExecFwk::MockAbilityToken();
162 
163     std::shared_ptr<AbilityRuntime::ContextImpl> contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
164     std::shared_ptr<AbilityRuntime::ApplicationContext> applicationContext =
165         AbilityRuntime::ApplicationContext::GetInstance();
166     applicationContext->AttachContextImpl(contextImpl);
167     application->SetApplicationContext(applicationContext);
168 
169     AppExecFwk::Configuration configuration;
170     application->SetConfiguration(configuration);
171 
172     ExtensionBase<ExtensionContext> extensionBase;
173     extensionBase.Init(record, application, handler, token);
174     extensionBase.OnConfigurationUpdated(configuration);
175     EXPECT_EQ(extensionBase.extensionCommon_, nullptr);
176 
177     TAG_LOGI(AAFwkTag::TEST, "OnConfigurationUpdated end");
178 }
179 
180 /**
181  * @tc.name: OnMemoryLevel_0100
182  * @tc.desc: basic function test.
183  * @tc.type: FUNC
184  */
185 HWTEST_F(AbilityExtensionBaseTest, OnMemoryLevel_0100, TestSize.Level1)
186 {
187     TAG_LOGI(AAFwkTag::TEST, "OnMemoryLevel start");
188 
189     std::shared_ptr<AppExecFwk::AbilityInfo> info = std::make_shared<AppExecFwk::AbilityInfo>();
190     auto record = std::make_shared<AppExecFwk::AbilityLocalRecord>(info, nullptr, nullptr, 0);
191     std::shared_ptr<AppExecFwk::OHOSApplication> application = std::make_shared<AppExecFwk::OHOSApplication>();
192     std::shared_ptr<AppExecFwk::AbilityHandler> handler = std::make_shared<AppExecFwk::AbilityHandler>(nullptr);
193     sptr<IRemoteObject> token = new AppExecFwk::MockAbilityToken();
194 
195     std::shared_ptr<AbilityRuntime::ContextImpl> contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
196     std::shared_ptr<AbilityRuntime::ApplicationContext> applicationContext =
197         AbilityRuntime::ApplicationContext::GetInstance();
198     applicationContext->AttachContextImpl(contextImpl);
199     application->SetApplicationContext(applicationContext);
200 
201     ExtensionBase<ExtensionContext> extensionBase;
202     extensionBase.Init(record, application, handler, token);
203 
204     int level = 0;
205     extensionBase.OnMemoryLevel(level);
206     EXPECT_EQ(extensionBase.extensionCommon_, nullptr);
207 
208     TAG_LOGI(AAFwkTag::TEST, "OnMemoryLevel end");
209 }
210 
211 /**
212  * @tc.name: SetExtensionCommon_0100
213  * @tc.desc: basic function test.
214  * @tc.type: FUNC
215  */
216 HWTEST_F(AbilityExtensionBaseTest, SetExtensionCommon_0100, TestSize.Level1)
217 {
218     TAG_LOGI(AAFwkTag::TEST, "SetExtensionCommon start");
219 
220     std::shared_ptr<AppExecFwk::AbilityInfo> info = std::make_shared<AppExecFwk::AbilityInfo>();
221     auto record = std::make_shared<AppExecFwk::AbilityLocalRecord>(info, nullptr, nullptr, 0);
222     std::shared_ptr<AppExecFwk::OHOSApplication> application = std::make_shared<AppExecFwk::OHOSApplication>();
223     std::shared_ptr<AppExecFwk::AbilityHandler> handler = std::make_shared<AppExecFwk::AbilityHandler>(nullptr);
224     sptr<IRemoteObject> token = new AppExecFwk::MockAbilityToken();
225 
226     std::shared_ptr<AbilityRuntime::ContextImpl> contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
227     std::shared_ptr<AbilityRuntime::ApplicationContext> applicationContext =
228         AbilityRuntime::ApplicationContext::GetInstance();
229     applicationContext->AttachContextImpl(contextImpl);
230     application->SetApplicationContext(applicationContext);
231 
232     ExtensionBase<ExtensionContext> extensionBase;
233     extensionBase.Init(record, application, handler, token);
234 
235     Runtime::Options options;
236     std::unique_ptr<Runtime> jsRuntime = JsRuntime::Create(options);
237     std::unique_ptr<NativeReference> jsObj;
238     extensionBase.SetExtensionCommon(JsExtensionCommon::Create(
239         static_cast<JsRuntime&>(*jsRuntime), static_cast<NativeReference&>(*jsObj), nullptr));
240     EXPECT_NE(extensionBase.extensionCommon_, nullptr);
241 
242     TAG_LOGI(AAFwkTag::TEST, "SetExtensionCommon end");
243 }
244 
245 }  // namespace AbilityRuntime
246 }  // namespace OHOS
247