|
1 | 1 | package at.bitfire.cert4android |
2 | 2 |
|
| 3 | +import androidx.core.app.NotificationManagerCompat |
3 | 4 | import androidx.test.platform.app.InstrumentationRegistry |
4 | 5 | import io.mockk.every |
| 6 | +import io.mockk.just |
| 7 | +import io.mockk.mockk |
5 | 8 | import io.mockk.mockkObject |
| 9 | +import io.mockk.runs |
6 | 10 | import io.mockk.unmockkAll |
7 | 11 | import io.mockk.verify |
8 | 12 | import kotlinx.coroutines.Dispatchers |
| 13 | +import kotlinx.coroutines.delay |
9 | 14 | import kotlinx.coroutines.launch |
10 | 15 | import kotlinx.coroutines.runBlocking |
11 | 16 | import org.junit.After |
12 | | -import org.junit.Assert.* |
| 17 | +import org.junit.Assert.assertEquals |
| 18 | +import org.junit.Assert.assertFalse |
| 19 | +import org.junit.Assert.assertTrue |
13 | 20 | import org.junit.Before |
14 | 21 | import org.junit.Test |
15 | 22 | import java.util.Collections |
@@ -111,6 +118,37 @@ class UserDecisionRegistryTest { |
111 | 118 | verify(exactly = 1) { registry.requestDecision(any(), any(), any()) } |
112 | 119 | } |
113 | 120 |
|
| 121 | + @Test |
| 122 | + fun testCheck_MultipleDecisionsForSameCert_cancel() { |
| 123 | + val canSendFeedback = Semaphore(0) |
| 124 | + val nm = mockk<NotificationManagerCompat>() |
| 125 | + every { nm.cancel(any(), any()) } just runs |
| 126 | + every { NotificationUtils.createChannels(any()) } returns nm |
| 127 | + every { registry.requestDecision(testCert, any(), any()) } answers { |
| 128 | + thread { |
| 129 | + canSendFeedback.acquire() |
| 130 | + registry.onUserDecision(testCert, false) |
| 131 | + } |
| 132 | + } |
| 133 | + val results = Collections.synchronizedList(mutableListOf<Boolean>()) |
| 134 | + runBlocking { |
| 135 | + repeat(5) { |
| 136 | + val job = launch(Dispatchers.Default) { |
| 137 | + results += registry.check(testCert, true) |
| 138 | + } |
| 139 | + delay(1000) |
| 140 | + job.cancel() // Cancel the job |
| 141 | + delay(1000) |
| 142 | + } |
| 143 | + canSendFeedback.release() |
| 144 | + } |
| 145 | + synchronized(registry.pendingDecisions) { |
| 146 | + assertFalse(registry.pendingDecisions.containsKey(testCert)) |
| 147 | + } |
| 148 | + assertEquals(0, results.size) |
| 149 | + verify(exactly = 5) { registry.requestDecision(any(), any(), any()) } |
| 150 | + } |
| 151 | + |
114 | 152 | @Test |
115 | 153 | fun testCheck_UserDecisionImpossible() { |
116 | 154 | every { NotificationUtils.notificationsPermitted(any()) } returns false |
|
0 commit comments