hughchen | 61c1c9f | 2019-01-17 14:34:51 +0800 | [diff] [blame] | 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.settingslib.media; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import static org.mockito.Mockito.mock; |
| 22 | import static org.mockito.Mockito.verify; |
| 23 | import static org.mockito.Mockito.when; |
| 24 | |
| 25 | import android.content.Context; |
| 26 | |
| 27 | import androidx.mediarouter.media.MediaRouteSelector; |
| 28 | import androidx.mediarouter.media.MediaRouter; |
| 29 | |
| 30 | import org.junit.Before; |
| 31 | import org.junit.Test; |
| 32 | import org.junit.runner.RunWith; |
| 33 | import org.mockito.Mock; |
| 34 | import org.mockito.MockitoAnnotations; |
| 35 | import org.robolectric.RobolectricTestRunner; |
| 36 | import org.robolectric.RuntimeEnvironment; |
| 37 | |
| 38 | @RunWith(RobolectricTestRunner.class) |
| 39 | public class InfoMediaManagerTest { |
| 40 | |
| 41 | private static final String TEST_PACKAGE_NAME = "com.test.packagename"; |
| 42 | private static final String TEST_ID = "test_id"; |
| 43 | |
| 44 | @Mock |
| 45 | private MediaRouter mMediaRouter; |
| 46 | @Mock |
| 47 | private MediaRouteSelector mSelector; |
| 48 | |
| 49 | private InfoMediaManager mInfoMediaManager; |
| 50 | private Context mContext; |
| 51 | |
| 52 | @Before |
| 53 | public void setUp() { |
| 54 | MockitoAnnotations.initMocks(this); |
| 55 | mContext = RuntimeEnvironment.application; |
| 56 | |
| 57 | mInfoMediaManager = new InfoMediaManager(mContext, TEST_PACKAGE_NAME, null); |
| 58 | mInfoMediaManager.mMediaRouter = mMediaRouter; |
| 59 | mInfoMediaManager.mSelector = mSelector; |
| 60 | } |
| 61 | |
| 62 | @Test |
| 63 | public void stopScan_shouldRemoveCallback() { |
| 64 | mInfoMediaManager.stopScan(); |
| 65 | |
| 66 | verify(mMediaRouter).removeCallback(mInfoMediaManager.mMediaRouterCallback); |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void startScan_shouldAddCallback() { |
| 71 | mInfoMediaManager.startScan(); |
| 72 | |
| 73 | verify(mMediaRouter).addCallback(mSelector, mInfoMediaManager.mMediaRouterCallback, |
| 74 | MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void onRouteAdded_mediaDeviceNotExistInList_addMediaDevice() { |
| 79 | final MediaRouter.RouteInfo info = mock(MediaRouter.RouteInfo.class); |
| 80 | when(info.getId()).thenReturn(TEST_ID); |
| 81 | |
| 82 | final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID); |
| 83 | assertThat(mediaDevice).isNull(); |
| 84 | |
| 85 | mInfoMediaManager.mMediaRouterCallback.onRouteAdded(mMediaRouter, info); |
| 86 | |
| 87 | final MediaDevice infoDevice = mInfoMediaManager.mMediaDevices.get(0); |
| 88 | assertThat(infoDevice.getId()).isEqualTo(TEST_ID); |
| 89 | } |
| 90 | |
| 91 | @Test |
| 92 | public void onRouteAdded_mediaDeviceExistInList_doNothing() { |
| 93 | final MediaRouter.RouteInfo info = mock(MediaRouter.RouteInfo.class); |
| 94 | when(info.getId()).thenReturn(TEST_ID); |
| 95 | final InfoMediaDevice infoDevice = new InfoMediaDevice(mContext, info); |
| 96 | mInfoMediaManager.mMediaDevices.add(infoDevice); |
| 97 | |
| 98 | final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID); |
| 99 | final int size = mInfoMediaManager.mMediaDevices.size(); |
| 100 | assertThat(mediaDevice).isNotNull(); |
| 101 | |
| 102 | mInfoMediaManager.mMediaRouterCallback.onRouteAdded(mMediaRouter, info); |
| 103 | |
| 104 | assertThat(mInfoMediaManager.mMediaDevices).hasSize(size); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void onRouteRemoved_mediaDeviceExistInList_removeMediaDevice() { |
| 109 | final MediaRouter.RouteInfo info = mock(MediaRouter.RouteInfo.class); |
| 110 | when(info.getId()).thenReturn(TEST_ID); |
| 111 | final InfoMediaDevice infoDevice = new InfoMediaDevice(mContext, info); |
| 112 | mInfoMediaManager.mMediaDevices.add(infoDevice); |
| 113 | |
| 114 | final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID); |
| 115 | assertThat(mediaDevice).isNotNull(); |
| 116 | assertThat(mInfoMediaManager.mMediaDevices).hasSize(1); |
| 117 | |
| 118 | mInfoMediaManager.mMediaRouterCallback.onRouteRemoved(mMediaRouter, info); |
| 119 | |
| 120 | assertThat(mInfoMediaManager.mMediaDevices).isEmpty(); |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void onRouteRemoved_mediaDeviceNotExistInList_doNothing() { |
| 125 | final MediaRouter.RouteInfo info = mock(MediaRouter.RouteInfo.class); |
| 126 | when(info.getId()).thenReturn(TEST_ID); |
| 127 | |
| 128 | final MediaDevice mediaDevice = mInfoMediaManager.findMediaDevice(TEST_ID); |
| 129 | final int size = mInfoMediaManager.mMediaDevices.size(); |
| 130 | assertThat(mediaDevice).isNull(); |
| 131 | |
| 132 | mInfoMediaManager.mMediaRouterCallback.onRouteRemoved(mMediaRouter, info); |
| 133 | |
| 134 | assertThat(mInfoMediaManager.mMediaDevices).hasSize(size); |
| 135 | } |
| 136 | } |