Skip to content

Commit b3f96f3

Browse files
Add Android tests to Instabug Sample
1 parent c736520 commit b3f96f3

File tree

6 files changed

+333
-0
lines changed

6 files changed

+333
-0
lines changed

InstabugSample/android/app/build.gradle

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ android {
4343
targetSdkVersion 29
4444
versionCode flutterVersionCode.toInteger()
4545
versionName flutterVersionName
46+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
47+
multiDexEnabled true
4648
}
4749

4850
buildTypes {
@@ -52,6 +54,10 @@ android {
5254
signingConfig signingConfigs.debug
5355
}
5456
}
57+
58+
configurations.all {
59+
resolutionStrategy.force 'org.hamcrest:hamcrest-core:1.3'
60+
}
5561
}
5662

5763
flutter {
@@ -60,4 +66,11 @@ flutter {
6066

6167
dependencies {
6268
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
69+
testImplementation 'junit:junit:4.12'
70+
implementation 'com.android.support:multidex:1.0.3'
71+
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
72+
androidTestImplementation 'androidx.test:rules:1.1.1'
73+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
74+
testImplementation 'org.mockito:mockito-core:1.10.19'
75+
implementation 'org.mockito:mockito-core:1.10.19'
6376
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.example.InstabugSample;
2+
3+
import androidx.test.rule.ActivityTestRule;
4+
import androidx.test.ext.junit.runners.AndroidJUnit4;
5+
6+
import org.junit.Rule;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import java.lang.reflect.InvocationTargetException;
11+
import java.lang.reflect.Method;
12+
13+
import static androidx.test.espresso.Espresso.onView;
14+
import static androidx.test.espresso.action.ViewActions.click;
15+
import static androidx.test.espresso.action.ViewActions.replaceText;
16+
import static androidx.test.espresso.matcher.ViewMatchers.withResourceName;
17+
import static androidx.test.espresso.matcher.ViewMatchers.withText;
18+
19+
@RunWith(AndroidJUnit4.class)
20+
public class InvokeInstabugUITest {
21+
22+
@Rule
23+
public ActivityTestRule<MainActivity> mActivityRule =
24+
new ActivityTestRule<>(MainActivity.class);
25+
26+
@Test
27+
public void ensureInstabugInvocati1on() throws InterruptedException {
28+
disableScreenShotByMediaProjection();
29+
Thread.sleep(1000);
30+
onView(withResourceName("instabug_floating_button")).perform(click());
31+
Thread.sleep(1000);
32+
onView(withText("Report a bug")).perform(click());
33+
Thread.sleep(1000);
34+
onView(withResourceName("instabug_edit_text_email")).perform(replaceText("[email protected]"));
35+
onView(withResourceName("instabug_bugreporting_send")).perform(click());
36+
onView(withResourceName("instabug_success_dialog_container")).perform(click());
37+
}
38+
39+
private void disableScreenShotByMediaProjection() {
40+
try {
41+
Method method = getMethod(Class.forName("com.instabug.bug.BugReporting"), "setScreenshotByMediaProjectionEnabled", boolean.class);
42+
if (method != null) {
43+
method.invoke(null, false);
44+
}
45+
} catch (ClassNotFoundException e) {
46+
e.printStackTrace();
47+
} catch (IllegalAccessException e) {
48+
e.printStackTrace();
49+
} catch (InvocationTargetException e) {
50+
e.printStackTrace();
51+
}
52+
}
53+
54+
public static Method getMethod(Class clazz, String methodName, Class... parameterType) {
55+
final Method[] methods = clazz.getDeclaredMethods();
56+
for (Method method : methods) {
57+
if (method.getName().equals(methodName) && method.getParameterTypes().length ==
58+
parameterType.length) {
59+
for (int i = 0; i < parameterType.length; i++) {
60+
if (method.getParameterTypes()[i] == parameterType[i]) {
61+
if (i == method.getParameterTypes().length - 1) {
62+
method.setAccessible(true);
63+
return method;
64+
}
65+
} else {
66+
break;
67+
}
68+
}
69+
}
70+
}
71+
return null;
72+
}
73+
}
74+

InstabugSample/android/app/src/main/AndroidManifest.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
22
package="com.example.InstabugSample">
3+
4+
<!-- The INTERNET permission is required for development. Specifically,
5+
flutter needs it to communicate with the running application
6+
to allow setting breakpoints, to provide hot reload, etc.
7+
-->
8+
<uses-permission android:name="android.permission.INTERNET"/>
9+
310
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
411
calls FlutterMain.startInitialization(this); in its onCreate method.
512
In most cases you can leave this as-is, but you if you want to provide
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package com.example.InstabugSample;
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 testShowBugReportingWithReportTypeAndOptions() {
81+
String methodName = "showBugReportingWithReportTypeAndOptions";
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).showBugReportingWithReportTypeAndOptions(any(String.class),any(ArrayList.class));
89+
testMethodCall(methodName,argsList);
90+
verify(instabugMock).showBugReportingWithReportTypeAndOptions("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: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.example.InstabugSample;
2+
3+
import org.junit.Test;
4+
5+
public class InstabugFlutterPluginTest {
6+
7+
/**
8+
Each Method in this class is preceded with a comment identifying the type of parameters
9+
that are tested
10+
*/
11+
12+
/**
13+
* (String)
14+
*/
15+
@Test
16+
public void testShowWelcomeMessageWithMode() {
17+
new OnMethodCallTests().testShowWelcomeMessageWithMode();
18+
}
19+
20+
/**
21+
* (String, String)
22+
*/
23+
@Test
24+
public void testIdentifyUserWithEmail() {
25+
new OnMethodCallTests().testIdentifyUserWithEmail();
26+
}
27+
28+
/**
29+
* ()
30+
*/
31+
@Test
32+
public void testLogOut() {
33+
new OnMethodCallTests().testLogOut();
34+
}
35+
36+
/**
37+
* (ArrayList<String>)
38+
*/
39+
@Test
40+
public void testAppendTags() {
41+
new OnMethodCallTests().testAppendTags();
42+
}
43+
44+
/**
45+
* (String, ArrayList<String>)
46+
*/
47+
@Test
48+
public void testShowBugReportingWithReportTypeAndOptions() {
49+
new OnMethodCallTests().testShowBugReportingWithReportTypeAndOptions();
50+
}
51+
52+
/**
53+
* (boolean)
54+
*/
55+
@Test
56+
public void testSetSessionProfilerEnabled() {
57+
new OnMethodCallTests().testSetSessionProfilerEnabled();
58+
}
59+
60+
/**
61+
* (long)
62+
*/
63+
@Test
64+
public void testSetPrimaryColor() {
65+
new OnMethodCallTests().testSetPrimaryColor();
66+
}
67+
68+
/**
69+
* (byte[], String)
70+
*/
71+
@Test
72+
public void testAddFileAttachmentWithData() {
73+
new OnMethodCallTests().testAddFileAttachmentWithData();
74+
}
75+
76+
/**
77+
* (boolean, boolean, boolean, boolean)
78+
*/
79+
@Test
80+
public void testSetEnabledAttachmentTypes() {
81+
new OnMethodCallTests().testSetEnabledAttachmentTypes();
82+
}
83+
84+
/**
85+
* (boolean, ArrayList<String>)
86+
*/
87+
@Test
88+
public void testSetEmailFieldRequiredForFeatureRequests() {
89+
new OnMethodCallTests().testSetEmailFieldRequiredForFeatureRequests();
90+
}
91+
}

0 commit comments

Comments
 (0)