1 /* 2 * Copyright (C) 2024 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.car.docklib.media 18 19 import android.app.ActivityManager.RunningTaskInfo 20 import android.content.ComponentName 21 import android.content.Intent 22 import android.content.pm.PackageManager 23 import android.net.Uri 24 import androidx.test.ext.junit.runners.AndroidJUnit4 25 import com.android.car.docklib.media.MediaUtils.Companion.CAR_MEDIA_DATA_SCHEME 26 import com.google.common.truth.Truth.assertThat 27 import org.junit.Before 28 import org.junit.Test 29 import org.junit.runner.RunWith 30 import org.mockito.ArgumentMatchers.anyInt 31 import org.mockito.kotlin.argumentCaptor 32 import org.mockito.kotlin.doReturn 33 import org.mockito.kotlin.mock 34 import org.mockito.kotlin.verify 35 import org.mockito.kotlin.whenever 36 37 @RunWith(AndroidJUnit4::class) 38 class MediaUtilsTest { <lambda>null39 private val uriMock = mock<Uri> {} <lambda>null40 private val intentMock = mock<Intent> {} <lambda>null41 private val runningTaskInfoMock = mock<RunningTaskInfo> {} <lambda>null42 private val packageManagerMock = mock<PackageManager> {} 43 private val intentCaptor = argumentCaptor<Intent>() 44 45 @Before setupnull46 fun setup() { 47 runningTaskInfoMock.baseIntent = intentMock 48 } 49 50 @Test getMediaComponentName_noData_returnsNullnull51 fun getMediaComponentName_noData_returnsNull() { 52 whenever(intentMock.data).doReturn(null) 53 54 val ret = MediaUtils.getMediaComponentName(runningTaskInfoMock) 55 56 assertThat(ret).isNull() 57 } 58 59 @Test getMediaComponentName_incorrectDataScheme_returnsNullnull60 fun getMediaComponentName_incorrectDataScheme_returnsNull() { 61 whenever(intentMock.data).doReturn(uriMock) 62 whenever(uriMock.scheme).doReturn("MediaUtilsTestScheme") 63 64 val ret = MediaUtils.getMediaComponentName(runningTaskInfoMock) 65 66 assertThat(ret).isNull() 67 } 68 69 @Test getMediaComponentName_returnsCorrectComponentnull70 fun getMediaComponentName_returnsCorrectComponent() { 71 val componentName = ComponentName("testPkg", "testClass") 72 whenever(intentMock.data).doReturn(uriMock) 73 whenever(uriMock.scheme).doReturn(CAR_MEDIA_DATA_SCHEME) 74 whenever(uriMock.schemeSpecificPart).doReturn("/" + componentName.flattenToString()) 75 76 val ret = MediaUtils.getMediaComponentName(runningTaskInfoMock) 77 78 assertThat(ret).isEqualTo(componentName) 79 } 80 81 @Test fetchMediaServiceComponents_packageNotNull_queriesOnlyForThatPackagenull82 fun fetchMediaServiceComponents_packageNotNull_queriesOnlyForThatPackage() { 83 val pkg = "testPkg" 84 85 MediaUtils.fetchMediaServiceComponents(packageManagerMock, pkg) 86 87 verify(packageManagerMock).queryIntentServices(intentCaptor.capture(), anyInt()) 88 assertThat(intentCaptor.firstValue.getPackage()).isEqualTo(pkg) 89 } 90 } 91