微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何为具有上下文、标题和消息的函数编写本地单元测试用例?

如何解决如何为具有上下文、标题和消息的函数编写本地单元测试用例?

如何在 Kotlin 中为以下函数编写测试用例。

代码--->

fun showNotification(
    context: Context,title: String?,message: String?
) {
    val ACTION: String = "actionstring"
    val CUSTOM: String = "custom://"
    val intent: Intent = Intent(context,MainActivity::class.java)
    intent.data = Uri.parse( CUSTOM + System.currentTimeMillis())
    intent.action = ACTION + System.currentTimeMillis()
    //Toast.makeText(applicationContext,ACTION,Toast.LENGTH_SHORT).show()
    intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
    val pendingintent =
        PendingIntent.getActivity(context,intent,PendingIntent.FLAG_UPDATE_CURRENT)

    val notification: Notification
    // Since android Oreo notification channel is needed and check id SDK > 26
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        notification = NotificationCompat.Builder(context,notificationContants.NOTIFICATION_CHANNEL_ID)
            .setongoing(true)
            .setSmallIcon(getNotificationIcon())
            .setContentText(message)
             // automatically removes the notification when the user taps it.
            .setAutoCancel(true)
            .setContentIntent(pendingintent)
            // priority determines how intrusive the notification should be
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setCategory(Notification.CATEGORY_SERVICE)
            .setWhen(System.currentTimeMillis())
            .setSound(ringtoneManager.getDefaultUri(ringtoneManager.TYPE_NOTIFICATION))
            .setContentTitle(title).build()

        val notificationmanager = context.getSystemService(
            Context.NOTIFICATION_SERVICE
        ) as notificationmanager

        // Register the channel with the system
        val notificationChannel = NotificationChannel(
            notificationContants.NOTIFICATION_CHANNEL_ID,title,notificationmanager.IMPORTANCE_DEFAULT
        )

        notificationmanager.createNotificationChannel(notificationChannel)
        notificationmanager.notify(notificationContants.NOTIFICATION_ID,notification)
    }
    else {
        notification = NotificationCompat.Builder(context)
            .setSmallIcon(getNotificationIcon())
            .setAutoCancel(true)
            .setContentText(message)
            .setContentIntent(pendingintent)
            .setSound(ringtoneManager.getDefaultUri(ringtoneManager.TYPE_NOTIFICATION))
            .setContentTitle(title).build()

        val notificationmanager = context.getSystemService(
            Context.NOTIFICATION_SERVICE
        ) as notificationmanager
        notificationmanager.notify(notificationContants.NOTIFICATION_ID,notification)
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。