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.telecom.cts.apps; 18 19 import static android.telecom.Call.STATE_ACTIVE; 20 import static android.telecom.Call.STATE_AUDIO_PROCESSING; 21 import static android.telecom.Call.STATE_CONNECTING; 22 import static android.telecom.Call.STATE_DIALING; 23 import static android.telecom.Call.STATE_DISCONNECTED; 24 import static android.telecom.Call.STATE_DISCONNECTING; 25 import static android.telecom.Call.STATE_HOLDING; 26 import static android.telecom.Call.STATE_NEW; 27 import static android.telecom.Call.STATE_RINGING; 28 import static android.telecom.Call.STATE_SELECT_PHONE_ACCOUNT; 29 import static android.telecom.Call.STATE_SIMULATED_RINGING; 30 31 import static org.junit.Assert.fail; 32 33 import android.telecom.Call; 34 import android.util.Log; 35 36 import java.util.List; 37 38 public class WaitForInCallService { 39 private static final String DID_NOT_BIND_IN_TIME_ERR_MSG = 40 "InCallServiceVerifier did NOT bind in time"; 41 private static final String CALL_COUNT_WAS_NOT_INCREMENTED_ERR_MSG = 42 "Call Count was not incremented in time"; 43 verifyCallState(InCallServiceMethods verifierMethods, String id, int targetCallState)44 public static void verifyCallState(InCallServiceMethods verifierMethods, 45 String id, int targetCallState) { 46 List<Call> mCalls = verifierMethods.getOngoingCalls(); 47 Call targetCall = getCallWithId(mCalls, id); 48 boolean containsCall = targetCall != null; 49 50 if ((targetCallState == STATE_DISCONNECTED 51 || targetCallState == STATE_DISCONNECTING) && !containsCall) { 52 return; 53 } 54 if (!containsCall) { 55 fail("call is not in map"); 56 } 57 assertCallState(targetCall, targetCallState); 58 } 59 60 waitForInCallServiceBinding(InCallServiceMethods verifierMethods)61 public static void waitForInCallServiceBinding(InCallServiceMethods verifierMethods) { 62 WaitUntil.waitUntilConditionIsTrueOrTimeout( 63 new Condition() { 64 @Override 65 public Object expected() { 66 return true; 67 } 68 69 @Override 70 public Object actual() { 71 return verifierMethods.isBound(); 72 } 73 }, 74 WaitUntil.DEFAULT_TIMEOUT_MS, 75 DID_NOT_BIND_IN_TIME_ERR_MSG 76 ); 77 } 78 waitUntilExpectCallCount(InCallServiceMethods verifierMethods, int expectedCallCount)79 public static void waitUntilExpectCallCount(InCallServiceMethods verifierMethods, 80 int expectedCallCount) { 81 WaitUntil.waitUntilConditionIsTrueOrTimeout( 82 new Condition() { 83 @Override 84 public Object expected() { 85 return expectedCallCount; 86 } 87 88 @Override 89 public Object actual() { 90 return verifierMethods.getCurrentCallCount(); 91 } 92 }, 93 WaitUntil.DEFAULT_TIMEOUT_MS, 94 CALL_COUNT_WAS_NOT_INCREMENTED_ERR_MSG 95 ); 96 } 97 98 /*********************************************************** 99 / private methods 100 /***********************************************************/ getCallWithId(List<Call> calls, String id)101 private static Call getCallWithId(List<Call> calls, String id) { 102 for (Call call : calls) { 103 if (call.getDetails().getId().equals(id)) { 104 return call; 105 } 106 } 107 return null; 108 } 109 assertCallState(final Call call, final int targetState)110 private static void assertCallState(final Call call, final int targetState) { 111 WaitUntil.waitUntilConditionIsTrueOrTimeout( 112 new Condition() { 113 @Override 114 public Object expected() { 115 return targetState; 116 } 117 118 @Override 119 public Object actual() { 120 Log.i("tomsDebug", String.format("checking call=[%s]", call)); 121 return call.getState(); 122 } 123 }, WaitUntil.DEFAULT_TIMEOUT_MS, 124 "Expected CallState=[" + stateToString(targetState) + "];" 125 + " actual CallState[" + stateToString(call.getState()) + "]" 126 ); 127 } 128 stateToString(int state)129 private static String stateToString(int state) { 130 switch (state) { 131 case STATE_NEW: 132 return "NEW"; 133 case STATE_RINGING: 134 return "RINGING"; 135 case STATE_DIALING: 136 return "DIALING"; 137 case STATE_ACTIVE: 138 return "ACTIVE"; 139 case STATE_HOLDING: 140 return "HOLDING"; 141 case STATE_DISCONNECTED: 142 return "DISCONNECTED"; 143 case STATE_CONNECTING: 144 return "CONNECTING"; 145 case STATE_DISCONNECTING: 146 return "DISCONNECTING"; 147 case STATE_SELECT_PHONE_ACCOUNT: 148 return "SELECT_PHONE_ACCOUNT"; 149 case STATE_SIMULATED_RINGING: 150 return "SIMULATED_RINGING"; 151 case STATE_AUDIO_PROCESSING: 152 return "AUDIO_PROCESSING"; 153 default: 154 Log.i("tomsLog", String.format("Unknown state %d", state)); 155 return "UNKNOWN"; 156 } 157 } 158 } 159