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