Bug 1663039 - [2.0] Add GV test to verify autofillEnabled setting is respected. r=geckoview-reviewers,agi

Differential Revision: https://phabricator.services.mozilla.com/D92245
This commit is contained in:
Eugen Sawin 2020-10-06 17:53:14 +00:00
parent 80ec8b0fd5
commit 4182c16fd3
3 changed files with 109 additions and 14 deletions

View File

@ -8,6 +8,7 @@ import androidx.test.filters.MediumTest
import androidx.test.ext.junit.runners.AndroidJUnit4
import android.os.Handler
import android.view.KeyEvent
import org.hamcrest.Matchers.*
@ -539,6 +540,94 @@ class AutocompleteTest : BaseSessionTest() {
sessionRule.runtime.settings.loginAutofillEnabled = true
}
fun testPasswordAutofill(autofillEnabled: Boolean) {
sessionRule.setPrefsUntilTestEnd(mapOf(
// Enable login management since it's disabled in automation.
"signon.rememberSignons" to true,
"signon.autofillForms.http" to true,
"signon.userInputRequiredToCapture.enabled" to false))
val runtime = sessionRule.runtime
val register = { delegate: LoginStorageDelegate ->
runtime.loginStorageDelegate = delegate
}
val unregister = { _: LoginStorageDelegate ->
runtime.loginStorageDelegate = null
}
val user1 = "user1x"
val pass1 = "pass1x"
val guid = "test-guid"
val origin = GeckoSessionTestRule.TEST_ENDPOINT
val savedLogin = LoginEntry.Builder()
.guid(guid)
.origin(origin)
.formActionOrigin(origin)
.username(user1)
.password(pass1)
.build()
val savedLogins = mutableListOf<LoginEntry>(savedLogin)
sessionRule.addExternalDelegateUntilTestEnd(
LoginStorageDelegate::class, register, unregister,
object : LoginStorageDelegate {
@AssertCalled
override fun onLoginFetch(domain: String)
: GeckoResult<Array<LoginEntry>>? {
assertThat("Domain should match", domain, equalTo("localhost"))
return GeckoResult.fromValue(savedLogins.toTypedArray())
}
@AssertCalled(false)
override fun onLoginUsed(login: LoginEntry, usedFields: Int) {}
})
sessionRule.delegateUntilTestEnd(object : Callbacks.PromptDelegate {
@AssertCalled(false)
override fun onLoginSave(
session: GeckoSession,
prompt: AutocompleteRequest<LoginSaveOption>)
: GeckoResult<PromptDelegate.PromptResponse>? {
return null
}
})
mainSession.loadTestPath(FORMS3_HTML_PATH)
mainSession.waitForPageStop()
mainSession.evaluateJS("document.querySelector('#user1').focus()")
mainSession.evaluateJS(
"document.querySelector('#user1').value = '$user1'")
mainSession.pressKey(KeyEvent.KEYCODE_TAB)
val pass = mainSession.evaluateJS(
"document.querySelector('#pass1').value") as String
if (autofillEnabled) {
assertThat(
"Password should match",
pass,
equalTo(pass1))
} else {
assertThat(
"Password should not be filled",
pass,
equalTo(""))
}
}
@Test
fun loginAutofillDisabledPasswordAutofill() {
sessionRule.runtime.settings.loginAutofillEnabled = false
testPasswordAutofill(false)
sessionRule.runtime.settings.loginAutofillEnabled = true
}
@Test
fun loginAutofillEnabledPasswordAutofill() {
testPasswordAutofill(true)
}
@Test
fun loginSelectAccept() {
sessionRule.setPrefsUntilTestEnd(mapOf(

View File

@ -6,7 +6,11 @@
package org.mozilla.geckoview.test
import android.os.Parcel
import android.os.SystemClock
import android.view.KeyEvent
import androidx.test.platform.app.InstrumentationRegistry
import org.mozilla.geckoview.GeckoRuntimeSettings
import org.mozilla.geckoview.GeckoSession
import org.mozilla.geckoview.test.rule.GeckoSessionTestRule
@ -170,6 +174,19 @@ open class BaseSessionTest(noErrorCollector: Boolean = false) {
fun GeckoSession.waitForRoundTrip() = sessionRule.waitForRoundTrip(this)
fun GeckoSession.pressKey(keyCode: Int) {
// Create a Promise to listen to the key event, and wait on it below.
val promise = this.evaluatePromiseJS(
"""new Promise(r => window.addEventListener(
'keyup', r, { once: true }))""")
val time = SystemClock.uptimeMillis()
val keyEvent = KeyEvent(time, time, KeyEvent.ACTION_DOWN, keyCode, 0)
this.textInput.onKeyDown(keyCode, keyEvent)
this.textInput.onKeyUp(
keyCode, KeyEvent.changeAction(keyEvent, KeyEvent.ACTION_UP))
promise.value
}
@Suppress("UNCHECKED_CAST")
fun Any?.asJsonArray(): JSONArray = this as JSONArray

View File

@ -171,17 +171,6 @@ class TextInputDelegateTest : BaseSessionTest() {
promise.value
}
private fun pressKey(keyCode: Int) {
// Create a Promise to listen to the key event, and wait on it below.
val promise = mainSession.evaluatePromiseJS(
"new Promise(r => window.addEventListener('keyup', r, { once: true }))")
val time = SystemClock.uptimeMillis()
val keyEvent = KeyEvent(time, time, KeyEvent.ACTION_DOWN, keyCode, 0)
mainSession.textInput.onKeyDown(keyCode, keyEvent)
mainSession.textInput.onKeyUp(keyCode, KeyEvent.changeAction(keyEvent, KeyEvent.ACTION_UP))
promise.value
}
private fun pressKey(ic: InputConnection, keyCode: Int) {
val promise = mainSession.evaluatePromiseJS(
when (id) {
@ -248,7 +237,7 @@ class TextInputDelegateTest : BaseSessionTest() {
mainSession.evaluateJS("document.querySelector('$id').focus(); document.querySelector('$id').blur()")
// Simulate a user action so we're allowed to show/hide the keyboard.
pressKey(KeyEvent.KEYCODE_CTRL_LEFT)
mainSession.pressKey(KeyEvent.KEYCODE_CTRL_LEFT)
mainSession.evaluateJS("document.querySelector('$id').focus()")
mainSession.waitUntilCalled(object : Callbacks.TextInputDelegate {
@ -278,7 +267,7 @@ class TextInputDelegateTest : BaseSessionTest() {
mainSession.waitForPageStop()
// Simulate a user action so we're allowed to show/hide the keyboard.
pressKey(KeyEvent.KEYCODE_CTRL_LEFT)
mainSession.pressKey(KeyEvent.KEYCODE_CTRL_LEFT)
mainSession.evaluateJS("document.querySelector('$id').focus()")
mainSession.waitUntilCalled(GeckoSession.TextInputDelegate::class,
"restartInput", "showSoftInput")
@ -313,7 +302,7 @@ class TextInputDelegateTest : BaseSessionTest() {
mainSession.waitForPageStop()
// Simulate a user action so we're allowed to show/hide the keyboard.
pressKey(KeyEvent.KEYCODE_CTRL_LEFT)
mainSession.pressKey(KeyEvent.KEYCODE_CTRL_LEFT)
mainSession.evaluateJS("document.querySelector('$id').focus()")
mainSession.waitUntilCalled(object : Callbacks.TextInputDelegate {