ChatRoomFragmentTest.kt 2.07 KB
Newer Older
1 2 3
package chat.rocket.android.chatroom.ui

import android.content.Intent
4 5
import androidx.test.espresso.intent.rule.IntentsTestRule
import androidx.test.filters.LargeTest
6 7 8 9
import org.junit.Rule
import org.junit.Test
import android.app.Activity
import android.app.Instrumentation.ActivityResult
10 11 12 13
import androidx.test.InstrumentationRegistry
import androidx.test.espresso.intent.Intents.intended
import androidx.test.espresso.intent.Intents.intending
import androidx.test.espresso.intent.matcher.IntentMatchers.*
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
import org.hamcrest.Matchers.allOf
import org.hamcrest.Matchers.not
import org.junit.Before

@LargeTest
class ChatRoomFragmentTest {

    @JvmField
    @Rule
    val activityRule = IntentsTestRule<ChatRoomActivity>(ChatRoomActivity::class.java, false, false)

    @Before
    fun stubAllExternalIntents() {
        val activityIntent = InstrumentationRegistry.getTargetContext().chatRoomIntent("id", "name", "type", false, 0L)
        activityRule.launchActivity(activityIntent)
        intending(not(isInternal())).respondWith(ActivityResult(Activity.RESULT_OK, null))
    }

    @Test
    fun showFileSelection_nonNullFiltersAreApplied() {
        val fragment = activityRule.activity.supportFragmentManager.findFragmentByTag(ChatRoomActivity.TAG_CHAT_ROOM_FRAGMENT) as ChatRoomFragment

        val filters = arrayOf("image/*")
        fragment.showFileSelection(filters)

        intended(allOf(
                hasAction(Intent.ACTION_GET_CONTENT),
                hasType("*/*"),
                hasCategories(setOf(Intent.CATEGORY_OPENABLE)),
                hasExtra(Intent.EXTRA_MIME_TYPES, filters)))
    }

    @Test
    fun showFileSelection_nullFiltersAreNotApplied() {
        val fragment = activityRule.activity.supportFragmentManager.findFragmentByTag(ChatRoomActivity.TAG_CHAT_ROOM_FRAGMENT) as ChatRoomFragment

        fragment.showFileSelection(null)

        intended(allOf(
                hasAction(Intent.ACTION_GET_CONTENT),
                hasType("*/*"),
                hasCategories(setOf(Intent.CATEGORY_OPENABLE)),
                not(hasExtraWithKey(Intent.EXTRA_MIME_TYPES))))
    }
}