2626 */
2727package com .salesforce .androidsdk .accounts ;
2828
29+ import static androidx .core .content .ContextCompat .RECEIVER_NOT_EXPORTED ;
30+ import static com .salesforce .androidsdk .accounts .UserAccountTest .TEST_USERNAME ;
2931import static com .salesforce .androidsdk .accounts .UserAccountTest .checkSameUserAccount ;
3032
3133import android .accounts .Account ;
3234import android .accounts .AccountManager ;
3335import android .content .Context ;
36+ import android .content .IntentFilter ;
3437
38+ import androidx .core .content .ContextCompat ;
3539import androidx .test .ext .junit .runners .AndroidJUnit4 ;
3640import androidx .test .filters .SmallTest ;
3741import androidx .test .platform .app .InstrumentationRegistry ;
3842
43+ import com .salesforce .androidsdk .app .SalesforceSDKManager ;
3944import com .salesforce .androidsdk .auth .OAuth2 ;
45+ import com .salesforce .androidsdk .util .LogoutCompleteReceiver ;
4046
47+ import org .jetbrains .annotations .NotNull ;
48+ import org .jetbrains .annotations .Nullable ;
4149import org .junit .After ;
4250import org .junit .Assert ;
4351import org .junit .Before ;
4452import org .junit .Test ;
4553import org .junit .runner .RunWith ;
4654
4755import java .util .List ;
56+ import java .util .concurrent .Semaphore ;
4857
4958/**
5059 * Tests for UserAccountManager.
@@ -59,20 +68,27 @@ public class UserAccountManagerTest {
5968
6069 private UserAccountManager userAccMgr ;
6170 private AccountManager accMgr ;
71+ private FakeLogoutCompleteReceiver logoutCompleteReceiver ;
6272
6373 @ Before
6474 public void setUp () throws Exception {
6575 final Context targetContext = InstrumentationRegistry .getInstrumentation ().getTargetContext ();
6676 accMgr = AccountManager .get (targetContext );
6777 userAccMgr = UserAccountManager .getInstance ();
6878 Assert .assertNull ("There should be no authenticated users" , userAccMgr .getAuthenticatedUsers ());
79+ logoutCompleteReceiver = new FakeLogoutCompleteReceiver ();
80+ ContextCompat .registerReceiver (targetContext , logoutCompleteReceiver ,
81+ new IntentFilter (SalesforceSDKManager .LOGOUT_COMPLETE_INTENT_ACTION ), RECEIVER_NOT_EXPORTED );
6982 }
7083
7184 @ After
7285 public void tearDown () throws Exception {
86+ final Context targetContext = InstrumentationRegistry .getInstrumentation ().getTargetContext ();
87+ targetContext .unregisterReceiver (logoutCompleteReceiver );
7388 cleanupAccounts (accMgr );
7489 userAccMgr = null ;
7590 accMgr = null ;
91+ logoutCompleteReceiver = null ;
7692 }
7793
7894 /**
@@ -165,6 +181,9 @@ public void testSignoutCurrentUser() {
165181 Assert .assertEquals ("There should be 1 authenticated user" , 1 , userAccMgr .getAuthenticatedUsers ().size ());
166182 userAccMgr .signoutCurrentUser (null , true , OAuth2 .LogoutReason .USER_LOGOUT );
167183 Assert .assertNull ("There should be no authenticated users" , userAccMgr .getAuthenticatedUsers ());
184+ Assert .assertEquals (OAuth2 .LogoutReason .USER_LOGOUT , logoutCompleteReceiver .getLastReasonReceived ());
185+ Assert .assertNotNull (logoutCompleteReceiver .getLastUserAccountReceived ());
186+ Assert .assertEquals (TEST_USERNAME , logoutCompleteReceiver .getLastUserAccountReceived ().getUsername ());
168187 }
169188
170189 /**
@@ -177,6 +196,9 @@ public void testSignoutBackgroundUser() {
177196 userAccMgr .signoutUser (firstUser , null , false , OAuth2 .LogoutReason .USER_LOGOUT );
178197 Assert .assertEquals ("There should be 1 authenticated user" , 1 , userAccMgr .getAuthenticatedUsers ().size ());
179198 checkSameUserAccount (secondUser , userAccMgr .getCurrentUser ());
199+ Assert .assertEquals (OAuth2 .LogoutReason .USER_LOGOUT , logoutCompleteReceiver .getLastReasonReceived ());
200+ Assert .assertNotNull (logoutCompleteReceiver .getLastUserAccountReceived ());
201+ Assert .assertEquals (TEST_USERNAME , logoutCompleteReceiver .getLastUserAccountReceived ().getUsername ());
180202 }
181203
182204 /**
@@ -210,4 +232,37 @@ private UserAccount createOtherTestAccountInAccountManager() {
210232 return userAccount ;
211233 }
212234
235+ private static class FakeLogoutCompleteReceiver extends LogoutCompleteReceiver {
236+ private OAuth2 .LogoutReason lastReasonReceived ;
237+ private UserAccount lastUserAccountReceived ;
238+ // Use a semaphore here to ensure the test doesn't proceed until the logout complete
239+ // receiver has been called
240+ private final Semaphore completionSemaphore = new Semaphore (0 );
241+
242+ protected void onLogoutComplete (@ NotNull OAuth2 .LogoutReason reason , @ Nullable UserAccount userAccount ) {
243+ lastReasonReceived = reason ;
244+ lastUserAccountReceived = userAccount ;
245+ completionSemaphore .release ();
246+ }
247+
248+ public OAuth2 .LogoutReason getLastReasonReceived () {
249+ try {
250+ completionSemaphore .acquire ();
251+ } catch (InterruptedException e ) {
252+ Assert .fail ("Interrupted while waiting for lastReasonReceived to be set" );
253+ }
254+ completionSemaphore .release ();
255+ return lastReasonReceived ;
256+ }
257+
258+ public UserAccount getLastUserAccountReceived () {
259+ try {
260+ completionSemaphore .acquire ();
261+ } catch (InterruptedException e ) {
262+ Assert .fail ("Interrupted while waiting for lastUserAccountReceived to be set" );
263+ }
264+ completionSemaphore .release ();
265+ return lastUserAccountReceived ;
266+ }
267+ }
213268}
0 commit comments