Search in sources :

Example 21 with ArgumentMatcher

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

the class CrashesTest method trackExceptionForWrapperSdk.

@Test
public void trackExceptionForWrapperSdk() {
    StackFrame frame = new StackFrame();
    frame.setClassName("1");
    frame.setFileName("2");
    frame.setLineNumber(3);
    frame.setMethodName("4");
    final com.microsoft.azure.mobile.crashes.ingestion.models.Exception exception = new com.microsoft.azure.mobile.crashes.ingestion.models.Exception();
    exception.setType("5");
    exception.setMessage("6");
    exception.setFrames(singletonList(frame));
    Crashes crashes = Crashes.getInstance();
    Channel mockChannel = mock(Channel.class);
    Crashes.getInstance().trackException(exception);
    verify(mockChannel, never()).enqueue(any(Log.class), eq(crashes.getGroupName()));
    crashes.onStarted(mock(Context.class), "", mockChannel);
    Crashes.getInstance().trackException(exception);
    verify(mockChannel).enqueue(argThat(new ArgumentMatcher<Log>() {

        @Override
        public boolean matches(Object item) {
            return item instanceof ManagedErrorLog && exception.equals(((ManagedErrorLog) item).getException());
        }
    }), eq(crashes.getGroupName()));
}
Also used : Context(android.content.Context) 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) JSONException(org.json.JSONException) TestCrashException(com.microsoft.azure.mobile.crashes.model.TestCrashException) IOException(java.io.IOException) ManagedErrorLog(com.microsoft.azure.mobile.crashes.ingestion.models.ManagedErrorLog) StackFrame(com.microsoft.azure.mobile.crashes.ingestion.models.StackFrame) ArgumentMatcher(org.mockito.ArgumentMatcher) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 22 with ArgumentMatcher

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

the class DefaultChannelTest method shutdownInterrupted.

@Test
public void shutdownInterrupted() throws Exception {
    Persistence mockPersistence = mock(Persistence.class);
    IngestionHttp mockIngestion = mock(IngestionHttp.class);
    Channel.GroupListener mockListener = mock(Channel.GroupListener.class);
    DatabasePersistenceAsync mockPersistenceAsync = spy(new DatabasePersistenceAsync(mockPersistence));
    whenNew(DatabasePersistenceAsync.class).withArguments(mockPersistence).thenReturn(mockPersistenceAsync);
    when(mockPersistence.getLogs(any(String.class), anyInt(), Matchers.<List<Log>>any())).then(getGetLogsAnswer(1));
    doThrow(new InterruptedException()).when(mockPersistenceAsync).waitForCurrentTasksToComplete(anyLong());
    DefaultChannel channel = new DefaultChannel(mock(Context.class), UUIDUtils.randomUUID().toString(), mockPersistence, mockIngestion);
    channel.addGroup(TEST_GROUP, 1, BATCH_TIME_INTERVAL, MAX_PARALLEL_BATCHES, mockListener);
    /* Enqueuing 1 event. */
    channel.enqueue(mock(Log.class), TEST_GROUP);
    verify(mockListener).onBeforeSending(notNull(Log.class));
    channel.shutdown();
    verify(mockListener, never()).onFailure(any(Log.class), any(Exception.class));
    verify(mockPersistence).clearPendingLogState();
    verify(mockPersistenceAsync).waitForCurrentTasksToComplete(DefaultChannel.SHUTDOWN_TIMEOUT);
    verifyStatic();
    MobileCenterLog.warn(eq(MobileCenterLog.LOG_TAG), anyString(), argThat(new ArgumentMatcher<Throwable>() {

        @Override
        public boolean matches(Object argument) {
            return argument instanceof InterruptedException;
        }
    }));
}
Also used : Context(android.content.Context) MobileCenterLog(com.microsoft.azure.mobile.utils.MobileCenterLog) Log(com.microsoft.azure.mobile.ingestion.models.Log) Matchers.anyString(org.mockito.Matchers.anyString) SocketException(java.net.SocketException) IOException(java.io.IOException) HttpException(com.microsoft.azure.mobile.http.HttpException) CancellationException(com.microsoft.azure.mobile.CancellationException) Persistence(com.microsoft.azure.mobile.persistence.Persistence) IngestionHttp(com.microsoft.azure.mobile.ingestion.IngestionHttp) ArgumentMatcher(org.mockito.ArgumentMatcher) DatabasePersistenceAsync(com.microsoft.azure.mobile.persistence.DatabasePersistenceAsync) Test(org.junit.Test)

Example 23 with ArgumentMatcher

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

the class BrowserUtilsTest method twoBrowsersAndNoDefault.

@Test
public void twoBrowsersAndNoDefault() throws Exception {
    /* Mock no browser. */
    Activity activity = mock(Activity.class);
    doThrow(new ActivityNotFoundException()).when(activity).startActivity(argThat(CHROME_MATCHER));
    PackageManager packageManager = mock(PackageManager.class);
    when(activity.getPackageManager()).thenReturn(packageManager);
    {
        ActivityInfo activityInfo = new ActivityInfo();
        activityInfo.packageName = "system";
        activityInfo.name = "picker";
        ResolveInfo resolveInfo = new ResolveInfo();
        resolveInfo.activityInfo = activityInfo;
        when(packageManager.resolveActivity(any(Intent.class), eq(PackageManager.MATCH_DEFAULT_ONLY))).thenReturn(resolveInfo);
    }
    {
        ActivityInfo activityInfo = new ActivityInfo();
        activityInfo.packageName = "system";
        activityInfo.name = "browser";
        ResolveInfo resolveInfo = new ResolveInfo();
        resolveInfo.activityInfo = activityInfo;
        ActivityInfo activityInfo2 = new ActivityInfo();
        activityInfo2.packageName = "mozilla";
        activityInfo2.name = "firefox";
        ResolveInfo resolveInfo2 = new ResolveInfo();
        resolveInfo2.activityInfo = activityInfo;
        when(packageManager.queryIntentActivities(any(Intent.class), anyInt())).thenReturn(asList(resolveInfo, resolveInfo2));
    }
    /* Open Chrome then abort. */
    BrowserUtils.openBrowser(TEST_URL, activity);
    InOrder order = inOrder(activity);
    order.verify(activity).startActivity(argThat(CHROME_MATCHER));
    order.verify(activity).startActivity(argThat(new ArgumentMatcher<Intent>() {

        @Override
        public boolean matches(Object o) {
            Intent intent = (Intent) o;
            return Intent.ACTION_VIEW.equals(intent.getAction()) && Uri.parse(TEST_URL).equals(intent.getData()) && intent.getComponent().getClassName().equals("browser");
        }
    }));
    order.verifyNoMoreInteractions();
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) ActivityInfo(android.content.pm.ActivityInfo) InOrder(org.mockito.InOrder) PackageManager(android.content.pm.PackageManager) ActivityNotFoundException(android.content.ActivityNotFoundException) ArgumentMatcher(org.mockito.ArgumentMatcher) Activity(android.app.Activity) Intent(android.content.Intent) Test(org.junit.Test)

Example 24 with ArgumentMatcher

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

the class BrowserUtilsTest method secondBrowserIsDefault.

@Test
public void secondBrowserIsDefault() throws Exception {
    /* Mock no browser. */
    Activity activity = mock(Activity.class);
    doThrow(new ActivityNotFoundException()).when(activity).startActivity(argThat(CHROME_MATCHER));
    PackageManager packageManager = mock(PackageManager.class);
    when(activity.getPackageManager()).thenReturn(packageManager);
    {
        ActivityInfo activityInfo = new ActivityInfo();
        activityInfo.packageName = "mozilla";
        activityInfo.name = "firefox";
        ResolveInfo resolveInfo = new ResolveInfo();
        resolveInfo.activityInfo = activityInfo;
        when(packageManager.resolveActivity(any(Intent.class), eq(PackageManager.MATCH_DEFAULT_ONLY))).thenReturn(resolveInfo);
    }
    {
        ActivityInfo activityInfo = new ActivityInfo();
        activityInfo.packageName = "system";
        activityInfo.name = "browser";
        ResolveInfo resolveInfo = new ResolveInfo();
        resolveInfo.activityInfo = activityInfo;
        ActivityInfo activityInfo2 = new ActivityInfo();
        activityInfo2.packageName = "mozilla";
        activityInfo2.name = "firefox";
        ResolveInfo resolveInfo2 = new ResolveInfo();
        resolveInfo2.activityInfo = activityInfo2;
        when(packageManager.queryIntentActivities(any(Intent.class), anyInt())).thenReturn(asList(resolveInfo, resolveInfo2));
    }
    /* Open Chrome then abort. */
    BrowserUtils.openBrowser(TEST_URL, activity);
    InOrder order = inOrder(activity);
    order.verify(activity).startActivity(argThat(CHROME_MATCHER));
    order.verify(activity).startActivity(argThat(new ArgumentMatcher<Intent>() {

        @Override
        public boolean matches(Object o) {
            Intent intent = (Intent) o;
            return Intent.ACTION_VIEW.equals(intent.getAction()) && Uri.parse(TEST_URL).equals(intent.getData()) && intent.getComponent().getClassName().equals("firefox");
        }
    }));
    order.verifyNoMoreInteractions();
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) ActivityInfo(android.content.pm.ActivityInfo) InOrder(org.mockito.InOrder) PackageManager(android.content.pm.PackageManager) ActivityNotFoundException(android.content.ActivityNotFoundException) ArgumentMatcher(org.mockito.ArgumentMatcher) Activity(android.app.Activity) Intent(android.content.Intent) Test(org.junit.Test)

Example 25 with ArgumentMatcher

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

the class BrowserUtilsTest method onlySystemBrowserNoDefaultAsPicker.

@Test
public void onlySystemBrowserNoDefaultAsPicker() throws Exception {
    /* Mock no browser. */
    Activity activity = mock(Activity.class);
    doThrow(new ActivityNotFoundException()).when(activity).startActivity(argThat(CHROME_MATCHER));
    PackageManager packageManager = mock(PackageManager.class);
    when(activity.getPackageManager()).thenReturn(packageManager);
    {
        ActivityInfo activityInfo = new ActivityInfo();
        activityInfo.packageName = "system";
        activityInfo.name = "picker";
        ResolveInfo resolveInfo = new ResolveInfo();
        resolveInfo.activityInfo = activityInfo;
        when(packageManager.resolveActivity(any(Intent.class), eq(PackageManager.MATCH_DEFAULT_ONLY))).thenReturn(resolveInfo);
    }
    {
        ActivityInfo activityInfo = new ActivityInfo();
        activityInfo.packageName = "system";
        activityInfo.name = "browser";
        ResolveInfo resolveInfo = new ResolveInfo();
        resolveInfo.activityInfo = activityInfo;
        when(packageManager.queryIntentActivities(any(Intent.class), anyInt())).thenReturn(Collections.singletonList(resolveInfo));
    }
    /* Open Chrome then abort. */
    BrowserUtils.openBrowser(TEST_URL, activity);
    InOrder order = inOrder(activity);
    order.verify(activity).startActivity(argThat(CHROME_MATCHER));
    order.verify(activity).startActivity(argThat(new ArgumentMatcher<Intent>() {

        @Override
        public boolean matches(Object o) {
            Intent intent = (Intent) o;
            return Intent.ACTION_VIEW.equals(intent.getAction()) && Uri.parse(TEST_URL).equals(intent.getData()) && intent.getComponent().getClassName().equals("browser");
        }
    }));
    order.verifyNoMoreInteractions();
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) ActivityInfo(android.content.pm.ActivityInfo) InOrder(org.mockito.InOrder) PackageManager(android.content.pm.PackageManager) ActivityNotFoundException(android.content.ActivityNotFoundException) ArgumentMatcher(org.mockito.ArgumentMatcher) Activity(android.app.Activity) Intent(android.content.Intent) 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