package com.braintreepayments.demo.test;

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

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

import androidx.preference.PreferenceManager;
import androidx.test.runner.AndroidJUnit4;

import static androidx.test.InstrumentationRegistry.getTargetContext;
import static com.lukekorth.deviceautomator.AutomatorAction.click;
import static com.lukekorth.deviceautomator.AutomatorAction.setText;
import static com.lukekorth.deviceautomator.DeviceAutomator.onDevice;
import static com.lukekorth.deviceautomator.UiObjectMatcher.withText;

@RunWith(AndroidJUnit4.class)
public class ThreeDSecureCardinalTest extends TestHelper {
    @Before
    public void setup() {
        super.setup();
        onDevice(withText("Credit or Debit Cards")).waitForEnabled().perform(click());

        PreferenceManager.getDefaultSharedPreferences(getTargetContext())
                .edit()
                .putBoolean("enable_three_d_secure", true)
                .commit();
    }

    @Test
    public void threeDSecure_authenticates() {
        onDevice(withText("Card Number")).perform(setText("4000000000001091"));
        fillInExpiration("01", "2022");
        onDevice(withText("CVV")).perform(setText("123"));
        onDevice(withText("Postal Code")).perform(setText("12345"));
        onDevice(withText("Purchase")).perform(click());
    }
}

@@ -471,24 +471,93 @@
 on.setText;%0A
+import static com.lukekorth.deviceautomator.AutomatorAssertion.text;%0A
 import stati
@@ -683,16 +683,207 @@
 ithText;
+%0Aimport static com.lukekorth.deviceautomator.UiObjectMatcher.withTextStartingWith;%0Aimport static org.hamcrest.Matchers.containsString;%0Aimport static org.hamcrest.core.StringEndsWith.endsWith;
 %0A%0A@RunWi
@@ -965,16 +965,17 @@
 elper %7B%0A
+%0A
     @Bef
@@ -1315,16 +1315,33 @@
    @Test
+(timeout = 40000)
 %0A    pub
@@ -1684,16 +1684,1017 @@
 lick());
+%0A%0A        onDevice(withText(%22Code%22)).perform(setText(%221234%22));%0A        onDevice(withText(%22Submit%22)).perform(click());%0A%0A        workAroundForCardinalUiNotClosing();%0A%0A        getNonceDetails().check(text(containsString(%22Card Last Two: 91%22)));%0A        getNonceDetails().check(text(containsString(%22isLiabilityShifted: true%22)));%0A        getNonceDetails().check(text(containsString(%22isLiabilityShiftPossible: true%22)));%0A%0A        onDevice(withText(%22Create a Transaction%22)).perform(click());%0A        onDevice(withTextStartingWith(%22created%22)).check(text(endsWith(%22authorized%22)));%0A    %7D%0A%0A    /**%0A     * Cardinal SDKs UI does not close on submit.%0A     * They're working on a fix but in the meantime,%0A     * pressing back to exit out of the UI works.%0A     */%0A    private void workAroundForCardinalUiNotClosing() %7B%0A        try %7B%0A            // We need to sleep for a bit or Cardinal considers this a cancel%0A            Thread.sleep(4000);%0A        %7D catch (InterruptedException e) %7B%7D%0A%0A        onDevice().pressBack();
 %0A    %7D%0A%7D

Add happy path UI test for Cardinal