1 /*
2 * Copyright (c) 2022-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 "background_audio_controller.h"
18 #include "avsession_log.h"
19 #include "message_parcel.h"
20 #include "ability_connect_callback_interface.h"
21 #include "ability_manager_interface.h"
22 #include "avsession_descriptor.h"
23 #include "avsession_errors.h"
24 #include "ipc_types.h"
25 #include "iremote_object.h"
26 #include "ability_connect_helper.h"
27 #include "insight_intent_execute_param.h"
28
29 using namespace testing::ext;
30 using namespace OHOS::AVSession;
31
32 class AbilityConnectHelperTest : public testing::Test {
33 public:
34 static void SetUpTestCase(void);
35 static void TearDownTestCase(void);
36 void SetUp();
37 void TearDown();
38 };
39
SetUpTestCase()40 void AbilityConnectHelperTest::SetUpTestCase()
41 {
42 }
43
TearDownTestCase()44 void AbilityConnectHelperTest::TearDownTestCase()
45 {
46 }
47
SetUp()48 void AbilityConnectHelperTest::SetUp()
49 {
50 }
51
TearDown()52 void AbilityConnectHelperTest::TearDown()
53 {
54 }
55
56 /**
57 * @tc.name: OnRemoteRequest001
58 * @tc.desc:
59 * @tc.type: FUNC
60 * @tc.require: #I62OZV
61 */
62 static HWTEST(AbilityConnectHelperTest, OnRemoteRequest001, TestSize.Level1)
63 {
64 SLOGI("OnRemoteRequest001 begin!");
65 uint32_t code = OHOS::AAFwk::IAbilityConnection::ON_ABILITY_DISCONNECT_DONE;
66 AbilityConnectCallback abilityconnectcallback;
67 OHOS::MessageParcel data;
68 OHOS::MessageParcel reply;
69 OHOS::MessageOption option;
70 int ret = abilityconnectcallback.OnRemoteRequest(code, data, reply, option);
71 EXPECT_EQ(ret, OHOS::AVSession::AVSESSION_ERROR);
72 }
73
74 /**
75 * @tc.name: OnRemoteRequest002
76 * @tc.desc: AAFwk::IAbilityConnection::ON_ABILITY_DISCONNECT_DONE
77 * @tc.type: FUNC
78 * @tc.require: #I62OZV
79 */
80 static HWTEST(AbilityConnectHelperTest, OnRemoteRequest002, TestSize.Level1)
81 {
82 SLOGI("OnRemoteRequest002 begin!");
83 uint32_t code = OHOS::AAFwk::IAbilityConnection::ON_ABILITY_DISCONNECT_DONE;
84 AbilityConnectCallback abilityconnectcallback;
85 OHOS::MessageParcel data;
86 data.WriteInterfaceToken(OHOS::AVSession::AbilityConnectionStub::GetDescriptor());
87 OHOS::MessageParcel reply;
88 OHOS::MessageOption option;
89 OHOS::AAFwk::Want want;
90 OHOS::AppExecFwk::ElementName element("", "12345", "12345");
91 want.SetElement(element);
92 data.WriteParcelable(&want);
93 int ret = abilityconnectcallback.OnRemoteRequest(code, data, reply, option);
94 EXPECT_EQ(ret, OHOS::ERR_NONE);
95 }
96
97 /**
98 * @tc.name: OnRemoteRequest003
99 * @tc.desc: AAFwk::IAbilityConnection::ON_ABILITY_CONNECT_DONE&&remoteObject == nullptr
100 * @tc.type: FUNC
101 * @tc.require: #I62OZV
102 */
103 static HWTEST(AbilityConnectHelperTest, OnRemoteRequest003, TestSize.Level1)
104 {
105 SLOGI("OnRemoteRequest003 begin!");
106 uint32_t code = OHOS::AAFwk::IAbilityConnection::ON_ABILITY_CONNECT_DONE;
107 AbilityConnectCallback abilityconnectcallback;
108 OHOS::MessageParcel data;
109 data.WriteInterfaceToken(OHOS::AVSession::AbilityConnectionStub::GetDescriptor());
110 OHOS::MessageParcel reply;
111 OHOS::MessageOption option;
112 OHOS::AAFwk::Want want;
113 OHOS::AppExecFwk::ElementName element("", "12345", "12345");
114 want.SetElement(element);
115 data.WriteParcelable(&want);
116 int ret = abilityconnectcallback.OnRemoteRequest(code, data, reply, option);
117 EXPECT_EQ(ret, OHOS::AVSession::AVSESSION_ERROR);
118 }
119
120 /**
121 * @tc.name: OnRemoteRequest004
122 * @tc.desc: AAFwk::IAbilityConnection::ON_ABILITY_CONNECT_DONE
123 * @tc.type: FUNC
124 * @tc.require: #I62OZV
125 */
126 static HWTEST(AbilityConnectHelperTest, OnRemoteRequest004, TestSize.Level1)
127 {
128 SLOGI("OnRemoteRequest004 begin!");
129 uint32_t code = OHOS::AAFwk::IAbilityConnection::ON_ABILITY_CONNECT_DONE;
130 AbilityConnectCallback abilityconnectcallback;
131 OHOS::MessageParcel data;
132 data.WriteInterfaceToken(OHOS::AVSession::AbilityConnectionStub::GetDescriptor());
133 OHOS::MessageParcel reply;
134 OHOS::MessageOption option;
135 OHOS::AAFwk::Want want;
136 OHOS::AppExecFwk::ElementName element("", "12345", "12345");
137 want.SetElement(element);
138 data.WriteParcelable(&want);
139 OHOS::sptr<OHOS::AAFwk::IAbilityConnection> connect = new (std::nothrow) AbilityConnectCallback();
140 if (connect == nullptr) {
141 SLOGI("OnRemoteRequest004 connect is nullptr");
142 }
143 SLOGI("OnRemoteRequest004 connect is success");
144 data.WriteRemoteObject(connect->AsObject());
145 int ret = abilityconnectcallback.OnRemoteRequest(code, data, reply, option);
146 EXPECT_EQ(ret, OHOS::AVSession::AVSESSION_ERROR);
147 }
148
149 /**
150 * @tc.name: OnRemoteRequest005
151 * @tc.desc: uint32_t code = 3;
152 * @tc.type: FUNC
153 * @tc.require: #I62OZV
154 */
155 static HWTEST(AbilityConnectHelperTest, OnRemoteRequest005, TestSize.Level1)
156 {
157 SLOGI("OnRemoteRequest005 begin!");
158 uint32_t code = 3;
159 AbilityConnectCallback abilityconnectcallback;
160 OHOS::MessageParcel data;
161 data.WriteInterfaceToken(OHOS::AVSession::AbilityConnectionStub::GetDescriptor());
162 OHOS::MessageParcel reply;
163 OHOS::MessageOption option;
164 OHOS::AAFwk::Want want;
165 OHOS::AppExecFwk::ElementName element("", "12345", "12345");
166 want.SetElement(element);
167 data.WriteParcelable(&want);
168 int ret = abilityconnectcallback.OnRemoteRequest(code, data, reply, option);
169 EXPECT_EQ(ret, OHOS::IPC_STUB_UNKNOW_TRANS_ERR);
170 }
171
172 /**
173 * @tc.name: StartAbilityForegroundByCall001
174 * @tc.desc: Test StartAbilityForegroundByCall
175 * @tc.type: FUNC
176 */
177 static HWTEST(AbilityConnectHelperTest, StartAbilityForegroundByCall001, TestSize.Level1)
178 {
179 SLOGI("StartAbilityForegroundByCall001 begin!");
180 std::string bundleName;
181 std::string abilityName;
182 int32_t ret = AbilityConnectHelper::GetInstance().StartAbilityForegroundByCall(bundleName, abilityName);
183 EXPECT_EQ(ret, ERR_ABILITY_NOT_AVAILABLE);
184 }
185
186 /**
187 * @tc.name: StartAbilityForegroundByCall002
188 * @tc.desc: Test StartAbilityForegroundByCall
189 * @tc.type: FUNC
190 * @tc.require:
191 */
192 static HWTEST(AbilityConnectHelperTest, StartAbilityForegroundByCall002, TestSize.Level1)
193 {
194 SLOGI("StartAbilityForegroundByCall002 begin!");
195 std::string bundleName = "com.ohos.camera";
196 std::string abilityName = "test.ability";
197 int32_t ret = AbilityConnectHelper::GetInstance().StartAbilityForegroundByCall(bundleName, abilityName);
198 EXPECT_EQ(ret, ERR_ABILITY_NOT_AVAILABLE);
199 }
200
201 /**
202 * @tc.name: StartAbilityByCall001
203 * @tc.desc: Test StartAbilityByCall
204 * @tc.type: FUNC
205 */
206 static HWTEST(AbilityConnectHelperTest, StartAbilityByCall001, TestSize.Level1)
207 {
208 SLOGI("StartAbilityByCall001 begin!");
209 std::string bundleName;
210 std::string abilityName;
211 int32_t ret = AbilityConnectHelper::GetInstance().StartAbilityByCall(bundleName, abilityName);
212 EXPECT_EQ(ret, ERR_ABILITY_NOT_AVAILABLE);
213 }
214
215 /**
216 * @tc.name: StartAbilityByCall002
217 * @tc.desc: Test StartAbilityByCall
218 * @tc.type: FUNC
219 */
220 static HWTEST(AbilityConnectHelperTest, StartAbilityByCall002, TestSize.Level1)
221 {
222 SLOGI("StartAbilityByCall002 begin!");
223 std::string bundleName = "com.ohos.camera";
224 std::string abilityName = "test.ability";
225 int32_t ret = AbilityConnectHelper::GetInstance().StartAbilityByCall(bundleName, abilityName);
226 EXPECT_EQ(ret, ERR_ABILITY_NOT_AVAILABLE);
227 }
228