Skip to content

Commit

Permalink
Merge pull request #864 from Microsoft/feature/ui-multiple-logs
Browse files Browse the repository at this point in the history
Send multiple events at once in UI
  • Loading branch information
guperrot committed Nov 5, 2018
2 parents 42e4884 + 97c15a7 commit 47c65bd
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 46 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 @@ -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 @@ -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 @@ -141,8 +141,10 @@ public void onClick(View v) {
});

/* Hide persistence flag UI as not supported by jCenter */
findViewById(R.id.persistence_flag_label).setVisibility(View.GONE);
findViewById(R.id.persistence_flag_spinner).setVisibility(View.GONE);
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
@@ -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
18 changes: 15 additions & 3 deletions apps/sasquatch/src/main/res/layout/activity_event.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,30 @@
android:visibility="gone" />

<TextView
android:id="@+id/persistence_flag_label"
android:id="@+id/event_priority_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/persistence_flag" />
android:text="@string/event_priority" />

<Spinner
android:id="@+id/persistence_flag_spinner"
android:id="@+id/event_priority_spinner"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />

<TextView
android:id="@+id/number_of_logs_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/number_of_logs" />

<SeekBar
android:id="@+id/number_of_logs"
android:layout_width="match_parent"
android:layout_height="50dp"
android:max="1000" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
5 changes: 3 additions & 2 deletions apps/sasquatch/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@
<string name="max_storage_size_change_success" tools:ignore="MissingTranslation">Max storage size has changed to %s successfully. Check SDK logs for actual size of storage.</string>
<string name="max_storage_size_change_failed" tools:ignore="MissingTranslation">Failed to change max storage size.</string>
<string name="size_in_bytes" tools:ignore="MissingTranslation">Size in bytes</string>
<string name="persistence_flag" tools:ignore="MissingTranslation">Persistence Flag</string>
<string-array name="persistence_flag_values" tools:ignore="MissingTranslation">
<string name="event_priority" tools:ignore="MissingTranslation">Event Priority</string>
<string-array name="event_priority_values" tools:ignore="MissingTranslation">
<item>Default</item>
<item>Normal</item>
<item>Critical</item>
<item>Invalid</item>
</string-array>
<string name="number_of_logs" tools:ignore="MissingTranslation">Number of logs: %d</string>
</resources>
Loading

0 comments on commit 47c65bd

Please sign in to comment.