Search in sources :

Example 1 with Log

use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.

the class AnalyticsAndroidTest method testAnalyticsListener.

@Test
public void testAnalyticsListener() {
    AnalyticsListener analyticsListener = mock(AnalyticsListener.class);
    Analytics.setListener(analyticsListener);
    Channel channel = mock(Channel.class);
    Analytics.getInstance().onStarted(sContext, "", channel);
    Analytics.trackEvent("event");
    /* First process: enqueue log but network is down... */
    final EventLog log = new EventLog();
    log.setId(randomUUID());
    log.setName("name");
    Analytics.unsetInstance();
    Analytics.setListener(analyticsListener);
    verify(channel).enqueue(any(Log.class), anyString());
    verifyNoMoreInteractions(analyticsListener);
    /* Second process: sending succeeds. */
    final AtomicReference<Channel.GroupListener> groupListener = new AtomicReference<>();
    channel = mock(Channel.class);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Channel.GroupListener listener = (Channel.GroupListener) invocationOnMock.getArguments()[4];
            groupListener.set(listener);
            listener.onBeforeSending(log);
            return null;
        }
    }).when(channel).addGroup(anyString(), anyInt(), anyInt(), anyInt(), any(Channel.GroupListener.class));
    Analytics.unsetInstance();
    Analytics.setListener(analyticsListener);
    Analytics.getInstance().onStarted(sContext, "", channel);
    assertNotNull(groupListener.get());
    groupListener.get().onSuccess(log);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    verify(analyticsListener).onBeforeSending(any(EventLog.class));
    verify(analyticsListener).onSendingSucceeded(any(EventLog.class));
    verifyNoMoreInteractions(analyticsListener);
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) AnalyticsListener(com.microsoft.azure.mobile.analytics.channel.AnalyticsListener) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Channel(com.microsoft.azure.mobile.channel.Channel) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 2 with Log

use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.

the class SessionTrackerTest method goBackgroundAndComeBackMuchLater.

@Test
public void goBackgroundAndComeBackMuchLater() {
    /* 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.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertNotNull(log.getSid());
        expectedSid = log.getSid();
        expectedStartSessionLog.setSid(expectedSid);
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* Go background. */
    spendTime(1);
    mSessionTracker.onActivityPaused();
    /* Come back after a long time. */
    spendTime(30000);
    mSessionTracker.onActivityResumed();
    /* Send a log again: new session. */
    {
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertNotEquals(expectedSid, log.getSid());
        expectedSid = log.getSid();
        expectedStartSessionLog.setSid(expectedSid);
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
}
Also used : StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) StartServiceLog(com.microsoft.azure.mobile.ingestion.models.StartServiceLog) StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with Log

use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.

the class SessionTrackerTest method sdkConfiguredBetweenPauseAndResume.

@Test
public void sdkConfiguredBetweenPauseAndResume() {
    /* Pause application before we saw the first resume event (integration problem). We are handling that gracefully though. */
    mSessionTracker.onActivityPaused();
    /* Application is in background, send a log, verify decoration. */
    UUID expectedSid;
    StartSessionLog expectedStartSessionLog = new StartSessionLog();
    {
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertNotNull(log.getSid());
        expectedSid = log.getSid();
        expectedStartSessionLog.setSid(expectedSid);
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* Verify session reused for second log. */
    {
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(expectedSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* No usage from background for a long time: new session. */
    {
        spendTime(30000);
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertNotEquals(expectedSid, log.getSid());
        expectedSid = log.getSid();
        expectedStartSessionLog.setSid(expectedSid);
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* App comes to foreground and sends a log, we were in background for a long time but we sent a log recently, still session. */
    {
        mSessionTracker.onActivityResumed();
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(expectedSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
    /* We are in foreground, even after timeout a log is still in session. */
    {
        spendTime(30000);
        Log log = newEvent();
        mSessionTracker.onEnqueuingLog(log, TEST_GROUP);
        mSessionTracker.onEnqueuingLog(expectedStartSessionLog, TEST_GROUP);
        assertEquals(expectedSid, log.getSid());
        verify(mChannel).enqueue(expectedStartSessionLog, TEST_GROUP);
    }
}
Also used : StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) StartServiceLog(com.microsoft.azure.mobile.ingestion.models.StartServiceLog) StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) UUID(java.util.UUID) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with Log

use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.

the class CrashesAndroidTest method testNoDuplicateCallbacksOrSending.

@Test
public void testNoDuplicateCallbacksOrSending() throws InterruptedException {
    /* Crash on 1st process. */
    assertFalse(Crashes.hasCrashedInLastSession());
    android.util.Log.i(TAG, "Process 1");
    Thread.UncaughtExceptionHandler uncaughtExceptionHandler = mock(Thread.UncaughtExceptionHandler.class);
    Thread.setDefaultUncaughtExceptionHandler(uncaughtExceptionHandler);
    Channel channel = mock(Channel.class);
    Crashes.getInstance().onStarted(sContext, "", channel);
    CrashesListener crashesListener = mock(CrashesListener.class);
    when(crashesListener.shouldProcess(any(ErrorReport.class))).thenReturn(true);
    when(crashesListener.shouldAwaitUserConfirmation()).thenReturn(true);
    Crashes.setListener(crashesListener);
    final Error exception = generateStackOverflowError();
    assertTrue(exception.getStackTrace().length > ErrorLogHelper.FRAME_LIMIT);
    final Thread thread = new Thread() {

        @Override
        public void run() {
            throw exception;
        }
    };
    thread.start();
    thread.join();
    assertEquals(ErrorLogHelper.FRAME_LIMIT, exception.getStackTrace().length);
    verify(uncaughtExceptionHandler).uncaughtException(thread, exception);
    assertEquals(2, ErrorLogHelper.getErrorStorageDirectory().listFiles().length);
    verifyZeroInteractions(crashesListener);
    /* Second process: enqueue log but network is down... */
    android.util.Log.i(TAG, "Process 2");
    final AtomicReference<Log> log = new AtomicReference<>();
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            log.set((Log) invocationOnMock.getArguments()[0]);
            return null;
        }
    }).when(channel).enqueue(any(Log.class), anyString());
    Crashes.unsetInstance();
    Crashes.setListener(crashesListener);
    Crashes.getInstance().onStarted(sContext, "", channel);
    waitForCrashesHandlerTasksToComplete();
    /* Check last session error report. */
    assertTrue(Crashes.hasCrashedInLastSession());
    Crashes.getLastSessionCrashReport(new ResultCallback<ErrorReport>() {

        @Override
        public void onResult(ErrorReport errorReport) {
            assertNotNull(errorReport);
            Throwable lastThrowable = errorReport.getThrowable();
            assertTrue(lastThrowable instanceof StackOverflowError);
            assertEquals(ErrorLogHelper.FRAME_LIMIT, lastThrowable.getStackTrace().length);
        }
    });
    /* Waiting user confirmation so no log sent yet. */
    verify(channel, never()).enqueue(any(Log.class), anyString());
    assertEquals(2, ErrorLogHelper.getErrorStorageDirectory().listFiles().length);
    verify(crashesListener).shouldProcess(any(ErrorReport.class));
    verify(crashesListener).shouldAwaitUserConfirmation();
    verifyNoMoreInteractions(crashesListener);
    /* Confirm to resume processing. */
    Crashes.notifyUserConfirmation(Crashes.ALWAYS_SEND);
    verify(channel).enqueue(any(Log.class), anyString());
    assertNotNull(log.get());
    assertEquals(1, ErrorLogHelper.getErrorStorageDirectory().listFiles().length);
    verify(crashesListener).getErrorAttachments(any(ErrorReport.class));
    verifyNoMoreInteractions(crashesListener);
    /* Third process: sending succeeds. */
    android.util.Log.i(TAG, "Process 3");
    final AtomicReference<Channel.GroupListener> groupListener = new AtomicReference<>();
    channel = mock(Channel.class);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            Channel.GroupListener listener = (Channel.GroupListener) invocationOnMock.getArguments()[4];
            groupListener.set(listener);
            listener.onBeforeSending(log.get());
            return null;
        }
    }).when(channel).addGroup(anyString(), anyInt(), anyInt(), anyInt(), any(Channel.GroupListener.class));
    Crashes.unsetInstance();
    Crashes.setListener(crashesListener);
    Crashes.getInstance().onStarted(sContext, "", channel);
    waitForCrashesHandlerTasksToComplete();
    assertFalse(Crashes.hasCrashedInLastSession());
    Crashes.getLastSessionCrashReport(new ResultCallback<ErrorReport>() {

        @Override
        public void onResult(ErrorReport errorReport) {
            assertNull(errorReport);
        }
    });
    assertNotNull(groupListener.get());
    groupListener.get().onSuccess(log.get());
    waitForCrashesHandlerTasksToComplete();
    assertEquals(0, ErrorLogHelper.getErrorStorageDirectory().listFiles().length);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    verify(crashesListener).onBeforeSending(any(ErrorReport.class));
    verify(crashesListener).onSendingSucceeded(any(ErrorReport.class));
    verifyNoMoreInteractions(crashesListener);
    /* Verify log was truncated to 256 frames. */
    assertTrue(log.get() instanceof ManagedErrorLog);
    ManagedErrorLog errorLog = (ManagedErrorLog) log.get();
    assertNotNull(errorLog.getException());
    assertNotNull(errorLog.getException().getFrames());
    assertEquals(ErrorLogHelper.FRAME_LIMIT, errorLog.getException().getFrames().size());
}
Also used : MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Channel(com.microsoft.azure.mobile.channel.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 5 with Log

use of com.microsoft.azure.mobile.ingestion.models.Log in project mobile-center-sdk-android by Microsoft.

the class AnalyticsTest method testTrackEvent.

@Test
public void testTrackEvent() {
    Analytics analytics = Analytics.getInstance();
    Channel channel = mock(Channel.class);
    analytics.onStarted(mock(Context.class), "", channel);
    Analytics.trackEvent(null, null);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackEvent("", null);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackEvent(" ", null);
    verify(channel, times(1)).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackEvent(generateString(257, '*'), null);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackEvent(generateString(256, '*'), null);
    verify(channel, times(1)).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackEvent("eventName", new HashMap<String, String>() {

        {
            put(null, null);
            put("", null);
            put(generateString(65, '*'), null);
            put("1", null);
            put("2", generateString(65, '*'));
        }
    });
    verify(channel, times(1)).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof EventLog) {
                EventLog eventLog = (EventLog) item;
                return eventLog.getProperties().size() == 0;
            }
            return false;
        }
    }), anyString());
    reset(channel);
    final String validMapItem = "valid";
    Analytics.trackEvent("eventName", new HashMap<String, String>() {

        {
            for (int i = 0; i < 10; i++) {
                put(validMapItem + i, validMapItem);
            }
        }
    });
    verify(channel, times(1)).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof EventLog) {
                EventLog eventLog = (EventLog) item;
                return eventLog.getProperties().size() == 5;
            }
            return false;
        }
    }), anyString());
}
Also used : Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) PageLog(com.microsoft.azure.mobile.analytics.ingestion.models.PageLog) StartSessionLog(com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) Channel(com.microsoft.azure.mobile.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.azure.mobile.test.TestUtils.generateString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Log (com.microsoft.azure.mobile.ingestion.models.Log)59 Test (org.junit.Test)44 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)37 ArrayList (java.util.ArrayList)24 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 Context (android.content.Context)18 UUID (java.util.UUID)18 EventLog (com.microsoft.azure.mobile.analytics.ingestion.models.EventLog)13 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)13 StartSessionLog (com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog)12 LogContainer (com.microsoft.azure.mobile.ingestion.models.LogContainer)12 DefaultLogSerializer (com.microsoft.azure.mobile.ingestion.models.json.DefaultLogSerializer)11 Persistence (com.microsoft.azure.mobile.persistence.Persistence)11 Matchers.anyString (org.mockito.Matchers.anyString)10 IOException (java.io.IOException)9 Channel (com.microsoft.azure.mobile.channel.Channel)8 IngestionHttp (com.microsoft.azure.mobile.ingestion.IngestionHttp)8 StartServiceLog (com.microsoft.azure.mobile.ingestion.models.StartServiceLog)8 JSONException (org.json.JSONException)8 MediumTest (android.support.test.filters.MediumTest)7