ActivityDelegate extends MvvmDelegate<T, S> { @Nullable public Object onRetainCustomNonConfigurationInstance() { if (!delegatedActivity.isChangingConfigurations() && viewModel != null) { viewModel.onDestroy(); viewModel = null; } return viewModel; } ActivityDelegate(@NonNull ActivityDelegateCallback<T, S> callback, @NonNull Activity delegatedActivity); @Nullable Object onRetainCustomNonConfigurationInstance();  }
@Test public void testDestroyViewModelWhenInBackStack() { activityDelegate.viewModel = viewModel; when(activity.isChangingConfigurations()).thenReturn(false); activityDelegate.onRetainCustomNonConfigurationInstance(); verify(viewModel).onDestroy(); }
@Test public void testNotDestroyViewModelOnConfigurationChange() { activityDelegate.viewModel = viewModel; when(activity.isChangingConfigurations()).thenReturn(true); activityDelegate.onRetainCustomNonConfigurationInstance(); verify(viewModel, never()).onDestroy(); }
@Test public void testNotCacheViewModelWhenInBackStack() { activityDelegate.viewModel = viewModel; when(activity.isChangingConfigurations()).thenReturn(false); assertNull(activityDelegate.onRetainCustomNonConfigurationInstance()); }
@Test public void testCacheViewModelOnConfigurationChange() { activityDelegate.viewModel = viewModel; when(activity.isChangingConfigurations()).thenReturn(true); activityDelegate.onRetainCustomNonConfigurationInstance(); assertSame(viewModel, activityDelegate.onRetainCustomNonConfigurationInstance()); }