Java Code Examples for org.robolectric.util.ReflectionHelpers#getField()

The following examples show how to use org.robolectric.util.ReflectionHelpers#getField() . 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: UTest.java    From Taskbar with Apache License 2.0 6 votes vote down vote up
@Test
public void testWrapContext() {
    SharedPreferences prefs = U.getSharedPreferences(context);
    prefs.edit().putString(PREF_THEME, "light").apply();
    Context newContext = U.wrapContext(context);
    Integer themeResource = ReflectionHelpers.getField(newContext, "mThemeResource");
    assertNotNull(themeResource);
    assertEquals(R.style.Taskbar, (int) themeResource);
    prefs.edit().putString(PREF_THEME, "dark").apply();
    newContext = U.wrapContext(context);
    themeResource = ReflectionHelpers.getField(newContext, "mThemeResource");
    assertNotNull(themeResource);
    assertEquals(R.style.Taskbar_Dark, (int) themeResource);
    prefs.edit().putString(PREF_THEME, "non-support").apply();
    newContext = U.wrapContext(context);
    assertTrue(newContext instanceof ContextThemeWrapper);
    prefs.edit().remove(PREF_THEME).apply();
    newContext = U.wrapContext(context);
    themeResource = ReflectionHelpers.getField(newContext, "mThemeResource");
    assertNotNull(themeResource);
    assertEquals(R.style.Taskbar, (int) themeResource);
}
 
Example 2
Source File: TestApplication.java    From materialistic with Apache License 2.0 6 votes vote down vote up
private void resetWindowManager() {
    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.
    Collection<View> dyingViews = ReflectionHelpers.getField(instance, "mDyingViews");
    dyingViews.clear();

}
 
Example 3
Source File: DefaultStorIOContentResolverTest.java    From storio with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldUseCustomHandlerForContentObservers() {
    ContentResolver contentResolver = mock(ContentResolver.class);
    ArgumentCaptor<ContentObserver> observerArgumentCaptor = ArgumentCaptor.forClass(ContentObserver.class);
    doNothing().when(contentResolver)
            .registerContentObserver(any(Uri.class), anyBoolean(), observerArgumentCaptor.capture());
    Handler handler = mock(Handler.class);

    StorIOContentResolver storIOContentResolver = DefaultStorIOContentResolver.builder()
            .contentResolver(contentResolver)
            .contentObserverHandler(handler)
            .defaultRxScheduler(null)
            .build();

    Disposable disposable = storIOContentResolver.observeChangesOfUri(mock(Uri.class), LATEST).subscribe();

    assertThat(observerArgumentCaptor.getAllValues()).hasSize(1);
    ContentObserver contentObserver = observerArgumentCaptor.getValue();
    Object actualHandler = ReflectionHelpers.getField(contentObserver, "mHandler");
    assertThat(actualHandler).isEqualTo(handler);

    disposable.dispose();
}
 
Example 4
Source File: TaskbarControllerTest.java    From Taskbar with Apache License 2.0 4 votes vote down vote up
private LinearLayout getFieldSysTrayLayout(TaskbarController uiController) {
    return ReflectionHelpers.getField(uiController, "sysTrayLayout");
}
 
Example 5
Source File: ShadowAlertDialogSupport.java    From openwebnet-android with MIT License 4 votes vote down vote up
public static ShadowAlertDialogSupport getShadowAlertDialog(){
    ShadowDialog dialog = shadowOf(RuntimeEnvironment.application).getLatestDialog();
    AlertDialog alertDialog = getPrivateField(dialog, "realDialog", AlertDialog.class);
    AlertController alert = ReflectionHelpers.getField(alertDialog, "mAlert");
    return new ShadowAlertDialogSupport(alert);
}
 
Example 6
Source File: TestUtil.java    From RxLifecycle with Apache License 2.0 2 votes vote down vote up
/**
 * Manually retrieve the view's attach state change listeners of an event. Robolectric
 * doesn't currently support manually firing these, and it would seem the events are not called
 * in normal Robolectric usage either.
 *
 * @param view View with listeners to notify
 */
static CopyOnWriteArrayList<View.OnAttachStateChangeListener> getAttachStateChangeListeners(View view) {
    Object listenerInfo = ReflectionHelpers.callInstanceMethod(view, "getListenerInfo");
    return ReflectionHelpers.getField(listenerInfo, "mOnAttachStateChangeListeners");
}