1 /*
2  * Copyright 2018 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 com.android.bluetooth.avrcp;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.media.session.MediaSession;
22 import android.os.Bundle;
23 import android.util.Log;
24 
25 import com.android.internal.annotations.VisibleForTesting;
26 
27 /**
28  * Provide a mockable interface in order to test classes that use MediaBrowser.
29  * We need this class due to the fact that the MediaController class is marked as final and
30  * there is no way to currently mock final classes in Android. Once this is possible this class
31  * can be deleted.
32  */
33 public class MediaBrowser {
34     android.media.browse.MediaBrowser mDelegate;
35 
36     /**
37      * Wrap a real MediaBrowser object
38      */
MediaBrowser(android.media.browse.MediaBrowser delegate)39     public MediaBrowser(android.media.browse.MediaBrowser delegate) {
40         mDelegate = delegate;
41     }
42 
43     /**
44      * Create a real MediaBrowser object and wrap it
45      */
MediaBrowser(Context context, ComponentName serviceComponent, ConnectionCallback callback, Bundle rootHints)46     public MediaBrowser(Context context, ComponentName serviceComponent,
47             ConnectionCallback callback, Bundle rootHints) {
48         mDelegate = new android.media.browse.MediaBrowser(context, serviceComponent, callback,
49                 rootHints);
50     }
51 
52     /**
53      * Wrapper for MediaBrowser.ConnectionCallback
54      */
55     public abstract static class ConnectionCallback extends
56             android.media.browse.MediaBrowser.ConnectionCallback {}
57 
58     /**
59      * Wrapper for MediaBrowser.ItemCallback
60      */
61     public abstract static class ItemCallback extends
62             android.media.browse.MediaBrowser.ItemCallback {}
63 
64     /**
65      * Wrapper for MediaBrowser.SubscriptionCallback
66      */
67     public abstract static class SubscriptionCallback extends
68             android.media.browse.MediaBrowser.SubscriptionCallback {}
69 
70     /**
71      * Wrapper for MediaBrowser.connect()
72      */
connect()73     public void connect() {
74         mDelegate.connect();
75     }
76 
77     /**
78      * Wrapper for MediaBrowser.disconnect()
79      */
disconnect()80     public void disconnect() {
81         mDelegate.disconnect();
82     }
83 
84     /**
85      * Wrapper for MediaBrowser.getExtras()
86      */
getExtras()87     public Bundle getExtras() {
88         return mDelegate.getExtras();
89     }
90 
91     /**
92      * Wrapper for MediaBrowser.getItem(String mediaId, ItemCallback callback)
93      */
getItem(String mediaId, ItemCallback callback)94     public void getItem(String mediaId, ItemCallback callback) {
95         mDelegate.getItem(mediaId, callback);
96     }
97 
98     /**
99      * Wrapper for MediaBrowser.getRoot()
100      */
getRoot()101     public String getRoot() {
102         return mDelegate.getRoot();
103     }
104 
105     /**
106      * Wrapper for MediaBrowser.getServiceComponent()
107      */
getServiceComponent()108     public ComponentName getServiceComponent() {
109         return mDelegate.getServiceComponent();
110     }
111 
112     /**
113      * Wrapper for MediaBrowser.getSessionToken()
114      */
getSessionToken()115     public MediaSession.Token getSessionToken() {
116         return mDelegate.getSessionToken();
117     }
118     /**
119      * Wrapper for MediaBrowser.isConnected()
120      */
isConnected()121     public boolean isConnected() {
122         return mDelegate.isConnected();
123     }
124 
125     /**
126      * Wrapper for MediaBrowser.subscribe(String parentId, Bundle options,
127      * SubscriptionCallback callback)
128      */
subscribe(String parentId, Bundle options, SubscriptionCallback callback)129     public void subscribe(String parentId, Bundle options, SubscriptionCallback callback) {
130         mDelegate.subscribe(parentId, options, callback);
131     }
132 
133     /**
134      * Wrapper for MediaBrowser.subscribe(String parentId, SubscriptionCallback callback)
135      */
subscribe(String parentId, SubscriptionCallback callback)136     public void subscribe(String parentId, SubscriptionCallback callback) {
137         mDelegate.subscribe(parentId, callback);
138     }
139 
140     /**
141      * Wrapper for MediaBrowser.unsubscribe(String parentId)
142      */
unsubscribe(String parentId)143     public void unsubscribe(String parentId) {
144         mDelegate.unsubscribe(parentId);
145     }
146 
147 
148     /**
149      * Wrapper for MediaBrowser.unsubscribe(String parentId, SubscriptionCallback callback)
150      */
unsubscribe(String parentId, SubscriptionCallback callback)151     public void unsubscribe(String parentId, SubscriptionCallback callback) {
152         mDelegate.unsubscribe(parentId, callback);
153     }
154 
155     /**
156      * A function that allows Mockito to capture the constructor arguments when using
157      * MediaBrowserFactory.make()
158      */
159     @VisibleForTesting
testInit(Context context, ComponentName serviceComponent, ConnectionCallback callback, Bundle rootHints)160     public void testInit(Context context, ComponentName serviceComponent,
161             ConnectionCallback callback, Bundle rootHints) {
162         // This is only used by Mockito to capture the constructor arguments on creation
163         Log.wtf("AvrcpMockMediaBrowser", "This function should never be called");
164     }
165 }
166