move tests to MockedServer to not have infinite waits by Espresso

This commit is contained in:
Matthieu 2020-03-31 14:43:16 +02:00
parent ece6716e2d
commit fcec1029fc
3 changed files with 42 additions and 118 deletions

View File

@ -1,10 +1,15 @@
package com.h.pixeldroid
import android.content.Context
import android.view.Gravity
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.DrawerActions
import androidx.test.espresso.contrib.DrawerMatchers
import androidx.test.espresso.contrib.NavigationViewActions
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.ActivityScenarioRule
@ -121,5 +126,42 @@ class MockedServerTest {
Thread.sleep(1000)
onView(withText("Geonosys")).check(matches(withId(R.id.username)))
}
@Test
fun testDrawerSettingsButton() {
// Open Drawer to click on navigation.
onView(withId(R.id.drawer_layout))
.check(matches(DrawerMatchers.isClosed(Gravity.LEFT))) // Left Drawer should be closed.
.perform(DrawerActions.open()); // Open Drawer
// Start the screen of your activity.
onView(withId(R.id.nav_view)).perform(NavigationViewActions.navigateTo(R.id.nav_settings))
// Check that settings activity was opened.
onView(withText(R.string.signature_title)).check(matches(ViewMatchers.isDisplayed()))
}
@Test
fun swipingLeftStopsAtProfile() {
onView(withId(R.id.main_activity_main_linear_layout))
.perform(ViewActions.swipeLeft()) // search
.perform(ViewActions.swipeLeft()) // camera
.perform(ViewActions.swipeLeft()) // notifications
.perform(ViewActions.swipeLeft()) // profile
.perform(ViewActions.swipeLeft()) // should stop at profile
onView(withId(R.id.nbFollowersTextView)).check(matches(ViewMatchers.isDisplayed()))
}
@Test
fun swipingRightStopsAtHomepage() {
ActivityScenario.launch(MainActivity::class.java).onActivity {
a -> a.findViewById<TabLayout>(R.id.tabs).getTabAt(4)?.select()
} // go to the last tab
onView(withId(R.id.main_activity_main_linear_layout))
.perform(ViewActions.swipeRight()) // notifications
.perform(ViewActions.swipeRight()) // camera
.perform(ViewActions.swipeRight()) // search
.perform(ViewActions.swipeRight()) // homepage
.perform(ViewActions.swipeRight()) // should stop at homepage
onView(withId(R.id.list)).check(matches(ViewMatchers.isDisplayed()))
}
}

View File

@ -1,52 +0,0 @@
package com.h.pixeldroid
import android.content.Context
import android.view.Gravity
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.onView
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.NavigationViewActions
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.Timeout
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class SettingsTest {
@get:Rule
var activityRule: ActivityScenarioRule<MainActivity>
= ActivityScenarioRule(MainActivity::class.java)
@get:Rule
var globalTimeout: Timeout = Timeout.seconds(100)
@Before
fun before(){
val preferences = InstrumentationRegistry.getInstrumentation()
.targetContext.getSharedPreferences("com.h.pixeldroid.pref", Context.MODE_PRIVATE)
preferences.edit().putString("accessToken", "azerty").apply()
preferences.edit().putString("domain", "http://localhost").apply()
}
@Test
fun testDrawerSettingsButton() {
// Open Drawer to click on navigation.
onView(withId(R.id.drawer_layout))
.check(matches(isClosed(Gravity.LEFT))) // Left Drawer should be closed.
.perform(DrawerActions.open()); // Open Drawer
// Start the screen of your activity.
onView(withId(R.id.nav_view)).perform(NavigationViewActions.navigateTo(R.id.nav_settings))
// Check that settings activity was opened.
onView(withText(R.string.signature_title)).check(matches(isDisplayed()))
}
}

View File

@ -1,66 +0,0 @@
package com.h.pixeldroid
import android.content.Context
import android.content.Intent
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.action.ViewActions.*
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.matcher.IntentMatchers
import androidx.test.espresso.intent.rule.IntentsTestRule
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import com.google.android.material.tabs.TabLayout
import org.hamcrest.CoreMatchers
import org.hamcrest.Matcher
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.Timeout
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class SwipeTest {
@get:Rule
var activityRule: ActivityScenarioRule<MainActivity>
= ActivityScenarioRule(MainActivity::class.java)
@get:Rule
var globalTimeout: Timeout = Timeout.seconds(100)
@Before
fun before(){
val preferences = getInstrumentation()
.targetContext.getSharedPreferences("com.h.pixeldroid.pref", Context.MODE_PRIVATE)
preferences.edit().putString("accessToken", "azerty").apply()
preferences.edit().putString("domain", "http://localhost").apply()
}
@Test
fun swipingLeftStopsAtProfile() {
onView(withId(R.id.main_activity_main_linear_layout))
.perform(swipeLeft()) // search
.perform(swipeLeft()) // camera
.perform(swipeLeft()) // notifications
.perform(swipeLeft()) // profile
.perform(swipeLeft()) // should stop at profile
onView(withId(R.id.nbFollowersTextView)).check(matches(isDisplayed()))
}
@Test
fun swipingRightStopsAtHomepage() {
ActivityScenario.launch(MainActivity::class.java).onActivity {
a -> a.findViewById<TabLayout>(R.id.tabs).getTabAt(4)?.select()
} // go to the last tab
onView(withId(R.id.main_activity_main_linear_layout))
.perform(swipeRight()) // notifications
.perform(swipeRight()) // camera
.perform(swipeRight()) // search
.perform(swipeRight()) // homepage
.perform(swipeRight()) // should stop at homepage
onView(withId(R.id.feedList)).check(matches(isDisplayed()))
}
}