• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 MediaTranscodingService.
18 
19 //#define LOG_NDEBUG 0
20 #define LOG_TAG "MediaTranscodingServiceSimulatedTest"
21 
22 #include <aidl/android/media/BnTranscodingClientCallback.h>
23 #include <aidl/android/media/IMediaTranscodingService.h>
24 #include <aidl/android/media/ITranscodingClient.h>
25 #include <aidl/android/media/ITranscodingClientCallback.h>
26 #include <aidl/android/media/TranscodingRequestParcel.h>
27 #include <aidl/android/media/TranscodingSessionParcel.h>
28 #include <aidl/android/media/TranscodingSessionPriority.h>
29 #include <android-base/logging.h>
30 #include <android/binder_manager.h>
31 #include <android/binder_process.h>
32 #include <binder/PermissionController.h>
33 #include <cutils/multiuser.h>
34 #include <gtest/gtest.h>
35 #include <utils/Log.h>
36 
37 #include <iostream>
38 #include <list>
39 #include <unordered_set>
40 
41 #include "MediaTranscodingServiceTestHelper.h"
42 #include "SimulatedTranscoder.h"
43 
44 namespace android {
45 
46 namespace media {
47 
48 // Note that -1 is valid and means using calling pid/uid for the service. But only privilege caller
49 // could use them. This test is not a privilege caller.
50 constexpr int32_t kInvalidClientPid = -5;
51 constexpr int32_t kInvalidClientUid = -10;
52 constexpr const char* kInvalidClientName = "";
53 constexpr const char* kInvalidClientOpPackageName = "";
54 
55 constexpr int64_t kPaddingUs = 1000000;
56 constexpr int64_t kSessionWithPaddingUs = SimulatedTranscoder::kSessionDurationUs + kPaddingUs;
57 constexpr int64_t kWatchdogTimeoutUs = 3000000;
58 // Pacer settings used for simulated tests. Listed here for reference.
59 constexpr int32_t kSimulatedPacerBurstThresholdMs = 500;
60 //constexpr int32_t kSimulatedPacerBurstCountQuota = 10;
61 //constexpr int32_t kSimulatedPacerBurstTimeQuotaSec = 3;
62 
63 constexpr const char* kClientOpPackageName = "TestClientPackage";
64 
65 class MediaTranscodingServiceSimulatedTest : public MediaTranscodingServiceTestBase {
66 public:
MediaTranscodingServiceSimulatedTest()67     MediaTranscodingServiceSimulatedTest() { ALOGI("MediaTranscodingServiceResourceTest created"); }
68 
~MediaTranscodingServiceSimulatedTest()69     virtual ~MediaTranscodingServiceSimulatedTest() {
70         ALOGI("MediaTranscodingServiceResourceTest destroyed");
71     }
72 
testPacerHelper(int numSubmits,int sessionDurationMs,int expectedSuccess)73     void testPacerHelper(int numSubmits, int sessionDurationMs, int expectedSuccess) {
74         // Idle to clear out burst history.
75         usleep(kSimulatedPacerBurstThresholdMs * 2 * 1000);
76         for (int i = 0; i < numSubmits; i++) {
77             EXPECT_TRUE(mClient3->submit(i, "test_source_file_0", "test_destination_file_0",
78                                          TranscodingSessionPriority::kNormal, -1 /*bitrateBps*/,
79                                          -1 /*overridePid*/, -1 /*overrideUid*/,
80                                          sessionDurationMs));
81         }
82         for (int i = 0; i < expectedSuccess; i++) {
83             EXPECT_EQ(mClient3->pop(kPaddingUs), EventTracker::Start(CLIENT(3), i));
84             EXPECT_EQ(mClient3->pop(kSessionWithPaddingUs), EventTracker::Finished(CLIENT(3), i));
85         }
86         for (int i = expectedSuccess; i < numSubmits; i++) {
87             EXPECT_EQ(mClient3->pop(kPaddingUs), EventTracker::Failed(CLIENT(3), i));
88             EXPECT_EQ(mClient3->getLastError(), TranscodingErrorCode::kDroppedByService);
89         }
90     }
91 };
92 
TEST_F(MediaTranscodingServiceSimulatedTest,TestRegisterNullClient)93 TEST_F(MediaTranscodingServiceSimulatedTest, TestRegisterNullClient) {
94     std::shared_ptr<ITranscodingClient> client;
95 
96     // Register the client with null callback.
97     Status status = mService->registerClient(nullptr, kClientName, kClientOpPackageName, &client);
98     EXPECT_FALSE(status.isOk());
99 }
100 
TEST_F(MediaTranscodingServiceSimulatedTest,TestRegisterClientWithInvalidClientName)101 TEST_F(MediaTranscodingServiceSimulatedTest, TestRegisterClientWithInvalidClientName) {
102     std::shared_ptr<ITranscodingClient> client;
103 
104     // Register the client with the service.
105     Status status = mService->registerClient(mClient1, kInvalidClientName,
106                                              kInvalidClientOpPackageName, &client);
107     EXPECT_FALSE(status.isOk());
108 }
109 
TEST_F(MediaTranscodingServiceSimulatedTest,TestRegisterClientWithInvalidClientPackageName)110 TEST_F(MediaTranscodingServiceSimulatedTest, TestRegisterClientWithInvalidClientPackageName) {
111     std::shared_ptr<ITranscodingClient> client;
112 
113     // Register the client with the service.
114     Status status =
115             mService->registerClient(mClient1, kClientName, kInvalidClientOpPackageName, &client);
116     EXPECT_FALSE(status.isOk());
117 }
118 
TEST_F(MediaTranscodingServiceSimulatedTest,TestRegisterOneClient)119 TEST_F(MediaTranscodingServiceSimulatedTest, TestRegisterOneClient) {
120     std::shared_ptr<ITranscodingClient> client;
121 
122     Status status = mService->registerClient(mClient1, kClientName, kClientOpPackageName, &client);
123     EXPECT_TRUE(status.isOk());
124 
125     // Validate the client.
126     EXPECT_TRUE(client != nullptr);
127 
128     // Check the number of Clients.
129     int32_t numOfClients;
130     status = mService->getNumOfClients(&numOfClients);
131     EXPECT_TRUE(status.isOk());
132     EXPECT_GE(numOfClients, 1);
133 
134     // Unregister the client.
135     status = client->unregister();
136     EXPECT_TRUE(status.isOk());
137 }
138 
TEST_F(MediaTranscodingServiceSimulatedTest,TestRegisterClientTwice)139 TEST_F(MediaTranscodingServiceSimulatedTest, TestRegisterClientTwice) {
140     std::shared_ptr<ITranscodingClient> client;
141 
142     Status status = mService->registerClient(mClient1, kClientName, kClientOpPackageName, &client);
143     EXPECT_TRUE(status.isOk());
144 
145     // Validate the client.
146     EXPECT_TRUE(client != nullptr);
147 
148     // Register the client again and expects failure.
149     std::shared_ptr<ITranscodingClient> client1;
150     status = mService->registerClient(mClient1, kClientName, kClientOpPackageName, &client1);
151     EXPECT_FALSE(status.isOk());
152 
153     // Unregister the client.
154     status = client->unregister();
155     EXPECT_TRUE(status.isOk());
156 }
157 
TEST_F(MediaTranscodingServiceSimulatedTest,TestRegisterMultipleClients)158 TEST_F(MediaTranscodingServiceSimulatedTest, TestRegisterMultipleClients) {
159     registerMultipleClients();
160     unregisterMultipleClients();
161 }
162 
TEST_F(MediaTranscodingServiceSimulatedTest,TestSessionIdIndependence)163 TEST_F(MediaTranscodingServiceSimulatedTest, TestSessionIdIndependence) {
164     registerMultipleClients();
165 
166     // Submit 2 requests on client1 first.
167     EXPECT_TRUE(mClient1->submit(0, "test_source_file", "test_destination_file"));
168     EXPECT_TRUE(mClient1->submit(1, "test_source_file", "test_destination_file"));
169 
170     // Submit 2 requests on client2, sessionId should be independent for each client.
171     EXPECT_TRUE(mClient2->submit(0, "test_source_file", "test_destination_file"));
172     EXPECT_TRUE(mClient2->submit(1, "test_source_file", "test_destination_file"));
173 
174     // Cancel all sessions.
175     EXPECT_TRUE(mClient1->cancel(0));
176     EXPECT_TRUE(mClient1->cancel(1));
177     EXPECT_TRUE(mClient2->cancel(0));
178     EXPECT_TRUE(mClient2->cancel(1));
179 
180     unregisterMultipleClients();
181 }
182 
TEST_F(MediaTranscodingServiceSimulatedTest,TestSubmitCancelSessions)183 TEST_F(MediaTranscodingServiceSimulatedTest, TestSubmitCancelSessions) {
184     registerMultipleClients();
185 
186     // Test sessionId assignment.
187     EXPECT_TRUE(mClient1->submit(0, "test_source_file_0", "test_destination_file"));
188     EXPECT_TRUE(mClient1->submit(1, "test_source_file_1", "test_destination_file"));
189     EXPECT_TRUE(mClient1->submit(2, "test_source_file_2", "test_destination_file"));
190 
191     // Test submit bad request (no valid sourceFilePath) fails.
192     EXPECT_TRUE(mClient1->submit<fail>(0, "", ""));
193 
194     // Test submit bad request (no valid sourceFilePath) fails.
195     EXPECT_TRUE(mClient1->submit<fail>(0, "src", "dst", TranscodingSessionPriority::kNormal,
196                                        1000000, kInvalidClientPid, kInvalidClientUid));
197 
198     // Test cancel non-existent session fails.
199     EXPECT_TRUE(mClient1->cancel<fail>(100));
200 
201     // Session 0 should start immediately and finish in 2 seconds, followed by Session 1 start.
202     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Start(CLIENT(1), 0));
203     EXPECT_EQ(mClient1->pop(kSessionWithPaddingUs), EventTracker::Finished(CLIENT(1), 0));
204     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Start(CLIENT(1), 1));
205 
206     // Test cancel valid sessionId in random order.
207     // Test cancel finished session fails.
208     EXPECT_TRUE(mClient1->cancel(2));
209     EXPECT_TRUE(mClient1->cancel<fail>(0));
210     EXPECT_TRUE(mClient1->cancel(1));
211 
212     // Test cancel session again fails.
213     EXPECT_TRUE(mClient1->cancel<fail>(1));
214 
215     // Test no more events arriving after cancel.
216     EXPECT_EQ(mClient1->pop(kSessionWithPaddingUs), EventTracker::NoEvent);
217 
218     unregisterMultipleClients();
219 }
220 
TEST_F(MediaTranscodingServiceSimulatedTest,TestGetSessions)221 TEST_F(MediaTranscodingServiceSimulatedTest, TestGetSessions) {
222     registerMultipleClients();
223 
224     // Submit 3 requests.
225     EXPECT_TRUE(mClient1->submit(0, "test_source_file_0", "test_destination_file_0"));
226     EXPECT_TRUE(mClient1->submit(1, "test_source_file_1", "test_destination_file_1"));
227     EXPECT_TRUE(mClient1->submit(2, "test_source_file_2", "test_destination_file_2"));
228 
229     // Test get sessions by id.
230     EXPECT_TRUE(mClient1->getSession(2, "test_source_file_2", "test_destination_file_2"));
231     EXPECT_TRUE(mClient1->getSession(1, "test_source_file_1", "test_destination_file_1"));
232     EXPECT_TRUE(mClient1->getSession(0, "test_source_file_0", "test_destination_file_0"));
233 
234     // Test get session by invalid id fails.
235     EXPECT_TRUE(mClient1->getSession<fail>(100, "", ""));
236     EXPECT_TRUE(mClient1->getSession<fail>(-1, "", ""));
237 
238     // Test get session after cancel fails.
239     EXPECT_TRUE(mClient1->cancel(2));
240     EXPECT_TRUE(mClient1->getSession<fail>(2, "", ""));
241 
242     // Session 0 should start immediately and finish in 2 seconds, followed by Session 1 start.
243     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Start(CLIENT(1), 0));
244     EXPECT_EQ(mClient1->pop(kSessionWithPaddingUs), EventTracker::Finished(CLIENT(1), 0));
245     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Start(CLIENT(1), 1));
246 
247     // Test get session after finish fails.
248     EXPECT_TRUE(mClient1->getSession<fail>(0, "", ""));
249 
250     // Test get the remaining session 1.
251     EXPECT_TRUE(mClient1->getSession(1, "test_source_file_1", "test_destination_file_1"));
252 
253     // Cancel remaining session 1.
254     EXPECT_TRUE(mClient1->cancel(1));
255 
256     unregisterMultipleClients();
257 }
258 
TEST_F(MediaTranscodingServiceSimulatedTest,TestAddGetClientUids)259 TEST_F(MediaTranscodingServiceSimulatedTest, TestAddGetClientUids) {
260     registerMultipleClients();
261 
262     std::vector<int32_t> clientUids;
263     TranscodingRequestParcel request;
264     TranscodingSessionParcel session;
265     uid_t ownUid = ::getuid();
266 
267     // Submit one real-time session.
268     EXPECT_TRUE(mClient1->submit(0, "test_source_file_0", "test_destination_file"));
269 
270     // Should have mClientUid in client uid list.
271     EXPECT_TRUE(mClient1->getClientUids(0, &clientUids));
272     EXPECT_EQ(clientUids.size(), 1u);
273     EXPECT_EQ(clientUids[0], (int32_t)mClient1->mClientUid);
274 
275     // Adding invalid client uid should fail.
276     EXPECT_TRUE(mClient1->addClientUid<fail>(0, kInvalidClientUid));
277 
278     // Adding mClientUid again should fail.
279     EXPECT_TRUE(mClient1->addClientUid<fail>(0, mClient1->mClientUid));
280 
281     // Submit one offline session.
282     EXPECT_TRUE(mClient1->submit(1, "test_source_file_1", "test_destination_file_1",
283                                  TranscodingSessionPriority::kUnspecified));
284 
285     // Should not have any uids in client uid list.
286     EXPECT_TRUE(mClient1->getClientUids(1, &clientUids));
287     EXPECT_EQ(clientUids.size(), 0u);
288 
289     // Add own uid (with IMediaTranscodingService::USE_CALLING_UID), should succeed.
290     EXPECT_TRUE(mClient1->addClientUid(1, IMediaTranscodingService::USE_CALLING_UID));
291     EXPECT_TRUE(mClient1->getClientUids(1, &clientUids));
292     EXPECT_EQ(clientUids.size(), 1u);
293     EXPECT_EQ(clientUids[0], (int32_t)ownUid);
294 
295     // Adding mClientUid should succeed.
296     EXPECT_TRUE(mClient1->addClientUid(1, mClient1->mClientUid));
297     EXPECT_TRUE(mClient1->getClientUids(1, &clientUids));
298     std::unordered_set<uid_t> uidSet;
299     uidSet.insert(clientUids.begin(), clientUids.end());
300     EXPECT_EQ(uidSet.size(), 2u);
301     EXPECT_EQ(uidSet.count(ownUid), 1u);
302     EXPECT_EQ(uidSet.count(mClient1->mClientUid), 1u);
303 
304     unregisterMultipleClients();
305 }
306 
TEST_F(MediaTranscodingServiceSimulatedTest,TestSubmitCancelWithOfflineSessions)307 TEST_F(MediaTranscodingServiceSimulatedTest, TestSubmitCancelWithOfflineSessions) {
308     registerMultipleClients();
309 
310     // Submit some offline sessions first.
311     EXPECT_TRUE(mClient1->submit(0, "test_source_file_0", "test_destination_file_0",
312                                  TranscodingSessionPriority::kUnspecified));
313     EXPECT_TRUE(mClient1->submit(1, "test_source_file_1", "test_destination_file_1",
314                                  TranscodingSessionPriority::kUnspecified));
315 
316     // Session 0 should start immediately.
317     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Start(CLIENT(1), 0));
318 
319     // Submit more real-time sessions.
320     EXPECT_TRUE(mClient1->submit(2, "test_source_file_2", "test_destination_file_2"));
321     EXPECT_TRUE(mClient1->submit(3, "test_source_file_3", "test_destination_file_3"));
322 
323     // Session 0 should pause immediately and session 2 should start.
324     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Pause(CLIENT(1), 0));
325     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Start(CLIENT(1), 2));
326 
327     // Session 2 should finish in 2 seconds and session 3 should start.
328     EXPECT_EQ(mClient1->pop(kSessionWithPaddingUs), EventTracker::Finished(CLIENT(1), 2));
329     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Start(CLIENT(1), 3));
330 
331     // Cancel session 3 now
332     EXPECT_TRUE(mClient1->cancel(3));
333 
334     // Session 0 should resume and finish in 2 seconds, followed by session 1 start.
335     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Resume(CLIENT(1), 0));
336     EXPECT_EQ(mClient1->pop(kSessionWithPaddingUs), EventTracker::Finished(CLIENT(1), 0));
337     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Start(CLIENT(1), 1));
338 
339     // Cancel remaining session 1.
340     EXPECT_TRUE(mClient1->cancel(1));
341 
342     unregisterMultipleClients();
343 }
344 
TEST_F(MediaTranscodingServiceSimulatedTest,TestClientUseAfterUnregister)345 TEST_F(MediaTranscodingServiceSimulatedTest, TestClientUseAfterUnregister) {
346     std::shared_ptr<ITranscodingClient> client;
347 
348     // Register a client, then unregister.
349     Status status = mService->registerClient(mClient1, kClientName, kClientOpPackageName, &client);
350     EXPECT_TRUE(status.isOk());
351 
352     status = client->unregister();
353     EXPECT_TRUE(status.isOk());
354 
355     // Test various operations on the client, should fail with ERROR_DISCONNECTED.
356     TranscodingSessionParcel session;
357     bool result;
358     status = client->getSessionWithId(0, &session, &result);
359     EXPECT_EQ(status.getServiceSpecificError(), IMediaTranscodingService::ERROR_DISCONNECTED);
360 
361     status = client->cancelSession(0, &result);
362     EXPECT_EQ(status.getServiceSpecificError(), IMediaTranscodingService::ERROR_DISCONNECTED);
363 
364     TranscodingRequestParcel request;
365     status = client->submitRequest(request, &session, &result);
366     EXPECT_EQ(status.getServiceSpecificError(), IMediaTranscodingService::ERROR_DISCONNECTED);
367 }
368 
TEST_F(MediaTranscodingServiceSimulatedTest,TestTranscodingUidPolicy)369 TEST_F(MediaTranscodingServiceSimulatedTest, TestTranscodingUidPolicy) {
370     ALOGD("TestTranscodingUidPolicy starting...");
371 
372     EXPECT_TRUE(ShellHelper::RunCmd("input keyevent KEYCODE_WAKEUP"));
373     EXPECT_TRUE(ShellHelper::RunCmd("wm dismiss-keyguard"));
374     EXPECT_TRUE(ShellHelper::Stop(kClientPackageA));
375     EXPECT_TRUE(ShellHelper::Stop(kClientPackageB));
376     EXPECT_TRUE(ShellHelper::Stop(kClientPackageC));
377 
378     registerMultipleClients();
379 
380     ALOGD("Moving app A to top...");
381     EXPECT_TRUE(ShellHelper::Start(kClientPackageA, kTestActivityName));
382 
383     // Submit 3 requests.
384     ALOGD("Submitting session to client1 (app A) ...");
385     EXPECT_TRUE(mClient1->submit(0, "test_source_file_0", "test_destination_file_0"));
386     EXPECT_TRUE(mClient1->submit(1, "test_source_file_1", "test_destination_file_1"));
387     EXPECT_TRUE(mClient1->submit(2, "test_source_file_2", "test_destination_file_2"));
388 
389     // Session 0 should start immediately.
390     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Start(CLIENT(1), 0));
391 
392     ALOGD("Moving app B to top...");
393     EXPECT_TRUE(ShellHelper::Start(kClientPackageB, kTestActivityName));
394 
395     // Session 0 should continue and finish in 2 seconds, then session 1 should start.
396     EXPECT_EQ(mClient1->pop(kSessionWithPaddingUs), EventTracker::Finished(CLIENT(1), 0));
397     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Start(CLIENT(1), 1));
398 
399     ALOGD("Submitting session to client2 (app B) ...");
400     EXPECT_TRUE(mClient2->submit(0, "test_source_file_0", "test_destination_file_0"));
401 
402     // Client1's session should pause, client2's session should start.
403     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Pause(CLIENT(1), 1));
404     EXPECT_EQ(mClient2->pop(kPaddingUs), EventTracker::Start(CLIENT(2), 0));
405 
406     ALOGD("Moving app A back to top...");
407     EXPECT_TRUE(ShellHelper::Start(kClientPackageA, kTestActivityName));
408 
409     // Client2's session should pause, client1's session 1 should resume.
410     EXPECT_EQ(mClient2->pop(kPaddingUs), EventTracker::Pause(CLIENT(2), 0));
411     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Resume(CLIENT(1), 1));
412 
413     // Client2's session 1 should finish in 2 seconds, then its session 2 should start.
414     EXPECT_EQ(mClient1->pop(kSessionWithPaddingUs), EventTracker::Finished(CLIENT(1), 1));
415     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Start(CLIENT(1), 2));
416 
417     // After client2's sessions finish, client1's session should resume.
418     EXPECT_EQ(mClient1->pop(kSessionWithPaddingUs), EventTracker::Finished(CLIENT(1), 2));
419     EXPECT_EQ(mClient2->pop(kPaddingUs), EventTracker::Resume(CLIENT(2), 0));
420 
421     unregisterMultipleClients();
422 
423     EXPECT_TRUE(ShellHelper::Stop(kClientPackageA));
424     EXPECT_TRUE(ShellHelper::Stop(kClientPackageB));
425     EXPECT_TRUE(ShellHelper::Stop(kClientPackageC));
426 
427     ALOGD("TestTranscodingUidPolicy finished.");
428 }
429 
TEST_F(MediaTranscodingServiceSimulatedTest,TestTranscodingUidPolicyWithMultipleClientUids)430 TEST_F(MediaTranscodingServiceSimulatedTest, TestTranscodingUidPolicyWithMultipleClientUids) {
431     ALOGD("TestTranscodingUidPolicyWithMultipleClientUids starting...");
432 
433     EXPECT_TRUE(ShellHelper::RunCmd("input keyevent KEYCODE_WAKEUP"));
434     EXPECT_TRUE(ShellHelper::RunCmd("wm dismiss-keyguard"));
435     EXPECT_TRUE(ShellHelper::Stop(kClientPackageA));
436     EXPECT_TRUE(ShellHelper::Stop(kClientPackageB));
437     EXPECT_TRUE(ShellHelper::Stop(kClientPackageC));
438 
439     registerMultipleClients();
440 
441     ALOGD("Moving app A to top...");
442     EXPECT_TRUE(ShellHelper::Start(kClientPackageA, kTestActivityName));
443 
444     // Submit 3 requests.
445     ALOGD("Submitting session to client1 (app A)...");
446     EXPECT_TRUE(mClient1->submit(0, "test_source_file_0", "test_destination_file_0"));
447     EXPECT_TRUE(mClient1->submit(1, "test_source_file_1", "test_destination_file_1"));
448     EXPECT_TRUE(mClient1->submit(2, "test_source_file_2", "test_destination_file_2"));
449 
450     // mClient1's Session 0 should start immediately.
451     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Start(CLIENT(1), 0));
452 
453     // Add client2 (app B)'s uid to mClient1's session 1.
454     EXPECT_TRUE(mClient1->addClientUid(1, mClient2->mClientUid));
455 
456     ALOGD("Moving app B to top...");
457     EXPECT_TRUE(ShellHelper::Start(kClientPackageB, kTestActivityName));
458 
459     // mClient1's session 0 should pause, session 1 should start.
460     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Pause(CLIENT(1), 0));
461     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Start(CLIENT(1), 1));
462 
463     ALOGD("Moving app A back to top...");
464     EXPECT_TRUE(ShellHelper::Start(kClientPackageA, kTestActivityName));
465     EXPECT_EQ(mClient1->pop(kSessionWithPaddingUs), EventTracker::Finished(CLIENT(1), 1));
466     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Resume(CLIENT(1), 0));
467 
468     unregisterMultipleClients();
469 
470     EXPECT_TRUE(ShellHelper::Stop(kClientPackageA));
471     EXPECT_TRUE(ShellHelper::Stop(kClientPackageB));
472     EXPECT_TRUE(ShellHelper::Stop(kClientPackageC));
473 
474     ALOGD("TestTranscodingUidPolicyWithMultipleClientUids finished.");
475 }
476 
TEST_F(MediaTranscodingServiceSimulatedTest,TestTranscodingThermalPolicy)477 TEST_F(MediaTranscodingServiceSimulatedTest, TestTranscodingThermalPolicy) {
478     ALOGD("TestTranscodingThermalPolicy starting...");
479 
480     registerMultipleClients();
481 
482     // Submit request, should start immediately.
483     EXPECT_TRUE(mClient1->submit(0, "test_source_file_0", "test_destination_file_0"));
484     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Start(CLIENT(1), 0));
485 
486     // Now, simulate thermal status change by adb cmd. The status code is as defined in
487     // frameworks/native/include/android/thermal.h.
488     // ATHERMAL_STATUS_SEVERE(3): should start throttling.
489     EXPECT_TRUE(ShellHelper::RunCmd("cmd thermalservice override-status 3"));
490     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Pause(CLIENT(1), 0));
491 
492     // ATHERMAL_STATUS_CRITICAL(4): shouldn't start throttling again (already started).
493     EXPECT_TRUE(ShellHelper::RunCmd("cmd thermalservice override-status 4"));
494     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::NoEvent);
495 
496     // ATHERMAL_STATUS_MODERATE(2): should stop throttling.
497     EXPECT_TRUE(ShellHelper::RunCmd("cmd thermalservice override-status 2"));
498     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Resume(CLIENT(1), 0));
499 
500     // ATHERMAL_STATUS_LIGHT(1): shouldn't stop throttling again (already stopped).
501     EXPECT_TRUE(ShellHelper::RunCmd("cmd thermalservice override-status 1"));
502     EXPECT_EQ(mClient1->pop(kSessionWithPaddingUs), EventTracker::Finished(CLIENT(1), 0));
503 
504     unregisterMultipleClients();
505 
506     ALOGD("TestTranscodingThermalPolicy finished.");
507 }
508 
TEST_F(MediaTranscodingServiceSimulatedTest,TestTranscodingWatchdog)509 TEST_F(MediaTranscodingServiceSimulatedTest, TestTranscodingWatchdog) {
510     ALOGD("TestTranscodingWatchdog starting...");
511 
512     registerMultipleClients();
513 
514     // SimulatedTranscoder itself does not send heartbeat. Its sessions last 1sec
515     // by default, so timeout will not happen normally.
516     // Here we run a session of 4000ms with TranscodingTestConfig. This will trigger
517     // a watchdog timeout on server side. We use it to check that error code is correct.
518     EXPECT_TRUE(mClient1->submit(
519             0, "test_source_file_0", "test_destination_file_0", TranscodingSessionPriority::kNormal,
520             -1 /*bitrateBps*/, -1 /*overridePid*/, -1 /*overrideUid*/, 4000 /*sessionDurationMs*/));
521     EXPECT_EQ(mClient1->pop(100000), EventTracker::Start(CLIENT(1), 0));
522     EXPECT_EQ(mClient1->pop(kWatchdogTimeoutUs - 100000), EventTracker::NoEvent);
523     EXPECT_EQ(mClient1->pop(200000), EventTracker::Failed(CLIENT(1), 0));
524     EXPECT_EQ(mClient1->getLastError(), TranscodingErrorCode::kWatchdogTimeout);
525 
526     // After the timeout, submit another request and check it's finished.
527     EXPECT_TRUE(mClient1->submit(1, "test_source_file_1", "test_destination_file_1"));
528     EXPECT_EQ(mClient1->pop(kPaddingUs), EventTracker::Start(CLIENT(1), 1));
529     EXPECT_EQ(mClient1->pop(kSessionWithPaddingUs), EventTracker::Finished(CLIENT(1), 1));
530 
531     unregisterMultipleClients();
532 
533     ALOGD("TestTranscodingWatchdog finished.");
534 }
535 
TEST_F(MediaTranscodingServiceSimulatedTest,TestTranscodingPacerOverCountQuotaOnly)536 TEST_F(MediaTranscodingServiceSimulatedTest, TestTranscodingPacerOverCountQuotaOnly) {
537     ALOGD("TestTranscodingPacerOverCountQuotaOnly starting...");
538 
539     registerMultipleClients();
540     testPacerHelper(12 /*numSubmits*/, 100 /*sessionDurationMs*/, 12 /*expectedSuccess*/);
541     unregisterMultipleClients();
542 
543     ALOGD("TestTranscodingPacerOverCountQuotaOnly finished.");
544 }
545 
TEST_F(MediaTranscodingServiceSimulatedTest,TestTranscodingPacerOverTimeQuotaOnly)546 TEST_F(MediaTranscodingServiceSimulatedTest, TestTranscodingPacerOverTimeQuotaOnly) {
547     ALOGD("TestTranscodingPacerOverTimeQuotaOnly starting...");
548 
549     registerMultipleClients();
550     testPacerHelper(5 /*numSubmits*/, 1000 /*sessionDurationMs*/, 5 /*expectedSuccess*/);
551     unregisterMultipleClients();
552 
553     ALOGD("TestTranscodingPacerOverTimeQuotaOnly finished.");
554 }
555 
TEST_F(MediaTranscodingServiceSimulatedTest,TestTranscodingPacerOverQuota)556 TEST_F(MediaTranscodingServiceSimulatedTest, TestTranscodingPacerOverQuota) {
557     ALOGD("TestTranscodingPacerOverQuota starting...");
558 
559     registerMultipleClients();
560     testPacerHelper(12 /*numSubmits*/, 400 /*sessionDurationMs*/, 10 /*expectedSuccess*/);
561     unregisterMultipleClients();
562 
563     // Idle to clear out burst history. Since we expect it to actually fail, wait for cooldown.
564     ALOGD("TestTranscodingPacerOverQuota finished.");
565 }
566 
567 }  // namespace media
568 }  // namespace android
569