1 /*
2 * Copyright (c) 2024 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 #include "bundlemgr/mock_bundle_manager.h"
18 #include "mock_ability_connect_callback.h"
19 #include "ability_manager_errors.h"
20 #include "ability_scheduler.h"
21 #define private public
22 #define protected public
23 #include "ability_event_handler.h"
24 #include "ability_manager_service.h"
25 #undef private
26 #undef protected
27 #include "if_system_ability_manager.h"
28 #include "iservice_registry.h"
29 #define private public
30 #define protected public
31 #include "prepare_terminate_callback_proxy.h"
32 #include "pending_want_manager.h"
33 #undef private
34 #undef protected
35 #include "sa_mgr_client.h"
36 #include "sender_info.h"
37 #include "system_ability_definition.h"
38 #include "wants_info.h"
39 #include "want_receiver_stub.h"
40 #include "want_sender_stub.h"
41 #include "hilog_tag_wrapper.h"
42
43 using namespace testing;
44 using namespace testing::ext;
45 using namespace OHOS::AppExecFwk;
46 using OHOS::AppExecFwk::ElementName;
47
48 namespace OHOS {
49 namespace AAFwk {
50 class PrepareTerminateCallbackProxyTest : public testing::Test {
51 public:
52 static void SetUpTestCase();
53 static void TearDownTestCase();
54 void SetUp();
55 void TearDown();
56 sptr<Token> MockToken();
57 };
58
SetUpTestCase(void)59 void PrepareTerminateCallbackProxyTest::SetUpTestCase(void)
60 {}
61
TearDownTestCase(void)62 void PrepareTerminateCallbackProxyTest::TearDownTestCase(void)
63 {}
64
SetUp()65 void PrepareTerminateCallbackProxyTest::SetUp()
66 {}
67
TearDown()68 void PrepareTerminateCallbackProxyTest::TearDown()
69 {}
70
MockToken()71 sptr<Token> PrepareTerminateCallbackProxyTest::MockToken()
72 {
73 sptr<Token> token = nullptr;
74 AbilityRequest abilityRequest;
75 abilityRequest.appInfo.bundleName = "com.test.demo";
76 abilityRequest.abilityInfo.name = "MainAbility";
77 abilityRequest.abilityInfo.type = AbilityType::PAGE;
78 std::shared_ptr<AbilityRecord> abilityRecord = AbilityRecord::CreateAbilityRecord(abilityRequest);
79 if (!abilityRecord) {
80 return nullptr;
81 }
82
83 return abilityRecord->GetToken();
84 }
85
86 /**
87 * @tc.name: DoPrepareTerminate_0100
88 * @tc.desc: Test the state of DoPrepareTerminate
89 * @tc.type: FUNC
90 */
91 HWTEST_F(PrepareTerminateCallbackProxyTest, DoPrepareTerminate_0100, TestSize.Level1)
92 {
93 TAG_LOGI(AAFwkTag::TEST, "DoPrepareTerminate_0100 called start.");
94 sptr<IRemoteObject> impl;
95 auto Info = std::make_shared<PrepareTerminateCallbackProxy>(impl);
96 Info->DoPrepareTerminate();
97 EXPECT_EQ(impl, nullptr);
98 TAG_LOGI(AAFwkTag::TEST, "DoPrepareTerminate_0100 called end.");
99 }
100
101 /**
102 * @tc.name: DoPrepareTerminate_0200
103 * @tc.desc: Test the state of DoPrepareTerminate
104 * @tc.type: FUNC
105 */
106 HWTEST_F(PrepareTerminateCallbackProxyTest, DoPrepareTerminate_0200, TestSize.Level1)
107 {
108 TAG_LOGI(AAFwkTag::TEST, "DoPrepareTerminate_0200 called start.");
109 sptr<Token> remoteObject = MockToken();
110 sptr<IRemoteObject> impl(remoteObject);
111 auto Info = std::make_shared<PrepareTerminateCallbackProxy>(impl);
112 Info->DoPrepareTerminate();
113 EXPECT_NE(impl, nullptr);
114 TAG_LOGI(AAFwkTag::TEST, "DoPrepareTerminate_0200 called end.");
115 }
116 } // namespace AAFwk
117 } // namespace OHOS
118