Skip to content

Commit

Permalink
tests: channelFragment and member module
Browse files Browse the repository at this point in the history
  • Loading branch information
GOVINDDIXIT committed Jun 30, 2019
1 parent 125249b commit a9a763b
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package chat.rocket.android.createchannel.ui

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.NoMatchingViewException
import androidx.test.espresso.action.ViewActions.*
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.rule.ActivityTestRule
import chat.rocket.android.R
import chat.rocket.android.analytics.event.ScreenViewEvent
import chat.rocket.android.authentication.ui.AuthenticationActivity
import chat.rocket.android.util.extensions.addFragmentBackStack
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import testConfig.Config.Companion.PASSWORD
import testConfig.Config.Companion.USERNAME
import testConfig.Config.Companion.serverUrl


class CreateChannelFragmentTest {
@JvmField
var activityRule = ActivityTestRule(AuthenticationActivity::class.java, true, true)

@Rule
fun rule() = activityRule

@Before
fun setUp() {
try {
rule().activity.addFragmentBackStack(ScreenViewEvent.Login.screenName, R.id.fragment_container) {
chat.rocket.android.authentication.login.ui.newInstance(serverUrl)
}
onView(withId(R.id.text_username_or_email)).perform(typeText(USERNAME), closeSoftKeyboard())
onView(withId(R.id.text_password)).perform(typeText(PASSWORD), closeSoftKeyboard())
onView(withId(R.id.button_log_in)).perform(click())
Thread.sleep(12000)
onView(withId(R.id.action_new_channel)).perform(click())
} catch (e: NoMatchingViewException) {
onView(withId(R.id.action_new_channel)).perform(click())
}
}

@Test
fun check_UI_element() {
onView(withId(R.id.text_channel_type)).check(matches(isDisplayed()))
onView(withId(R.id.text_channel_type_description)).check(matches(isDisplayed()))
onView(withId(R.id.switch_channel_type)).check(matches(isDisplayed()))
onView(withId(R.id.text_read_only)).check(matches(isDisplayed()))
onView(withId(R.id.text_read_only_description)).check(matches(isDisplayed()))
onView(withId(R.id.switch_read_only)).check(matches(isDisplayed()))
onView(withId(R.id.image_channel_icon)).check(matches(isDisplayed()))
onView(withId(R.id.text_channel_name)).check(matches(isDisplayed()))
onView(withId(R.id.image_invite_member)).check(matches(isDisplayed()))
onView(withId(R.id.text_invite_members)).check(matches(isDisplayed()))
}

@Test
fun channel_should_be_public() {
onView(withId(R.id.text_channel_type)).check(matches(withText("Public")))
onView(withId(R.id.text_channel_type_description)).check(matches(withText(R.string.msg_public_channel_description)))
}

@Test
fun channel_should_be_private() {
onView(withId(R.id.switch_channel_type)).perform(click())
onView(withId(R.id.text_channel_type)).check(matches(withText("Private")))
onView(withId(R.id.switch_channel_type)).perform(click())
}

@Test
fun channel_description_should_change_on_making_channel_private() {
onView(withId(R.id.switch_channel_type)).perform(click())
onView(withId(R.id.text_channel_type_description)).check(matches(withText(R.string.msg_private_channel_description)))
onView(withId(R.id.switch_channel_type)).perform(click())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package chat.rocket.android.matchers

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.NoMatchingViewException
import androidx.test.espresso.ViewAssertion
import org.hamcrest.Matcher
import org.hamcrest.MatcherAssert.assertThat


class RecyclerViewItemCountAssertion private constructor(private val matcher: Matcher<Int>) :
ViewAssertion {

override fun check(view: View, noViewFoundException: NoMatchingViewException?) {
if (noViewFoundException != null) {
throw noViewFoundException
}
val recyclerView = view as RecyclerView
val adapter = recyclerView.getAdapter()
assertThat(adapter!!.itemCount, matcher)
}

companion object {

fun withItemCount(matcher: Matcher<Int>): RecyclerViewItemCountAssertion {
return RecyclerViewItemCountAssertion(matcher)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package chat.rocket.android.members.ui

import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.NoMatchingViewException
import androidx.test.espresso.action.ViewActions.*
import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.rule.ActivityTestRule
import chat.rocket.android.R
import chat.rocket.android.analytics.event.ScreenViewEvent
import chat.rocket.android.authentication.ui.AuthenticationActivity
import chat.rocket.android.matchers.RecyclerViewItemCountAssertion.Companion.withItemCount
import chat.rocket.android.util.extensions.addFragmentBackStack
import org.hamcrest.Matchers.greaterThan
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import testConfig.Config.Companion.MEMBERS
import testConfig.Config.Companion.PASSWORD
import testConfig.Config.Companion.USERNAME
import testConfig.Config.Companion.serverUrl


class MembersFragmentTest {

@JvmField
var activityRule = ActivityTestRule(AuthenticationActivity::class.java, true, true)

@Rule
fun rule() = activityRule

@Before
fun setUp() {
try {
login_if_user_is_logged_out()
navigate_to_channel_details()
} catch (e: NoMatchingViewException) {
navigate_to_channel_details()
}
}

@Test
fun members_should_be_greater_than_zero(){
onView(withText(MEMBERS)).perform(click())
Thread.sleep(6000)
onView(withId(R.id.recycler_view)).check(withItemCount(greaterThan(0)))
}


private fun login_if_user_is_logged_out(){
rule().activity.addFragmentBackStack(ScreenViewEvent.Login.screenName, R.id.fragment_container) {
chat.rocket.android.authentication.login.ui.newInstance(serverUrl)
}
onView(withId(R.id.text_username_or_email)).perform(
typeText(USERNAME),
closeSoftKeyboard()
)
onView(withId(R.id.text_password)).perform(typeText(PASSWORD), closeSoftKeyboard())
onView(withId(R.id.button_log_in)).perform(click())
Thread.sleep(12000)
}

private fun navigate_to_channel_details() {
Thread.sleep(5000)
onView(withId(R.id.recycler_view))
.perform(
RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(
0, click()
)
)
Thread.sleep(2000)
onView(withId(R.id.text_toolbar_title)).perform(click())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fun AppCompatActivity.addFragmentBackStack(
)
.replace(layoutId, fragment, tag)
.addToBackStack(tag)
.commit()
.commitAllowingStateLoss()
}

fun AppCompatActivity.toPreviousView() {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/testConfig/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ class Config {
const val BUSY: String = "Busy"
const val AWAY: String = "Away"
const val INVISIBLE: String = "Invisible"
const val MEMBERS: String = "Members"
}
}

0 comments on commit a9a763b

Please sign in to comment.