1 /* 2 * Copyright (C) 2020 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.settings.media; 18 19 import static android.app.slice.Slice.EXTRA_RANGE_VALUE; 20 import static android.app.slice.Slice.HINT_LIST_ITEM; 21 import static android.app.slice.SliceItem.FORMAT_SLICE; 22 23 import static com.android.settings.media.MediaOutputGroupSlice.ACTION_MEDIA_SESSION_OPERATION; 24 import static com.android.settings.media.MediaOutputGroupSlice.ACTION_VOLUME_ADJUSTMENT; 25 import static com.android.settings.media.MediaOutputGroupSlice.CUSTOMIZED_ACTION; 26 import static com.android.settings.media.MediaOutputGroupSlice.GROUP_DEVICES; 27 import static com.android.settings.media.MediaOutputGroupSlice.MEDIA_DEVICE_ID; 28 import static com.android.settings.slices.CustomSliceRegistry.MEDIA_OUTPUT_GROUP_SLICE_URI; 29 30 import static com.google.common.truth.Truth.assertThat; 31 32 import static org.mockito.ArgumentMatchers.anyInt; 33 import static org.mockito.Mockito.doReturn; 34 import static org.mockito.Mockito.never; 35 import static org.mockito.Mockito.spy; 36 import static org.mockito.Mockito.verify; 37 import static org.mockito.Mockito.when; 38 39 import android.content.Context; 40 import android.content.Intent; 41 import android.graphics.drawable.Drawable; 42 import android.net.Uri; 43 44 import androidx.slice.Slice; 45 import androidx.slice.SliceMetadata; 46 import androidx.slice.SliceProvider; 47 import androidx.slice.core.SliceAction; 48 import androidx.slice.core.SliceQuery; 49 import androidx.slice.widget.SliceLiveData; 50 51 import com.android.settings.R; 52 import com.android.settings.slices.SliceBackgroundWorker; 53 import com.android.settingslib.media.LocalMediaManager; 54 import com.android.settingslib.media.MediaDevice; 55 56 import org.junit.Before; 57 import org.junit.Test; 58 import org.junit.runner.RunWith; 59 import org.mockito.Mock; 60 import org.mockito.MockitoAnnotations; 61 import org.robolectric.RobolectricTestRunner; 62 import org.robolectric.RuntimeEnvironment; 63 import org.robolectric.annotation.Config; 64 import org.robolectric.annotation.Implementation; 65 import org.robolectric.annotation.Implements; 66 67 import java.util.ArrayList; 68 import java.util.List; 69 70 @RunWith(RobolectricTestRunner.class) 71 @Config(shadows = MediaOutputGroupSliceTest.ShadowSliceBackgroundWorker.class) 72 public class MediaOutputGroupSliceTest { 73 74 private static final String TEST_PACKAGE_NAME = "com.test.music"; 75 private static final String TEST_PACKAGE_NAME2 = "com.test.music2"; 76 private static final String TEST_DEVICE_1_ID = "test_device_1_id"; 77 private static final String TEST_DEVICE_1_NAME = "test_device_1_name"; 78 private static final String TEST_DEVICE_2_ID = "test_device_2_id"; 79 private static final String TEST_DEVICE_2_NAME = "test_device_2_name"; 80 private static final int TEST_VOLUME = 3; 81 82 private static MediaDeviceUpdateWorker sMediaDeviceUpdateWorker; 83 84 @Mock 85 private LocalMediaManager mLocalMediaManager; 86 @Mock 87 private MediaDevice mDevice1; 88 @Mock 89 private MediaDevice mDevice2; 90 91 private final List<MediaDevice> mSelectableDevices = new ArrayList<>(); 92 private final List<MediaDevice> mSelectedDevices = new ArrayList<>(); 93 private final List<MediaDevice> mDeselectableDevices = new ArrayList<>(); 94 95 private Context mContext; 96 private MediaOutputGroupSlice mMediaOutputGroupSlice; 97 private Drawable mDrawable; 98 99 @Before setUp()100 public void setUp() throws Exception { 101 MockitoAnnotations.initMocks(this); 102 mContext = spy(RuntimeEnvironment.application); 103 104 // Set-up specs for SliceMetadata. 105 SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS); 106 107 mMediaOutputGroupSlice = new MediaOutputGroupSlice(mContext); 108 sMediaDeviceUpdateWorker = spy(new MediaDeviceUpdateWorker(mContext, 109 MEDIA_OUTPUT_GROUP_SLICE_URI)); 110 sMediaDeviceUpdateWorker.mLocalMediaManager = mLocalMediaManager; 111 when(sMediaDeviceUpdateWorker.getPackageName()).thenReturn(TEST_PACKAGE_NAME); 112 mDrawable = mContext.getDrawable(R.drawable.ic_check_box_blue_24dp); 113 when(sMediaDeviceUpdateWorker.getSelectableMediaDevice()).thenReturn(mSelectableDevices); 114 doReturn(false).when(sMediaDeviceUpdateWorker).hasAdjustVolumeUserRestriction(); 115 when(mDevice1.getId()).thenReturn(TEST_DEVICE_1_ID); 116 when(mDevice1.getIcon()).thenReturn(mDrawable); 117 when(mDevice1.getName()).thenReturn(TEST_DEVICE_1_NAME); 118 when(mDevice1.getMaxVolume()).thenReturn(100); 119 when(mDevice1.getCurrentVolume()).thenReturn(10); 120 when(mDevice1.getClientPackageName()).thenReturn(TEST_PACKAGE_NAME); 121 when(mDevice2.getId()).thenReturn(TEST_DEVICE_2_ID); 122 when(mDevice2.getIcon()).thenReturn(mDrawable); 123 when(mDevice2.getName()).thenReturn(TEST_DEVICE_2_NAME); 124 when(mDevice2.getMaxVolume()).thenReturn(100); 125 when(mDevice2.getCurrentVolume()).thenReturn(20); 126 } 127 128 @Test getSlice_noMatchedDevice_doNothing()129 public void getSlice_noMatchedDevice_doNothing() { 130 mSelectableDevices.add(mDevice1); 131 mSelectedDevices.add(mDevice1); 132 when(mLocalMediaManager.getMediaDeviceById(mSelectableDevices, TEST_DEVICE_1_ID)) 133 .thenReturn(mDevice1); 134 sMediaDeviceUpdateWorker.onDeviceListUpdate(mSelectableDevices); 135 when(sMediaDeviceUpdateWorker.getSelectedMediaDevice()).thenReturn(mSelectedDevices); 136 final Intent intent = new Intent(); 137 intent.putExtra(EXTRA_RANGE_VALUE, TEST_VOLUME); 138 intent.putExtra(MEDIA_DEVICE_ID, TEST_DEVICE_2_ID); 139 intent.putExtra(CUSTOMIZED_ACTION, ACTION_VOLUME_ADJUSTMENT); 140 141 mMediaOutputGroupSlice.onNotifyChange(intent); 142 143 verify(sMediaDeviceUpdateWorker, never()).adjustSessionVolume(anyInt()); 144 verify(mDevice1, never()).requestSetVolume(TEST_VOLUME); 145 } 146 147 @Test getSlice_withOneSelectableDevice_checkRowNumber()148 public void getSlice_withOneSelectableDevice_checkRowNumber() { 149 mSelectableDevices.add(mDevice1); 150 mSelectedDevices.add(mDevice2); 151 when(sMediaDeviceUpdateWorker.getSelectedMediaDevice()).thenReturn(mSelectedDevices); 152 when(sMediaDeviceUpdateWorker.getSelectableMediaDevice()).thenReturn(mSelectableDevices); 153 final Slice slice = mMediaOutputGroupSlice.getSlice(); 154 final int rows = SliceQuery.findAll(slice, FORMAT_SLICE, HINT_LIST_ITEM, null).size(); 155 156 // Group item and 2 * InputRange 157 assertThat(rows).isEqualTo(3); 158 } 159 160 @Test getSlice_nullWorker_noException()161 public void getSlice_nullWorker_noException() { 162 sMediaDeviceUpdateWorker = null; 163 mMediaOutputGroupSlice.getSlice(); 164 } 165 166 @Test getSlice_withOneSelectableDevice_checkTitle()167 public void getSlice_withOneSelectableDevice_checkTitle() { 168 mSelectableDevices.add(mDevice1); 169 mSelectedDevices.add(mDevice1); 170 sMediaDeviceUpdateWorker.onDeviceListUpdate(mSelectableDevices); 171 when(sMediaDeviceUpdateWorker.getSelectedMediaDevice()).thenReturn(mSelectedDevices); 172 final Slice slice = mMediaOutputGroupSlice.getSlice(); 173 final SliceMetadata metadata = SliceMetadata.from(mContext, slice); 174 final SliceAction primaryAction = metadata.getPrimaryAction(); 175 176 assertThat(primaryAction.getTitle().toString()).isEqualTo(GROUP_DEVICES); 177 } 178 179 @Test onNotifyChange_verifyAdjustDeviceVolume()180 public void onNotifyChange_verifyAdjustDeviceVolume() { 181 mSelectableDevices.add(mDevice1); 182 mSelectedDevices.add(mDevice1); 183 when(mLocalMediaManager.getMediaDeviceById(mSelectableDevices, TEST_DEVICE_1_ID)) 184 .thenReturn(mDevice1); 185 sMediaDeviceUpdateWorker.onDeviceListUpdate(mSelectableDevices); 186 when(sMediaDeviceUpdateWorker.getSelectedMediaDevice()).thenReturn(mSelectedDevices); 187 final Intent intent = new Intent(); 188 intent.putExtra(EXTRA_RANGE_VALUE, TEST_VOLUME); 189 intent.putExtra(MEDIA_DEVICE_ID, TEST_DEVICE_1_ID); 190 intent.putExtra(CUSTOMIZED_ACTION, ACTION_VOLUME_ADJUSTMENT); 191 192 mMediaOutputGroupSlice.onNotifyChange(intent); 193 194 verify(mDevice1).requestSetVolume(TEST_VOLUME); 195 } 196 197 @Test onNotifyChange_verifyAdjustGroupVolume()198 public void onNotifyChange_verifyAdjustGroupVolume() { 199 mSelectableDevices.add(mDevice1); 200 mSelectedDevices.add(mDevice1); 201 when(mLocalMediaManager.getMediaDeviceById(mSelectableDevices, TEST_DEVICE_1_ID)) 202 .thenReturn(mDevice1); 203 sMediaDeviceUpdateWorker.onDeviceListUpdate(mSelectableDevices); 204 when(sMediaDeviceUpdateWorker.getSelectedMediaDevice()).thenReturn(mSelectedDevices); 205 final Intent intent = new Intent(); 206 intent.putExtra(EXTRA_RANGE_VALUE, TEST_VOLUME); 207 intent.putExtra(MEDIA_DEVICE_ID, GROUP_DEVICES); 208 intent.putExtra(CUSTOMIZED_ACTION, ACTION_VOLUME_ADJUSTMENT); 209 210 mMediaOutputGroupSlice.onNotifyChange(intent); 211 212 verify(sMediaDeviceUpdateWorker).adjustSessionVolume(TEST_VOLUME); 213 } 214 215 @Test onNotifyChange_sendSelectableDevice_verifyAddSession()216 public void onNotifyChange_sendSelectableDevice_verifyAddSession() { 217 mSelectableDevices.add(mDevice2); 218 mSelectedDevices.add(mDevice1); 219 when(mLocalMediaManager.getMediaDeviceById(mSelectableDevices, TEST_DEVICE_2_ID)) 220 .thenReturn(mDevice2); 221 sMediaDeviceUpdateWorker.onDeviceListUpdate(mSelectableDevices); 222 when(sMediaDeviceUpdateWorker.getSelectedMediaDevice()).thenReturn(mSelectedDevices); 223 final Intent intent = new Intent(); 224 intent.putExtra(MEDIA_DEVICE_ID, TEST_DEVICE_2_ID); 225 intent.putExtra(CUSTOMIZED_ACTION, ACTION_MEDIA_SESSION_OPERATION); 226 227 mMediaOutputGroupSlice.onNotifyChange(intent); 228 229 verify(sMediaDeviceUpdateWorker).addDeviceToPlayMedia(mDevice2); 230 } 231 232 @Test onNotifyChange_sendDeselectableDevice_verifyRemoveSession()233 public void onNotifyChange_sendDeselectableDevice_verifyRemoveSession() { 234 mSelectedDevices.add(mDevice1); 235 mSelectedDevices.add(mDevice2); 236 mDeselectableDevices.add(mDevice1); 237 mDeselectableDevices.add(mDevice2); 238 when(mLocalMediaManager.getMediaDeviceById(mSelectedDevices, TEST_DEVICE_2_ID)) 239 .thenReturn(mDevice2); 240 sMediaDeviceUpdateWorker.onDeviceListUpdate(mSelectedDevices); 241 when(sMediaDeviceUpdateWorker.getSelectedMediaDevice()).thenReturn(mSelectedDevices); 242 when(sMediaDeviceUpdateWorker.getDeselectableMediaDevice()).thenReturn( 243 mDeselectableDevices); 244 final Intent intent = new Intent(); 245 intent.putExtra(MEDIA_DEVICE_ID, TEST_DEVICE_2_ID); 246 intent.putExtra(CUSTOMIZED_ACTION, ACTION_MEDIA_SESSION_OPERATION); 247 248 mMediaOutputGroupSlice.onNotifyChange(intent); 249 250 verify(sMediaDeviceUpdateWorker).removeDeviceFromPlayMedia(mDevice2); 251 } 252 253 @Test onNotifyChange_sendNonDeselectableDevice_notRemoveSession()254 public void onNotifyChange_sendNonDeselectableDevice_notRemoveSession() { 255 mSelectedDevices.add(mDevice1); 256 mSelectedDevices.add(mDevice2); 257 mDeselectableDevices.add(mDevice1); 258 when(mLocalMediaManager.getMediaDeviceById(mSelectedDevices, TEST_DEVICE_2_ID)) 259 .thenReturn(mDevice2); 260 sMediaDeviceUpdateWorker.onDeviceListUpdate(mSelectedDevices); 261 when(sMediaDeviceUpdateWorker.getSelectedMediaDevice()).thenReturn(mSelectedDevices); 262 when(sMediaDeviceUpdateWorker.getDeselectableMediaDevice()).thenReturn( 263 mDeselectableDevices); 264 final Intent intent = new Intent(); 265 intent.putExtra(MEDIA_DEVICE_ID, TEST_DEVICE_2_ID); 266 intent.putExtra(CUSTOMIZED_ACTION, ACTION_MEDIA_SESSION_OPERATION); 267 268 mMediaOutputGroupSlice.onNotifyChange(intent); 269 270 verify(sMediaDeviceUpdateWorker, never()).removeDeviceFromPlayMedia(mDevice2); 271 } 272 273 @Test onNotifyChange_noId_doNothing()274 public void onNotifyChange_noId_doNothing() { 275 mSelectableDevices.add(mDevice1); 276 mSelectedDevices.add(mDevice1); 277 when(mLocalMediaManager.getMediaDeviceById(mSelectableDevices, TEST_DEVICE_1_ID)) 278 .thenReturn(mDevice1); 279 sMediaDeviceUpdateWorker.onDeviceListUpdate(mSelectableDevices); 280 when(sMediaDeviceUpdateWorker.getSelectedMediaDevice()).thenReturn(mSelectedDevices); 281 final Intent intent = new Intent(); 282 intent.putExtra(EXTRA_RANGE_VALUE, TEST_VOLUME); 283 intent.putExtra(CUSTOMIZED_ACTION, ACTION_VOLUME_ADJUSTMENT); 284 285 mMediaOutputGroupSlice.onNotifyChange(intent); 286 287 verify(sMediaDeviceUpdateWorker, never()).adjustSessionVolume(anyInt()); 288 verify(mDevice1, never()).requestSetVolume(TEST_VOLUME); 289 } 290 291 @Implements(SliceBackgroundWorker.class) 292 public static class ShadowSliceBackgroundWorker { 293 294 @Implementation getInstance(Uri uri)295 public static SliceBackgroundWorker getInstance(Uri uri) { 296 return sMediaDeviceUpdateWorker; 297 } 298 } 299 } 300