PresenterManager { @NonNull @MainThread static ActivityScopedCache getOrCreateActivityScopedCache( @NonNull Activity activity) { if (activity == null) { throw new NullPointerException("Activity is null"); } String activityId = activityIdMap.get(activity); if (activityId == null) { activityId = UUID.randomUUID().toString(); activityIdMap.put(activity, activityId); if (activityIdMap.size() == 1) { activity.getApplication().registerActivityLifecycleCallbacks(activityLifecycleCallbacks); if (DEBUG) { Log.d(DEBUG_TAG, "Registering ActivityLifecycleCallbacks"); } } } ActivityScopedCache activityScopedCache = activityScopedCacheMap.get(activityId); if (activityScopedCache == null) { activityScopedCache = new ActivityScopedCache(); activityScopedCacheMap.put(activityId, activityScopedCache); } return activityScopedCache; } private  PresenterManager(); @Nullable static P getPresenter(@NonNull Activity activity, @NonNull String viewId); @Nullable static VS getViewState(@NonNull Activity activity, @NonNull String viewId); @NonNull static Activity getActivity(@NonNull Context context); static void putPresenter(@NonNull Activity activity, @NonNull String viewId,
      @NonNull MvpPresenter<? extends MvpView> presenter); static void putViewState(@NonNull Activity activity, @NonNull String viewId,
      @NonNull Object viewState); static void remove(@NonNull Activity activity, @NonNull String viewId); static boolean DEBUG; static final String DEBUG_TAG; }
@Test public void removesLifecycleListener() { Activity activity = Mockito.mock(Activity.class); Application application = Mockito.mock(Application.class); Mockito.when(activity.getApplication()).thenReturn(application); Mockito.when(activity.isChangingConfigurations()).thenReturn(false); ActivityScopedCache scopedCache = PresenterManager.getOrCreateActivityScopedCache(activity); Mockito.verify(application, Mockito.times(1)) .registerActivityLifecycleCallbacks(PresenterManager.activityLifecycleCallbacks); PresenterManager.activityLifecycleCallbacks.onActivityDestroyed(activity); Mockito.verify(application, Mockito.times(1)) .unregisterActivityLifecycleCallbacks(PresenterManager.activityLifecycleCallbacks); }
@Test public void saveActivityIdAndRestoreFromBundle() { Bundle bundle1 = new Bundle(); Activity portraitActivity1 = Mockito.mock(Activity.class); Activity landscapeActivity1 = Mockito.mock(Activity.class); MockApplication application = new MockApplication(); Mockito.when(portraitActivity1.getApplication()).thenReturn(application); Mockito.when(landscapeActivity1.getApplication()).thenReturn(application); Mockito.when(portraitActivity1.isChangingConfigurations()).thenReturn(true); Mockito.when(landscapeActivity1.isChangingConfigurations()).thenReturn(false); ActivityScopedCache scopedCache1 = PresenterManager.getOrCreateActivityScopedCache(portraitActivity1); Assert.assertTrue( application.lifecycleCallbacksList.contains(PresenterManager.activityLifecycleCallbacks)); Assert.assertEquals(1, application.registerInvocations); Assert.assertEquals(0, application.unregisterInvocations); PresenterManager.activityLifecycleCallbacks.onActivityPaused(portraitActivity1); PresenterManager.activityLifecycleCallbacks.onActivityStopped(portraitActivity1); PresenterManager.activityLifecycleCallbacks.onActivitySaveInstanceState(portraitActivity1, bundle1); PresenterManager.activityLifecycleCallbacks.onActivityDestroyed(portraitActivity1); Assert.assertTrue( application.lifecycleCallbacksList.contains(PresenterManager.activityLifecycleCallbacks)); Assert.assertEquals(1, application.registerInvocations); Assert.assertEquals(0, application.unregisterInvocations); String activityId = bundle1.getString(PresenterManager.KEY_ACTIVITY_ID); Assert.assertNotNull(activityId); PresenterManager.activityLifecycleCallbacks.onActivityCreated(landscapeActivity1, bundle1); PresenterManager.activityLifecycleCallbacks.onActivityStarted(landscapeActivity1); PresenterManager.activityLifecycleCallbacks.onActivityResumed(landscapeActivity1); ActivityScopedCache scopedCache2 = PresenterManager.getOrCreateActivityScopedCache(landscapeActivity1); Assert.assertTrue(scopedCache1 == scopedCache2); Assert.assertTrue( application.lifecycleCallbacksList.contains(PresenterManager.activityLifecycleCallbacks)); Assert.assertEquals(1, application.registerInvocations); Assert.assertEquals(0, application.unregisterInvocations); PresenterManager.activityLifecycleCallbacks.onActivityPaused(landscapeActivity1); PresenterManager.activityLifecycleCallbacks.onActivityStopped(landscapeActivity1); PresenterManager.activityLifecycleCallbacks.onActivitySaveInstanceState(landscapeActivity1, bundle1); PresenterManager.activityLifecycleCallbacks.onActivityDestroyed(landscapeActivity1); Assert.assertFalse( application.lifecycleCallbacksList.contains(PresenterManager.activityLifecycleCallbacks)); Assert.assertEquals(1, application.registerInvocations); Assert.assertEquals(1, application.unregisterInvocations); }
@Test public void saveActivityIdAndRestoreFromBundleWithTwoActivitiesOnBackStack() { Bundle bundle1 = new Bundle(); Bundle bundle2 = new Bundle(); Activity portraitActivity1 = Mockito.mock(Activity.class); Activity landscapeActivity1 = Mockito.mock(Activity.class); Activity portraitActivity2 = Mockito.mock(Activity.class); Activity landscapeActivity2 = Mockito.mock(Activity.class); MockApplication application = new MockApplication(); Mockito.when(portraitActivity1.getApplication()).thenReturn(application); Mockito.when(landscapeActivity1.getApplication()).thenReturn(application); Mockito.when(portraitActivity2.getApplication()).thenReturn(application); Mockito.when(landscapeActivity2.getApplication()).thenReturn(application); Mockito.when(portraitActivity1.isChangingConfigurations()).thenReturn(true); Mockito.when(landscapeActivity1.isChangingConfigurations()).thenReturn(false); Mockito.when(portraitActivity2.isChangingConfigurations()).thenReturn(true); Mockito.when(landscapeActivity2.isChangingConfigurations()).thenReturn(false); ActivityScopedCache activity1ScopedCache1 = PresenterManager.getOrCreateActivityScopedCache(portraitActivity1); Assert.assertTrue( application.lifecycleCallbacksList.contains(PresenterManager.activityLifecycleCallbacks)); Assert.assertEquals(1, application.registerInvocations); Assert.assertEquals(0, application.unregisterInvocations); PresenterManager.activityLifecycleCallbacks.onActivityCreated(portraitActivity2, null); PresenterManager.activityLifecycleCallbacks.onActivityStarted(portraitActivity2); PresenterManager.activityLifecycleCallbacks.onActivityResumed(portraitActivity2); ActivityScopedCache activity2ScopedCache1 = PresenterManager.getOrCreateActivityScopedCache(portraitActivity2); Assert.assertTrue( application.lifecycleCallbacksList.contains(PresenterManager.activityLifecycleCallbacks)); Assert.assertEquals(1, application.registerInvocations); Assert.assertEquals(0, application.unregisterInvocations); PresenterManager.activityLifecycleCallbacks.onActivityPaused(portraitActivity2); PresenterManager.activityLifecycleCallbacks.onActivityStopped(portraitActivity2); PresenterManager.activityLifecycleCallbacks.onActivitySaveInstanceState(portraitActivity2, bundle2); PresenterManager.activityLifecycleCallbacks.onActivityDestroyed(portraitActivity2); Assert.assertTrue( application.lifecycleCallbacksList.contains(PresenterManager.activityLifecycleCallbacks)); Assert.assertEquals(1, application.registerInvocations); Assert.assertEquals(0, application.unregisterInvocations); String activity2Id = bundle2.getString(PresenterManager.KEY_ACTIVITY_ID); Assert.assertNotNull(activity2Id); PresenterManager.activityLifecycleCallbacks.onActivityPaused(portraitActivity1); PresenterManager.activityLifecycleCallbacks.onActivityStopped(portraitActivity1); PresenterManager.activityLifecycleCallbacks.onActivitySaveInstanceState(portraitActivity1, bundle1); PresenterManager.activityLifecycleCallbacks.onActivityDestroyed(portraitActivity1); Assert.assertTrue( application.lifecycleCallbacksList.contains(PresenterManager.activityLifecycleCallbacks)); Assert.assertEquals(1, application.registerInvocations); Assert.assertEquals(0, application.unregisterInvocations); String activity1Id = bundle1.getString(PresenterManager.KEY_ACTIVITY_ID); Assert.assertNotNull(activity1Id); PresenterManager.activityLifecycleCallbacks.onActivityCreated(landscapeActivity2, bundle2); PresenterManager.activityLifecycleCallbacks.onActivityStarted(landscapeActivity2); PresenterManager.activityLifecycleCallbacks.onActivityResumed(landscapeActivity2); ActivityScopedCache activity2ScopedCache2 = PresenterManager.getOrCreateActivityScopedCache(landscapeActivity2); Assert.assertTrue(activity2ScopedCache1 == activity2ScopedCache2); PresenterManager.activityLifecycleCallbacks.onActivityCreated(landscapeActivity1, bundle1); PresenterManager.activityLifecycleCallbacks.onActivityStarted(landscapeActivity1); PresenterManager.activityLifecycleCallbacks.onActivityResumed(landscapeActivity1); ActivityScopedCache activity1ScopedCache2 = PresenterManager.getOrCreateActivityScopedCache(landscapeActivity1); Assert.assertTrue(activity1ScopedCache1 == activity1ScopedCache2); Assert.assertTrue( application.lifecycleCallbacksList.contains(PresenterManager.activityLifecycleCallbacks)); Assert.assertEquals(1, application.registerInvocations); Assert.assertEquals(0, application.unregisterInvocations); PresenterManager.activityLifecycleCallbacks.onActivityPaused(landscapeActivity2); PresenterManager.activityLifecycleCallbacks.onActivityStopped(landscapeActivity2); PresenterManager.activityLifecycleCallbacks.onActivitySaveInstanceState(landscapeActivity2, bundle2); PresenterManager.activityLifecycleCallbacks.onActivityDestroyed(landscapeActivity2); Assert.assertTrue( application.lifecycleCallbacksList.contains(PresenterManager.activityLifecycleCallbacks)); Assert.assertEquals(1, application.registerInvocations); Assert.assertEquals(0, application.unregisterInvocations); PresenterManager.activityLifecycleCallbacks.onActivityPaused(landscapeActivity1); PresenterManager.activityLifecycleCallbacks.onActivityStopped(landscapeActivity1); PresenterManager.activityLifecycleCallbacks.onActivitySaveInstanceState(landscapeActivity1, bundle1); PresenterManager.activityLifecycleCallbacks.onActivityDestroyed(landscapeActivity1); Assert.assertFalse( application.lifecycleCallbacksList.contains(PresenterManager.activityLifecycleCallbacks)); Assert.assertEquals(1, application.registerInvocations); Assert.assertEquals(1, application.unregisterInvocations); }
@Test public void getOrCreateActivityScopeCacheThrowsNullpointerException() { try { PresenterManager.getOrCreateActivityScopedCache(null); Assert.fail("Exception expected"); } catch (NullPointerException e) { } }
@Test public void returnsSameScopedCacheAndRegisterLifecycleListener() { Activity activity = Mockito.mock(Activity.class); Application application = Mockito.mock(Application.class); Mockito.when(activity.getApplication()).thenReturn(application); ActivityScopedCache scopedCache1 = PresenterManager.getOrCreateActivityScopedCache(activity); ActivityScopedCache scopedCache2 = PresenterManager.getOrCreateActivityScopedCache(activity); Assert.assertTrue(scopedCache1 == scopedCache2); Mockito.verify(application, Mockito.times(1)) .registerActivityLifecycleCallbacks(PresenterManager.activityLifecycleCallbacks); }