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()));
}
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()));
}
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()));
}
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());
}
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());
}
Aggregations