package com.braintreepayments.popupbridge.example.test;

import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static com.lukekorth.deviceautomator.AutomatorAction.click;
import static com.lukekorth.deviceautomator.DeviceAutomator.onDevice;
import static com.lukekorth.deviceautomator.UiObjectMatcher.withContentDescription;

@RunWith(AndroidJUnit4.class)
public class PopupBridgeTest {

    private static final long BROWSER_TIMEOUT = 20000;

    @Before
    public void setup() {
        onDevice().onHomeScreen().launchApp("com.braintreepayments.popupbridge.example");
    }

    @Test(timeout = 30000)
    public void opensPopup_whenClickingRed_returnsRedColor() {
       testColor("Red");
    }

    @Test(timeout = 30000)
    public void opensPopup_whenClickingGreen_returnsGreenColor() {
        testColor("Green");
    }

    @Test(timeout = 30000)
    public void opensPopup_whenClickingBlue_returnsBlueColor() {
        testColor("Blue");
    }

    @Test(timeout = 30000)
    public void opensPopup_whenClickingDontLikeAnyOfTheseColors_returnsCanceledSelection() {
        onDevice(withContentDescription("Launch Popup")).perform(click());
        onDevice(withContentDescription("I don't like any of these colors"))
                .waitForExists(BROWSER_TIMEOUT).perform(click());
        onDevice(withContentDescription("You do not like any of these colors")).waitForExists();
    }

    @Test(timeout = 30000)
    public void opensPopup_whenClickingBack_returnsCanceledSelection() {
        onDevice(withContentDescription("Launch Popup")).perform(click());
        onDevice(withContentDescription("I don't like any of these colors"))
                .waitForExists(BROWSER_TIMEOUT);
        onDevice().pressBack();
        onDevice(withContentDescription("You did not choose a color")).waitForExists();
    }

    private void testColor(String color) {
        onDevice(withContentDescription("Launch Popup")).perform(click());
        onDevice(withContentDescription(color)).waitForExists(BROWSER_TIMEOUT).perform(click());
        onDevice(withContentDescription("Your favorite color:")).waitForExists();
        onDevice(withContentDescription(color.toLowerCase())).waitForExists();
    }
}

@@ -50,16 +50,41 @@
 .test;%0A%0A
+import android.os.Build;%0A
 import a
@@ -126,16 +126,71 @@
 Unit4;%0A%0A
+import com.lukekorth.deviceautomator.DeviceAutomator;%0A%0A
 import o
@@ -483,16 +483,86 @@
 ription;
+%0Aimport static com.lukekorth.deviceautomator.UiObjectMatcher.withText;
 %0A%0A@RunWi
@@ -1319,37 +1319,20 @@
   on
-Device(withContentDescription
+ViewWithText
 (%22La
@@ -1335,33 +1335,32 @@
 (%22Launch Popup%22)
-)
 .perform(click()
@@ -1376,37 +1376,20 @@
   on
-Device(withContentDescription
+ViewWithText
 (%22I 
@@ -1412,33 +1412,32 @@
 f these colors%22)
-)
 %0A               
@@ -1501,37 +1501,20 @@
   on
-Device(withContentDescription
+ViewWithText
 (%22Yo
@@ -1548,17 +1548,16 @@
 colors%22)
-)
 .waitFor
@@ -1687,37 +1687,20 @@
   on
-Device(withContentDescription
+ViewWithText
 (%22La
@@ -1703,33 +1703,32 @@
 (%22Launch Popup%22)
-)
 .perform(click()
@@ -1744,37 +1744,20 @@
   on
-Device(withContentDescription
+ViewWithText
 (%22I 
@@ -1788,17 +1788,16 @@
 colors%22)
-)
 %0A       
@@ -1884,37 +1884,20 @@
   on
-Device(withContentDescription
+ViewWithText
 (%22Yo
@@ -1922,17 +1922,16 @@
  color%22)
-)
 .waitFor
@@ -2004,37 +2004,20 @@
   on
-Device(withContentDescription
+ViewWithText
 (%22La
@@ -2028,17 +2028,16 @@
  Popup%22)
-)
 .perform
@@ -2061,45 +2061,27 @@
   on
-Device(withContentDescription
+ViewWithText
 (color)
-)
 .wai
@@ -2140,37 +2140,20 @@
   on
-Device(withContentDescription
+ViewWithText
 (%22Yo
@@ -2172,17 +2172,16 @@
 color:%22)
-)
 .waitFor
@@ -2204,37 +2204,20 @@
   on
-Device(withContentDescription
+ViewWithText
 (col
@@ -2233,17 +2233,16 @@
 rCase())
-)
 .waitFor
@@ -2256,11 +2256,248 @@
 );%0A    %7D
+%0A%0A    private DeviceAutomator onViewWithText(String text) %7B%0A        if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.P) %7B%0A            return onDevice(withText(text));%0A        %7D%0A        return onDevice(withContentDescription(text));%0A    %7D
 %0A%7D%0A

Check for text rather than content desc for Android P