Add repeatable notification capture smoke tests

This commit is contained in:
ajp_anton
2026-07-23 22:04:22 +00:00
parent 50aebbd42b
commit 4d99c199dd
4 changed files with 165 additions and 16 deletions
+1 -1
View File
@@ -1 +1 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"><uses-permission android:name="android.permission.POST_NOTIFICATIONS"/><application android:theme="@style/Theme.AppCompat.DayNight"><activity android:name=".MainActivity" android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></intent-filter></activity></application></manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"><uses-permission android:name="android.permission.POST_NOTIFICATIONS"/><application android:theme="@style/Theme.AppCompat.DayNight"><activity android:name=".MainActivity" android:exported="true" android:launchMode="singleTop"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></intent-filter></activity></application></manifest>
@@ -1,23 +1,124 @@
package se.ajpanton.notificationlog.helper
import android.app.*
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Person
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.widget.*
import android.widget.Button
import android.widget.LinearLayout
import android.widget.ScrollView
import androidx.appcompat.app.AppCompatActivity
/**
* A debug companion app. Each button is also addressable with
* `adb shell am start -n .../.MainActivity --es helper_action <action>`.
*/
class MainActivity : AppCompatActivity() {
private val manager by lazy { getSystemService(NotificationManager::class.java) }
override fun onCreate(state: Bundle?) { super.onCreate(state); manager.createNotificationChannel(NotificationChannel("test", "Test", NotificationManager.IMPORTANCE_DEFAULT)); setContentView(LinearLayout(this).apply { orientation=LinearLayout.VERTICAL; listOf("Post text" to { post("Text message") }, "Edit text" to { post("Edited message") }, "Post messaging" to { messaging() }, "Post image" to { image() }, "Post dismissible" to { dismissible() }, "Cancel text" to { manager.cancel(1) }).forEach { (label, action) -> addView(Button(this@MainActivity).apply { text=label; setOnClickListener { action() } }) } }) }
private fun post(text:String)=manager.notify(1, Notification.Builder(this,"test").setSmallIcon(android.R.drawable.ic_dialog_info).setContentTitle("Helper").setContentText(text).build())
private fun messaging() {
val me = android.app.Person.Builder().setName("Me").build()
val alice = android.app.Person.Builder().setName("Alice").build()
val style = Notification.MessagingStyle(me)
.addMessage("Hello from Alice", 1, alice)
.addMessage("A reply from me", 2, null as android.app.Person?)
manager.notify(3, Notification.Builder(this, "test").setSmallIcon(android.R.drawable.ic_dialog_email).setStyle(style).build())
}
private fun image(){ val b=Bitmap.createBitmap(20,20,Bitmap.Config.ARGB_8888).apply{eraseColor(0xffff0000.toInt())}; manager.notify(2,Notification.Builder(this,"test").setSmallIcon(android.R.drawable.ic_menu_gallery).setStyle(Notification.BigPictureStyle().bigPicture(b)).build()) }
private fun dismissible()=manager.notify(4, Notification.Builder(this,"test").setSmallIcon(android.R.drawable.ic_dialog_info).setContentTitle("Dismiss me manually").setContentText("Swipe this notification away to test user dismissal.").setAutoCancel(true).build())
private val manager by lazy { getSystemService(NotificationManager::class.java) }
override fun onCreate(state: Bundle?) {
super.onCreate(state)
manager.createNotificationChannel(NotificationChannel(CHANNEL, "Test", NotificationManager.IMPORTANCE_DEFAULT))
setContentView(ScrollView(this).apply {
addView(LinearLayout(this@MainActivity).apply {
orientation = LinearLayout.VERTICAL
ACTIONS.forEach { (label, action) ->
addView(Button(this@MainActivity).apply {
text = label
setOnClickListener { perform(action) }
})
}
})
})
intent.getStringExtra(EXTRA_ACTION)?.let(::perform)
}
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent)
intent.getStringExtra(EXTRA_ACTION)?.let(::perform)
}
private fun perform(action: String) {
when (action) {
"post_text" -> postText("Text message")
"edit_text" -> postText("Edited message")
"messaging" -> postMessaging()
"image" -> postImage()
"dismissible" -> postDismissible()
"big_text" -> postBigText()
"inbox" -> postInbox()
"progress" -> postProgress(25)
"progress_update" -> postProgress(75)
"chronometer" -> postChronometer()
"timeout" -> postTimeout()
"group" -> postGroup()
"cancel_text" -> manager.cancel(TEXT_ID)
"cancel_all" -> manager.cancelAll()
}
}
private fun base() = Notification.Builder(this, CHANNEL).setSmallIcon(android.R.drawable.ic_dialog_info)
private fun postText(text: String) = manager.notify(TEXT_ID, base().setContentTitle("Helper").setContentText(text).build())
private fun postMessaging() {
val me = Person.Builder().setName("Me").build()
val alice = Person.Builder().setName("Alice").build()
val style = Notification.MessagingStyle(me)
.addMessage("Hello from Alice", 1, alice)
.addMessage("A reply from me", 2, null as Person?)
manager.notify(MESSAGING_ID, base().setSmallIcon(android.R.drawable.ic_dialog_email).setStyle(style).build())
}
private fun postImage() {
val image = Bitmap.createBitmap(20, 20, Bitmap.Config.ARGB_8888).apply { eraseColor(0xffff0000.toInt()) }
manager.notify(IMAGE_ID, base().setSmallIcon(android.R.drawable.ic_menu_gallery).setStyle(Notification.BigPictureStyle().bigPicture(image)).build())
}
private fun postDismissible() = manager.notify(DISMISSIBLE_ID, base().setContentTitle("Dismiss me manually").setContentText("Swipe this notification away to test user dismissal.").setAutoCancel(true).build())
private fun postBigText() = manager.notify(BIG_TEXT_ID, base().setStyle(Notification.BigTextStyle().bigText("A long notification body used to exercise Notification.EXTRA_BIG_TEXT.".repeat(3))).build())
private fun postInbox() = manager.notify(INBOX_ID, base().setStyle(Notification.InboxStyle().addLine("First inbox line").addLine("Second inbox line")).build())
private fun postProgress(progress: Int) = manager.notify(PROGRESS_ID, base().setContentTitle("Download").setContentText("$progress% complete").setOnlyAlertOnce(true).setProgress(100, progress, false).build())
private fun postChronometer() = manager.notify(CHRONOMETER_ID, base().setContentTitle("Timer").setWhen(System.currentTimeMillis()).setShowWhen(true).setUsesChronometer(true).build())
private fun postTimeout() = manager.notify(TIMEOUT_ID, base().setContentTitle("Timeout test").setContentText("This should expire automatically.").setTimeoutAfter(2_000).build())
private fun postGroup() {
manager.notify(GROUP_CHILD_ONE, base().setContentText("Grouped child one").setGroup(GROUP_KEY).build())
manager.notify(GROUP_CHILD_TWO, base().setContentText("Grouped child two").setGroup(GROUP_KEY).build())
manager.notify(GROUP_SUMMARY, base().setContentTitle("Grouped summary").setGroup(GROUP_KEY).setGroupSummary(true).build())
}
private companion object {
const val CHANNEL = "test"
const val EXTRA_ACTION = "helper_action"
const val TEXT_ID = 1
const val IMAGE_ID = 2
const val MESSAGING_ID = 3
const val DISMISSIBLE_ID = 4
const val BIG_TEXT_ID = 5
const val INBOX_ID = 6
const val PROGRESS_ID = 7
const val CHRONOMETER_ID = 8
const val TIMEOUT_ID = 9
const val GROUP_CHILD_ONE = 10
const val GROUP_CHILD_TWO = 11
const val GROUP_SUMMARY = 12
const val GROUP_KEY = "helper-group"
val ACTIONS = listOf(
"Post text" to "post_text", "Edit text" to "edit_text", "Post messaging" to "messaging",
"Post image" to "image", "Post dismissible" to "dismissible", "Post big text" to "big_text",
"Post inbox" to "inbox", "Post progress" to "progress", "Update progress" to "progress_update",
"Post chronometer" to "chronometer", "Post timeout" to "timeout", "Post group" to "group",
"Cancel text" to "cancel_text", "Cancel all" to "cancel_all",
)
}
}