Skip to content

Commit 6e897ad

Browse files
authored
[Mob 3158] - Flutter: add unit tests for method invocation code (#66)
* Adds Unit tests for onMethodCall Handler
1 parent cf9ba98 commit 6e897ad

File tree

4 files changed

+244
-2
lines changed

4 files changed

+244
-2
lines changed

android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ android {
3434
}
3535

3636
dependencies {
37-
implementation 'com.instabug.library:instabug:8.2.0'
37+
implementation 'com.instabug.library:instabug:8.2.2'
3838
testImplementation 'junit:junit:4.12'
39-
}
39+
}

example/android/app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,6 @@ dependencies {
5858
testImplementation 'junit:junit:4.12'
5959
androidTestImplementation 'com.android.support.test:runner:1.0.2'
6060
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
61+
testImplementation 'org.mockito:mockito-core:1.10.19'
62+
implementation 'org.mockito:mockito-core:1.10.19'
6163
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package com.instabug.instabugflutterexample;
2+
3+
import com.instabug.instabugflutter.InstabugFlutterPlugin;
4+
5+
import org.mockito.Mockito;
6+
import org.mockito.Spy;
7+
8+
import java.util.ArrayList;
9+
10+
import io.flutter.plugin.common.MethodCall;
11+
import io.flutter.plugin.common.MethodChannel.Result;
12+
13+
import static org.mockito.Matchers.any;
14+
import static org.mockito.Mockito.spy;
15+
import static org.mockito.Mockito.verify;
16+
17+
class OnMethodCallTests {
18+
19+
@Spy
20+
private InstabugFlutterPlugin instabugMock;
21+
22+
OnMethodCallTests() {
23+
instabugMock = spy(new InstabugFlutterPlugin());
24+
}
25+
26+
private void testMethodCall(final String methodName, final Object params) {
27+
final MethodCall methodCall = new MethodCall(methodName, params);
28+
final Result result = new Result() {
29+
@Override
30+
public void success(Object o) {
31+
}
32+
@Override
33+
public void error(String s, String s1, Object o) {
34+
}
35+
@Override
36+
public void notImplemented() {
37+
}
38+
};
39+
instabugMock.onMethodCall(methodCall,result);
40+
}
41+
42+
public void testShowWelcomeMessageWithMode() {
43+
String methodName = "showWelcomeMessageWithMode";
44+
ArrayList<Object> argsList = new ArrayList<>();
45+
argsList.add("WelcomeMessageMode.live");
46+
Mockito.doNothing().when(instabugMock).showWelcomeMessageWithMode(any(String.class));
47+
testMethodCall(methodName,argsList);
48+
verify(instabugMock).showWelcomeMessageWithMode("WelcomeMessageMode.live");
49+
}
50+
51+
public void testIdentifyUserWithEmail() {
52+
String methodName = "identifyUserWithEmail";
53+
ArrayList<Object> argsList = new ArrayList<>();
54+
argsList.add("[email protected]");
55+
argsList.add("Aly");
56+
Mockito.doNothing().when(instabugMock).identifyUserWithEmail(any(String.class),any(String.class));
57+
testMethodCall(methodName,argsList);
58+
verify(instabugMock).identifyUserWithEmail("[email protected]","Aly");
59+
}
60+
61+
public void testLogOut() {
62+
String methodName = "logOut";
63+
Mockito.doNothing().when(instabugMock).logOut();
64+
testMethodCall(methodName,null);
65+
verify(instabugMock).logOut();
66+
}
67+
68+
public void testAppendTags() {
69+
String methodName = "appendTags";
70+
ArrayList<Object> argsList = new ArrayList<>();
71+
ArrayList<String> tags = new ArrayList<>();
72+
tags.add("tag1");
73+
tags.add("tag2");
74+
argsList.add(tags);
75+
Mockito.doNothing().when(instabugMock).appendTags(any(ArrayList.class));
76+
testMethodCall(methodName,argsList);
77+
verify(instabugMock).appendTags(tags);
78+
}
79+
80+
public void testInvokeWithMode() {
81+
String methodName = "invokeWithMode";
82+
ArrayList<Object> argsList = new ArrayList<>();
83+
ArrayList<String> options = new ArrayList<>();
84+
options.add("commentFieldRequired");
85+
options.add("disablePostSendingDialog");
86+
argsList.add("bug");
87+
argsList.add(options);
88+
Mockito.doNothing().when(instabugMock).invokeWithMode(any(String.class),any(ArrayList.class));
89+
testMethodCall(methodName,argsList);
90+
verify(instabugMock).invokeWithMode("bug", options);
91+
}
92+
93+
public void testSetSessionProfilerEnabled() {
94+
String methodName = "setSessionProfilerEnabled";
95+
ArrayList<Object> argsList = new ArrayList<>();
96+
argsList.add(true);
97+
Mockito.doNothing().when(instabugMock).setSessionProfilerEnabled(any(Boolean.class));
98+
testMethodCall(methodName,argsList);
99+
verify(instabugMock).setSessionProfilerEnabled(true);
100+
}
101+
102+
public void testSetPrimaryColor() {
103+
String methodName = "setPrimaryColor";
104+
ArrayList<Object> argsList = new ArrayList<>();
105+
argsList.add(12312331231233L);
106+
Mockito.doNothing().when(instabugMock).setPrimaryColor(any(Long.class));
107+
testMethodCall(methodName,argsList);
108+
verify(instabugMock).setPrimaryColor(12312331231233L);
109+
}
110+
111+
public void testAddFileAttachmentWithData() {
112+
String methodName = "addFileAttachmentWithData";
113+
ArrayList<Object> argsList = new ArrayList<>();
114+
String string = "myfile";
115+
argsList.add(string.getBytes());
116+
argsList.add(string);
117+
Mockito.doNothing().when(instabugMock).addFileAttachmentWithData(any(byte[].class), any(String.class));
118+
testMethodCall(methodName,argsList);
119+
verify(instabugMock).addFileAttachmentWithData(string.getBytes(), string);
120+
}
121+
122+
public void testSetEnabledAttachmentTypes() {
123+
String methodName = "setEnabledAttachmentTypes";
124+
ArrayList<Object> argsList = new ArrayList<>();
125+
argsList.add(true);
126+
argsList.add(true);
127+
argsList.add(true);
128+
argsList.add(true);
129+
Mockito.doNothing().when(instabugMock).setEnabledAttachmentTypes(any(Boolean.class),any(Boolean.class),any(Boolean.class),any(Boolean.class));
130+
testMethodCall(methodName,argsList);
131+
verify(instabugMock).setEnabledAttachmentTypes(true,true,true,true);
132+
}
133+
134+
public void testSetEmailFieldRequiredForFeatureRequests() {
135+
String methodName = "setEmailFieldRequiredForFeatureRequests";
136+
ArrayList<Object> argsList = new ArrayList<>();
137+
ArrayList<String> actions = new ArrayList<>();
138+
actions.add("reportBug");
139+
actions.add("requestNewFeature");
140+
argsList.add(true);
141+
argsList.add(actions);
142+
Mockito.doNothing().when(instabugMock).setEmailFieldRequiredForFeatureRequests(any(Boolean.class),any(ArrayList.class));
143+
testMethodCall(methodName,argsList);
144+
verify(instabugMock).setEmailFieldRequiredForFeatureRequests(true,actions);
145+
}
146+
147+
148+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.instabug.instabugflutterexample;
2+
3+
import org.junit.Test;
4+
5+
6+
public class InstabugFlutterPluginTest {
7+
8+
/**
9+
Each Method in this class is preceded with a comment identifying the type of parameters
10+
that are tested
11+
*/
12+
13+
/**
14+
* (String)
15+
*/
16+
@Test
17+
public void testShowWelcomeMessageWithMode() {
18+
new OnMethodCallTests().testShowWelcomeMessageWithMode();
19+
}
20+
21+
/**
22+
* (String, String)
23+
*/
24+
@Test
25+
public void testIdentifyUserWithEmail() {
26+
new OnMethodCallTests().testIdentifyUserWithEmail();
27+
}
28+
29+
/**
30+
* ()
31+
*/
32+
@Test
33+
public void testLogOut() {
34+
new OnMethodCallTests().testLogOut();
35+
}
36+
37+
/**
38+
* (ArrayList<String>)
39+
*/
40+
@Test
41+
public void testAppendTags() {
42+
new OnMethodCallTests().testAppendTags();
43+
}
44+
45+
/**
46+
* (String, ArrayList<String>)
47+
*/
48+
@Test
49+
public void testInvokeWithMode() {
50+
new OnMethodCallTests().testInvokeWithMode();
51+
}
52+
53+
/**
54+
* (boolean)
55+
*/
56+
@Test
57+
public void testSetSessionProfilerEnabled() {
58+
new OnMethodCallTests().testSetSessionProfilerEnabled();
59+
}
60+
61+
/**
62+
* (long)
63+
*/
64+
@Test
65+
public void testSetPrimaryColor() {
66+
new OnMethodCallTests().testSetPrimaryColor();
67+
}
68+
69+
/**
70+
* (byte[], String)
71+
*/
72+
@Test
73+
public void testAddFileAttachmentWithData() {
74+
new OnMethodCallTests().testAddFileAttachmentWithData();
75+
}
76+
77+
/**
78+
* (boolean, boolean, boolean, boolean)
79+
*/
80+
@Test
81+
public void testSetEnabledAttachmentTypes() {
82+
new OnMethodCallTests().testSetEnabledAttachmentTypes();
83+
}
84+
85+
/**
86+
* (boolean, ArrayList<String>)
87+
*/
88+
@Test
89+
public void testSetEmailFieldRequiredForFeatureRequests() {
90+
new OnMethodCallTests().testSetEmailFieldRequiredForFeatureRequests();
91+
}
92+
}

0 commit comments

Comments
 (0)