Commit 16c2744f authored by Grisha Kruglov's avatar Grisha Kruglov Committed by Grisha Kruglov
Browse files

Pre: Add "Async" prefix to AccountManager deferred functions

This is now a recommended way to name functions that return Deffered.
parent 83bca596
......@@ -29,7 +29,7 @@ class FirefoxAccountsAuthFeature(
fun beginAuthentication() {
CoroutineScope(coroutineContext).launch {
val authUrl = try {
accountManager.beginAuthentication().await()
accountManager.beginAuthenticationAsync().await()
} catch (e: FxaException) {
// FIXME return a fallback URL provided by Config...
"https://accounts.firefox.com/signin"
......@@ -55,7 +55,7 @@ class FirefoxAccountsAuthFeature(
val state = parsedUri.getQueryParameter("state") as String
// Notify the state machine about our success.
accountManager.finishAuthentication(code, state)
accountManager.finishAuthenticationAsync(code, state)
// TODO this can be simplified once https://github.com/mozilla/application-services/issues/305 lands
val successUrl = "${parsedUri.scheme}://${parsedUri.host}/$successPath"
......
......@@ -78,7 +78,7 @@ class FirefoxAccountsAuthFeatureTest {
}
runBlocking {
manager.init().await()
manager.initAsync().await()
}
val mockAddTab: TabsUseCases.AddNewTabUseCase = mock()
......@@ -130,7 +130,7 @@ class FirefoxAccountsAuthFeatureTest {
}
runBlocking {
manager.init().await()
manager.initAsync().await()
}
val mockAddTab: TabsUseCases.AddNewTabUseCase = mock()
......
......@@ -200,8 +200,8 @@ open class FxaAccountManager(
/**
* Call this after registering your observers, and before interacting with this class.
*/
fun init(): Deferred<Unit> {
return processQueue(Event.Init)
fun initAsync(): Deferred<Unit> {
return processQueueAsync(Event.Init)
}
fun authenticatedAccount(): FirefoxAccountShaped? {
......@@ -219,11 +219,11 @@ open class FxaAccountManager(
}
}
fun updateProfile(): Deferred<Unit> {
return processQueue(Event.FetchProfile)
fun updateProfileAsync(): Deferred<Unit> {
return processQueueAsync(Event.FetchProfile)
}
fun beginAuthentication(): Deferred<String> {
fun beginAuthenticationAsync(): Deferred<String> {
val deferredAuthUrl: CompletableDeferred<String> = CompletableDeferred()
oauth.register(object : OAuthObserver {
......@@ -238,17 +238,17 @@ open class FxaAccountManager(
}
})
processQueue(Event.Authenticate)
processQueueAsync(Event.Authenticate)
return deferredAuthUrl
}
fun finishAuthentication(code: String, state: String): Deferred<Unit> {
return processQueue(Event.Authenticated(code, state))
fun finishAuthenticationAsync(code: String, state: String): Deferred<Unit> {
return processQueueAsync(Event.Authenticated(code, state))
}
fun logout(): Deferred<Unit> {
return processQueue(Event.Logout)
fun logoutAsync(): Deferred<Unit> {
return processQueueAsync(Event.Logout)
}
override fun close() {
......@@ -259,7 +259,7 @@ open class FxaAccountManager(
/**
* Pumps the state machine until all events are processed and their side-effects resolve.
*/
private fun processQueue(event: Event): Deferred<Unit> = CoroutineScope(coroutineContext).async {
private fun processQueueAsync(event: Event): Deferred<Unit> = CoroutineScope(coroutineContext).async {
eventQueue.add(event)
do {
val toProcess = eventQueue.poll()
......@@ -299,7 +299,7 @@ open class FxaAccountManager(
// States will have certain side-effects associated with different event transitions.
// In other words, the same state may have different side-effects depending on the event
// which caused a transition.
// For example, a "NotAuthenticated" state may be entered after a logout, and its side-effects
// For example, a "NotAuthenticated" state may be entered after a logoutAsync, and its side-effects
// will include clean-up and re-initialization of an account. Alternatively, it may be entered
// after we've checked local disk, and didn't find a persisted authenticated account.
return when (forState) {
......
......@@ -142,7 +142,7 @@ class FxaAccountManagerTest {
manager.register(accountObserver)
runBlocking {
manager.init().await()
manager.initAsync().await()
}
assertTrue(onErrorCalled)
......@@ -165,7 +165,7 @@ class FxaAccountManagerTest {
val accountObserver: AccountObserver = mock()
manager.register(accountObserver)
manager.init().await()
manager.initAsync().await()
verify(accountObserver, never()).onError(any())
verify(accountObserver, never()).onAuthenticated(any())
......@@ -201,7 +201,7 @@ class FxaAccountManagerTest {
manager.register(accountObserver)
manager.init().await()
manager.initAsync().await()
// Make sure that account and profile observers are fired exactly once.
verify(accountObserver, never()).onError(any())
......@@ -216,10 +216,10 @@ class FxaAccountManagerTest {
assertEquals(mockAccount, manager.authenticatedAccount())
assertEquals(profile, manager.accountProfile())
// Make sure 'logout' clears out state and fires correct observers.
// Make sure 'logoutAsync' clears out state and fires correct observers.
reset(accountObserver)
reset(accountStorage)
manager.logout().await()
manager.logoutAsync().await()
verify(accountObserver, never()).onError(any())
verify(accountObserver, never()).onAuthenticated(any())
......@@ -274,7 +274,7 @@ class FxaAccountManagerTest {
manager.register(accountObserver)
runBlocking {
manager.init().await()
manager.initAsync().await()
}
// We start off as logged-out.
......@@ -282,13 +282,13 @@ class FxaAccountManagerTest {
reset(accountObserver)
runBlocking {
assertEquals("auth://url", manager.beginAuthentication().await())
assertEquals("auth://url", manager.beginAuthenticationAsync().await())
}
assertNull(manager.authenticatedAccount())
assertNull(manager.accountProfile())
runBlocking {
manager.finishAuthentication("dummyCode", "dummyState").await()
manager.finishAuthenticationAsync("dummyCode", "dummyState").await()
}
verify(accountStorage, times(1)).read()
......@@ -348,7 +348,7 @@ class FxaAccountManagerTest {
manager.register(accountObserver)
runBlocking {
manager.init().await()
manager.initAsync().await()
}
// We start off as logged-out.
......@@ -357,7 +357,7 @@ class FxaAccountManagerTest {
reset(accountObserver)
runBlocking {
try {
manager.beginAuthentication().await()
manager.beginAuthenticationAsync().await()
fail()
} catch (e: FxaNetworkException) {
assertEquals(fxaException, e)
......@@ -374,14 +374,14 @@ class FxaAccountManagerTest {
.thenReturn(CompletableDeferred("auth://url"))
runBlocking {
assertEquals("auth://url", manager.beginAuthentication().await())
assertEquals("auth://url", manager.beginAuthenticationAsync().await())
}
assertNull(manager.authenticatedAccount())
assertNull(manager.accountProfile())
runBlocking {
manager.finishAuthentication("dummyCode", "dummyState").await()
manager.finishAuthenticationAsync("dummyCode", "dummyState").await()
}
verify(accountStorage, times(1)).read()
......@@ -435,7 +435,7 @@ class FxaAccountManagerTest {
manager.register(accountObserver)
runBlocking {
manager.init().await()
manager.initAsync().await()
}
// We start off as logged-out.
......@@ -443,13 +443,13 @@ class FxaAccountManagerTest {
reset(accountObserver)
runBlocking {
assertEquals("auth://url", manager.beginAuthentication().await())
assertEquals("auth://url", manager.beginAuthenticationAsync().await())
}
assertNull(manager.authenticatedAccount())
assertNull(manager.accountProfile())
runBlocking {
manager.finishAuthentication("dummyCode", "dummyState").await()
manager.finishAuthenticationAsync("dummyCode", "dummyState").await()
}
verify(accountStorage, times(1)).read()
......@@ -473,7 +473,7 @@ class FxaAccountManagerTest {
`when`(mockAccount.getProfile(ArgumentMatchers.anyBoolean())).thenReturn(CompletableDeferred(profile))
runBlocking {
manager.updateProfile().await()
manager.updateProfileAsync().await()
}
verify(accountObserver, times(1)).onProfileUpdated(profile)
......
......@@ -74,7 +74,7 @@ class MainActivity : AppCompatActivity(), LoginFragment.OnLoginCompleteListener,
findViewById<View>(R.id.buttonSignIn).setOnClickListener {
launch {
val authUrl = try {
accountManager.beginAuthentication().await()
accountManager.beginAuthenticationAsync().await()
} catch (error: FxaException) {
val txtView: TextView = findViewById(R.id.fxaStatusView)
txtView.text = getString(R.string.account_error, error.toString())
......@@ -85,7 +85,7 @@ class MainActivity : AppCompatActivity(), LoginFragment.OnLoginCompleteListener,
}
findViewById<View>(R.id.buttonLogout).setOnClickListener {
launch { accountManager.logout().await() }
launch { accountManager.logoutAsync().await() }
}
// NB: ObserverRegistry takes care of unregistering this observer when appropriate, and
......@@ -95,7 +95,7 @@ class MainActivity : AppCompatActivity(), LoginFragment.OnLoginCompleteListener,
accountManager.register(accountObserver, owner = this, autoPause = true)
// Now that our account state observer is registered, we can kick off the account manager.
launch { accountManager.init().await() }
launch { accountManager.initAsync().await() }
findViewById<View>(R.id.buttonSyncHistory).setOnClickListener {
val account = accountManager.authenticatedAccount() ?: return@setOnClickListener
......@@ -133,7 +133,7 @@ class MainActivity : AppCompatActivity(), LoginFragment.OnLoginCompleteListener,
override fun onLoginComplete(code: String, state: String, fragment: LoginFragment) {
launch {
supportFragmentManager?.popBackStack()
accountManager.finishAuthentication(code, state).await()
accountManager.finishAuthenticationAsync(code, state).await()
}
}
......
......@@ -88,12 +88,12 @@ open class MainActivity : AppCompatActivity(), LoginFragment.OnLoginCompleteList
accountManager.register(accountObserver, owner = this, autoPause = true)
launch { accountManager.init().await() }
launch { accountManager.initAsync().await() }
findViewById<View>(R.id.buttonWebView).setOnClickListener {
launch {
val authUrl = try {
accountManager.beginAuthentication().await()
accountManager.beginAuthenticationAsync().await()
} catch (error: FxaException) {
Toast.makeText(this@MainActivity, "Account auth error: $error", Toast.LENGTH_LONG).show()
return@launch
......@@ -135,7 +135,7 @@ open class MainActivity : AppCompatActivity(), LoginFragment.OnLoginCompleteList
override fun onLoginComplete(code: String, state: String, fragment: LoginFragment) {
launch {
accountManager.finishAuthentication(code, state).await()
accountManager.finishAuthenticationAsync(code, state).await()
supportFragmentManager?.popBackStack()
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment