AppStateManager { boolean shouldEndSession() { InternalSession session = getSession(); MParticle instance = MParticle.getInstance(); return 0 != session.mSessionStartTime && isBackgrounded() && session.isTimedOut(mConfigManager.getSessionTimeout()) && (instance == null || !instance.Media().getAudioPlaying()); } AppStateManager(Context context, boolean unitTesting);  AppStateManager(Context context); void init(int apiVersion); String getLaunchAction(); Uri getLaunchUri(); void setConfigManager(ConfigManager manager); void setMessageManager(MessageManager manager); void onActivityResumed(Activity activity); void onActivityPaused(Activity activity); void ensureActiveSession(); void logStateTransition(String transitionType, String currentActivity); void onActivityCreated(Activity activity, Bundle savedInstanceState); void onActivityStarted(Activity activity); void onActivityStopped(Activity activity); boolean isBackgrounded(); String getCurrentActivityName(); InternalSession getSession(); void endSession(); void startSession(); void onActivitySaveInstanceState(Activity activity, Bundle outState); void onActivityDestroyed(Activity activity); WeakReference<Activity> getCurrentActivity(); static boolean mInitialized; static final String APP_STATE_FOREGROUND; static final String APP_STATE_BACKGROUND; static final String APP_STATE_NOTRUNNING; }
@Test public void testShouldEndSession() { final Mutable<Boolean> isTimedOut = new Mutable(false); final Mutable<Boolean> isBackground = new Mutable(false); final Mutable<InternalSession> session = new Mutable(new InternalSession() { @Override public boolean isTimedOut(int sessionTimeout) { return isTimedOut.value; } }); session.value.mSessionStartTime = 1; manager = new AppStateManager(new MockContext(), true) { @Override public boolean isBackgrounded() { return isBackground.value; } @Override public InternalSession getSession() { return session.value; } }; ConfigManager configManager = Mockito.mock(ConfigManager.class); manager.setConfigManager(configManager); Mockito.when(MParticle.getInstance().Media().getAudioPlaying()).thenReturn(false); isTimedOut.value = true; isBackground.value = true; assertTrue(manager.shouldEndSession()); isTimedOut.value = false; isBackground.value = true; assertFalse(manager.shouldEndSession()); isTimedOut.value = true; isBackground.value = false; assertFalse(manager.shouldEndSession()); isTimedOut.value = true; isBackground.value = true; assertTrue(manager.shouldEndSession()); Mockito.when(MParticle.getInstance().Media().getAudioPlaying()).thenReturn(true); assertFalse(manager.shouldEndSession()); MParticle.setInstance(null); assertTrue(manager.shouldEndSession()); }