@@ -22,7 +22,11 @@ import org.junit.Assert.assertTrue
22
22
import org.junit.Before
23
23
import org.junit.Test
24
24
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
26
30
import org.robolectric.RobolectricTestRunner
27
31
import org.robolectric.annotation.Config
28
32
import org.robolectric.annotation.Implementation
@@ -36,15 +40,23 @@ class AbstractAmazonPushServiceTest {
36
40
37
41
@Before
38
42
fun setup () {
39
- Mockito . reset(processor)
43
+ reset(processor)
40
44
PushProcessor .install(processor)
41
45
}
42
46
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
+
43
55
@Test
44
56
fun `onNewToken passes token to processor` () {
45
57
service.onRegistered(" token" )
46
58
47
- Mockito . verify(processor).onNewToken(" token" )
59
+ verify(processor).onNewToken(" token" )
48
60
}
49
61
50
62
@Test
@@ -61,7 +73,7 @@ class AbstractAmazonPushServiceTest {
61
73
messageIntent.putExtras(bundleExtra)
62
74
service.onMessage(messageIntent)
63
75
64
- Mockito . verify(processor).onMessageReceived(captor.capture())
76
+ verify(processor).onMessageReceived(captor.capture())
65
77
66
78
assertEquals(" 1234" , captor.value.channelId)
67
79
assertEquals(" contents" , captor.value.body)
@@ -82,7 +94,7 @@ class AbstractAmazonPushServiceTest {
82
94
83
95
service.onRegistrationError(" 123" )
84
96
85
- Mockito . verify(processor).onError(captor.capture())
97
+ verify(processor).onError(captor.capture())
86
98
87
99
assertTrue(captor.value is PushError .Registration )
88
100
assertTrue(captor.value.desc.contains(" registration failed" ))
@@ -98,7 +110,7 @@ class AbstractAmazonPushServiceTest {
98
110
messageIntent.putExtras(bundleExtra)
99
111
service.onMessage(messageIntent)
100
112
101
- Mockito . verify(processor).onError(captor.capture())
113
+ verify(processor).onError(captor.capture())
102
114
103
115
assertTrue(captor.value is PushError .MalformedMessage )
104
116
assertTrue(captor.value.desc.contains(" NoSuchElementException" ))
@@ -109,15 +121,37 @@ class AbstractAmazonPushServiceTest {
109
121
val messageIntent = Intent ()
110
122
service.onMessage(messageIntent)
111
123
112
- Mockito . verifyZeroInteractions(processor)
124
+ verifyZeroInteractions(processor)
113
125
}
114
126
115
127
@Test
116
128
fun `service available reflects Amazon Device Messaging availability` () {
117
129
assertTrue(service.isServiceAvailable(testContext))
118
130
}
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
+ }
119
147
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
+ }
121
155
}
122
156
123
157
/* *
@@ -146,4 +180,23 @@ class ShadowADM {
146
180
fun __constructor__ (context : Context ) {}
147
181
148
182
fun isSupported () = true
183
+
184
+ fun getRegistrationId () = " 123"
149
185
}
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