本文整理汇总了Java中org.robolectric.util.ReflectionHelpers.setStaticField方法的典型用法代码示例。如果您正苦于以下问题:Java ReflectionHelpers.setStaticField方法的具体用法?Java ReflectionHelpers.setStaticField怎么用?Java ReflectionHelpers.setStaticField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.robolectric.util.ReflectionHelpers
的用法示例。
在下文中一共展示了ReflectionHelpers.setStaticField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testPromptOptions_IconDrawable_TintList
import org.robolectric.util.ReflectionHelpers; //导入方法依赖的package包/类
@Test
public void testPromptOptions_IconDrawable_TintList()
{
final Drawable drawable = mock(Drawable.class);
final ColorStateList colourStateList = mock(ColorStateList.class);
final PromptOptions options = UnitTestUtils.createPromptOptions();
assertEquals(options, options.setIconDrawable(drawable));
assertEquals(drawable, options.getIconDrawable());
assertEquals(options, options.setIconDrawableTintList(colourStateList));
options.setPrimaryText("Primary Text");
options.setTarget(mock(View.class));
options.create();
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 16);
options.create();
assertEquals(options, options.setIconDrawableTintList(null));
}
示例2: shouldThrowOnNoSuchAlgorithmErrorWhenTryingToObtainRSAKeys
import org.robolectric.util.ReflectionHelpers; //导入方法依赖的package包/类
@Test
public void shouldThrowOnNoSuchAlgorithmErrorWhenTryingToObtainRSAKeys() throws Exception {
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 19);
exception.expect(KeyException.class);
exception.expectMessage("An error occurred while trying to obtain the RSA KeyPair Entry from the Android KeyStore.");
PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(false);
KeyPairGeneratorSpec spec = PowerMockito.mock(KeyPairGeneratorSpec.class);
KeyPairGeneratorSpec.Builder builder = newKeyPairGeneratorSpecBuilder(spec);
PowerMockito.whenNew(KeyPairGeneratorSpec.Builder.class).withAnyArguments().thenReturn(builder);
PowerMockito.mockStatic(KeyPairGenerator.class);
PowerMockito.when(KeyPairGenerator.getInstance(ALGORITHM_RSA, ANDROID_KEY_STORE))
.thenThrow(new NoSuchAlgorithmException());
cryptoUtil.getRSAKeyEntry();
}
示例3: shouldNotRequireAuthenticationIfAPI21AndLockScreenDisabled
import org.robolectric.util.ReflectionHelpers; //导入方法依赖的package包/类
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Test
@Config(constants = com.auth0.android.auth0.BuildConfig.class, sdk = 21, manifest = Config.NONE)
public void shouldNotRequireAuthenticationIfAPI21AndLockScreenDisabled() throws Exception {
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 21);
Activity activity = spy(Robolectric.buildActivity(Activity.class).create().start().resume().get());
//Set LockScreen as Disabled
KeyguardManager kService = mock(KeyguardManager.class);
when(activity.getSystemService(Context.KEYGUARD_SERVICE)).thenReturn(kService);
when(kService.isKeyguardSecure()).thenReturn(false);
when(kService.createConfirmDeviceCredentialIntent("title", "description")).thenReturn(null);
boolean willAskAuthentication = manager.requireAuthentication(activity, 123, "title", "description");
assertThat(willAskAuthentication, is(false));
}
示例4: shouldNotRequireAuthenticationIfAPI23AndLockScreenDisabled
import org.robolectric.util.ReflectionHelpers; //导入方法依赖的package包/类
@RequiresApi(api = Build.VERSION_CODES.M)
@Test
@Config(constants = com.auth0.android.auth0.BuildConfig.class, sdk = 23, manifest = Config.NONE)
public void shouldNotRequireAuthenticationIfAPI23AndLockScreenDisabled() throws Exception {
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 23);
Activity activity = spy(Robolectric.buildActivity(Activity.class).create().start().resume().get());
//Set LockScreen as Disabled
KeyguardManager kService = mock(KeyguardManager.class);
when(activity.getSystemService(Context.KEYGUARD_SERVICE)).thenReturn(kService);
when(kService.isDeviceSecure()).thenReturn(false);
when(kService.createConfirmDeviceCredentialIntent("title", "description")).thenReturn(null);
boolean willAskAuthentication = manager.requireAuthentication(activity, 123, "title", "description");
assertThat(willAskAuthentication, is(false));
}
示例5: shouldThrowOnNoSuchProviderErrorWhenTryingToObtainRSAKeys
import org.robolectric.util.ReflectionHelpers; //导入方法依赖的package包/类
@Test
public void shouldThrowOnNoSuchProviderErrorWhenTryingToObtainRSAKeys() throws Exception {
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 19);
exception.expect(KeyException.class);
exception.expectMessage("An error occurred while trying to obtain the RSA KeyPair Entry from the Android KeyStore.");
PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(false);
KeyPairGeneratorSpec spec = PowerMockito.mock(KeyPairGeneratorSpec.class);
KeyPairGeneratorSpec.Builder builder = newKeyPairGeneratorSpecBuilder(spec);
PowerMockito.whenNew(KeyPairGeneratorSpec.Builder.class).withAnyArguments().thenReturn(builder);
PowerMockito.mockStatic(KeyPairGenerator.class);
PowerMockito.when(KeyPairGenerator.getInstance(ALGORITHM_RSA, ANDROID_KEY_STORE))
.thenThrow(new NoSuchProviderException());
cryptoUtil.getRSAKeyEntry();
}
示例6: setup
import org.robolectric.util.ReflectionHelpers; //导入方法依赖的package包/类
@Before
public void setup() {
today = Calendar.getInstance();
now = new DayTime(60 * 60 * 10 - 1); // 1 second to 10:00
Context context = RuntimeEnvironment.application;
synchronized (ScheduleDatabase.class) {
db = Room
.inMemoryDatabaseBuilder(context, ScheduleDatabase.class)
.allowMainThreadQueries()
.build();
ReflectionHelpers.setStaticField(ScheduleDatabase.class, "instance", db);
}
ScheduleDatabase dd = ScheduleDatabase.get(context);
assertThat((Boolean) ReflectionHelpers.getField(dd, "mAllowMainThreadQueries")).isTrue();
// Even app would load it, we load again here to wait for result
ScheduleLoader.load(context, db);
}
示例7: startActivityTestForPriorJellyBean
import org.robolectric.util.ReflectionHelpers; //导入方法依赖的package包/类
@Test
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
public void startActivityTestForPriorJellyBean() {
ReflectionHelpers.setStaticField(Build.VERSION.class, SDK_INT, Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1);
VersionUtilities.startActivity(activityMock, intentMock, optionsMock);
verify(activityMock, times(1)).startActivity(intentMock);
verifyNoMoreInteractions(activityMock);
}
示例8: setBackgroundTestForPriorJellyBean
import org.robolectric.util.ReflectionHelpers; //导入方法依赖的package包/类
@Test
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
@Ignore("android.content.res.Resources$NotFoundException: Unable to find resource ID #0x0 in packages")
public void setBackgroundTestForPriorJellyBean() {
ReflectionHelpers.setStaticField(Build.VERSION.class, SDK_INT, Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1);
DeprecationUtilities.setBackground(viewMock, R.drawable.snackbar__design_snackbar_background);
verify(viewMock, times(1)).setBackgroundDrawable(any(Drawable.class));
verifyNoMoreInteractions(connectivityManagerMock);
}
示例9: beforeTest
import org.robolectric.util.ReflectionHelpers; //导入方法依赖的package包/类
protected void beforeTest(Sandbox sandbox, Spec spec) throws Throwable {
SdkEnvironment sdkEnvironment = (SdkEnvironment) sandbox;
RoboSpec roboSpec = (RoboSpec) spec;
roboSpec.parallelUniverseInterface = getHooksInterface(sdkEnvironment);
Class<TestLifecycle> cl = sdkEnvironment.bootstrappedClass(getTestLifecycleClass());
roboSpec.testLifecycle = ReflectionHelpers.newInstance(cl);
final Config config = roboSpec.config;
final AndroidManifest appManifest = roboSpec.getAppManifest();
roboSpec.parallelUniverseInterface.setSdkConfig((sdkEnvironment).getSdkConfig());
roboSpec.parallelUniverseInterface.resetStaticState(config);
SdkConfig sdkConfig = roboSpec.sdkConfig;
Class<?> androidBuildVersionClass = (sdkEnvironment).bootstrappedClass(Build.VERSION.class);
ReflectionHelpers.setStaticField(androidBuildVersionClass, "SDK_INT", sdkConfig.getApiLevel());
ReflectionHelpers.setStaticField(androidBuildVersionClass, "RELEASE", sdkConfig.getAndroidVersion());
ReflectionHelpers.setStaticField(androidBuildVersionClass, "CODENAME", sdkConfig.getAndroidCodeName());
PackageResourceTable systemResourceTable = sdkEnvironment.getSystemResourceTable(getJarResolver());
PackageResourceTable appResourceTable = getAppResourceTable(appManifest);
// This will always be non empty since every class has basic methods like toString.
Method randomMethod = getTestClass().getJavaClass().getMethods()[0];
roboSpec.parallelUniverseInterface.setUpApplicationState(
randomMethod,
roboSpec.testLifecycle,
appManifest,
config,
new RoutingResourceTable(getCompiletimeSdkResourceTable(), appResourceTable),
new RoutingResourceTable(systemResourceTable, appResourceTable),
new RoutingResourceTable(systemResourceTable));
roboSpec.testLifecycle.beforeTest(null);
}
示例10: resetWindowManager
import org.robolectric.util.ReflectionHelpers; //导入方法依赖的package包/类
@SuppressLint("NewApi")
@After
public void resetWindowManager() throws Exception {
// https://github.com/robolectric/robolectric/pull/1741
final Class<?> btclass = Class.forName("com.android.internal.os.BackgroundThread");
Object backgroundThreadSingleton = ReflectionHelpers.getStaticField(btclass,"sInstance");
if (backgroundThreadSingleton!=null) {
btclass.getMethod("quit").invoke(backgroundThreadSingleton);
ReflectionHelpers.setStaticField(btclass, "sInstance", null);
ReflectionHelpers.setStaticField(btclass, "sHandler", null);
}
// https://github.com/robolectric/robolectric/issues/2068
Class clazz = ReflectionHelpers.loadClass(getClass().getClassLoader(), "android.view.WindowManagerGlobal");
Object instance = ReflectionHelpers.callStaticMethod(clazz, "getInstance");
// We essentially duplicate what's in {@link WindowManagerGlobal#closeAll} with what's below.
// The closeAll method has a bit of a bug where it's iterating through the "roots" but
// bases the number of objects to iterate through by the number of "views." This can result in
// an {@link java.lang.IndexOutOfBoundsException} being thrown.
Object lock = ReflectionHelpers.getField(instance, "mLock");
ArrayList<Object> roots = ReflectionHelpers.getField(instance, "mRoots");
//noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (lock) {
for (int i = 0; i < roots.size(); i++) {
ReflectionHelpers.callInstanceMethod(instance, "removeViewLocked",
ReflectionHelpers.ClassParameter.from(int.class, i),
ReflectionHelpers.ClassParameter.from(boolean.class, false));
}
}
// Views will still be held by this array. We need to clear it out to ensure
// everything is released.
ArraySet<View> dyingViews = ReflectionHelpers.getField(instance, "mDyingViews");
dyingViews.clear();
}
示例11: testGetDrawablePreLollipop
import org.robolectric.util.ReflectionHelpers; //导入方法依赖的package包/类
@Test
public void testGetDrawablePreLollipop()
{
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 20);
final Drawable resource = mock(Drawable.class);
final int resourceId = 64532;
final Activity activity = mock(Activity.class);
final ActivityResourceFinder resourceFinder = new ActivityResourceFinder(activity);
final Resources resources = mock(Resources.class);
when(activity.getResources()).thenReturn(resources);
when(resources.getDrawable(resourceId)).thenReturn(resource);
assertEquals(resource, resourceFinder.getDrawable(resourceId));
}
示例12: testIsRtlPreIceCreamSandwich
import org.robolectric.util.ReflectionHelpers; //导入方法依赖的package包/类
@Test
public void testIsRtlPreIceCreamSandwich()
{
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.HONEYCOMB_MR2);
final Layout layout = mock(Layout.class);
when(layout.getAlignment()).thenReturn(Layout.Alignment.ALIGN_NORMAL);
assertFalse(PromptUtils.isRtlText(layout, null));
}
示例13: testIsRtlPreIceCreamSandwichOpposite
import org.robolectric.util.ReflectionHelpers; //导入方法依赖的package包/类
@Test
public void testIsRtlPreIceCreamSandwichOpposite()
{
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.HONEYCOMB_MR2);
final Layout layout = mock(Layout.class);
when(layout.getAlignment()).thenReturn(Layout.Alignment.ALIGN_OPPOSITE);
assertTrue(PromptUtils.isRtlText(layout, null));
}
示例14: shouldThrowOnInvalidAlgorithmParameterErrorWhenTryingToObtainRSAKeys
import org.robolectric.util.ReflectionHelpers; //导入方法依赖的package包/类
@Test
public void shouldThrowOnInvalidAlgorithmParameterErrorWhenTryingToObtainRSAKeys() throws Exception {
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 19);
exception.expect(KeyException.class);
exception.expectMessage("An error occurred while trying to obtain the RSA KeyPair Entry from the Android KeyStore.");
PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(false);
KeyPairGeneratorSpec spec = PowerMockito.mock(KeyPairGeneratorSpec.class);
KeyPairGeneratorSpec.Builder builder = newKeyPairGeneratorSpecBuilder(spec);
PowerMockito.whenNew(KeyPairGeneratorSpec.Builder.class).withAnyArguments().thenReturn(builder);
doThrow(new InvalidAlgorithmParameterException()).when(keyPairGenerator).initialize(any(AlgorithmParameterSpec.class));
cryptoUtil.getRSAKeyEntry();
}
示例15: testIsRtlFirstCharacterNotRtl
import org.robolectric.util.ReflectionHelpers; //导入方法依赖的package包/类
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Test
public void testIsRtlFirstCharacterNotRtl()
{
ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN_MR1);
final Resources resources = mock(Resources.class);
final Configuration configuration = mock(Configuration.class);
when(resources.getConfiguration()).thenReturn(configuration);
when(configuration.getLayoutDirection()).thenReturn(View.LAYOUT_DIRECTION_LTR);
final Layout layout = mock(Layout.class);
when(layout.isRtlCharAt(0)).thenReturn(false);
when(layout.getAlignment()).thenReturn(Layout.Alignment.ALIGN_NORMAL);
assertFalse(PromptUtils.isRtlText(layout, resources));
}