package io.trustbuilder.mfa.android.demoapp;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;

import androidx.test.espresso.assertion.ViewAssertions;
import androidx.test.ext.junit.rules.ActivityScenarioRule;

import io.trustbuilder.mfa.android.demoapp.screen.PendingAuthSessionsRetrievalActivity;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class PendingAuthSessionsRetrievalActivityTest {

    @Rule
    public ActivityScenarioRule<PendingAuthSessionsRetrievalActivity> mLoginActivityTestRule =
            new ActivityScenarioRule<>(PendingAuthSessionsRetrievalActivity.class);

    @org.junit.Test
    public void onCreate() {
        onView(withId(R.id.tknAlias_GetPendingAuthSessions)).check(ViewAssertions.matches(isDisplayed()));
        onView(withId(R.id.proceed_GetPendingAuthSessions)).check(ViewAssertions.matches(isDisplayed()));
        onView(withId(R.id.result_GetPendingAuthSessions)).check(ViewAssertions.matches(isDisplayed()));
    }

    @Test
    public void onStart() {
        onView(withId(R.id.result_GetPendingAuthSessions)).check(ViewAssertions.matches(withText("")));
        onView(withId(R.id.proceed_GetPendingAuthSessions)).perform(click());
        onView(withId(R.id.result_GetPendingAuthSessions)).check(ViewAssertions.matches(not(withText(""))));
    }

    @Test
    public void onStart_shouldReturnWhenTknAliasIsEmpty() {
        onView(withId(R.id.tknAlias_GetPendingAuthSessions))
                .perform(closeSoftKeyboard());
        onView(withId(R.id.proceed_GetPendingAuthSessions)).perform(click());
        onView(withId(R.id.result_GetPendingAuthSessions))
                .check(ViewAssertions.matches(withText("Error: tknAlias is empty")));
    }

    @Test
    public void onStart_withInput() {
        onView(withId(R.id.tknAlias_GetPendingAuthSessions))
                .perform(typeText("tknAlias"), closeSoftKeyboard());
        onView(withId(R.id.proceed_GetPendingAuthSessions))
                .perform(click());
        onView(withId(R.id.result_GetPendingAuthSessions))
                .check(matches(withText(containsString("IW_LOCAL_TKN_NOT_FOUND_ERR"))));
    }
}