Skip to content

Commit

Permalink
Merge pull request #874 from Microsoft/feature/priority
Browse files Browse the repository at this point in the history
Event persistence priority
  • Loading branch information
guperrot committed Nov 9, 2018
2 parents a152e73 + 98899f2 commit 74f2eb5
Show file tree
Hide file tree
Showing 89 changed files with 4,444 additions and 3,689 deletions.
1 change: 1 addition & 0 deletions PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Please have a look at our [guidelines for contributions](https://github.com/Micr
* [ ] Are the files formatted correctly?
* [ ] Did you add unit tests?
* [ ] Did you test your change with either the sample apps that are included in the repository or with a blank app that uses your change?
* [ ] Did you check UI tests on the sample app? They are not executed on CI.

## Description

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import com.microsoft.appcenter.Constants;
import com.microsoft.appcenter.sasquatch.R;
import com.microsoft.appcenter.sasquatch.listeners.SasquatchAnalyticsListener;
import com.microsoft.appcenter.sasquatch.listeners.SasquatchCrashesListener;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

Expand Down Expand Up @@ -35,6 +38,16 @@ public class AnalyticsTest {
@Rule
public final ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

@Before
public void setUp() {
IdlingRegistry.getInstance().register(SasquatchAnalyticsListener.analyticsIdlingResource);
}

@After
public void tearDown() {
IdlingRegistry.getInstance().unregister(SasquatchAnalyticsListener.analyticsIdlingResource);
}

@Test
public void sendEventTest() throws InterruptedException {

Expand All @@ -50,7 +63,7 @@ public void sendEventTest() throws InterruptedException {
waitFor(onToast(mActivityTestRule.getActivity(),
withText(R.string.event_before_sending)), Constants.DEFAULT_TRIGGER_INTERVAL + CHECK_DELAY)
.check(matches(isDisplayed()));
waitAnalytics();
onView(isRoot()).perform(waitFor(TOAST_DELAY));
waitFor(onToast(mActivityTestRule.getActivity(), anyOf(
withContainsText(R.string.event_sent_succeeded),
withContainsText(R.string.event_sent_failed))), TOAST_DELAY)
Expand All @@ -71,16 +84,10 @@ public void sendPageTest() throws InterruptedException {
/* Check toasts. */
waitFor(onToast(mActivityTestRule.getActivity(), withText(R.string.page_before_sending)), Constants.DEFAULT_TRIGGER_INTERVAL + CHECK_DELAY)
.check(matches(isDisplayed()));
waitAnalytics();
onView(isRoot()).perform(waitFor(TOAST_DELAY));
waitFor(onToast(mActivityTestRule.getActivity(), anyOf(
withContainsText(R.string.page_sent_succeeded),
withContainsText(R.string.page_sent_failed))), TOAST_DELAY)
.check(matches(isDisplayed()));
}

private void waitAnalytics() {
IdlingRegistry.getInstance().register(SasquatchAnalyticsListener.analyticsIdlingResource);
onView(isRoot()).perform(waitFor(CHECK_DELAY));
IdlingRegistry.getInstance().unregister(SasquatchAnalyticsListener.analyticsIdlingResource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.microsoft.appcenter.crashes.utils.ErrorLogHelper;
import com.microsoft.appcenter.sasquatch.R;
import com.microsoft.appcenter.sasquatch.listeners.SasquatchCrashesListener;
import com.microsoft.appcenter.utils.storage.StorageHelper;
import com.microsoft.appcenter.utils.storage.SharedPreferencesManager;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
Expand All @@ -43,6 +43,7 @@
import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
import static android.support.test.espresso.matcher.ViewMatchers.withChild;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static com.microsoft.appcenter.sasquatch.activities.utils.EspressoUtils.CHECK_DELAY;
import static com.microsoft.appcenter.sasquatch.activities.utils.EspressoUtils.TOAST_DELAY;
import static com.microsoft.appcenter.sasquatch.activities.utils.EspressoUtils.onToast;
import static com.microsoft.appcenter.sasquatch.activities.utils.EspressoUtils.waitFor;
Expand Down Expand Up @@ -87,8 +88,8 @@ public void setUp() {
mContext = getInstrumentation().getTargetContext();

/* Clear preferences. */
StorageHelper.initialize(mContext);
StorageHelper.PreferencesStorage.clear();
SharedPreferencesManager.initialize(mContext);
SharedPreferencesManager.clear();

/* Clear crashes. */
Constants.loadFromContext(mContext);
Expand Down Expand Up @@ -193,12 +194,11 @@ private void crashTest(@StringRes int titleId) throws InterruptedException {
waitFor(onToast(mActivityTestRule.getActivity(),
withText(R.string.crash_before_sending)), TOAST_DELAY)
.check(matches(isDisplayed()));
onView(isRoot()).perform(waitFor(TOAST_DELAY));
onView(isRoot()).perform(waitFor(CHECK_DELAY));
waitFor(onToast(mActivityTestRule.getActivity(), anyOf(
withContainsText(R.string.crash_sent_succeeded),
withText(R.string.crash_sent_failed))), TOAST_DELAY)
.check(matches(isDisplayed()));
onView(isRoot()).perform(waitFor(TOAST_DELAY));
}

private ViewInteraction onCrash(@StringRes int titleId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.microsoft.appcenter.distribute.Distribute;
import com.microsoft.appcenter.push.Push;
import com.microsoft.appcenter.sasquatch.R;
import com.microsoft.appcenter.utils.storage.StorageHelper;
import com.microsoft.appcenter.utils.storage.SharedPreferencesManager;

import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -41,8 +41,8 @@ public void setUp() {
mContext = getInstrumentation().getTargetContext();

/* Clear preferences. */
StorageHelper.initialize(mContext);
StorageHelper.PreferencesStorage.clear();
SharedPreferencesManager.initialize(mContext);
SharedPreferencesManager.clear();

/* Launch main activity and go to setting page. Required to properly initialize. */
mActivityTestRule.launchActivity(new Intent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import java.util.Map;
import java.util.Set;


/**
* TODO delete this version and move projectDependency version to main folder during release process.
*/
public class EventActivity extends AppCompatActivity {

/**
Expand Down Expand Up @@ -135,6 +139,12 @@ public void onClick(View v) {
getSelectedTarget().resume();
}
});

/* Hide persistence flag UI as not supported by jCenter */
findViewById(R.id.event_priority_label).setVisibility(View.GONE);
findViewById(R.id.event_priority_spinner).setVisibility(View.GONE);
findViewById(R.id.number_of_logs_label).setVisibility(View.GONE);
findViewById(R.id.number_of_logs).setVisibility(View.GONE);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import com.microsoft.appcenter.sasquatch.eventfilter.EventFilter;
import com.microsoft.appcenter.utils.PrefStorageConstants;
import com.microsoft.appcenter.utils.async.AppCenterFuture;
import com.microsoft.appcenter.utils.storage.StorageHelper;

import java.io.File;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -367,7 +366,8 @@ public boolean onPreferenceClick(final Preference preference) {
public void onClick(DialogInterface dialog, int which) {
if (input.getText().toString().matches(UUID_FORMAT_REGEX)) {
UUID uuid = UUID.fromString(input.getText().toString());
StorageHelper.PreferencesStorage.putString(PrefStorageConstants.KEY_INSTALL_ID, uuid.toString());
SharedPreferences appCenterPreferences = getActivity().getSharedPreferences("AppCenter", Context.MODE_PRIVATE);
appCenterPreferences.edit().putString(PrefStorageConstants.KEY_INSTALL_ID, uuid.toString()).apply();
Toast.makeText(getActivity(), String.format(getActivity().getString(R.string.install_id_changed_format), uuid.toString()), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), R.string.install_id_invalid, Toast.LENGTH_SHORT).show();
Expand Down Expand Up @@ -492,7 +492,8 @@ public void onClick(DialogInterface dialog, int which) {

@Override
public boolean onPreferenceClick(Preference preference) {
StorageHelper.PreferencesStorage.remove(Crashes.PREF_KEY_ALWAYS_SEND);
SharedPreferences appCenterPreferences = getActivity().getSharedPreferences("AppCenter", Context.MODE_PRIVATE);
appCenterPreferences.edit().remove(Crashes.PREF_KEY_ALWAYS_SEND).apply();
Toast.makeText(getActivity(), R.string.clear_crash_user_confirmation_toast, Toast.LENGTH_SHORT).show();
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.microsoft.appcenter.sasquatch.listeners;

import android.content.Context;
import android.os.SystemClock;
import android.support.annotation.VisibleForTesting;
import android.support.test.espresso.idling.CountingIdlingResource;
import android.widget.Toast;
Expand All @@ -10,6 +11,7 @@
import com.microsoft.appcenter.ingestion.models.LogWithProperties;
import com.microsoft.appcenter.ingestion.models.one.CommonSchemaLog;
import com.microsoft.appcenter.sasquatch.R;
import com.microsoft.appcenter.utils.HandlerUtils;

import org.json.JSONObject;

Expand All @@ -20,16 +22,22 @@ public class SasquatchAnalyticsListener implements com.microsoft.appcenter.analy

private final Context mContext;

private static final long TOAST_DELAY = 2000;

private long mLastToastTime;

private long mPendingLogCount;

public SasquatchAnalyticsListener(Context context) {
this.mContext = context;
}

@Override
public void onBeforeSending(com.microsoft.appcenter.ingestion.models.Log log) {
if (log instanceof EventLog || log instanceof CommonSchemaLog) {
Toast.makeText(mContext, R.string.event_before_sending, Toast.LENGTH_SHORT).show();
notifyBeforeSending(mContext.getString(R.string.event_before_sending));
} else if (log instanceof PageLog) {
Toast.makeText(mContext, R.string.page_before_sending, Toast.LENGTH_SHORT).show();
notifyBeforeSending(mContext.getString(R.string.page_before_sending));
}
analyticsIdlingResource.increment();
}
Expand All @@ -43,8 +51,7 @@ public void onSendingFailed(com.microsoft.appcenter.ingestion.models.Log log, Ex
message = mContext.getString(R.string.page_sent_failed);
}
if (message != null) {
message = String.format("%s\nException: %s", message, e.toString());
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
notifySendingCompletion(String.format("%s\nException: %s", message, e.toString()));
}
analyticsIdlingResource.decrement();
}
Expand All @@ -69,8 +76,39 @@ public void onSendingSucceeded(com.microsoft.appcenter.ingestion.models.Log log)
}
}
if (message != null) {
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
notifySendingCompletion(message);
}
analyticsIdlingResource.decrement();
}

private void notifyBeforeSending(String message) {
if (mPendingLogCount++ == 0) {
showOrDelayToast(message);
}
}

private void notifySendingCompletion(String message) {
if (--mPendingLogCount == 0) {
showOrDelayToast(message);
}
}

private void showOrDelayToast(final String message) {
long now = SystemClock.uptimeMillis();
long timeToWait = mLastToastTime + TOAST_DELAY - now;
if (timeToWait <= 0) {
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
mLastToastTime = now;
} else {
mLastToastTime = now + timeToWait;
HandlerUtils.getMainHandler().postDelayed(new Runnable() {

@Override
public void run() {
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
}
}, timeToWait);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import static com.microsoft.appcenter.sasquatch.activities.MainActivity.LOG_TAG;

@SuppressWarnings("TryFinallyCanBeTryWithResources")
public class SasquatchCrashesListener extends AbstractCrashesListener {

@VisibleForTesting
Expand Down Expand Up @@ -148,7 +149,7 @@ public void onSendingSucceeded(ErrorReport report) {
}

private void notifySending(final String message) {
long timeToWait = SystemClock.uptimeMillis() - mBeforeSendingToastTime + TOAST_DELAY;
long timeToWait = mBeforeSendingToastTime + TOAST_DELAY - SystemClock.uptimeMillis();
if (timeToWait <= 0) {
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
} else {
Expand Down Expand Up @@ -234,7 +235,7 @@ private byte[] getFileAttachmentData() throws SecurityException {

private String getFileAttachmentMimeType() {
String mimeType;
if (mFileAttachment.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
if (ContentResolver.SCHEME_CONTENT.equals(mFileAttachment.getScheme())) {
mimeType = mContext.getContentResolver().getType(mFileAttachment);
} else {
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(mFileAttachment.toString());
Expand Down
Loading

0 comments on commit 74f2eb5

Please sign in to comment.