Oana Horvath
Oana Horvath

Reputation: 41

How to set my app as default in espresso tests?

I want to write some UI tests where my app needs to be the default app in Android Default apps settings. There is a preference in the app that will launch the Default apps screen to let me pick the default and then return and I will have the preference set. Another way would be to reset all app defaults, launch an intent with a link, and then select my app from the app picker, as default.

Is there any way to do this programmatically using kotlin and espresso? I've read and tried espresso-intents but that will only let me launch the Default apps screen, not to select what I need in it. Espresso can't interact with external activities.

I don't have any code sample to share, nothing is even close to be working.

Upvotes: 2

Views: 408

Answers (1)

Oana Horvath
Oana Horvath

Reputation: 41

The app has a switch to set it as default, which triggers the Default apps screen (on stock Android OS, not custom ones), so the method would look like this: '''

private fun setDefaultBrowserToggle() = onView(withId(R.id.switch_widget))
private fun defaultBrowserAppList() = mDevice.findObject((UiSelector().text("Browser app")))
private fun myAppOption() = mDevice.findObject(UiSelector().text("<App Name>"))

   fun selectDefaultBrowser(){
        setDefaultBrowserToggle().click()
        mDevice.waitNotNull(
            Until.findObject(By.text("Browser app")),
            TestAssetHelper.waitingTime
        )
        defaultBrowserAppList().click()
        myAppOption().click()
        mDevice.pressBack()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mDevice.pressBack() //needs an extra back press on Android O or higher, to return to the app
        }
    }

'''

Upvotes: 1

Related Questions