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 "service_extension.h"
24
25 #include "hilog_wrapper.h"
26 #include "iremote_object.h"
27
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace AbilityRuntime {
32 class AbilityServiceExtensionTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp() override;
37 void TearDown() override;
38 };
39
SetUpTestCase(void)40 void AbilityServiceExtensionTest::SetUpTestCase(void)
41 {}
42
TearDownTestCase(void)43 void AbilityServiceExtensionTest::TearDownTestCase(void)
44 {}
45
SetUp()46 void AbilityServiceExtensionTest::SetUp()
47 {}
48
TearDown()49 void AbilityServiceExtensionTest::TearDown()
50 {}
51
52 /*
53 * Feature: ServiceExtension
54 * Function: Create
55 * SubFunction: NA
56 * FunctionPoints: Create
57 * EnvConditions: NA
58 * CaseDescription: Test the function of Create to create a ServiceExtension instance.
59 */
60 HWTEST_F(AbilityServiceExtensionTest, Create_0100, TestSize.Level1)
61 {
62 ServiceExtension *serviceExtension = ServiceExtension::Create(nullptr);
63 EXPECT_NE(serviceExtension, nullptr);
64 }
65
66 /*
67 * Feature: ServiceExtension
68 * Function: CreateAndInitContext
69 * SubFunction: NA
70 * FunctionPoints: CreateAndInitContext
71 * EnvConditions: NA
72 * CaseDescription: Test the function of CreateAndInitContext.
73 */
74 HWTEST_F(AbilityServiceExtensionTest, CreateAndInitContext_0100, TestSize.Level1)
75 {
76 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
77 sptr<IRemoteObject> token(new (std::nothrow) MockAbilityToken());
78
79 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
80 std::shared_ptr<AbilityRuntime::ContextImpl> contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
81 std::shared_ptr<AbilityRuntime::ApplicationContext> applicationContext =
82 AbilityRuntime::ApplicationContext::GetInstance();
83 applicationContext->AttachContextImpl(contextImpl);
84 application->SetApplicationContext(applicationContext);
85
86 std::shared_ptr<AbilityLocalRecord> record = std::make_shared<AbilityLocalRecord>(abilityInfo, token);
87 std::shared_ptr<AbilityHandler> handler = std::make_shared<AbilityHandler>(nullptr);
88
89 ServiceExtension *serviceExtension = ServiceExtension::Create(nullptr);
90 EXPECT_NE(serviceExtension, nullptr);
91
92 serviceExtension->Init(record, application, handler, token);
93
94 std::shared_ptr<ServiceExtensionContext> context =
95 serviceExtension->CreateAndInitContext(record, application, handler, token);
96 EXPECT_NE(context, nullptr);
97 }
98
99 /*
100 * Feature: ServiceExtension
101 * Function: Init
102 * SubFunction: NA
103 * FunctionPoints: Init
104 * EnvConditions: NA
105 * CaseDescription: Test the function of Init.
106 */
107 HWTEST_F(AbilityServiceExtensionTest, Init_0100, TestSize.Level1)
108 {
109 std::shared_ptr<AbilityInfo> abilityInfo = std::make_shared<AbilityInfo>();
110 sptr<IRemoteObject> token(new (std::nothrow) MockAbilityToken());
111
112 std::shared_ptr<OHOSApplication> application = std::make_shared<OHOSApplication>();
113 std::shared_ptr<AbilityRuntime::ContextImpl> contextImpl = std::make_shared<AbilityRuntime::ContextImpl>();
114 std::shared_ptr<AbilityRuntime::ApplicationContext> applicationContext =
115 AbilityRuntime::ApplicationContext::GetInstance();
116 applicationContext->AttachContextImpl(contextImpl);
117 application->SetApplicationContext(applicationContext);
118
119 std::shared_ptr<AbilityLocalRecord> record = std::make_shared<AbilityLocalRecord>(abilityInfo, token);
120 std::shared_ptr<AbilityHandler> handler = std::make_shared<AbilityHandler>(nullptr);
121
122 ServiceExtension *serviceExtension = ServiceExtension::Create(nullptr);
123 EXPECT_NE(serviceExtension, nullptr);
124
125 GTEST_LOG_(INFO) << "service extension Init start";
126
127 serviceExtension->Init(record, application, handler, token);
128
129 GTEST_LOG_(INFO) << "service extension Init end";
130 }
131 } // namespace AbilityRuntime
132 } // namespace OHOS