Search in sources :

Example 31 with Channel

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

the class PushTest method receivedInForeground.

@Test
public void receivedInForeground() {
    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);
    Activity activity = mock(Activity.class);
    when(activity.getIntent()).thenReturn(mock(Intent.class));
    push.onActivityResumed(activity);
    /* Mock some message. */
    RemoteMessage message = mock(RemoteMessage.class);
    RemoteMessage.Notification notification = mock(RemoteMessage.Notification.class);
    when(message.getNotification()).thenReturn(notification);
    when(notification.getTitle()).thenReturn("some title");
    when(notification.getBody()).thenReturn("some message");
    PushMessagingService service = new PushMessagingService();
    service.onMessageReceived(message);
    ArgumentCaptor<PushNotification> captor = ArgumentCaptor.forClass(PushNotification.class);
    verify(pushListener).onPushNotificationReceived(eq(activity), captor.capture());
    PushNotification pushNotification = captor.getValue();
    assertNotNull(pushNotification);
    assertEquals("some title", pushNotification.getTitle());
    assertEquals("some message", pushNotification.getMessage());
    assertEquals(new HashMap<String, String>(), pushNotification.getCustomData());
    /* If disabled, no notification anymore. */
    Push.setEnabled(false);
    service.onMessageReceived(message);
    /* Called once. */
    verify(pushListener).onPushNotificationReceived(eq(activity), captor.capture());
    /* Enabled but remove listener. */
    Push.setEnabled(true);
    Push.setListener(null);
    service.onMessageReceived(message);
    /* Called once. */
    verify(pushListener).onPushNotificationReceived(eq(activity), captor.capture());
    /* Mock null notification and custom data. */
    Push.setListener(pushListener);
    Map<String, String> data = new HashMap<>();
    data.put("a", "b");
    data.put("c", "d");
    when(message.getNotification()).thenReturn(null);
    when(message.getData()).thenReturn(data);
    service.onMessageReceived(message);
    verify(pushListener, times(2)).onPushNotificationReceived(eq(activity), captor.capture());
    pushNotification = captor.getValue();
    assertNotNull(pushNotification);
    assertNull(pushNotification.getTitle());
    assertNull(pushNotification.getMessage());
    assertEquals(data, pushNotification.getCustomData());
    /* Disable while posting the command to the U.I. thread. */
    activity = mock(Activity.class);
    when(activity.getIntent()).thenReturn(mock(Intent.class));
    push.onActivityResumed(activity);
    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));
    service.onMessageReceived(message);
    Push.setEnabled(false);
    runnable.get().run();
    verify(pushListener, never()).onPushNotificationReceived(eq(activity), captor.capture());
    /* Remove listener while posting to UI thread. */
    Push.setEnabled(true);
    service.onMessageReceived(message);
    Push.setListener(null);
    runnable.get().run();
    verify(pushListener, never()).onPushNotificationReceived(eq(activity), captor.capture());
    /* Update listener while posting to UI thread. */
    Push.setListener(pushListener);
    service.onMessageReceived(message);
    PushListener pushListener2 = mock(PushListener.class);
    Push.setListener(pushListener2);
    runnable.get().run();
    verify(pushListener, never()).onPushNotificationReceived(eq(activity), captor.capture());
    verify(pushListener2).onPushNotificationReceived(eq(activity), captor.capture());
}
Also used : Context(android.content.Context) RemoteMessage(com.google.firebase.messaging.RemoteMessage) HashMap(java.util.HashMap) Channel(com.microsoft.azure.mobile.channel.Channel) Activity(android.app.Activity) Intent(android.content.Intent) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString) Answer(org.mockito.stubbing.Answer) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 32 with Channel

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

the class PushTest method verifyEnableFirebaseAnalytics.

@Test
public void verifyEnableFirebaseAnalytics() {
    Context contextMock = mock(Context.class);
    Push push = Push.getInstance();
    Channel channel = mock(Channel.class);
    push.onStarted(contextMock, DUMMY_APP_SECRET, channel);
    verifyStatic();
    FirebaseAnalyticsUtils.setEnabled(any(Context.class), eq(false));
    /* For check enable firebase analytics collection. */
    Push.enableFirebaseAnalytics(contextMock);
    verifyStatic();
    FirebaseAnalyticsUtils.setEnabled(any(Context.class), eq(true));
}
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 33 with Channel

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

the class PushTest method verifyEnableFirebaseAnalyticsBeforeStart.

@Test
public void verifyEnableFirebaseAnalyticsBeforeStart() {
    Context contextMock = mock(Context.class);
    Push push = Push.getInstance();
    Channel channel = mock(Channel.class);
    Push.enableFirebaseAnalytics(contextMock);
    push.onStarted(contextMock, DUMMY_APP_SECRET, channel);
    verifyStatic(never());
    FirebaseAnalyticsUtils.setEnabled(any(Context.class), eq(false));
}
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 34 with Channel

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

the class CrashesTest method crashInLastSessionError.

@Test
public void crashInLastSessionError() throws JSONException, IOException, ClassNotFoundException {
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(mock(ManagedErrorLog.class));
    mockStatic(ErrorLogHelper.class);
    File lastErrorLogFile = errorStorageDirectory.newFile("last-error-log.json");
    when(ErrorLogHelper.getLastErrorLogFile()).thenReturn(lastErrorLogFile);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { lastErrorLogFile });
    when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
    Crashes.getInstance().setLogSerializer(logSerializer);
    assertFalse(Crashes.hasCrashedInLastSession());
    JSONException jsonException = new JSONException("Fake JSON exception");
    when(logSerializer.deserializeLog(anyString())).thenThrow(jsonException);
    ResultCallback<ErrorReport> callback = new ResultCallback<ErrorReport>() {

        @Override
        public void onResult(ErrorReport data) {
            assertNull(data);
        }
    };
    /*
         * Last session error is only fetched upon initialization: enabled and channel ready.
         * Here the service is enabled by default but we are waiting channel to be ready, simulate that.
         */
    assertTrue(Crashes.isEnabled());
    Crashes.getLastSessionCrashReport(callback);
    Crashes.getInstance().onStarted(mock(Context.class), "", mock(Channel.class));
    assertFalse(Crashes.hasCrashedInLastSession());
    Crashes.getLastSessionCrashReport(callback);
    /*
         * De-serializing fails twice: processing the log from last time as part of the bulk processing.
         * And loading that same file for exposing it in getLastErrorReport.
         */
    verifyStatic(times(2));
    MobileCenterLog.error(eq(Crashes.LOG_TAG), anyString(), eq(jsonException));
}
Also used : ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) Context(android.content.Context) ResultCallback(com.microsoft.azure.mobile.ResultCallback) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Channel(com.microsoft.azure.mobile.channel.Channel) JSONException(org.json.JSONException) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 35 with Channel

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

the class CrashesTest method queuePendingCrashesShouldNotProcess.

@Test
public void queuePendingCrashesShouldNotProcess() throws IOException, ClassNotFoundException, JSONException {
    Context mockContext = mock(Context.class);
    Channel mockChannel = mock(Channel.class);
    ErrorReport report = new ErrorReport();
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    when(ErrorLogHelper.getStoredThrowableFile(any(UUID.class))).thenReturn(mock(File.class));
    when(ErrorLogHelper.getErrorReportFromErrorLog(any(ManagedErrorLog.class), any(Throwable.class))).thenReturn(report);
    when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
    when(StorageHelper.InternalStorage.readObject(any(File.class))).thenReturn(new RuntimeException()).thenReturn(new byte[] {});
    CrashesListener mockListener = mock(CrashesListener.class);
    when(mockListener.shouldProcess(report)).thenReturn(false);
    Crashes crashes = Crashes.getInstance();
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(mErrorLog);
    crashes.setLogSerializer(logSerializer);
    crashes.setInstanceListener(mockListener);
    crashes.onStarted(mockContext, "", mockChannel);
    verify(mockListener).shouldProcess(report);
    verify(mockListener, never()).shouldAwaitUserConfirmation();
    verify(mockListener, never()).getErrorAttachments(report);
    verify(mockChannel, never()).enqueue(any(Log.class), eq(crashes.getGroupName()));
}
Also used : Context(android.content.Context) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) Channel(com.microsoft.azure.mobile.channel.Channel) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) UUID(java.util.UUID) File(java.io.File) 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