Add test for startMainActivity

This commit is contained in:
TPJ Schikhof
2025-11-08 21:36:38 +01:00
parent 74e23ce924
commit 8b5be7b43e

View File

@@ -17,7 +17,7 @@ import org.kde.kdeconnect.Plugins.PingPlugin.PingPlugin
class PingPluginTest { class PingPluginTest {
// Mocks the necessary components to test the PingPlugin // Mocks the necessary components to test the PingPlugin
private fun executeWithMocks(test: (plugin: PingPlugin, notificationManager: NotificationManager) -> Unit) { private fun executeWithMocks(test: (device: Device, plugin: PingPlugin, notificationManager: NotificationManager) -> Unit) {
val plugin = PingPlugin() val plugin = PingPlugin()
val notificationManager = mockk<NotificationManager> { val notificationManager = mockk<NotificationManager> {
every { notify(any(), any()) } returns Unit every { notify(any(), any()) } returns Unit
@@ -28,6 +28,7 @@ class PingPluginTest {
} }
val device = mockk<Device> { val device = mockk<Device> {
every { name } returns "Test Device" every { name } returns "Test Device"
every { sendPacket(any()) } returns Unit
} }
mockkStatic(PendingIntent::class) { mockkStatic(PendingIntent::class) {
every<PendingIntent> { every<PendingIntent> {
@@ -38,16 +39,25 @@ class PingPluginTest {
every { anyConstructed<NotificationCompat.Builder>().build() } returns mockk() every { anyConstructed<NotificationCompat.Builder>().build() } returns mockk()
plugin.setContext(context, device) plugin.setContext(context, device)
test(plugin, notificationManager) test(device, plugin, notificationManager)
} }
} }
} }
@Test
fun startPlugin() {
executeWithMocks { device, plugin, notificationManager ->
plugin.startMainActivity(mockk())
verify(exactly = 1) { device.sendPacket(match { np -> np.type == "kdeconnect.ping" && np.payload == null }) }
}
}
// Tests all 3 return paths of PingPlugin.onPacketReceived // Tests all 3 return paths of PingPlugin.onPacketReceived
@Test @Test
fun wrongPacketType() { fun wrongPacketType() {
executeWithMocks { plugin, notificationManager -> executeWithMocks { device, plugin, notificationManager ->
val np = NetworkPacket("kdeconnect.wrench") val np = NetworkPacket("kdeconnect.wrench")
np["message"] = "Test Ping" np["message"] = "Test Ping"
assert(!plugin.onPacketReceived(np)) assert(!plugin.onPacketReceived(np))
@@ -56,7 +66,7 @@ class PingPluginTest {
@Test @Test
fun sendsNotificationNoMessage() { fun sendsNotificationNoMessage() {
executeWithMocks { plugin, notificationManager -> executeWithMocks { device, plugin, notificationManager ->
val np = NetworkPacket("kdeconnect.ping") val np = NetworkPacket("kdeconnect.ping")
// np["message"] not set here // np["message"] not set here
assert(plugin.onPacketReceived(np)) assert(plugin.onPacketReceived(np))
@@ -67,7 +77,7 @@ class PingPluginTest {
@Test @Test
fun sendsNotification() { fun sendsNotification() {
executeWithMocks { plugin, notificationManager -> executeWithMocks { device, plugin, notificationManager ->
val np = NetworkPacket("kdeconnect.ping") val np = NetworkPacket("kdeconnect.ping")
np["message"] = "Test Ping" np["message"] = "Test Ping"
assert(plugin.onPacketReceived(np)) assert(plugin.onPacketReceived(np))