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 "avsession_errors.h"
18 #include "avsession_log.h"
19 #include "insight_intent_execute_param.h"
20 #define private public
21 #define protected public
22 #include "bundle_status_adapter.h"
23 #undef protected
24 #undef private
25 
26 using namespace testing::ext;
27 using namespace OHOS::AVSession;
28 
29 class BundleStatusAdapterTest : public testing::Test {
30 public:
31     static void SetUpTestCase(void);
32     static void TearDownTestCase(void);
33     void SetUp();
34     void TearDown();
35 };
36 
SetUpTestCase()37 void BundleStatusAdapterTest::SetUpTestCase()
38 {
39 }
40 
TearDownTestCase()41 void BundleStatusAdapterTest::TearDownTestCase()
42 {
43 }
44 
SetUp()45 void BundleStatusAdapterTest::SetUp()
46 {
47     BundleStatusAdapter::GetInstance().Init();
48 }
49 
TearDown()50 void BundleStatusAdapterTest::TearDown()
51 {
52 }
53 
54 /**
55  * @tc.name: GetBundleNameFromUid001
56  * @tc.desc: Test GetBundleNameFromUid
57  * @tc.type: FUNC
58  */
HWTEST_F(BundleStatusAdapterTest, GetBundleNameFromUid001, testing::ext::TestSize.Level1)59 static HWTEST_F(BundleStatusAdapterTest, GetBundleNameFromUid001, testing::ext::TestSize.Level1)
60 {
61     SLOGI("GetBundleNameFromUid001, start");
62     const int32_t uid = 0;
63     BundleStatusAdapter::GetInstance().GetBundleNameFromUid(uid);
64     EXPECT_EQ(uid, 0);
65     SLOGI("GetBundleNameFromUid001, end");
66 }
67 
68 /**
69  * @tc.name: CheckBundleSupport001
70  * @tc.desc: Test CheckBundleSupport
71  * @tc.type: FUNC
72  */
HWTEST_F(BundleStatusAdapterTest, CheckBundleSupport001, testing::ext::TestSize.Level1)73 static HWTEST_F(BundleStatusAdapterTest, CheckBundleSupport001, testing::ext::TestSize.Level1)
74 {
75     SLOGI("CheckBundleSupport001, start");
76     std::string profile = "";
77     bool ret = BundleStatusAdapter::GetInstance().CheckBundleSupport(profile);
78     EXPECT_EQ(ret, false);
79     SLOGI("CheckBundleSupport001, end");
80 }
81 
82 /**
83  * @tc.name: CheckBundleSupport002
84  * @tc.desc: Test CheckBundleSupport
85  * @tc.type: FUNC
86  */
HWTEST_F(BundleStatusAdapterTest, CheckBundleSupport002, testing::ext::TestSize.Level1)87 static HWTEST_F(BundleStatusAdapterTest, CheckBundleSupport002, testing::ext::TestSize.Level1)
88 {
89     SLOGI("CheckBundleSupport002, start");
90     std::string profile = R"({
91         "insightIntents": [
92             {
93                 "intentName": "OTHER_INTENT",
94                 "uiAbility": {
95                     "executeMode": ["background"]
96                 }
97             }
98         ]
99     })";
100     bool ret = BundleStatusAdapter::GetInstance().CheckBundleSupport(profile);
101     EXPECT_EQ(ret, false);
102     SLOGI("CheckBundleSupport002, end");
103 }
104 
105 /**
106  * @tc.name: CheckBundleSupport003
107  * @tc.desc: Test CheckBundleSupport
108  * @tc.type: FUNC
109  */
HWTEST_F(BundleStatusAdapterTest, CheckBundleSupport003, testing::ext::TestSize.Level1)110 static HWTEST_F(BundleStatusAdapterTest, CheckBundleSupport003, testing::ext::TestSize.Level1)
111 {
112     SLOGI("CheckBundleSupport003, start");
113     std::string profile = R"({
114         "insightIntents": [
115             {
116                 "intentName": "PLAY_MUSICLIST",
117                 "uiAbility": {
118                     "executeMode": ["background"]
119                 }
120             }
121         ]
122     })";
123     bool ret = BundleStatusAdapter::GetInstance().CheckBundleSupport(profile);
124     EXPECT_EQ(ret, false);
125     SLOGI("CheckBundleSupport003, end");
126 }
127 
128 /**
129  * @tc.name: CheckBundleSupport004
130  * @tc.desc: Test CheckBundleSupport
131  * @tc.type: FUNC
132  */
HWTEST_F(BundleStatusAdapterTest, CheckBundleSupport004, testing::ext::TestSize.Level1)133 static HWTEST_F(BundleStatusAdapterTest, CheckBundleSupport004, testing::ext::TestSize.Level1)
134 {
135     SLOGI("CheckBundleSupport004, start");
136     std::string profile = R"({
137         "insightIntents": [
138             {
139                 "intentName": "PLAY_AUDIO",
140                 "uiAbility": {
141                     "executeMode": ["background"]
142                 }
143             }
144         ]
145     })";
146     bool ret = BundleStatusAdapter::GetInstance().CheckBundleSupport(profile);
147     EXPECT_EQ(ret, false);
148     SLOGI("CheckBundleSupport004, end");
149 }
150 
151 /**
152  * @tc.name: SubscribeBundleStatusEvent001
153  * @tc.desc: Test SubscribeBundleStatusEvent
154  * @tc.type: FUNC
155  */
HWTEST_F(BundleStatusAdapterTest, SubscribeBundleStatusEvent001, testing::ext::TestSize.Level1)156 static HWTEST_F(BundleStatusAdapterTest, SubscribeBundleStatusEvent001, testing::ext::TestSize.Level1)
157 {
158     SLOGI("SubscribeBundleStatusEvent001, start");
159     std::string bundleName = "";
160     auto callback = [bundleName](const std::string& capturedBundleName, int32_t userId) {
161         SLOGI("SubscribeBundleStatusEvent001: get bundle name: %{public}s, userId: %{public}d",
162             capturedBundleName.c_str(), userId);
163     };
164     bool ret = BundleStatusAdapter::GetInstance().SubscribeBundleStatusEvent(bundleName, callback);
165     EXPECT_EQ(ret, false);
166     SLOGI("SubscribeBundleStatusEvent001, end");
167 }
168 
169 /**
170  * @tc.name: SubscribeBundleStatusEvent002
171  * @tc.desc: Test SubscribeBundleStatusEvent
172  * @tc.type: FUNC
173  */
HWTEST_F(BundleStatusAdapterTest, SubscribeBundleStatusEvent002, testing::ext::TestSize.Level1)174 static HWTEST_F(BundleStatusAdapterTest, SubscribeBundleStatusEvent002, testing::ext::TestSize.Level1)
175 {
176     SLOGI("SubscribeBundleStatusEvent002, start");
177     std::string bundleName = "com.ohos.camera";
178     auto callback = [bundleName](const std::string& capturedBundleName, int32_t userId) {
179         SLOGI("SubscribeBundleStatusEvent002: get bundle name: %{public}s, userId: %{public}d",
180             capturedBundleName.c_str(), userId);
181     };
182     bool ret = BundleStatusAdapter::GetInstance().SubscribeBundleStatusEvent(bundleName, callback);
183     EXPECT_EQ(ret, true);
184     SLOGI("SubscribeBundleStatusEvent002, end");
185 }
186 
187 /**
188  * @tc.name: IsAudioPlayback001
189  * @tc.desc: Test IsAudioPlayback
190  * @tc.type: FUNC
191  */
HWTEST_F(BundleStatusAdapterTest, IsAudioPlayback001, testing::ext::TestSize.Level1)192 static HWTEST_F(BundleStatusAdapterTest, IsAudioPlayback001, testing::ext::TestSize.Level1)
193 {
194     SLOGI("IsAudioPlayback001, start");
195     std::string bundleName = "";
196     std::string abilityName = "";
197     bool ret = BundleStatusAdapter::GetInstance().IsAudioPlayback(bundleName, abilityName);
198     EXPECT_EQ(ret, false);
199     SLOGI("IsAudioPlayback001, end");
200 }
201 
202 /**
203  * @tc.name: IsAudioPlayback002
204  * @tc.desc: Test IsAudioPlayback
205  * @tc.type: FUNC
206  */
HWTEST_F(BundleStatusAdapterTest, IsAudioPlayback002, testing::ext::TestSize.Level1)207 static HWTEST_F(BundleStatusAdapterTest, IsAudioPlayback002, testing::ext::TestSize.Level1)
208 {
209     SLOGI("IsAudioPlayback002, start");
210     std::string bundleName = "com.ohos.screenshot";
211     std::string abilityName = "MainAbility";
212     auto callback = [bundleName](const std::string& capturedBundleName, int32_t userId) {
213         SLOGI("SubscribeBundleStatusEvent003: get bundle name: %{public}s, userId: %{public}d",
214             capturedBundleName.c_str(), userId);
215     };
216     BundleStatusAdapter::GetInstance().SubscribeBundleStatusEvent(bundleName, callback);
217     bool ret = BundleStatusAdapter::GetInstance().IsAudioPlayback(bundleName, abilityName);
218     EXPECT_EQ(ret, false);
219     SLOGI("IsAudioPlayback002, end");
220 }
221 
222 /**
223  * @tc.name: NotifyBundleRemoved001
224  * @tc.desc: Test NotifyBundleRemoved
225  * @tc.type: FUNC
226  */
HWTEST_F(BundleStatusAdapterTest, NotifyBundleRemoved001, testing::ext::TestSize.Level1)227 static HWTEST_F(BundleStatusAdapterTest, NotifyBundleRemoved001, testing::ext::TestSize.Level1)
228 {
229     SLOGI("NotifyBundleRemoved001, start");
230     std::string bundleName = "com.ohos.screenshot";
231     std::string abilityName = "MainAbility";
232     auto callback = [bundleName](const std::string& capturedBundleName, int32_t userId) {
233         SLOGI("SubscribeBundleStatusEvent004: get bundle name: %{public}s, userId: %{public}d",
234             capturedBundleName.c_str(), userId);
235     };
236     BundleStatusAdapter::GetInstance().SubscribeBundleStatusEvent(bundleName, callback);
237     BundleStatusAdapter::GetInstance().NotifyBundleRemoved(bundleName, 100);
238     EXPECT_EQ(bundleName, "com.ohos.screenshot");
239     SLOGI("NotifyBundleRemoved001, end");
240 }
241 
242 /**
243  * @tc.name: NotifyBundleRemoved002
244  * @tc.desc: Test NotifyBundleRemoved
245  * @tc.type: FUNC
246  */
HWTEST_F(BundleStatusAdapterTest, NotifyBundleRemoved002, testing::ext::TestSize.Level1)247 static HWTEST_F(BundleStatusAdapterTest, NotifyBundleRemoved002, testing::ext::TestSize.Level1)
248 {
249     SLOGI("NotifyBundleRemoved002, start");
250     std::string bundleName = "com.ohos.test";
251     std::string abilityName = "MainAbility";
252     BundleStatusAdapter::GetInstance().NotifyBundleRemoved(bundleName, 100);
253     EXPECT_EQ(bundleName, "com.ohos.test");
254     SLOGI("NotifyBundleRemoved002, end");
255 }
256 
257 /**
258  * @tc.name: IsSupportPlayIntent001
259  * @tc.desc: Test IsSupportPlayIntent
260  * @tc.type: FUNC
261  */
HWTEST_F(BundleStatusAdapterTest, IsSupportPlayIntent001, testing::ext::TestSize.Level1)262 static HWTEST_F(BundleStatusAdapterTest, IsSupportPlayIntent001, testing::ext::TestSize.Level1)
263 {
264     SLOGI("IsSupportPlayIntent001, start");
265     std::string bundleName = "";
266     std::string supportModule = "";
267     std::string profile = "";
268     bool ret = BundleStatusAdapter::GetInstance().IsSupportPlayIntent(bundleName, supportModule, profile);
269     EXPECT_EQ(ret, false);
270     SLOGI("IsSupportPlayIntent001, end");
271 }
272 
273 /**
274  * @tc.name: IsSupportPlayIntent002
275  * @tc.desc: Test IsSupportPlayIntent
276  * @tc.type: FUNC
277  */
HWTEST_F(BundleStatusAdapterTest, IsSupportPlayIntent002, testing::ext::TestSize.Level1)278 static HWTEST_F(BundleStatusAdapterTest, IsSupportPlayIntent002, testing::ext::TestSize.Level1)
279 {
280     SLOGI("IsSupportPlayIntent002, start");
281     std::string bundleName = "com.IsSupportPlayIntent.test";
282     std::string supportModule = "";
283     std::string profile = "";
284     auto callback = [bundleName](const std::string& capturedBundleName, int32_t userId) {
285         SLOGI("SubscribeBundleStatusEvent005: get bundle name: %{public}s, userId: %{public}d",
286             capturedBundleName.c_str(), userId);
287     };
288     BundleStatusAdapter::GetInstance().SubscribeBundleStatusEvent(bundleName, callback);
289     bool ret = BundleStatusAdapter::GetInstance().IsSupportPlayIntent(bundleName, supportModule, profile);
290     EXPECT_EQ(ret, false);
291     SLOGI("IsSupportPlayIntent002, end");
292 }