android-fragments - 出于测试目的注入和模拟第 3 方 ActivityResultContract
问题描述
我有一个Fragment
。在这里面我开了Fragment
一个 3rd 派对Activity
。registerForActivityResult(CropImageContract())
现在我想测试Fragment
. 特别是我想验证是否Activity
正确处理了第 3 方的结果。
由于在仪器测试期间我无法访问第 3 方Activity
,我想注入和模拟CropImageContract()
但我不知道如何。有什么想法或替代解决方案吗?
这是Fragment
我要测试的:
class ProductDetailsFragment : Fragment() {
private var mBinding: FragmentProductDetailsBinding? = null
private val cropImageLauncher = registerForActivityResult(FakeCropImageContract()) { result ->
if (result.isSuccessful) {
// use the returned uri
val uriContent = result.uriContent
} else {
// an error occurred
val exception = result.error
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
mBinding = FragmentProductDetailsBinding.inflate(
inflater,
container,
false
)
return mBinding!!.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
mBinding!!.contentProduct.productProductImageView.setOnClickListener { takePicture() }
}
private fun takePicture() {
cropImageLauncher.launch()
}
}
解决方案
推荐阅读
- python - 使用 win32api.ShellExecute 更改默认打印机参数
- java - 如何使用 2 个虚拟机在 java 中测试我的套接字程序?
- java - HTTP 状态 400 - 错误请求 - 休眠
- r - 如何有条件地选择时间序列
- android-studio - Android Studio 中的多行编辑
- mongodb - 有没有办法将 MongoEngine 中的 MongoDB _id 字段名称从 $oid 更改为 $id?
- python-3.x - 跨数字的随机几何分布
- c - 从 C 中的文件读取和写入
- php - Apple Pay 条纹集成
- javascript - 未捕获的 SyntaxError:严格模式代码可能不包含 with 语句