Skip to content

Commit 08c9d21

Browse files
authored
Refactor IBGPlugin Execute and adds unit tests (#76)
1 parent f370264 commit 08c9d21

File tree

6 files changed

+402
-254
lines changed

6 files changed

+402
-254
lines changed

.circleci/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ jobs:
6969
- run:
7070
name: Run UI Tests
7171
command: cd ../sampleApp/platforms/android; ./gradlew app:connectedAndroidTest
72+
- run:
73+
name: Run unit Tests
74+
command: cd ../sampleApp/platforms/android; ./gradlew test
7275

7376
ios_tests:
7477
macos:

sampleApp/platforms/android/app/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,12 @@ dependencies {
274274
androidTestImplementation 'com.android.support.test:runner:1.0.2'
275275
androidTestImplementation 'com.android.support.test:rules:1.0.2'
276276
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
277+
testImplementation 'org.mockito:mockito-core:1.10.19'
278+
testImplementation 'org.powermock:powermock-api-mockito:1.6.6'
279+
testImplementation 'org.powermock:powermock-module-junit4-rule-agent:1.6.2'
280+
testImplementation 'org.powermock:powermock-module-junit4-rule:1.6.6'
281+
testImplementation 'org.powermock:powermock-module-junit4:1.6.6'
282+
testImplementation 'org.json:json:20140107'
277283
}
278284

279285
def promptForReleaseKeyPassword() {
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,20 @@
11
package com.instabug.cordova.plugin;
22

3-
import android.support.test.espresso.PerformException;
4-
import android.support.test.espresso.UiController;
5-
import android.support.test.espresso.ViewAction;
6-
import android.support.test.espresso.action.GeneralClickAction;
7-
import android.support.test.espresso.action.GeneralLocation;
8-
import android.support.test.espresso.action.Press;
9-
import android.support.test.espresso.action.Tap;
10-
import android.support.test.espresso.util.HumanReadables;
11-
import android.support.test.espresso.util.TreeIterables;
123
import android.support.test.rule.ActivityTestRule;
134
import android.support.test.runner.AndroidJUnit4;
14-
import android.view.InputDevice;
15-
import android.view.MotionEvent;
16-
import android.view.View;
175

186
import com.instabug.library.Instabug;
197
import com.instabug.library.ui.onboarding.WelcomeMessage;
208

21-
import org.hamcrest.Matcher;
229
import org.junit.Rule;
2310
import org.junit.Test;
2411
import org.junit.runner.RunWith;
2512

26-
import java.util.concurrent.TimeoutException;
27-
2813
import io.cordova.hellocordova.MainActivity;
29-
import io.cordova.hellocordova.R;
3014

3115
import static android.support.test.espresso.Espresso.onView;
32-
import static android.support.test.espresso.action.ViewActions.actionWithAssertions;
3316
import static android.support.test.espresso.action.ViewActions.click;
3417
import static android.support.test.espresso.action.ViewActions.replaceText;
35-
import static android.support.test.espresso.core.internal.deps.guava.base.Preconditions.checkNotNull;
36-
import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
37-
import static android.support.test.espresso.matcher.ViewMatchers.withId;
3818
import static android.support.test.espresso.matcher.ViewMatchers.withResourceName;
3919
import static android.support.test.espresso.matcher.ViewMatchers.withText;
4020

@@ -49,20 +29,11 @@ public class InvokeInstabugUITest {
4929
public void ensureInstabugInvocati1on() throws InterruptedException {
5030
Thread.sleep(5000);
5131
onView(withResourceName("instabug_floating_button")).perform(click());
32+
5233
// onView(withText("Report a bug")).perform(click());
5334
// onView(withResourceName("instabug_edit_text_email")).perform(replaceText("[email protected]"));
5435
// onView(withResourceName("instabug_bugreporting_send")).perform(click());
5536
// onView(withResourceName("instabug_success_dialog_container")).perform(click());
5637
}
5738

58-
public static ViewAction click() {
59-
return actionWithAssertions(
60-
new GeneralClickAction(
61-
Tap.SINGLE,
62-
GeneralLocation.VISIBLE_CENTER,
63-
Press.FINGER,
64-
InputDevice.SOURCE_UNKNOWN,
65-
MotionEvent.BUTTON_PRIMARY));
66-
}
67-
6839
}
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
package com.instabug.cordova.plugin;
2+
3+
import android.content.Context;
4+
import android.content.Intent;
5+
import android.graphics.Color;
6+
import android.os.Handler;
7+
import android.os.Looper;
8+
import android.view.View;
9+
import android.webkit.WebChromeClient;
10+
11+
import com.instabug.library.Instabug;
12+
13+
import org.apache.cordova.CallbackContext;
14+
import org.apache.cordova.CordovaInterface;
15+
import org.apache.cordova.CordovaPreferences;
16+
import org.apache.cordova.CordovaResourceApi;
17+
import org.apache.cordova.CordovaWebView;
18+
import org.apache.cordova.CordovaWebViewEngine;
19+
import org.apache.cordova.ICordovaCookieManager;
20+
import org.apache.cordova.PluginEntry;
21+
import org.apache.cordova.PluginManager;
22+
import org.apache.cordova.PluginResult;
23+
import org.json.JSONArray;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.mockito.internal.verification.VerificationModeFactory;
28+
import org.mockito.invocation.InvocationOnMock;
29+
import org.mockito.stubbing.Answer;
30+
import org.powermock.api.mockito.PowerMockito;
31+
import org.powermock.core.classloader.annotations.PrepareForTest;
32+
import org.powermock.modules.junit4.PowerMockRunner;
33+
34+
import java.util.List;
35+
import java.util.Map;
36+
import java.util.concurrent.Executors;
37+
import java.util.concurrent.ScheduledExecutorService;
38+
39+
import static org.mockito.Matchers.any;
40+
import static org.mockito.Matchers.anyLong;
41+
import static org.mockito.Matchers.anyString;
42+
import static org.mockito.Mockito.doAnswer;
43+
import static org.powermock.api.mockito.PowerMockito.mock;
44+
import static org.powermock.api.mockito.PowerMockito.when;
45+
46+
47+
@RunWith(PowerMockRunner.class)
48+
@PrepareForTest({Looper.class, android.os.Handler.class, Instabug.class, IBGPlugin.class, CallbackContext.class, CordovaWebView.class, JSONArray.class, Color.class})
49+
50+
public class IBGPluginTests {
51+
52+
private IBGPlugin cordovaModule = new IBGPlugin();
53+
private CallbackContext callbackContext = new CallbackContext("", new CordovaWebView() {
54+
@Override
55+
public void init(CordovaInterface cordova, List<PluginEntry> pluginEntries, CordovaPreferences preferences) {
56+
57+
58+
}
59+
60+
@Override
61+
public boolean isInitialized() {
62+
return false;
63+
}
64+
65+
@Override
66+
public View getView() {
67+
return null;
68+
}
69+
70+
@Override
71+
public void loadUrlIntoView(String url, boolean recreatePlugins) {
72+
73+
}
74+
75+
@Override
76+
public void stopLoading() {
77+
78+
}
79+
80+
@Override
81+
public boolean canGoBack() {
82+
return false;
83+
}
84+
85+
@Override
86+
public void clearCache() {
87+
88+
}
89+
90+
@Override
91+
public void clearCache(boolean b) {
92+
93+
}
94+
95+
@Override
96+
public void clearHistory() {
97+
98+
}
99+
100+
@Override
101+
public boolean backHistory() {
102+
return false;
103+
}
104+
105+
@Override
106+
public void handlePause(boolean keepRunning) {
107+
108+
}
109+
110+
@Override
111+
public void onNewIntent(Intent intent) {
112+
113+
}
114+
115+
@Override
116+
public void handleResume(boolean keepRunning) {
117+
118+
}
119+
120+
@Override
121+
public void handleStart() {
122+
123+
}
124+
125+
@Override
126+
public void handleStop() {
127+
128+
}
129+
130+
@Override
131+
public void handleDestroy() {
132+
133+
}
134+
135+
@Override
136+
public void sendJavascript(String statememt) {
137+
138+
}
139+
140+
@Override
141+
public void showWebPage(String url, boolean openExternal, boolean clearHistory, Map<String, Object> params) {
142+
143+
}
144+
145+
@Override
146+
public boolean isCustomViewShowing() {
147+
return false;
148+
}
149+
150+
@Override
151+
public void showCustomView(View view, WebChromeClient.CustomViewCallback callback) {
152+
153+
}
154+
155+
@Override
156+
public void hideCustomView() {
157+
158+
}
159+
160+
@Override
161+
public CordovaResourceApi getResourceApi() {
162+
return null;
163+
}
164+
165+
@Override
166+
public void setButtonPlumbedToJs(int keyCode, boolean override) {
167+
168+
}
169+
170+
@Override
171+
public boolean isButtonPlumbedToJs(int keyCode) {
172+
return false;
173+
}
174+
175+
@Override
176+
public void sendPluginResult(PluginResult cr, String callbackId) {
177+
178+
}
179+
180+
@Override
181+
public PluginManager getPluginManager() {
182+
return null;
183+
}
184+
185+
@Override
186+
public CordovaWebViewEngine getEngine() {
187+
return null;
188+
}
189+
190+
@Override
191+
public CordovaPreferences getPreferences() {
192+
return null;
193+
}
194+
195+
@Override
196+
public ICordovaCookieManager getCookieManager() {
197+
return null;
198+
}
199+
200+
@Override
201+
public String getUrl() {
202+
return null;
203+
}
204+
205+
@Override
206+
public Context getContext() {
207+
return null;
208+
}
209+
210+
@Override
211+
public void loadUrl(String url) {
212+
213+
}
214+
215+
@Override
216+
public Object postMessage(String id, Object data) {
217+
return null;
218+
}
219+
});
220+
221+
@Before
222+
public void mockMainThreadHandler() throws Exception {
223+
PowerMockito.mockStatic(Looper.class);
224+
Looper mockMainThreadLooper = mock(Looper.class);
225+
when(Looper.getMainLooper()).thenReturn(mockMainThreadLooper);
226+
Handler mockMainThreadHandler = mock(Handler.class);
227+
Answer<Boolean> handlerPostAnswer = new Answer<Boolean>() {
228+
@Override
229+
public Boolean answer(InvocationOnMock invocation) throws Throwable {
230+
invocation.getArgumentAt(0, Runnable.class).run();
231+
return true;
232+
}
233+
};
234+
doAnswer(handlerPostAnswer).when(mockMainThreadHandler).post(any(Runnable.class));
235+
doAnswer(handlerPostAnswer).when(mockMainThreadHandler).postDelayed(any(Runnable.class), anyLong());
236+
PowerMockito.whenNew(Handler.class).withArguments(mockMainThreadLooper).thenReturn(mockMainThreadHandler);
237+
}
238+
239+
@Test
240+
public void givenArg$setPrimaryColor_whenQuery_thenShouldCallNativeApiWithArg() {
241+
// given
242+
PowerMockito.mockStatic(Instabug.class);
243+
PowerMockito.mockStatic(Color.class);
244+
int color = 3902;
245+
when(Color.parseColor(anyString())).thenReturn(color);
246+
// when
247+
JSONArray args = new JSONArray();
248+
args.put(color+"");
249+
250+
cordovaModule.execute("setPrimaryColor", args, callbackContext);
251+
// then
252+
PowerMockito.verifyStatic(VerificationModeFactory.times(1));
253+
Instabug.setPrimaryColor(color);
254+
}
255+
256+
257+
}

sampleApp/www/js/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ var app = {
3434
},
3535
cordova.plugins.bugReporting.invocationEvents.button,
3636
{
37-
floatingButtonEdge: 'left',
3837
commentRequired: true,
3938
colorTheme: 'light',
4039
enableIntroDialog: false

0 commit comments

Comments
 (0)