|
| 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 | +} |
0 commit comments