blob: c2ca9b4d5984c403ea96c64e0fdf1fb1c4eb535f [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 Zhang0579c6f2020-10-05 12:03:34 -0700238 EXPECT_EQ(
239 mClientManager->addClient(mClientCallback1, kClientName, kClientPackage, &mClient1),
240 OK);
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700241 EXPECT_NE(mClient1, nullptr);
242
Chong Zhang0579c6f2020-10-05 12:03:34 -0700243 EXPECT_EQ(
244 mClientManager->addClient(mClientCallback2, kClientName, kClientPackage, &mClient2),
245 OK);
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700246 EXPECT_NE(mClient2, nullptr);
247
Chong Zhang0579c6f2020-10-05 12:03:34 -0700248 EXPECT_EQ(
249 mClientManager->addClient(mClientCallback3, kClientName, kClientPackage, &mClient3),
250 OK);
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700251 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 Zhang0579c6f2020-10-05 12:03:34 -0700276 status_t err = mClientManager->addClient(nullptr, kClientName, kClientPackage, &client);
Chong Zhang15c192a2020-05-05 16:24:00 -0700277 EXPECT_EQ(err, IMediaTranscodingService::ERROR_ILLEGAL_ARGUMENT);
hkuang26587cb2020-01-16 10:36:08 -0800278}
Chong Zhang3f23e982020-09-24 14:03:41 -0700279//
280//TEST_F(TranscodingClientManagerTest, TestAddingWithInvalidClientPid) {
281// // Add a client with invalid Pid and expect failure.
282// std::shared_ptr<ITranscodingClient> client;
283// status_t err = mClientManager->addClient(mClientCallback1,
284// kClientName, kClientPackage, &client);
285// EXPECT_EQ(err, IMediaTranscodingService::ERROR_ILLEGAL_ARGUMENT);
286//}
hkuang26587cb2020-01-16 10:36:08 -0800287
Chong Zhang8e062632020-03-31 10:56:37 -0700288TEST_F(TranscodingClientManagerTest, TestAddingWithInvalidClientName) {
289 // Add a client with invalid name and expect failure.
290 std::shared_ptr<ITranscodingClient> client;
Chong Zhang0579c6f2020-10-05 12:03:34 -0700291 status_t err = mClientManager->addClient(mClientCallback1, kInvalidClientName, kClientPackage,
292 &client);
Chong Zhang15c192a2020-05-05 16:24:00 -0700293 EXPECT_EQ(err, IMediaTranscodingService::ERROR_ILLEGAL_ARGUMENT);
hkuang26587cb2020-01-16 10:36:08 -0800294}
295
296TEST_F(TranscodingClientManagerTest, TestAddingWithInvalidClientPackageName) {
Chong Zhang8e062632020-03-31 10:56:37 -0700297 // Add a client with invalid packagename and expect failure.
298 std::shared_ptr<ITranscodingClient> client;
Chong Zhang0579c6f2020-10-05 12:03:34 -0700299 status_t err = mClientManager->addClient(mClientCallback1, kClientName, kInvalidClientPackage,
300 &client);
Chong Zhang15c192a2020-05-05 16:24:00 -0700301 EXPECT_EQ(err, IMediaTranscodingService::ERROR_ILLEGAL_ARGUMENT);
hkuang26587cb2020-01-16 10:36:08 -0800302}
303
304TEST_F(TranscodingClientManagerTest, TestAddingValidClient) {
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700305 // Add a valid client, should succeed.
Chong Zhang8e062632020-03-31 10:56:37 -0700306 std::shared_ptr<ITranscodingClient> client;
Chong Zhang0579c6f2020-10-05 12:03:34 -0700307 status_t err =
308 mClientManager->addClient(mClientCallback1, kClientName, kClientPackage, &client);
Chong Zhang8e062632020-03-31 10:56:37 -0700309 EXPECT_EQ(err, OK);
310 EXPECT_NE(client.get(), nullptr);
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700311 EXPECT_EQ(mClientManager->getNumOfClients(), 1);
hkuang26587cb2020-01-16 10:36:08 -0800312
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700313 // Unregister client, should succeed.
Chong Zhang8e062632020-03-31 10:56:37 -0700314 Status status = client->unregister();
315 EXPECT_TRUE(status.isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700316 EXPECT_EQ(mClientManager->getNumOfClients(), 0);
hkuang26587cb2020-01-16 10:36:08 -0800317}
318
319TEST_F(TranscodingClientManagerTest, TestAddingDupliacteClient) {
Chong Zhang8e062632020-03-31 10:56:37 -0700320 std::shared_ptr<ITranscodingClient> client;
Chong Zhang0579c6f2020-10-05 12:03:34 -0700321 status_t err =
322 mClientManager->addClient(mClientCallback1, kClientName, kClientPackage, &client);
Chong Zhang8e062632020-03-31 10:56:37 -0700323 EXPECT_EQ(err, OK);
324 EXPECT_NE(client.get(), nullptr);
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700325 EXPECT_EQ(mClientManager->getNumOfClients(), 1);
hkuang26587cb2020-01-16 10:36:08 -0800326
Chong Zhang8e062632020-03-31 10:56:37 -0700327 std::shared_ptr<ITranscodingClient> dupClient;
Chong Zhang0579c6f2020-10-05 12:03:34 -0700328 err = mClientManager->addClient(mClientCallback1, "dupClient", "dupPackage", &dupClient);
Chong Zhang15c192a2020-05-05 16:24:00 -0700329 EXPECT_EQ(err, IMediaTranscodingService::ERROR_ALREADY_EXISTS);
Chong Zhang8e062632020-03-31 10:56:37 -0700330 EXPECT_EQ(dupClient.get(), nullptr);
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700331 EXPECT_EQ(mClientManager->getNumOfClients(), 1);
hkuang26587cb2020-01-16 10:36:08 -0800332
Chong Zhang8e062632020-03-31 10:56:37 -0700333 Status status = client->unregister();
334 EXPECT_TRUE(status.isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700335 EXPECT_EQ(mClientManager->getNumOfClients(), 0);
hkuang26587cb2020-01-16 10:36:08 -0800336
Chong Zhang3f23e982020-09-24 14:03:41 -0700337 err = mClientManager->addClient(mClientCallback1, "dupClient", "dupPackage", &dupClient);
Chong Zhang8e062632020-03-31 10:56:37 -0700338 EXPECT_EQ(err, OK);
339 EXPECT_NE(dupClient.get(), nullptr);
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700340 EXPECT_EQ(mClientManager->getNumOfClients(), 1);
hkuang26587cb2020-01-16 10:36:08 -0800341
Chong Zhang8e062632020-03-31 10:56:37 -0700342 status = dupClient->unregister();
343 EXPECT_TRUE(status.isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700344 EXPECT_EQ(mClientManager->getNumOfClients(), 0);
hkuang26587cb2020-01-16 10:36:08 -0800345}
346
347TEST_F(TranscodingClientManagerTest, TestAddingMultipleClient) {
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700348 addMultipleClients();
349 unregisterMultipleClients();
350}
hkuang26587cb2020-01-16 10:36:08 -0800351
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700352TEST_F(TranscodingClientManagerTest, TestSubmitCancelGetJobs) {
353 addMultipleClients();
hkuang26587cb2020-01-16 10:36:08 -0800354
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700355 // Test jobId assignment.
356 TranscodingRequestParcel request;
hkuang72d105f2020-05-21 10:48:55 -0700357 request.sourceFilePath = "test_source_file_0";
358 request.destinationFilePath = "test_desintaion_file_0";
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700359 TranscodingJobParcel job;
360 bool result;
361 EXPECT_TRUE(mClient1->submitRequest(request, &job, &result).isOk());
362 EXPECT_TRUE(result);
Chong Zhang15c192a2020-05-05 16:24:00 -0700363 EXPECT_EQ(job.jobId, JOB(0));
hkuang26587cb2020-01-16 10:36:08 -0800364
hkuang72d105f2020-05-21 10:48:55 -0700365 request.sourceFilePath = "test_source_file_1";
366 request.destinationFilePath = "test_desintaion_file_1";
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700367 EXPECT_TRUE(mClient1->submitRequest(request, &job, &result).isOk());
368 EXPECT_TRUE(result);
Chong Zhang15c192a2020-05-05 16:24:00 -0700369 EXPECT_EQ(job.jobId, JOB(1));
hkuang26587cb2020-01-16 10:36:08 -0800370
hkuang72d105f2020-05-21 10:48:55 -0700371 request.sourceFilePath = "test_source_file_2";
372 request.destinationFilePath = "test_desintaion_file_2";
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700373 EXPECT_TRUE(mClient1->submitRequest(request, &job, &result).isOk());
374 EXPECT_TRUE(result);
Chong Zhang15c192a2020-05-05 16:24:00 -0700375 EXPECT_EQ(job.jobId, JOB(2));
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700376
hkuang72d105f2020-05-21 10:48:55 -0700377 // Test submit bad request (no valid sourceFilePath) fails.
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700378 TranscodingRequestParcel badRequest;
hkuang72d105f2020-05-21 10:48:55 -0700379 badRequest.sourceFilePath = "bad_source_file";
380 badRequest.destinationFilePath = "bad_destination_file";
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700381 EXPECT_TRUE(mClient1->submitRequest(badRequest, &job, &result).isOk());
382 EXPECT_FALSE(result);
383
Chong Zhang3f23e982020-09-24 14:03:41 -0700384 // Test submit with bad pid/uid.
385 badRequest.sourceFilePath = "test_source_file_3";
386 badRequest.destinationFilePath = "test_desintaion_file_3";
387 badRequest.clientPid = kInvalidClientPid;
388 badRequest.clientUid = kInvalidClientUid;
389 EXPECT_TRUE(mClient1->submitRequest(badRequest, &job, &result).isOk());
390 EXPECT_FALSE(result);
391
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700392 // Test get jobs by id.
Chong Zhang15c192a2020-05-05 16:24:00 -0700393 EXPECT_TRUE(mClient1->getJobWithId(JOB(2), &job, &result).isOk());
394 EXPECT_EQ(job.jobId, JOB(2));
hkuang72d105f2020-05-21 10:48:55 -0700395 EXPECT_EQ(job.request.sourceFilePath, "test_source_file_2");
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700396 EXPECT_TRUE(result);
397
398 // Test get jobs by invalid id fails.
Chong Zhang15c192a2020-05-05 16:24:00 -0700399 EXPECT_TRUE(mClient1->getJobWithId(JOB(100), &job, &result).isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700400 EXPECT_FALSE(result);
401
402 // Test cancel non-existent job fail.
Chong Zhang15c192a2020-05-05 16:24:00 -0700403 EXPECT_TRUE(mClient2->cancelJob(JOB(100), &result).isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700404 EXPECT_FALSE(result);
405
406 // Test cancel valid jobId in arbitrary order.
Chong Zhang15c192a2020-05-05 16:24:00 -0700407 EXPECT_TRUE(mClient1->cancelJob(JOB(2), &result).isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700408 EXPECT_TRUE(result);
409
Chong Zhang15c192a2020-05-05 16:24:00 -0700410 EXPECT_TRUE(mClient1->cancelJob(JOB(0), &result).isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700411 EXPECT_TRUE(result);
412
Chong Zhang15c192a2020-05-05 16:24:00 -0700413 EXPECT_TRUE(mClient1->cancelJob(JOB(1), &result).isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700414 EXPECT_TRUE(result);
415
416 // Test cancel job again fails.
Chong Zhang15c192a2020-05-05 16:24:00 -0700417 EXPECT_TRUE(mClient1->cancelJob(JOB(1), &result).isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700418 EXPECT_FALSE(result);
419
420 // Test get job after cancel fails.
Chong Zhang15c192a2020-05-05 16:24:00 -0700421 EXPECT_TRUE(mClient1->getJobWithId(JOB(2), &job, &result).isOk());
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700422 EXPECT_FALSE(result);
423
424 // Test jobId independence for each client.
425 EXPECT_TRUE(mClient2->submitRequest(request, &job, &result).isOk());
426 EXPECT_TRUE(result);
Chong Zhang15c192a2020-05-05 16:24:00 -0700427 EXPECT_EQ(job.jobId, JOB(0));
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700428
429 EXPECT_TRUE(mClient2->submitRequest(request, &job, &result).isOk());
430 EXPECT_TRUE(result);
Chong Zhang15c192a2020-05-05 16:24:00 -0700431 EXPECT_EQ(job.jobId, JOB(1));
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700432
433 unregisterMultipleClients();
434}
435
436TEST_F(TranscodingClientManagerTest, TestClientCallback) {
437 addMultipleClients();
438
439 TranscodingRequestParcel request;
hkuang72d105f2020-05-21 10:48:55 -0700440 request.sourceFilePath = "test_source_file_name";
441 request.destinationFilePath = "test_destination_file_name";
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700442 TranscodingJobParcel job;
443 bool result;
444 EXPECT_TRUE(mClient1->submitRequest(request, &job, &result).isOk());
445 EXPECT_TRUE(result);
Chong Zhang15c192a2020-05-05 16:24:00 -0700446 EXPECT_EQ(job.jobId, JOB(0));
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700447
448 mScheduler->finishLastJob();
449 EXPECT_EQ(mClientCallback1->popEvent(), TestClientCallback::Finished(job.jobId));
450
451 EXPECT_TRUE(mClient1->submitRequest(request, &job, &result).isOk());
452 EXPECT_TRUE(result);
Chong Zhang15c192a2020-05-05 16:24:00 -0700453 EXPECT_EQ(job.jobId, JOB(1));
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700454
455 mScheduler->abortLastJob();
456 EXPECT_EQ(mClientCallback1->popEvent(), TestClientCallback::Failed(job.jobId));
457
458 EXPECT_TRUE(mClient1->submitRequest(request, &job, &result).isOk());
459 EXPECT_TRUE(result);
Chong Zhang15c192a2020-05-05 16:24:00 -0700460 EXPECT_EQ(job.jobId, JOB(2));
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700461
462 EXPECT_TRUE(mClient2->submitRequest(request, &job, &result).isOk());
463 EXPECT_TRUE(result);
Chong Zhang15c192a2020-05-05 16:24:00 -0700464 EXPECT_EQ(job.jobId, JOB(0));
Chong Zhang6d58e4b2020-03-31 09:41:10 -0700465
466 mScheduler->finishLastJob();
467 EXPECT_EQ(mClientCallback2->popEvent(), TestClientCallback::Finished(job.jobId));
468
469 unregisterMultipleClients();
hkuang26587cb2020-01-16 10:36:08 -0800470}
471
Chong Zhang15c192a2020-05-05 16:24:00 -0700472TEST_F(TranscodingClientManagerTest, TestUseAfterUnregister) {
473 // Add a client.
474 std::shared_ptr<ITranscodingClient> client;
Chong Zhang0579c6f2020-10-05 12:03:34 -0700475 status_t err =
476 mClientManager->addClient(mClientCallback1, kClientName, kClientPackage, &client);
Chong Zhang15c192a2020-05-05 16:24:00 -0700477 EXPECT_EQ(err, OK);
478 EXPECT_NE(client.get(), nullptr);
479
480 // Submit 2 requests, 1 offline and 1 realtime.
481 TranscodingRequestParcel request;
482 TranscodingJobParcel job;
483 bool result;
484
hkuang72d105f2020-05-21 10:48:55 -0700485 request.sourceFilePath = "test_source_file_0";
486 request.destinationFilePath = "test_destination_file_0";
Chong Zhang15c192a2020-05-05 16:24:00 -0700487 request.priority = TranscodingJobPriority::kUnspecified;
488 EXPECT_TRUE(client->submitRequest(request, &job, &result).isOk() && result);
489 EXPECT_EQ(job.jobId, JOB(0));
490
hkuang72d105f2020-05-21 10:48:55 -0700491 request.sourceFilePath = "test_source_file_1";
492 request.destinationFilePath = "test_destination_file_1";
Chong Zhang15c192a2020-05-05 16:24:00 -0700493 request.priority = TranscodingJobPriority::kNormal;
494 EXPECT_TRUE(client->submitRequest(request, &job, &result).isOk() && result);
495 EXPECT_EQ(job.jobId, JOB(1));
496
497 // Unregister client, should succeed.
498 Status status = client->unregister();
499 EXPECT_TRUE(status.isOk());
500
501 // Test submit new request after unregister, should fail with ERROR_DISCONNECTED.
hkuang72d105f2020-05-21 10:48:55 -0700502 request.sourceFilePath = "test_source_file_2";
503 request.destinationFilePath = "test_destination_file_2";
Chong Zhang15c192a2020-05-05 16:24:00 -0700504 request.priority = TranscodingJobPriority::kNormal;
505 status = client->submitRequest(request, &job, &result);
506 EXPECT_FALSE(status.isOk());
507 EXPECT_EQ(status.getServiceSpecificError(), IMediaTranscodingService::ERROR_DISCONNECTED);
508
509 // Test cancel jobs after unregister, should fail with ERROR_DISCONNECTED
510 // regardless of realtime or offline job, or whether the jobId is valid.
511 status = client->cancelJob(JOB(0), &result);
512 EXPECT_FALSE(status.isOk());
513 EXPECT_EQ(status.getServiceSpecificError(), IMediaTranscodingService::ERROR_DISCONNECTED);
514
515 status = client->cancelJob(JOB(1), &result);
516 EXPECT_FALSE(status.isOk());
517 EXPECT_EQ(status.getServiceSpecificError(), IMediaTranscodingService::ERROR_DISCONNECTED);
518
519 status = client->cancelJob(JOB(2), &result);
520 EXPECT_FALSE(status.isOk());
521 EXPECT_EQ(status.getServiceSpecificError(), IMediaTranscodingService::ERROR_DISCONNECTED);
522
523 // Test get jobs, should fail with ERROR_DISCONNECTED regardless of realtime
524 // or offline job, or whether the jobId is valid.
525 status = client->getJobWithId(JOB(0), &job, &result);
526 EXPECT_FALSE(status.isOk());
527 EXPECT_EQ(status.getServiceSpecificError(), IMediaTranscodingService::ERROR_DISCONNECTED);
528
529 status = client->getJobWithId(JOB(1), &job, &result);
530 EXPECT_FALSE(status.isOk());
531 EXPECT_EQ(status.getServiceSpecificError(), IMediaTranscodingService::ERROR_DISCONNECTED);
532
533 status = client->getJobWithId(JOB(2), &job, &result);
534 EXPECT_FALSE(status.isOk());
535 EXPECT_EQ(status.getServiceSpecificError(), IMediaTranscodingService::ERROR_DISCONNECTED);
536}
537
Chong Zhang8e062632020-03-31 10:56:37 -0700538} // namespace android