|
4 | 4 | import static junit.framework.Assert.assertEquals; |
5 | 5 | import static org.junit.Assert.assertNull; |
6 | 6 | import static org.mockito.ArgumentMatchers.anyString; |
| 7 | +import static org.mockito.Mockito.clearInvocations; |
7 | 8 | import static org.mockito.Mockito.mock; |
8 | 9 | import static org.mockito.Mockito.never; |
9 | 10 | import static org.mockito.Mockito.spy; |
@@ -270,26 +271,53 @@ public void testOnEmbeddedMessagingDisabled() throws Exception { |
270 | 271 | public void testOnEmbeddedMessagingSyncSucceeded() throws Exception { |
271 | 272 | dispatcher.enqueueResponse("/embedded-messaging/messages", new MockResponse().setBody(IterableTestUtils.getResourceString("embedded_payload_single_1.json"))); |
272 | 273 | IterableEmbeddedManager embeddedManager = IterableApi.getInstance().getEmbeddedManager(); |
| 274 | + // Flush all automatic syncs (init + foreground) before adding listener |
| 275 | + shadowOf(getMainLooper()).idle(); |
273 | 276 |
|
274 | 277 | IterableEmbeddedUpdateHandler mockHandler = mock(IterableEmbeddedUpdateHandler.class); |
275 | 278 | embeddedManager.addUpdateListener(mockHandler); |
276 | 279 |
|
| 280 | + dispatcher.enqueueResponse("/embedded-messaging/messages", new MockResponse().setBody(IterableTestUtils.getResourceString("embedded_payload_single_1.json"))); |
277 | 281 | embeddedManager.syncMessages(); |
278 | 282 | shadowOf(getMainLooper()).idle(); |
279 | 283 |
|
280 | 284 | verify(mockHandler).onEmbeddedMessagingSyncSucceeded(); |
281 | 285 | verify(mockHandler, never()).onEmbeddedMessagingSyncFailed(anyString()); |
282 | | - assertEquals(1, embeddedManager.getMessages(0L).size()); |
| 286 | + } |
| 287 | + |
| 288 | + @Test |
| 289 | + public void testOnEmbeddedMessagingSyncFailedOnParseError() throws Exception { |
| 290 | + // Enqueue malformed responses so auto-syncs also fail (avoiding mixed success/failure) |
| 291 | + dispatcher.enqueueResponse("/embedded-messaging/messages", new MockResponse().setBody(IterableTestUtils.getResourceString("embedded_payload_malformed.json"))); |
| 292 | + dispatcher.enqueueResponse("/embedded-messaging/messages", new MockResponse().setBody(IterableTestUtils.getResourceString("embedded_payload_malformed.json"))); |
| 293 | + IterableEmbeddedManager embeddedManager = IterableApi.getInstance().getEmbeddedManager(); |
| 294 | + |
| 295 | + IterableEmbeddedUpdateHandler mockHandler = mock(IterableEmbeddedUpdateHandler.class); |
| 296 | + embeddedManager.addUpdateListener(mockHandler); |
| 297 | + shadowOf(getMainLooper()).idle(); |
| 298 | + clearInvocations(mockHandler); |
| 299 | + |
| 300 | + dispatcher.enqueueResponse("/embedded-messaging/messages", new MockResponse().setBody(IterableTestUtils.getResourceString("embedded_payload_malformed.json"))); |
| 301 | + embeddedManager.syncMessages(); |
| 302 | + shadowOf(getMainLooper()).idle(); |
| 303 | + |
| 304 | + verify(mockHandler).onEmbeddedMessagingSyncFailed(anyString()); |
| 305 | + verify(mockHandler, never()).onEmbeddedMessagingSyncSucceeded(); |
283 | 306 | } |
284 | 307 |
|
285 | 308 | @Test |
286 | 309 | public void testOnEmbeddedMessagingSyncFailed() throws Exception { |
| 310 | + // Enqueue 401 responses so auto-syncs also fail (avoiding mixed success/failure) |
| 311 | + dispatcher.enqueueResponse("/embedded-messaging/messages", new MockResponse().setResponseCode(401).setBody(IterableTestUtils.getResourceString("embedded_payload_bad_api_key.json"))); |
287 | 312 | dispatcher.enqueueResponse("/embedded-messaging/messages", new MockResponse().setResponseCode(401).setBody(IterableTestUtils.getResourceString("embedded_payload_bad_api_key.json"))); |
288 | 313 | IterableEmbeddedManager embeddedManager = IterableApi.getInstance().getEmbeddedManager(); |
289 | 314 |
|
290 | 315 | IterableEmbeddedUpdateHandler mockHandler = mock(IterableEmbeddedUpdateHandler.class); |
291 | 316 | embeddedManager.addUpdateListener(mockHandler); |
| 317 | + shadowOf(getMainLooper()).idle(); |
| 318 | + clearInvocations(mockHandler); |
292 | 319 |
|
| 320 | + dispatcher.enqueueResponse("/embedded-messaging/messages", new MockResponse().setResponseCode(401).setBody(IterableTestUtils.getResourceString("embedded_payload_bad_api_key.json"))); |
293 | 321 | embeddedManager.syncMessages(); |
294 | 322 | shadowOf(getMainLooper()).idle(); |
295 | 323 |
|
|
0 commit comments