Android Kotlin测试中的Intent putExtra值
创始人
2024-08-14 14:00:13
0

在Android Kotlin测试中,可以使用以下方法为Intent的putExtra方法设置值:

  1. 使用Intent的putExtra方法设置基本数据类型的值,如字符串、整数、布尔值等。
val intent = Intent()
intent.putExtra("key", "value")
intent.putExtra("count", 10)
intent.putExtra("isSuccess", true)
  1. 使用Intent的putExtra方法设置Parcelable对象的值。首先,需要在被传递的对象类中实现Parcelable接口。
data class User(val name: String, val age: Int) : Parcelable {
    constructor(parcel: Parcel) : this(
        parcel.readString() ?: "",
        parcel.readInt()
    )
    
    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(name)
        parcel.writeInt(age)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator {
        override fun createFromParcel(parcel: Parcel): User {
            return User(parcel)
        }

        override fun newArray(size: Int): Array {
            return arrayOfNulls(size)
        }
    }
}

val user = User("John", 25)
intent.putExtra("user", user)
  1. 使用Intent的putExtra方法设置序列化对象的值。被传递的对象类需要实现Serializable接口。
data class Product(val name: String, val price: Double) : Serializable

val product = Product("Phone", 500.0)
intent.putExtra("product", product)

注意:在测试中,可以使用Intent的getStringExtra、getIntExtra、getBooleanExtra等方法获取putExtra设置的值,并进行断言或验证。

val value = intent.getStringExtra("key")
assertEquals("value", value)

val count = intent.getIntExtra("count", 0)
assertEquals(10, count)

val isSuccess = intent.getBooleanExtra("isSuccess", false)
assertTrue(isSuccess)

val user = intent.getParcelableExtra("user")
assertEquals("John", user?.name)
assertEquals(25, user?.age)

val product = intent.getSerializableExtra("product") as? Product
assertEquals("Phone", product?.name)
assertEquals(500.0, product?.price)

相关内容

热门资讯

交流学习经验!德普之星怎么设置... 交流学习经验!德普之星怎么设置埋牌,德州hhpoker脚本,确实存在有挂(有挂方针)小薇(辅助器软件...
分享个大家!哈糖大菠萝软件下载... 分享个大家!哈糖大菠萝软件下载,线上德州的辅助器是什么,好像是有挂(有挂详情)1、点击下载安装,脚本...
避坑细节!wpk辅助,soho... 避坑细节!wpk辅助,sohoo poker辅助器,总是有挂(证实有挂)该软件可以轻松地帮助玩家将外...
科技通报!德普之星透视辅助,哈... 科技通报!德普之星透视辅助,哈糖大菠萝免费辅助器,其实真的有挂(有挂透明挂)哈糖大菠萝免费辅助器透视...
实测发现!hhpoker万能辅... 实测发现!hhpoker万能辅助器,wpk显示有作弊,竟然是真的有挂(真实有挂)一、可以开透视的定义...
玩家攻略推荐!wpk模拟器,w... 玩家攻略推荐!wpk模拟器,wpk系统是否存在作弊行为,本来存在有挂(有人有挂)1、任何透视是真的假...
实操分享!竞技联盟透视插件,哈... 实操分享!竞技联盟透视插件,哈糖大菠萝可以开挂吗,原来是真的有挂(有挂方式)1、首先打开辅助器下载最...
玩家必看教程!wepoker可... 玩家必看教程!wepoker可以免费玩吗,hhpoker买挂,切实有挂(有挂实锤)破解侠是真的助透视...
一分钟了解!wepoker透视... 一分钟了解!wepoker透视有用吗,pokerrrr2辅助,好像有挂(有挂方式)1、这是跨平台的轻...
一分钟秒懂!wepoker怎么... 一分钟秒懂!wepoker怎么设置透视,竞技联盟透视插件,真是是有挂(有挂方针)破解侠是真的助透视。...