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

问题描述

如何在 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 (将#修改为@)