|
| 1 | +package com.bugsnag.android; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | +import static org.junit.Assert.assertNull; |
| 6 | +import static org.junit.Assert.assertTrue; |
| 7 | + |
| 8 | +import android.support.test.filters.SmallTest; |
| 9 | +import android.support.test.runner.AndroidJUnit4; |
| 10 | + |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | +import org.junit.runner.RunWith; |
| 14 | + |
| 15 | +import java.util.Map; |
| 16 | +import java.util.concurrent.CountDownLatch; |
| 17 | +import java.util.concurrent.TimeUnit; |
| 18 | + |
| 19 | +@RunWith(AndroidJUnit4.class) |
| 20 | +@SmallTest |
| 21 | +public class ClientNotifyAsyncTest { |
| 22 | + |
| 23 | + private Client client; |
| 24 | + private NullCheckClient apiClient; |
| 25 | + |
| 26 | + /** |
| 27 | + * Generates a configuration and clears sharedPrefs values to begin the test with a clean slate |
| 28 | + * |
| 29 | + * @throws Exception if initialisation failed |
| 30 | + */ |
| 31 | + @Before |
| 32 | + public void setUp() throws Exception { |
| 33 | + client = BugsnagTestUtils.generateClient(); |
| 34 | + apiClient = new NullCheckClient(); |
| 35 | + client.setErrorReportApiClient(apiClient); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testNotifyAsyncMetadata() throws Exception { |
| 40 | + MetaData metaData = new MetaData(); |
| 41 | + metaData.addToTab("animals", "dog", true); |
| 42 | + |
| 43 | + client.notify(new RuntimeException("Foo"), metaData); |
| 44 | + assertNull(apiClient.report); |
| 45 | + apiClient.nullCheckLatch.countDown(); |
| 46 | + |
| 47 | + apiClient.awaitReport(); |
| 48 | + assertNotNull(apiClient.report); |
| 49 | + MetaData data = apiClient.report.getError().getMetaData(); |
| 50 | + assertTrue((Boolean) data.getTab("animals").get("dog")); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testNotifyAsyncSeverity() throws Exception { |
| 55 | + client.notify(new RuntimeException("Foo"), Severity.INFO); |
| 56 | + assertNull(apiClient.report); |
| 57 | + apiClient.nullCheckLatch.countDown(); |
| 58 | + |
| 59 | + apiClient.awaitReport(); |
| 60 | + assertNotNull(apiClient.report); |
| 61 | + assertEquals(Severity.INFO, apiClient.report.getError().getSeverity()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testNotifyAsyncSeverityMetadata() throws Exception { |
| 66 | + MetaData metaData = new MetaData(); |
| 67 | + metaData.addToTab("animals", "bird", "chicken"); |
| 68 | + |
| 69 | + client.notify(new RuntimeException("Foo"), Severity.ERROR, metaData); |
| 70 | + assertNull(apiClient.report); |
| 71 | + apiClient.nullCheckLatch.countDown(); |
| 72 | + |
| 73 | + apiClient.awaitReport(); |
| 74 | + assertNotNull(apiClient.report); |
| 75 | + MetaData data = apiClient.report.getError().getMetaData(); |
| 76 | + assertEquals("chicken", data.getTab("animals").get("bird")); |
| 77 | + assertEquals(Severity.ERROR, apiClient.report.getError().getSeverity()); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testNotifyAsyncCallback() throws Exception { |
| 82 | + client.notify(new RuntimeException("Foo"), new Callback() { |
| 83 | + @Override |
| 84 | + public void beforeNotify(Report report) { |
| 85 | + report.getError().setContext("Manual"); |
| 86 | + } |
| 87 | + }); |
| 88 | + assertNull(apiClient.report); |
| 89 | + apiClient.nullCheckLatch.countDown(); |
| 90 | + |
| 91 | + apiClient.awaitReport(); |
| 92 | + assertNotNull(apiClient.report); |
| 93 | + assertEquals("Manual", apiClient.report.getError().getContext()); |
| 94 | + } |
| 95 | + |
| 96 | + static class NullCheckClient extends ClientNotifyTest.FakeClient { |
| 97 | + |
| 98 | + CountDownLatch nullCheckLatch = new CountDownLatch(1); |
| 99 | + |
| 100 | + @Override |
| 101 | + public void postReport(String urlString, |
| 102 | + Report report, |
| 103 | + Map<String, String> headers) |
| 104 | + throws NetworkException, BadResponseException { |
| 105 | + try { |
| 106 | + nullCheckLatch.await(100, TimeUnit.MILLISECONDS); |
| 107 | + } catch (InterruptedException exception) { |
| 108 | + exception.printStackTrace(); |
| 109 | + } |
| 110 | + super.postReport(urlString, report, headers); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments