0

I am writing an Espresso test to test that my App Links are handled properly by my app. I've setup Android Studio and created a test that passes, but the problem is that the test suite hangs. I have created a class, LinkDispatcherActivity, that is responsible for parsing the incoming links and dispatching their data to the appropriate activity. The tests are setup to use the ActivityTestRule to launch LinkDispatcherActivity and give it an intent with the URL to be tested. The tests are running and passing, so I assume I've set everything up correctly (a challenge all on its own ).

I have tried using the new ActivityScenarioRule but can't figure out how to pass an intent to it.

Here's my test class:

import android.content.Intent
import android.net.Uri
import android.widget.TextView
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.rule.ActivityTestRule
import gov.nih.nlm.wiser.R
import gov.nih.nlm.wiser.link.WiserLinkDispatcherActivity
import org.hamcrest.Matchers.allOf
import org.hamcrest.Matchers.instanceOf
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@LargeTest
class SubstanceLinkTest {
    @get:Rule
    val activityTestRule: ActivityTestRule<LinkDispatcherActivity>
            = ActivityTestRule(LinkDispatcherActivity::class.java, false, false) // initialTouchMode: false, launchActivity: false

    @Test
    fun shouldOpenLinkDispatcherActivity() {
        val i = Intent().apply {
            data = Uri.parse("https://mydomain/action?data=338")
        }

        activityTestRule.launchActivity(i)

        onView(allOf(instanceOf(TextView::class.java), withParent(withId(R.id.action_bar))))
                .check(matches(withText("My Title"))) // Passes!!
    }
}

Here's my added dependencies in by build.gradle:

dependencies {
    // Core library
    implementation 'androidx.test:core:1.2.0'

    // AndroidJUnitRunner and JUnit Rules
    implementation 'androidx.test:runner:1.2.0'
    implementation 'androidx.test:rules:1.2.0'

    // Assertions
    implementation 'androidx.test.ext:junit:1.1.1'
    implementation 'androidx.test.ext:truth:1.2.0'
    implementation 'com.google.truth:truth:0.42'

    // Espresso dependencies
    implementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'com.android.support:multidex-instrumentation:1.0.3'
}

Finally, here is what Android Studio shows in it's Run panel: Android Studio

Cheers!

Edit: Forgot to add that I'm getting this error in Logcat:

2019-06-21 12:02:24.326 6311-6035/? E/cckz: *~*~*~ Channel {0} was not shutdown properly!!! ~*~*~*
        Make sure to call shutdown()/shutdownNow() and wait until awaitTermination() returns true.
    java.lang.RuntimeException: ManagedChannel allocation site
        at cclc.<init>(:com.google.android.gms@17455040@17.4.55 (100700-248795830):1)
        at cckz.<init>(:com.google.android.gms@17455040@17.4.55 (100700-248795830):2)
        at ccdo.b(:com.google.android.gms@17455040@17.4.55 (100700-248795830):14)
        at rzc.a(:com.google.android.gms@17455040@17.4.55 (100700-248795830):43)
        at rzc.a(:com.google.android.gms@17455040@17.4.55 (100700-248795830):58)
        at atbl.a(:com.google.android.gms@17455040@17.4.55 (100700-248795830):9)
        at atbl.a(:com.google.android.gms@17455040@17.4.55 (100700-248795830):26)
        at qbi.run(:com.google.android.gms@17455040@17.4.55 (100700-248795830):1)
        at sgs.b(:com.google.android.gms@17455040@17.4.55 (100700-248795830):37)
        at sgs.run(:com.google.android.gms@17455040@17.4.55 (100700-248795830):21)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at smq.run(Unknown Source:7)
        at java.lang.Thread.run(Thread.java:764)

UPDATE: The test completes properly when run on a physical device. Still not sure what's causing the emulator to hang.

Chase Farmer
  • 113
  • 9

1 Answers1

0

I would suggest using an @After annotation with a method called something like 'tearDown'. You could do final assertions here and clean up any databases or processes that need to end and possibly call publisher.shutdown(); and publisher.awaitTermination(1, TimeUnit.MINUTES) Hope this can help somewhat.

Check this thread out too: [https://github.com/googleapis/google-cloud-java/issues/3648]

TheLab
  • 1
  • 1
  • 1