package com.braintreepayments.demo.test;

import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;

import com.braintreepayments.demo.test.utilities.TestHelper;

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

import static com.lukekorth.deviceautomator.AutomatorAction.clearTextField;
import static com.lukekorth.deviceautomator.AutomatorAction.click;
import static com.lukekorth.deviceautomator.AutomatorAction.setText;
import static com.lukekorth.deviceautomator.AutomatorAssertion.text;
import static com.lukekorth.deviceautomator.DeviceAutomator.onDevice;
import static com.lukekorth.deviceautomator.UiObjectMatcher.withContentDescription;
import static com.lukekorth.deviceautomator.UiObjectMatcher.withResourceId;
import static com.lukekorth.deviceautomator.UiObjectMatcher.withText;
import static org.hamcrest.Matchers.containsString;

@RunWith(AndroidJUnit4.class)
public class VisaCheckoutTest extends TestHelper {

    private static final String VISA_CHECKOUT_USERNAME = "no-reply-visa-checkout@getbraintree.com";
    private static final String VISA_CHECKOUT_PASSWORD = "12345678";

    @Before
    public void setup() {
        super.setup();
        onDevice(withText("Visa Checkout")).waitForEnabled().perform(click());
        onDevice(withContentDescription("Visa Checkout")).waitForExists().perform(click());
    }

    @Test(timeout = 60000)
    public void cancelsVisaCheckout_whenPressingBack() {
        onDevice().pressBack(); // Dismiss keyboard
        onDevice().pressBack();
        onDevice(withText("OK")).perform(click());
        onDevice(withText("Reset")).waitForExists();
    }

    @Test(timeout = 60000)
    public void cancelsVisaCheckout_whenClickingUpButton() {
        onDevice(withResourceId("com.braintreepayments.demo:id/vco_header_right_btn")).perform(click());
        onDevice(withText("OK")).perform(click());
        onDevice(withText("Reset")).waitForExists();
    }

    @Test(timeout = 60000)
    public void tokenizesVisaCheckout() throws UiObjectNotFoundException {
        String rememberMeResourceId = "com.braintreepayments.demo:id/com_visa_checkout_cbSignInUsernamePreferences";

        // TODO Device Automator doesn't check for checked inputs
        if (new UiObject(new UiSelector().resourceId(rememberMeResourceId)).isChecked()) {
            onDevice(withResourceId(rememberMeResourceId)).perform(click());
        }

        onDevice(withContentDescription("Email or Mobile Number")).perform(click(), clearTextField(),
                setText(VISA_CHECKOUT_USERNAME));
        onDevice(withContentDescription("Password")).perform(click());
        onDevice().typeText(VISA_CHECKOUT_PASSWORD);
        onDevice().pressBack(); // Dismiss keyboard
        onDevice(withText("Sign In")).perform(click());
        onDevice(withText("Pay with")).waitForExists();
        onDevice(withText("Continue")).perform(click());
        onDevice(withText("Create a Transaction")).waitForExists();
        onDevice(withText("Nonce:")).waitForExists();
        getNonceDetails().check(text(containsString("First name: NoReply")));
        getNonceDetails().check(text(containsString("Last name: VisaCheckout")));
        getNonceDetails().check(text(containsString("User name: no-reply-visa-checkout@getbraintree.com")));
        getNonceDetails().check(text(containsString("Email: no-reply-visa-checkout@getbraintree.com")));
        getNonceDetails().check(text(containsString(
                "Billing Address: NoReply VisaCheckout 123 Townsend Street San Francisco 94107 CA US")));
        getNonceDetails().check(text(containsString(
                "Shipping Address: NoReply VisaCheckout 123 Townsend Street San Francisco 94107 CA US")));
    }
}

@@ -546,77 +546,8 @@
 ck;%0A
-import static com.lukekorth.deviceautomator.AutomatorAction.setText;%0A
 impo
@@ -2254,24 +2254,251 @@
 ferences%22;%0A%0A
+        if (new UiObject(new UiSelector().text(%22New To Visa Checkout?%22)).exists()) %7B%0A            // If we land in the sign up page, move to the login page.%0A            onDevice(withText(%22Sign In%22)).perform(click());%0A        %7D%0A%0A
         // T
@@ -2830,17 +2830,18 @@
 tField()
-,
+);
 %0A       
@@ -2845,19 +2845,71 @@
     
-        set
+onDevice(withContentDescription(%22Email or Mobile Number%22)).type
 Text
@@ -2932,17 +2932,16 @@
 SERNAME)
-)
 ;%0A      

Upgrade to device-automator 0.1.1