@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 shouldEnterIntoDummyActivityContextBeforeInjectingTestSubject() { when(testCase.getTestSubjectClassType()).thenReturn(Object.class); testFieldInjector.setupTestCase(); InOrder inOrder = inOrder(roboUtils, injector); inOrder.verify(roboUtils).enterContext(any(FragmentActivity.class)); inOrder.verify(roboUtils).getInjector(any(FragmentActivity.class)); inOrder.verify(injector).getInstance(Object.class); }
@Test public void shouldInjectActivityIntoTestCaseMarkedAsSubject() throws NoSuchFieldException, IllegalAccessException { when(testCase.getTestSubjectClassType()).thenReturn(Activity.class); when(testCase.getTestSubjectField()).thenReturn(getSampleField()); when(testCase.getTestCaseInstance()).thenReturn(testCaseInstance); when(injector.getInstance(Activity.class)).thenReturn(activity); testFieldInjector.setupTestCase(); verify(reflectionUtils).forceSetValueOnField(getSampleField(), testCaseInstance, activity); }
@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 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 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)); }