-
Notifications
You must be signed in to change notification settings - Fork 205
/
ClientNotifyTest.java
111 lines (93 loc) · 3.44 KB
/
ClientNotifyTest.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
package com.bugsnag.android;
import static org.junit.Assert.assertEquals;
import android.support.annotation.NonNull;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import org.junit.After;
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 ClientNotifyTest {
private Client client;
private FakeClient apiClient;
/**
* Generates a configuration and clears sharedPrefs values to begin the test with a clean slate
*
* @throws Exception if initialisation failed
*/
@SuppressWarnings("deprecation")
@Before
public void setUp() throws Exception {
client = BugsnagTestUtils.generateClient();
apiClient = new FakeClient();
client.setErrorReportApiClient(apiClient);
}
@After
public void tearDown() throws Exception {
client.getOrientationListener().disable();
}
@Test
public void testNotifyBlockingDefaultSeverity() {
client.notifyBlocking(new RuntimeException("Testing"));
assertEquals(Severity.WARNING, apiClient.report.getError().getSeverity());
}
@Test
public void testNotifyBlockingCallback() {
client.notifyBlocking(new RuntimeException("Testing"), new Callback() {
@Override
public void beforeNotify(@NonNull Report report) {
report.getError().setUserName("Foo");
}
});
Error error = apiClient.report.getError();
assertEquals(Severity.WARNING, error.getSeverity());
assertEquals("Foo", error.getUser().getName());
}
@Test
public void testNotifyBlockingCustomSeverity() {
client.notifyBlocking(new RuntimeException("Testing"), Severity.INFO);
assertEquals(Severity.INFO, apiClient.report.getError().getSeverity());
}
@Test
public void testNotifyBlockingCustomStackTrace() {
StackTraceElement[] stacktrace = {
new StackTraceElement("MyClass", "MyMethod", "MyFile", 5)
};
client.notifyBlocking("Name", "Message", stacktrace, new Callback() {
@Override
public void beforeNotify(@NonNull Report report) {
report.getError().setSeverity(Severity.ERROR);
}
});
Error error = apiClient.report.getError();
assertEquals(Severity.ERROR, error.getSeverity());
assertEquals("Name", error.getExceptionName());
assertEquals("Message", error.getExceptionMessage());
}
@SuppressWarnings("deprecation")
static class FakeClient implements ErrorReportApiClient {
CountDownLatch latch = new CountDownLatch(1);
Report report;
@Override
public void postReport(@NonNull String urlString,
@NonNull Report report,
@NonNull Map<String, String> headers)
throws NetworkException, BadResponseException {
try {
Thread.sleep(10); // simulate async request
} catch (InterruptedException ignored) {
ignored.printStackTrace();
}
this.report = report;
latch.countDown();
}
void awaitReport() throws InterruptedException {
latch.await(2000, TimeUnit.MILLISECONDS);
}
}
}