-
Notifications
You must be signed in to change notification settings - Fork 932
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure autofill disable prompt disabled no matter how a credential is…
… saved (#3856) <!-- Note: This checklist is a reminder of our shared engineering expectations. The items in Bold are required If your PR involves UI changes: 1. Upload screenshots or screencasts that illustrate the changes before / after 2. Add them under the UI changes section (feel free to add more columns if needed) If your PR does not involve UI changes, you can remove the **UI changes** section At a minimum, make sure your changes are tested in API 23 and one of the more recent API levels available. --> Task/Issue URL: https://app.asana.com/0/1203822806345703/1205939031559534/f ### Description Change the way the Autofill decline counter works to ensure it is disabled when credentials are added regardless of how they were added. Previously, this required the decline counter to be manually disabled at the point in the code that was adding a credential (e.g., when handing the result of save credential prompt) but wasn't handling the other places that a credential could be created (sync + manually created). This PR introduces a dedicated observer on the credential count that will automatically disable the decline counter if it detects one or more credentials exist. ### Steps to test this PR Logcat filter: `package:mine & (message~:"CredentialEverPopulatedObserver" | message~:"Permanently disabling Autofill decline counter" | message~:"User declined to save credentials" | message~:"Determined if decline counter should be active")` #### Decline counter disabled when manually adding a credential - [x] Fresh install (`internal`) - [x] Visit Overflow->Logins - [x] Click the ➕ and add details for a login. tap the ✔️ to save - [x] Verify you see the following in the logs: ``` Permanently disabling Autofill decline counter CredentialEverPopulatedObserver stopped ``` #### Decline counter disabled from sync - [x] Fresh install (`internal`) - [x] Go to sync, and sync saved logins from elsewhere - [x] Verify you see the following in the logs: ``` Permanently disabling Autofill decline counter CredentialEverPopulatedObserver stopped ``` #### Decline counter disabled from saving a login - [x] Fresh install (`internal`) - [x] Visit https://fill.dev/form/login-simple. Enter username and password (>= 4 chars). Tap login button - [x] Save when prompted - [x] Verify you see the following in the logs: ``` Permanently disabling Autofill decline counter CredentialEverPopulatedObserver stopped ``` #### Ensure decline counter still works - [x] Fresh install (`internal`) - [x] Visit https://fill.dev/form/login-simple. Enter username and password (>= 4 chars). Tap login button. **Don't save**. - [x] Login on another page( e.g., https://privacy-test-pages.glitch.me/autofill/form-submission.html). **Don't save.** - [x] Verify you see the prompt to "Keep saving logins"
- Loading branch information
Showing
6 changed files
with
171 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...duckduckgo/autofill/impl/ui/credential/saving/declines/CredentialEverPopulatedObserver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (c) 2023 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.autofill.impl.ui.credential.saving.declines | ||
|
||
import androidx.lifecycle.LifecycleOwner | ||
import com.duckduckgo.app.di.AppCoroutineScope | ||
import com.duckduckgo.app.lifecycle.MainProcessLifecycleObserver | ||
import com.duckduckgo.autofill.api.store.AutofillStore | ||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.squareup.anvil.annotations.ContributesMultibinding | ||
import dagger.SingleInstanceIn | ||
import javax.inject.Inject | ||
import kotlin.coroutines.coroutineContext | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.takeWhile | ||
import kotlinx.coroutines.job | ||
import kotlinx.coroutines.launch | ||
import timber.log.Timber | ||
|
||
@ContributesMultibinding( | ||
scope = AppScope::class, | ||
boundType = MainProcessLifecycleObserver::class, | ||
) | ||
@SingleInstanceIn(AppScope::class) | ||
class CredentialEverPopulatedObserver @Inject constructor( | ||
private val declineCounter: AutofillDisablingDeclineCounter, | ||
private val autofillStore: AutofillStore, | ||
@AppCoroutineScope private val appCoroutineScope: CoroutineScope, | ||
private val dispatchers: DispatcherProvider, | ||
) : MainProcessLifecycleObserver { | ||
|
||
override fun onCreate(owner: LifecycleOwner) { | ||
super.onCreate(owner) | ||
|
||
appCoroutineScope.launch(dispatchers.io()) { | ||
observeForCredentials() | ||
} | ||
} | ||
|
||
private suspend fun observeForCredentials() { | ||
autofillStore.getCredentialCount() | ||
.takeWhile { declineCounter.isActive() } | ||
.collect { numberCredentials -> | ||
Timber.d("CredentialEverPopulatedObserver received number of stored credentials: $numberCredentials") | ||
|
||
// when any credentials added, we can permanently disable the decline counter and stop observing the credential count | ||
if (numberCredentials > 0) { | ||
declineCounter.disableDeclineCounter() | ||
coroutineContext.job.cancel() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...duckgo/autofill/impl/ui/credential/saving/declines/CredentialEverPopulatedObserverTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.duckduckgo.autofill.impl.ui.credential.saving.declines | ||
|
||
import androidx.lifecycle.LifecycleOwner | ||
import com.duckduckgo.autofill.api.store.AutofillStore | ||
import com.duckduckgo.common.test.CoroutineTestRule | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.test.TestScope | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Assert.assertFalse | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.whenever | ||
|
||
@ExperimentalCoroutinesApi | ||
class CredentialEverPopulatedObserverTest { | ||
|
||
@get:Rule | ||
val coroutineTestRule: CoroutineTestRule = CoroutineTestRule() | ||
|
||
private val autofillStore: AutofillStore = mock() | ||
private val credentialCountFlow = MutableSharedFlow<Int>() | ||
|
||
@Before | ||
fun setup() { | ||
runTest { | ||
whenever(autofillStore.monitorDeclineCounts).thenReturn(true) | ||
whenever(autofillStore.autofillAvailable).thenReturn(true) | ||
whenever(autofillStore.getCredentialCount()).thenReturn(credentialCountFlow) | ||
} | ||
|
||
declineCounter = AutofillDisablingDeclineCounter( | ||
autofillStore = autofillStore, | ||
appCoroutineScope = coroutineTestRule.testScope, | ||
dispatchers = coroutineTestRule.testDispatcherProvider, | ||
) | ||
|
||
testee = CredentialEverPopulatedObserver( | ||
declineCounter = declineCounter, | ||
autofillStore = autofillStore, | ||
appCoroutineScope = coroutineTestRule.testScope, | ||
dispatchers = coroutineTestRule.testDispatcherProvider, | ||
) | ||
} | ||
|
||
private lateinit var declineCounter: AutofillDisablingDeclineCounter | ||
private lateinit var testee: CredentialEverPopulatedObserver | ||
|
||
@Test | ||
fun whenUpdatedCredentialCountIsAbove0ThenDeclineCounterDisabled() = runTest { | ||
testee.onCreate(mock<LifecycleOwner>()) | ||
assertTrue(declineCounter.isActive()) | ||
|
||
simulateFlowEmissionUpdatedCredentialCount(1, this) | ||
|
||
assertFalse(declineCounter.isActive()) | ||
} | ||
|
||
@Test | ||
fun whenUpdatedCredentialCountIs0ThenDeclineCounterNotDisabled() = runTest { | ||
testee.onCreate(mock<LifecycleOwner>()) | ||
assertTrue(declineCounter.isActive()) | ||
|
||
simulateFlowEmissionUpdatedCredentialCount(credentialCount = 0, this) | ||
|
||
assertTrue(declineCounter.isActive()) | ||
} | ||
|
||
private suspend fun simulateFlowEmissionUpdatedCredentialCount( | ||
credentialCount: Int, | ||
scope: TestScope, | ||
) { | ||
// simulate emitting an updated credential count | ||
scope.launch { | ||
credentialCountFlow.emit(credentialCount) | ||
}.join() | ||
} | ||
} |