测试Android深层链接导航到活动的最佳实践 [英] Best practise for testing Android Deep Links navigation into activities

查看:111
本文介绍了测试Android深层链接导航到活动的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动来保存片段.我创建此文件是为了能够运行指向配置文件的深度链接.另外,我将PROFILE_ID传递为查询参数.因此,整个深层链接看起来是这样的:" tigranes://home/profile?profileId = 3545664 ".

I have an activity for holding a fragment. I created this for being able to run Deep Link to the profile. Also I pass PROFILE_ID as a query parameter. So the whole deep link looks this: "tigranes://home/profile?profileId=3545664".

class ProfileActivity : BaseActivity() {

    companion object {
        @JvmStatic
        fun newInstance(context: Context, profileId: String): Intent {
            val intent = Intent(context, ProfileActivity::class.java)
            intent.putExtra(ProfileFragment.PROFILE_ID, profileId)
            return intent
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)

        val profileId: String = intent.getStringExtra(ProfileFragment.PROFILE_ID)
        val transaction = supportFragmentManager.beginTransaction()
        val fragment = ProfileFragment.newInstance(profileId)
        transaction.add(R.id.fragment_container, fragment)
        transaction.commit()
    }
}

所以我的问题是,如果此深层链接正在打开ProfileActivity,那么编写测试检查的最佳策略是什么?我尝试使用ActivityTestRule,但无法找到将参数传递给它的方法.

So my question is what will be the best strategy for writing test checking if this deep link is opening ProfileActivity. I tried to use ActivityTestRule but I wasn't able to find a way for passing a parameters to it.

推荐答案

方法newInstance()似乎完全是毫无意义的,因为Intent被传递给了Activity.您应该重新考虑ProfileActivity的构造方式,因为这不是它的工作方式. getIntent()是获取Intent所需要的(正如方法名称所建议的那样).另外,还应考虑@EpicPandaForce的建议,以免造成混乱.但是,这不是真正的问题(只是说出来,因为您可能会声称它不起作用").

Method newInstance() seems to be utter non-sense, because the Intent is being passed to the Activity; you should reconsider how that ProfileActivity is being constructed, because this is not how it works. getIntent() is all you need to get the Intent (as the method's name might suggest). Also @EpicPandaForce's suggestion should be taken into consideration, in order to avoid a mess. However, this wasn't the actual question (just telling, because you might claim "it doesn't work").

使用深度链接Intent测试Activity的工作原理如下:

Testing an Activity with a deep-link Intent works alike this:

import android.content.Intent
import android.net.Uri
import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Rule
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class ProfileActivityTest {

    @get:Rule
    var testRule = activityScenarioRule<ProfileActivity>(
        Intent(Intent.ACTION_VIEW, Uri.parse(DEEP_LINK))
    )

    @Test
    ...

    companion object {
        val DEEP_LINK = "tigranes://home/profile?profileId=3545664"
    }
}

activityScenarioRule取决于:

androidTestImplementation "androidx.test.ext:junit-ktx:1.1.1"

请告诉我这是否可行(首先需要修复ProfileActivity).

Please let me know if this works (this would require fixing the ProfileActivity in the first place).

还要确保正确设置了AndroidManifest.xml中的intent-filter.

Also make sure, that the intent-filter in the AndroidManifest.xml is setup properly.

这篇关于测试Android深层链接导航到活动的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆