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