BaseFragment extends Fragment implements BackPressWatcher, ViewClickWatcher { @Override public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) { if (annotationHandler != null) { final int viewResource = annotationHandler.getContentViewResource(-1); if (viewResource != -1) { if (annotationHandler.shouldAttachContentViewToContainer()) { inflater.inflate(viewResource, container, true); return null; } return inflater.inflate(viewResource, container, false); } } return super.onCreateView(inflater, container, savedInstanceState); } BaseFragment(); @SuppressWarnings("TryWithIdenticalCatches") @Nullable static F newInstanceWithArguments(@NonNull final Class<F> classOfFragment, @Nullable final Bundle args); @SuppressWarnings("deprecation") @Override void onAttach(@NonNull final Activity activity); final boolean isAttached(); final boolean runOnUiThread(@NonNull final Runnable action); @Override void onCreate(@Nullable final Bundle savedInstanceState); final boolean isCreated(); @Override void onStart(); final boolean isStarted(); @Override View onCreateView(@NonNull final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState); @Override void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState); final boolean isViewCreated(); @Override void onResume(); boolean invalidateOptionsMenu(); @Override boolean dispatchViewClick(@NonNull final View view); @Override void onPause(); final boolean isPaused(); @Override void onStop(); final boolean isStopped(); @Override @CheckResult boolean dispatchBackPress(); @Override void onDestroy(); final boolean isDestroyed(); @Override void onDetach();  }
@Test public void testOnCreateView() { final Activity activity = Robolectric.buildActivity(TestActivity.class).create().start().resume().get(); final BaseFragment fragment = new TestFragment(); final View view = fragment.onCreateView(activity.getLayoutInflater(), null, null); assertThat(view, is(notNullValue())); }
@Test public void testOnCreateViewWithoutResource() { final Activity activity = Robolectric.buildActivity(TestActivity.class).create().start().resume().get(); final BaseFragment fragment = new TestFragmentWithoutContentView(); final View view = fragment.onCreateView(activity.getLayoutInflater(), null, null); assertThat(view, is(nullValue())); }
@Test public void testOnCreateViewToBeAttachedToTheContainer() { final Activity activity = Robolectric.buildActivity(TestActivity.class).create().start().resume().get(); final BaseFragment fragment = new TestFragmentWithContentViewToBeAttachedToContainer(); final ViewGroup container = new FrameLayout(activity); final View view = fragment.onCreateView(activity.getLayoutInflater(), container, null); assertThat(view, is(nullValue())); assertThat(container.getChildCount(), is(1)); }
@Test public void testOnCreateViewWhenAnnotationsAreDisabled() { final Activity activity = Robolectric.buildActivity(TestActivity.class).create().start().resume().get(); FragmentAnnotations.setEnabled(false); final BaseFragment fragment = new TestFragment(); final View view = fragment.onCreateView(activity.getLayoutInflater(), null, null); assertThat(view, is(nullValue())); FragmentAnnotations.setEnabled(true); }