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)

相关内容

热门资讯

透视有挂!wepoker私人局... 透视有挂!wepoker私人局辅助器怎么用(透视)hhpkoer辅助器视频,教程指南书(有挂秘笈)-...
透视推荐!aapoker透视怎... 透视推荐!aapoker透视怎么用(透视)智星德州插件2024最新版,教程法子(真的有挂)-哔哩哔哩...
透视普及!hhpoker透视码... 透视普及!hhpoker透视码(透视)aapoker公共底牌,教程模板(有挂规律)-哔哩哔哩1、上手...
透视揭露!hhpoker有没有... 透视揭露!hhpoker有没有开挂(透视)pokemmo辅助器手机版下载,教程步骤(有挂秘诀)-哔哩...
透视了解!hhpkoer辅助挂... 透视了解!hhpkoer辅助挂是真的吗(透视)哈糖大菠萝攻略,教程学习(证实有挂)-哔哩哔哩1、金币...
透视解迷!约局吧怎么看有没有挂... 透视解迷!约局吧怎么看有没有挂(透视)wepoker有没有机器人,教程窍门(确实有挂)-哔哩哔哩亲,...
透视推荐!wepokerplu... 透视推荐!wepokerplus透视脚本免费(透视)菠萝德州透视脚本,教程教材(存在有挂)-哔哩哔哩...
透视辅助!wepoker辅助器... 透视辅助!wepoker辅助器安装包(透视)wepoker插件程序激活码,教程阶段(有挂分析)-哔哩...
透视总结!wepoker安装教... 透视总结!wepoker安装教程(透视)约局吧开挂神器是真的吗,教程方式(有挂猫腻)-哔哩哔哩所有人...
透视详细!wepoker辅助器... 透视详细!wepoker辅助器安装包(透视)wepoker辅助器安装包,教程手筋(竟然有挂)-哔哩哔...