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