Skip to content

Commit

Permalink
Improve notifications test to be more reliable
Browse files Browse the repository at this point in the history
Since UiDevice is not like Espresso, it does not wait for ui actions/animations to complete. For that I added sleeps
  • Loading branch information
tasomaniac committed Oct 28, 2016
1 parent 87e8199 commit 056d4f4
Showing 1 changed file with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,28 @@
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.widget.Toolbar;

import org.gdg.frisbee.android.app.App;
import org.gdg.frisbee.android.chapter.MainActivity;
import org.gdg.frisbee.android.eventseries.NotificationHandler;
import org.gdg.frisbee.android.eventseries.TaggedEventSeries;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.swipeUp;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.core.AllOf.allOf;

@RunWith(AndroidJUnit4.class)
public class NotificationTest {
Expand All @@ -34,17 +40,29 @@ public void setUp() throws Exception {
mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
}

@After
public void tearDown() throws Exception {
onView(isRoot()).perform(swipeUp());
}

@Test
public void testNotificationForEventSeries() {
public void testNotificationForEventSeries() throws InterruptedException {
NotificationManagerCompat nm = NotificationManagerCompat.from(rule.getActivity());

int i = 0;
for (TaggedEventSeries series : App.getInstance().currentTaggedEventSeries()) {
Notification notification = new NotificationHandler(rule.getActivity(), series).createNotification();
NotificationManagerCompat.from(rule.getActivity()).notify(i, notification);
nm.notify(i, notification);

mDevice.openNotification();
Thread.sleep(2000);
mDevice.findObject(By.textContains(rule.getActivity().getString(series.getTitleResId())))
.click();
onView(withText(series.getDescriptionResId())).check(matches(isDisplayed()));
Thread.sleep(2000);
onView(allOf(
withText(series.getTitleResId()),
isDescendantOfA(isAssignableFrom(Toolbar.class)))
).check(matches(isDisplayed()));
}
}
}

0 comments on commit 056d4f4

Please sign in to comment.