Skip to content

Instantly share code, notes, and snippets.

@Cool-fire
Created June 4, 2019 13:24
Show Gist options
  • Save Cool-fire/f2b313709574cc9e5da40f334f4ee59c to your computer and use it in GitHub Desktop.
Save Cool-fire/f2b313709574cc9e5da40f334f4ee59c to your computer and use it in GitHub Desktop.
package chat.rocket.android.authentication.ui
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.Espresso.onData
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.*
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.matcher.BoundedMatcher
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.filters.LargeTest
import androidx.test.rule.ActivityTestRule
import androidx.test.runner.AndroidJUnit4
import chat.rocket.android.R
import chat.rocket.android.authentication.ui.CustomMatchers.Companion.withItemName
import chat.rocket.android.authentication.ui.CustomMatchers.Companion.withWebView
import chat.rocket.android.chatroom.adapter.AttachmentViewHolder
import chat.rocket.android.chatrooms.adapter.RoomViewHolder
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.Matchers.*
import org.hamcrest.TypeSafeMatcher
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@LargeTest
@RunWith(AndroidJUnit4::class)
class login_test {
@Rule
@JvmField
var mActivityTestRule = ActivityTestRule(AuthenticationActivity::class.java)
@Test
fun login_test() {
val relativeLayout = onView(
allOf(withId(R.id.connect_with_a_server_container),
childAtPosition(
childAtPosition(
withClassName(`is`("android.widget.ScrollView")),
0),
3)))
relativeLayout.perform(scrollTo(), click())
val appCompatSpinner = onView(
allOf(withId(R.id.spinner_server_protocol),
childAtPosition(
allOf(withId(R.id.server_url_container),
childAtPosition(
withClassName(`is`("androidx.constraintlayout.widget.ConstraintLayout")),
2)),
0)))
appCompatSpinner.perform(scrollTo(), click())
val appCompatTextView = onData(anything())
.atPosition(1)
appCompatTextView.perform(click())
val appCompatEditText = onView(
allOf(withId(R.id.text_server_url),
childAtPosition(
allOf(withId(R.id.server_url_container),
childAtPosition(
withClassName(`is`("androidx.constraintlayout.widget.ConstraintLayout")),
2)),
1)))
appCompatEditText.perform(scrollTo(), replaceText("35.200.186.0"), closeSoftKeyboard())
val button = onView(
withId(R.id.button_connect))
button.perform(click())
Thread.sleep(6000)
val button2 = onView(withId(R.id.button_login_with_email))
button2.perform(click())
val appCompatEditText2 = onView(
allOf(withId(R.id.text_username_or_email),
childAtPosition(
allOf(withId(R.id.constraint_layout),
childAtPosition(
withId(R.id.fragment_container),
0)),
1),
isDisplayed()))
appCompatEditText2.perform(replaceText("sainathreddy.k16@iiits.in"), closeSoftKeyboard())
val appCompatEditText3 = onView(
allOf(withId(R.id.text_password),
childAtPosition(
allOf(withId(R.id.constraint_layout),
childAtPosition(
withId(R.id.fragment_container),
0)),
2),
isDisplayed()))
appCompatEditText3.perform(replaceText("upendra2"), closeSoftKeyboard())
val button3 = onView(
allOf(withId(R.id.button_log_in), withText("Login"),
childAtPosition(
allOf(withId(R.id.constraint_layout),
childAtPosition(
withId(R.id.fragment_container),
0)),
3),
isDisplayed()))
button3.perform(click())
Thread.sleep(10000)
onView(withId(R.id.recycler_view)).check(matches(withItemName("general")))
onView(withId(R.id.recycler_view))
.perform(RecyclerViewActions.actionOnItem<RoomViewHolder>(hasDescendant(withText("general")), click()))
onView(withId(R.id.text_message)).perform(replaceText("@jsbot button with full webview"), closeSoftKeyboard())
onView(withId(R.id.button_send)).perform(click())
Thread.sleep(15000)
onView(withId(R.id.recycler_view)).check(matches(withWebView("Full webview")))
}
private fun childAtPosition(
parentMatcher: Matcher<View>, position: Int): Matcher<View> {
return object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description) {
description.appendText("Child at position $position in parent ")
parentMatcher.describeTo(description)
}
public override fun matchesSafely(view: View): Boolean {
val parent = view.parent
return parent is ViewGroup && parentMatcher.matches(parent)
&& view == parent.getChildAt(position)
}
}
}
}
class CustomMatchers {
companion object {
fun withItemName(Roomname: String): Matcher<View> {
return object : BoundedMatcher<View, RecyclerView>(RecyclerView::class.java) {
override fun describeTo(description: Description?) {
description?.appendText("RecyclerView with Room name: $Roomname")
}
override fun matchesSafely(item: RecyclerView?): Boolean {
val adapter = item?.adapter
if (adapter != null) {
for(position in 0 until adapter.itemCount){
val itemType = adapter.getItemViewType(position)
val viewHolder = adapter.createViewHolder(item, itemType)
adapter.bindViewHolder(viewHolder, position)
val view = viewHolder.itemView
val name = view.findViewById<TextView>(R.id.text_chat_name)
if(name.text == Roomname){
return true
}
}
}
return false
}
}
}
fun withWebView(webview: String): Matcher<View> {
return object : BoundedMatcher<View, RecyclerView>(RecyclerView::class.java) {
override fun describeTo(description: Description?) {
description?.appendText("RecyclerView with webview: $webview")
}
override fun matchesSafely(item: RecyclerView?): Boolean {
val adapter = item?.adapter
if (adapter != null) {
val itemType = adapter.getItemViewType(adapter.itemCount - 1)
val viewHolder = adapter.createViewHolder(item, itemType)
adapter.bindViewHolder(viewHolder, adapter.itemCount -1)
}
return false
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment