package bou.amine.apps.readerforselfossv2.android

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.scrollCompletelyTo
import androidx.test.espresso.action.ViewActions.typeTextIntoFocusedView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.util.UUID

@RunWith(AndroidJUnit4::class)
@LargeTest
class SourcesActivityTest {

    @get:Rule
    val activityRule = ActivityScenarioRule(LoginActivity::class.java)

    lateinit var sourceName: String

    @Before
    fun init() {
        sourceName = UUID.randomUUID().toString().substring(0, 15)

        loginAndInitHome()
        openActionBarOverflowOrOptionsMenu(
            ApplicationProvider.getApplicationContext<Context>()
        )
        onView(withText(R.string.menu_home_sources))
            .perform(click())
    }

    @Test
    fun addSource() {
        onView(withId(R.id.fab))
            .perform(click())
        onView(withId(R.id.nameInput))
            .perform(click()).perform(typeTextIntoFocusedView(sourceName))
        onView(withId(R.id.sourceUri))
            .perform(click())
            .perform(typeTextIntoFocusedView("https://lorem-rss.herokuapp.com/feed?unit=year&interval=1&length=10"))
        onView(withId(R.id.tags))
            .perform(click()).perform(typeTextIntoFocusedView("tag1,tag2,tag3"))
        onView(withId(R.id.spoutsSpinner))
            .perform(click())
        onView(withText("RSS Feed"))
            .perform(scrollCompletelyTo())
            .perform(click())
        onView(withId(R.id.saveBtn))
            .perform(click())
        onView(withText(sourceName)).check(matches(isDisplayed()))
    }

}