From 288970483b46e6e0c4314470742e46724a78b6c8 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Wed, 9 Feb 2022 15:43:22 +0100 Subject: [PATCH] Fix broken tests (#406) * Correct Home Activity Test to prevent crashing * Fix broken tests * Fix tests --- .../HomeActivityEspressoTest.kt | 10 ++++---- .../LoginActivityEspressoTest.kt | 17 +++++++------ .../MainActivityEspressoTest.kt | 25 ++++++++++++++----- .../apps/amine/bou/readerforselfoss/Utils.kt | 6 ++--- .../{ => utils}/DateUtilsTest.kt | 4 +-- 5 files changed, 37 insertions(+), 25 deletions(-) rename app/src/androidTest/java/apps/amine/bou/readerforselfoss/{ => utils}/DateUtilsTest.kt (80%) diff --git a/app/src/androidTest/java/apps/amine/bou/readerforselfoss/HomeActivityEspressoTest.kt b/app/src/androidTest/java/apps/amine/bou/readerforselfoss/HomeActivityEspressoTest.kt index cd521c2..793d88d 100644 --- a/app/src/androidTest/java/apps/amine/bou/readerforselfoss/HomeActivityEspressoTest.kt +++ b/app/src/androidTest/java/apps/amine/bou/readerforselfoss/HomeActivityEspressoTest.kt @@ -7,23 +7,20 @@ import androidx.test.espresso.Espresso.onView import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu import androidx.test.espresso.action.ViewActions.click import androidx.test.espresso.action.ViewActions.closeSoftKeyboard -import androidx.test.espresso.action.ViewActions.pressBack import androidx.test.espresso.action.ViewActions.pressKey import androidx.test.espresso.action.ViewActions.typeText import androidx.test.espresso.assertion.ViewAssertions.matches -import androidx.test.espresso.contrib.DrawerActions import androidx.test.espresso.intent.Intents import androidx.test.espresso.intent.Intents.intended -import androidx.test.espresso.intent.Intents.times import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent import androidx.test.espresso.matcher.ViewMatchers.isDisplayed -import androidx.test.espresso.matcher.ViewMatchers.isRoot import androidx.test.espresso.matcher.ViewMatchers.withContentDescription import androidx.test.espresso.matcher.ViewMatchers.withId import androidx.test.espresso.matcher.ViewMatchers.withText import androidx.test.rule.ActivityTestRule import androidx.test.runner.AndroidJUnit4 import android.view.KeyEvent +import androidx.test.espresso.matcher.RootMatchers.isDialog import apps.amine.bou.readerforselfoss.utils.Config import org.junit.After import org.junit.Before @@ -84,11 +81,14 @@ class HomeActivityEspressoTest { onView(withMenu(id = R.id.refresh, titleId = R.string.menu_home_refresh)) .perform(click()) + onView(withText(android.R.string.ok)) + .inRoot(isDialog()).check(matches(isDisplayed())).perform(click()) + openActionBarOverflowOrOptionsMenu(context) onView(withText(R.string.action_disconnect)).perform(click()) - intended(hasComponent(LoginActivity::class.java.name), times(1)) + intended(hasComponent(LoginActivity::class.java.name)) } // TODO: test articles opening and actions for cards and lists diff --git a/app/src/androidTest/java/apps/amine/bou/readerforselfoss/LoginActivityEspressoTest.kt b/app/src/androidTest/java/apps/amine/bou/readerforselfoss/LoginActivityEspressoTest.kt index 1bcdd4c..6a5f5b4 100644 --- a/app/src/androidTest/java/apps/amine/bou/readerforselfoss/LoginActivityEspressoTest.kt +++ b/app/src/androidTest/java/apps/amine/bou/readerforselfoss/LoginActivityEspressoTest.kt @@ -85,7 +85,7 @@ class LoginActivityEspressoTest { onView(withId(R.id.signInButton)).perform(click()) - onView(withId(R.id.urlLayout)).check(matches(isHintOrErrorEnabled())) + onView(withId(R.id.urlView)).check(matches(isHintOrErrorEnabled())) } // TODO: Add tests for multiple false urls with dialog @@ -101,19 +101,19 @@ class LoginActivityEspressoTest { onView(withId(R.id.signInButton)).perform(click()) - onView(withId(R.id.loginLayout)).check(matches(isHintOrErrorEnabled())) - onView(withId(R.id.passwordLayout)).check(matches(isHintOrErrorEnabled())) + onView(withId(R.id.loginView)).check(matches(isHintOrErrorEnabled())) + onView(withId(R.id.passwordView)).check(matches(isHintOrErrorEnabled())) onView(withId(R.id.loginView)).perform(click()).perform( typeText(username), closeSoftKeyboard() ) - onView(withId(R.id.passwordLayout)).check(matches(isHintOrErrorEnabled())) + onView(withId(R.id.passwordView)).check(matches(isHintOrErrorEnabled())) onView(withId(R.id.signInButton)).perform(click()) - onView(withId(R.id.passwordLayout)).check( + onView(withId(R.id.passwordView)).check( matches( isHintOrErrorEnabled() ) @@ -141,9 +141,9 @@ class LoginActivityEspressoTest { onView(withId(R.id.signInButton)).perform(click()) - onView(withId(R.id.urlLayout)).check(matches(isHintOrErrorEnabled())) - onView(withId(R.id.loginLayout)).check(matches(isHintOrErrorEnabled())) - onView(withId(R.id.passwordLayout)).check(matches(isHintOrErrorEnabled())) + onView(withId(R.id.urlView)).check(matches(isHintOrErrorEnabled())) + onView(withId(R.id.loginView)).check(matches(isHintOrErrorEnabled())) + onView(withId(R.id.passwordView)).check(matches(isHintOrErrorEnabled())) } @Test @@ -167,6 +167,7 @@ class LoginActivityEspressoTest { onView(withId(R.id.signInButton)).perform(click()) + Thread.sleep(2000) intended(hasComponent(HomeActivity::class.java.name)) } diff --git a/app/src/androidTest/java/apps/amine/bou/readerforselfoss/MainActivityEspressoTest.kt b/app/src/androidTest/java/apps/amine/bou/readerforselfoss/MainActivityEspressoTest.kt index a9228d2..341593a 100644 --- a/app/src/androidTest/java/apps/amine/bou/readerforselfoss/MainActivityEspressoTest.kt +++ b/app/src/androidTest/java/apps/amine/bou/readerforselfoss/MainActivityEspressoTest.kt @@ -1,5 +1,6 @@ package apps.amine.bou.readerforselfoss +import android.content.Context import android.content.Intent import android.content.SharedPreferences import android.preference.PreferenceManager @@ -10,6 +11,7 @@ import androidx.test.espresso.intent.Intents.times import androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent import androidx.test.rule.ActivityTestRule import androidx.test.runner.AndroidJUnit4 +import apps.amine.bou.readerforselfoss.utils.Config import org.junit.After import org.junit.Before @@ -22,6 +24,9 @@ class MainActivityEspressoTest { lateinit var intent: Intent lateinit var preferencesEditor: SharedPreferences.Editor + private lateinit var url: String + private lateinit var username: String + private lateinit var password: String @Rule @JvmField val rule = ActivityTestRule(MainActivity::class.java, true, false) @@ -32,31 +37,39 @@ class MainActivityEspressoTest { val context = getInstrumentation().targetContext // create a SharedPreferences editor - preferencesEditor = PreferenceManager.getDefaultSharedPreferences(context).edit() + preferencesEditor = context.getSharedPreferences(Config.settingsName, Context.MODE_PRIVATE).edit() + + url = BuildConfig.LOGIN_URL + username = BuildConfig.LOGIN_USERNAME + password = BuildConfig.LOGIN_PASSWORD Intents.init() } @Test fun checkFirstOpenLaunchesIntro() { - preferencesEditor.putBoolean("firstStart", true) + preferencesEditor.putString("url", "") + preferencesEditor.putString("password", "") + preferencesEditor.putString("login", "") preferencesEditor.commit() rule.launchActivity(intent) - intended(hasComponent(MainActivity::class.java.name)) - intended(hasComponent(LoginActivity::class.java.name), times(0)) + intended(hasComponent(LoginActivity::class.java.name)) + intended(hasComponent(HomeActivity::class.java.name), times(0)) } @Test fun checkNotFirstOpenLaunchesLogin() { - preferencesEditor.putBoolean("firstStart", false) + preferencesEditor.putString("url", url) + preferencesEditor.putString("password", password) + preferencesEditor.putString("login", username) preferencesEditor.commit() rule.launchActivity(intent) intended(hasComponent(MainActivity::class.java.name)) - intended(hasComponent(LoginActivity::class.java.name)) + intended(hasComponent(HomeActivity::class.java.name)) } @After diff --git a/app/src/androidTest/java/apps/amine/bou/readerforselfoss/Utils.kt b/app/src/androidTest/java/apps/amine/bou/readerforselfoss/Utils.kt index 0c30d74..6c748a3 100644 --- a/app/src/androidTest/java/apps/amine/bou/readerforselfoss/Utils.kt +++ b/app/src/androidTest/java/apps/amine/bou/readerforselfoss/Utils.kt @@ -1,8 +1,8 @@ package apps.amine.bou.readerforselfoss -import com.google.android.material.textfield.TextInputLayout import androidx.test.espresso.matcher.ViewMatchers import android.view.View +import android.widget.EditText import org.hamcrest.Description import org.hamcrest.Matcher import org.hamcrest.Matchers @@ -14,11 +14,11 @@ fun isHintOrErrorEnabled(): Matcher = } override fun matchesSafely(item: View?): Boolean { - if (item !is TextInputLayout) { + if (item !is EditText) { return false } - return item.isHintEnabled || item.isErrorEnabled + return item.error.isNotEmpty() } } diff --git a/app/src/androidTest/java/apps/amine/bou/readerforselfoss/DateUtilsTest.kt b/app/src/androidTest/java/apps/amine/bou/readerforselfoss/utils/DateUtilsTest.kt similarity index 80% rename from app/src/androidTest/java/apps/amine/bou/readerforselfoss/DateUtilsTest.kt rename to app/src/androidTest/java/apps/amine/bou/readerforselfoss/utils/DateUtilsTest.kt index e3cc12f..7b59599 100644 --- a/app/src/androidTest/java/apps/amine/bou/readerforselfoss/DateUtilsTest.kt +++ b/app/src/androidTest/java/apps/amine/bou/readerforselfoss/utils/DateUtilsTest.kt @@ -1,7 +1,5 @@ -package apps.amine.bou.readerforselfoss +package apps.amine.bou.readerforselfoss.utils -import apps.amine.bou.readerforselfoss.utils.Config -import apps.amine.bou.readerforselfoss.utils.parseDate import org.junit.Test class DateUtilsTest {