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
18 #define private public
19 #define protected public
20 #include "update_caller_info_util.h"
21 #include "ability_manager_service.h"
22 #undef private
23 #undef protected
24 #include "ability_manager_errors.h"
25 #include "hilog_tag_wrapper.h"
26 #include "string_wrapper.h"
27
28 using namespace testing;
29 using namespace testing::ext;
30 using namespace OHOS::AppExecFwk;
31 using OHOS::AppExecFwk::AbilityType;
32 using OHOS::AppExecFwk::ExtensionAbilityType;
33 namespace OHOS {
34 namespace AAFwk {
35 class UpdateCallerInfoUtilTest : public testing::Test {
36 public:
37 static void SetUpTestCase();
38 static void TearDownTestCase();
39 void SetUp();
40 void TearDown();
41 std::shared_ptr<AbilityRecord> MockAbilityRecord(AbilityType);
42 sptr<Token> MockToken(AbilityType);
43 sptr<SessionInfo> MockSessionInfo(int32_t persistentId);
44
45 public:
46 AbilityRequest abilityRequest_{};
47 Want want_{};
48 };
49
MockAbilityRecord(AbilityType abilityType)50 std::shared_ptr<AbilityRecord> UpdateCallerInfoUtilTest::MockAbilityRecord(AbilityType abilityType)
51 {
52 AbilityRequest abilityRequest;
53 abilityRequest.appInfo.bundleName = "com.test.demo";
54 abilityRequest.abilityInfo.name = "MainAbility";
55 abilityRequest.abilityInfo.type = abilityType;
56 return AbilityRecord::CreateAbilityRecord(abilityRequest);
57 }
58
MockToken(AbilityType abilityType)59 sptr<Token> UpdateCallerInfoUtilTest::MockToken(AbilityType abilityType)
60 {
61 std::shared_ptr<AbilityRecord> abilityRecord = MockAbilityRecord(abilityType);
62 if (!abilityRecord) {
63 return nullptr;
64 }
65 return abilityRecord->GetToken();
66 }
67
MockSessionInfo(int32_t persistentId)68 sptr<SessionInfo> UpdateCallerInfoUtilTest::MockSessionInfo(int32_t persistentId)
69 {
70 sptr<SessionInfo> sessionInfo = new (std::nothrow) SessionInfo();
71 if (!sessionInfo) {
72 TAG_LOGE(AAFwkTag::TEST, "sessionInfo is nullptr");
73 return nullptr;
74 }
75 sessionInfo->persistentId = persistentId;
76 return sessionInfo;
77 }
78
SetUpTestCase()79 void UpdateCallerInfoUtilTest::SetUpTestCase() {}
80
TearDownTestCase()81 void UpdateCallerInfoUtilTest::TearDownTestCase() {}
82
SetUp()83 void UpdateCallerInfoUtilTest::SetUp() {}
84
TearDown()85 void UpdateCallerInfoUtilTest::TearDown() {}
86
87 /**
88 * @tc.name: UpdateCallerInfoUtilTest_UpdateAsCallerInfoFromCallerRecord_0001
89 * @tc.desc: Test the state of QueryAllAutoStartupApplications
90 * @tc.type: FUNC
91 */
92 HWTEST_F(UpdateCallerInfoUtilTest, UpdateAsCallerInfoFromCallerRecord_0001, TestSize.Level1)
93 {
94 std::shared_ptr<UpdateCallerInfoUtil> updateCallerUtil = std::make_shared<UpdateCallerInfoUtil>();
95 Want want;
96 sptr<IRemoteObject> callerToken = nullptr;
97 std::shared_ptr<AbilityRecord> abilityRecord = MockAbilityRecord(AbilityType::PAGE);
98 callerToken = abilityRecord->GetToken();
99
100 updateCallerUtil->UpdateAsCallerInfoFromCallerRecord(want, callerToken);
101 EXPECT_NE(callerToken, nullptr);
102 }
103
104 /**
105 * @tc.name: UpdateCallerInfoUtilTest_UpdateCallerInfoFromToken_0001
106 * @tc.desc: Test the state of UpdateCallerInfoFromToken
107 * @tc.type: FUNC
108 */
109 HWTEST_F(UpdateCallerInfoUtilTest, UpdateCallerInfoFromToken_0001, TestSize.Level1)
110 {
111 std::shared_ptr<UpdateCallerInfoUtil> updateCallerUtil = std::make_shared<UpdateCallerInfoUtil>();
112 Want want;
113 sptr<IRemoteObject> callerToken = nullptr;
114 std::shared_ptr<AbilityRecord> abilityRecord = MockAbilityRecord(AbilityType::PAGE);
115 callerToken = abilityRecord->GetToken();
116
117 updateCallerUtil->UpdateCallerInfoFromToken(want, callerToken);
118 EXPECT_NE(abilityRecord, nullptr);
119
120 abilityRecord = nullptr;
121 updateCallerUtil->UpdateCallerInfoFromToken(want, callerToken);
122 EXPECT_EQ(abilityRecord, nullptr);
123 }
124 } // namespace AAFwk
125 } // namespace OHOS
126