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