0

I want to simpley stub an intent that asks the system to open the web brower at a specified url after the user clicks a button

Intents.init()
val expectedIntent = allOf(hasAction(Intent.ACTION_VIEW), hasData(url))
intending(expectedIntent).respondWith(ActivityResult(0, null))

// here text view with link is clicked which will 
// launch browser to show web site
onView(withId(id)).perform(click())

intended(expectedIntent)
Intents.release()

When test is run I get

junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 0 intents.

IntentMatcher: (has action: is "android.intent.action.VIEW" and has data: is <false>)

Matched intents:[]

Recorded intents:
-Intent { act=android.intent.action.VIEW dat=http://www.mysite.or/... } handling packages:[[com.android.chrome]])
at junit.framework.Assert.fail(Assert.java:50)
at androidx.test.espresso.intent.VerificationModes$Times.verify(VerificationModes.java:80)
at androidx.test.espresso.intent.Intents.internalIntended(Intents.java:346)
at androidx.test.espresso.intent.Intents$3.run(Intents.java:204)
at androidx.test.espresso.intent.Intents$PropogatingRunnable.run(Intents.java:224)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:2163)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

so clearly the intent is launched but not stubbed, but why?

Maybe I need to use test rule? However, I am launching fragment in isolation

@Before
fun init() {
    scenario = launchFragmentInContainer(null, R.style.Theme_AppCompat) {}
}

edit

Is the test rule required?

edit

Maybe I need a fragment test rule?

the_prole
  • 8,275
  • 16
  • 78
  • 163

2 Answers2

1

Intent validation for intent type Intent.ACTION_VIEW is working, it's the hasData method which is causing fault.

I do not need any test rules apparently.

the_prole
  • 8,275
  • 16
  • 78
  • 163
0

Since you didn't specify what url is, I can only guess that this parameter was not matching the expected value.

If you launched the browser by something like:

val url = "https://stackoverflow.com"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)

Then this works perfectly fine in the test:

val url = "https://stackoverflow.com"
val expectedIntent = allOf(hasAction(Intent.ACTION_VIEW), hasData(url))
intending(expectedIntent).respondWith(Instrumentation.ActivityResult(0, null))

// perform click...

intended(expectedIntent)
gosr
  • 4,593
  • 9
  • 46
  • 82