diff --git a/facebook/src/test/java/com/facebook/appevents/AppEventTestUtilities.java b/facebook/src/test/java/com/facebook/appevents/AppEventTestUtilities.java index 69414c7a77..8272100bf9 100644 --- a/facebook/src/test/java/com/facebook/appevents/AppEventTestUtilities.java +++ b/facebook/src/test/java/com/facebook/appevents/AppEventTestUtilities.java @@ -22,8 +22,19 @@ import android.os.Bundle; +import junit.framework.Assert; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; import java.util.UUID; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + public class AppEventTestUtilities { public static AppEvent getTestAppEvent() throws Exception { Bundle customParams = new Bundle(); @@ -39,4 +50,31 @@ public static AppEvent getTestAppEvent() throws Exception { appEvent.isChecksumValid(); return appEvent; } + + public static void assertEquals(JSONObject expected, JSONObject actual) throws JSONException { + if (expected == null) { + assertNull(actual); + } + assertNotNull(actual); + + Set set1 = getKeySet(expected); + Set set2 = getKeySet(actual); + Assert.assertEquals(set1, set2); + + for (String k : set1) { + Assert.assertEquals(expected.get(k), actual.get(k)); + } + } + + public static Set getKeySet(JSONObject object){ + Set set = new HashSet<>(); + + Iterator keysItr = object.keys(); + while(keysItr.hasNext()) { + String key = keysItr.next(); + set.add(key); + } + return set; + } + } diff --git a/facebook/src/test/java/com/facebook/appevents/AppEventsLoggerTest.java b/facebook/src/test/java/com/facebook/appevents/AppEventsLoggerTest.java index e7fbadc40b..39389af62d 100644 --- a/facebook/src/test/java/com/facebook/appevents/AppEventsLoggerTest.java +++ b/facebook/src/test/java/com/facebook/appevents/AppEventsLoggerTest.java @@ -20,28 +20,43 @@ package com.facebook.appevents; +import android.os.Bundle; + import com.facebook.FacebookPowerMockTestCase; import com.facebook.FacebookSdk; import com.facebook.appevents.internal.AppEventUtility; import com.facebook.appevents.internal.AppEventsLoggerUtility; +import com.facebook.appevents.internal.Constants; import com.facebook.internal.Utility; +import org.json.JSONException; import org.json.JSONObject; import org.junit.Before; import org.junit.Test; +import org.mockito.Matchers; +import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.robolectric.RuntimeEnvironment; +import java.math.BigDecimal; +import java.util.Currency; +import java.util.Locale; +import java.util.UUID; import java.util.concurrent.Executor; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import static org.powermock.api.mockito.PowerMockito.doReturn; +import static org.powermock.api.mockito.PowerMockito.mock; import static org.powermock.api.mockito.PowerMockito.mockStatic; import static org.powermock.api.mockito.PowerMockito.spy; +import static org.powermock.api.mockito.PowerMockito.verifyNew; import static org.powermock.api.mockito.PowerMockito.when; @PrepareForTest({ + AppEvent.class, + AppEventQueue.class, AppEventUtility.class, AppEventsLogger.class, FacebookSdk.class, @@ -52,14 +67,16 @@ public class AppEventsLoggerTest extends FacebookPowerMockTestCase { private final Executor executor = new FacebookSerialExecutor(); @Before - public void init() throws Exception { - mockStatic(FacebookSdk.class); + public void before() throws Exception { + spy(FacebookSdk.class); when(FacebookSdk.isInitialized()).thenReturn(true); when(FacebookSdk.getApplicationContext()).thenReturn(RuntimeEnvironment.application); // Mock Utility class with empty stub functions, which will be called in // AppEventsLoggerUtility.getJSONObjectForGraphAPICall mockStatic(Utility.class); + // Stub empty implementations to AppEventQueue to not really flush events + mockStatic(AppEventQueue.class); // Disable AppEventUtility.isMainThread since executor now runs in main thread spy(AppEventUtility.class); @@ -70,7 +87,19 @@ public void init() throws Exception { } @Test - public void testSetAndClearUserID() throws Exception { + public void testSetAndClearUserData() throws JSONException { + AppEventsLogger.setUserData( + "em@gmail.com", "fn", "ln", "123", null, null, null, null, null, null); + JSONObject actualUserData = new JSONObject(AppEventsLogger.getUserData()); + JSONObject expectedUserData = new JSONObject("{\"ln\":\"e545c2c24e6463d7c4fe3829940627b226c0b9be7a8c7dbe964768da48f1ab9d\",\"ph\":\"a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3\",\"em\":\"5f341666fb1ce60d716e4afc302c8658f09412290aa2ca8bc623861f452f9d33\",\"fn\":\"0f1e18bb4143dc4be22e61ea4deb0491c2bf7018c6504ad631038aed5ca4a0ca\"}"); + AppEventTestUtilities.assertEquals(expectedUserData, actualUserData); + + AppEventsLogger.clearUserData(); + assertTrue(AppEventsLogger.getUserData().isEmpty()); + } + + @Test + public void testSetAndClearUserID() { String userID = "12345678"; AppEventsLogger.setUserID(userID); assertEquals(AppEventsLogger.getUserID(), userID); @@ -82,7 +111,7 @@ public void testSetAndClearUserID() throws Exception { public void testUserIDAddedToAppEvent() throws Exception { String userID = "12345678"; AppEventsLogger.setUserID(userID); - JSONObject jsonObject =AppEventsLoggerUtility.getJSONObjectForGraphAPICall( + JSONObject jsonObject = AppEventsLoggerUtility.getJSONObjectForGraphAPICall( AppEventsLoggerUtility.GraphAPIActivityType.MOBILE_INSTALL_EVENT, null, "123", @@ -90,4 +119,89 @@ public void testUserIDAddedToAppEvent() throws Exception { FacebookSdk.getApplicationContext()); assertEquals(jsonObject.getString("app_user_id"), userID); } + + @Test + public void testLogEvent() throws Exception { + AppEvent mockEvent = mock(AppEvent.class); + doReturn(true).when(mockEvent).getIsImplicit(); + PowerMockito.whenNew(AppEvent.class).withAnyArguments().thenReturn(mockEvent); + AppEventsLogger.newLogger(FacebookSdk.getApplicationContext()).logEvent("fb_mock_event"); + verifyNew(AppEvent.class).withArguments( + Matchers.anyString(), + Matchers.eq("fb_mock_event"), + Matchers.anyDouble(), + Matchers.any(Bundle.class), + Matchers.anyBoolean(), + Matchers.any(UUID.class)); + } + + @Test + public void testLogPurchase() throws Exception { + AppEvent mockEvent = mock(AppEvent.class); + doReturn(true).when(mockEvent).getIsImplicit(); + PowerMockito.whenNew(AppEvent.class).withAnyArguments().thenReturn(mockEvent); + AppEventsLogger.newLogger(FacebookSdk.getApplicationContext()).logPurchase( + new BigDecimal(1.0), Currency.getInstance(Locale.US)); + Bundle parameters = new Bundle(); + parameters.putString( + AppEventsConstants.EVENT_PARAM_CURRENCY, + Currency.getInstance(Locale.US).getCurrencyCode()); + verifyNew(AppEvent.class).withArguments( + Matchers.anyString(), + Matchers.eq(AppEventsConstants.EVENT_NAME_PURCHASED), + Matchers.eq(1.0), + Matchers.eq(parameters), + Matchers.anyBoolean(), + Matchers.any(UUID.class)); + } + + @Test + public void testLogProductItem() throws Exception { + AppEvent mockEvent = mock(AppEvent.class); + doReturn(true).when(mockEvent).getIsImplicit(); + PowerMockito.whenNew(AppEvent.class).withAnyArguments().thenReturn(mockEvent); + AppEventsLogger.newLogger(FacebookSdk.getApplicationContext()).logProductItem( + "F40CEE4E-471E-45DB-8541-1526043F4B21", + AppEventsLogger.ProductAvailability.IN_STOCK, + AppEventsLogger.ProductCondition.NEW, + "description", + "https://www.sample.com", + "https://www.sample.com", + "title", + new BigDecimal(1.0), + Currency.getInstance(Locale.US), + "BLUE MOUNTAIN", + "BLUE MOUNTAIN", + "PHILZ", + null); + Bundle parameters = new Bundle(); + parameters.putString( + Constants.EVENT_PARAM_PRODUCT_ITEM_ID, + "F40CEE4E-471E-45DB-8541-1526043F4B21"); + parameters.putString( + Constants.EVENT_PARAM_PRODUCT_AVAILABILITY, + AppEventsLogger.ProductAvailability.IN_STOCK.name()); + parameters.putString( + Constants.EVENT_PARAM_PRODUCT_CONDITION, + AppEventsLogger.ProductCondition.NEW.name()); + parameters.putString(Constants.EVENT_PARAM_PRODUCT_DESCRIPTION, "description"); + parameters.putString(Constants.EVENT_PARAM_PRODUCT_IMAGE_LINK, "https://www.sample.com"); + parameters.putString(Constants.EVENT_PARAM_PRODUCT_LINK, "https://www.sample.com"); + parameters.putString(Constants.EVENT_PARAM_PRODUCT_TITLE, "title"); + parameters.putString(Constants.EVENT_PARAM_PRODUCT_PRICE_AMOUNT, + (new BigDecimal(1.0)).setScale(3, BigDecimal.ROUND_HALF_UP).toString()); + parameters.putString( + Constants.EVENT_PARAM_PRODUCT_PRICE_CURRENCY, + Currency.getInstance(Locale.US).getCurrencyCode()); + parameters.putString(Constants.EVENT_PARAM_PRODUCT_GTIN, "BLUE MOUNTAIN"); + parameters.putString(Constants.EVENT_PARAM_PRODUCT_MPN, "BLUE MOUNTAIN"); + parameters.putString(Constants.EVENT_PARAM_PRODUCT_BRAND, "PHILZ"); + + verifyNew(AppEvent.class).withArguments( + Matchers.anyString(), + Matchers.eq(AppEventsConstants.EVENT_NAME_PRODUCT_CATALOG_UPDATE), + Matchers.anyDouble(), Matchers.eq(parameters), + Matchers.anyBoolean(), + Matchers.any(UUID.class)); + } }