@Test public void shouldCreateFragmentProxyWhenTestSubjectIsFragment() { when(testCase.getTestSubjectClassType()).thenReturn(RoboFragment.class); when(reflectionUtils.classIsOfAssignableForm(RoboFragment.class, RoboFragment.class)) .thenReturn(true); when(reflectionUtils.createObjectProxy(RoboFragment.class, testFieldInjector)) .thenReturn(fragment); testFieldInjector.setupTestCase(); verify(reflectionUtils).createObjectProxy(RoboFragment.class, testFieldInjector); }
@Test public void shouldInjectFragmentIntoTestCaseMarkedAsSubject() throws NoSuchFieldException, IllegalAccessException { when(testCase.getTestSubjectClassType()).thenReturn(RoboFragment.class); when(testCase.getTestSubjectField()).thenReturn(getSampleField()); when(testCase.getTestCaseInstance()).thenReturn(testCaseInstance); when(reflectionUtils.classIsOfAssignableForm(RoboFragment.class, RoboFragment.class)) .thenReturn(true); when(reflectionUtils.createObjectProxy(RoboFragment.class, testFieldInjector)) .thenReturn(fragment); testFieldInjector.setupTestCase(); verify(reflectionUtils).forceSetValueOnField(getSampleField(), testCaseInstance, fragment); }
@Test public void shouldUseFragmentTransactionToAddFragmentToDummyActivityIfSubjectIsAnFragment() { when(testCase.getTestSubjectClassType()).thenReturn(RoboFragment.class); when(reflectionUtils.classIsOfAssignableForm(RoboFragment.class, RoboFragment.class)) .thenReturn(true); when(reflectionUtils.createObjectProxy(RoboFragment.class, testFieldInjector)) .thenReturn(fragment); when(reflectionUtils.objectIsOfAnyType(fragment, RoboFragment.class)).thenReturn(true); testFieldInjector.setupTestCase(); TestFragmentManager fragmentManager = (TestFragmentManager) shadowOf((FragmentActivity) testFieldInjector.getCurrentContextForTest()) .getSupportFragmentManager(); assertThat(fragmentManager.getCommittedTransactions().size(), is(1)); assertThat( (RoboFragment) fragmentManager.getCommittedTransactions().get(0).getFragment(), is(fragment)); }
@Test public void shouldNotListenToContentChangesIfTestSubjectIsNotAnActivity() { Date testSubject = new Date(); when(testCase.getTestSubjectClassType()).thenReturn(Date.class); when(injector.getInstance(Date.class)).thenReturn(testSubject); when(reflectionUtils.objectIsOfAnyType(testSubject, Activity.class)).thenReturn(false); testFieldInjector.setupTestCase(); verify(injector, never()).getInstance(EventManager.class); }
@Test public void shouldListenToContentChangesIfTestSubjectIsAnActivity() { when(testCase.getTestSubjectClassType()).thenReturn(RoboActivity.class); when(injector.getInstance(RoboActivity.class)).thenReturn(activity); when(reflectionUtils.objectIsOfAnyType(activity, RoboActivity.class)).thenReturn(true); testFieldInjector.setupTestCase(); verify(injector).getInstance(EventManager.class); verify(eventManager) .registerObserver(eq(OnContentChangedEvent.class), isA(ContentChangedListener.class)); }
@Test public void shouldEnterIntoTestSubjectContextIfTestSubjectIsAContext() { when(testCase.getTestSubjectClassType()).thenReturn(Activity.class); when(injector.getInstance(Activity.class)).thenReturn(activity); when(reflectionUtils.objectIsOfAnyType(activity, Context.class)).thenReturn(true); when(roboUtils.getInjector(activity)).thenReturn(injector); testFieldInjector.setupTestCase(); verify(roboUtils).enterContext(activity); }
@Test public void shouldGetTheInjectorForTheCurrentContextOnlyAfterEnteringCurrentContext() { when(testCase.getTestSubjectClassType()).thenReturn(Activity.class); when(injector.getInstance(Activity.class)).thenReturn(activity); when(reflectionUtils.objectIsOfAnyType(activity, Context.class)).thenReturn(true); when(roboUtils.getInjector(activity)).thenReturn(injector); testFieldInjector.setupTestCase(); InOrder inOrder = inOrder(roboUtils); inOrder.verify(roboUtils).enterContext(activity); inOrder.verify(roboUtils).getInjector(activity); }
@Test public void shouldEnterIntoDummyContextIfTestSubjectIsNotAContext() { when(reflectionUtils.objectIsOfAnyType(any(), eq(Context.class))).thenReturn(false); testFieldInjector.setupTestCase(); verify(roboUtils, times(2)).enterContext(any(FragmentActivity.class)); }