Skip to content

Commit

Permalink
test: resolve FIXME in sleep create test
Browse files Browse the repository at this point in the history
Try the uiautomator approach, espresso doesn't seem to be able to cope
with the coroutines/room/observer-based UI updates.
  • Loading branch information
vmiklos committed Oct 25, 2024
1 parent 578b4c7 commit 5cf023b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.6.1'
androidTestImplementation "androidx.test.uiautomator:uiautomator:2.3.0"
def room_version = "2.6.1"
implementation "androidx.room:room-runtime:$room_version"
ksp "androidx.room:room-compiler:$room_version"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@ import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
import androidx.test.espresso.action.ViewActions.click
// import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.Intents.intending
import androidx.test.espresso.intent.matcher.IntentMatchers.hasAction
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import java.io.File
import java.time.Duration
Expand All @@ -33,14 +30,12 @@ import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith

/**
* Instrumented tests for MainActivity.
*/
@RunWith(AndroidJUnit4::class)
// @RunWith(AndroidJUnit4::class)
class MainActivityInstrumentedTest {

@JvmField
Expand Down Expand Up @@ -69,21 +64,6 @@ class MainActivityInstrumentedTest {
Intents.release()
}

@Test
fun testCountStat(): Unit = runBlocking {
val startStop = onView(withId(R.id.start_stop_layout))
// Start.
startStop.perform(click())
// Stop.
startStop.perform(click())

// Read number of created sleeps.
assertEquals(1, database.sleepDao().getAll().size)
// FIXME UI is not yet updated, how to wait for this?
// val sleepsCount = onView(withId(R.id.fragment_stats_sleeps))
// sleepsCount.check(matches(withText("1")))
}

// FIXME started to fail with: java.lang.AssertionError: expected:<1> but was:<0>
// @Test
fun testImportExport() = runBlocking {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2024 Miklos Vajna
*
* SPDX-License-Identifier: MIT
*/

package hu.vmiklos.plees_tracker

import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.By
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.Until
import org.junit.Assert.assertNotNull
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
* UI tests for MainActivity.
*/
@RunWith(AndroidJUnit4::class)
class MainActivityUITest {
@JvmField
@Rule
var activityScenarioRule = ActivityScenarioRule(MainActivity::class.java)

@Test
fun testCreate() {
val instrumentation = InstrumentationRegistry.getInstrumentation()
val device = UiDevice.getInstance(instrumentation)
val pkg = instrumentation.processName
val timeout: Long = 5000

val startStop = device.wait(Until.findObject(By.res(pkg, "start_stop")), timeout)
startStop.click()
startStop.click()

val sleeps = device.wait(
Until.findObject(By.res(pkg, "fragment_stats_sleeps").text("1")),
timeout
)
assertNotNull(sleeps)
}
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

0 comments on commit 5cf023b

Please sign in to comment.