1 /*
2  * Copyright (C) 2022 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.bettertogether.cts;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static java.util.concurrent.TimeUnit.MILLISECONDS;
22 
23 import android.annotation.Nullable;
24 import android.content.ComponentName;
25 import android.media.browse.MediaBrowser;
26 import android.media.browse.MediaBrowser.MediaItem;
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.os.Looper;
30 
31 import java.util.List;
32 import java.util.concurrent.CountDownLatch;
33 import java.util.concurrent.atomic.AtomicInteger;
34 
35 public class MediaBrowserServiceTestService extends RemoteService {
36     public static final int TEST_SERIES_OF_NOTIFY_CHILDREN_CHANGED = 0;
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_SERVICE_COMPONENT_NAME = "serviceComponentName";
43     public static final String KEY_PARENT_MEDIA_ID = "parentMediaId";
44     public static final String KEY_EXPECTED_TOTAL_NUMBER_OF_ITEMS = "expectedTotalNumberOfItems";
45 
46     private final Handler mMainHandler = new Handler(Looper.getMainLooper());
47     private MediaBrowser mMediaBrowser;
48     private CountDownLatch mAllItemsNotified;
49 
testSeriesOfNotifyChildrenChanged_setUp(Bundle args)50     private void testSeriesOfNotifyChildrenChanged_setUp(Bundle args) throws Exception {
51         ComponentName componentName = args.getParcelable(KEY_SERVICE_COMPONENT_NAME);
52         String parentMediaId = args.getString(KEY_PARENT_MEDIA_ID);
53         int expectedTotalNumberOfItems = args.getInt(KEY_EXPECTED_TOTAL_NUMBER_OF_ITEMS);
54 
55         mAllItemsNotified = new CountDownLatch(1);
56         AtomicInteger numberOfItems = new AtomicInteger();
57         CountDownLatch subscribed = new CountDownLatch(1);
58         MediaBrowser.ConnectionCallback connectionCallback = new MediaBrowser.ConnectionCallback();
59         MediaBrowser.SubscriptionCallback subscriptionCallback =
60                 new MediaBrowser.SubscriptionCallback() {
61                     @Override
62                     public void onChildrenLoaded(String parentId, List<MediaItem> children) {
63                         if (parentMediaId.equals(parentId) && children != null) {
64                             if (subscribed.getCount() > 0) {
65                                 subscribed.countDown();
66                                 return;
67                             }
68                             if (numberOfItems.addAndGet(children.size())
69                                     >= expectedTotalNumberOfItems) {
70                                 mAllItemsNotified.countDown();
71                             }
72                         }
73                     }
74                 };
75         mMainHandler.post(() -> {
76             mMediaBrowser = new MediaBrowser(this, componentName, connectionCallback, null);
77             mMediaBrowser.connect();
78             mMediaBrowser.subscribe(parentMediaId, subscriptionCallback);
79         });
80         assertThat(subscribed.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
81     }
82 
testSeriesOfNotifyChildrenChanged_check()83     private void testSeriesOfNotifyChildrenChanged_check() throws Exception {
84         assertThat(mAllItemsNotified.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
85     }
86 
testSeriesOfNotifyChildrenChanged_cleanUp()87     private void testSeriesOfNotifyChildrenChanged_cleanUp() {
88         mMainHandler.post(() -> {
89             mMediaBrowser.disconnect();
90             mMediaBrowser = null;
91         });
92         mAllItemsNotified = null;
93     }
94 
95     @Override
onRun(int testId, int step, @Nullable Bundle args)96     public void onRun(int testId, int step, @Nullable Bundle args) throws Exception {
97         if (testId == TEST_SERIES_OF_NOTIFY_CHILDREN_CHANGED) {
98             if (step == STEP_SET_UP) {
99                 testSeriesOfNotifyChildrenChanged_setUp(args);
100             } else if (step == STEP_CHECK) {
101                 testSeriesOfNotifyChildrenChanged_check();
102             } else if (step == STEP_CLEAN_UP) {
103                 testSeriesOfNotifyChildrenChanged_cleanUp();
104             } else {
105                 throw new IllegalArgumentException("Unknown step=" + step);
106             }
107         } else {
108             throw new IllegalArgumentException("Unknown testId=" + testId);
109         }
110     }
111 }
112