Skip to content

Commit

Permalink
Update Android code search link to the public version
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 409454414
  • Loading branch information
hoisie authored and copybara-robolectric committed Nov 12, 2021
1 parent d0d1696 commit efff57f
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 5 deletions.
Expand Up @@ -51,8 +51,8 @@ enum FileType {
kFileTypeSocket,
}


// transliterated from https://cs.corp.google.com/android/frameworks/base/libs/androidfw/include/androidfw/AssetManager.h
// transliterated from
// https://cs.android.com/android/platform/superproject/+/master:frameworks/base/libs/androidfw/include/androidfw/AssetManager.h
private static class asset_path {
// asset_path() : path(""), type(kFileTypeRegular), idmap(""),
// isSystemOverlay(false), isSystemAsset(false) {}
Expand Down
Expand Up @@ -2,6 +2,8 @@

import static android.os.Build.VERSION_CODES.M;
import static android.os.Build.VERSION_CODES.O;
import static android.os.Build.VERSION_CODES.R;
import static android.os.Build.VERSION_CODES.S;
import static com.google.common.truth.Truth.assertThat;

import android.os.Build;
Expand Down Expand Up @@ -56,6 +58,13 @@ public void setVersionIncremental() {
assertThat(VERSION.INCREMENTAL).isEqualTo("robo_incremental");
}

@Test
@Config(minSdk = S)
public void setVersionMediaPerformanceClass() {
ShadowBuild.setVersionMediaPerformanceClass(R);
assertThat(VERSION.MEDIA_PERFORMANCE_CLASS).isEqualTo(R);
}

@Test
@Config(minSdk = M)
public void setVersionSecurityPatch() {
Expand Down
Expand Up @@ -2,11 +2,18 @@

import static android.os.Build.VERSION_CODES.M;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
import static org.robolectric.Shadows.shadowOf;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import androidx.annotation.Nullable;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
Expand All @@ -16,16 +23,30 @@
@RunWith(AndroidJUnit4.class)
@Config(minSdk = M)
public class ShadowIconTest {

private static final int MSG_ICON_LOADED = 312;

public static final int TYPE_BITMAP = 1;
public static final int TYPE_RESOURCE = 2;
public static final int TYPE_DATA = 3;
public static final int TYPE_URI = 4;

@Nullable private Drawable loadedDrawable;

private final Context appContext = ApplicationProvider.getApplicationContext();
private final Handler mainHandler =
new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
if (msg.what == MSG_ICON_LOADED) {
loadedDrawable = (Drawable) msg.obj;
}
}
};

@Test
public void testGetRes() {
Icon icon =
Icon.createWithResource(
ApplicationProvider.getApplicationContext(), android.R.drawable.ic_delete);
Icon icon = Icon.createWithResource(appContext, android.R.drawable.ic_delete);
assertThat(shadowOf(icon).getType()).isEqualTo(TYPE_RESOURCE);
assertThat(shadowOf(icon).getResId()).isEqualTo(android.R.drawable.ic_delete);
}
Expand Down Expand Up @@ -55,4 +76,32 @@ public void testGetUri() {
assertThat(shadowOf(icon).getType()).isEqualTo(TYPE_URI);
assertThat(shadowOf(icon).getUri()).isEqualTo(uri);
}

@Test
public void testLoadDrawableAsyncWithMessage() {
ShadowIcon.overrideExecutor(directExecutor());

Icon icon = Icon.createWithResource(appContext, android.R.drawable.ic_delete);

Message andThen = Message.obtain(mainHandler, MSG_ICON_LOADED);

icon.loadDrawableAsync(appContext, andThen);
ShadowLooper.idleMainLooper();

assertThat(shadowOf(loadedDrawable).getCreatedFromResId())
.isEqualTo(android.R.drawable.ic_delete);
}

@Test
public void testLoadDrawableAsyncWithListener() {
ShadowIcon.overrideExecutor(directExecutor());

Icon icon = Icon.createWithResource(appContext, android.R.drawable.ic_delete);

icon.loadDrawableAsync(appContext, drawable -> this.loadedDrawable = drawable, mainHandler);
ShadowLooper.idleMainLooper();

assertThat(shadowOf(loadedDrawable).getCreatedFromResId())
.isEqualTo(android.R.drawable.ic_delete);
}
}
Expand Up @@ -3,6 +3,7 @@
import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static android.os.Build.VERSION_CODES.M;
import static android.os.Build.VERSION_CODES.O;
import static android.os.Build.VERSION_CODES.S;
import static org.robolectric.util.reflector.Reflector.reflector;

import android.annotation.TargetApi;
Expand Down Expand Up @@ -110,6 +111,18 @@ public static void setVersionIncremental(String versionIncremental) {
ReflectionHelpers.setStaticField(Build.VERSION.class, "INCREMENTAL", versionIncremental);
}

/**
* Sets the value of the {@link Build.VERSION#MEDIA_PERFORMANCE_CLASS} field. Available in Android
* S+.
*
* <p>It will be reset for the next test.
*/
@TargetApi(S)
public static void setVersionMediaPerformanceClass(int performanceClass) {
ReflectionHelpers.setStaticField(
Build.VERSION.class, "MEDIA_PERFORMANCE_CLASS", performanceClass);
}

/**
* Sets the value of the {@link Build.VERSION#RELEASE} field.
*
Expand Down
Expand Up @@ -3,9 +3,16 @@
import static android.os.Build.VERSION_CODES.M;
import static org.robolectric.util.reflector.Reflector.reflector;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Icon;
import android.graphics.drawable.Icon.OnDrawableLoadedListener;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import androidx.annotation.Nullable;
import java.util.concurrent.Executor;
import org.robolectric.annotation.HiddenApi;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
Expand All @@ -17,6 +24,13 @@
@Implements(value = Icon.class, minSdk = M)
public class ShadowIcon {

@Nullable private static Executor executorOverride;

/** Set the executor where async drawable loading will run. */
public static void overrideExecutor(Executor executor) {
executorOverride = executor;
}

@RealObject private Icon realIcon;

@HiddenApi
Expand Down Expand Up @@ -61,6 +75,33 @@ public byte[] getDataBytes() {
return reflector(IconReflector.class, realIcon).getDataBytes();
}

@Implementation
protected void loadDrawableAsync(Context context, Message andThen) {
if (executorOverride != null) {
executorOverride.execute(
() -> {
andThen.obj = realIcon.loadDrawable(context);
andThen.sendToTarget();
});
} else {
reflector(IconReflector.class, realIcon).loadDrawableAsync(context, andThen);
}
}

@Implementation
protected void loadDrawableAsync(
Context context, final OnDrawableLoadedListener listener, Handler handler) {
if (executorOverride != null) {
executorOverride.execute(
() -> {
Drawable result = realIcon.loadDrawable(context);
handler.post(() -> listener.onDrawableLoaded(result));
});
} else {
reflector(IconReflector.class, realIcon).loadDrawableAsync(context, listener, handler);
}
}

@ForType(Icon.class)
interface IconReflector {

Expand All @@ -84,5 +125,12 @@ interface IconReflector {

@Direct
byte[] getDataBytes();

@Direct
void loadDrawableAsync(Context context, Message andThen);

@Direct
void loadDrawableAsync(
Context context, final OnDrawableLoadedListener listener, Handler handler);
}
}

0 comments on commit efff57f

Please sign in to comment.