public void testSessions() { Context context = activity.getApplicationContext(); // starting from a clean slate mockLogger.test( "Was AdjustActivityState deleted? " + ActivityHandler.deleteActivityState(context)); // adjust the intervals for testing AdjustFactory.setSessionInterval(2000); AdjustFactory.setSubsessionInterval(100); ActivityHandler activityHandler = new ActivityHandler(activity); // start the first session activityHandler.trackSubsessionStart(); // wait enough to be a new subsession, but not a new session SystemClock.sleep(1500); activityHandler.trackSubsessionStart(); // wait enough to be a new session SystemClock.sleep(4000); activityHandler.trackSubsessionStart(); // test the subsession end activityHandler.trackSubsessionEnd(); SystemClock.sleep(1000); // check that a new subsession was created assertTrue( mockLogger.toString(), mockLogger.containsMessage(LogLevel.INFO, "Started subsession 2 of session 1")); // check that it's now on the 2nd session assertTrue(mockLogger.toString(), mockLogger.containsMessage(LogLevel.DEBUG, "Session 2")); // check that 2 packages were added to the package handler assertEquals(2, mockPackageHandler.queue.size()); // get the second session package and its parameters ActivityPackage activityPackage = mockPackageHandler.queue.get(1); Map<String, String> parameters = activityPackage.getParameters(); // the session and subsession count should be 2 assertEquals( activityPackage.getExtendedString(), 2, Integer.parseInt(parameters.get("session_count"))); assertEquals( activityPackage.getExtendedString(), 2, Integer.parseInt(parameters.get("subsession_count"))); // TODO test updated timeSpent and sessionLenght mockLogger.test("timeSpent " + parameters.get("time_spent")); mockLogger.test("sessionLength " + parameters.get("session_length")); // check that the package handler was paused assertTrue( mockLogger.toString(), mockLogger.containsTestMessage("PackageHandler pauseSending")); }
@Override protected void tearDown() throws Exception { super.tearDown(); AdjustFactory.setPackageHandler(null); AdjustFactory.setLogger(null); AdjustFactory.setTimerInterval(-1); AdjustFactory.setSessionInterval(-1); AdjustFactory.setSubsessionInterval(-1); }