1 /*
2  * Copyright (C) 2022 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.server.telecom.tests;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertThrows;
21 import static org.mockito.Mockito.isA;
22 import static org.mockito.Mockito.times;
23 import static org.mockito.Mockito.verify;
24 
25 import android.content.ComponentName;
26 import android.net.Uri;
27 import android.os.Parcel;
28 import android.telecom.CallAttributes;
29 import android.telecom.PhoneAccountHandle;
30 
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 
37 public class CallAttributesTests extends TelecomTestCase {
38 
39     private static final PhoneAccountHandle mHandle = new PhoneAccountHandle(
40             new ComponentName("foo", "bar"), "1");
41     private static final String TEST_NAME = "Larry Page";
42     private static final Uri TEST_URI = Uri.fromParts("tel", "abc", "123");
43     @Mock private Parcel mParcel;
44 
45     @Override
46     @Before
setUp()47     public void setUp() throws Exception {
48         super.setUp();
49         MockitoAnnotations.initMocks(this);
50     }
51 
52     @Override
53     @After
tearDown()54     public void tearDown() throws Exception {
55         super.tearDown();
56     }
57 
58     @Test
testRequiredAttributes()59     public void testRequiredAttributes() {
60         CallAttributes callAttributes = new CallAttributes.Builder(mHandle,
61                 CallAttributes.DIRECTION_OUTGOING, TEST_NAME, TEST_URI).build();
62 
63         assertEquals(CallAttributes.DIRECTION_OUTGOING, callAttributes.getDirection());
64         assertEquals(mHandle, callAttributes.getPhoneAccountHandle());
65     }
66 
67     @Test
testInvalidDirectionAttributes()68     public void testInvalidDirectionAttributes() {
69         assertThrows(IllegalArgumentException.class, () ->
70                 new CallAttributes.Builder(mHandle, -1, TEST_NAME, TEST_URI).build()
71         );
72     }
73 
74     @Test
testInvalidCallType()75     public void testInvalidCallType() {
76         assertThrows(IllegalArgumentException.class, () ->
77                 new CallAttributes.Builder(mHandle, CallAttributes.DIRECTION_OUTGOING,
78                         TEST_NAME, TEST_URI).setCallType(-1).build()
79         );
80     }
81 
82     @Test
testOptionalAttributes()83     public void testOptionalAttributes() {
84         CallAttributes callAttributes = new CallAttributes.Builder(mHandle,
85                 CallAttributes.DIRECTION_OUTGOING, TEST_NAME, TEST_URI)
86                 .setCallCapabilities(CallAttributes.SUPPORTS_SET_INACTIVE)
87                 .setCallType(CallAttributes.AUDIO_CALL)
88                 .build();
89 
90         assertEquals(CallAttributes.DIRECTION_OUTGOING, callAttributes.getDirection());
91         assertEquals(mHandle, callAttributes.getPhoneAccountHandle());
92         assertEquals(CallAttributes.SUPPORTS_SET_INACTIVE, callAttributes.getCallCapabilities());
93         assertEquals(CallAttributes.AUDIO_CALL, callAttributes.getCallType());
94         assertEquals(TEST_URI, callAttributes.getAddress());
95         assertEquals(TEST_NAME, callAttributes.getDisplayName());
96     }
97 
98     @Test
testDescribeContents()99     public void testDescribeContents() {
100         CallAttributes callAttributes = new CallAttributes.Builder(mHandle,
101                 CallAttributes.DIRECTION_OUTGOING, TEST_NAME, TEST_URI).build();
102 
103         assertEquals(0, callAttributes.describeContents());
104     }
105 
106     @Test
testWriteToParcel()107     public void testWriteToParcel() {
108         // GIVEN
109         CallAttributes callAttributes = new CallAttributes.Builder(mHandle,
110                 CallAttributes.DIRECTION_OUTGOING, TEST_NAME, TEST_URI)
111                 .setCallCapabilities(CallAttributes.SUPPORTS_SET_INACTIVE)
112                 .setCallType(CallAttributes.AUDIO_CALL)
113                 .build();
114 
115         // WHEN
116         callAttributes.writeToParcel(mParcel, 0);
117 
118         // THEN
119         verify(mParcel, times(1))
120                 .writeParcelable(isA(PhoneAccountHandle.class), isA(Integer.class));
121         verify(mParcel, times(1)).writeCharSequence(isA(CharSequence.class));
122         verify(mParcel, times(1))
123                 .writeParcelable(isA(Uri.class), isA(Integer.class));
124         verify(mParcel, times(3)).writeInt(isA(Integer.class));
125     }
126 }
127