Skip to content

Commit

Permalink
Add onConnectionEvent() to ShadowInCallService.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 410879168
  • Loading branch information
Googler authored and copybara-robolectric committed Nov 19, 2021
1 parent 0cb21aa commit 89d194d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
Expand Up @@ -8,6 +8,7 @@
import static android.os.Build.VERSION_CODES.N;
import static android.os.Build.VERSION_CODES.Q;
import static org.robolectric.RuntimeEnvironment.getApiLevel;
import static org.robolectric.util.reflector.Reflector.reflector;

import android.graphics.Rect;
import android.os.Bundle;
Expand Down Expand Up @@ -36,7 +37,9 @@
import org.robolectric.annotation.Resetter;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.ReflectionHelpers.ClassParameter;
import org.robolectric.util.reflector.Accessor;
import org.robolectric.util.reflector.ForType;
import org.robolectric.util.reflector.Static;

/**
* Properties of {@link android.view.accessibility.AccessibilityNodeInfo} that are normally locked
Expand Down Expand Up @@ -191,8 +194,7 @@ public AccessibilityNodeInfo[] newArray(int size) {

@Implementation
protected void __constructor__() {
ReflectionHelpers.setStaticField(
AccessibilityNodeInfo.class, "CREATOR", ShadowAccessibilityNodeInfo.CREATOR);
reflector(AccessibilityNodeInfoReflector.class).setCreator(ShadowAccessibilityNodeInfo.CREATOR);
}

@Implementation
Expand Down Expand Up @@ -1223,11 +1225,7 @@ public static final class ShadowAccessibilityAction {

@Implementation
protected void __constructor__(int id, CharSequence label) {
if (((id
& (int)
ReflectionHelpers.getStaticField(
AccessibilityNodeInfo.class, "ACTION_TYPE_MASK"))
== 0)
if (((id & reflector(AccessibilityNodeInfoReflector.class).getActionTypeMask()) == 0)
&& Integer.bitCount(id) != 1) {
throw new IllegalArgumentException("Invalid standard action id");
}
Expand Down Expand Up @@ -1266,8 +1264,8 @@ public boolean equals(Object other) {

@Override
public String toString() {
String actionSybolicName = ReflectionHelpers.callStaticMethod(
AccessibilityNodeInfo.class, "getActionSymbolicName", ClassParameter.from(int.class, id));
String actionSybolicName =
reflector(AccessibilityNodeInfoReflector.class).getActionSymbolicName(id);
return "AccessibilityAction: " + actionSybolicName + " - " + label;
}
}
Expand All @@ -1292,19 +1290,16 @@ protected void writeToParcel(Parcel dest, int flags) {

private static int getActionTypeMaskFromFramework() {
// Get the mask to determine whether an int is a legit ID for an action, defined by Android
return (int)ReflectionHelpers.getStaticField(AccessibilityNodeInfo.class, "ACTION_TYPE_MASK");
return reflector(AccessibilityNodeInfoReflector.class).getActionTypeMask();
}

private static AccessibilityAction getActionFromIdFromFrameWork(int id) {
// Convert an action ID to Android standard Accessibility Action defined by Android
return ReflectionHelpers.callStaticMethod(
AccessibilityNodeInfo.class, "getActionSingleton", ClassParameter.from(int.class, id));
return reflector(AccessibilityNodeInfoReflector.class).getActionSingleton(id);
}

private static int getLastLegacyActionFromFrameWork() {
return (int)
ReflectionHelpers.getStaticField(
AccessibilityNodeInfo.class, "LAST_LEGACY_STANDARD_ACTION");
return reflector(AccessibilityNodeInfoReflector.class).getLastLegacyStandardAction();
}

/**
Expand All @@ -1331,4 +1326,25 @@ public String toString() {
+ className
+ "}";
}

@ForType(AccessibilityNodeInfo.class)
interface AccessibilityNodeInfoReflector {
@Static
@Accessor("CREATOR")
void setCreator(Parcelable.Creator<AccessibilityNodeInfo> creator);

@Static
@Accessor("ACTION_TYPE_MASK")
int getActionTypeMask();

@Static
@Accessor("LAST_LEGACY_STANDARD_ACTION")
int getLastLegacyStandardAction();

@Static
String getActionSymbolicName(int id);

@Static
AccessibilityAction getActionSingleton(int id);
}
}
Expand Up @@ -9,6 +9,7 @@
import android.annotation.TargetApi;
import android.bluetooth.BluetoothDevice;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.os.Handler;
import android.telecom.Call;
import android.telecom.CallAudioState;
Expand Down Expand Up @@ -36,6 +37,7 @@ public class ShadowInCallService extends ShadowService {
private static final int MSG_UPDATE_CALL = 3;
private static final int MSG_SET_POST_DIAL_WAIT = 4;
private static final int MSG_ON_CALL_AUDIO_STATE_CHANGED = 5;
private static final int MSG_ON_CONNECTION_EVENT = 9;

private ShadowPhone shadowPhone;
private boolean canAddCall;
Expand Down Expand Up @@ -83,7 +85,7 @@ public void addCall(ParcelableCall parcelableCall) {

/**
* Exposes {@link IIInCallService.Stub#setPostDialWait}. This is normally invoked by Telecom but
* in Robolectric Telecom doesn't exist, so tests can invoke this to simulate Telecom's actions.
* in Robolectric, Telecom doesn't exist, so tests can invoke this to simulate Telecom's actions.
*/
public void setPostDialWait(String callId, String remaining) {
SomeArgs args = SomeArgs.obtain();
Expand All @@ -92,6 +94,18 @@ public void setPostDialWait(String callId, String remaining) {
getHandler().obtainMessage(MSG_SET_POST_DIAL_WAIT, args).sendToTarget();
}

/**
* Exposes {@link IIInCallService.Stub#onConnectionEvent}. This is normally invoked by Telecom but
* in Robolectric, Telecom doesn't exist, so tests can invoke this to simulate Telecom's actions.
*/
public void onConnectionEvent(String callId, String event, Bundle extras) {
SomeArgs args = SomeArgs.obtain();
args.arg1 = callId;
args.arg2 = event;
args.arg3 = extras;
getHandler().obtainMessage(MSG_ON_CONNECTION_EVENT, args).sendToTarget();
}

public void removeCall(Call call) {
shadowPhone.removeCall(call);
}
Expand Down

0 comments on commit 89d194d

Please sign in to comment.