• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.car.media;
18 
19 import static org.junit.Assert.assertThrows;
20 
21 import android.car.test.AbstractExpectableTestCase;
22 import android.os.Parcel;
23 
24 import org.junit.Test;
25 
26 public final class CarAudioPatchHandleUnitTest extends AbstractExpectableTestCase {
27 
28     private static final int TEST_PARCEL_FLAGS = 0;
29     private static final int TEST_HANDLE_ID = 1;
30     private static final String TEST_SOURCE_ADDRESS = "media_bus_device";
31     private static final String TEST_SINK_ADDRESS = "FM Tuner";
32     private static final CarAudioPatchHandle TEST_AUDIO_PATCH_HANDLE = new CarAudioPatchHandle(
33             TEST_HANDLE_ID, TEST_SOURCE_ADDRESS, TEST_SINK_ADDRESS);
34 
35     @Test
constructor_withNullSourceAddress()36     public void constructor_withNullSourceAddress() {
37         NullPointerException thrown = assertThrows(NullPointerException.class, () ->
38                 new CarAudioPatchHandle(TEST_HANDLE_ID, /* sourceAddress= */ null,
39                         TEST_SINK_ADDRESS));
40 
41         expectWithMessage("Exception for constructing car audio patch handle with null "
42                 + "source address").that(thrown).hasMessageThat().contains(
43                         "Source's Address device can not be null");
44     }
45 
46     @Test
constructor_withNullSinkAddress()47     public void constructor_withNullSinkAddress() {
48         NullPointerException thrown = assertThrows(NullPointerException.class, () ->
49                 new CarAudioPatchHandle(TEST_HANDLE_ID, TEST_SOURCE_ADDRESS,
50                         /* sinkAddress= */ null));
51 
52         expectWithMessage("Exception for constructing car audio patch handle with null "
53                 + "sink address").that(thrown).hasMessageThat().contains(
54                 "Sink's Address device can not be null");
55     }
56 
57     @Test
toString_containsIdAndAddresses()58     public void toString_containsIdAndAddresses() {
59         String audioPatchHandleString = TEST_AUDIO_PATCH_HANDLE.toString();
60 
61         expectWithMessage("Audio patch string for id %s", TEST_HANDLE_ID).that(
62                 audioPatchHandleString).contains(Integer.toString(TEST_HANDLE_ID));
63         expectWithMessage("Audio patch string for source address %s", TEST_SOURCE_ADDRESS).that(
64                 audioPatchHandleString).contains(TEST_SOURCE_ADDRESS);
65         expectWithMessage("Audio patch string for sink address %s", TEST_SINK_ADDRESS).that(
66                 audioPatchHandleString).contains(TEST_SINK_ADDRESS);
67     }
68 
69     @Test
writeToParcel_createFromParcel()70     public void writeToParcel_createFromParcel() {
71         Parcel parcel = Parcel.obtain();
72 
73         TEST_AUDIO_PATCH_HANDLE.writeToParcel(parcel, TEST_PARCEL_FLAGS);
74         parcel.setDataPosition(/* pos= */ 0);
75         CarAudioPatchHandle handleFromParcel = CarAudioPatchHandle.CREATOR.createFromParcel(parcel);
76 
77         expectWithMessage("Handle id in car audio handle created from parcel")
78                 .that(handleFromParcel.getHandleId()).isEqualTo(TEST_HANDLE_ID);
79         expectWithMessage("Source address in car audio handle created from parcel")
80                 .that(handleFromParcel.getSourceAddress()).isEqualTo(TEST_SOURCE_ADDRESS);
81         expectWithMessage("Sink address in car audio handle created from parcel")
82                 .that(handleFromParcel.getSinkAddress()).isEqualTo(TEST_SINK_ADDRESS);
83     }
84 
85     @Test
newArray()86     public void newArray() {
87         CarAudioPatchHandle[] handles = CarAudioPatchHandle.CREATOR.newArray(/* size= */ 3);
88 
89         expectWithMessage("Car audio patch handles size").that(handles).hasLength(3);
90     }
91 
92     @Test
getSourceAddress()93     public void getSourceAddress() {
94         expectWithMessage("Source address").that(TEST_AUDIO_PATCH_HANDLE.getSourceAddress())
95                 .isEqualTo(TEST_SOURCE_ADDRESS);
96     }
97 
98     @Test
getSinkAddress()99     public void getSinkAddress() {
100         expectWithMessage("Sink address").that(TEST_AUDIO_PATCH_HANDLE.getSinkAddress())
101                 .isEqualTo(TEST_SINK_ADDRESS);
102     }
103 
104     @Test
getHandleId()105     public void getHandleId() {
106         expectWithMessage("Handle id").that(TEST_AUDIO_PATCH_HANDLE.getHandleId())
107                 .isEqualTo(TEST_HANDLE_ID);
108     }
109 }
110