use of com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog in project AppCenter-SDK-Android by Microsoft.
the class SessionTrackerTest method startSessionWithoutLogs.
@Test
public void startSessionWithoutLogs() {
final AtomicReference<StartSessionLog> startSessionLog = new AtomicReference<>();
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
startSessionLog.set((StartSessionLog) invocation.getArguments()[0]);
return null;
}
}).when(mChannel).enqueue(notNull(StartSessionLog.class), eq(TEST_GROUP));
/* Go foreground, start session is sent. */
mSessionTracker.onActivityResumed();
verify(mChannel, times(1)).enqueue(notNull(StartSessionLog.class), eq(TEST_GROUP));
assertNotNull(startSessionLog.get());
UUID sid = startSessionLog.get().getSid();
assertNotNull(sid);
/* Change screen after a long time, session reused. */
spendTime(30000);
mSessionTracker.onActivityPaused();
spendTime(1);
mSessionTracker.onActivityResumed();
verify(mChannel, times(1)).enqueue(notNull(StartSessionLog.class), eq(TEST_GROUP));
/* Go background and come back after timeout, second session. */
spendTime(1);
mSessionTracker.onActivityPaused();
spendTime(30000);
mSessionTracker.onActivityResumed();
verify(mChannel, times(2)).enqueue(notNull(StartSessionLog.class), eq(TEST_GROUP));
assertNotEquals(sid, startSessionLog.get().getSid());
}
use of com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog in project AppCenter-SDK-Android by Microsoft.
the class SessionTracker method onEnqueuingLog.
@Override
public void onEnqueuingLog(@NonNull Log log, @NonNull String groupName) {
/*
* Since we enqueue start session logs, skip them to avoid infinite loop.
* Also skip start service log as it's always sent and should not trigger a session.
*/
if (log instanceof StartSessionLog || log instanceof StartServiceLog) {
return;
}
/*
* If the log has already specified a timestamp, try correlating with a past session.
* Note that it can also find the current session but that's ok: in that case that means
* its a log that will be associated to current session but won't trigger expiration logic.
*/
Date timestamp = log.getTimestamp();
if (timestamp != null) {
SessionContext.SessionInfo pastSession = SessionContext.getInstance().getSessionAt(timestamp.getTime());
if (pastSession != null) {
log.setSid(pastSession.getSessionId());
}
} else /* If the log does not have a timestamp yet, then we just correlate with current session. */
{
/* Send a new start session log if needed. */
sendStartSessionIfNeeded();
/* Set current session identifier. */
log.setSid(mSid);
/* Record queued time only if the log is using current session. */
mLastQueuedLogTime = SystemClock.elapsedRealtime();
}
}
use of com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog in project mobile-center-sdk-android by Microsoft.
the class AnalyticsTest method startSessionAfterUserApproval.
@Test
public void startSessionAfterUserApproval() {
/*
* Disable analytics while in background to set up the initial condition
* simulating the opt-in use case.
*/
Analytics analytics = Analytics.getInstance();
Channel channel = mock(Channel.class);
analytics.onStarting(mAppCenterHandler);
analytics.onStarted(mock(Context.class), channel, "", null, true);
Analytics.setEnabled(false);
/* App in foreground: no log yet, we are disabled. */
analytics.onActivityResumed(new Activity());
verify(channel, never()).enqueue(any(Log.class), eq(analytics.getGroupName()), anyInt());
/* Enable: start session sent retroactively. */
Analytics.setEnabled(true);
verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {
@Override
public boolean matches(Object argument) {
return argument instanceof StartSessionLog;
}
}), eq(analytics.getGroupName()), eq(DEFAULTS));
verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {
@Override
public boolean matches(Object argument) {
return argument instanceof PageLog;
}
}), eq(analytics.getGroupName()), eq(DEFAULTS));
/* Go background. */
analytics.onActivityPaused(new Activity());
/* Disable/enable: nothing happens on background. */
Analytics.setEnabled(false);
Analytics.setEnabled(true);
/* No additional log. */
verify(channel, times(2)).enqueue(any(Log.class), eq(analytics.getGroupName()), eq(DEFAULTS));
}
use of com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog in project mobile-center-sdk-android by Microsoft.
the class AnalyticsTest method disableAutomaticPageTracking.
@Test
public void disableAutomaticPageTracking() {
Analytics analytics = Analytics.getInstance();
assertTrue(Analytics.isAutoPageTrackingEnabled());
Analytics.setAutoPageTrackingEnabled(false);
assertFalse(Analytics.isAutoPageTrackingEnabled());
Channel channel = mock(Channel.class);
analytics.onStarting(mAppCenterHandler);
analytics.onStarted(mock(Context.class), channel, "", null, true);
analytics.onActivityResumed(new MyActivity());
verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {
@Override
public boolean matches(Object argument) {
return argument instanceof StartSessionLog;
}
}), anyString(), eq(DEFAULTS));
verify(channel, never()).enqueue(argThat(new ArgumentMatcher<Log>() {
@Override
public boolean matches(Object argument) {
return argument instanceof PageLog;
}
}), anyString(), anyInt());
Analytics.setAutoPageTrackingEnabled(true);
assertTrue(Analytics.isAutoPageTrackingEnabled());
analytics.onActivityResumed(new SomeScreen());
verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {
@Override
public boolean matches(Object item) {
if (item instanceof PageLog) {
PageLog pageLog = (PageLog) item;
return "SomeScreen".equals(pageLog.getName());
}
return false;
}
}), eq(analytics.getGroupName()), eq(DEFAULTS));
}
use of com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog in project mobile-center-sdk-android by Microsoft.
the class SessionTrackerTest method stayOnFirstScreenForLong.
@Test
public void stayOnFirstScreenForLong() {
/* Application is in foreground, send a log, verify decoration with a new session. */
mSessionTracker.onActivityResumed();
UUID expectedSid;
StartSessionLog expectedStartSessionLog = new StartSessionLog();
{
Log log = newEvent();
mSessionTracker.onPreparingLog(log, TEST_GROUP);
mSessionTracker.onPreparingLog(expectedStartSessionLog, TEST_GROUP);
assertNotNull(log.getSid());
expectedSid = log.getSid();
expectedStartSessionLog.setSid(expectedSid);
verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP, DEFAULTS);
}
/* Wait a long time. */
spendTime(30000);
/* Go to another activity. */
mSessionTracker.onActivityPaused();
spendTime(2);
mSessionTracker.onActivityResumed();
/* Send a log again: session must be reused. */
{
Log log = newEvent();
mSessionTracker.onPreparingLog(log, TEST_GROUP);
mSessionTracker.onPreparingLog(expectedStartSessionLog, TEST_GROUP);
assertEquals(expectedSid, log.getSid());
verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP, DEFAULTS);
}
}
Aggregations