IntentStarters { public static IntentStarter supportFragmentStarter(@NonNull final androidx.fragment.app.Fragment fragment) { return new SupportFragmentStarter(fragment); } private  IntentStarters(); @NonNull static IntentStarter activityStarter(@NonNull final Activity activity); static IntentStarter fragmentStarter(@NonNull final Fragment fragment); static IntentStarter supportFragmentStarter(@NonNull final androidx.fragment.app.Fragment fragment);  }
@Test public void testSupportFragmentStarter() throws IllegalStateException { final FragmentActivity activity = Robolectric.buildActivity(TestCompatActivity.class).create().start().resume().get(); final androidx.fragment.app.FragmentManager fragmentManager = activity.getSupportFragmentManager(); final androidx.fragment.app.Fragment fragment = new TestCompatFragment(); fragmentManager.beginTransaction().add(fragment, null).commitAllowingStateLoss(); fragmentManager.executePendingTransactions(); final IntentStarter starter = IntentStarters.supportFragmentStarter(fragment); assertThat(starter, is(notNullValue())); assertThat(starter.getContext(), is((Context) activity)); fragmentManager.beginTransaction().remove(fragment).commitAllowingStateLoss(); fragmentManager.executePendingTransactions(); }
@Test public void testSupportFragmentStarterStartIntent() { final androidx.fragment.app.Fragment mockFragment = mock(androidx.fragment.app.Fragment.class); final Intent intent = new Intent(); final IntentStarter starter = IntentStarters.supportFragmentStarter(mockFragment); starter.startIntent(intent); verify(mockFragment).startActivity(intent); }
@Test public void testSupportFragmentStarterStartIntentWithOptions() { final androidx.fragment.app.Fragment mockFragment = mock(androidx.fragment.app.Fragment.class); final Intent intent = new Intent(); final IntentStarter starter = IntentStarters.supportFragmentStarter(mockFragment); starter.startIntent(intent, Bundle.EMPTY); verify(mockFragment).startActivity(intent, Bundle.EMPTY); }
@Test public void testSupportFragmentStarterStartIntentForResult() { final androidx.fragment.app.Fragment mockFragment = mock(androidx.fragment.app.Fragment.class); final Intent intent = new Intent(); final IntentStarter starter = IntentStarters.supportFragmentStarter(mockFragment); starter.startIntentForResult(intent, 1000); verify(mockFragment).startActivityForResult(intent, 1000); }
@Test public void testSupportFragmentStarterStartIntentForResultWithOptions() { final androidx.fragment.app.Fragment mockFragment = mock(androidx.fragment.app.Fragment.class); final Intent intent = new Intent(); final IntentStarter starter = IntentStarters.supportFragmentStarter(mockFragment); starter.startIntentForResult(intent, 1000, Bundle.EMPTY); verify(mockFragment).startActivityForResult(intent, 1000, Bundle.EMPTY); }
@Test public void testSupportFragmentStarterOverridePendingTransition() throws IllegalStateException { final FragmentActivity activity = Robolectric.buildActivity(TestCompatActivity.class).create().start().resume().get(); final androidx.fragment.app.FragmentManager fragmentManager = activity.getSupportFragmentManager(); final androidx.fragment.app.Fragment fragment = new TestCompatFragment(); fragmentManager.beginTransaction().add(fragment, null).commitAllowingStateLoss(); fragmentManager.executePendingTransactions(); final IntentStarter starter = IntentStarters.supportFragmentStarter(fragment); starter.overridePendingTransition( android.R.anim.fade_in, android.R.anim.fade_out ); fragmentManager.beginTransaction().remove(fragment).commitAllowingStateLoss(); fragmentManager.executePendingTransactions(); }