Search in sources :

Example 21 with Channel

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

the class AbstractMobileCenterServiceTest method optionalGroup.

@Test
public void optionalGroup() {
    service = new AbstractMobileCenterService() {

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

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

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

Example 22 with Channel

use of com.microsoft.azure.mobile.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);
    push.onStarted(contextMock, DUMMY_APP_SECRET, channel);
    /* Mock activity to contain push */
    Activity activity = mock(Activity.class);
    Intent intent = mock(Intent.class);
    when(activity.getIntent()).thenReturn(intent);
    Bundle extras = mock(Bundle.class);
    when(intent.getExtras()).thenReturn(extras);
    when(extras.getString(Push.EXTRA_GOOGLE_MESSAGE_ID)).thenReturn("reserved value by google");
    final Map<String, String> extraMap = new HashMap<>();
    for (String key : Push.EXTRA_STANDARD_KEYS) {
        extraMap.put(key, "reserved value by google");
    }
    Map<String, String> customMap = new HashMap<>();
    customMap.put("custom", "data");
    customMap.put("b", "c");
    extraMap.putAll(customMap);
    when(extras.keySet()).thenReturn(extraMap.keySet());
    when(extras.getString(anyString())).then(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            return extraMap.get(invocation.getArguments()[0].toString());
        }
    });
    /* 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(extras.getString(Push.EXTRA_GOOGLE_MESSAGE_ID)).thenReturn("new id");
    Push.setEnabled(false);
    push.onActivityResumed(activity);
    verify(pushListener, never()).onPushNotificationReceived(eq(activity), captor.capture());
    /* Same if we remove listener. */
    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());
}
Also used : Context(android.content.Context) HashMap(java.util.HashMap) Bundle(android.os.Bundle) Channel(com.microsoft.azure.mobile.channel.Channel) Activity(android.app.Activity) Intent(android.content.Intent) Matchers.anyString(org.mockito.Matchers.anyString) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 23 with Channel

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

the class PushTest method onTokenRefresh.

@Test
public void onTokenRefresh() {
    String testToken = "TEST";
    Push push = Mockito.spy(Push.getInstance());
    push.setInstanceEnabled(false);
    Channel channel = mock(Channel.class);
    push.onStarted(mock(Context.class), DUMMY_APP_SECRET, channel);
    verify(mFirebaseInstanceId, never()).getToken();
    /* When token unavailable */
    when(mFirebaseInstanceId.getToken()).thenReturn(null);
    push.setInstanceEnabled(true);
    verify(mFirebaseInstanceId).getToken();
    verify(push, never()).onTokenRefresh(anyString());
    /* When token available */
    when(mFirebaseInstanceId.getToken()).thenReturn(testToken);
    push.setInstanceEnabled(true);
    verify(push).onTokenRefresh(anyString());
    verifyStatic(times(1));
    StorageHelper.PreferencesStorage.putString(eq(PREFERENCE_KEY_PUSH_TOKEN), eq(testToken));
    /* For check enqueue only once */
    push.onTokenRefresh(testToken);
    verify(channel).enqueue(any(PushInstallationLog.class), anyString());
    /* For check resend token on change */
    push.onTokenRefresh("OTHER");
    verifyStatic(times(1));
    StorageHelper.PreferencesStorage.putString(eq(PREFERENCE_KEY_PUSH_TOKEN), eq("OTHER"));
}
Also used : Context(android.content.Context) Channel(com.microsoft.azure.mobile.channel.Channel) PushInstallationLog(com.microsoft.azure.mobile.push.ingestion.models.PushInstallationLog) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 24 with Channel

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

the class PushTest method setEnabled.

@Test
public void setEnabled() {
    String testToken = "TEST";
    Push push = Push.getInstance();
    Channel channel = mock(Channel.class);
    assertTrue(Push.isEnabled());
    Push.setEnabled(true);
    assertTrue(Push.isEnabled());
    Push.setEnabled(false);
    assertFalse(Push.isEnabled());
    push.onStarted(mock(Context.class), DUMMY_APP_SECRET, channel);
    verify(channel).clear(push.getGroupName());
    verify(channel).removeGroup(eq(push.getGroupName()));
    verify(mFirebaseInstanceId, never()).getToken();
    /* If disabled when PushTokenTask executing */
    Push.setEnabled(false);
    push.onTokenRefresh(testToken);
    verify(channel, never()).enqueue(any(PushInstallationLog.class), eq(push.getGroupName()));
    /* For check enqueue only once */
    when(mFirebaseInstanceId.getToken()).thenReturn(testToken);
    Push.setEnabled(true);
    Push.setEnabled(true);
    verify(channel).enqueue(any(PushInstallationLog.class), eq(push.getGroupName()));
}
Also used : Context(android.content.Context) Channel(com.microsoft.azure.mobile.channel.Channel) PushInstallationLog(com.microsoft.azure.mobile.push.ingestion.models.PushInstallationLog) Matchers.anyString(org.mockito.Matchers.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 25 with Channel

use of com.microsoft.azure.mobile.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 optin use case.
         */
    Analytics analytics = Analytics.getInstance();
    Channel channel = mock(Channel.class);
    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.azure.mobile.analytics.ingestion.models.StartSessionLog) 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) PageLog(com.microsoft.azure.mobile.analytics.ingestion.models.PageLog) Channel(com.microsoft.azure.mobile.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Channel (com.microsoft.azure.mobile.channel.Channel)36 Test (org.junit.Test)35 Context (android.content.Context)31 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)30 ManagedErrorLog (com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog)15 Log (com.microsoft.azure.mobile.ingestion.models.Log)14 MobileCenterLog (com.microsoft.azure.mobile.utils.MobileCenterLog)14 File (java.io.File)11 ErrorAttachmentLog (com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog)10 ErrorReport (com.microsoft.azure.mobile.crashes.model.ErrorReport)10 LogSerializer (com.microsoft.azure.mobile.ingestion.models.json.LogSerializer)10 ArgumentMatcher (org.mockito.ArgumentMatcher)10 Matchers.anyString (org.mockito.Matchers.anyString)9 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9 EventLog (com.microsoft.azure.mobile.analytics.ingestion.models.EventLog)7 PageLog (com.microsoft.azure.mobile.analytics.ingestion.models.PageLog)7 StartSessionLog (com.microsoft.azure.mobile.analytics.ingestion.models.StartSessionLog)6 UUID (java.util.UUID)6 Answer (org.mockito.stubbing.Answer)4 TestUtils.generateString (com.microsoft.azure.mobile.test.TestUtils.generateString)3