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