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.mediaroutertest;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.res.Resources;
22 import android.media.MediaRoute2Info;
23 import android.media.RoutingSessionInfo;
24 
25 import androidx.test.ext.junit.runners.AndroidJUnit4;
26 import androidx.test.filters.SmallTest;
27 
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 
31 /**
32  * Tests {@link RoutingSessionInfo} and its {@link RoutingSessionInfo.Builder builder}.
33  */
34 @RunWith(AndroidJUnit4.class)
35 @SmallTest
36 public class RoutingSessionInfoTest {
37     public static final String TEST_ID = "test_id";
38     public static final String TEST_CLIENT_PACKAGE_NAME = "com.test.client.package.name";
39     public static final String TEST_NAME = "test_name";
40 
41     public static final String TEST_ROUTE_ID_0 = "test_route_type_0";
42     public static final String TEST_ROUTE_ID_2 = "test_route_type_2";
43     public static final String TEST_ROUTE_ID_4 = "test_route_type_4";
44     public static final String TEST_ROUTE_ID_6 = "test_route_type_6";
45 
46     public static final String TEST_PROVIDER_ID = "test_provider_id";
47     public static final String TEST_OTHER_PROVIDER_ID = "test_other_provider_id";
48 
49     // Tests if route IDs are changed properly according to provider ID.
50     @Test
testProviderId()51     public void testProviderId() {
52         RoutingSessionInfo sessionInfo = new RoutingSessionInfo.Builder(
53                 TEST_ID, TEST_CLIENT_PACKAGE_NAME)
54                 .setName(TEST_NAME)
55                 .addSelectedRoute(TEST_ROUTE_ID_0)
56                 .addSelectableRoute(TEST_ROUTE_ID_2)
57                 .addDeselectableRoute(TEST_ROUTE_ID_4)
58                 .addTransferableRoute(TEST_ROUTE_ID_6)
59                 .build();
60 
61         RoutingSessionInfo sessionInfoWithProviderId = new RoutingSessionInfo.Builder(sessionInfo)
62                 .setProviderId(TEST_PROVIDER_ID).build();
63 
64         assertThat(sessionInfoWithProviderId.getSelectedRoutes())
65                 .isNotEqualTo(sessionInfo.getSelectedRoutes());
66         assertThat(sessionInfoWithProviderId.getSelectableRoutes())
67                 .isNotEqualTo(sessionInfo.getSelectableRoutes());
68         assertThat(sessionInfoWithProviderId.getDeselectableRoutes())
69                 .isNotEqualTo(sessionInfo.getDeselectableRoutes());
70         assertThat(sessionInfoWithProviderId.getTransferableRoutes())
71                 .isNotEqualTo(sessionInfo.getTransferableRoutes());
72 
73         RoutingSessionInfo sessionInfoWithOtherProviderId =
74                 new RoutingSessionInfo.Builder(sessionInfoWithProviderId)
75                         .setProviderId(TEST_OTHER_PROVIDER_ID).build();
76 
77         assertThat(sessionInfoWithOtherProviderId.getSelectedRoutes())
78                 .isNotEqualTo(sessionInfoWithProviderId.getSelectedRoutes());
79         assertThat(sessionInfoWithOtherProviderId.getSelectableRoutes())
80                 .isNotEqualTo(sessionInfoWithProviderId.getSelectableRoutes());
81         assertThat(sessionInfoWithOtherProviderId.getDeselectableRoutes())
82                 .isNotEqualTo(sessionInfoWithProviderId.getDeselectableRoutes());
83         assertThat(sessionInfoWithOtherProviderId.getTransferableRoutes())
84                 .isNotEqualTo(sessionInfoWithProviderId.getTransferableRoutes());
85 
86         RoutingSessionInfo sessionInfoWithProviderId2 =
87                 new RoutingSessionInfo.Builder(sessionInfoWithProviderId).build();
88 
89         assertThat(sessionInfoWithProviderId2.getSelectedRoutes())
90                 .isEqualTo(sessionInfoWithProviderId.getSelectedRoutes());
91         assertThat(sessionInfoWithProviderId2.getSelectableRoutes())
92                 .isEqualTo(sessionInfoWithProviderId.getSelectableRoutes());
93         assertThat(sessionInfoWithProviderId2.getDeselectableRoutes())
94                 .isEqualTo(sessionInfoWithProviderId.getDeselectableRoutes());
95         assertThat(sessionInfoWithProviderId2.getTransferableRoutes())
96                 .isEqualTo(sessionInfoWithProviderId.getTransferableRoutes());
97     }
98 
99     @Test
testGetVolumeHandlingGroupSession()100     public void testGetVolumeHandlingGroupSession() {
101         RoutingSessionInfo sessionInfo = new RoutingSessionInfo.Builder(
102                 TEST_ID, TEST_CLIENT_PACKAGE_NAME)
103                 .setName(TEST_NAME)
104                 .addSelectedRoute(TEST_ROUTE_ID_0)
105                 .addSelectedRoute(TEST_ROUTE_ID_2)
106                 .setVolumeHandling(MediaRoute2Info.PLAYBACK_VOLUME_VARIABLE)
107                 .build();
108 
109         boolean volumeAdjustmentForRemoteGroupSessions = Resources.getSystem().getBoolean(
110                 com.android.internal.R.bool.config_volumeAdjustmentForRemoteGroupSessions);
111 
112         int expectedResult = volumeAdjustmentForRemoteGroupSessions
113                 ? MediaRoute2Info.PLAYBACK_VOLUME_VARIABLE :
114                 MediaRoute2Info.PLAYBACK_VOLUME_FIXED;
115 
116         assertThat(sessionInfo.getVolumeHandling()).isEqualTo(expectedResult);
117     }
118 }
119