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