Skip to content

Commit

Permalink
Attempt to fix UI tests on CI
Browse files Browse the repository at this point in the history
Add delays and clean the state independently of the orchestrator
  • Loading branch information
fibelatti committed Aug 6, 2023
1 parent 51c5497 commit 421dc7a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ dependencies {
androidTestImplementation(libs.runner)
androidTestUtil(libs.orchestrator)

androidTestImplementation(libs.espresso.core)

androidTestImplementation(libs.truth)
androidTestImplementation(libs.coroutines.test)
androidTestImplementation(libs.room.testing)
Expand Down
18 changes: 18 additions & 0 deletions app/src/androidTest/kotlin/com/fibelatti/pinboard/EndToEndTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import androidx.test.espresso.IdlingPolicies
import com.fibelatti.core.extension.clear
import com.fibelatti.pinboard.core.persistence.getUserPreferences
import com.fibelatti.pinboard.core.util.DateFormatter
import com.fibelatti.pinboard.features.MainActivity
import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.util.concurrent.TimeUnit
import javax.inject.Inject

@HiltAndroidTest
Expand All @@ -31,10 +36,19 @@ class EndToEndTests {
@Before
fun setup() {
hiltRule.inject()
IdlingPolicies.setMasterPolicyTimeout(10, TimeUnit.SECONDS)
}

@After
fun tearDown() {
MockServer.instance.shutdown()
context.getUserPreferences().clear()
}

@Test
fun loginScreenIsVisibleWhenFirstLaunchingTheApp() {
TestUtil.delay(seconds = 1)

with(composeRule) {
// Assert
onNodeWithText(context.getString(R.string.auth_title)).assertIsDisplayed()
Expand All @@ -43,6 +57,8 @@ class EndToEndTests {

@Test
fun userCanLaunchAppReviewMode() {
TestUtil.delay(seconds = 1)

with(composeRule) {
// Act
onNodeWithText(context.getString(R.string.auth_token_hint)).performTextInput("app_review_mode")
Expand All @@ -56,6 +72,8 @@ class EndToEndTests {

@Test
fun userCanLoginAndFetchBookmarks() {
TestUtil.delay(seconds = 1)

// Arrange
MockServer.loginResponses(updateTimestamp = dateFormatter.nowAsTzFormat())

Expand Down
27 changes: 27 additions & 0 deletions app/src/androidTest/kotlin/com/fibelatti/pinboard/TestUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.fibelatti.pinboard

import android.view.View
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.matcher.ViewMatchers.isRoot
import org.hamcrest.Matcher
import java.util.concurrent.TimeUnit

object TestUtil {

fun delay(seconds: Long) {
onView(isRoot()).perform(waitOnId(TimeUnit.SECONDS.toMillis(seconds)))
}

fun waitOnId(millis: Long): ViewAction = object : ViewAction {

override fun getConstraints(): Matcher<View> = isRoot()

override fun getDescription(): String = "Wait a specified amount of time."

override fun perform(uiController: UiController?, view: View?) {
uiController?.loopMainThreadForAtLeast(millis)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.fibelatti.pinboard.core.di.modules

import android.app.Application
import androidx.room.Room
import com.fibelatti.pinboard.core.persistence.database.AppDatabase
import dagger.Module
import dagger.Provides
import dagger.hilt.components.SingletonComponent
import dagger.hilt.testing.TestInstallIn
import javax.inject.Singleton

@Module
@TestInstallIn(
components = [SingletonComponent::class],
replaces = [DatabaseModule::class],
)
object TestDatabaseModule {

@Provides
@Singleton
fun appDatabase(
application: Application,
): AppDatabase = Room.inMemoryDatabaseBuilder(application, AppDatabase::class.java).build()
}
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ compose-ui-test-manifest = { module = "androidx.compose.ui:ui-test-manifest", ve
hilt-android-testing = { module = "com.google.dagger:hilt-android-testing", version.ref = "hilt" }
hilt-android-compiler = { module = "com.google.dagger:hilt-android-compiler", version.ref = "hilt" }
mockwebserver = { module = "com.squareup.okhttp3:mockwebserver", version = "4.11.0" }
espresso-core = { module = "androidx.test.espresso:espresso-core", version = "3.5.1" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand Down

0 comments on commit 421dc7a

Please sign in to comment.