This repository was archived by the owner on Dec 29, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathCrashReportingApiTest.java
More file actions
119 lines (89 loc) · 3.72 KB
/
CrashReportingApiTest.java
File metadata and controls
119 lines (89 loc) · 3.72 KB
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
115
116
117
118
119
package com.instabug.flutter;
import static com.instabug.crash.CrashReporting.getFingerprintObject;
import static com.instabug.flutter.util.GlobalMocks.reflected;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import com.instabug.crash.CrashReporting;
import com.instabug.crash.models.IBGNonFatalException;
import com.instabug.flutter.generated.CrashReportingPigeon;
import com.instabug.flutter.modules.CrashReportingApi;
import com.instabug.flutter.util.ArgsRegistry;
import com.instabug.flutter.util.GlobalMocks;
import com.instabug.flutter.util.MockReflected;
import com.instabug.library.Feature;
import org.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.MockedStatic;
import java.util.HashMap;
import java.util.Map;
import io.flutter.plugin.common.BinaryMessenger;
public class CrashReportingApiTest {
private final CrashReportingApi api = new CrashReportingApi();
private MockedStatic<CrashReporting> mCrashReporting;
private MockedStatic<CrashReportingPigeon.CrashReportingHostApi> mHostApi;
@Before
public void setUp() throws NoSuchMethodException {
mCrashReporting = mockStatic(CrashReporting.class);
mHostApi = mockStatic(CrashReportingPigeon.CrashReportingHostApi.class);
GlobalMocks.setUp();
}
@After
public void cleanUp() {
mCrashReporting.close();
mHostApi.close();
GlobalMocks.close();
}
@Test
public void testInit() {
BinaryMessenger messenger = mock(BinaryMessenger.class);
CrashReportingApi.init(messenger);
mHostApi.verify(() -> CrashReportingPigeon.CrashReportingHostApi.setup(eq(messenger), any(CrashReportingApi.class)));
}
@Test
public void testSetEnabledGivenTrue() {
boolean isEnabled = true;
api.setEnabled(isEnabled);
mCrashReporting.verify(() -> CrashReporting.setState(Feature.State.ENABLED));
}
@Test
public void testSetEnabledGivenFalse() {
boolean isEnabled = false;
api.setEnabled(isEnabled);
mCrashReporting.verify(() -> CrashReporting.setState(Feature.State.DISABLED));
}
@Test
public void testSend() {
String jsonCrash = "{}";
boolean isHandled = false;
api.send(jsonCrash, isHandled);
reflected.verify(() -> MockReflected.crashReportException(any(JSONObject.class), eq(isHandled)));
}
@Test
public void testSendNonFatalError() {
String jsonCrash = "{}";
boolean isHandled = true;
String fingerPrint = "test";
Map<String, String> expectedUserAttributes = new HashMap<>();
String level = ArgsRegistry.nonFatalExceptionLevel.keySet().iterator().next();
JSONObject expectedFingerprint = getFingerprintObject(fingerPrint);
IBGNonFatalException.Level expectedLevel = ArgsRegistry.nonFatalExceptionLevel.get(level);
api.sendNonFatalError(jsonCrash, expectedUserAttributes, fingerPrint, level);
reflected.verify(() -> MockReflected.crashReportException(any(JSONObject.class), eq(isHandled), eq(expectedUserAttributes), eq(expectedFingerprint), eq(expectedLevel)));
}
@Test
public void testSetNDKEnabledGivenTrue() {
boolean isEnabled = true;
api.setNDKEnabled(isEnabled);
mCrashReporting.verify(() -> CrashReporting.setNDKCrashesState(Feature.State.ENABLED));
}
@Test
public void testSetNDKEnabledGivenFalse() {
boolean isEnabled = false;
api.setNDKEnabled(isEnabled);
mCrashReporting.verify(() -> CrashReporting.setNDKCrashesState(Feature.State.DISABLED));
}
}