ViewModelUtil { public static void closeOnViewModelCleared( @NonNull FragmentActivity activity, @NonNull Scope scope) { Factory factory = new TPViewModelFactory(scope); ViewModelProviders.of(activity, factory).get(TPViewModel.class); } private  ViewModelUtil(); static void closeOnViewModelCleared(
      @NonNull FragmentActivity activity, @NonNull Scope scope); static void closeOnViewModelCleared(@NonNull Fragment fragment, @NonNull Scope scope); static void installViewModelBinding(
      final Scope scope,
      final FragmentActivity activity,
      final Class<T> viewModelClass,
      final Factory factory); static void installViewModelBinding(
      final Scope scope,
      final Fragment fragment,
      final Class<T> viewModelClass,
      final Factory factory);  }
@Test public void testCloseOnClear() { ActivityController<FragmentActivity> activityController = Robolectric.buildActivity(FragmentActivity.class).create(); FragmentActivity activity = activityController.get(); Application application = ApplicationProvider.getApplicationContext(); Scope activityScope = Toothpick.openScopes(application, activity); ViewModelUtil.closeOnViewModelCleared(activity, activityScope); activityController.destroy(); assertThat(Toothpick.isScopeOpen(activity), is(false)); }
@Test public void testCloseOnClear_shouldNotCloseScope_whenConfigurationChange() { ActivityController<FragmentActivity> activityController = Robolectric.buildActivity(FragmentActivity.class).create(); FragmentActivity activity = activityController.get(); Application application = ApplicationProvider.getApplicationContext(); Scope activityScope = Toothpick.openScopes(application, activity); ViewModelUtil.closeOnViewModelCleared(activity, activityScope); activityController.configurationChange(new Configuration()); assertThat(Toothpick.isScopeOpen(activity), is(true)); }