Search in sources :

Example 11 with Channel

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

the class CrashesTest method processPendingErrorsCorrupted.

@Test
public void processPendingErrorsCorrupted() throws JSONException {
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getStoredErrorLogFiles()).thenReturn(new File[] { mock(File.class) });
    when(ErrorLogHelper.getNewMinidumpFiles()).thenReturn(new File[0]);
    when(StorageHelper.InternalStorage.read(any(File.class))).thenReturn("");
    Crashes crashes = Crashes.getInstance();
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(mock(ManagedErrorLog.class));
    crashes.setLogSerializer(logSerializer);
    CrashesListener listener = mock(CrashesListener.class);
    crashes.setInstanceListener(listener);
    Channel channel = mock(Channel.class);
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mock(Context.class), "", channel);
    verifyZeroInteractions(listener);
    verify(channel, never()).enqueue(any(Log.class), anyString());
}
Also used : Context(android.content.Context) SessionContext(com.microsoft.appcenter.SessionContext) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) Channel(com.microsoft.appcenter.channel.Channel) LogSerializer(com.microsoft.appcenter.ingestion.models.json.LogSerializer) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with Channel

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

the class CrashesTest method noQueueingWhenDisabled.

@Test
public void noQueueingWhenDisabled() {
    mockStatic(ErrorLogHelper.class);
    when(ErrorLogHelper.getErrorStorageDirectory()).thenReturn(errorStorageDirectory.getRoot());
    when(StorageHelper.PreferencesStorage.getBoolean(CRASHES_ENABLED_KEY, true)).thenReturn(false);
    Channel channel = mock(Channel.class);
    Crashes crashes = Crashes.getInstance();
    crashes.onStarting(mAppCenterHandler);
    crashes.onStarted(mock(Context.class), "", channel);
    verify(channel, never()).enqueue(any(Log.class), anyString());
}
Also used : Context(android.content.Context) SessionContext(com.microsoft.appcenter.SessionContext) HandledErrorLog(com.microsoft.appcenter.crashes.ingestion.models.HandledErrorLog) ManagedErrorLog(com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog) Log(com.microsoft.appcenter.ingestion.models.Log) AppCenterLog(com.microsoft.appcenter.utils.AppCenterLog) ErrorAttachmentLog(com.microsoft.appcenter.crashes.ingestion.models.ErrorAttachmentLog) Channel(com.microsoft.appcenter.channel.Channel) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with Channel

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

the class PushTest method setEnabled.

@Test
public void setEnabled() throws InterruptedException, FirebaseUtils.FirebaseUnavailableException {
    /* Before start it's disabled. */
    assertFalse(Push.isEnabled().get());
    verifyStatic();
    AppCenterLog.error(anyString(), anyString());
    /* Start. */
    String testToken = "TEST";
    Push push = Push.getInstance();
    Channel channel = mock(Channel.class);
    when(mFirebaseInstanceId.getToken()).thenReturn(testToken);
    start(mock(Context.class), push, channel);
    verify(channel).removeGroup(eq(push.getGroupName()));
    assertTrue(Push.isEnabled().get());
    verify(mFirebaseInstanceId).getToken();
    verify(channel).enqueue(any(PushInstallationLog.class), eq(push.getGroupName()));
    /* Enable while already enabled. */
    Push.setEnabled(true);
    assertTrue(Push.isEnabled().get());
    /* Verify behavior happened only once. */
    verify(mFirebaseInstanceId).getToken();
    verify(channel).enqueue(any(PushInstallationLog.class), eq(push.getGroupName()));
    /* Disable. */
    Push.setEnabled(false).get();
    assertFalse(Push.isEnabled().get());
    verify(channel).clear(push.getGroupName());
    verify(channel, times(2)).removeGroup(eq(push.getGroupName()));
    /* Disable again. Test waiting with async callback. */
    final CountDownLatch latch = new CountDownLatch(1);
    Push.setEnabled(false).thenAccept(new AppCenterConsumer<Void>() {

        @Override
        public void accept(Void aVoid) {
            latch.countDown();
        }
    });
    assertTrue(latch.await(0, TimeUnit.MILLISECONDS));
    /* Ignore on token refresh. */
    push.onTokenRefresh(testToken);
    /* Verify behavior happened only once. */
    verify(mFirebaseInstanceId).getToken();
    verify(channel).enqueue(any(PushInstallationLog.class), eq(push.getGroupName()));
    /* Make sure no logging when posting check activity intent commands. */
    Activity activity = mock(Activity.class);
    when(activity.getIntent()).thenReturn(mock(Intent.class));
    push.onActivityResumed(activity);
    /* No additional error was logged since before start. */
    verifyStatic();
    AppCenterLog.error(anyString(), anyString());
    verify(mFirebaseAnalyticsInstance).setAnalyticsCollectionEnabled(false);
    verify(mFirebaseAnalyticsInstance, never()).setAnalyticsCollectionEnabled(true);
    /* If disabled before start, still we must disable firebase. */
    Push.unsetInstance();
    push = Push.getInstance();
    start(mock(Context.class), push, channel);
    verify(mFirebaseAnalyticsInstance, times(2)).setAnalyticsCollectionEnabled(false);
    verify(mFirebaseAnalyticsInstance, never()).setAnalyticsCollectionEnabled(true);
}
Also used : Context(android.content.Context) Channel(com.microsoft.appcenter.channel.Channel) PushInstallationLog(com.microsoft.appcenter.push.ingestion.models.PushInstallationLog) Activity(android.app.Activity) Intent(android.content.Intent) Matchers.anyString(org.mockito.Matchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 14 with Channel

use of com.microsoft.appcenter.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);
    start(contextMock, push, channel);
    Activity activity = mock(Activity.class);
    when(activity.getIntent()).thenReturn(mock(Intent.class));
    push.onActivityResumed(activity);
    /* Mock some message. */
    Intent pushIntent = createPushIntent("some title", "some message", null);
    Push.getInstance().onMessageReceived(contextMock, pushIntent);
    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);
    Push.getInstance().onMessageReceived(contextMock, pushIntent);
    /* Called once. */
    verify(pushListener).onPushNotificationReceived(eq(activity), captor.capture());
    /* Enabled but remove listener. */
    Push.setEnabled(true);
    Push.setListener(null);
    Push.getInstance().onMessageReceived(contextMock, pushIntent);
    /* Called once. */
    verify(pushListener).onPushNotificationReceived(eq(activity), captor.capture());
    /* Mock notification and custom data. */
    Push.setListener(pushListener);
    Map<String, String> data = new HashMap<>();
    data.put("a", "b");
    data.put("c", "d");
    pushIntent = createPushIntent("some title", "some message", data);
    Push.getInstance().onMessageReceived(contextMock, pushIntent);
    verify(pushListener, times(2)).onPushNotificationReceived(eq(activity), captor.capture());
    pushNotification = captor.getValue();
    assertNotNull(pushNotification);
    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));
    Push.getInstance().onMessageReceived(contextMock, pushIntent);
    Push.setEnabled(false);
    runnable.get().run();
    verify(pushListener, never()).onPushNotificationReceived(eq(activity), captor.capture());
    /* Remove listener while posting to UI thread. */
    Push.setEnabled(true);
    Push.getInstance().onMessageReceived(contextMock, pushIntent);
    Push.setListener(null);
    runnable.get().run();
    verify(pushListener, never()).onPushNotificationReceived(eq(activity), captor.capture());
    /* Update listener while posting to UI thread. */
    Push.setListener(pushListener);
    Push.getInstance().onMessageReceived(contextMock, pushIntent);
    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) HashMap(java.util.HashMap) Channel(com.microsoft.appcenter.channel.Channel) Activity(android.app.Activity) Intent(android.content.Intent) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.anyString(org.mockito.Matchers.anyString) 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 15 with Channel

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

the class PushTest method verifyEnableFirebaseAnalyticsBeforeStart.

@SuppressWarnings("deprecation")
@Test
public void verifyEnableFirebaseAnalyticsBeforeStart() throws FirebaseUtils.FirebaseUnavailableException {
    Context contextMock = mock(Context.class);
    Push push = Push.getInstance();
    Channel channel = mock(Channel.class);
    Push.enableFirebaseAnalytics(contextMock);
    start(contextMock, push, channel);
    verify(mFirebaseAnalyticsInstance, never()).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)

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