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.os.Bundle; 22 23 import com.android.internal.annotations.VisibleForTesting; 24 25 /** 26 * Provide a method to inject custom MediaBrowser objects for testing. By using the factory 27 * methods instead of calling the constructor of MediaBrowser directly, we can inject a custom 28 * MediaBrowser that can be used with JUnit and Mockito to set expectations and validate 29 * behaviour in tests. 30 */ 31 public final class MediaBrowserFactory { 32 private static MediaBrowser sInjectedBrowser; 33 wrap(android.media.browse.MediaBrowser delegate)34 static MediaBrowser wrap(android.media.browse.MediaBrowser delegate) { 35 if (sInjectedBrowser != null) return sInjectedBrowser; 36 return (delegate != null) ? new MediaBrowser(delegate) : null; 37 } 38 make(Context context, ComponentName serviceComponent, MediaBrowser.ConnectionCallback callback, Bundle rootHints)39 static MediaBrowser make(Context context, ComponentName serviceComponent, 40 MediaBrowser.ConnectionCallback callback, Bundle rootHints) { 41 if (sInjectedBrowser != null) { 42 sInjectedBrowser.testInit(context, serviceComponent, callback, rootHints); 43 return sInjectedBrowser; 44 } 45 return new MediaBrowser(context, serviceComponent, callback, rootHints); 46 } 47 48 @VisibleForTesting inject(MediaBrowser browser)49 static void inject(MediaBrowser browser) { 50 sInjectedBrowser = browser; 51 } 52 } 53