Search in sources :

Example 1 with Channel

use of com.microsoft.appcenter.channel.Channel in project mobile-center-sdk-android by Microsoft.

the class AnalyticsTest method startSessionAfterUserApprovalWeakReference.

@Test
public void startSessionAfterUserApprovalWeakReference() {
    /*
         * 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);
    Analytics.setEnabled(false);
    /* App in foreground: no log yet, we are disabled. */
    analytics.onActivityResumed(new Activity());
    analytics.getCurrentActivity().clear();
    verify(channel, never()).enqueue(any(Log.class), eq(analytics.getGroupName()));
    /* Enable: start session not sent retroactively, weak reference lost. */
    Analytics.setEnabled(true);
    verify(channel, never()).enqueue(any(Log.class), eq(analytics.getGroupName()));
}
Also used : Context(android.content.Context) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) Channel(com.microsoft.appcenter.channel.Channel) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with Channel

use of com.microsoft.appcenter.channel.Channel in project mobile-center-sdk-android by Microsoft.

the class AnalyticsTest method testTrackPage.

@Test
public void testTrackPage() {
    Analytics analytics = Analytics.getInstance();
    Channel channel = mock(Channel.class);
    analytics.onStarting(mAppCenterHandler);
    analytics.onStarted(mock(Context.class), "", channel);
    Analytics.trackPage(null, null);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackPage("", null);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackPage(" ", null);
    verify(channel, times(1)).enqueue(any(Log.class), anyString());
    reset(channel);
    final String maxName = generateString(Analytics.MAX_NAME_LENGTH, '*');
    Analytics.trackPage(maxName + "*", null);
    verify(channel, times(1)).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof PageLog) {
                PageLog pageLog = (PageLog) item;
                return pageLog.getName().equals(maxName) && pageLog.getProperties() == null;
            }
            return false;
        }
    }), anyString());
    reset(channel);
    Analytics.trackPage(maxName, null);
    verify(channel, times(1)).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackPage("pageName", new HashMap<String, String>() {

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

        @Override
        public boolean matches(Object item) {
            if (item instanceof PageLog) {
                PageLog pageLog = (PageLog) item;
                return pageLog.getProperties().size() == 0;
            }
            return false;
        }
    }), anyString());
    reset(channel);
    final String validMapItem = "valid";
    Analytics.trackPage("pageName", 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 PageLog) {
                PageLog pageLog = (PageLog) item;
                return pageLog.getProperties().size() == 5;
            }
            return false;
        }
    }), anyString());
    reset(channel);
    final String longerMapItem = generateString(Analytics.MAX_PROPERTY_ITEM_LENGTH + 1, '*');
    Analytics.trackPage("pageName", new HashMap<String, String>() {

        {
            put(longerMapItem, longerMapItem);
        }
    });
    verify(channel, times(1)).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof PageLog) {
                PageLog pageLog = (PageLog) item;
                if (pageLog.getProperties().size() == 1) {
                    Map.Entry<String, String> entry = pageLog.getProperties().entrySet().iterator().next();
                    String truncatedMapItem = generateString(Analytics.MAX_PROPERTY_ITEM_LENGTH, '*');
                    return entry.getKey().length() == Analytics.MAX_PROPERTY_ITEM_LENGTH && entry.getValue().length() == Analytics.MAX_PROPERTY_ITEM_LENGTH;
                }
            }
            return false;
        }
    }), anyString());
}
Also used : Context(android.content.Context) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) Channel(com.microsoft.appcenter.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) Map(java.util.Map) HashMap(java.util.HashMap) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with Channel

use of com.microsoft.appcenter.channel.Channel 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.onStarting(mAppCenterHandler);
    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);
    final String maxName = generateString(Analytics.MAX_NAME_LENGTH, '*');
    Analytics.trackEvent(maxName + "*", null);
    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.getName().equals(maxName) && eventLog.getProperties() == null;
            }
            return false;
        }
    }), anyString());
    reset(channel);
    Analytics.trackEvent(maxName, 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(Analytics.MAX_PROPERTY_ITEM_LENGTH + 1, '*'), null);
            put("1", null);
        }
    });
    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());
    reset(channel);
    final String longerMapItem = generateString(Analytics.MAX_PROPERTY_ITEM_LENGTH + 1, '*');
    Analytics.trackEvent("eventName", new HashMap<String, String>() {

        {
            put(longerMapItem, longerMapItem);
        }
    });
    verify(channel, times(1)).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof EventLog) {
                EventLog eventLog = (EventLog) item;
                if (eventLog.getProperties().size() == 1) {
                    Map.Entry<String, String> entry = eventLog.getProperties().entrySet().iterator().next();
                    String truncatedMapItem = generateString(Analytics.MAX_PROPERTY_ITEM_LENGTH, '*');
                    return entry.getKey().length() == Analytics.MAX_PROPERTY_ITEM_LENGTH && entry.getValue().length() == Analytics.MAX_PROPERTY_ITEM_LENGTH;
                }
            }
            return false;
        }
    }), anyString());
}
Also used : Context(android.content.Context) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) Channel(com.microsoft.appcenter.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher) EventLog(com.microsoft.appcenter.analytics.ingestion.models.EventLog) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.appcenter.test.TestUtils.generateString) Map(java.util.Map) HashMap(java.util.HashMap) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with Channel

use of com.microsoft.appcenter.channel.Channel in project mobile-center-sdk-android by Microsoft.

the class AnalyticsTest method activityResumed.

private void activityResumed(final String expectedName, android.app.Activity activity) {
    /*
         * Before start, calling onActivityResume is ignored.
         * In reality it never happens, it means someone is messing with internals directly.
         */
    Analytics analytics = Analytics.getInstance();
    analytics.onActivityResumed(new Activity());
    assertNull(analytics.getCurrentActivity());
    verifyStatic();
    AppCenterLog.error(anyString(), anyString());
    analytics.onActivityPaused(new Activity());
    verifyStatic(times(2));
    AppCenterLog.error(anyString(), anyString());
    /* Start. */
    Channel channel = mock(Channel.class);
    analytics.onStarting(mAppCenterHandler);
    analytics.onStarted(mock(Context.class), "", channel);
    /* Test resume/pause. */
    analytics.onActivityResumed(activity);
    analytics.onActivityPaused(activity);
    verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof PageLog) {
                PageLog pageLog = (PageLog) item;
                return expectedName.equals(pageLog.getName());
            }
            return false;
        }
    }), eq(analytics.getGroupName()));
}
Also used : Context(android.content.Context) PageLog(com.microsoft.appcenter.analytics.ingestion.models.PageLog) Channel(com.microsoft.appcenter.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher)

Example 5 with Channel

use of com.microsoft.appcenter.channel.Channel in project mobile-center-sdk-android by Microsoft.

the class AnalyticsTest method disablePersisted.

@Test
public void disablePersisted() {
    when(StorageHelper.PreferencesStorage.getBoolean(ANALYTICS_ENABLED_KEY, true)).thenReturn(false);
    Analytics analytics = Analytics.getInstance();
    /* Start. */
    Channel channel = mock(Channel.class);
    analytics.onStarting(mAppCenterHandler);
    analytics.onStarted(mock(Context.class), "", channel);
    verify(channel, never()).removeListener(any(Channel.Listener.class));
    verify(channel, never()).addListener(any(Channel.Listener.class));
}
Also used : Context(android.content.Context) AnalyticsListener(com.microsoft.appcenter.analytics.channel.AnalyticsListener) Channel(com.microsoft.appcenter.channel.Channel) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Channel (com.microsoft.appcenter.channel.Channel)35 Context (android.content.Context)33 Test (org.junit.Test)32 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)32 SessionContext (com.microsoft.appcenter.SessionContext)14 Log (com.microsoft.appcenter.ingestion.models.Log)14 AppCenterLog (com.microsoft.appcenter.utils.AppCenterLog)14 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)13 ErrorAttachmentLog (com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog)11 File (java.io.File)11 LogSerializer (com.microsoft.appcenter.ingestion.models.json.LogSerializer)10 HandledErrorLog (com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog)9 ArgumentMatcher (org.mockito.ArgumentMatcher)9 PageLog (com.microsoft.appcenter.analytics.ingestion.models.PageLog)8 Matchers.anyString (org.mockito.Matchers.anyString)8 StartSessionLog (com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog)7 ErrorReport (com.microsoft.appcenter.crashes.model.ErrorReport)7 EventLog (com.microsoft.appcenter.analytics.ingestion.models.EventLog)6 HashMap (java.util.HashMap)6 UUID (java.util.UUID)6