package org.gdg.frisbee.android.activity;

import android.app.Notification;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
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 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.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.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

@RunWith(AndroidJUnit4.class)
public class NotificationTest {
    @Rule
    public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);
    private UiDevice mDevice;

    @Before
    public void setUp() throws Exception {
        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
    }

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

            mDevice.openNotification();
            mDevice.findObject(By.textContains(rule.getActivity().getString(series.getTitleResId())))
                .click();
            onView(withText(series.getDescriptionResId())).check(matches(isDisplayed()));
        }
    }
}

@@ -344,34 +344,40 @@
 ort.
-test.uiaut
+v4.app.NotificationManagerC
 om
+p
 at
-or.UiObject2
 ;%0Aim
@@ -402,39 +402,24 @@
 rt.v
-4.app.NotificationManagerCompat
+7.widget.Toolbar
 ;%0A%0Ai
@@ -632,24 +632,48 @@
 ventSeries;%0A
+import org.junit.After;%0A
 import org.j
@@ -826,16 +826,88 @@
 onView;%0A
+import static android.support.test.espresso.action.ViewActions.swipeUp;%0A
 import s
@@ -972,24 +972,189 @@
 ns.matches;%0A
+import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;%0Aimport static android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA;%0A
 import stati
@@ -1219,16 +1219,89 @@
 played;%0A
+import static android.support.test.espresso.matcher.ViewMatchers.isRoot;%0A
 import s
@@ -1366,16 +1366,61 @@
 ithText;
+%0Aimport static org.hamcrest.core.AllOf.allOf;
 %0A%0A@RunWi
@@ -1766,63 +1766,292 @@
    @
-Test%0A    public void testNotificationForEventSeries() %7B
+After%0A    public void tearDown() throws Exception %7B%0A        onView(isRoot()).perform(swipeUp());%0A    %7D%0A%0A    @Test%0A    public void testNotificationForEventSeries() throws InterruptedException %7B%0A        NotificationManagerCompat nm = NotificationManagerCompat.from(rule.getActivity());%0A
 %0A   
@@ -2284,58 +2284,10 @@
     
-NotificationManagerCompat.from(rule.getActivity())
+nm
 .not
@@ -2341,32 +2341,64 @@
 Notification();%0A
+            Thread.sleep(2000);%0A
             mDev
@@ -2525,53 +2525,182 @@
     
-onView(withText(series.getDescriptionResId())
+Thread.sleep(2000);%0A            onView(allOf(%0A                withText(series.getTitleResId()),%0A                isDescendantOfA(isAssignableFrom(Toolbar.class)))%0A            
 ).ch

Improve notifications test to be more reliable