package com.braintreepayments.demo.test;

import android.support.test.filters.RequiresDevice;
import android.support.test.filters.SdkSuppress;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;

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

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

import static com.braintreepayments.testutils.AppInstallationHelper.uninstallPayPalWallet;
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.withText;
import static com.lukekorth.deviceautomator.UiObjectMatcher.withTextContaining;
import static com.lukekorth.deviceautomator.UiObjectMatcher.withTextStartingWith;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.core.StringEndsWith.endsWith;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class PaymentButtonTest extends TestHelper {

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

    @SdkSuppress(minSdkVersion = 21)
    @Test(timeout = 60000)
    public void tokenizesPayPal() {
        uninstallPayPalWallet();

        onDevice(withContentDescription("Pay with PayPal")).waitForExists().perform(click());
        onDevice(withContentDescription("Email")).perform(click(), setText("test@paypal.com"));
        onDevice().pressDPadDown().typeText("password");
        onDevice(withContentDescription("Log In")).perform(click());
        onDevice(withContentDescription("Agree")).perform(click());

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

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

    @RequiresDevice
    @Test(timeout = 60000)
    public void tokenizesAndroidPay() {
        onDevice(withContentDescription("Pay with Android Pay")).perform(click());
        onDevice(withText("CONTINUE")).perform(click());

        onDevice(withTextStartingWith("Underlying Card Last Two"))
                .check(text(containsString("Underlying Card Last Two")));

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

@@ -548,77 +548,8 @@
 ck;%0A
-import static com.lukekorth.deviceautomator.AutomatorAction.setText;%0A
 impo
@@ -1628,234 +1628,36 @@
 on(%22
-Email%22)).perform(click(), setText(%22test@paypal.com%22));%0A        onDevice().pressDPadDown().typeText(%22password%22);%0A        onDevice(withContentDescription(%22Log In%22)).perform(click());%0A        onDevice(withContentDescription(%22Agre
+Proceed with Sandbox Purchas
 e%22))
@@ -1754,24 +1754,26 @@
 ng(%22
-jane.doe@example
+bt_buyer_us@paypal
 .com

Update PaymentButtonTest to use billing agreements