Search in sources :

Example 31 with Channel

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

the class PushTest method nullTokenOnStartThenRefresh.

@Test
public void nullTokenOnStartThenRefresh() {
    /* Start. */
    String testToken = "TEST";
    Push push = Push.getInstance();
    Channel channel = mock(Channel.class);
    start(mock(Context.class), push, channel);
    assertTrue(Push.isEnabled().get());
    verify(mFirebaseInstanceId).getToken();
    verify(channel, never()).enqueue(any(PushInstallationLog.class), eq(push.getGroupName()));
    /* Refresh. */
    push.onTokenRefresh(testToken);
    verify(channel).enqueue(any(PushInstallationLog.class), eq(push.getGroupName()));
    /* Only once. */
    verify(mFirebaseInstanceId).getToken();
}
Also used : Context(android.content.Context) Channel(com.microsoft.appcenter.channel.Channel) PushInstallationLog(com.microsoft.appcenter.push.ingestion.models.PushInstallationLog) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 32 with Channel

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

the class PushTest method clickedFromBackgroundDisableWhilePostingToUI.

@Test
public void clickedFromBackgroundDisableWhilePostingToUI() {
    /* Mock activity to contain push */
    PushListener pushListener = mock(PushListener.class);
    Push.setListener(pushListener);
    Context contextMock = mock(Context.class);
    Push push = Push.getInstance();
    Channel channel = mock(Channel.class);
    start(contextMock, push, channel);
    Activity activity = mock(Activity.class);
    Intent intent = createPushIntent(null, null, null);
    when(PushIntentUtils.getGoogleMessageId(intent)).thenReturn("some id");
    when(activity.getIntent()).thenReturn(intent);
    /* Disable while posting the command to the U.I. thread. */
    activity = mock(Activity.class);
    when(activity.getIntent()).thenReturn(intent);
    final AtomicReference<Runnable> runnable = new AtomicReference<>();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            runnable.set((Runnable) invocation.getArguments()[0]);
            return null;
        }
    }).when(HandlerUtils.class);
    HandlerUtils.runOnUiThread(any(Runnable.class));
    push.onActivityResumed(activity);
    Push.setEnabled(false);
    runnable.get().run();
    ArgumentCaptor<PushNotification> captor = ArgumentCaptor.forClass(PushNotification.class);
    verify(pushListener, never()).onPushNotificationReceived(eq(activity), captor.capture());
}
Also used : Context(android.content.Context) Channel(com.microsoft.appcenter.channel.Channel) Activity(android.app.Activity) Intent(android.content.Intent) AtomicReference(java.util.concurrent.atomic.AtomicReference) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 33 with Channel

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

the class PushTest method verifyEnableFirebaseAnalytics.

@SuppressWarnings("deprecation")
@Test
public void verifyEnableFirebaseAnalytics() throws FirebaseUtils.FirebaseUnavailableException {
    Context contextMock = mock(Context.class);
    Push push = Push.getInstance();
    Channel channel = mock(Channel.class);
    start(contextMock, push, channel);
    verify(mFirebaseAnalyticsInstance).setAnalyticsCollectionEnabled(false);
    Push.enableFirebaseAnalytics(contextMock);
    verify(mFirebaseAnalyticsInstance).setAnalyticsCollectionEnabled(false);
}
Also used : Context(android.content.Context) Channel(com.microsoft.appcenter.channel.Channel) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 34 with Channel

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

the class PushTest method clickedFromBackground.

@Test
public void clickedFromBackground() {
    PushListener pushListener = mock(PushListener.class);
    Push.setListener(pushListener);
    Context contextMock = mock(Context.class);
    Push push = Push.getInstance();
    Channel channel = mock(Channel.class);
    start(contextMock, push, channel);
    /* Mock activity to contain push */
    Activity activity = mock(Activity.class);
    Map<String, String> customMap = new HashMap<>();
    customMap.put("custom", "data");
    customMap.put("b", "c");
    Intent intent = createPushIntent(null, null, customMap);
    when(PushIntentUtils.getGoogleMessageId(intent)).thenReturn("reserved value by google");
    when(activity.getIntent()).thenReturn(intent);
    /* Simulate we detect push in onCreate. */
    push.onActivityCreated(activity, null);
    ArgumentCaptor<PushNotification> captor = ArgumentCaptor.forClass(PushNotification.class);
    verify(pushListener).onPushNotificationReceived(eq(activity), captor.capture());
    PushNotification pushNotification = captor.getValue();
    assertNotNull(pushNotification);
    assertNull(pushNotification.getTitle());
    assertNull(pushNotification.getMessage());
    assertEquals(customMap, pushNotification.getCustomData());
    /* On started on resume will not duplicate the callback. */
    push.onActivityStarted(activity);
    push.onActivityResumed(activity);
    verify(pushListener).onPushNotificationReceived(eq(activity), captor.capture());
    /* Disable SDK stops callbacks. */
    push.onActivityPaused(activity);
    activity = mock(Activity.class);
    when(activity.getIntent()).thenReturn(intent);
    when(PushIntentUtils.getGoogleMessageId(intent)).thenReturn("new id");
    Push.setEnabled(false);
    push.onActivityResumed(activity);
    verify(pushListener, never()).onPushNotificationReceived(eq(activity), captor.capture());
    verifyStatic(never());
    AppCenterLog.error(anyString(), anyString());
    /* Same effect if we disable App Center. */
    when(mBooleanAppCenterFuture.get()).thenReturn(false);
    push.onActivityResumed(activity);
    verify(pushListener, never()).onPushNotificationReceived(eq(activity), captor.capture());
    verifyStatic(never());
    AppCenterLog.error(anyString(), anyString());
    /* Same if we remove listener. */
    when(mBooleanAppCenterFuture.get()).thenReturn(true);
    Push.setEnabled(true);
    Push.setListener(null);
    push.onActivityResumed(activity);
    verify(pushListener, never()).onPushNotificationReceived(eq(activity), captor.capture());
    /* Set listener to read the new push when resumed. */
    Push.setListener(pushListener);
    push.onActivityResumed(activity);
    verify(pushListener).onPushNotificationReceived(eq(activity), captor.capture());
    /* If intent extras are null, nothing happens. */
    activity = mock(Activity.class);
    when(activity.getIntent()).thenReturn(intent);
    push.onActivityResumed(activity);
    verify(pushListener, never()).onPushNotificationReceived(eq(activity), captor.capture());
    /* If intent contains non push extras, same thing. */
    when(intent.getExtras()).thenReturn(mock(Bundle.class));
    push.onActivityResumed(activity);
    verify(pushListener, never()).onPushNotificationReceived(eq(activity), captor.capture());
    /* Receiving push with the same id as first push should do nothing. */
    when(PushIntentUtils.getGoogleMessageId(intent)).thenReturn("a new id");
    push.onActivityResumed(activity);
    when(PushIntentUtils.getGoogleMessageId(intent)).thenReturn("reserved value by google");
    push.onActivityResumed(activity);
    verify(pushListener, times(1)).onPushNotificationReceived(eq(activity), any(PushNotification.class));
}
Also used : Context(android.content.Context) HashMap(java.util.HashMap) Bundle(android.os.Bundle) Channel(com.microsoft.appcenter.channel.Channel) Activity(android.app.Activity) Intent(android.content.Intent) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 35 with Channel

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

the class AbstractAppCenterServiceTest method onChannelReadyEnabledThenDisable.

@Test
public void onChannelReadyEnabledThenDisable() {
    Channel channel = mock(Channel.class);
    mService.onStarted(mock(Context.class), "", channel);
    verify(channel).removeGroup(mService.getGroupName());
    verify(channel).addGroup(mService.getGroupName(), mService.getTriggerCount(), mService.getTriggerInterval(), mService.getTriggerMaxParallelRequests(), mService.getChannelListener());
    verifyNoMoreInteractions(channel);
    assertSame(channel, mService.mChannel);
    mService.setInstanceEnabled(false);
    verify(channel, times(2)).removeGroup(mService.getGroupName());
    verify(channel).clear(mService.getGroupName());
    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)

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