1 /* 2 * Copyright 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 package com.android.bluetooth.audio_util; 18 19 import android.content.Context; 20 import android.os.Looper; 21 import android.text.TextUtils; 22 23 import com.android.internal.annotations.VisibleForTesting; 24 25 /** 26 * Provide a method to inject custom MediaControllerWrapper objects for testing. By using the 27 * factory methods instead of calling the wrap method of MediaControllerWrapper directly, we can 28 * inject a custom MediaControllerWrapper that can be used with JUnit and Mockito to set 29 * expectations and validate behaviour in tests. 30 */ 31 public final class MediaPlayerWrapperFactory { 32 private static MediaPlayerWrapper sInjectedWrapper; 33 wrap(Context context, MediaController controller, Looper looper)34 static MediaPlayerWrapper wrap(Context context, MediaController controller, Looper looper) { 35 if (sInjectedWrapper != null) return sInjectedWrapper; 36 if (controller == null || looper == null) { 37 return null; 38 } 39 40 MediaPlayerWrapper newWrapper; 41 if (TextUtils.equals(controller.getPackageName(), "com.google.android.music")) { 42 newWrapper = new GPMWrapper(context, controller, looper); 43 } else { 44 newWrapper = new MediaPlayerWrapper(context, controller, looper); 45 } 46 return newWrapper; 47 } 48 49 @VisibleForTesting inject(MediaPlayerWrapper wrapper)50 static void inject(MediaPlayerWrapper wrapper) { 51 sInjectedWrapper = wrapper; 52 } 53 } 54 55