From 8b5be7b43e17f1a3bf216816351bb79fbfa9f039 Mon Sep 17 00:00:00 2001 From: TPJ Schikhof Date: Sat, 8 Nov 2025 21:36:38 +0100 Subject: [PATCH] Add test for startMainActivity --- .../kde/kdeconnect/Plugin/PingPluginTest.kt | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/tests/org/kde/kdeconnect/Plugin/PingPluginTest.kt b/tests/org/kde/kdeconnect/Plugin/PingPluginTest.kt index ab1af9a2..6cbe4072 100644 --- a/tests/org/kde/kdeconnect/Plugin/PingPluginTest.kt +++ b/tests/org/kde/kdeconnect/Plugin/PingPluginTest.kt @@ -17,7 +17,7 @@ import org.kde.kdeconnect.Plugins.PingPlugin.PingPlugin class PingPluginTest { // 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 notificationManager = mockk { every { notify(any(), any()) } returns Unit @@ -28,6 +28,7 @@ class PingPluginTest { } val device = mockk { every { name } returns "Test Device" + every { sendPacket(any()) } returns Unit } mockkStatic(PendingIntent::class) { every { @@ -38,16 +39,25 @@ class PingPluginTest { every { anyConstructed().build() } returns mockk() 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 @Test fun wrongPacketType() { - executeWithMocks { plugin, notificationManager -> + executeWithMocks { device, plugin, notificationManager -> val np = NetworkPacket("kdeconnect.wrench") np["message"] = "Test Ping" assert(!plugin.onPacketReceived(np)) @@ -56,7 +66,7 @@ class PingPluginTest { @Test fun sendsNotificationNoMessage() { - executeWithMocks { plugin, notificationManager -> + executeWithMocks { device, plugin, notificationManager -> val np = NetworkPacket("kdeconnect.ping") // np["message"] not set here assert(plugin.onPacketReceived(np)) @@ -67,7 +77,7 @@ class PingPluginTest { @Test fun sendsNotification() { - executeWithMocks { plugin, notificationManager -> + executeWithMocks { device, plugin, notificationManager -> val np = NetworkPacket("kdeconnect.ping") np["message"] = "Test Ping" assert(plugin.onPacketReceived(np))