Java Code Examples for org.robolectric.util.ReflectionHelpers#setStaticField()
The following examples show how to use
org.robolectric.util.ReflectionHelpers#setStaticField() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestOldTest.java From Leanplum-Android-SDK with Apache License 2.0 | 6 votes |
/** * Runs before every test case. */ @Before public void setUp() throws Exception { Application context = RuntimeEnvironment.application; assertNotNull(context); Leanplum.setApplicationContext(context); ReflectionHelpers.setStaticField(LeanplumEventDataManager.class, "instance", null); LeanplumEventDataManager.sharedInstance(); ShadowOperationQueue shadowOperationQueue = new ShadowOperationQueue(); Field instance = OperationQueue.class.getDeclaredField("instance"); instance.setAccessible(true); instance.set(instance, shadowOperationQueue); ShadowLooper.idleMainLooperConstantly(true); }
Example 2
Source File: SecureCredentialsManagerTest.java From Auth0.Android with MIT License | 6 votes |
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Test @Config(sdk = 21) public void shouldNotRequireAuthenticationIfAPI21AndLockScreenDisabled() { 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)); }
Example 3
Source File: CryptoUtilTest.java From Auth0.Android with MIT License | 6 votes |
@RequiresApi(api = Build.VERSION_CODES.P) @Test @Config(sdk = 28) public void shouldUseExistingRSAKeyPairRebuildingTheEntryOnAPI28AndUp() throws Exception { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 28); KeyStore.PrivateKeyEntry entry = PowerMockito.mock(KeyStore.PrivateKeyEntry.class); PrivateKey privateKey = PowerMockito.mock(PrivateKey.class); Certificate certificate = PowerMockito.mock(Certificate.class); ArgumentCaptor<Object> varargsCaptor = ArgumentCaptor.forClass(Object.class); PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(true); PowerMockito.when(keyStore.getKey(KEY_ALIAS, null)).thenReturn(privateKey); PowerMockito.when(keyStore.getCertificate(KEY_ALIAS)).thenReturn(certificate); PowerMockito.whenNew(KeyStore.PrivateKeyEntry.class).withAnyArguments().thenReturn(entry); KeyStore.PrivateKeyEntry rsaEntry = cryptoUtil.getRSAKeyEntry(); PowerMockito.verifyNew(KeyStore.PrivateKeyEntry.class).withArguments(varargsCaptor.capture()); assertThat(rsaEntry, is(notNullValue())); assertThat(rsaEntry, is(entry)); assertThat(varargsCaptor.getAllValues(), is(notNullValue())); PrivateKey capturedPrivateKey = (PrivateKey) varargsCaptor.getAllValues().get(0); Certificate[] capturedCertificatesArray = (Certificate[]) varargsCaptor.getAllValues().get(1); assertThat(capturedPrivateKey, is(privateKey)); assertThat(capturedCertificatesArray[0], is(certificate)); assertThat(capturedCertificatesArray.length, is(1)); }
Example 4
Source File: CryptoUtilTest.java From Auth0.Android with MIT License | 6 votes |
@Test public void shouldThrowOnNoSuchAlgorithmExceptionWhenTryingToObtainRSAKeys() throws Exception { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 19); exception.expect(IncompatibleDeviceException.class); exception.expectMessage("The device is not compatible with the CryptoUtil class"); 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(); }
Example 5
Source File: PromptOptionsUnitTest.java From MaterialTapTargetPrompt with Apache License 2.0 | 6 votes |
@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)); }
Example 6
Source File: CryptoUtilTest.java From Auth0.Android with MIT License | 6 votes |
@Test public void shouldThrowOnNoSuchProviderExceptionWhenTryingToObtainRSAKeys() throws Exception { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 19); exception.expect(IncompatibleDeviceException.class); exception.expectMessage("The device is not compatible with the CryptoUtil class"); 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(); }
Example 7
Source File: CryptoUtilTest.java From Auth0.Android with MIT License | 5 votes |
@RequiresApi(api = Build.VERSION_CODES.P) @Test @Config(sdk = 28) public void shouldUseExistingRSAKeyPairOnAPI28AndUp() throws Exception { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 28); KeyStore.PrivateKeyEntry entry = PowerMockito.mock(KeyStore.PrivateKeyEntry.class); PowerMockito.when(keyStore.getEntry(KEY_ALIAS, null)).thenReturn(entry); PrivateKey privateKey = null; PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(true); PowerMockito.when(keyStore.getKey(KEY_ALIAS, null)).thenReturn(privateKey); KeyStore.PrivateKeyEntry rsaEntry = cryptoUtil.getRSAKeyEntry(); assertThat(rsaEntry, is(notNullValue())); assertThat(rsaEntry, is(entry)); }
Example 8
Source File: MaterialTapTargetPromptUnitTest.java From MaterialTapTargetPrompt with Apache License 2.0 | 5 votes |
@Test public void targetViewBelowKitKatNotAttached() { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN_MR2); final MaterialTapTargetPrompt.Builder builder = createMockBuilder(SCREEN_WIDTH, SCREEN_HEIGHT) .setPrimaryText("test"); final Button button = mock(Button.class); builder.setTarget(button); final MaterialTapTargetPrompt prompt = builder.show(); assertNotNull(prompt); }
Example 9
Source File: ActivityResourceFinderUnitTest.java From MaterialTapTargetPrompt with Apache License 2.0 | 5 votes |
@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)); }
Example 10
Source File: SupportFragmentResourceFinderUnitTest.java From MaterialTapTargetPrompt with Apache License 2.0 | 5 votes |
@Test public void testGetDrawablePreLollipop() { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 20); final Drawable resource = mock(Drawable.class); final int resourceId = 64532; final DialogFragment dialogFragment = spy(new DialogFragment()); final SupportFragmentResourceFinder resourceFinder = new SupportFragmentResourceFinder(dialogFragment); final Resources resources = mock(Resources.class); final Context context = mock(Context.class); when(dialogFragment.getContext()).thenReturn(context); when(dialogFragment.getResources()).thenReturn(resources); when(resources.getDrawable(resourceId)).thenReturn(resource); assertEquals(resource, resourceFinder.getDrawable(resourceId)); }
Example 11
Source File: CryptoUtilTest.java From Auth0.Android with MIT License | 5 votes |
@RequiresApi(api = Build.VERSION_CODES.O_MR1) @Test @Config(sdk = 27) public void shouldUseExistingRSAKeyPairOnAPI27AndDown() throws Exception { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 27); KeyStore.PrivateKeyEntry entry = PowerMockito.mock(KeyStore.PrivateKeyEntry.class); PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(true); PowerMockito.when(keyStore.getEntry(KEY_ALIAS, null)).thenReturn(entry); KeyStore.PrivateKeyEntry rsaEntry = cryptoUtil.getRSAKeyEntry(); assertThat(rsaEntry, is(notNullValue())); assertThat(rsaEntry, is(entry)); }
Example 12
Source File: PromptUtilsUnitTest.java From MaterialTapTargetPrompt with Apache License 2.0 | 5 votes |
@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)); }
Example 13
Source File: PromptUtilsUnitTest.java From MaterialTapTargetPrompt with Apache License 2.0 | 5 votes |
@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)); }
Example 14
Source File: PromptUtilsUnitTest.java From MaterialTapTargetPrompt with Apache License 2.0 | 5 votes |
@Test public void testIsRtlFirstCharacterNotRtlPreJellyBeanMR1() { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN); final Layout layout = mock(Layout.class); when(layout.isRtlCharAt(0)).thenReturn(false); when(layout.getAlignment()).thenReturn(Layout.Alignment.ALIGN_NORMAL); assertFalse(PromptUtils.isRtlText(layout, null)); }
Example 15
Source File: PromptUtilsUnitTest.java From MaterialTapTargetPrompt with Apache License 2.0 | 5 votes |
@Test public void testIsRtlFirstCharacterNotRtlNotOppositePreJellyBeanMR1() { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN_MR1); final Layout layout = mock(Layout.class); when(layout.isRtlCharAt(0)).thenReturn(false); when(layout.getAlignment()).thenReturn(Layout.Alignment.ALIGN_CENTER); assertFalse(PromptUtils.isRtlText(layout, null)); }
Example 16
Source File: CorePeripheralTest.java From RxCentralBle with Apache License 2.0 | 5 votes |
@Before public void setup() { MockitoAnnotations.initMocks(this); ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 21); corePeripheral = new CorePeripheral(bluetoothDevice, context); }
Example 17
Source File: CryptoUtilTest.java From Auth0.Android with MIT License | 4 votes |
@RequiresApi(api = Build.VERSION_CODES.M) @Test @Config(sdk = 23) public void shouldCreateRSAKeyPairIfMissingOnAPI23AndUp() throws Exception { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 23); PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(false); KeyStore.PrivateKeyEntry expectedEntry = PowerMockito.mock(KeyStore.PrivateKeyEntry.class); PowerMockito.when(keyStore.getEntry(KEY_ALIAS, null)).thenReturn(expectedEntry); KeyGenParameterSpec spec = PowerMockito.mock(KeyGenParameterSpec.class); KeyGenParameterSpec.Builder builder = newKeyGenParameterSpecBuilder(spec); PowerMockito.whenNew(KeyGenParameterSpec.Builder.class).withArguments(KEY_ALIAS, KeyProperties.PURPOSE_DECRYPT | KeyProperties.PURPOSE_ENCRYPT).thenReturn(builder); ArgumentCaptor<X500Principal> principalCaptor = ArgumentCaptor.forClass(X500Principal.class); ArgumentCaptor<Date> startDateCaptor = ArgumentCaptor.forClass(Date.class); ArgumentCaptor<Date> endDateCaptor = ArgumentCaptor.forClass(Date.class); final KeyStore.PrivateKeyEntry entry = cryptoUtil.getRSAKeyEntry(); Mockito.verify(builder).setKeySize(2048); Mockito.verify(builder).setCertificateSubject(principalCaptor.capture()); Mockito.verify(builder).setCertificateSerialNumber(BigInteger.ONE); Mockito.verify(builder).setCertificateNotBefore(startDateCaptor.capture()); Mockito.verify(builder).setCertificateNotAfter(endDateCaptor.capture()); Mockito.verify(builder).setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1); Mockito.verify(builder).setBlockModes(KeyProperties.BLOCK_MODE_ECB); Mockito.verify(keyPairGenerator).initialize(spec); Mockito.verify(keyPairGenerator).generateKeyPair(); assertThat(principalCaptor.getValue(), is(notNullValue())); assertThat(principalCaptor.getValue().getName(), is(CERTIFICATE_PRINCIPAL)); assertThat(startDateCaptor.getValue(), is(notNullValue())); long diffMillis = startDateCaptor.getValue().getTime() - new Date().getTime(); long days = TimeUnit.MILLISECONDS.toDays(diffMillis); assertThat(days, is(0L)); //Date is Today assertThat(endDateCaptor.getValue(), is(notNullValue())); diffMillis = endDateCaptor.getValue().getTime() - new Date().getTime(); days = TimeUnit.MILLISECONDS.toDays(diffMillis); assertThat(days, is(greaterThan(25 * 365L))); //Date more than 25 Years in days assertThat(entry, is(expectedEntry)); }
Example 18
Source File: CryptoUtilTest.java From Auth0.Android with MIT License | 4 votes |
@RequiresApi(api = Build.VERSION_CODES.P) @Test @Config(sdk = 28) public void shouldCreateNewRSAKeyPairWhenExistingRSAKeyPairCannotBeRebuiltOnAPI28AndUp() throws Exception { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 28); PrivateKey privateKey = PowerMockito.mock(PrivateKey.class); //This is required to trigger the fallback when alias is present but key is not PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(true); PowerMockito.when(keyStore.getKey(KEY_ALIAS, null)).thenReturn(privateKey).thenReturn(null); PowerMockito.when(keyStore.getCertificate(KEY_ALIAS)).thenReturn(null); //This is required to trigger finding the key after generating it KeyStore.PrivateKeyEntry expectedEntry = PowerMockito.mock(KeyStore.PrivateKeyEntry.class); PowerMockito.when(keyStore.getEntry(KEY_ALIAS, null)).thenReturn(expectedEntry); //Tests no instantiation of PrivateKeyEntry PowerMockito.verifyZeroInteractions(KeyStore.PrivateKeyEntry.class); //Creation assertion KeyGenParameterSpec spec = PowerMockito.mock(KeyGenParameterSpec.class); KeyGenParameterSpec.Builder builder = newKeyGenParameterSpecBuilder(spec); PowerMockito.whenNew(KeyGenParameterSpec.Builder.class).withArguments(KEY_ALIAS, KeyProperties.PURPOSE_DECRYPT | KeyProperties.PURPOSE_ENCRYPT).thenReturn(builder); ArgumentCaptor<X500Principal> principalCaptor = ArgumentCaptor.forClass(X500Principal.class); ArgumentCaptor<Date> startDateCaptor = ArgumentCaptor.forClass(Date.class); ArgumentCaptor<Date> endDateCaptor = ArgumentCaptor.forClass(Date.class); final KeyStore.PrivateKeyEntry entry = cryptoUtil.getRSAKeyEntry(); Mockito.verify(builder).setKeySize(2048); Mockito.verify(builder).setCertificateSubject(principalCaptor.capture()); Mockito.verify(builder).setCertificateSerialNumber(BigInteger.ONE); Mockito.verify(builder).setCertificateNotBefore(startDateCaptor.capture()); Mockito.verify(builder).setCertificateNotAfter(endDateCaptor.capture()); Mockito.verify(builder).setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1); Mockito.verify(builder).setBlockModes(KeyProperties.BLOCK_MODE_ECB); Mockito.verify(keyPairGenerator).initialize(spec); Mockito.verify(keyPairGenerator).generateKeyPair(); assertThat(principalCaptor.getValue(), is(notNullValue())); assertThat(principalCaptor.getValue().getName(), is(CERTIFICATE_PRINCIPAL)); assertThat(startDateCaptor.getValue(), is(notNullValue())); long diffMillis = startDateCaptor.getValue().getTime() - new Date().getTime(); long days = TimeUnit.MILLISECONDS.toDays(diffMillis); assertThat(days, is(0L)); //Date is Today assertThat(endDateCaptor.getValue(), is(notNullValue())); diffMillis = endDateCaptor.getValue().getTime() - new Date().getTime(); days = TimeUnit.MILLISECONDS.toDays(diffMillis); assertThat(days, is(greaterThan(25 * 365L))); //Date more than 25 Years in days assertThat(entry, is(expectedEntry)); }
Example 19
Source File: CryptoUtilTest.java From Auth0.Android with MIT License | 4 votes |
@RequiresApi(api = Build.VERSION_CODES.KITKAT) @Test @Config(sdk = 19) public void shouldNotCreateProtectedRSAKeyPairIfMissingAndLockScreenEnabledOnAPI19() throws Exception { ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 19); PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(false); KeyStore.PrivateKeyEntry expectedEntry = PowerMockito.mock(KeyStore.PrivateKeyEntry.class); PowerMockito.when(keyStore.getEntry(KEY_ALIAS, null)).thenReturn(expectedEntry); KeyPairGeneratorSpec spec = PowerMockito.mock(KeyPairGeneratorSpec.class); KeyPairGeneratorSpec.Builder builder = newKeyPairGeneratorSpecBuilder(spec); PowerMockito.whenNew(KeyPairGeneratorSpec.Builder.class).withAnyArguments().thenReturn(builder); ArgumentCaptor<X500Principal> principalCaptor = ArgumentCaptor.forClass(X500Principal.class); ArgumentCaptor<Date> startDateCaptor = ArgumentCaptor.forClass(Date.class); ArgumentCaptor<Date> endDateCaptor = ArgumentCaptor.forClass(Date.class); //Set LockScreen as Enabled KeyguardManager kService = PowerMockito.mock(KeyguardManager.class); PowerMockito.when(context.getSystemService(Context.KEYGUARD_SERVICE)).thenReturn(kService); PowerMockito.when(kService.isKeyguardSecure()).thenReturn(true); final KeyStore.PrivateKeyEntry entry = cryptoUtil.getRSAKeyEntry(); Mockito.verify(builder).setKeySize(2048); Mockito.verify(builder).setSubject(principalCaptor.capture()); Mockito.verify(builder).setAlias(KEY_ALIAS); Mockito.verify(builder).setSerialNumber(BigInteger.ONE); Mockito.verify(builder).setStartDate(startDateCaptor.capture()); Mockito.verify(builder).setEndDate(endDateCaptor.capture()); Mockito.verify(builder, never()).setEncryptionRequired(); Mockito.verify(keyPairGenerator).initialize(spec); Mockito.verify(keyPairGenerator).generateKeyPair(); assertThat(principalCaptor.getValue(), is(notNullValue())); assertThat(principalCaptor.getValue().getName(), is(CERTIFICATE_PRINCIPAL)); assertThat(startDateCaptor.getValue(), is(notNullValue())); long diffMillis = startDateCaptor.getValue().getTime() - new Date().getTime(); long days = TimeUnit.MILLISECONDS.toDays(diffMillis); assertThat(days, is(0L)); //Date is Today assertThat(endDateCaptor.getValue(), is(notNullValue())); diffMillis = endDateCaptor.getValue().getTime() - new Date().getTime(); days = TimeUnit.MILLISECONDS.toDays(diffMillis); assertThat(days, is(greaterThan(25 * 365L))); //Date more than 25 Years in days assertThat(entry, is(expectedEntry)); }
Example 20
Source File: LeanplumEventDataManagerTest.java From Leanplum-Android-SDK with Apache License 2.0 | 4 votes |
public static void setDatabaseToNull(){ ReflectionHelpers.setStaticField(LeanplumEventDataManager.class, "instance", null); }