Search in sources :

Example 16 with Channel

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

the class AbstractAppCenterServiceTest method onChannelReadyDisabledThenEnable.

@Test
public void onChannelReadyDisabledThenEnable() {
    Channel channel = mock(Channel.class);
    mService.onStarted(mock(Context.class), "", channel);
    verify(channel).removeGroup(mService.getGroupName());
    verify(channel).addGroup(eq(mService.getGroupName()), anyInt(), anyLong(), anyInt(), any(Channel.GroupListener.class));
    mService.setInstanceEnabled(false);
    verify(channel, times(2)).removeGroup(mService.getGroupName());
    verify(channel).clear(mService.getGroupName());
    verifyNoMoreInteractions(channel);
    assertSame(channel, mService.mChannel);
    mService.setInstanceEnabled(true);
    verify(channel, times(2)).addGroup(mService.getGroupName(), mService.getTriggerCount(), mService.getTriggerInterval(), mService.getTriggerMaxParallelRequests(), mService.getChannelListener());
    verifyNoMoreInteractions(channel);
}
Also used : Context(android.content.Context) Channel(com.microsoft.appcenter.channel.Channel) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 17 with Channel

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

the class AbstractAppCenterServiceTest method optionalGroup.

@Test
public void optionalGroup() {
    mService = new AbstractAppCenterService() {

        @Override
        protected String getGroupName() {
            return null;
        }

        @Override
        public String getServiceName() {
            return "Test";
        }

        @Override
        protected String getLoggerTag() {
            return "TestLog";
        }
    };
    Channel channel = mock(Channel.class);
    mService.onStarted(mock(Context.class), "", channel);
    mService.setInstanceEnabled(false);
    mService.setInstanceEnabled(true);
    verifyZeroInteractions(channel);
}
Also used : Context(android.content.Context) Channel(com.microsoft.appcenter.channel.Channel) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 18 with Channel

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

the class AnalyticsTest method setEnabled.

@Test
public void setEnabled() throws InterruptedException {
    /* Before start it does not work to change state, it's disabled. */
    Analytics analytics = Analytics.getInstance();
    Analytics.setEnabled(true);
    assertFalse(Analytics.isEnabled().get());
    Analytics.setEnabled(false);
    assertFalse(Analytics.isEnabled().get());
    /* Start. */
    Channel channel = mock(Channel.class);
    analytics.onStarting(mAppCenterHandler);
    analytics.onStarted(mock(Context.class), "", channel);
    verify(channel).removeGroup(eq(analytics.getGroupName()));
    verify(channel).addGroup(eq(analytics.getGroupName()), anyInt(), anyLong(), anyInt(), any(Channel.GroupListener.class));
    verify(channel).addListener(any(Channel.Listener.class));
    /* Now we can see the service enabled. */
    assertTrue(Analytics.isEnabled().get());
    /* Disable. Testing to wait setEnabled to finish while we are at it. */
    Analytics.setEnabled(false).get();
    assertFalse(Analytics.isEnabled().get());
    verify(channel).removeListener(any(SessionTracker.class));
    verify(channel, times(2)).removeGroup(analytics.getGroupName());
    verify(channel).clear(analytics.getGroupName());
    verifyStatic();
    StorageHelper.PreferencesStorage.remove("sessions");
    Analytics.trackEvent("test");
    Analytics.trackPage("test");
    analytics.onActivityResumed(new Activity());
    analytics.onActivityPaused(new Activity());
    verify(channel, never()).enqueue(any(Log.class), eq(analytics.getGroupName()));
    /* Enable again, verify the async behavior of setEnabled with the callback. */
    final CountDownLatch latch = new CountDownLatch(1);
    Analytics.setEnabled(true).thenAccept(new AppCenterConsumer<Void>() {

        @Override
        public void accept(Void aVoid) {
            latch.countDown();
        }
    });
    assertTrue(latch.await(0, TimeUnit.MILLISECONDS));
    assertTrue(Analytics.isEnabled().get());
    /* Test double call to setEnabled true. */
    Analytics.setEnabled(true);
    assertTrue(Analytics.isEnabled().get());
    Analytics.trackEvent("test");
    Analytics.trackPage("test");
    verify(channel, times(2)).enqueue(any(Log.class), eq(analytics.getGroupName()));
    /* Disable again. */
    Analytics.setEnabled(false);
    assertFalse(Analytics.isEnabled().get());
    Analytics.trackEvent("test");
    Analytics.trackPage("test");
    analytics.onActivityResumed(new Activity());
    analytics.onActivityPaused(new Activity());
    /* No more log enqueued. */
    verify(channel, times(2)).enqueue(any(Log.class), eq(analytics.getGroupName()));
}
Also used : Context(android.content.Context) AnalyticsListener(com.microsoft.appcenter.analytics.channel.AnalyticsListener) 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) SessionTracker(com.microsoft.appcenter.analytics.channel.SessionTracker) CountDownLatch(java.util.concurrent.CountDownLatch) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 19 with Channel

use of com.microsoft.appcenter.channel.Channel 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);
    analytics.onActivityResumed(new MyActivity());
    verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof StartSessionLog;
        }
    }), anyString());
    verify(channel, never()).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof PageLog;
        }
    }), anyString());
    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()));
}
Also used : Context(android.content.Context) 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) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 20 with Channel

use of com.microsoft.appcenter.channel.Channel 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);
    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()));
    /* 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()));
    verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof PageLog;
        }
    }), eq(analytics.getGroupName()));
    /* 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()));
}
Also used : Context(android.content.Context) StartSessionLog(com.microsoft.appcenter.analytics.ingestion.models.StartSessionLog) 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) 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