MainActivityTest.kt 2.88 KB
Newer Older
1 2
package fr.chenry.android.freshrss.activities

3 4 5
import android.app.Activity
import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout
6
import androidx.test.espresso.Espresso.onView
7
import androidx.test.espresso.NoActivityResumedException
8
import androidx.test.espresso.action.ViewActions.*
9 10 11 12 13
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.DrawerActions
import androidx.test.espresso.contrib.DrawerMatchers.isClosed
import androidx.test.espresso.contrib.DrawerMatchers.isOpen
import androidx.test.espresso.matcher.ViewMatchers.*
14
import androidx.test.ext.junit.rules.ActivityScenarioRule
15 16
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
17 18 19
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry
import androidx.test.runner.lifecycle.Stage
20
import fr.chenry.android.freshrss.R
21
import fr.chenry.android.freshrss.utils.FreshRSSBaseTest
22
import junit.framework.TestCase.assertFalse
23
import org.junit.Assert.*
24 25 26 27 28 29
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@LargeTest
30
class MainActivityTest: FreshRSSBaseTest() {
31
    @get:Rule
32
    val activityRule = ActivityScenarioRule(MainActivity::class.java)
33

34
    @Test
35
    fun `drawer-is-closed-when-adding-a-new-subscription`() {
36 37 38 39
        // Open drawer
        onView(withId(R.id.activity_main_navigation_drawer)).perform(DrawerActions.open())
        onView(withId(R.id.activity_main_navigation_drawer)).check(matches(isOpen()))
        onView(withText(R.string.title_add_subscription)).perform(click())
40 41 42 43 44 45 46 47 48

        val drawer = getActivityInstance().findViewById<DrawerLayout>((R.id.activity_main_navigation_drawer))
        assertFalse(drawer.isDrawerOpen(GravityCompat.START))
    }

    @Test
    fun `drawer-is-closed-before-application-exits`() {
        onView(withId(R.id.activity_main_navigation_drawer)).perform(DrawerActions.open())
        onView(withId(R.id.activity_main_navigation_drawer)).check(matches(isOpen()))
49
        onView(isRoot()).perform(pressBack())
50
        onView(withId(R.id.activity_main_navigation_drawer)).check(matches(isClosed()))
51 52

        // We assert that pressing back again exists the app
53 54 55
        assertThrows(NoActivityResumedException::class.java) {
            onView(isRoot()).perform(pressBack())
        }
56 57
    }

58 59 60 61 62 63 64 65 66 67
    // See https://stackoverflow.com/a/60341723/3459089
    private fun getActivityInstance(): Activity {
        val currentActivity = arrayOf<Activity?>(null)
        InstrumentationRegistry.getInstrumentation().runOnMainSync {
            val resumedActivity = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED)
            val it: Iterator<Activity> = resumedActivity.iterator()
            currentActivity[0] = it.next()
        }
        return currentActivity[0]!!
    }
68
}