@@ -13,9 +13,11 @@ import android.content.Context
1313import android.content.Intent
1414import android.content.SharedPreferences
1515import android.net.Uri
16+ import android.os.Bundle
1617import com.facebook.FacebookPowerMockTestCase
1718import com.facebook.FacebookSdk
1819import com.facebook.MockSharedPreference
20+ import org.junit.After
1921import org.junit.Before
2022import org.junit.Test
2123import org.mockito.kotlin.any
@@ -31,14 +33,16 @@ import kotlin.test.assertNull
3133class AppLinkManagerTest : FacebookPowerMockTestCase () {
3234
3335 private lateinit var mockApplicationContext: Context
34- private lateinit var mockSharedPreference: SharedPreferences
3536 private lateinit var mockActivity: Activity
3637
38+ companion object {
39+ private val mockSharedPreference: SharedPreferences = MockSharedPreference ()
40+ }
41+
3742 @Before
3843 fun init () {
3944 mockActivity = mock()
4045 mockApplicationContext = mock()
41- mockSharedPreference = MockSharedPreference ()
4246 PowerMockito .mockStatic(FacebookSdk ::class .java)
4347 whenever(FacebookSdk .isInitialized()).thenReturn(true )
4448 whenever(FacebookSdk .getApplicationId()).thenReturn(" 123456789" )
@@ -47,6 +51,11 @@ class AppLinkManagerTest: FacebookPowerMockTestCase() {
4751 .thenReturn(mockSharedPreference)
4852 }
4953
54+ @After
55+ fun after () {
56+ mockSharedPreference.edit().clear()
57+ }
58+
5059 @Test
5160 fun testGetInstance () {
5261 assertNotNull(AppLinkManager .getInstance())
@@ -72,10 +81,23 @@ class AppLinkManagerTest: FacebookPowerMockTestCase() {
7281 }
7382
7483 @Test
75- fun testHandleNonnullCampaignIDs () {
84+ fun testHandleNonnullCampaignIDsFromURL () {
7685 val intent = Intent ()
77- whenever(mockActivity.intent).thenReturn(intent)
7886 intent.setData(Uri .parse(" fb123://test.com?al_applink_data=%7B%22acs_token%22%3A+%22test_token_1234567%22%2C+%22campaign_ids%22%3A+%22test_campaign_1234%22%2C+%22advertiser_id%22%3A+%22test_advertiserid_12345%22%7D" ))
87+ whenever(mockActivity.intent).thenReturn(intent)
88+ AppLinkManager .getInstance()?.handleURL(mockActivity)
89+
90+ assertEquals(" test_campaign_1234" , mockSharedPreference.getString(" campaign_ids" , null ))
91+ }
92+
93+ @Test
94+ fun testHandleNonnullCampaignIDsFromExtra () {
95+ val intent = Intent ()
96+ val bundle = Bundle ()
97+ bundle.putString(" campaign_ids" , " test_campaign_1234" )
98+ intent.putExtra(" al_applink_data" , bundle)
99+ whenever(mockActivity.intent).thenReturn(intent)
100+ intent.setData(Uri .parse(" fb123://test.com" ))
79101 AppLinkManager .getInstance()?.handleURL(mockActivity)
80102
81103 assertEquals(" test_campaign_1234" , mockSharedPreference.getString(" campaign_ids" , null ))
0 commit comments