-
Notifications
You must be signed in to change notification settings - Fork 205
/
BugsnagTestUtils.java
112 lines (90 loc) · 3.54 KB
/
BugsnagTestUtils.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
package com.bugsnag.android;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.test.InstrumentationRegistry;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Date;
import java.util.Map;
final class BugsnagTestUtils {
private BugsnagTestUtils() {
}
private static String streamableToString(JsonStream.Streamable streamable) throws IOException {
StringWriter writer = new StringWriter();
JsonStream jsonStream = new JsonStream(writer);
streamable.toStream(jsonStream);
return writer.toString();
}
static JSONObject mapToJson(Map<String, Object> map) {
return new JSONObject(map);
}
static JSONObject streamableToJson(JsonStream.Streamable streamable)
throws JSONException, IOException {
return new JSONObject(streamableToString(streamable));
}
static JSONArray streamableToJsonArray(JsonStream.Streamable streamable)
throws JSONException, IOException {
return new JSONArray(streamableToString(streamable));
}
static SharedPreferences getSharedPrefs(Context context) {
return context.getSharedPreferences("com.bugsnag.android", Context.MODE_PRIVATE);
}
static Client generateClient() {
Client client = new Client(InstrumentationRegistry.getContext(), "api-key");
client.config.setDelivery(generateDelivery());
return client;
}
static Session generateSession() {
return new Session("test", new Date(), new User(), false);
}
static Configuration generateConfiguration() {
Configuration configuration = new Configuration("test");
configuration.setDelivery(generateDelivery());
return configuration;
}
static SessionTracker generateSessionTracker() {
return new SessionTracker(generateConfiguration(), BugsnagTestUtils.generateClient(),
generateSessionStore());
}
@NonNull
static SessionStore generateSessionStore() {
return new SessionStore(generateConfiguration(), InstrumentationRegistry.getContext());
}
@NonNull
static SessionTrackingApiClient generateSessionTrackingApiClient() {
return new SessionTrackingApiClient() {
@Override
public void postSessionTrackingPayload(String urlString,
SessionTrackingPayload payload,
Map<String, String> headers)
throws NetworkException, BadResponseException {
}
};
}
static ErrorReportApiClient generateErrorReportApiClient() { // no-op
return new ErrorReportApiClient() {
@Override
public void postReport(String urlString,
Report report,
Map<String, String> headers)
throws NetworkException, BadResponseException {
}
};
}
public static Delivery generateDelivery() {
return new Delivery() {
@Override
public void deliver(SessionTrackingPayload payload,
Configuration config)
throws DeliveryFailureException {}
@Override
public void deliver(Report report,
Configuration config)
throws DeliveryFailureException {}
};
}
}