package com.braintreepayments.demo.test;

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.PAYPAL_WALLET_PACKAGE_NAME;
import static com.braintreepayments.testutils.AppInstallationHelper.installPayPalWallet;
import static com.lukekorth.deviceautomator.AutomatorAction.click;
import static com.lukekorth.deviceautomator.AutomatorAssertion.contentDescription;
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 org.hamcrest.Matchers.containsString;

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

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

    @Test(timeout = 120000)
    public void appSwitch_forSinglePayment() {
        onDevice(withText("Single Payment")).waitForEnabled().perform(click());

        onDevice().checkForegroundAppIs(PAYPAL_WALLET_PACKAGE_NAME);
    }

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

        onDevice().checkForegroundAppIs(PAYPAL_WALLET_PACKAGE_NAME);
    }

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

        onDevice().checkForegroundAppIs(PAYPAL_WALLET_PACKAGE_NAME);
    }

    @Test(timeout = 120000)
    public void appSwitch_forBillingAgreement() {
        onDevice(withText("Billing Agreement")).waitForEnabled().perform(click());

        onDevice().checkForegroundAppIs(PAYPAL_WALLET_PACKAGE_NAME);
    }

    @Test(timeout = 120000)
    public void appSwitch_postsACancelWhenBackButtonIsPressed() {
        onDevice(withText("Single Payment")).waitForEnabled().perform(click());
        onDevice().checkForegroundAppIs(PAYPAL_WALLET_PACKAGE_NAME);

        onDevice(withContentDescription("Proceed with Sandbox Purchase"))
                .check(contentDescription(containsString("Proceed with Sandbox Purchase")));
        onDevice().pressBack();
        onDevice(withText("Yes")).perform(click());

        onDevice().checkForegroundAppIs("com.braintreepayments.demo");
        onDevice(withText("Single Payment")).check(text(containsString("Single Payment")));
    }
}

@@ -604,249 +604,82 @@
 ion.
-contentDescription;%0Aimport static com.lukekorth.deviceautomator.AutomatorAssertion.text;%0Aimport static com.lukekorth.deviceautomator.DeviceAutomator.onDevice;%0Aimport static com.lukekorth.deviceautomator.UiObjectMatcher.withContentDescription
+text;%0Aimport static com.lukekorth.deviceautomator.DeviceAutomator.onDevice
 ;%0Aim
@@ -2301,151 +2301,37 @@
 with
-ContentDescription(%22Proceed with Sandbox Purchase%22))%0A                .check(contentDescription(containsString(%22Proceed with Sandbox Purchase%22))
+Text(%22Email%22)).waitForExists(
 );%0A 
@@ -2359,16 +2359,28 @@
 ssBack()
+.pressBack()
 ;%0A      
@@ -2404,11 +2404,10 @@
 xt(%22
-Yes
+OK
 %22)).

Fix PayPal app switch cancel test