• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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_config.h"
19 #define private public
20 #include "dialog_session_manager.h"
21 #undef private
22 #include "hilog_tag_wrapper.h"
23 #include "mock_ability_token.h"
24 #include "start_ability_utils.h"
25 
26 using OHOS::AppExecFwk::AbilityType;
27 using namespace testing::ext;
28 
29 namespace OHOS {
30 namespace AAFwk {
31 namespace {
32 constexpr int32_t TEST_USER_ID = 10001;
33 constexpr int32_t TEST_ERMS_ISALLOW_RESULTCODE = 9;
34 const std::string TEST_BUNDLE_NAME = "com.test.demo";
35 const std::string TEST_DIALOG_SESSION_ID = "dialogSessionId";
36 }
37 
38 class DialogSessionManagerTest : public testing::Test {
39 public:
40     static void SetUpTestCase();
41     static void TearDownTestCase();
42     void SetUp() override;
43     void TearDown() override;
44     std::shared_ptr<AbilityRecord> MockAbilityRecord(AbilityType);
45     sptr<Token> MockToken(AbilityType);
46 };
47 
SetUpTestCase(void)48 void DialogSessionManagerTest::SetUpTestCase(void)
49 {}
50 
TearDownTestCase(void)51 void DialogSessionManagerTest::TearDownTestCase(void)
52 {}
53 
SetUp()54 void DialogSessionManagerTest::SetUp()
55 {}
56 
TearDown()57 void DialogSessionManagerTest::TearDown()
58 {}
59 
MockAbilityRecord(AbilityType abilityType)60 std::shared_ptr<AbilityRecord> DialogSessionManagerTest::MockAbilityRecord(AbilityType abilityType)
61 {
62     AbilityRequest abilityRequest;
63     abilityRequest.appInfo.bundleName = TEST_BUNDLE_NAME;
64     abilityRequest.abilityInfo.name = "MainAbility";
65     abilityRequest.abilityInfo.type = abilityType;
66     return AbilityRecord::CreateAbilityRecord(abilityRequest);
67 }
68 
MockToken(AbilityType abilityType)69 sptr<Token> DialogSessionManagerTest::MockToken(AbilityType abilityType)
70 {
71     std::shared_ptr<AbilityRecord> abilityRecord = MockAbilityRecord(abilityType);
72     if (!abilityRecord) {
73         return nullptr;
74     }
75     return abilityRecord->GetToken();
76 }
77 
78 /**
79  * @tc.name: GetStartupSessionInfoTest_0100
80  * @tc.desc: Test GetStartupSessionInfo
81  * @tc.type: FUNC
82  */
83 HWTEST_F(DialogSessionManagerTest, GetStartupSessionInfoTest_0100, TestSize.Level1)
84 {
85     GTEST_LOG_(INFO) << "GetStartupSessionInfoTest_0100 start";
86     DialogSessionManager dialogSessionManager;
87     std::shared_ptr<StartupSessionInfo> ret = dialogSessionManager.GetStartupSessionInfo(TEST_DIALOG_SESSION_ID);
88     EXPECT_EQ(ret, nullptr);
89 
90     AbilityRequest abilityRequest;
91     dialogSessionManager.SetStartupSessionInfo(TEST_DIALOG_SESSION_ID, abilityRequest);
92     ret = dialogSessionManager.GetStartupSessionInfo(TEST_DIALOG_SESSION_ID);
93     EXPECT_NE(ret, nullptr);
94     GTEST_LOG_(INFO) << "GetStartupSessionInfoTest_0100 end";
95 }
96 
97 /**
98  * @tc.name: SendDialogResultTest_0100
99  * @tc.desc: Test SendDialogResult
100  * @tc.type: FUNC
101  */
102 HWTEST_F(DialogSessionManagerTest, SendDialogResultTest_0100, TestSize.Level1)
103 {
104     GTEST_LOG_(INFO) << "SendDialogResultTest_0100 start";
105     AbilityRequest abilityRequest;
106     DialogSessionManager dialogSessionManager;
107     Want want;
108     bool isAllowed = false;
109     int32_t ret = dialogSessionManager.SendDialogResult(want, TEST_DIALOG_SESSION_ID, isAllowed);
110     EXPECT_EQ(ret, ERR_OK);
111 
112     isAllowed = true;
113     ret = dialogSessionManager.SendDialogResult(want, TEST_DIALOG_SESSION_ID, isAllowed);
114     EXPECT_EQ(ret, ERR_INVALID_VALUE);
115 
116     dialogSessionManager.SetStartupSessionInfo(TEST_DIALOG_SESSION_ID, abilityRequest);
117     ret = dialogSessionManager.SendDialogResult(want, TEST_DIALOG_SESSION_ID, isAllowed);
118     EXPECT_EQ(ret, ERR_INVALID_VALUE);
119     GTEST_LOG_(INFO) << "SendDialogResultTest_0100 end";
120 }
121 
122 /**
123  * @tc.name: SendDialogResultTest_0200
124  * @tc.desc: Test SendDialogResult
125  * @tc.type: FUNC
126  */
127 HWTEST_F(DialogSessionManagerTest, SendDialogResultTest_0200, TestSize.Level1)
128 {
129     GTEST_LOG_(INFO) << "SendDialogResultTest_0200 start";
130     AbilityRequest abilityRequest;
131     DialogSessionManager dialogSessionManager;
132     Want want;
133     bool isAllowed = true;
134     sptr<DialogSessionInfo> dilogSessionInfo = nullptr;
135     std::shared_ptr<DialogCallerInfo> dialogCallerInfo = std::make_shared<DialogCallerInfo>();
136 
137     dialogSessionManager.SetDialogSessionInfo(TEST_DIALOG_SESSION_ID, dilogSessionInfo, dialogCallerInfo);
138     int32_t ret = dialogSessionManager.SendDialogResult(want, TEST_DIALOG_SESSION_ID, isAllowed);
139     EXPECT_EQ(ret, ERR_INVALID_VALUE);
140 
141     want.SetParam(Want::PARAM_APP_CLONE_INDEX_KEY, 0);
142     ret = dialogSessionManager.SendDialogResult(want, TEST_DIALOG_SESSION_ID, isAllowed);
143     EXPECT_EQ(ret, ERR_INVALID_VALUE);
144     GTEST_LOG_(INFO) << "SendDialogResultTest_0200 end";
145 }
146 
147 /**
148  * @tc.name: NotifySCBToRecoveryAfterInterceptionTest_0100
149  * @tc.desc: Test NotifySCBToRecoveryAfterInterception
150  * @tc.type: FUNC
151  */
152 HWTEST_F(DialogSessionManagerTest, NotifySCBToRecoveryAfterInterceptionTest_0100, TestSize.Level1)
153 {
154     GTEST_LOG_(INFO) << "NotifySCBToRecoveryAfterInterceptionTest_0100 start";
155     AbilityRequest abilityRequest;
156     DialogSessionManager dialogSessionManager;
157 
158     int32_t ret = dialogSessionManager.NotifySCBToRecoveryAfterInterception(TEST_DIALOG_SESSION_ID, abilityRequest);
159     EXPECT_EQ(ret, ERR_INVALID_VALUE);
160     GTEST_LOG_(INFO) << "NotifySCBToRecoveryAfterInterceptionTest_0100 end";
161 }
162 
163 /**
164  * @tc.name: CreateModalDialogCommonTest_0100
165  * @tc.desc: Test CreateModalDialogCommon
166  * @tc.type: FUNC
167  */
168 HWTEST_F(DialogSessionManagerTest, CreateModalDialogCommonTest_0100, TestSize.Level1)
169 {
170     GTEST_LOG_(INFO) << "CreateModalDialogCommonTest_0100 start";
171     Want replaceWant;
172     sptr<IRemoteObject> callerToken = MockToken(AbilityType::PAGE);
173     DialogSessionManager dialogSessionManager;
174 
175     int32_t ret = dialogSessionManager.CreateModalDialogCommon(replaceWant, callerToken, TEST_DIALOG_SESSION_ID);
176     EXPECT_EQ(ret, ERR_INVALID_VALUE);
177     GTEST_LOG_(INFO) << "CreateModalDialogCommonTest_0100 end";
178 }
179 
180 /**
181  * @tc.name: IsCreateCloneSelectorDialogTest_0100
182  * @tc.desc: Test IsCreateCloneSelectorDialog
183  * @tc.type: FUNC
184  */
185 HWTEST_F(DialogSessionManagerTest, IsCreateCloneSelectorDialogTest_0100, TestSize.Level1)
186 {
187     GTEST_LOG_(INFO) << "IsCreateCloneSelectorDialogTest_0100 start";
188     DialogSessionManager dialogSessionManager;
189 
190     StartAbilityUtils::isWantWithAppCloneIndex = true;
191     bool ret = dialogSessionManager.IsCreateCloneSelectorDialog(TEST_BUNDLE_NAME, TEST_USER_ID);
192     EXPECT_FALSE(ret);
193     GTEST_LOG_(INFO) << "IsCreateCloneSelectorDialogTest_0100 end";
194 }
195 
196 /**
197  * @tc.name: UpdateExtensionWantWithDialogCallerInfo_0100
198  * @tc.desc: callerToken is nullptr
199  * @tc.type: FUNC
200  */
201 HWTEST_F(DialogSessionManagerTest, UpdateExtensionWantWithDialogCallerInfo_0100, TestSize.Level1)
202 {
203     GTEST_LOG_(INFO) << "UpdateExtensionWantWithDialogCallerInfo_0100 start";
204     std::string dialogSessionId = "";
205     AbilityRequest abilityRequest;
206     bool isSCBCall = false;
207     sptr<IRemoteObject> callerToken = nullptr;
208     abilityRequest.want.SetParam(TEST_DIALOG_SESSION_ID, dialogSessionId);
209     DialogSessionManager dialogSessionManager;
210     auto ret = dialogSessionManager.UpdateExtensionWantWithDialogCallerInfo(abilityRequest, callerToken, isSCBCall);
211     EXPECT_FALSE(ret);
212     GTEST_LOG_(INFO) << "UpdateExtensionWantWithDialogCallerInfo_0100 end";
213 }
214 
215 /**
216  * @tc.name: UpdateExtensionWantWithDialogCallerInfo_0200
217  * @tc.desc: dialogSessionId is empty
218  * @tc.type: FUNC
219  */
220 HWTEST_F(DialogSessionManagerTest, UpdateExtensionWantWithDialogCallerInfo_0200, TestSize.Level1)
221 {
222     GTEST_LOG_(INFO) << "UpdateExtensionWantWithDialogCallerInfo_0200 start";
223     std::string dialogSessionId = "";
224     AbilityRequest abilityRequest;
225     bool isSCBCall = false;
226     sptr<IRemoteObject> callerToken = sptr<AppExecFwk::MockAbilityToken>::MakeSptr();
227     abilityRequest.want.SetParam(TEST_DIALOG_SESSION_ID, dialogSessionId);
228     DialogSessionManager dialogSessionManager;
229     auto ret = dialogSessionManager.UpdateExtensionWantWithDialogCallerInfo(abilityRequest, callerToken, isSCBCall);
230     EXPECT_FALSE(ret);
231     GTEST_LOG_(INFO) << "UpdateExtensionWantWithDialogCallerInfo_0200 end";
232 }
233 
234 /**
235  * @tc.name: UpdateExtensionWantWithDialogCallerInfo_0300
236  * @tc.desc: can not find dialogCallerInfo by dialogSessionId
237  * @tc.type: FUNC
238  */
239 HWTEST_F(DialogSessionManagerTest, UpdateExtensionWantWithDialogCallerInfo_0300, TestSize.Level1)
240 {
241     GTEST_LOG_(INFO) << "UpdateExtensionWantWithDialogCallerInfo_0300 start";
242     std::string dialogSessionId = "1000001";
243     AbilityRequest abilityRequest;
244     bool isSCBCall = false;
245     sptr<IRemoteObject> callerToken = sptr<AppExecFwk::MockAbilityToken>::MakeSptr();
246     abilityRequest.want.SetParam(TEST_DIALOG_SESSION_ID, dialogSessionId);
247     DialogSessionManager dialogSessionManager;
248     auto ret = dialogSessionManager.UpdateExtensionWantWithDialogCallerInfo(abilityRequest, callerToken, isSCBCall);
249     EXPECT_FALSE(ret);
250     GTEST_LOG_(INFO) << "UpdateExtensionWantWithDialogCallerInfo_0300 end";
251 }
252 
253 /**
254  * @tc.name: UpdateExtensionWantWithDialogCallerInfo_0400
255  * @tc.desc: dialog callerInfo do not need grant uri permission
256  * @tc.type: FUNC
257  */
258 HWTEST_F(DialogSessionManagerTest, UpdateExtensionWantWithDialogCallerInfo_0400, TestSize.Level1)
259 {
260     GTEST_LOG_(INFO) << "UpdateExtensionWantWithDialogCallerInfo_0400 start";
261     std::string dialogSessionId = "1000001";
262     AbilityRequest abilityRequest;
263     bool isSCBCall = false;
264     sptr<IRemoteObject> callerToken = sptr<AppExecFwk::MockAbilityToken>::MakeSptr();
265     abilityRequest.want.SetParam(TEST_DIALOG_SESSION_ID, dialogSessionId);
266 
267     std::shared_ptr<DialogCallerInfo> dialogCallerInfo = std::make_shared<DialogCallerInfo>();
268     dialogCallerInfo->needGrantUriPermission = false;
269     DialogSessionManager dialogSessionManager;
270     dialogSessionManager.dialogCallerInfoMap_[dialogSessionId] = dialogCallerInfo;
271     auto ret = dialogSessionManager.UpdateExtensionWantWithDialogCallerInfo(abilityRequest, callerToken, isSCBCall);
272     EXPECT_FALSE(ret);
273     GTEST_LOG_(INFO) << "UpdateExtensionWantWithDialogCallerInfo_0400 end";
274 }
275 
276 /**
277  * @tc.name: UpdateExtensionWantWithDialogCallerInfo_0500
278  * @tc.desc: do not have uri permission flag
279  * @tc.type: FUNC
280  */
281 HWTEST_F(DialogSessionManagerTest, UpdateExtensionWantWithDialogCallerInfo_0500, TestSize.Level1)
282 {
283     GTEST_LOG_(INFO) << "UpdateExtensionWantWithDialogCallerInfo_0500 start";
284     std::string dialogSessionId = "1000001";
285     AbilityRequest abilityRequest;
286     bool isSCBCall = false;
287     sptr<IRemoteObject> callerToken = sptr<AppExecFwk::MockAbilityToken>::MakeSptr();
288     abilityRequest.want.SetParam(TEST_DIALOG_SESSION_ID, dialogSessionId);
289 
290     std::shared_ptr<DialogCallerInfo> dialogCallerInfo = std::make_shared<DialogCallerInfo>();
291     dialogCallerInfo->needGrantUriPermission = true;
292     DialogSessionManager dialogSessionManager;
293     dialogSessionManager.dialogCallerInfoMap_[dialogSessionId] = dialogCallerInfo;
294     auto ret = dialogSessionManager.UpdateExtensionWantWithDialogCallerInfo(abilityRequest, callerToken, isSCBCall);
295     EXPECT_FALSE(ret);
296     GTEST_LOG_(INFO) << "UpdateExtensionWantWithDialogCallerInfo_0500 end";
297 }
298 
299 /**
300  * @tc.name: UpdateExtensionWantWithDialogCallerInfo_0600
301  * @tc.desc: not scb call and have uri permission flag
302  * @tc.type: FUNC
303  */
304 HWTEST_F(DialogSessionManagerTest, UpdateExtensionWantWithDialogCallerInfo_0600, TestSize.Level1)
305 {
306     GTEST_LOG_(INFO) << "UpdateExtensionWantWithDialogCallerInfo_0600 start";
307     std::string dialogSessionId = "1000001";
308     AbilityRequest abilityRequest;
309     bool isSCBCall = false;
310     sptr<IRemoteObject> callerToken = sptr<AppExecFwk::MockAbilityToken>::MakeSptr();
311     abilityRequest.want.SetParam(TEST_DIALOG_SESSION_ID, dialogSessionId);
312 
313     std::shared_ptr<DialogCallerInfo> dialogCallerInfo = std::make_shared<DialogCallerInfo>();
314     uint32_t flag = 1;
315     dialogCallerInfo->targetWant.SetFlags(flag);
316     dialogCallerInfo->needGrantUriPermission = true;
317 
318     DialogSessionManager dialogSessionManager;
319     dialogSessionManager.dialogCallerInfoMap_[dialogSessionId] = dialogCallerInfo;
320     auto ret = dialogSessionManager.UpdateExtensionWantWithDialogCallerInfo(abilityRequest, callerToken, isSCBCall);
321     EXPECT_FALSE(ret);
322     GTEST_LOG_(INFO) << "UpdateExtensionWantWithDialogCallerInfo_0600 end";
323 }
324 
325 /**
326  * @tc.name: UpdateExtensionWantWithDialogCallerInfo_0700
327  * @tc.desc: scb call and have uri permission flag
328  * @tc.type: FUNC
329  */
330 HWTEST_F(DialogSessionManagerTest, UpdateExtensionWantWithDialogCallerInfo_0700, TestSize.Level1)
331 {
332     GTEST_LOG_(INFO) << "UpdateExtensionWantWithDialogCallerInfo_0600 start";
333     std::string dialogSessionId = "1000001";
334     AbilityRequest abilityRequest;
335     bool isSCBCall = true;
336     sptr<IRemoteObject> callerToken = sptr<AppExecFwk::MockAbilityToken>::MakeSptr();
337     abilityRequest.want.SetParam(TEST_DIALOG_SESSION_ID, dialogSessionId);
338 
339     std::shared_ptr<DialogCallerInfo> dialogCallerInfo = std::make_shared<DialogCallerInfo>();
340     uint32_t flag = 1;
341     std::string uri = "file://com.example.test/temp.txt";
342     std::vector<std::string> uriVec = { uri };
343     dialogCallerInfo->targetWant.SetFlags(flag);
344     dialogCallerInfo->targetWant.SetUri(uri);
345     dialogCallerInfo->targetWant.SetParam(AbilityConfig::PARAMS_STREAM, uriVec);
346     dialogCallerInfo->needGrantUriPermission = true;
347 
348     DialogSessionManager dialogSessionManager;
349     dialogSessionManager.dialogCallerInfoMap_[dialogSessionId] = dialogCallerInfo;
350     auto ret = dialogSessionManager.UpdateExtensionWantWithDialogCallerInfo(abilityRequest, callerToken, isSCBCall);
351     EXPECT_FALSE(ret);
352 
353     EXPECT_EQ(abilityRequest.want.GetFlags(), flag);
354     EXPECT_EQ(abilityRequest.want.GetUriString(), uri);
355     EXPECT_EQ(abilityRequest.want.GetStringArrayParam(AbilityConfig::PARAMS_STREAM).size(), 1);
356     GTEST_LOG_(INFO) << "UpdateExtensionWantWithDialogCallerInfo_0700 end";
357 }
358 }  // namespace AAFwk
359 }  // namespace OHOS
360