Search in sources :

Example 16 with ArgumentMatcher

use of org.mockito.ArgumentMatcher in project mobile-center-sdk-android by Microsoft.

the class AnalyticsTest method activityResumed.

private void activityResumed(final String expectedName, android.app.Activity activity) {
    Analytics analytics = Analytics.getInstance();
    Channel channel = mock(Channel.class);
    analytics.onStarted(mock(Context.class), "", channel);
    analytics.onActivityResumed(activity);
    analytics.onActivityPaused(activity);
    verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof PageLog) {
                PageLog pageLog = (PageLog) item;
                return expectedName.equals(pageLog.getName());
            }
            return false;
        }
    }), eq(analytics.getGroupName()));
}
Also used : Context(android.content.Context) PageLog(com.microsoft.azure.mobile.analytics.ingestion.models.PageLog) Channel(com.microsoft.azure.mobile.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher)

Example 17 with ArgumentMatcher

use of org.mockito.ArgumentMatcher in project mobile-center-sdk-android by Microsoft.

the class AnalyticsTest method trackEvent.

@Test
public void trackEvent() {
    Analytics analytics = Analytics.getInstance();
    Channel channel = mock(Channel.class);
    analytics.onStarted(mock(Context.class), "", channel);
    final String name = "testEvent";
    final HashMap<String, String> properties = new HashMap<>();
    properties.put("a", "b");
    Analytics.trackEvent(name, properties);
    verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof EventLog) {
                EventLog eventLog = (EventLog) item;
                return name.equals(eventLog.getName()) && properties.equals(eventLog.getProperties()) && eventLog.getId() != null;
            }
            return false;
        }
    }), eq(analytics.getGroupName()));
}
Also used : Context(android.content.Context) HashMap(java.util.HashMap) Channel(com.microsoft.azure.mobile.channel.Channel) ArgumentMatcher(org.mockito.ArgumentMatcher) EventLog(com.microsoft.azure.mobile.analytics.ingestion.models.EventLog) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.azure.mobile.test.TestUtils.generateString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 18 with ArgumentMatcher

use of org.mockito.ArgumentMatcher 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.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.azure.mobile.analytics.ingestion.models.StartSessionLog) 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)

Example 19 with ArgumentMatcher

use of org.mockito.ArgumentMatcher in project mobile-center-sdk-android by Microsoft.

the class AnalyticsTest method testTrackPage.

@Test
public void testTrackPage() {
    Analytics analytics = Analytics.getInstance();
    Channel channel = mock(Channel.class);
    analytics.onStarted(mock(Context.class), "", channel);
    Analytics.trackPage(null, null);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackPage("", null);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackPage(" ", null);
    verify(channel, times(1)).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackPage(generateString(257, '*'), null);
    verify(channel, never()).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackPage(generateString(256, '*'), null);
    verify(channel, times(1)).enqueue(any(Log.class), anyString());
    reset(channel);
    Analytics.trackPage("pageName", new HashMap<String, String>() {

        {
            put(null, null);
            put("", null);
            put(generateString(65, '*'), null);
            put("1", null);
            put("2", generateString(65, '*'));
        }
    });
    verify(channel, times(1)).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof PageLog) {
                PageLog pageLog = (PageLog) item;
                return pageLog.getProperties().size() == 0;
            }
            return false;
        }
    }), anyString());
    reset(channel);
    final String validMapItem = "valid";
    Analytics.trackPage("pageName", new HashMap<String, String>() {

        {
            for (int i = 0; i < 10; i++) {
                put(validMapItem + i, validMapItem);
            }
        }
    });
    verify(channel, times(1)).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            if (item instanceof PageLog) {
                PageLog pageLog = (PageLog) item;
                return pageLog.getProperties().size() == 5;
            }
            return false;
        }
    }), anyString());
}
Also used : Context(android.content.Context) 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) Matchers.anyString(org.mockito.Matchers.anyString) TestUtils.generateString(com.microsoft.azure.mobile.test.TestUtils.generateString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 20 with ArgumentMatcher

use of org.mockito.ArgumentMatcher in project mobile-center-sdk-android by Microsoft.

the class CrashesTest method queuePendingCrashesShouldProcess.

@Test
public void queuePendingCrashesShouldProcess() throws IOException, ClassNotFoundException, JSONException {
    /* Setup mock. */
    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());
    CrashesListener mockListener = mock(CrashesListener.class);
    when(mockListener.shouldProcess(report)).thenReturn(true);
    when(mockListener.shouldAwaitUserConfirmation()).thenReturn(false);
    ErrorAttachmentLog mockAttachment = mock(ErrorAttachmentLog.class);
    when(mockAttachment.getId()).thenReturn(UUID.randomUUID());
    when(mockAttachment.getErrorId()).thenReturn(UUID.randomUUID());
    when(mockAttachment.getContentType()).thenReturn("");
    when(mockAttachment.getFileName()).thenReturn("");
    when(mockAttachment.getData()).thenReturn(new byte[0]);
    when(mockAttachment.isValid()).thenReturn(true);
    ErrorAttachmentLog mockEmptyAttachment = mock(ErrorAttachmentLog.class);
    final int skipAttachmentLogsCount = 2;
    List<ErrorAttachmentLog> errorAttachmentLogList = Arrays.asList(mockAttachment, mockAttachment, mockEmptyAttachment, null);
    when(mockListener.getErrorAttachments(report)).thenReturn(errorAttachmentLogList);
    LogSerializer logSerializer = mock(LogSerializer.class);
    when(logSerializer.deserializeLog(anyString())).thenReturn(mErrorLog);
    Crashes crashes = Crashes.getInstance();
    crashes.setLogSerializer(logSerializer);
    crashes.setInstanceListener(mockListener);
    crashes.onStarted(mockContext, "", mockChannel);
    /* Test. */
    verify(mockListener).shouldProcess(report);
    verify(mockListener).shouldAwaitUserConfirmation();
    verify(mockListener).getErrorAttachments(report);
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object log) {
            return log.equals(mErrorLog);
        }
    }), eq(crashes.getGroupName()));
    verify(mockChannel, times(errorAttachmentLogList.size() - skipAttachmentLogsCount)).enqueue(mockAttachment, crashes.getGroupName());
}
Also used : Context(android.content.Context) Channel(com.microsoft.azure.mobile.channel.Channel) LogSerializer(com.microsoft.azure.mobile.ingestion.models.json.LogSerializer) ErrorReport(com.microsoft.azure.mobile.crashes.model.ErrorReport) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) ArgumentMatcher(org.mockito.ArgumentMatcher) ErrorAttachmentLog(com.microsoft.azure.mobile.crashes.ingestion.models.ErrorAttachmentLog) UUID(java.util.UUID) File(java.io.File) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ArgumentMatcher (org.mockito.ArgumentMatcher)104 Test (org.junit.Test)82 Appender (ch.qos.logback.core.Appender)23 Logger (org.slf4j.Logger)23 ArrayList (java.util.ArrayList)19 Context (android.content.Context)14 Channel (com.microsoft.azure.mobile.channel.Channel)13 ApplicationService (org.codice.ddf.admin.application.service.ApplicationService)13 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)12 Intent (android.content.Intent)11 HashMap (java.util.HashMap)11 HashSet (java.util.HashSet)10 FeaturesService (org.apache.karaf.features.FeaturesService)10 ResolveInfo (android.content.pm.ResolveInfo)9 File (java.io.File)9 Repository (org.apache.karaf.features.Repository)9 Matchers.anyString (org.mockito.Matchers.anyString)9 UUID (java.util.UUID)8 ApplicationServiceException (org.codice.ddf.admin.application.service.ApplicationServiceException)8 Log (com.microsoft.azure.mobile.ingestion.models.Log)7