
org.robolectric.shadows.ShadowActivityThread Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shadows-framework Show documentation
Show all versions of shadows-framework Show documentation
An alternative Android testing framework.
package org.robolectric.shadows;
import android.app.ActivityThread;
import android.app.Application;
import android.app.Instrumentation;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import javax.annotation.Nonnull;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.util.reflector.Accessor;
import org.robolectric.util.reflector.ForType;
@Implements(value = ActivityThread.class, isInAndroidSdk = false, looseSignatures = true)
public class ShadowActivityThread {
private static ApplicationInfo applicationInfo;
@Implementation
public static Object getPackageManager() {
ClassLoader classLoader = ShadowActivityThread.class.getClassLoader();
Class> iPackageManagerClass;
try {
iPackageManagerClass = classLoader.loadClass("android.content.pm.IPackageManager");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
return Proxy.newProxyInstance(
classLoader,
new Class[] {iPackageManagerClass},
new InvocationHandler() {
@Override
public Object invoke(Object proxy, @Nonnull Method method, Object[] args)
throws Exception {
if (method.getName().equals("getApplicationInfo")) {
String packageName = (String) args[0];
int flags = (Integer) args[1];
if (packageName.equals(ShadowActivityThread.applicationInfo.packageName)) {
return ShadowActivityThread.applicationInfo;
}
try {
return RuntimeEnvironment.application
.getPackageManager()
.getApplicationInfo(packageName, flags);
} catch (PackageManager.NameNotFoundException e) {
return null;
}
} else if (method.getName().equals("notifyPackageUse")) {
return null;
} else if (method.getName().equals("getPackageInstaller")) {
return null;
}
throw new UnsupportedOperationException("sorry, not supporting " + method + " yet!");
}
});
}
@Implementation
public static Object currentActivityThread() {
return RuntimeEnvironment.getActivityThread();
}
/**
* Internal use only.
*
* @deprecated do not use
*/
@Deprecated
public static void setApplicationInfo(ApplicationInfo applicationInfo) {
ShadowActivityThread.applicationInfo = applicationInfo;
}
/** Accessor interface for {@link ActivityThread}'s internals. */
@ForType(ActivityThread.class)
public interface _ActivityThread_ {
@Accessor("mBoundApplication")
void setBoundApplication(Object data);
@Accessor("mCompatConfiguration")
void setCompatConfiguration(Configuration configuration);
@Accessor("mInitialApplication")
void setInitialApplication(Application application);
@Accessor("mInstrumentation")
void setInstrumentation(Instrumentation instrumentation);
}
/** Accessor interface for {@link ActivityThread.AppBindData}'s internals. */
@ForType(className = "android.app.ActivityThread$AppBindData")
public interface _AppBindData_ {
@Accessor("appInfo")
void setAppInfo(ApplicationInfo applicationInfo);
@Accessor("processName")
void setProcessName(String name);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy