|
| 1 | +package com.unity3d.ads.test.unit; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.Intent; |
| 5 | +import android.os.ConditionVariable; |
| 6 | +import android.os.Handler; |
| 7 | +import android.os.Looper; |
| 8 | +import android.support.test.InstrumentationRegistry; |
| 9 | +import android.support.test.runner.AndroidJUnit4; |
| 10 | +import android.test.ActivityInstrumentationTestCase2; |
| 11 | + |
| 12 | +import com.unity3d.ads.adunit.AdUnitActivity; |
| 13 | +import com.unity3d.ads.log.DeviceLog; |
| 14 | +import com.unity3d.ads.video.VideoPlayerView; |
| 15 | +import com.unity3d.ads.webview.WebView; |
| 16 | +import com.unity3d.ads.webview.WebViewApp; |
| 17 | +import com.unity3d.ads.webview.bridge.CallbackStatus; |
| 18 | +import com.unity3d.ads.webview.bridge.Invocation; |
| 19 | +import android.support.test.rule.ActivityTestRule; |
| 20 | + |
| 21 | +import org.junit.After; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Ignore; |
| 24 | +import org.junit.Rule; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.runner.RunWith; |
| 27 | + |
| 28 | +import java.lang.reflect.Method; |
| 29 | +import java.util.ArrayList; |
| 30 | +import java.util.Arrays; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +@RunWith(AndroidJUnit4.class) |
| 34 | +public class AdUnitActivityTestBaseClass extends ActivityInstrumentationTestCase2<AdUnitActivity> { |
| 35 | + public AdUnitActivityTestBaseClass() { |
| 36 | + super(AdUnitActivity.class); |
| 37 | + } |
| 38 | + |
| 39 | + protected class MyCustomRule<A extends AdUnitActivity> extends ActivityTestRule<A> { |
| 40 | + public MyCustomRule(Class<A> activityClass, boolean initialTouchMode, boolean launchActivity) { |
| 41 | + super(activityClass, initialTouchMode, launchActivity); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + protected void afterActivityFinished() { |
| 46 | + super.afterActivityFinished(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Rule |
| 51 | + public MyCustomRule<AdUnitActivity> testRule = new MyCustomRule<>(AdUnitActivity.class, false, false); |
| 52 | + |
| 53 | + @Before |
| 54 | + public void setUp() throws Exception { |
| 55 | + super.setUp(); |
| 56 | + injectInstrumentation(InstrumentationRegistry.getInstrumentation()); |
| 57 | + } |
| 58 | + |
| 59 | + @After |
| 60 | + public void tearDown() throws Exception { |
| 61 | + super.tearDown(); |
| 62 | + } |
| 63 | + |
| 64 | + protected class MockWebViewApp extends WebViewApp { |
| 65 | + public CallbackStatus CALLBACK_STATUS = null; |
| 66 | + public Enum CALLBACK_ERROR = null; |
| 67 | + public Object[] CALLBACK_PARAMS = null; |
| 68 | + public VideoPlayerView VIDEOPLAYER_VIEW = null; |
| 69 | + public List<Integer> INFO_EVENTS = null; |
| 70 | + public boolean EVENT_TRIGGERED = false; |
| 71 | + public Object[] EVENT_PARAMS = null; |
| 72 | + public int EVENT_COUNT = 0; |
| 73 | + public ConditionVariable CONDITION_VARIABLE = null; |
| 74 | + |
| 75 | + public ArrayList<Enum> EVENT_CATEGORIES = new ArrayList<>(); |
| 76 | + public ArrayList<Enum> EVENTS = new ArrayList<>(); |
| 77 | + |
| 78 | + @Override |
| 79 | + public boolean sendEvent(Enum eventCategory, Enum eventId, Object... params) { |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public boolean invokeCallback(Invocation invocation) { |
| 85 | + for (ArrayList<Object> response : invocation.getResponses()) { |
| 86 | + CallbackStatus status = (CallbackStatus) response.get(0); |
| 87 | + Enum error = (Enum) response.get(1); |
| 88 | + Object[] params = (Object[]) response.get(2); |
| 89 | + |
| 90 | + ArrayList<Object> paramList = new ArrayList<>(); |
| 91 | + paramList.addAll(Arrays.asList(params)); |
| 92 | + paramList.add(1, status.name()); |
| 93 | + |
| 94 | + if (error != null) { |
| 95 | + paramList.add(2, error.name()); |
| 96 | + } |
| 97 | + |
| 98 | + CALLBACK_ERROR = error; |
| 99 | + CALLBACK_PARAMS = params; |
| 100 | + CALLBACK_STATUS = status; |
| 101 | + |
| 102 | + break; |
| 103 | + } |
| 104 | + |
| 105 | + return true; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public boolean invokeMethod(String className, String methodName, Method callback, Object... params) { |
| 110 | + return true; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + protected boolean waitForActivityFinish (final Activity activity) { |
| 115 | + final ConditionVariable cv = new ConditionVariable(); |
| 116 | + new Thread(new Runnable() { |
| 117 | + @Override |
| 118 | + public void run() { |
| 119 | + WebViewApp.setCurrentApp(new MockWebViewApp() { |
| 120 | + private boolean allowEvents = true; |
| 121 | + @Override |
| 122 | + public boolean sendEvent(Enum eventCategory, Enum eventId, Object... params) { |
| 123 | + if (allowEvents) { |
| 124 | + EVENT_CATEGORIES.add(eventCategory); |
| 125 | + EVENTS.add(eventId); |
| 126 | + EVENT_PARAMS = params; |
| 127 | + EVENT_COUNT++; |
| 128 | + |
| 129 | + DeviceLog.debug(eventId.name()); |
| 130 | + |
| 131 | + if ("ON_DESTROY".equals(eventId.name())) { |
| 132 | + allowEvents = false; |
| 133 | + cv.open(); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return true; |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + activity.finish(); |
| 142 | + } |
| 143 | + }).start(); |
| 144 | + return cv.block(30000); |
| 145 | + } |
| 146 | + |
| 147 | + protected Activity waitForActivityStart (final Intent intent) { |
| 148 | + final ConditionVariable cv = new ConditionVariable(); |
| 149 | + WebViewApp.setCurrentApp(new MockWebViewApp() { |
| 150 | + private boolean allowEvents = true; |
| 151 | + private boolean launched = false; |
| 152 | + |
| 153 | + @Override |
| 154 | + public boolean sendEvent(Enum eventCategory, Enum eventId, Object... params) { |
| 155 | + if (allowEvents) { |
| 156 | + |
| 157 | + DeviceLog.debug(eventId.name()); |
| 158 | + |
| 159 | + if ("LAUNCHED".equals(eventId.name())) { |
| 160 | + launched = true; |
| 161 | + } |
| 162 | + else { |
| 163 | + EVENT_CATEGORIES.add(eventCategory); |
| 164 | + EVENTS.add(eventId); |
| 165 | + EVENT_PARAMS = params; |
| 166 | + EVENT_COUNT++; |
| 167 | + } |
| 168 | + |
| 169 | + if ("ON_RESUME".equals(eventId.name())) { |
| 170 | + //allowEvents = false; |
| 171 | + |
| 172 | + if (launched) { |
| 173 | + DeviceLog.debug("Activity launch already came through, opening CV"); |
| 174 | + cv.open(); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + return true; |
| 180 | + } |
| 181 | + }); |
| 182 | + |
| 183 | + final ConditionVariable webViewCV = new ConditionVariable(); |
| 184 | + Handler handler = new Handler(Looper.getMainLooper()); |
| 185 | + handler.post(new Runnable() { |
| 186 | + @Override |
| 187 | + public void run() { |
| 188 | + WebViewApp.getCurrentApp().setWebView(new WebView(InstrumentationRegistry.getTargetContext())); |
| 189 | + webViewCV.open(); |
| 190 | + } |
| 191 | + }); |
| 192 | + |
| 193 | + new Thread(new Runnable() { |
| 194 | + @Override |
| 195 | + public void run() { |
| 196 | + Intent tmpIntent = intent; |
| 197 | + if (tmpIntent == null) tmpIntent = new Intent(); |
| 198 | + testRule.launchActivity(tmpIntent); |
| 199 | + WebViewApp.getCurrentApp().sendEvent(ExtraEvents.LAUNCHED, ExtraEvents.LAUNCHED); |
| 200 | + cv.open(); |
| 201 | + } |
| 202 | + }).start(); |
| 203 | + |
| 204 | + boolean success = cv.block(30000); |
| 205 | + return testRule.getActivity(); |
| 206 | + } |
| 207 | + |
| 208 | + private enum ExtraEvents { LAUNCHED } |
| 209 | + |
| 210 | + protected String printEvents (ArrayList<Enum> events) { |
| 211 | + String retString = ""; |
| 212 | + |
| 213 | + if (events != null) { |
| 214 | + for (Enum event : events) { |
| 215 | + retString += event.name() + ", "; |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + retString = retString.substring(0, retString.length() - 1); |
| 220 | + return retString; |
| 221 | + } |
| 222 | +} |
| 223 | + |
0 commit comments