• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.bluetooth.avrcp;
2 
3 import static org.mockito.Mockito.*;
4 
5 import android.content.Context;
6 import android.content.Intent;
7 import android.content.pm.PackageManager;
8 import android.content.pm.ResolveInfo;
9 import android.content.pm.ServiceInfo;
10 import android.media.AudioManager;
11 import android.os.Looper;
12 import android.support.test.InstrumentationRegistry;
13 import android.support.test.filters.MediumTest;
14 import android.support.test.runner.AndroidJUnit4;
15 
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 
20 import java.util.ArrayList;
21 import java.util.List;
22 
23 
24 /**
25  * Unit tests for {@link Avrcp}
26  */
27 @MediumTest
28 @RunWith(AndroidJUnit4.class)
29 public class AvrcpTest {
30     @Test
testCanStart()31     public void testCanStart() {
32         if (Looper.myLooper() == null) {
33             Looper.prepare();
34         }
35 
36         Avrcp a = Avrcp.make(InstrumentationRegistry.getTargetContext());
37     }
38 
39     @Test
testFailedBrowseStart()40     public void testFailedBrowseStart() {
41         if (Looper.myLooper() == null) {
42             Looper.prepare();
43         }
44 
45         Context mockContext = mock(Context.class);
46         AudioManager mockAudioManager = mock(AudioManager.class);
47         PackageManager mockPackageManager = mock(PackageManager.class);
48 
49         when(mockAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)).thenReturn(100);
50 
51         when(mockContext.getSystemService(Context.AUDIO_SERVICE)).thenReturn(mockAudioManager);
52 
53         when(mockContext.getApplicationContext()).thenReturn(mockContext);
54         when(mockContext.getPackageManager()).thenReturn(mockPackageManager);
55 
56 
57         // Call to get the BrowsableMediaPlayers
58         // We must return at least one to try to startService
59         List<ResolveInfo> resInfos = new ArrayList<ResolveInfo>();
60 
61         ServiceInfo fakeService = new ServiceInfo();
62         fakeService.name = ".browse.MediaBrowserService";
63         fakeService.packageName = "com.test.android.fake";
64 
65         ResolveInfo fakePackage = new ResolveInfo();
66         fakePackage.serviceInfo = fakeService;
67         fakePackage.nonLocalizedLabel = "Fake Package";
68         resInfos.add(fakePackage);
69         when(mockPackageManager.queryIntentServices(isA(Intent.class), anyInt())).thenReturn(
70                 resInfos);
71 
72         when(mockContext.startService(isA(Intent.class))).thenThrow(new SecurityException("test"));
73 
74         // Make calls start() which calls buildMediaPlayersList() which should
75         // try to start the service?
76         try {
77             Avrcp a = Avrcp.make(mockContext);
78         } catch (SecurityException e) {
79             Assert.fail(
80                     "Threw SecurityException instead of protecting against it: " + e.toString());
81         }
82     }
83 }
84