Skip to content

Commit

Permalink
Add Logevent unit tests to AppEventsLoggerTest
Browse files Browse the repository at this point in the history
Summary: add unit tests for different logging functions

Reviewed By: YOUDAN

Differential Revision: D14497233

fbshipit-source-id: 315aefd3209dcd574a43aa8c713c42d6778dda01
  • Loading branch information
KylinChang authored and facebook-github-bot committed Mar 18, 2019
1 parent e4e6000 commit 04716f3
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<String> set1 = getKeySet(expected);
Set<String> set2 = getKeySet(actual);
Assert.assertEquals(set1, set2);

for (String k : set1) {
Assert.assertEquals(expected.get(k), actual.get(k));
}
}

public static Set<String> getKeySet(JSONObject object){
Set<String> set = new HashSet<>();

Iterator<String> keysItr = object.keys();
while(keysItr.hasNext()) {
String key = keysItr.next();
set.add(key);
}
return set;
}

}
122 changes: 118 additions & 4 deletions facebook/src/test/java/com/facebook/appevents/AppEventsLoggerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -70,7 +87,19 @@ public void init() throws Exception {
}

@Test
public void testSetAndClearUserID() throws Exception {
public void testSetAndClearUserData() throws JSONException {
AppEventsLogger.setUserData(
"[email protected]", "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);
Expand All @@ -82,12 +111,97 @@ 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",
true,
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));
}
}

0 comments on commit 04716f3

Please sign in to comment.