Skip to content

Commit

Permalink
Merge branch 'google'
Browse files Browse the repository at this point in the history
  • Loading branch information
hoisie committed Mar 25, 2022
2 parents d463f2c + 93881e2 commit 3fd918c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Those software configurations are recommended and tested.
See [Building Robolectric](http://robolectric.org/building-robolectric/) for more details about setting up a build environment for Robolectric.

### Building

Robolectric supports running tests against multiple Android API levels. The work it must do to support each API level is slightly different, so its shadows are built separately for each. To build shadows for every API version, run:

./gradlew clean assemble testClasses --parallel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ public void onCellInfo(List<CellInfo> list) {
}

@Test
public void shouldGiveNetworkCountryIso() {
public void shouldGiveNetworkCountryIsoInLowercase() {
shadowOf(telephonyManager).setNetworkCountryIso("SomeIso");
assertEquals("SomeIso", telephonyManager.getNetworkCountryIso());
assertEquals("someiso", telephonyManager.getNetworkCountryIso());
}

@Test
Expand Down Expand Up @@ -506,7 +506,7 @@ public void shouldGetSimIso() {

@Test
@Config(minSdk = N, maxSdk = Q)
public void shouldGetSimIosWhenSetUsingSlotNumber() {
public void shouldGetSimIsoWhenSetUsingSlotNumber() {
String expectedSimIso = "usa";
int subId = 2;
shadowOf(telephonyManager).setSimCountryIso(subId, expectedSimIso);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.view.View.DragShadowBuilder;
import androidx.annotation.Nullable;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
Expand All @@ -22,6 +23,11 @@
@RunWith(AndroidJUnit4.class)
public class ShadowWindowManagerGlobalTest {

@Before
public void setup() {
System.setProperty("robolectric.areWindowsMarkedVisible", "true");
}

@Test
@Config(minSdk = JELLY_BEAN_MR1)
public void getWindowSession_shouldReturnSession() {
Expand All @@ -47,6 +53,15 @@ public void getLastDragClipData() {
assertThat(ShadowWindowManagerGlobal.getLastDragClipData()).isNotNull();
}

@Test
@Config(minSdk = JELLY_BEAN_MR1)
public void windowIsVisible() {
View decorView =
Robolectric.buildActivity(DragActivity.class).setup().get().getWindow().getDecorView();

assertThat(decorView.getWindowVisibility()).isEqualTo(View.VISIBLE);
}

static final class DragActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle bundle) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import android.text.TextUtils;
import android.util.SparseArray;
import android.util.SparseIntArray;
import com.google.common.base.Ascii;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -210,6 +211,7 @@ public int getEventFlags() {
/** Call state may be specified via {@link #setCallState(int)}. */
@Implementation
protected int getCallState() {
checkReadPhoneStatePermission();
return callState;
}

Expand Down Expand Up @@ -318,9 +320,13 @@ public void setNetworkCountryIso(String networkCountryIso) {
this.networkCountryIso = networkCountryIso;
}

/**
* Returns the SIM country lowercase. This matches the API this shadows:
* https://developer.android.com/reference/android/telephony/TelephonyManager#getNetworkCountryIso().
*/
@Implementation
protected String getNetworkCountryIso() {
return networkCountryIso;
return networkCountryIso == null ? null : Ascii.toLowerCase(networkCountryIso);
}

/** Sets the sim locale returned by {@link #getSimLocale()}. */
Expand Down Expand Up @@ -373,9 +379,14 @@ public void setSimSerialNumber(String simSerialNumber) {
this.simSerialNumber = simSerialNumber;
}

/**
* Returns the SIM country lowercase. This matches the API it shadows:
* https://developer.android.com/reference/android/telephony/TelephonyManager#getSimCountryIso().
*/
@Implementation
protected String getSimCountryIso() {
return simCountryIsoMap.get(/* subId= */ 0);
String simCountryIso = simCountryIsoMap.get(/* subId= */ 0);
return simCountryIso == null ? simCountryIso : Ascii.toLowerCase(simCountryIso);
}

@Implementation(minSdk = N, maxSdk = Q)
Expand Down Expand Up @@ -461,6 +472,7 @@ public void setPhoneType(int phoneType) {

@Implementation
protected String getLine1Number() {
checkReadPhoneStatePermission();
return line1Number;
}

Expand All @@ -470,6 +482,7 @@ public void setLine1Number(String line1Number) {

@Implementation
protected int getNetworkType() {
checkReadPhoneStatePermission();
return networkType;
}

Expand All @@ -489,6 +502,7 @@ public void setNetworkType(int networkType) {
*/
@Implementation(minSdk = N)
protected int getDataNetworkType() {
checkReadPhoneStatePermission();
return dataNetworkType;
}

Expand Down Expand Up @@ -601,6 +615,7 @@ public void setCellLocation(CellLocation cellLocation) {

@Implementation(minSdk = JELLY_BEAN_MR2)
protected String getGroupIdLevel1() {
checkReadPhoneStatePermission();
return this.groupIdLevel1;
}

Expand Down Expand Up @@ -644,7 +659,9 @@ public boolean apply(PhoneStateListener input) {
});
}

/** @return true by default, or the value specified via {@link #setIsSmsCapable(boolean)} */
/**
* @return true by default, or the value specified via {@link #setIsSmsCapable(boolean)}
*/
@Implementation
protected boolean isSmsCapable() {
return isSmsCapable;
Expand All @@ -661,6 +678,7 @@ public void setIsSmsCapable(boolean isSmsCapable) {
*/
@Implementation(minSdk = O)
protected PersistableBundle getCarrierConfig() {
checkReadPhoneStatePermission();
return carrierConfig != null ? carrierConfig : new PersistableBundle();
}

Expand All @@ -679,6 +697,7 @@ public void setCarrierConfig(PersistableBundle carrierConfig) {
*/
@Implementation
protected String getVoiceMailNumber() {
checkReadPhoneStatePermission();
return voiceMailNumber;
}

Expand All @@ -693,6 +712,7 @@ public void setVoiceMailNumber(String voiceMailNumber) {
*/
@Implementation
protected String getVoiceMailAlphaTag() {
checkReadPhoneStatePermission();
return voiceMailAlphaTag;
}

Expand Down Expand Up @@ -814,6 +834,7 @@ public void setTelephonyManagerForSubscriptionId(
*/
@Implementation(minSdk = O)
protected ServiceState getServiceState() {
checkReadPhoneStatePermission();
return serviceState;
}

Expand Down Expand Up @@ -907,6 +928,7 @@ public void setSubscriberId(String subscriberId) {
/** Returns the value set by {@link #setVisualVoicemailPackageName(String)}. */
@Implementation(minSdk = O)
protected String getVisualVoicemailPackageName() {
checkReadPhoneStatePermission();
return visualVoicemailPackageName;
}

Expand Down Expand Up @@ -1007,6 +1029,7 @@ protected boolean isPotentialEmergencyNumber(String number) {
*/
@Implementation(minSdk = Build.VERSION_CODES.O)
protected boolean isDataEnabled() {
checkReadPhoneStatePermission();
return dataEnabled;
}

Expand Down Expand Up @@ -1100,6 +1123,7 @@ public void setTelephonyDisplayInfo(Object telephonyDisplayInfo) {
@Implementation(minSdk = R)
@HiddenApi
protected boolean isDataConnectionAllowed() {
checkReadPhoneStatePermission();
return isDataConnectionAllowed;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static android.os.Build.VERSION_CODES.Q;
import static android.os.Build.VERSION_CODES.R;
import static android.os.Build.VERSION_CODES.S;
import static android.os.Build.VERSION_CODES.S_V2;
import static org.robolectric.util.reflector.Reflector.reflector;

import android.app.Instrumentation;
Expand All @@ -27,6 +28,7 @@
import android.view.InputChannel;
import android.view.InsetsSourceControl;
import android.view.InsetsState;
import android.view.InsetsVisibilities;
import android.view.Surface;
import android.view.SurfaceControl;
import android.view.View;
Expand Down Expand Up @@ -58,7 +60,9 @@ public static void reset() {
private static synchronized WindowSessionDelegate getWindowSessionDelegate() {
if (windowSessionDelegate == null) {
int apiLevel = RuntimeEnvironment.getApiLevel();
if (apiLevel >= S) {
if (apiLevel >= S_V2) {
windowSessionDelegate = new WindowSessionDelegateSV2();
} else if (apiLevel >= S) {
windowSessionDelegate = new WindowSessionDelegateS();
} else if (apiLevel >= R) {
windowSessionDelegate = new WindowSessionDelegateR();
Expand Down Expand Up @@ -162,12 +166,20 @@ interface WindowManagerGlobalReflector {
private abstract static class WindowSessionDelegate {
// From WindowManagerGlobal (was WindowManagerImpl in JB).
static final int ADD_FLAG_IN_TOUCH_MODE = 0x1;
static final int ADD_FLAG_APP_VISIBLE = 0x2;

private boolean inTouchMode;
@Nullable protected ClipData lastDragClipData;

protected int getAddFlags() {
int res = 0;
// Temporarily enable this based on a system property to allow for test migration. This will
// eventually be updated to default and true and eventually removed, Robolectric's previous
// behavior of not marking windows as visible by default is a bug. This flag should only be
// used as a temporary toggle during migration.
if ("true".equals(System.getProperty("robolectric.areWindowsMarkedVisible", "false"))) {
res |= ADD_FLAG_APP_VISIBLE;
}
if (getInTouchMode()) {
res |= ADD_FLAG_IN_TOUCH_MODE;
}
Expand Down Expand Up @@ -348,7 +360,7 @@ public int addToDisplayAsUser(
}

private static class WindowSessionDelegateS extends WindowSessionDelegateR {
// @Implementation(minSdk = S)
// @Implementation(sdk = S)
public int addToDisplayAsUser(
IWindow window,
WindowManager.LayoutParams attrs,
Expand All @@ -362,4 +374,20 @@ public int addToDisplayAsUser(
return getAddFlags();
}
}

private static class WindowSessionDelegateSV2 extends WindowSessionDelegateS {
// @Implementation(minSdk = S_V2)
public int addToDisplayAsUser(
IWindow window,
WindowManager.LayoutParams attrs,
int viewVisibility,
int displayId,
int userId,
InsetsVisibilities requestedVisibilities,
InputChannel outInputChannel,
InsetsState outInsetsState,
InsetsSourceControl[] outActiveControls) {
return getAddFlags();
}
}
}

0 comments on commit 3fd918c

Please sign in to comment.