Skip to content

Commit

Permalink
Add unit tests for Auto Log App Events decision tree
Browse files Browse the repository at this point in the history
Summary: As title

Reviewed By: xta0

Differential Revision: D51380870

fbshipit-source-id: 7424cc4a4d14f7f7c80d5bafdaa3d6dc6f263777
  • Loading branch information
Xinzhu Cai authored and facebook-github-bot committed Nov 20, 2023
1 parent e9096b0 commit ed85622
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,14 @@ internal object UserSettingsManager {
readAutoLogAppEventsSettingFromCache()?.let {
return it
}

loadAutoLogAppEventsSettingFromManifest()?.let {
return it
}

return null
}

@JvmStatic
private fun readAutoLogAppEventsSettingFromCache(): Boolean? {
validateInitialized()
Expand Down Expand Up @@ -383,7 +383,7 @@ internal object UserSettingsManager {
}
return null
}

@JvmStatic
fun setAdvertiserIDCollectionEnabled(flag: Boolean) {
advertiserIDCollectionEnabled.value = flag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -30,26 +34,31 @@ 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()
FacebookSdk.setApplicationId("123456789")
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<String>(), any()))
.thenReturn(MockSharedPreference())
.thenReturn(mockSharedPreference)
PowerMockito.mockStatic(FacebookSdk::class.java)
whenever(FacebookSdk.isInitialized()).thenReturn(true)
whenever(FacebookSdk.getApplicationId()).thenReturn("123456789")
Expand All @@ -68,6 +77,11 @@ class UserSettingsManagerTest : FacebookPowerMockTestCase() {
.thenReturn(mockLogger)
}

@After
fun clean() {
mockSharedPreference.edit().clear().commit()
}

@Test
fun testAutoInitEnabled() {
PowerMockito.mockStatic(UserSettingsManager::class.java)
Expand Down Expand Up @@ -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<String, Boolean>()
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<String, Boolean>()
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<String, Boolean>()
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<String, Boolean>()
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)
Expand Down

0 comments on commit ed85622

Please sign in to comment.