Skip to content

Commit 3ccb7dc

Browse files
committed
Closes mozilla-mobile#4458: Use cached registration token from ADM if available
Since the ADM instance does not live in the app itself, clearing app data doesn't remove the registration ID for it. This means that we never initialize the native PushManager with the cached code. Our fix is straight-forward: if we have a cached registration ID, invoke the `PushProcessor#onNewToken` and follow the regular flow.
1 parent 9fecd17 commit 3ccb7dc

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
lines changed

components/lib/push-amazon/src/main/java/mozilla/components/lib/push/amazon/AbstractAmazonPushService.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ abstract class AbstractAmazonPushService : ADMMessageHandlerBase("AbstractAmazon
2828

2929
override fun start(context: Context) {
3030
adm = ADM(context)
31-
if (adm.registrationId == null) {
31+
32+
val registrationId = adm.registrationId
33+
if (registrationId == null) {
3234
adm.startRegister()
35+
} else {
36+
PushProcessor.requireInstance.onNewToken(registrationId)
3337
}
3438
}
3539

components/lib/push-amazon/src/test/java/mozilla/components/lib/push/amazon/AbstractAmazonPushServiceTest.kt

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ import org.junit.Assert.assertTrue
2222
import org.junit.Before
2323
import org.junit.Test
2424
import org.junit.runner.RunWith
25-
import org.mockito.Mockito
25+
import org.mockito.Mockito.anyString
26+
import org.mockito.Mockito.never
27+
import org.mockito.Mockito.reset
28+
import org.mockito.Mockito.verify
29+
import org.mockito.Mockito.verifyZeroInteractions
2630
import org.robolectric.RobolectricTestRunner
2731
import org.robolectric.annotation.Config
2832
import org.robolectric.annotation.Implementation
@@ -36,15 +40,23 @@ class AbstractAmazonPushServiceTest {
3640

3741
@Before
3842
fun setup() {
39-
Mockito.reset(processor)
43+
reset(processor)
4044
PushProcessor.install(processor)
4145
}
4246

47+
@Test
48+
fun `if registrationId exists startRegister invokes onNewToken`() {
49+
val testService = TestService()
50+
51+
testService.start(testContext)
52+
verify(processor).onNewToken(anyString())
53+
}
54+
4355
@Test
4456
fun `onNewToken passes token to processor`() {
4557
service.onRegistered("token")
4658

47-
Mockito.verify(processor).onNewToken("token")
59+
verify(processor).onNewToken("token")
4860
}
4961

5062
@Test
@@ -61,7 +73,7 @@ class AbstractAmazonPushServiceTest {
6173
messageIntent.putExtras(bundleExtra)
6274
service.onMessage(messageIntent)
6375

64-
Mockito.verify(processor).onMessageReceived(captor.capture())
76+
verify(processor).onMessageReceived(captor.capture())
6577

6678
assertEquals("1234", captor.value.channelId)
6779
assertEquals("contents", captor.value.body)
@@ -82,7 +94,7 @@ class AbstractAmazonPushServiceTest {
8294

8395
service.onRegistrationError("123")
8496

85-
Mockito.verify(processor).onError(captor.capture())
97+
verify(processor).onError(captor.capture())
8698

8799
assertTrue(captor.value is PushError.Registration)
88100
assertTrue(captor.value.desc.contains("registration failed"))
@@ -98,7 +110,7 @@ class AbstractAmazonPushServiceTest {
98110
messageIntent.putExtras(bundleExtra)
99111
service.onMessage(messageIntent)
100112

101-
Mockito.verify(processor).onError(captor.capture())
113+
verify(processor).onError(captor.capture())
102114

103115
assertTrue(captor.value is PushError.MalformedMessage)
104116
assertTrue(captor.value.desc.contains("NoSuchElementException"))
@@ -109,15 +121,37 @@ class AbstractAmazonPushServiceTest {
109121
val messageIntent = Intent()
110122
service.onMessage(messageIntent)
111123

112-
Mockito.verifyZeroInteractions(processor)
124+
verifyZeroInteractions(processor)
113125
}
114126

115127
@Test
116128
fun `service available reflects Amazon Device Messaging availability`() {
117129
assertTrue(service.isServiceAvailable(testContext))
118130
}
131+
}
132+
133+
class TestService : AbstractAmazonPushService()
134+
135+
@RunWith(RobolectricTestRunner::class)
136+
@Config(shadows = [ShadowADMMessageHandlerBase::class, ShadowADM2::class])
137+
class AbstractAmazonPushServiceRegistrationTest {
138+
139+
private val processor: PushProcessor = mock()
140+
private val service = TestService()
141+
142+
@Before
143+
fun setup() {
144+
reset(processor)
145+
PushProcessor.install(processor)
146+
}
119147

120-
class TestService : AbstractAmazonPushService()
148+
@Test
149+
fun `if registrationId does NOT exist startRegister never invokes onNewToken`() {
150+
val testService = TestService()
151+
152+
testService.start(testContext)
153+
verify(processor, never()).onNewToken(anyString())
154+
}
121155
}
122156

123157
/**
@@ -146,4 +180,23 @@ class ShadowADM {
146180
fun __constructor__(context: Context) {}
147181

148182
fun isSupported() = true
183+
184+
fun getRegistrationId() = "123"
149185
}
186+
187+
/**
188+
* Custom Shadow for [ADM] where the registration ID is null. Currently, we have no way to alter the
189+
* Shadow class inside of the service, so this is our work around.
190+
*/
191+
@Implements(ADM::class)
192+
class ShadowADM2 {
193+
@Implementation
194+
@Suppress("UNUSED_PARAMETER")
195+
fun __constructor__(context: Context) {}
196+
197+
fun isSupported() = true
198+
199+
fun getRegistrationId(): String? = null
200+
201+
fun startRegister() {}
202+
}

0 commit comments

Comments
 (0)