From 0dceb86cb837e05029f9e131291fe845b1ee6438 Mon Sep 17 00:00:00 2001 From: Christian Sadilek Date: Thu, 29 Jul 2021 09:37:58 -0400 Subject: [PATCH] [fenix] Fix intermittent test failures in WebPushEngineIntegrationTest --- .../fenix/push/WebPushEngineIntegrationTest.kt | 133 +++++++++++++-------- 1 file changed, 81 insertions(+), 52 deletions(-) diff --git a/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/push/WebPushEngineIntegrationTest.kt b/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/push/WebPushEngineIntegrationTest.kt index ffe508aeaa79..4ed2af5b38ae 100644 --- a/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/push/WebPushEngineIntegrationTest.kt +++ b/mobile/android/fenix/app/src/test/java/org/mozilla/fenix/push/WebPushEngineIntegrationTest.kt @@ -27,6 +27,11 @@ import mozilla.components.concept.engine.webpush.WebPushSubscription import mozilla.components.feature.push.AutoPushFeature import mozilla.components.feature.push.AutoPushSubscription import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue import org.junit.Before import org.junit.Test @@ -87,14 +92,20 @@ class WebPushEngineIntegrationTest { @Test fun `delegate calls getSubscription`() { integration.start() - val slot = slot<(AutoPushSubscription?) -> Unit>() - every { pushFeature.getSubscription("scope", block = capture(slot)) } just Runs + var subscribeFn: ((AutoPushSubscription?) -> Unit)? = null + every { pushFeature.getSubscription("scope", block = any()) } answers { + subscribeFn = thirdArg() + } + + var actualSubscription: WebPushSubscription? = null + delegate.captured.onGetSubscription("scope", onSubscription = { + actualSubscription = it + }) - val onSubscription = mockk<(WebPushSubscription?) -> Unit>(relaxed = true) - delegate.captured.onGetSubscription("scope", onSubscription) + assertNull(actualSubscription) + assertNotNull(subscribeFn) - verify { onSubscription wasNot Called } - slot.captured(AutoPushSubscription( + subscribeFn!!(AutoPushSubscription( scope = "scope", publicKey = "abc", endpoint = "def", @@ -102,83 +113,101 @@ class WebPushEngineIntegrationTest { appServerKey = null )) - verify { - onSubscription( - WebPushSubscription( - scope = "scope", - publicKey = "abc".toByteArray(), - endpoint = "def", - authSecret = "xyz".toByteArray(), - appServerKey = null - ) - ) - } + val expectedSubscription = WebPushSubscription( + scope = "scope", + publicKey = "abc".toByteArray(), + endpoint = "def", + authSecret = "xyz".toByteArray(), + appServerKey = null + ) + assertEquals(expectedSubscription, actualSubscription) } @Test fun `delegate calls subscribe`() { integration.start() - val onSubscribeError = slot<(Exception) -> Unit>() - val onSubscribe = slot<(AutoPushSubscription?) -> Unit>() + var onSubscribeErrorFn: ((Exception) -> Unit)? = null + var onSubscribeFn: ((AutoPushSubscription?) -> Unit)? = null every { pushFeature.subscribe( scope = "scope", appServerKey = null, - onSubscribeError = capture(onSubscribeError), - onSubscribe = capture(onSubscribe) + onSubscribeError = any(), + onSubscribe = any() ) - } just Runs - - val onSubscription = mockk<(WebPushSubscription?) -> Unit>(relaxed = true) - delegate.captured.onSubscribe("scope", null, onSubscription) + } answers { + onSubscribeErrorFn = thirdArg() + onSubscribeFn = lastArg() + } - verify { onSubscription wasNot Called } + var actualSubscription: WebPushSubscription? = null + var onSubscribeInvoked = false + delegate.captured.onSubscribe("scope", null) { + actualSubscription = it + onSubscribeInvoked = true + } + assertFalse(onSubscribeInvoked) + assertNull(actualSubscription) - onSubscribeError.captured(mockk()) - verify { onSubscription(null) } + assertNotNull(onSubscribeErrorFn) + onSubscribeErrorFn!!(mockk()) + assertTrue(onSubscribeInvoked) + assertNull(actualSubscription) - onSubscribe.captured(AutoPushSubscription( + assertNotNull(onSubscribeFn) + onSubscribeFn!!(AutoPushSubscription( scope = "scope", publicKey = "abc", endpoint = "def", authKey = "xyz", appServerKey = null )) - verify { - onSubscription( - WebPushSubscription( - scope = "scope", - publicKey = "abc".toByteArray(), - endpoint = "def", - authSecret = "xyz".toByteArray(), - appServerKey = null - ) - ) - } + + val expectedSubscription = WebPushSubscription( + scope = "scope", + publicKey = "abc".toByteArray(), + endpoint = "def", + authSecret = "xyz".toByteArray(), + appServerKey = null + ) + + assertEquals(expectedSubscription, actualSubscription) } @Test fun `delegate calls unsubscribe`() { integration.start() - val onUnsubscribeError = slot<(Exception) -> Unit>() - val onUnsubscribe = slot<(Boolean) -> Unit>() + var onUnsubscribeErrorFn: ((Exception) -> Unit)? = null + var onUnsubscribeFn: ((Boolean) -> Unit)? = null every { pushFeature.unsubscribe( scope = "scope", - onUnsubscribeError = capture(onUnsubscribeError), - onUnsubscribe = capture(onUnsubscribe) + onUnsubscribeError = any(), + onUnsubscribe = any() ) - } just Runs + } answers { + onUnsubscribeErrorFn = secondArg() + onUnsubscribeFn = thirdArg() + } - val onUnsubscription = mockk<(Boolean) -> Unit>(relaxed = true) - delegate.captured.onUnsubscribe("scope", onUnsubscription) + var onSubscribeInvoked = false + var unsubscribeSuccess: Boolean? = null + delegate.captured.onUnsubscribe("scope") { + onSubscribeInvoked = true + unsubscribeSuccess = it + } - verify { onUnsubscription wasNot Called } + assertFalse(onSubscribeInvoked) + assertNull(unsubscribeSuccess) - onUnsubscribeError.captured(mockk()) - verify { onUnsubscription(false) } + assertNotNull(onUnsubscribeErrorFn) + onUnsubscribeErrorFn!!(mockk()) + assertNotNull(unsubscribeSuccess) + assertFalse(unsubscribeSuccess!!) - onUnsubscribe.captured(true) - verify { onUnsubscription(true) } + assertNotNull(onUnsubscribeFn) + onUnsubscribeFn!!(true) + assertNotNull(unsubscribeSuccess) + assertTrue(unsubscribeSuccess!!) } } -- 2.11.4.GIT