1 /*
2  * Copyright 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 package android.media.cts;
18 
19 import static org.junit.Assert.assertTrue;
20 
21 import static java.util.concurrent.TimeUnit.MILLISECONDS;
22 
23 import android.annotation.Nullable;
24 import android.media.session.MediaController;
25 import android.media.session.MediaSession;
26 import android.os.Bundle;
27 import android.os.Handler;
28 import android.os.Looper;
29 
30 import java.util.List;
31 import java.util.concurrent.CountDownLatch;
32 import java.util.concurrent.atomic.AtomicInteger;
33 
34 public class MediaSessionTestService extends RemoteService {
35     public static final int TEST_SERIES_OF_SET_QUEUE = 0;
36     public static final int TEST_SET_QUEUE = 1;
37 
38     public static final int STEP_SET_UP = 0;
39     public static final int STEP_CHECK = 1;
40     public static final int STEP_CLEAN_UP = 2;
41 
42     public static final String KEY_SESSION_TOKEN = "sessionToken";
43     public static final String KEY_EXPECTED_TOTAL_NUMBER_OF_ITEMS = "expectedTotalNumberOfItems";
44     public static final String KEY_EXPECTED_QUEUE_SIZE = "expectedQueueSize";
45 
46     private MediaController mMediaController;
47     private MediaController.Callback mMediaControllerCallback;
48     private CountDownLatch mAllItemsNotified;
49     private CountDownLatch mQueueNotified;
50 
testSeriesOfSetQueue_setUp(Bundle args)51     private void testSeriesOfSetQueue_setUp(Bundle args) {
52         MediaSession.Token token = args.getParcelable(KEY_SESSION_TOKEN);
53         int expectedTotalNumberOfItems = args.getInt(KEY_EXPECTED_TOTAL_NUMBER_OF_ITEMS);
54 
55         mAllItemsNotified = new CountDownLatch(1);
56         AtomicInteger numberOfItems = new AtomicInteger();
57         mMediaControllerCallback = new MediaController.Callback() {
58             @Override
59             public void onQueueChanged(List<MediaSession.QueueItem> queue) {
60                 if (queue != null) {
61                     if (numberOfItems.addAndGet(queue.size()) >= expectedTotalNumberOfItems) {
62                         mAllItemsNotified.countDown();
63                     }
64                 }
65             }
66         };
67         mMediaController = new MediaController(this, token);
68         mMediaController.registerCallback(mMediaControllerCallback,
69                 new Handler(Looper.getMainLooper()));
70     }
71 
testSeriesOfSetQueue_check()72     private void testSeriesOfSetQueue_check() throws Exception {
73         assertTrue(mAllItemsNotified.await(TIMEOUT_MS, MILLISECONDS));
74     }
75 
testSeriesOfSetQueue_cleanUp()76     private void testSeriesOfSetQueue_cleanUp() {
77         mMediaController.unregisterCallback(mMediaControllerCallback);
78         mMediaController = null;
79         mMediaControllerCallback = null;
80         mAllItemsNotified = null;
81     }
82 
testSetQueue_setUp(Bundle args)83     private void testSetQueue_setUp(Bundle args) {
84         MediaSession.Token token = args.getParcelable(KEY_SESSION_TOKEN);
85         int expectedQueueSize = args.getInt(KEY_EXPECTED_QUEUE_SIZE);
86 
87         mQueueNotified = new CountDownLatch(1);
88         mMediaControllerCallback = new MediaController.Callback() {
89             @Override
90             public void onQueueChanged(List<MediaSession.QueueItem> queue) {
91                 if (queue != null && queue.size() == expectedQueueSize) {
92                     mQueueNotified.countDown();
93                 }
94             }
95         };
96         mMediaController = new MediaController(this, token);
97         mMediaController.registerCallback(mMediaControllerCallback,
98                 new Handler(Looper.getMainLooper()));
99     }
100 
testSetQueue_check()101     private void testSetQueue_check() throws Exception {
102         assertTrue(mQueueNotified.await(TIMEOUT_MS, MILLISECONDS));
103     }
104 
testSetQueue_cleanUp()105     private void testSetQueue_cleanUp() {
106         mMediaController.unregisterCallback(mMediaControllerCallback);
107         mMediaController = null;
108         mMediaControllerCallback = null;
109         mQueueNotified = null;
110     }
111 
112     @Override
onRun(int testId, int step, @Nullable Bundle args)113     public void onRun(int testId, int step, @Nullable Bundle args) throws Exception {
114         if (testId == TEST_SERIES_OF_SET_QUEUE) {
115             if (step == STEP_SET_UP) {
116                 testSeriesOfSetQueue_setUp(args);
117             } else if (step == STEP_CHECK) {
118                 testSeriesOfSetQueue_check();
119             } else if (step == STEP_CLEAN_UP) {
120                 testSeriesOfSetQueue_cleanUp();
121             } else {
122                 throw new IllegalArgumentException("Unknown step=" + step);
123             }
124         } else if (testId == TEST_SET_QUEUE) {
125             if (step == STEP_SET_UP) {
126                 testSetQueue_setUp(args);
127             } else if (step == STEP_CHECK) {
128                 testSetQueue_check();
129             } else if (step == STEP_CLEAN_UP) {
130                 testSetQueue_cleanUp();
131             } else {
132                 throw new IllegalArgumentException("Unknown step=" + step);
133             }
134 
135         } else {
136             throw new IllegalArgumentException("Unknown testId=" + testId);
137         }
138     }
139 }
140