diff --git a/facebook-core/src/main/java/com/facebook/UserSettingsManager.kt b/facebook-core/src/main/java/com/facebook/UserSettingsManager.kt index 93fd849fe3..44334348d1 100644 --- a/facebook-core/src/main/java/com/facebook/UserSettingsManager.kt +++ b/facebook-core/src/main/java/com/facebook/UserSettingsManager.kt @@ -347,14 +347,14 @@ internal object UserSettingsManager { readAutoLogAppEventsSettingFromCache()?.let { return it } - + loadAutoLogAppEventsSettingFromManifest()?.let { return it } - + return null } - + @JvmStatic private fun readAutoLogAppEventsSettingFromCache(): Boolean? { validateInitialized() @@ -383,7 +383,7 @@ internal object UserSettingsManager { } return null } - + @JvmStatic fun setAdvertiserIDCollectionEnabled(flag: Boolean) { advertiserIDCollectionEnabled.value = flag diff --git a/facebook-core/src/test/kotlin/com/facebook/UserSettingsManagerTest.kt b/facebook-core/src/test/kotlin/com/facebook/UserSettingsManagerTest.kt index fb0e6a6441..589e73895a 100644 --- a/facebook-core/src/test/kotlin/com/facebook/UserSettingsManagerTest.kt +++ b/facebook-core/src/test/kotlin/com/facebook/UserSettingsManagerTest.kt @@ -9,6 +9,7 @@ package com.facebook import android.content.Context +import android.content.SharedPreferences import android.content.pm.ApplicationInfo import android.content.pm.PackageManager import android.os.Bundle @@ -20,7 +21,10 @@ import com.facebook.UserSettingsManager.setAdvertiserIDCollectionEnabled import com.facebook.UserSettingsManager.setAutoInitEnabled import com.facebook.UserSettingsManager.setAutoLogAppEventsEnabled import com.facebook.appevents.InternalAppEventsLogger +import com.facebook.internal.FetchedAppSettingsManager import org.assertj.core.api.Assertions.assertThat +import org.json.JSONObject +import org.junit.After import org.junit.Before import org.junit.Test import org.mockito.kotlin.any @@ -30,13 +34,17 @@ import org.mockito.kotlin.verify import org.mockito.kotlin.whenever import org.powermock.api.mockito.PowerMockito import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.reflect.Whitebox +import java.util.concurrent.atomic.AtomicBoolean -@PrepareForTest(UserSettingsManager::class, FacebookSdk::class) +@PrepareForTest(UserSettingsManager::class, FacebookSdk::class, FetchedAppSettingsManager::class) class UserSettingsManagerTest : FacebookPowerMockTestCase() { private lateinit var mockApplicationContext: Context private lateinit var mockPackageManager: PackageManager private lateinit var mockApplicationInfo: ApplicationInfo private lateinit var mockLogger: InternalAppEventsLogger + private lateinit var mockSharedPreference: SharedPreferences + @Before override fun setup() { super.setup() @@ -44,12 +52,13 @@ class UserSettingsManagerTest : FacebookPowerMockTestCase() { mockPackageManager = mock() mockApplicationContext = mock() mockApplicationInfo = mock() + mockSharedPreference = MockSharedPreference() whenever(mockApplicationContext.packageManager).thenReturn(mockPackageManager) whenever(mockApplicationContext.applicationContext).thenReturn(mockApplicationContext) whenever(mockApplicationContext.packageName).thenReturn("com.facebook.test") whenever(mockPackageManager.getApplicationInfo(any(), any())).thenReturn(mockApplicationInfo) whenever(mockApplicationContext.getSharedPreferences(any(), any())) - .thenReturn(MockSharedPreference()) + .thenReturn(mockSharedPreference) PowerMockito.mockStatic(FacebookSdk::class.java) whenever(FacebookSdk.isInitialized()).thenReturn(true) whenever(FacebookSdk.getApplicationId()).thenReturn("123456789") @@ -68,6 +77,11 @@ class UserSettingsManagerTest : FacebookPowerMockTestCase() { .thenReturn(mockLogger) } + @After + fun clean() { + mockSharedPreference.edit().clear().commit() + } + @Test fun testAutoInitEnabled() { PowerMockito.mockStatic(UserSettingsManager::class.java) @@ -111,6 +125,75 @@ class UserSettingsManagerTest : FacebookPowerMockTestCase() { assertThat(setAutoLogAppEventsEnabledbledValue).isFalse } + @Test + fun `test AutoLogEnabled without values from server`() { + PowerMockito.mockStatic(FetchedAppSettingsManager::class.java) + whenever(FetchedAppSettingsManager.getCachedMigratedAutoLogValuesInAppSettings()).thenReturn(null) + + val enable = FacebookSdk.getAutoLogAppEventsEnabled() + assertThat(enable).isTrue + } + + @Test + fun `test AutoLogEnabled return the enabled value fetched from server`() { + PowerMockito.mockStatic(FetchedAppSettingsManager::class.java) + val mockedAutoLogAppEventsValues = HashMap() + mockedAutoLogAppEventsValues[FetchedAppSettingsManager.AUTO_LOG_APP_EVENTS_DEFAULT_FIELD] = false + mockedAutoLogAppEventsValues[FetchedAppSettingsManager.AUTO_LOG_APP_EVENT_ENABLED_FIELD] = false + whenever(FetchedAppSettingsManager.getCachedMigratedAutoLogValuesInAppSettings()).thenReturn(mockedAutoLogAppEventsValues) + + setAutoLogAppEventsEnabled(true) + val enable = FacebookSdk.getAutoLogAppEventsEnabled() + assertThat(enable).isFalse + } + + @Test + fun `test AutoLogEnabled return the value in cache`() { + PowerMockito.mockStatic(FetchedAppSettingsManager::class.java) + val mockedAutoLogAppEventsValues = HashMap() + mockedAutoLogAppEventsValues[FetchedAppSettingsManager.AUTO_LOG_APP_EVENTS_DEFAULT_FIELD] = false + whenever(FetchedAppSettingsManager.getCachedMigratedAutoLogValuesInAppSettings()).thenReturn(mockedAutoLogAppEventsValues) + + val jsonObject = JSONObject() + jsonObject.put("value", true) + mockSharedPreference.edit().putString(FacebookSdk.AUTO_LOG_APP_EVENTS_ENABLED_PROPERTY, jsonObject.toString()).apply() + Whitebox.setInternalState(UserSettingsManager::class.java,"isInitialized", AtomicBoolean(true)) + Whitebox.setInternalState(UserSettingsManager::class.java,"userSettingPref", mockSharedPreference) + + val enable = FacebookSdk.getAutoLogAppEventsEnabled() + assertThat(enable).isTrue + } + + @Test + fun `test AutoLogEnabled return the value set in manifest file`() { + PowerMockito.mockStatic(FetchedAppSettingsManager::class.java) + val mockedAutoLogAppEventsValues = HashMap() + mockedAutoLogAppEventsValues[FetchedAppSettingsManager.AUTO_LOG_APP_EVENTS_DEFAULT_FIELD] = false + whenever(FetchedAppSettingsManager.getCachedMigratedAutoLogValuesInAppSettings()).thenReturn(mockedAutoLogAppEventsValues) + Whitebox.setInternalState(UserSettingsManager::class.java,"isInitialized", AtomicBoolean(true)) + Whitebox.setInternalState(UserSettingsManager::class.java,"userSettingPref", mockSharedPreference) + + val metaData = Bundle() + metaData.putBoolean(FacebookSdk.AUTO_LOG_APP_EVENTS_ENABLED_PROPERTY, true) + mockApplicationInfo.metaData = metaData + + val enable = FacebookSdk.getAutoLogAppEventsEnabled() + assertThat(enable).isTrue + } + + @Test + fun `test AutoLogEnabled return the default value fetched from server`() { + PowerMockito.mockStatic(FetchedAppSettingsManager::class.java) + val mockedAutoLogAppEventsValues = HashMap() + mockedAutoLogAppEventsValues[FetchedAppSettingsManager.AUTO_LOG_APP_EVENTS_DEFAULT_FIELD] = false + whenever(FetchedAppSettingsManager.getCachedMigratedAutoLogValuesInAppSettings()).thenReturn(mockedAutoLogAppEventsValues) + Whitebox.setInternalState(UserSettingsManager::class.java,"isInitialized", AtomicBoolean(true)) + Whitebox.setInternalState(UserSettingsManager::class.java,"userSettingPref", mockSharedPreference) + + val enable = FacebookSdk.getAutoLogAppEventsEnabled() + assertThat(enable).isFalse + } + @Test fun testAdvertiserIDCollectionEnabled() { PowerMockito.mockStatic(UserSettingsManager::class.java)