|
34 | 34 | import android.accounts.AccountManager; |
35 | 35 | import android.content.Context; |
36 | 36 | import android.content.IntentFilter; |
| 37 | +import android.content.SharedPreferences; |
37 | 38 |
|
38 | 39 | import androidx.core.content.ContextCompat; |
39 | 40 | import androidx.test.ext.junit.runners.AndroidJUnit4; |
|
42 | 43 |
|
43 | 44 | import com.salesforce.androidsdk.app.SalesforceSDKManager; |
44 | 45 | import com.salesforce.androidsdk.auth.OAuth2; |
| 46 | +import com.salesforce.androidsdk.security.SalesforceKeyGenerator; |
45 | 47 | import com.salesforce.androidsdk.util.LogoutCompleteReceiver; |
46 | 48 |
|
47 | 49 | import org.jetbrains.annotations.NotNull; |
@@ -201,6 +203,40 @@ public void testSignoutBackgroundUser() { |
201 | 203 | Assert.assertEquals(TEST_USERNAME, logoutCompleteReceiver.getLastUserAccountReceived().getUsername()); |
202 | 204 | } |
203 | 205 |
|
| 206 | + /** |
| 207 | + * Test that shared preferences are cleared when the last user logs out. |
| 208 | + * This verifies the fix for W-17366971 - ensuring that identifier.xml |
| 209 | + * and current_user_info files are deleted on logout. |
| 210 | + */ |
| 211 | + @Test |
| 212 | + public void testSharedPreferencesCleanupOnLastUserLogout() { |
| 213 | + final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); |
| 214 | + |
| 215 | + // Create a user and force creation of identifier.xml by accessing the encryption key |
| 216 | + createTestAccountInAccountManager(userAccMgr); |
| 217 | + String encryptionKey = SalesforceKeyGenerator.getEncryptionKey("test_key"); |
| 218 | + Assert.assertNotNull("Encryption key should be created", encryptionKey); |
| 219 | + |
| 220 | + // Verify that identifier.xml shared preferences exists with data |
| 221 | + SharedPreferences identifierPrefs = context.getSharedPreferences("identifier.xml", 0); |
| 222 | + Assert.assertFalse("identifier.xml should have data", identifierPrefs.getAll().isEmpty()); |
| 223 | + |
| 224 | + // Verify that current_user_info shared preferences exists with data |
| 225 | + SharedPreferences currentUserPrefs = context.getSharedPreferences("current_user_info", 0); |
| 226 | + Assert.assertFalse("current_user_info should have data", currentUserPrefs.getAll().isEmpty()); |
| 227 | + |
| 228 | + // Logout the last user |
| 229 | + userAccMgr.signoutCurrentUser(null, false, OAuth2.LogoutReason.USER_LOGOUT); |
| 230 | + |
| 231 | + // Verify that identifier.xml shared preferences is cleared |
| 232 | + identifierPrefs = context.getSharedPreferences("identifier.xml", 0); |
| 233 | + Assert.assertTrue("identifier.xml should be empty after logout", identifierPrefs.getAll().isEmpty()); |
| 234 | + |
| 235 | + // Verify that current_user_info shared preferences is cleared |
| 236 | + currentUserPrefs = context.getSharedPreferences("current_user_info", 0); |
| 237 | + Assert.assertTrue("current_user_info should be empty after logout", currentUserPrefs.getAll().isEmpty()); |
| 238 | + } |
| 239 | + |
204 | 240 | /** |
205 | 241 | * Removes any existing accounts. |
206 | 242 | */ |
|
0 commit comments