use of com.microsoft.appcenter.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class AnalyticsTest method startSessionAfterUserApprovalWeakReference.
@Test
public void startSessionAfterUserApprovalWeakReference() {
/*
* Disable analytics while in background to set up the initial condition
* simulating the opt-in use case.
*/
Analytics analytics = Analytics.getInstance();
Channel channel = mock(Channel.class);
analytics.onStarting(mAppCenterHandler);
analytics.onStarted(mock(Context.class), "", channel);
Analytics.setEnabled(false);
/* App in foreground: no log yet, we are disabled. */
analytics.onActivityResumed(new Activity());
analytics.getCurrentActivity().clear();
verify(channel, never()).enqueue(any(Log.class), eq(analytics.getGroupName()));
/* Enable: start session not sent retroactively, weak reference lost. */
Analytics.setEnabled(true);
verify(channel, never()).enqueue(any(Log.class), eq(analytics.getGroupName()));
}
use of com.microsoft.appcenter.channel.Channel 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.onStarting(mAppCenterHandler);
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);
final String maxName = generateString(Analytics.MAX_NAME_LENGTH, '*');
Analytics.trackPage(maxName + "*", null);
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.getName().equals(maxName) && pageLog.getProperties() == null;
}
return false;
}
}), anyString());
reset(channel);
Analytics.trackPage(maxName, 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(Analytics.MAX_PROPERTY_ITEM_LENGTH + 1, '*'), null);
put("1", null);
}
});
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());
reset(channel);
final String longerMapItem = generateString(Analytics.MAX_PROPERTY_ITEM_LENGTH + 1, '*');
Analytics.trackPage("pageName", new HashMap<String, String>() {
{
put(longerMapItem, longerMapItem);
}
});
verify(channel, times(1)).enqueue(argThat(new ArgumentMatcher<Log>() {
@Override
public boolean matches(Object item) {
if (item instanceof PageLog) {
PageLog pageLog = (PageLog) item;
if (pageLog.getProperties().size() == 1) {
Map.Entry<String, String> entry = pageLog.getProperties().entrySet().iterator().next();
String truncatedMapItem = generateString(Analytics.MAX_PROPERTY_ITEM_LENGTH, '*');
return entry.getKey().length() == Analytics.MAX_PROPERTY_ITEM_LENGTH && entry.getValue().length() == Analytics.MAX_PROPERTY_ITEM_LENGTH;
}
}
return false;
}
}), anyString());
}
use of com.microsoft.appcenter.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class AnalyticsTest method testTrackEvent.
@Test
public void testTrackEvent() {
Analytics analytics = Analytics.getInstance();
Channel channel = mock(Channel.class);
analytics.onStarting(mAppCenterHandler);
analytics.onStarted(mock(Context.class), "", channel);
Analytics.trackEvent(null, null);
verify(channel, never()).enqueue(any(Log.class), anyString());
reset(channel);
Analytics.trackEvent("", null);
verify(channel, never()).enqueue(any(Log.class), anyString());
reset(channel);
Analytics.trackEvent(" ", null);
verify(channel, times(1)).enqueue(any(Log.class), anyString());
reset(channel);
final String maxName = generateString(Analytics.MAX_NAME_LENGTH, '*');
Analytics.trackEvent(maxName + "*", null);
verify(channel, times(1)).enqueue(argThat(new ArgumentMatcher<Log>() {
@Override
public boolean matches(Object item) {
if (item instanceof EventLog) {
EventLog eventLog = (EventLog) item;
return eventLog.getName().equals(maxName) && eventLog.getProperties() == null;
}
return false;
}
}), anyString());
reset(channel);
Analytics.trackEvent(maxName, null);
verify(channel, times(1)).enqueue(any(Log.class), anyString());
reset(channel);
Analytics.trackEvent("eventName", new HashMap<String, String>() {
{
put(null, null);
put("", null);
put(generateString(Analytics.MAX_PROPERTY_ITEM_LENGTH + 1, '*'), null);
put("1", null);
}
});
verify(channel, times(1)).enqueue(argThat(new ArgumentMatcher<Log>() {
@Override
public boolean matches(Object item) {
if (item instanceof EventLog) {
EventLog eventLog = (EventLog) item;
return eventLog.getProperties().size() == 0;
}
return false;
}
}), anyString());
reset(channel);
final String validMapItem = "valid";
Analytics.trackEvent("eventName", 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 EventLog) {
EventLog eventLog = (EventLog) item;
return eventLog.getProperties().size() == 5;
}
return false;
}
}), anyString());
reset(channel);
final String longerMapItem = generateString(Analytics.MAX_PROPERTY_ITEM_LENGTH + 1, '*');
Analytics.trackEvent("eventName", new HashMap<String, String>() {
{
put(longerMapItem, longerMapItem);
}
});
verify(channel, times(1)).enqueue(argThat(new ArgumentMatcher<Log>() {
@Override
public boolean matches(Object item) {
if (item instanceof EventLog) {
EventLog eventLog = (EventLog) item;
if (eventLog.getProperties().size() == 1) {
Map.Entry<String, String> entry = eventLog.getProperties().entrySet().iterator().next();
String truncatedMapItem = generateString(Analytics.MAX_PROPERTY_ITEM_LENGTH, '*');
return entry.getKey().length() == Analytics.MAX_PROPERTY_ITEM_LENGTH && entry.getValue().length() == Analytics.MAX_PROPERTY_ITEM_LENGTH;
}
}
return false;
}
}), anyString());
}
use of com.microsoft.appcenter.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class AnalyticsTest method activityResumed.
private void activityResumed(final String expectedName, android.app.Activity activity) {
/*
* Before start, calling onActivityResume is ignored.
* In reality it never happens, it means someone is messing with internals directly.
*/
Analytics analytics = Analytics.getInstance();
analytics.onActivityResumed(new Activity());
assertNull(analytics.getCurrentActivity());
verifyStatic();
AppCenterLog.error(anyString(), anyString());
analytics.onActivityPaused(new Activity());
verifyStatic(times(2));
AppCenterLog.error(anyString(), anyString());
/* Start. */
Channel channel = mock(Channel.class);
analytics.onStarting(mAppCenterHandler);
analytics.onStarted(mock(Context.class), "", channel);
/* Test resume/pause. */
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 com.microsoft.appcenter.channel.Channel in project mobile-center-sdk-android by Microsoft.
the class AnalyticsTest method disablePersisted.
@Test
public void disablePersisted() {
when(StorageHelper.PreferencesStorage.getBoolean(ANALYTICS_ENABLED_KEY, true)).thenReturn(false);
Analytics analytics = Analytics.getInstance();
/* Start. */
Channel channel = mock(Channel.class);
analytics.onStarting(mAppCenterHandler);
analytics.onStarted(mock(Context.class), "", channel);
verify(channel, never()).removeListener(any(Channel.Listener.class));
verify(channel, never()).addListener(any(Channel.Listener.class));
}
Aggregations