1 package org.robolectric.shadows.support.v4; 2 3 import static android.support.v4.media.MediaBrowserCompat.MediaItem.FLAG_BROWSABLE; 4 import static android.support.v4.media.MediaBrowserCompat.MediaItem.FLAG_PLAYABLE; 5 import static com.google.common.truth.Truth.assertThat; 6 import static org.robolectric.shadows.support.v4.Shadows.shadowOf; 7 8 import android.content.ComponentName; 9 import android.content.Context; 10 import android.os.Bundle; 11 import android.support.annotation.NonNull; 12 import android.support.v4.media.MediaBrowserCompat; 13 import android.support.v4.media.MediaBrowserCompat.MediaItem; 14 import java.util.Arrays; 15 import java.util.Collections; 16 import java.util.List; 17 import org.junit.Before; 18 import org.junit.Test; 19 import org.junit.runner.RunWith; 20 import org.robolectric.RobolectricTestRunner; 21 import org.robolectric.RuntimeEnvironment; 22 23 /** Tests for {@link org.robolectric.shadows.support.v4.ShadowMediaBrowserCompat}. */ 24 @RunWith(RobolectricTestRunner.class) 25 public class ShadowMediaBrowserCompatTest { 26 27 private Context context = RuntimeEnvironment.application; 28 private MediaBrowserCompat mediaBrowser; 29 private ShadowMediaBrowserCompat shadow; 30 31 private MediaItem root; 32 private MediaItem child; 33 34 private final MediaItemCallBack mediaItemCallBack = new MediaItemCallBack(); 35 private final MediaSubscriptionCallback mediaSubscriptionCallback = 36 new MediaSubscriptionCallback(); 37 private final MediaSearchCallback mediaSearchCallback = new MediaSearchCallback(); 38 39 private static final String ROOT_ID = "root_id"; 40 private static final String CHILD_ID = "child_id"; 41 42 @Before setUp()43 public void setUp() { 44 final ComponentName componentName = new ComponentName("a", "b"); 45 mediaBrowser = 46 new MediaBrowserCompat( 47 context, componentName, new MediaBrowserCompat.ConnectionCallback(), null); 48 shadow = shadowOf(mediaBrowser); 49 mediaBrowser.connect(); 50 root = shadow.createMediaItem(null, ROOT_ID, "root_title", FLAG_BROWSABLE); 51 shadow.setRootId(ROOT_ID); 52 child = shadow.createMediaItem(ROOT_ID, CHILD_ID, "child_title", FLAG_PLAYABLE); 53 } 54 55 @Test mediaBrowserConnection_isConnected()56 public void mediaBrowserConnection_isConnected() { 57 assertThat(mediaBrowser.isConnected()).isTrue(); 58 } 59 60 @Test mediaBrowserConnection_isDisconnected()61 public void mediaBrowserConnection_isDisconnected() { 62 mediaBrowser.disconnect(); 63 assertThat(mediaBrowser.isConnected()).isFalse(); 64 } 65 66 @Test mediaBrowser_getRootId()67 public void mediaBrowser_getRootId() { 68 String mediaBrowserRootId = mediaBrowser.getRoot(); 69 assertThat(mediaBrowserRootId).isEqualTo(ROOT_ID); 70 } 71 72 @Test mediaBrowser_getItem()73 public void mediaBrowser_getItem() { 74 mediaBrowser.getItem(ROOT_ID, mediaItemCallBack); 75 assertThat(mediaItemCallBack.getMediaItem()).isEqualTo(root); 76 77 mediaItemCallBack.mediaItem = null; 78 mediaBrowser.getItem("fake_id", mediaItemCallBack); 79 assertThat(mediaItemCallBack.getMediaItem()).isNull(); 80 } 81 82 @Test mediaBrowser_subscribe()83 public void mediaBrowser_subscribe() { 84 mediaBrowser.subscribe(ROOT_ID, mediaSubscriptionCallback); 85 assertThat(mediaSubscriptionCallback.getResults()).isEqualTo(Collections.singletonList(child)); 86 87 mediaBrowser.subscribe(CHILD_ID, mediaSubscriptionCallback); 88 assertThat(mediaSubscriptionCallback.getResults()).isEmpty(); 89 90 mediaBrowser.subscribe("fake_id", mediaSubscriptionCallback); 91 assertThat(mediaSubscriptionCallback.getResults()).isEmpty(); 92 } 93 94 @Test mediaBrowser_search()95 public void mediaBrowser_search() { 96 mediaBrowser.search("root", null, mediaSearchCallback); 97 assertThat(mediaSearchCallback.getResults()).isEqualTo(Collections.singletonList(root)); 98 99 mediaBrowser.search("title", null, mediaSearchCallback); 100 final List<MediaItem> expectedResults = Arrays.asList(root, child); 101 assertThat(mediaSearchCallback.getResults()).isEqualTo(expectedResults); 102 103 mediaBrowser.search("none", null, mediaSearchCallback); 104 assertThat(mediaSearchCallback.getResults()).isEmpty(); 105 } 106 107 private static class MediaSearchCallback extends MediaBrowserCompat.SearchCallback { 108 109 List<MediaItem> results; 110 111 @Override onSearchResult( @onNull String query, Bundle bundle, @NonNull List<MediaItem> list)112 public void onSearchResult( 113 @NonNull String query, Bundle bundle, @NonNull List<MediaItem> list) { 114 super.onSearchResult(query, bundle, list); 115 results = list; 116 } 117 getResults()118 public List<MediaItem> getResults() { 119 return results; 120 } 121 } 122 123 private static class MediaSubscriptionCallback extends MediaBrowserCompat.SubscriptionCallback { 124 125 List<MediaItem> results; 126 127 @Override onChildrenLoaded(@onNull String parentId, @NonNull List<MediaItem> list)128 public void onChildrenLoaded(@NonNull String parentId, @NonNull List<MediaItem> list) { 129 super.onChildrenLoaded(parentId, list); 130 results = list; 131 } 132 getResults()133 public List<MediaItem> getResults() { 134 return results; 135 } 136 } 137 138 private static class MediaItemCallBack extends MediaBrowserCompat.ItemCallback { 139 140 MediaItem mediaItem; 141 142 @Override onItemLoaded(MediaItem mediaItem)143 public void onItemLoaded(MediaItem mediaItem) { 144 super.onItemLoaded(mediaItem); 145 this.mediaItem = mediaItem; 146 } 147 getMediaItem()148 MediaItem getMediaItem() { 149 return mediaItem; 150 } 151 } 152 } 153