1 /* 2 * Copyright (C) 2017 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 android.media.session.cts; 18 19 import static android.media.cts.MediaSessionTestHelperConstants.MEDIA_SESSION_TEST_HELPER_PKG; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertTrue; 22 import static org.junit.Assert.fail; 23 24 import android.content.ComponentName; 25 import android.content.Context; 26 import android.media.session.MediaController; 27 import android.media.session.MediaSessionManager; 28 import android.service.notification.NotificationListenerService; 29 import android.support.test.InstrumentationRegistry; 30 import android.support.test.filters.SmallTest; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 35 import java.util.List; 36 37 /** 38 * Tests {@link MediaSessionManager} with the multi-user environment. 39 * <p>Don't run tests here directly. They aren't stand-alone tests and each test will be run 40 * indirectly by the host-side test CtsMediaHostTestCases after the proper device setup. 41 */ 42 @SmallTest 43 public class MediaSessionManagerTest extends NotificationListenerService { 44 private ComponentName mComponentName; 45 private MediaSessionManager mMediaSessionManager; 46 47 @Before 48 public void setUp() throws Exception { 49 Context context = InstrumentationRegistry.getTargetContext(); 50 mMediaSessionManager = (MediaSessionManager) context.getSystemService( 51 Context.MEDIA_SESSION_SERVICE); 52 mComponentName = new ComponentName(context, MediaSessionManagerTest.class); 53 } 54 55 /** 56 * Tests if the MediaSessionTestHelper doesn't have an active media session. 57 */ 58 @Test 59 public void testGetActiveSessions_noMediaSessionFromMediaSessionTestHelper() throws Exception { 60 List<MediaController> controllers = mMediaSessionManager.getActiveSessions(mComponentName); 61 for (MediaController controller : controllers) { 62 if (controller.getPackageName().equals(MEDIA_SESSION_TEST_HELPER_PKG)) { 63 fail("Media session for the media session app shouldn't be available"); 64 return; 65 } 66 } 67 } 68 69 /** 70 * Tests if the MediaSessionTestHelper has an active media session. 71 */ 72 @Test 73 public void testGetActiveSessions_hasMediaSessionFromMediaSessionTestHelper() throws Exception { 74 List<MediaController> controllers = mMediaSessionManager.getActiveSessions(mComponentName); 75 for (MediaController controller : controllers) { 76 if (controller.getPackageName().equals(MEDIA_SESSION_TEST_HELPER_PKG)) { 77 // Test success 78 return; 79 } 80 } 81 fail("Media session for the media session app is expected"); 82 } 83 84 /** 85 * Tests if there's no media session. 86 */ 87 @Test 88 public void testGetActiveSessions_noMediaSession() throws Exception { 89 List<MediaController> controllers = mMediaSessionManager.getActiveSessions(mComponentName); 90 assertTrue(controllers.isEmpty()); 91 } 92 } 93