-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Logevent unit tests to AppEventsLoggerTest
Summary: add unit tests for different logging functions Reviewed By: YOUDAN Differential Revision: D14497233 fbshipit-source-id: 315aefd3209dcd574a43aa8c713c42d6778dda01
- Loading branch information
1 parent
e4e6000
commit 04716f3
Showing
2 changed files
with
156 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
"[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); | ||
|
@@ -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)); | ||
} | ||
} |