package com.braintreepayments.demo.test;

import android.support.test.filters.SdkSuppress;
import android.support.test.runner.AndroidJUnit4;

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

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

import static com.braintreepayments.demo.test.utilities.AppInstallationHelper.uninstallPayPalWallet;
import static com.lukekorth.deviceautomator.AutomatorAction.click;
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.withText;
import static com.lukekorth.deviceautomator.UiObjectMatcher.withTextStartingWith;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.endsWith;

@RunWith(AndroidJUnit4.class)
@SdkSuppress(minSdkVersion = 21)
public class PayPalBrowserSwitchTest extends TestHelper {

    @Before
    public void setup() {
        super.setup();
        uninstallPayPalWallet();
        onDevice(withText("PayPal")).waitForEnabled().perform(click());
    }

    @Test(timeout = 60000)
    public void browserSwitch_makesASinglePayment() {
        onDevice(withText("Single Payment")).waitForEnabled().perform(click());
        onDevice(withContentDescription("Proceed with Sandbox Purchase")).perform(click());

        getNonceDetails().check(text(containsString("Email: bt_buyer_us@paypal.com")));

        onDevice(withText("Create a Transaction")).perform(click());
        onDevice(withTextStartingWith("created")).check(text(endsWith("authorized")));
    }

    @Test(timeout = 60000)
    public void browserSwitch_makesAFuturePayment() {
        onDevice(withText("Future Payment")).waitForEnabled().perform(click());

        try {
            onDevice(withContentDescription("Email")).perform(click());
        } catch (RuntimeException ignored) {}

        onDevice().typeText("test@paypal.com");
        onDevice().pressTab().typeText("password");
        onDevice(withContentDescription("Log In")).perform(click());
        onDevice(withContentDescription("Agree")).perform(click());

        getNonceDetails().check(text(containsString("Email: jane.doe@example.com")));

        onDevice(withText("Create a Transaction")).perform(click());
        onDevice(withTextStartingWith("created")).check(text(endsWith("authorized")));
    }

    @Test(timeout = 60000)
    public void browserSwitch_makesAFuturePaymentWithAddressScope() {
        onDevice(withText("Future Payment (Address Scope)")).waitForEnabled().perform(click());

        try {
            onDevice(withContentDescription("Email")).perform(click());
        } catch (RuntimeException ignored) {}

        onDevice().typeText("test@paypal.com");
        onDevice().pressTab().typeText("password");
        onDevice(withContentDescription("Log In")).perform(click());
        onDevice(withContentDescription("Agree")).perform(click());

        getNonceDetails().check(text(containsString("Email: jane.doe@example.com")));

        onDevice(withText("Create a Transaction")).perform(click());
        onDevice(withTextStartingWith("created")).check(text(endsWith("authorized")));
    }

    @Test(timeout = 60000)
    public void browserSwitch_makesABillingAgreement() {
        onDevice(withText("Billing Agreement")).waitForEnabled().perform(click());
        onDevice(withContentDescription("Proceed with Sandbox Purchase")).perform(click());

        getNonceDetails().check(text(containsString("Email: bt_buyer_us@paypal.com")));

        onDevice(withText("Create a Transaction")).perform(click());
        onDevice(withTextStartingWith("created")).check(text(endsWith("authorized")));
    }
}

@@ -1398,34 +1398,20 @@
 ice(with
-ContentDescription
+Text
 (%22Procee
@@ -2124,34 +2124,20 @@
 ice(with
-ContentDescription
+Text
 (%22Log In
@@ -2179,34 +2179,20 @@
 ice(with
-ContentDescription
+Text
 (%22Agree%22
@@ -2911,34 +2911,20 @@
 ice(with
-ContentDescription
+Text
 (%22Log In
@@ -2966,34 +2966,20 @@
 ice(with
-ContentDescription
+Text
 (%22Agree%22
@@ -3438,34 +3438,20 @@
 ice(with
-ContentDescription
+Text
 (%22Procee

Fix PayPal browser switch tests in demo app