NucleusLayout extends FrameLayout implements ViewWithPresenter<P> { public Activity getActivity() { Context context = getContext(); while (!(context instanceof Activity) && context instanceof ContextWrapper) context = ((ContextWrapper)context).getBaseContext(); if (!(context instanceof Activity)) throw new IllegalStateException("Expected an activity context, got " + context.getClass().getSimpleName()); return (Activity)context; } NucleusLayout(Context context);  NucleusLayout(Context context, AttributeSet attrs);  NucleusLayout(Context context, AttributeSet attrs, int defStyle); PresenterFactory<P> getPresenterFactory(); @Override void setPresenterFactory(PresenterFactory<P> presenterFactory); P getPresenter(); Activity getActivity();  }
@Test public void getActivityFromContext() throws Exception { Activity activity = mock(Activity.class); stub(method(BASE_VIEW_CLASS, "getContext")).toReturn(activity); assertEquals(activity, tested.getActivity()); }
@Test public void getActivityFromWrappedContext() throws Exception { Activity activity = mock(Activity.class); ContextWrapper wrapper = mock(ContextWrapper.class); when(wrapper.getBaseContext()).thenReturn(activity); ContextWrapper wrapper2 = mock(ContextWrapper.class); when(wrapper2.getBaseContext()).thenReturn(wrapper); stub(method(BASE_VIEW_CLASS, "getContext")).toReturn(wrapper2); assertEquals(activity, tested.getActivity()); }