1 /* 2 * Copyright (C) 2015 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.telecom.cts; 18 19 import static android.telecom.cts.TestUtils.*; 20 21 import android.content.ComponentName; 22 import android.content.Context; 23 import android.graphics.Color; 24 import android.graphics.drawable.Icon; 25 import android.media.ToneGenerator; 26 import android.net.Uri; 27 import android.os.Bundle; 28 import android.os.Parcel; 29 import android.os.Process; 30 import android.os.UserHandle; 31 import android.telecom.CallAudioState; 32 import android.telecom.ConnectionRequest; 33 import android.telecom.DisconnectCause; 34 import android.telecom.GatewayInfo; 35 import android.telecom.PhoneAccount; 36 import android.telecom.PhoneAccountHandle; 37 import android.telecom.StatusHints; 38 import android.telecom.TelecomManager; 39 import android.telecom.VideoProfile; 40 import android.test.InstrumentationTestCase; 41 42 import com.android.cts.telecom.R; 43 44 import java.util.Arrays; 45 import java.util.List; 46 47 /** 48 * Verifies that the setter, getter and parcelable interfaces of the Telecom data objects are 49 * working as intended. 50 */ 51 public class DataObjectUnitTests extends InstrumentationTestCase { 52 53 54 public void testPhoneAccount() throws Exception { 55 Context context = getInstrumentation().getContext(); 56 PhoneAccountHandle accountHandle = new PhoneAccountHandle( 57 new ComponentName(PACKAGE, COMPONENT), 58 ACCOUNT_ID); 59 Icon phoneIcon = Icon.createWithResource(context, R.drawable.ic_phone_24dp); 60 Uri tel = Uri.parse("tel:555-TEST"); 61 PhoneAccount account = PhoneAccount.builder( 62 accountHandle, ACCOUNT_LABEL) 63 .setAddress(tel) 64 .setSubscriptionAddress(tel) 65 .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER) 66 .setHighlightColor(Color.RED) 67 .setShortDescription(ACCOUNT_LABEL) 68 .setSupportedUriSchemes(Arrays.asList("tel")) 69 .setIcon(phoneIcon) 70 .build(); 71 assertNotNull(account); 72 assertEquals(accountHandle, account.getAccountHandle()); 73 assertEquals(tel, account.getAddress()); 74 assertEquals(tel, account.getSubscriptionAddress()); 75 assertEquals(PhoneAccount.CAPABILITY_CALL_PROVIDER, account.getCapabilities()); 76 assertEquals(Color.RED, account.getHighlightColor()); 77 assertEquals(ACCOUNT_LABEL, account.getShortDescription()); 78 assertEquals(ACCOUNT_LABEL, account.getLabel()); 79 assertEquals(Arrays.asList("tel"), account.getSupportedUriSchemes()); 80 assertEquals(phoneIcon.toString(), account.getIcon().toString()); 81 assertEquals(0, account.describeContents()); 82 83 // Create a parcel of the object and recreate the object back 84 // from the parcel. 85 Parcel p = Parcel.obtain(); 86 account.writeToParcel(p, 0); 87 p.setDataPosition(0); 88 PhoneAccount parcelAccount = PhoneAccount.CREATOR.createFromParcel(p); 89 assertNotNull(parcelAccount); 90 assertEquals(accountHandle, parcelAccount.getAccountHandle()); 91 assertEquals(tel, parcelAccount.getAddress()); 92 assertEquals(tel, parcelAccount.getSubscriptionAddress()); 93 assertEquals(PhoneAccount.CAPABILITY_CALL_PROVIDER, parcelAccount.getCapabilities()); 94 assertEquals(Color.RED, parcelAccount.getHighlightColor()); 95 assertEquals(ACCOUNT_LABEL, parcelAccount.getShortDescription()); 96 assertEquals(Arrays.asList("tel"), parcelAccount.getSupportedUriSchemes()); 97 assertEquals(phoneIcon.toString(), parcelAccount.getIcon().toString()); 98 assertEquals(0, parcelAccount.describeContents()); 99 p.recycle(); 100 } 101 102 public void testPhoneAccountHandle() throws Exception { 103 final ComponentName component = new ComponentName(PACKAGE, COMPONENT); 104 final UserHandle userHandle = Process.myUserHandle(); 105 PhoneAccountHandle accountHandle = new PhoneAccountHandle( 106 component, 107 ACCOUNT_ID, 108 userHandle); 109 assertNotNull(accountHandle); 110 assertEquals(component, accountHandle.getComponentName()); 111 assertEquals(ACCOUNT_ID, accountHandle.getId()); 112 assertEquals(userHandle, accountHandle.getUserHandle()); 113 assertEquals(0, accountHandle.describeContents()); 114 115 // Create a parcel of the object and recreate the object back 116 // from the parcel. 117 Parcel p = Parcel.obtain(); 118 accountHandle.writeToParcel(p, 0); 119 p.setDataPosition(0); 120 PhoneAccountHandle unparcelled = PhoneAccountHandle.CREATOR.createFromParcel(p); 121 assertEquals(accountHandle, unparcelled); 122 assertEquals(accountHandle.getComponentName(), unparcelled.getComponentName()); 123 assertEquals(accountHandle.getId(), unparcelled.getId()); 124 assertEquals(accountHandle.getUserHandle(), unparcelled.getUserHandle()); 125 p.recycle(); 126 } 127 128 public void testConnectionRequest() throws Exception { 129 PhoneAccountHandle accountHandle = new PhoneAccountHandle( 130 new ComponentName(PACKAGE, COMPONENT), 131 ACCOUNT_ID); 132 Bundle extras = new Bundle(); 133 extras.putString( 134 TelecomManager.GATEWAY_PROVIDER_PACKAGE, 135 PACKAGE); 136 ConnectionRequest request = new ConnectionRequest( 137 accountHandle, 138 Uri.parse("tel:555-TEST"), 139 extras, 140 VideoProfile.STATE_AUDIO_ONLY); 141 assertEquals(accountHandle, request.getAccountHandle()); 142 assertEquals(Uri.parse("tel:555-TEST"), request.getAddress()); 143 assertEquals(extras.getString( 144 TelecomManager.GATEWAY_PROVIDER_PACKAGE), 145 request.getExtras().getString(TelecomManager.GATEWAY_PROVIDER_PACKAGE)); 146 assertEquals(VideoProfile.STATE_AUDIO_ONLY, request.getVideoState()); 147 assertEquals(0, request.describeContents()); 148 149 // Create a parcel of the object and recreate the object back 150 // from the parcel. 151 Parcel p = Parcel.obtain(); 152 request.writeToParcel(p, 0); 153 p.setDataPosition(0); 154 ConnectionRequest parcelRequest = ConnectionRequest.CREATOR.createFromParcel(p); 155 assertEquals(accountHandle, parcelRequest.getAccountHandle()); 156 assertEquals(Uri.parse("tel:555-TEST"), parcelRequest.getAddress()); 157 assertEquals( 158 extras.getString(TelecomManager.GATEWAY_PROVIDER_PACKAGE), 159 parcelRequest.getExtras().getString(TelecomManager.GATEWAY_PROVIDER_PACKAGE)); 160 assertEquals(VideoProfile.STATE_AUDIO_ONLY, parcelRequest.getVideoState()); 161 assertEquals(0, parcelRequest.describeContents()); 162 p.recycle(); 163 } 164 165 public void testDisconnectCause() throws Exception { 166 final CharSequence label = "Out of service area"; 167 final CharSequence description = "Mobile network not available"; 168 final String reason = "CTS Testing"; 169 DisconnectCause cause = new DisconnectCause( 170 DisconnectCause.ERROR, 171 label, 172 description, 173 reason, 174 ToneGenerator.TONE_CDMA_CALLDROP_LITE); 175 assertEquals(DisconnectCause.ERROR, cause.getCode()); 176 assertEquals(label, cause.getLabel()); 177 assertEquals(description, cause.getDescription()); 178 assertEquals(reason, cause.getReason()); 179 assertEquals(ToneGenerator.TONE_CDMA_CALLDROP_LITE, cause.getTone()); 180 assertEquals(0, cause.describeContents()); 181 182 // Create a parcel of the object and recreate the object back 183 // from the parcel. 184 Parcel p = Parcel.obtain(); 185 cause.writeToParcel(p, 0); 186 p.setDataPosition(0); 187 DisconnectCause parcelCause = DisconnectCause.CREATOR.createFromParcel(p); 188 assertEquals(DisconnectCause.ERROR, parcelCause.getCode()); 189 assertEquals(label, parcelCause.getLabel()); 190 assertEquals(description, parcelCause.getDescription()); 191 assertEquals(reason, parcelCause.getReason()); 192 assertEquals(ToneGenerator.TONE_CDMA_CALLDROP_LITE, parcelCause.getTone()); 193 assertEquals(0, parcelCause.describeContents()); 194 assertEquals(cause, parcelCause); 195 p.recycle(); 196 } 197 198 public void testStatusHints() throws Exception { 199 Context context = getInstrumentation().getContext(); 200 final CharSequence label = "Wi-Fi call"; 201 Bundle extras = new Bundle(); 202 extras.putString( 203 TelecomManager.GATEWAY_PROVIDER_PACKAGE, 204 PACKAGE); 205 Icon icon = Icon.createWithResource(context, R.drawable.ic_phone_24dp); 206 StatusHints hints = new StatusHints( 207 label, 208 icon, 209 extras); 210 assertEquals(label, hints.getLabel()); 211 assertEquals(icon.toString(), hints.getIcon().toString()); 212 assertEquals(extras.getString( 213 TelecomManager.GATEWAY_PROVIDER_PACKAGE), 214 hints.getExtras().getString(TelecomManager.GATEWAY_PROVIDER_PACKAGE)); 215 assertEquals(0, hints.describeContents()); 216 217 // Create a parcel of the object and recreate the object back 218 // from the parcel. 219 Parcel p = Parcel.obtain(); 220 hints.writeToParcel(p, 0); 221 p.setDataPosition(0); 222 StatusHints parcelHints = StatusHints.CREATOR.createFromParcel(p); 223 assertEquals(label, parcelHints.getLabel()); 224 assertEquals(icon.toString(), parcelHints.getIcon().toString()); 225 assertEquals( 226 extras.getString(TelecomManager.GATEWAY_PROVIDER_PACKAGE), 227 parcelHints.getExtras().getString(TelecomManager.GATEWAY_PROVIDER_PACKAGE)); 228 assertEquals(0, parcelHints.describeContents()); 229 // This fails because Bundle does not have a equals implementation. 230 // assertEquals(hints, parcelHints); 231 p.recycle(); 232 } 233 234 public void testGatewayInfo() throws Exception { 235 final CharSequence label = "Wi-Fi call"; 236 Uri originalAddress = Uri.parse("http://www.google.com"); 237 Uri gatewayAddress = Uri.parse("http://www.google.com"); 238 GatewayInfo info = new GatewayInfo( 239 PACKAGE, 240 gatewayAddress, 241 originalAddress); 242 assertEquals(PACKAGE, info.getGatewayProviderPackageName()); 243 assertEquals(gatewayAddress, info.getGatewayAddress()); 244 assertEquals(originalAddress, info.getOriginalAddress()); 245 assertEquals(0, info.describeContents()); 246 assertFalse(info.isEmpty()); 247 248 // Create a parcel of the object and recreate the object back 249 // from the parcel. 250 Parcel p = Parcel.obtain(); 251 info.writeToParcel(p, 0); 252 p.setDataPosition(0); 253 GatewayInfo parcelInfo = GatewayInfo.CREATOR.createFromParcel(p); 254 assertEquals(PACKAGE, parcelInfo.getGatewayProviderPackageName()); 255 assertEquals(gatewayAddress, parcelInfo.getGatewayAddress()); 256 assertEquals(originalAddress, parcelInfo.getOriginalAddress()); 257 assertEquals(0, parcelInfo.describeContents()); 258 p.recycle(); 259 } 260 261 public void testCallAudioState() throws Exception { 262 CallAudioState audioState = new CallAudioState( 263 true, 264 CallAudioState.ROUTE_EARPIECE, 265 CallAudioState.ROUTE_WIRED_OR_EARPIECE); 266 assertEquals(true, audioState.isMuted()); 267 assertEquals(CallAudioState.ROUTE_EARPIECE, audioState.getRoute()); 268 assertEquals(CallAudioState.ROUTE_WIRED_OR_EARPIECE, audioState.getSupportedRouteMask()); 269 assertEquals(0, audioState.describeContents()); 270 assertEquals("EARPIECE", CallAudioState.audioRouteToString(audioState.getRoute())); 271 272 // Create a parcel of the object and recreate the object back 273 // from the parcel. 274 Parcel p = Parcel.obtain(); 275 audioState.writeToParcel(p, 0); 276 p.setDataPosition(0); 277 CallAudioState parcelAudioState = CallAudioState.CREATOR.createFromParcel(p); 278 assertEquals(true, parcelAudioState.isMuted()); 279 assertEquals(CallAudioState.ROUTE_EARPIECE, parcelAudioState.getRoute()); 280 assertEquals(CallAudioState.ROUTE_WIRED_OR_EARPIECE, parcelAudioState.getSupportedRouteMask()); 281 assertEquals(0, parcelAudioState.describeContents()); 282 assertEquals(audioState, parcelAudioState); 283 p.recycle(); 284 } 285 286 public void testVideoProfile() throws Exception { 287 VideoProfile videoProfile = new VideoProfile(VideoProfile.STATE_BIDIRECTIONAL, 288 VideoProfile.QUALITY_HIGH); 289 assertEquals(VideoProfile.STATE_BIDIRECTIONAL, videoProfile.getVideoState()); 290 assertEquals(VideoProfile.QUALITY_HIGH, videoProfile.getQuality()); 291 assertEquals(0, videoProfile.describeContents()); 292 assertEquals("Audio Tx Rx", VideoProfile.videoStateToString(videoProfile.getVideoState())); 293 294 // Create a parcel of the object and recreate the object back from the parcel. 295 Parcel p = Parcel.obtain(); 296 videoProfile.writeToParcel(p, 0); 297 p.setDataPosition(0); 298 VideoProfile unparcelled = VideoProfile.CREATOR.createFromParcel(p); 299 assertEquals(videoProfile.getQuality(), unparcelled.getQuality()); 300 assertEquals(videoProfile.getVideoState(), unparcelled.getVideoState()); 301 p.recycle(); 302 } 303 304 public void testCameraCapabilities() throws Exception { 305 VideoProfile.CameraCapabilities cameraCapabilities = 306 new VideoProfile.CameraCapabilities(500, 1000); 307 assertEquals(500, cameraCapabilities.getWidth()); 308 assertEquals(1000, cameraCapabilities.getHeight()); 309 assertEquals(0, cameraCapabilities.describeContents()); 310 311 // Create a parcel of the object and recreate the object back from the parcel. 312 Parcel p = Parcel.obtain(); 313 cameraCapabilities.writeToParcel(p, 0); 314 p.setDataPosition(0); 315 VideoProfile.CameraCapabilities unparcelled = 316 VideoProfile.CameraCapabilities.CREATOR.createFromParcel(p); 317 assertEquals(cameraCapabilities.getWidth(), unparcelled.getWidth()); 318 assertEquals(cameraCapabilities.getHeight(), unparcelled.getHeight()); 319 p.recycle(); 320 } 321 } 322