blob: 41f3ada58d0658cdccaa4d33101f29fb503ed177 [file] [log] [blame]
hkuang26587cb2020-01-16 10:36:08 -08001/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// Unit Test for TranscodingClientManager
18
19// #define LOG_NDEBUG 0
20#define LOG_TAG "TranscodingClientManagerTest"
21
Chong Zhang6d58e4b2020-03-31 09:41:10 -070022#include <aidl/android/media/BnTranscodingClientCallback.h>
hkuang26587cb2020-01-16 10:36:08 -080023#include <aidl/android/media/IMediaTranscodingService.h>
hkuang26587cb2020-01-16 10:36:08 -080024#include <android-base/logging.h>
25#include <android/binder_manager.h>
26#include <android/binder_process.h>
27#include <gtest/gtest.h>
Chong Zhang6d58e4b2020-03-31 09:41:10 -070028#include <media/SchedulerClientInterface.h>
hkuang26587cb2020-01-16 10:36:08 -080029#include <media/TranscodingClientManager.h>
Chong Zhang6d58e4b2020-03-31 09:41:10 -070030#include <media/TranscodingRequest.h>
hkuang26587cb2020-01-16 10:36:08 -080031#include <utils/Log.h>
32
Chong Zhang6d58e4b2020-03-31 09:41:10 -070033#include <list>
34
hkuang26587cb2020-01-16 10:36:08 -080035namespace android {
36
37using Status = ::ndk::ScopedAStatus;
Chong Zhang6d58e4b2020-03-31 09:41:10 -070038using ::aidl::android::media::BnTranscodingClientCallback;
39using ::aidl::android::media::IMediaTranscodingService;
40using ::aidl::android::media::TranscodingErrorCode;
41using ::aidl::android::media::TranscodingJobParcel;
Chong Zhang15c192a2020-05-05 16:24:00 -070042using ::aidl::android::media::TranscodingJobPriority;
Chong Zhang6d58e4b2020-03-31 09:41:10 -070043using ::aidl::android::media::TranscodingRequestParcel;
44using ::aidl::android::media::TranscodingResultParcel;
hkuang26587cb2020-01-16 10:36:08 -080045
Chong Zhang3f23e982020-09-24 14:03:41 -070046constexpr pid_t kInvalidClientPid = -5;
47constexpr pid_t kInvalidClientUid = -10;
Chong Zhang8e062632020-03-31 10:56:37 -070048constexpr const char* kInvalidClientName = "";
49constexpr const char* kInvalidClientPackage = "";
hkuang26587cb2020-01-16 10:36:08 -080050
Chong Zhang8e062632020-03-31 10:56:37 -070051constexpr const char* kClientName = "TestClientName";
52constexpr const char* kClientPackage = "TestClientPackage";
hkuang26587cb2020-01-16 10:36:08 -080053
Chong Zhang15c192a2020-05-05 16:24:00 -070054#define JOB(n) (n)
55
Chong Zhang6d58e4b2020-03-31 09:41:10 -070056struct TestClientCallback : public BnTranscodingClientCallback {
57 TestClientCallback() { ALOGI("TestClientCallback Created"); }
hkuang26587cb2020-01-16 10:36:08 -080058
Chong Zhang6d58e4b2020-03-31 09:41:10 -070059 virtual ~TestClientCallback() { ALOGI("TestClientCallback destroyed"); };
60
hkuang19253092020-06-01 09:10:49 -070061 Status openFileDescriptor(const std::string& /*in_fileUri*/, const std::string& /*in_mode*/,
62 ::ndk::ScopedFileDescriptor* /*_aidl_return*/) override {
63 return Status::ok();
64 }
65
hkuang96471b82020-06-08 11:12:46 -070066 Status onTranscodingStarted(int32_t /*in_jobId*/) override { return Status::ok(); }
67
68 Status onTranscodingPaused(int32_t /*in_jobId*/) override { return Status::ok(); }
69
70 Status onTranscodingResumed(int32_t /*in_jobId*/) override { return Status::ok(); }
71
Chong Zhang6d58e4b2020-03-31 09:41:10 -070072 Status onTranscodingFinished(int32_t in_jobId,
73 const TranscodingResultParcel& in_result) override {
74 EXPECT_EQ(in_jobId, in_result.jobId);
75 mEventQueue.push_back(Finished(in_jobId));
hkuang26587cb2020-01-16 10:36:08 -080076 return Status::ok();
77 }
78
Chong Zhang6d58e4b2020-03-31 09:41:10 -070079 Status onTranscodingFailed(int32_t in_jobId, TranscodingErrorCode /*in_errorCode */) override {
80 mEventQueue.push_back(Failed(in_jobId));
hkuang26587cb2020-01-16 10:36:08 -080081 return Status::ok();
82 }
83
84 Status onAwaitNumberOfJobsChanged(int32_t /* in_jobId */, int32_t /* in_oldAwaitNumber */,
85 int32_t /* in_newAwaitNumber */) override {
86 return Status::ok();
87 }
88
89 Status onProgressUpdate(int32_t /* in_jobId */, int32_t /* in_progress */) override {
90 return Status::ok();
91 }
92
Chong Zhang6d58e4b2020-03-31 09:41:10 -070093 struct Event {
94 enum {
95 NoEvent,
96 Finished,
97 Failed,
98 } type;
Chong Zhang3fa408f2020-04-30 11:04:28 -070099 JobIdType jobId;
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700100 };
101
102 static constexpr Event NoEvent = {Event::NoEvent, 0};
103#define DECLARE_EVENT(action) \
Chong Zhang3fa408f2020-04-30 11:04:28 -0700104 static Event action(JobIdType jobId) { return {Event::action, jobId}; }
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700105
106 DECLARE_EVENT(Finished);
107 DECLARE_EVENT(Failed);
108
109 const Event& popEvent() {
110 if (mEventQueue.empty()) {
111 mPoppedEvent = NoEvent;
112 } else {
113 mPoppedEvent = *mEventQueue.begin();
114 mEventQueue.pop_front();
115 }
116 return mPoppedEvent;
117 }
hkuang26587cb2020-01-16 10:36:08 -0800118
Chong Zhang8e062632020-03-31 10:56:37 -0700119private:
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700120 Event mPoppedEvent;
121 std::list<Event> mEventQueue;
122
123 TestClientCallback(const TestClientCallback&) = delete;
124 TestClientCallback& operator=(const TestClientCallback&) = delete;
125};
126
127bool operator==(const TestClientCallback::Event& lhs, const TestClientCallback::Event& rhs) {
128 return lhs.type == rhs.type && lhs.jobId == rhs.jobId;
129}
130
131struct TestScheduler : public SchedulerClientInterface {
132 TestScheduler() { ALOGI("TestScheduler Created"); }
133
134 virtual ~TestScheduler() { ALOGI("TestScheduler Destroyed"); }
135
Chong Zhang3fa408f2020-04-30 11:04:28 -0700136 bool submit(ClientIdType clientId, JobIdType jobId, uid_t /*uid*/,
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700137 const TranscodingRequestParcel& request,
138 const std::weak_ptr<ITranscodingClientCallback>& clientCallback) override {
139 JobKeyType jobKey = std::make_pair(clientId, jobId);
140 if (mJobs.count(jobKey) > 0) {
141 return false;
142 }
143
144 // This is the secret name we'll check, to test error propagation from
145 // the scheduler back to client.
hkuang72d105f2020-05-21 10:48:55 -0700146 if (request.sourceFilePath == "bad_source_file") {
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700147 return false;
148 }
149
150 mJobs[jobKey].request = request;
151 mJobs[jobKey].callback = clientCallback;
152
153 mLastJob = jobKey;
154 return true;
155 }
156
Chong Zhang3fa408f2020-04-30 11:04:28 -0700157 bool cancel(ClientIdType clientId, JobIdType jobId) override {
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700158 JobKeyType jobKey = std::make_pair(clientId, jobId);
159
160 if (mJobs.count(jobKey) == 0) {
161 return false;
162 }
163 mJobs.erase(jobKey);
164 return true;
165 }
166
Chong Zhang3fa408f2020-04-30 11:04:28 -0700167 bool getJob(ClientIdType clientId, JobIdType jobId,
168 TranscodingRequestParcel* request) override {
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700169 JobKeyType jobKey = std::make_pair(clientId, jobId);
170 if (mJobs.count(jobKey) == 0) {
171 return false;
172 }
173
174 *(TranscodingRequest*)request = mJobs[jobKey].request;
175 return true;
176 }
177
178 void finishLastJob() {
179 auto it = mJobs.find(mLastJob);
180 if (it == mJobs.end()) {
181 return;
182 }
183 {
184 auto clientCallback = it->second.callback.lock();
185 if (clientCallback != nullptr) {
186 clientCallback->onTranscodingFinished(
hkuang0bd73742020-06-24 12:57:09 -0700187 mLastJob.second,
188 TranscodingResultParcel({mLastJob.second, 0, std::nullopt}));
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700189 }
190 }
191 mJobs.erase(it);
192 }
193
194 void abortLastJob() {
195 auto it = mJobs.find(mLastJob);
196 if (it == mJobs.end()) {
197 return;
198 }
199 {
200 auto clientCallback = it->second.callback.lock();
201 if (clientCallback != nullptr) {
Chong Zhang7ae4e2f2020-04-17 15:24:34 -0700202 clientCallback->onTranscodingFailed(mLastJob.second,
203 TranscodingErrorCode::kUnknown);
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700204 }
205 }
206 mJobs.erase(it);
207 }
208
209 struct Job {
210 TranscodingRequest request;
211 std::weak_ptr<ITranscodingClientCallback> callback;
212 };
213
Chong Zhang3fa408f2020-04-30 11:04:28 -0700214 typedef std::pair<ClientIdType, JobIdType> JobKeyType;
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700215 std::map<JobKeyType, Job> mJobs;
216 JobKeyType mLastJob;
hkuang26587cb2020-01-16 10:36:08 -0800217};
218
219class TranscodingClientManagerTest : public ::testing::Test {
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700220public:
221 TranscodingClientManagerTest()
222 : mScheduler(new TestScheduler()),
223 mClientManager(new TranscodingClientManager(mScheduler)) {
hkuang5172cab2020-01-31 12:40:28 -0800224 ALOGD("TranscodingClientManagerTest created");
225 }
hkuang26587cb2020-01-16 10:36:08 -0800226
227 void SetUp() override {
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700228 mClientCallback1 = ::ndk::SharedRefBase::make<TestClientCallback>();
229 mClientCallback2 = ::ndk::SharedRefBase::make<TestClientCallback>();
230 mClientCallback3 = ::ndk::SharedRefBase::make<TestClientCallback>();
hkuang26587cb2020-01-16 10:36:08 -0800231 }
232
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700233 void TearDown() override { ALOGI("TranscodingClientManagerTest tear down"); }
hkuang26587cb2020-01-16 10:36:08 -0800234
235 ~TranscodingClientManagerTest() { ALOGD("TranscodingClientManagerTest destroyed"); }
236
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700237 void addMultipleClients() {
Chong Zhang3f23e982020-09-24 14:03:41 -0700238 EXPECT_EQ(mClientManager->addClient(mClientCallback1, kClientName,
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700239 kClientPackage, &mClient1),
240 OK);
241 EXPECT_NE(mClient1, nullptr);
242
Chong Zhang3f23e982020-09-24 14:03:41 -0700243 EXPECT_EQ(mClientManager->addClient(mClientCallback2, kClientName,
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700244 kClientPackage, &mClient2),
245 OK);
246 EXPECT_NE(mClient2, nullptr);
247
Chong Zhang3f23e982020-09-24 14:03:41 -0700248 EXPECT_EQ(mClientManager->addClient(mClientCallback3, kClientName,
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700249 kClientPackage, &mClient3),
250 OK);
251 EXPECT_NE(mClient3, nullptr);
252
253 EXPECT_EQ(mClientManager->getNumOfClients(), 3);
254 }
255
256 void unregisterMultipleClients() {
257 EXPECT_TRUE(mClient1->unregister().isOk());
258 EXPECT_TRUE(mClient2->unregister().isOk());
259 EXPECT_TRUE(mClient3->unregister().isOk());
260 EXPECT_EQ(mClientManager->getNumOfClients(), 0);
261 }
262
263 std::shared_ptr<TestScheduler> mScheduler;
264 std::shared_ptr<TranscodingClientManager> mClientManager;
265 std::shared_ptr<ITranscodingClient> mClient1;
266 std::shared_ptr<ITranscodingClient> mClient2;
267 std::shared_ptr<ITranscodingClient> mClient3;
268 std::shared_ptr<TestClientCallback> mClientCallback1;
269 std::shared_ptr<TestClientCallback> mClientCallback2;
270 std::shared_ptr<TestClientCallback> mClientCallback3;
hkuang26587cb2020-01-16 10:36:08 -0800271};
272
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700273TEST_F(TranscodingClientManagerTest, TestAddingWithInvalidClientCallback) {
274 // Add a client with null callback and expect failure.
Chong Zhang8e062632020-03-31 10:56:37 -0700275 std::shared_ptr<ITranscodingClient> client;
Chong Zhang3f23e982020-09-24 14:03:41 -0700276 status_t err = mClientManager->addClient(nullptr, kClientName,
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700277 kClientPackage, &client);
Chong Zhang15c192a2020-05-05 16:24:00 -0700278 EXPECT_EQ(err, IMediaTranscodingService::ERROR_ILLEGAL_ARGUMENT);
hkuang26587cb2020-01-16 10:36:08 -0800279}
Chong Zhang3f23e982020-09-24 14:03:41 -0700280//
281//TEST_F(TranscodingClientManagerTest, TestAddingWithInvalidClientPid) {
282// // Add a client with invalid Pid and expect failure.
283// std::shared_ptr<ITranscodingClient> client;
284// status_t err = mClientManager->addClient(mClientCallback1,
285// kClientName, kClientPackage, &client);
286// EXPECT_EQ(err, IMediaTranscodingService::ERROR_ILLEGAL_ARGUMENT);
287//}
hkuang26587cb2020-01-16 10:36:08 -0800288
Chong Zhang8e062632020-03-31 10:56:37 -0700289TEST_F(TranscodingClientManagerTest, TestAddingWithInvalidClientName) {
290 // Add a client with invalid name and expect failure.
291 std::shared_ptr<ITranscodingClient> client;
Chong Zhang3f23e982020-09-24 14:03:41 -0700292 status_t err = mClientManager->addClient(mClientCallback1,
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700293 kInvalidClientName, kClientPackage, &client);
Chong Zhang15c192a2020-05-05 16:24:00 -0700294 EXPECT_EQ(err, IMediaTranscodingService::ERROR_ILLEGAL_ARGUMENT);
hkuang26587cb2020-01-16 10:36:08 -0800295}
296
297TEST_F(TranscodingClientManagerTest, TestAddingWithInvalidClientPackageName) {
Chong Zhang8e062632020-03-31 10:56:37 -0700298 // Add a client with invalid packagename and expect failure.
299 std::shared_ptr<ITranscodingClient> client;
Chong Zhang3f23e982020-09-24 14:03:41 -0700300 status_t err = mClientManager->addClient(mClientCallback1, kClientName,
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700301 kInvalidClientPackage, &client);
Chong Zhang15c192a2020-05-05 16:24:00 -0700302 EXPECT_EQ(err, IMediaTranscodingService::ERROR_ILLEGAL_ARGUMENT);
hkuang26587cb2020-01-16 10:36:08 -0800303}
304
305TEST_F(TranscodingClientManagerTest, TestAddingValidClient) {
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700306 // Add a valid client, should succeed.
Chong Zhang8e062632020-03-31 10:56:37 -0700307 std::shared_ptr<ITranscodingClient> client;
Chong Zhang3f23e982020-09-24 14:03:41 -0700308 status_t err = mClientManager->addClient(mClientCallback1, kClientName,
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700309 kClientPackage, &client);
Chong Zhang8e062632020-03-31 10:56:37 -0700310 EXPECT_EQ(err, OK);
311 EXPECT_NE(client.get(), nullptr);
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700312 EXPECT_EQ(mClientManager->getNumOfClients(), 1);
hkuang26587cb2020-01-16 10:36:08 -0800313
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700314 // Unregister client, should succeed.
Chong Zhang8e062632020-03-31 10:56:37 -0700315 Status status = client->unregister();
316 EXPECT_TRUE(status.isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700317 EXPECT_EQ(mClientManager->getNumOfClients(), 0);
hkuang26587cb2020-01-16 10:36:08 -0800318}
319
320TEST_F(TranscodingClientManagerTest, TestAddingDupliacteClient) {
Chong Zhang8e062632020-03-31 10:56:37 -0700321 std::shared_ptr<ITranscodingClient> client;
Chong Zhang3f23e982020-09-24 14:03:41 -0700322 status_t err = mClientManager->addClient(mClientCallback1, kClientName,
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700323 kClientPackage, &client);
Chong Zhang8e062632020-03-31 10:56:37 -0700324 EXPECT_EQ(err, OK);
325 EXPECT_NE(client.get(), nullptr);
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700326 EXPECT_EQ(mClientManager->getNumOfClients(), 1);
hkuang26587cb2020-01-16 10:36:08 -0800327
Chong Zhang8e062632020-03-31 10:56:37 -0700328 std::shared_ptr<ITranscodingClient> dupClient;
Chong Zhang3f23e982020-09-24 14:03:41 -0700329 err = mClientManager->addClient(mClientCallback1, "dupClient",
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700330 "dupPackage", &dupClient);
Chong Zhang15c192a2020-05-05 16:24:00 -0700331 EXPECT_EQ(err, IMediaTranscodingService::ERROR_ALREADY_EXISTS);
Chong Zhang8e062632020-03-31 10:56:37 -0700332 EXPECT_EQ(dupClient.get(), nullptr);
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700333 EXPECT_EQ(mClientManager->getNumOfClients(), 1);
hkuang26587cb2020-01-16 10:36:08 -0800334
Chong Zhang8e062632020-03-31 10:56:37 -0700335 Status status = client->unregister();
336 EXPECT_TRUE(status.isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700337 EXPECT_EQ(mClientManager->getNumOfClients(), 0);
hkuang26587cb2020-01-16 10:36:08 -0800338
Chong Zhang3f23e982020-09-24 14:03:41 -0700339 err = mClientManager->addClient(mClientCallback1, "dupClient", "dupPackage", &dupClient);
Chong Zhang8e062632020-03-31 10:56:37 -0700340 EXPECT_EQ(err, OK);
341 EXPECT_NE(dupClient.get(), nullptr);
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700342 EXPECT_EQ(mClientManager->getNumOfClients(), 1);
hkuang26587cb2020-01-16 10:36:08 -0800343
Chong Zhang8e062632020-03-31 10:56:37 -0700344 status = dupClient->unregister();
345 EXPECT_TRUE(status.isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700346 EXPECT_EQ(mClientManager->getNumOfClients(), 0);
hkuang26587cb2020-01-16 10:36:08 -0800347}
348
349TEST_F(TranscodingClientManagerTest, TestAddingMultipleClient) {
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700350 addMultipleClients();
351 unregisterMultipleClients();
352}
hkuang26587cb2020-01-16 10:36:08 -0800353
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700354TEST_F(TranscodingClientManagerTest, TestSubmitCancelGetJobs) {
355 addMultipleClients();
hkuang26587cb2020-01-16 10:36:08 -0800356
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700357 // Test jobId assignment.
358 TranscodingRequestParcel request;
hkuang72d105f2020-05-21 10:48:55 -0700359 request.sourceFilePath = "test_source_file_0";
360 request.destinationFilePath = "test_desintaion_file_0";
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700361 TranscodingJobParcel job;
362 bool result;
363 EXPECT_TRUE(mClient1->submitRequest(request, &job, &result).isOk());
364 EXPECT_TRUE(result);
Chong Zhang15c192a2020-05-05 16:24:00 -0700365 EXPECT_EQ(job.jobId, JOB(0));
hkuang26587cb2020-01-16 10:36:08 -0800366
hkuang72d105f2020-05-21 10:48:55 -0700367 request.sourceFilePath = "test_source_file_1";
368 request.destinationFilePath = "test_desintaion_file_1";
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700369 EXPECT_TRUE(mClient1->submitRequest(request, &job, &result).isOk());
370 EXPECT_TRUE(result);
Chong Zhang15c192a2020-05-05 16:24:00 -0700371 EXPECT_EQ(job.jobId, JOB(1));
hkuang26587cb2020-01-16 10:36:08 -0800372
hkuang72d105f2020-05-21 10:48:55 -0700373 request.sourceFilePath = "test_source_file_2";
374 request.destinationFilePath = "test_desintaion_file_2";
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700375 EXPECT_TRUE(mClient1->submitRequest(request, &job, &result).isOk());
376 EXPECT_TRUE(result);
Chong Zhang15c192a2020-05-05 16:24:00 -0700377 EXPECT_EQ(job.jobId, JOB(2));
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700378
hkuang72d105f2020-05-21 10:48:55 -0700379 // Test submit bad request (no valid sourceFilePath) fails.
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700380 TranscodingRequestParcel badRequest;
hkuang72d105f2020-05-21 10:48:55 -0700381 badRequest.sourceFilePath = "bad_source_file";
382 badRequest.destinationFilePath = "bad_destination_file";
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700383 EXPECT_TRUE(mClient1->submitRequest(badRequest, &job, &result).isOk());
384 EXPECT_FALSE(result);
385
Chong Zhang3f23e982020-09-24 14:03:41 -0700386 // Test submit with bad pid/uid.
387 badRequest.sourceFilePath = "test_source_file_3";
388 badRequest.destinationFilePath = "test_desintaion_file_3";
389 badRequest.clientPid = kInvalidClientPid;
390 badRequest.clientUid = kInvalidClientUid;
391 EXPECT_TRUE(mClient1->submitRequest(badRequest, &job, &result).isOk());
392 EXPECT_FALSE(result);
393
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700394 // Test get jobs by id.
Chong Zhang15c192a2020-05-05 16:24:00 -0700395 EXPECT_TRUE(mClient1->getJobWithId(JOB(2), &job, &result).isOk());
396 EXPECT_EQ(job.jobId, JOB(2));
hkuang72d105f2020-05-21 10:48:55 -0700397 EXPECT_EQ(job.request.sourceFilePath, "test_source_file_2");
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700398 EXPECT_TRUE(result);
399
400 // Test get jobs by invalid id fails.
Chong Zhang15c192a2020-05-05 16:24:00 -0700401 EXPECT_TRUE(mClient1->getJobWithId(JOB(100), &job, &result).isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700402 EXPECT_FALSE(result);
403
404 // Test cancel non-existent job fail.
Chong Zhang15c192a2020-05-05 16:24:00 -0700405 EXPECT_TRUE(mClient2->cancelJob(JOB(100), &result).isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700406 EXPECT_FALSE(result);
407
408 // Test cancel valid jobId in arbitrary order.
Chong Zhang15c192a2020-05-05 16:24:00 -0700409 EXPECT_TRUE(mClient1->cancelJob(JOB(2), &result).isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700410 EXPECT_TRUE(result);
411
Chong Zhang15c192a2020-05-05 16:24:00 -0700412 EXPECT_TRUE(mClient1->cancelJob(JOB(0), &result).isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700413 EXPECT_TRUE(result);
414
Chong Zhang15c192a2020-05-05 16:24:00 -0700415 EXPECT_TRUE(mClient1->cancelJob(JOB(1), &result).isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700416 EXPECT_TRUE(result);
417
418 // Test cancel job again fails.
Chong Zhang15c192a2020-05-05 16:24:00 -0700419 EXPECT_TRUE(mClient1->cancelJob(JOB(1), &result).isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700420 EXPECT_FALSE(result);
421
422 // Test get job after cancel fails.
Chong Zhang15c192a2020-05-05 16:24:00 -0700423 EXPECT_TRUE(mClient1->getJobWithId(JOB(2), &job, &result).isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700424 EXPECT_FALSE(result);
425
426 // Test jobId independence for each client.
427 EXPECT_TRUE(mClient2->submitRequest(request, &job, &result).isOk());
428 EXPECT_TRUE(result);
Chong Zhang15c192a2020-05-05 16:24:00 -0700429 EXPECT_EQ(job.jobId, JOB(0));
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700430
431 EXPECT_TRUE(mClient2->submitRequest(request, &job, &result).isOk());
432 EXPECT_TRUE(result);
Chong Zhang15c192a2020-05-05 16:24:00 -0700433 EXPECT_EQ(job.jobId, JOB(1));
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700434
435 unregisterMultipleClients();
436}
437
438TEST_F(TranscodingClientManagerTest, TestClientCallback) {
439 addMultipleClients();
440
441 TranscodingRequestParcel request;
hkuang72d105f2020-05-21 10:48:55 -0700442 request.sourceFilePath = "test_source_file_name";
443 request.destinationFilePath = "test_destination_file_name";
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700444 TranscodingJobParcel job;
445 bool result;
446 EXPECT_TRUE(mClient1->submitRequest(request, &job, &result).isOk());
447 EXPECT_TRUE(result);
Chong Zhang15c192a2020-05-05 16:24:00 -0700448 EXPECT_EQ(job.jobId, JOB(0));
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700449
450 mScheduler->finishLastJob();
451 EXPECT_EQ(mClientCallback1->popEvent(), TestClientCallback::Finished(job.jobId));
452
453 EXPECT_TRUE(mClient1->submitRequest(request, &job, &result).isOk());
454 EXPECT_TRUE(result);
Chong Zhang15c192a2020-05-05 16:24:00 -0700455 EXPECT_EQ(job.jobId, JOB(1));
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700456
457 mScheduler->abortLastJob();
458 EXPECT_EQ(mClientCallback1->popEvent(), TestClientCallback::Failed(job.jobId));
459
460 EXPECT_TRUE(mClient1->submitRequest(request, &job, &result).isOk());
461 EXPECT_TRUE(result);
Chong Zhang15c192a2020-05-05 16:24:00 -0700462 EXPECT_EQ(job.jobId, JOB(2));
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700463
464 EXPECT_TRUE(mClient2->submitRequest(request, &job, &result).isOk());
465 EXPECT_TRUE(result);
Chong Zhang15c192a2020-05-05 16:24:00 -0700466 EXPECT_EQ(job.jobId, JOB(0));
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700467
468 mScheduler->finishLastJob();
469 EXPECT_EQ(mClientCallback2->popEvent(), TestClientCallback::Finished(job.jobId));
470
471 unregisterMultipleClients();
hkuang26587cb2020-01-16 10:36:08 -0800472}
473
Chong Zhang15c192a2020-05-05 16:24:00 -0700474TEST_F(TranscodingClientManagerTest, TestUseAfterUnregister) {
475 // Add a client.
476 std::shared_ptr<ITranscodingClient> client;
Chong Zhang3f23e982020-09-24 14:03:41 -0700477 status_t err = mClientManager->addClient(mClientCallback1, kClientName,
Chong Zhang15c192a2020-05-05 16:24:00 -0700478 kClientPackage, &client);
479 EXPECT_EQ(err, OK);
480 EXPECT_NE(client.get(), nullptr);
481
482 // Submit 2 requests, 1 offline and 1 realtime.
483 TranscodingRequestParcel request;
484 TranscodingJobParcel job;
485 bool result;
486
hkuang72d105f2020-05-21 10:48:55 -0700487 request.sourceFilePath = "test_source_file_0";
488 request.destinationFilePath = "test_destination_file_0";
Chong Zhang15c192a2020-05-05 16:24:00 -0700489 request.priority = TranscodingJobPriority::kUnspecified;
490 EXPECT_TRUE(client->submitRequest(request, &job, &result).isOk() && result);
491 EXPECT_EQ(job.jobId, JOB(0));
492
hkuang72d105f2020-05-21 10:48:55 -0700493 request.sourceFilePath = "test_source_file_1";
494 request.destinationFilePath = "test_destination_file_1";
Chong Zhang15c192a2020-05-05 16:24:00 -0700495 request.priority = TranscodingJobPriority::kNormal;
496 EXPECT_TRUE(client->submitRequest(request, &job, &result).isOk() && result);
497 EXPECT_EQ(job.jobId, JOB(1));
498
499 // Unregister client, should succeed.
500 Status status = client->unregister();
501 EXPECT_TRUE(status.isOk());
502
503 // Test submit new request after unregister, should fail with ERROR_DISCONNECTED.
hkuang72d105f2020-05-21 10:48:55 -0700504 request.sourceFilePath = "test_source_file_2";
505 request.destinationFilePath = "test_destination_file_2";
Chong Zhang15c192a2020-05-05 16:24:00 -0700506 request.priority = TranscodingJobPriority::kNormal;
507 status = client->submitRequest(request, &job, &result);
508 EXPECT_FALSE(status.isOk());
509 EXPECT_EQ(status.getServiceSpecificError(), IMediaTranscodingService::ERROR_DISCONNECTED);
510
511 // Test cancel jobs after unregister, should fail with ERROR_DISCONNECTED
512 // regardless of realtime or offline job, or whether the jobId is valid.
513 status = client->cancelJob(JOB(0), &result);
514 EXPECT_FALSE(status.isOk());
515 EXPECT_EQ(status.getServiceSpecificError(), IMediaTranscodingService::ERROR_DISCONNECTED);
516
517 status = client->cancelJob(JOB(1), &result);
518 EXPECT_FALSE(status.isOk());
519 EXPECT_EQ(status.getServiceSpecificError(), IMediaTranscodingService::ERROR_DISCONNECTED);
520
521 status = client->cancelJob(JOB(2), &result);
522 EXPECT_FALSE(status.isOk());
523 EXPECT_EQ(status.getServiceSpecificError(), IMediaTranscodingService::ERROR_DISCONNECTED);
524
525 // Test get jobs, should fail with ERROR_DISCONNECTED regardless of realtime
526 // or offline job, or whether the jobId is valid.
527 status = client->getJobWithId(JOB(0), &job, &result);
528 EXPECT_FALSE(status.isOk());
529 EXPECT_EQ(status.getServiceSpecificError(), IMediaTranscodingService::ERROR_DISCONNECTED);
530
531 status = client->getJobWithId(JOB(1), &job, &result);
532 EXPECT_FALSE(status.isOk());
533 EXPECT_EQ(status.getServiceSpecificError(), IMediaTranscodingService::ERROR_DISCONNECTED);
534
535 status = client->getJobWithId(JOB(2), &job, &result);
536 EXPECT_FALSE(status.isOk());
537 EXPECT_EQ(status.getServiceSpecificError(), IMediaTranscodingService::ERROR_DISCONNECTED);
538}
539
Chong Zhang8e062632020-03-31 10:56:37 -0700540} // namespace android