use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class AbstractMobileCenterServiceTest method optionalGroup.
@Test
public void optionalGroup() {
service = new AbstractMobileCenterService() {
@Override
protected String getGroupName() {
return null;
}
@Override
public String getServiceName() {
return "Test";
}
@Override
protected String getLoggerTag() {
return "TestLog";
}
};
Channel channel = mock(Channel.class);
service.onStarted(mock(Context.class), "", channel);
service.setInstanceEnabled(false);
service.setInstanceEnabled(true);
verifyZeroInteractions(channel);
}
use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class PushTest method clickedFromBackground.
@Test
public void clickedFromBackground() {
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);
/* Mock activity to contain push */
Activity activity = mock(Activity.class);
Intent intent = mock(Intent.class);
when(activity.getIntent()).thenReturn(intent);
Bundle extras = mock(Bundle.class);
when(intent.getExtras()).thenReturn(extras);
when(extras.getString(Push.EXTRA_GOOGLE_MESSAGE_ID)).thenReturn("reserved value by google");
final Map<String, String> extraMap = new HashMap<>();
for (String key : Push.EXTRA_STANDARD_KEYS) {
extraMap.put(key, "reserved value by google");
}
Map<String, String> customMap = new HashMap<>();
customMap.put("custom", "data");
customMap.put("b", "c");
extraMap.putAll(customMap);
when(extras.keySet()).thenReturn(extraMap.keySet());
when(extras.getString(anyString())).then(new Answer<String>() {
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
return extraMap.get(invocation.getArguments()[0].toString());
}
});
/* Simulate we detect push in onCreate. */
push.onActivityCreated(activity, null);
ArgumentCaptor<PushNotification> captor = ArgumentCaptor.forClass(PushNotification.class);
verify(pushListener).onPushNotificationReceived(eq(activity), captor.capture());
PushNotification pushNotification = captor.getValue();
assertNotNull(pushNotification);
assertNull(pushNotification.getTitle());
assertNull(pushNotification.getMessage());
assertEquals(customMap, pushNotification.getCustomData());
/* On started on resume will not duplicate the callback. */
push.onActivityStarted(activity);
push.onActivityResumed(activity);
verify(pushListener).onPushNotificationReceived(eq(activity), captor.capture());
/* Disable SDK stops callbacks. */
push.onActivityPaused(activity);
activity = mock(Activity.class);
when(activity.getIntent()).thenReturn(intent);
when(extras.getString(Push.EXTRA_GOOGLE_MESSAGE_ID)).thenReturn("new id");
Push.setEnabled(false);
push.onActivityResumed(activity);
verify(pushListener, never()).onPushNotificationReceived(eq(activity), captor.capture());
/* Same if we remove listener. */
Push.setEnabled(true);
Push.setListener(null);
push.onActivityResumed(activity);
verify(pushListener, never()).onPushNotificationReceived(eq(activity), captor.capture());
/* Set listener to read the new push when resumed. */
Push.setListener(pushListener);
push.onActivityResumed(activity);
verify(pushListener).onPushNotificationReceived(eq(activity), captor.capture());
/* If intent extras are null, nothing happens. */
activity = mock(Activity.class);
when(activity.getIntent()).thenReturn(intent);
push.onActivityResumed(activity);
verify(pushListener, never()).onPushNotificationReceived(eq(activity), captor.capture());
/* If intent contains non push extras, same thing. */
when(intent.getExtras()).thenReturn(mock(Bundle.class));
push.onActivityResumed(activity);
verify(pushListener, never()).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 onTokenRefresh.
@Test
public void onTokenRefresh() {
String testToken = "TEST";
Push push = Mockito.spy(Push.getInstance());
push.setInstanceEnabled(false);
Channel channel = mock(Channel.class);
push.onStarted(mock(Context.class), DUMMY_APP_SECRET, channel);
verify(mFirebaseInstanceId, never()).getToken();
/* When token unavailable */
when(mFirebaseInstanceId.getToken()).thenReturn(null);
push.setInstanceEnabled(true);
verify(mFirebaseInstanceId).getToken();
verify(push, never()).onTokenRefresh(anyString());
/* When token available */
when(mFirebaseInstanceId.getToken()).thenReturn(testToken);
push.setInstanceEnabled(true);
verify(push).onTokenRefresh(anyString());
verifyStatic(times(1));
StorageHelper.PreferencesStorage.putString(eq(PREFERENCE_KEY_PUSH_TOKEN), eq(testToken));
/* For check enqueue only once */
push.onTokenRefresh(testToken);
verify(channel).enqueue(any(PushInstallationLog.class), anyString());
/* For check resend token on change */
push.onTokenRefresh("OTHER");
verifyStatic(times(1));
StorageHelper.PreferencesStorage.putString(eq(PREFERENCE_KEY_PUSH_TOKEN), eq("OTHER"));
}
use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class PushTest method setEnabled.
@Test
public void setEnabled() {
String testToken = "TEST";
Push push = Push.getInstance();
Channel channel = mock(Channel.class);
assertTrue(Push.isEnabled());
Push.setEnabled(true);
assertTrue(Push.isEnabled());
Push.setEnabled(false);
assertFalse(Push.isEnabled());
push.onStarted(mock(Context.class), DUMMY_APP_SECRET, channel);
verify(channel).clear(push.getGroupName());
verify(channel).removeGroup(eq(push.getGroupName()));
verify(mFirebaseInstanceId, never()).getToken();
/* If disabled when PushTokenTask executing */
Push.setEnabled(false);
push.onTokenRefresh(testToken);
verify(channel, never()).enqueue(any(PushInstallationLog.class), eq(push.getGroupName()));
/* For check enqueue only once */
when(mFirebaseInstanceId.getToken()).thenReturn(testToken);
Push.setEnabled(true);
Push.setEnabled(true);
verify(channel).enqueue(any(PushInstallationLog.class), eq(push.getGroupName()));
}
use of com.microsoft.azure.mobile.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class AnalyticsTest method startSessionAfterUserApproval.
@Test
public void startSessionAfterUserApproval() {
/*
* Disable analytics while in background to set up the initial condition
* simulating the optin use case.
*/
Analytics analytics = Analytics.getInstance();
Channel channel = mock(Channel.class);
analytics.onStarted(mock(Context.class), "", channel);
Analytics.setEnabled(false);
/* App in foreground: no log yet, we are disabled. */
analytics.onActivityResumed(new Activity());
verify(channel, never()).enqueue(any(Log.class), eq(analytics.getGroupName()));
/* Enable: start session sent retroactively. */
Analytics.setEnabled(true);
verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {
@Override
public boolean matches(Object argument) {
return argument instanceof StartSessionLog;
}
}), eq(analytics.getGroupName()));
verify(channel).enqueue(argThat(new ArgumentMatcher<Log>() {
@Override
public boolean matches(Object argument) {
return argument instanceof PageLog;
}
}), eq(analytics.getGroupName()));
/* Go background. */
analytics.onActivityPaused(new Activity());
/* Disable/enable: nothing happens on background. */
Analytics.setEnabled(false);
Analytics.setEnabled(true);
/* No additional log. */
verify(channel, times(2)).enqueue(any(Log.class), eq(analytics.getGroupName()));
}
Aggregations