|
| 1 | +package io.branch.referral; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.support.test.runner.AndroidJUnit4; |
| 5 | +import android.util.Log; |
| 6 | + |
| 7 | +import org.json.JSONObject; |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.Test; |
| 10 | +import org.junit.runner.RunWith; |
| 11 | + |
| 12 | +import java.lang.reflect.Field; |
| 13 | + |
| 14 | +import io.branch.referral.util.AdType; |
| 15 | +import io.branch.referral.util.BRANCH_STANDARD_EVENT; |
| 16 | +import io.branch.referral.util.BranchEvent; |
| 17 | +import io.branch.referral.util.CurrencyType; |
| 18 | + |
| 19 | +/** |
| 20 | + * BranchEvent class tests. |
| 21 | + */ |
| 22 | +@RunWith(AndroidJUnit4.class) |
| 23 | +public class BranchEventTest extends BranchTest { |
| 24 | + private static final String TAG = "Branch::EventTest"; |
| 25 | + private static final String TEST_KEY = "key_live_testkey"; |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testStandardEvent() throws Throwable { |
| 29 | + BRANCH_STANDARD_EVENT eventType = BRANCH_STANDARD_EVENT.PURCHASE; |
| 30 | + Assert.assertEquals("PURCHASE", eventType.getName()); |
| 31 | + |
| 32 | + BranchEvent branchEvent = new BranchEvent(eventType); |
| 33 | + Assert.assertTrue(isStandardEvent(branchEvent)); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testCustomEvent() throws Throwable { |
| 38 | + BranchEvent branchEvent = new BranchEvent("CustomEvent"); |
| 39 | + Assert.assertFalse(isStandardEvent(branchEvent)); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testCustomEventWithStandardName() throws Throwable { |
| 44 | + // We assert that creating an event using a String *will be considered a custom event* |
| 45 | + BRANCH_STANDARD_EVENT eventType = BRANCH_STANDARD_EVENT.PURCHASE; |
| 46 | + BranchEvent branchEvent = new BranchEvent(eventType.getName()); |
| 47 | + Assert.assertTrue(isStandardEvent(branchEvent)); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testAllStandardEvents() throws Throwable { |
| 52 | + for (BRANCH_STANDARD_EVENT eventType : BRANCH_STANDARD_EVENT.values()) { |
| 53 | + BranchEvent branchEvent = new BranchEvent(eventType); |
| 54 | + Assert.assertTrue(isStandardEvent(branchEvent)); |
| 55 | + |
| 56 | + // We assert that creating an event using a String *will be considered a standard event* |
| 57 | + String eventName = eventType.getName(); |
| 58 | + BranchEvent branchEventStr = new BranchEvent(eventName); |
| 59 | + Assert.assertTrue(isStandardEvent(branchEventStr)); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testAddAllEventExtras() { |
| 65 | + BranchEvent event = new BranchEvent("CustomEvent"); |
| 66 | + |
| 67 | + event.setTransactionID("123"); |
| 68 | + for (AdType adType : AdType.values()) { |
| 69 | + event.setAdType(adType); |
| 70 | + } |
| 71 | + event.setAffiliation("CustomAffiliation"); |
| 72 | + event.setCoupon("test coupon"); |
| 73 | + event.setCurrency(CurrencyType.BZD); |
| 74 | + event.setDescription("Test Event"); |
| 75 | + event.setRevenue(123.456); |
| 76 | + event.setSearchQuery("Love"); |
| 77 | + event.setShipping(0.001); |
| 78 | + event.setTax(10); |
| 79 | + |
| 80 | + event.addCustomDataProperty("test", "test value"); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testLogEvent() throws Throwable { |
| 85 | + Branch.getInstance(getTestContext(), TEST_KEY); |
| 86 | + |
| 87 | + BRANCH_STANDARD_EVENT eventType = BRANCH_STANDARD_EVENT.PURCHASE; |
| 88 | + BranchEvent branchEvent = new BranchEvent(eventType); |
| 89 | + |
| 90 | + branchEvent.logEvent(getTestContext()); |
| 91 | + |
| 92 | + // There is some async stuff going on. Let's wait until that is complete. |
| 93 | + Thread.sleep(2000); |
| 94 | + |
| 95 | + // Verify that the server queue has two events |
| 96 | + // 1. Install Event |
| 97 | + // 2. This Event |
| 98 | + ServerRequestQueue queue = ServerRequestQueue.getInstance(getTestContext()); |
| 99 | + Assert.assertEquals(2, queue.getSize()); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testLogEvent_queue() throws Throwable { |
| 104 | + Branch.getInstance(getTestContext(), TEST_KEY); |
| 105 | + initQueue(getTestContext()); |
| 106 | + |
| 107 | + BRANCH_STANDARD_EVENT eventType = BRANCH_STANDARD_EVENT.PURCHASE; |
| 108 | + BranchEvent branchEvent = new BranchEvent(eventType); |
| 109 | + |
| 110 | + ServerRequest serverRequest = logEvent(getTestContext(), branchEvent); |
| 111 | + JSONObject jsonObject = serverRequest.getGetParams(); |
| 112 | + |
| 113 | + Assert.assertEquals(BRANCH_STANDARD_EVENT.PURCHASE.getName(), jsonObject.optString("name")); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testAdType() throws Throwable { |
| 118 | + Branch.getInstance(getTestContext(), TEST_KEY); |
| 119 | + initQueue(getTestContext()); |
| 120 | + |
| 121 | + BranchEvent branchEvent = new BranchEvent(BRANCH_STANDARD_EVENT.VIEW_AD); |
| 122 | + branchEvent.setAdType(AdType.BANNER); |
| 123 | + |
| 124 | + ServerRequest serverRequest = logEvent(getTestContext(), branchEvent); |
| 125 | + JSONObject jsonObject = serverRequest.getGetParams(); |
| 126 | + |
| 127 | + // Verify that the ad_type was set correctly. |
| 128 | + JSONObject eventData = jsonObject.getJSONObject(Defines.Jsonkey.EventData.getKey()); |
| 129 | + String adType = eventData.getString(Defines.Jsonkey.AdType.getKey()); |
| 130 | + |
| 131 | + Assert.assertEquals(adType, AdType.BANNER.getName()); |
| 132 | + } |
| 133 | + |
| 134 | + // Dig out the variable for isStandardEvent from the BranchEvent object. |
| 135 | + private boolean isStandardEvent(BranchEvent event) throws Throwable { |
| 136 | + // Use Reflection to find if it is considered a "Standard Event" |
| 137 | + Field f = event.getClass().getDeclaredField("isStandardEvent"); //NoSuchFieldException |
| 138 | + f.setAccessible(true); |
| 139 | + return (boolean) f.get(event); //IllegalAccessException |
| 140 | + } |
| 141 | + |
| 142 | + // This is an attempt to initialize the queue by adding an event and waiting for it to appear. |
| 143 | + // Once it appears, we remove it. |
| 144 | + // Note that adding an event to the queue the first time generates an install event as a side effect. |
| 145 | + void initQueue(Context context) throws Throwable { |
| 146 | + final String EVENT_NAME = "XXXyyyXXX"; |
| 147 | + Branch.getInstance(getTestContext(), TEST_KEY); |
| 148 | + ServerRequestQueue queue = ServerRequestQueue.getInstance(context); |
| 149 | + |
| 150 | + // Queue should be empty when we initialize |
| 151 | + Assert.assertEquals(0, queue.getSize()); |
| 152 | + |
| 153 | + // Create a test event and add it to the queue |
| 154 | + BranchEvent testEvent = new BranchEvent(EVENT_NAME); |
| 155 | + Assert.assertNotNull(addEventToQueueAndWait(context, testEvent)); |
| 156 | + |
| 157 | + // Remove the event from the queue. It should be the last one. |
| 158 | + if (queue.getSize() > 0) { |
| 159 | + int index = queue.getSize() - 1; |
| 160 | + queue.removeAt(index); |
| 161 | + } |
| 162 | + |
| 163 | + // We expect that the install event is the only event still on the queue |
| 164 | + if (queue.getSize() > 1) { |
| 165 | + for (int i = 0; i < queue.getSize(); i++) { |
| 166 | + ServerRequest request = queue.peekAt(i); |
| 167 | + Log.d(TAG, "Request " + i + ": " + request.getGetParams().toString()); |
| 168 | + } |
| 169 | + } |
| 170 | + Assert.assertEquals(1, queue.getSize()); |
| 171 | + } |
| 172 | + |
| 173 | + ServerRequest addEventToQueueAndWait(Context context, BranchEvent event) throws Throwable { |
| 174 | + event.logEvent(context); |
| 175 | + return findEventOnQueue(context, "name", event.getEventName()); |
| 176 | + } |
| 177 | + |
| 178 | + // Obtain the ServerRequest that is on the queue that matches the BranchEvent to be logged. |
| 179 | + ServerRequest logEvent(Context context, BranchEvent event) throws Throwable { |
| 180 | + ServerRequestQueue queue = ServerRequestQueue.getInstance(context); |
| 181 | + int queueSizeIn = queue.getSize(); |
| 182 | + |
| 183 | + ServerRequest queuedEvent = addEventToQueueAndWait(context, event); |
| 184 | + Assert.assertNotNull(queuedEvent); |
| 185 | + |
| 186 | + int queueSizeOut = queue.getSize(); |
| 187 | + Assert.assertEquals(queueSizeOut, (queueSizeIn + 1)); |
| 188 | + |
| 189 | + return doFinalUpdate(queuedEvent); |
| 190 | + } |
| 191 | + |
| 192 | + ServerRequest findEventOnQueue(Context context, String key, String eventName) throws Throwable { |
| 193 | + ServerRequestQueue queue = ServerRequestQueue.getInstance(context); |
| 194 | + |
| 195 | + int wait_remaining = 2000; |
| 196 | + final int interval = 50; |
| 197 | + |
| 198 | + while (wait_remaining > 0) { |
| 199 | + Thread.sleep(interval); |
| 200 | + if (queue.getSize() > 0) { |
| 201 | + int index = queue.getSize() - 1; |
| 202 | + |
| 203 | + ServerRequest request = queue.peekAt(index); |
| 204 | + JSONObject jsonObject = request.getGetParams(); |
| 205 | + |
| 206 | + String name = jsonObject.optString(key); |
| 207 | + if (name.equals(eventName)) { |
| 208 | + // Found it. |
| 209 | + return request; |
| 210 | + } |
| 211 | + } |
| 212 | + wait_remaining -= interval; |
| 213 | + } |
| 214 | + |
| 215 | + return null; |
| 216 | + } |
| 217 | + |
| 218 | + ServerRequest getLastEventOnQueue(Context context, int minimumQueueSize) { |
| 219 | + ServerRequestQueue queue = ServerRequestQueue.getInstance(context); |
| 220 | + |
| 221 | + int size = queue.getSize(); |
| 222 | + if (size >= minimumQueueSize) { |
| 223 | + return queue.peekAt(size - 1); |
| 224 | + } |
| 225 | + |
| 226 | + return null; |
| 227 | + } |
| 228 | + |
| 229 | + // Black Magic here. In order to really find out what is going to be in this request, |
| 230 | + // we need to pretend like we are updating it right before sending it out. We obviously |
| 231 | + // don't *actually* want to send this, we just want to verify that parameters are fully set. |
| 232 | + ServerRequest doFinalUpdate(ServerRequest request) { |
| 233 | + request.doFinalUpdateOnBackgroundThread(); |
| 234 | + return request; |
| 235 | + } |
| 236 | + |
| 237 | + void DebugLogQueue(Context context) { |
| 238 | + ServerRequestQueue queue = ServerRequestQueue.getInstance(context); |
| 239 | + |
| 240 | + for (int i = 0; i < queue.getSize(); i++) { |
| 241 | + ServerRequest request = queue.peekAt(i); |
| 242 | + doFinalUpdate(request); |
| 243 | + |
| 244 | + JSONObject jsonObject = request.getGetParams(); |
| 245 | + Log.d(TAG, "QUEUE " + i + ": " + jsonObject.toString()); |
| 246 | + } |
| 247 | + |
| 248 | + } |
| 249 | +} |
| 250 | + |
0 commit comments