summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/androidTest/java/org/mozilla/geckoview/test/AutofillDelegateTest.kt
blob: f1adc7bf1e9c7f169a57f4f3e58370febc59c317 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
 * Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

package org.mozilla.geckoview.test

import android.graphics.Rect
import android.util.SparseArray
import android.view.KeyEvent
import android.view.View
import androidx.test.filters.MediumTest
import org.hamcrest.Matchers.* // ktlint-disable no-wildcard-imports
import org.junit.Assume.assumeThat
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.mozilla.geckoview.Autofill
import org.mozilla.geckoview.GeckoSession
import org.mozilla.geckoview.GeckoSession.TextInputDelegate
import org.mozilla.geckoview.test.rule.GeckoSessionTestRule.* // ktlint-disable no-wildcard-imports

@RunWith(Parameterized::class)
@MediumTest
class AutofillDelegateTest : BaseSessionTest() {

    companion object {
        @get:Parameterized.Parameters(name = "{0}")
        @JvmStatic
        val parameters: List<Array<out Any>> = listOf(
            arrayOf("#inProcess"),
            arrayOf("#oop"),
        )
    }

    @field:Parameterized.Parameter(0)
    @JvmField
    var iframe: String = ""

    // Whether the iframe is loaded in-process (i.e. with the same origin as the
    // outer html page) or out-of-process.
    private val pageUrl by lazy {
        when (iframe) {
            "#inProcess" -> "http://example.org/tests/junit/forms_xorigin.html"
            "#oop" -> createTestUrl(FORMS_XORIGIN_HTML_PATH)
            else -> throw IllegalStateException()
        }
    }

    @Test fun autofillCommit() {
        sessionRule.setPrefsUntilTestEnd(
            mapOf(
                "signon.rememberSignons" to true,
                "signon.userInputRequiredToCapture.enabled" to false,
            ),
        )

        mainSession.loadUri(pageUrl)
        // Wait for the auto-fill nodes to populate.
        sessionRule.waitUntilCalled(object : Autofill.Delegate, GeckoSession.ProgressDelegate {
            // We expect to get a call to onSessionStart and many calls to onNodeAdd depending
            // on timing.
            @AssertCalled(count = 1)
            override fun onSessionStart(session: GeckoSession) {}

            @AssertCalled(count = -1)
            override fun onNodeAdd(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {}

            @AssertCalled(count = 1)
            override fun onPageStop(session: GeckoSession, success: Boolean) {}
        })

        // Assign node values.
        mainSession.evaluateJS("document.querySelector('#user1').value = 'user1x'")
        mainSession.evaluateJS("document.querySelector('#pass1').value = 'pass1x'")
        mainSession.evaluateJS("document.querySelector('#email1').value = 'e@mail.com'")
        mainSession.evaluateJS("document.querySelector('#number1').value = '1'")

        // Submit the session.
        mainSession.evaluateJS("document.querySelector('#form1').submit()")

        sessionRule.waitUntilCalled(object : Autofill.Delegate {
            @AssertCalled(order = [1, 2, 3, 4])
            override fun onNodeUpdate(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {
            }

            @AssertCalled(order = [5])
            override fun onSessionCommit(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {
                val autofillSession = mainSession.autofillSession
                assertThat(
                    "Values should match",
                    countAutofillNodes({
                        autofillSession.dataFor(it).value == "user1x"
                    }),
                    equalTo(1),
                )
                assertThat(
                    "Values should match",
                    countAutofillNodes({
                        autofillSession.dataFor(it).value == "pass1x"
                    }),
                    equalTo(1),
                )
                assertThat(
                    "Values should match",
                    countAutofillNodes({
                        autofillSession.dataFor(it).value == "e@mail.com"
                    }),
                    equalTo(1),
                )
                assertThat(
                    "Values should match",
                    countAutofillNodes({
                        autofillSession.dataFor(it).value == "1"
                    }),
                    equalTo(1),
                )
            }
        })
    }

    @Test fun autofillCommitIdValue() {
        sessionRule.setPrefsUntilTestEnd(
            mapOf(
                "signon.rememberSignons" to true,
                "signon.userInputRequiredToCapture.enabled" to false,
            ),
        )

        mainSession.loadTestPath(FORMS_ID_VALUE_HTML_PATH)
        // Wait for the auto-fill nodes to populate.
        sessionRule.waitUntilCalled(object : Autofill.Delegate, GeckoSession.ProgressDelegate {
            @AssertCalled(count = 1)
            override fun onSessionStart(session: GeckoSession) {}

            @AssertCalled(count = -1)
            override fun onNodeAdd(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {}

            @AssertCalled(count = 1)
            override fun onPageStop(session: GeckoSession, success: Boolean) {}
        })

        // Assign node values.
        mainSession.evaluateJS("document.querySelector('#value').value = 'pass1x'")

        // Submit the session.
        mainSession.evaluateJS("document.querySelector('#form1').submit()")

        sessionRule.waitUntilCalled(object : Autofill.Delegate {
            @AssertCalled(order = [1])
            override fun onNodeUpdate(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {
            }

            @AssertCalled(order = [2])
            override fun onSessionCommit(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {
                assertThat(
                    "Values should match",
                    countAutofillNodes({
                        mainSession.autofillSession.dataFor(it).value == "pass1x"
                    }),
                    equalTo(1),
                )
            }
        })
    }

    @Test fun autofill() {
        // Test parts of the Oreo auto-fill API; there is another autofill test in
        // SessionAccessibility for a11y auto-fill support.
        mainSession.loadUri(pageUrl)
        // Wait for the auto-fill nodes to populate.
        sessionRule.waitUntilCalled(object : Autofill.Delegate, GeckoSession.ProgressDelegate {
            // We expect many call to onNodeAdd while loading the page
            @AssertCalled(count = -1)
            override fun onNodeAdd(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {}

            @AssertCalled(count = 1)
            override fun onPageStop(session: GeckoSession, success: Boolean) {}
        })

        val autofills = mapOf(
            "#user1" to "bar",
            "#user2" to "bar",
            "#pass1" to "baz",
            "#pass2" to "baz",
            "#email1" to "a@b.c",
            "#number1" to "24",
            "#tel1" to "42",
        )

        // Set up promises to monitor the values changing.
        val promises = autofills.map { entry ->
            // Repeat each test with both the top document and the iframe document.
            mainSession.evaluatePromiseJS(
                """
                window.getDataForAllFrames('${entry.key}', '${entry.value}')
                """,
            )
        }

        val autofillValues = SparseArray<CharSequence>()

        // Perform auto-fill and return number of auto-fills performed.
        fun checkAutofillChild(child: Autofill.Node, domain: String) {
            // Seal the node info instance so we can perform actions on it.
            if (child.children.isNotEmpty()) {
                for (c in child.children) {
                    checkAutofillChild(c!!, child.domain)
                }
            }

            if (child == mainSession.autofillSession.root) {
                return
            }

            assertThat(
                "Should have HTML tag",
                child.tag,
                not(isEmptyOrNullString()),
            )
            if (domain != "") {
                assertThat(
                    "Web domain should match its parent.",
                    child.domain,
                    equalTo(domain),
                )
            }

            if (child.inputType == Autofill.InputType.TEXT) {
                assertThat("Input should be enabled", child.enabled, equalTo(true))
                assertThat(
                    "Input should be focusable",
                    child.focusable,
                    equalTo(true),
                )

                assertThat("Should have HTML tag", child.tag, equalTo("input"))
                assertThat("Should have ID attribute", child.attributes.get("id"), not(isEmptyOrNullString()))
            }

            val childId = mainSession.autofillSession.dataFor(child).id
            autofillValues.append(
                childId,
                when (child.inputType) {
                    Autofill.InputType.NUMBER -> "24"
                    Autofill.InputType.PHONE -> "42"
                    Autofill.InputType.TEXT -> when (child.hint) {
                        Autofill.Hint.PASSWORD -> "baz"
                        Autofill.Hint.EMAIL_ADDRESS -> "a@b.c"
                        else -> "bar"
                    }
                    else -> "bar"
                },
            )
        }

        val nodes = mainSession.autofillSession.root
        checkAutofillChild(nodes, "")

        mainSession.autofillSession.autofill(autofillValues)

        // Wait on the promises and check for correct values.
        for (values in promises.map { it.value.asJsonArray() }) {
            for (i in 0 until values.length()) {
                val (key, actual, expected, eventInterface) = values.get(i).asJSList<String>()

                assertThat("Auto-filled value must match ($key)", actual, equalTo(expected))
                assertThat(
                    "input event should be dispatched with InputEvent interface",
                    eventInterface,
                    equalTo("InputEvent"),
                )
            }
        }
    }

    @Test fun autofillUnknownValue() {
        // Test parts of the Oreo auto-fill API; there is another autofill test in
        // SessionAccessibility for a11y auto-fill support.
        mainSession.loadUri(pageUrl)
        // Wait for the auto-fill nodes to populate.
        sessionRule.waitUntilCalled(object : Autofill.Delegate, GeckoSession.ProgressDelegate {
            @AssertCalled(count = -1)
            override fun onNodeAdd(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {}

            @AssertCalled(count = 1)
            override fun onPageStop(session: GeckoSession, success: Boolean) {}
        })

        val autofillValues = SparseArray<CharSequence>()
        autofillValues.append(-1, "lobster")
        mainSession.autofillSession.autofill(autofillValues)
    }

    private fun countAutofillNodes(
        cond: (Autofill.Node) -> Boolean =
            { it.inputType != Autofill.InputType.NONE },
        root: Autofill.Node? = null,
    ): Int {
        val node = if (root !== null) root else mainSession.autofillSession.root
        return (if (cond(node)) 1 else 0) +
            node.children.sumOf {
                countAutofillNodes(cond, it)
            }
    }

    @WithDisplay(width = 100, height = 100)
    @Test
    fun autofillNavigation() {
        // Wait for the accessibility nodes to populate.
        mainSession.loadUri(pageUrl)

        sessionRule.waitUntilCalled(object :
            Autofill.Delegate,
            ShouldContinue,
            GeckoSession.ProgressDelegate {
            var nodeCount = 0

            // Continue waiting util we get all 16 nodes
            override fun shouldContinue(): Boolean = nodeCount < 16

            @AssertCalled(count = 1)
            override fun onSessionStart(session: GeckoSession) {}

            @AssertCalled(count = -1)
            override fun onNodeAdd(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {
                assertThat("Node should be valid", node, notNullValue())
                nodeCount = countAutofillNodes()
            }

            @AssertCalled(count = 1)
            override fun onPageStop(session: GeckoSession, success: Boolean) {}
        })

        assertThat(
            "Initial auto-fill count should match",
            countAutofillNodes(),
            equalTo(16),
        )

        // Now wait for the nodes to clear.
        mainSession.loadTestPath(HELLO_HTML_PATH)
        sessionRule.waitUntilCalled(object : Autofill.Delegate, GeckoSession.ProgressDelegate {
            @AssertCalled(count = 1)
            override fun onSessionCancel(session: GeckoSession) {}

            @AssertCalled
            override fun onPageStop(session: GeckoSession, success: Boolean) {}
        })

        assertThat(
            "Should not have auto-fill fields",
            countAutofillNodes(),
            equalTo(0),
        )

        mainSession.goBack()
        sessionRule.waitUntilCalled(object :
            Autofill.Delegate,
            GeckoSession.ProgressDelegate,
            ShouldContinue {
            var nodeCount = 0
            override fun shouldContinue(): Boolean = nodeCount < 16

            @AssertCalled(count = 1)
            override fun onSessionStart(session: GeckoSession) {}

            @AssertCalled(count = -1)
            override fun onNodeAdd(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {
                assertThat("Node should be valid", node, notNullValue())
                nodeCount = countAutofillNodes()
            }

            @AssertCalled(count = 1)
            override fun onPageStop(session: GeckoSession, success: Boolean) {}
        })

        assertThat(
            "Should have auto-fill fields again",
            countAutofillNodes(),
            equalTo(16),
        )

        var focused = mainSession.autofillSession.focused
        assertThat(
            "Should not have focused field",
            countAutofillNodes({ it == focused }),
            equalTo(0),
        )

        mainSession.evaluateJS("document.querySelector('#pass2').focus()")

        sessionRule.waitUntilCalled(object : Autofill.Delegate {
            @AssertCalled(count = 1)
            override fun onNodeFocus(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {
                assertThat("ID should be valid", node, notNullValue())
            }
        })

        focused = mainSession.autofillSession.focused
        assertThat(
            "Should have one focused field",
            countAutofillNodes({ it == focused }),
            equalTo(1),
        )
        // The focused field, its siblings, its parent, and the root node should
        // be visible.
        // Hidden elements are ignored.
        // TODO: Is this actually correct? Should the whole focused branch be
        // visible or just the nodes as described above?
        assertThat(
            "Should have nine visible nodes",
            countAutofillNodes({ node -> mainSession.autofillSession.isVisible(node) }),
            equalTo(8),
        )

        mainSession.evaluateJS("document.querySelector('#pass2').blur()")
        sessionRule.waitUntilCalled(object : Autofill.Delegate {
            @AssertCalled(count = 1)
            override fun onNodeBlur(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {
                assertThat("ID should be valid", node, notNullValue())
            }
        })

        focused = mainSession.autofillSession.focused
        assertThat(
            "Should not have focused field",
            countAutofillNodes({ it == focused }),
            equalTo(0),
        )
    }

    @WithDisplay(height = 100, width = 100)
    @Test
    fun autofillUserpass() {
        mainSession.loadTestPath(FORMS2_HTML_PATH)
        // Wait for the auto-fill nodes to populate.
        sessionRule.waitUntilCalled(object : Autofill.Delegate, GeckoSession.ProgressDelegate {
            @AssertCalled(count = 1)
            override fun onSessionStart(session: GeckoSession) {}

            @AssertCalled(count = 1)
            override fun onNodeFocus(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {}

            @AssertCalled(count = -1)
            override fun onNodeAdd(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {}

            @AssertCalled(count = 1)
            override fun onPageStop(session: GeckoSession, success: Boolean) {}
        })

        // Perform auto-fill and return number of auto-fills performed.
        fun checkAutofillChild(child: Autofill.Node): Int {
            var sum = 0
            // Seal the node info instance so we can perform actions on it.
            for (c in child.children) {
                sum += checkAutofillChild(c!!)
            }

            if (child.hint == Autofill.Hint.NONE) {
                return sum
            }

            val childId = mainSession.autofillSession.dataFor(child).id
            assertThat("ID should be valid", childId, not(equalTo(View.NO_ID)))
            assertThat("Should have HTML tag", child.tag, equalTo("input"))

            return sum + 1
        }

        val root = mainSession.autofillSession.root

        // form and iframe have each have 2 nodes with hints.
        assertThat(
            "autofill hint count",
            checkAutofillChild(root),
            equalTo(4),
        )
    }

    @WithDisplay(width = 100, height = 100)
    @Test
    fun autofillActiveChange() {
        // We should blur the active autofill node if the session is set
        // inactive. Likewise, we should focus a node once we return.
        mainSession.loadUri(pageUrl)
        // Wait for the auto-fill nodes to populate.
        sessionRule.waitUntilCalled(object : Autofill.Delegate, GeckoSession.ProgressDelegate {
            // For the root document and the iframe document, each has a form group and
            // a group for inputs outside of forms, so the total count is 4.
            @AssertCalled(count = 1)
            override fun onSessionStart(session: GeckoSession) {}

            @AssertCalled(count = -1)
            override fun onNodeAdd(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {}

            @AssertCalled(count = 1)
            override fun onPageStop(session: GeckoSession, success: Boolean) {}
        })

        mainSession.evaluateJS("document.querySelector('#pass2').focus()")
        sessionRule.waitUntilCalled(object : Autofill.Delegate {
            @AssertCalled(count = 1)
            override fun onNodeFocus(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {
                assertThat("ID should be valid", node, notNullValue())
            }
        })

        var focused = mainSession.autofillSession.focused
        assertThat(
            "Should have one focused field",
            countAutofillNodes({ it == focused }),
            equalTo(1),
        )

        // Make sure we get NODE_BLURRED when inactive
        mainSession.setActive(false)
        sessionRule.waitUntilCalled(object : Autofill.Delegate {
            @AssertCalled(count = 1)
            override fun onNodeBlur(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {
                assertThat("ID should be valid", node, notNullValue())
            }
        })

        // Make sure we get NODE_FOCUSED when active once again
        mainSession.setActive(true)
        sessionRule.waitUntilCalled(object : Autofill.Delegate {
            @AssertCalled(count = 1)
            override fun onNodeFocus(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {
                assertThat("ID should be valid", node, notNullValue())
            }
        })

        focused = mainSession.autofillSession.focused
        assertThat(
            "Should have one focused field",
            countAutofillNodes({ focused == it }),
            equalTo(1),
        )
    }

    @WithDisplay(width = 100, height = 100)
    @Test
    fun autofillAutocompleteAttribute() {
        mainSession.loadTestPath(FORMS_AUTOCOMPLETE_HTML_PATH)
        sessionRule.waitUntilCalled(object : Autofill.Delegate, GeckoSession.ProgressDelegate {
            @AssertCalled(count = -1)
            override fun onNodeAdd(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {}

            @AssertCalled(count = 1)
            override fun onPageStop(session: GeckoSession, success: Boolean) {}
        })

        fun checkAutofillChild(child: Autofill.Node): Int {
            var sum = 0
            for (c in child.children) {
                sum += checkAutofillChild(c!!)
            }
            if (child.hint == Autofill.Hint.NONE) {
                return sum
            }
            assertThat("Should have HTML tag", child.tag, equalTo("input"))
            return sum + 1
        }

        val root = mainSession.autofillSession.root
        // Each page has 3 nodes for autofill.
        assertThat(
            "autofill hint count",
            checkAutofillChild(root),
            equalTo(6),
        )
    }

    @WithDisplay(width = 100, height = 100)
    @Test
    fun autofillWaitForKeyboard() {
        // Wait for the accessibility nodes to populate.
        mainSession.loadUri(pageUrl)
        mainSession.waitForPageStop()

        mainSession.pressKey(KeyEvent.KEYCODE_CTRL_LEFT)
        mainSession.evaluateJS("document.querySelector('#pass2').focus()")

        sessionRule.waitUntilCalled(object : Autofill.Delegate, TextInputDelegate {
            @AssertCalled(order = [2])
            override fun onNodeFocus(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {
                assertThat("ID should be valid", node, notNullValue())
            }

            @AssertCalled(order = [1])
            override fun showSoftInput(session: GeckoSession) {}
        })
    }

    @WithDisplay(width = 300, height = 1000)
    @Test
    fun autofillIframe() {
        // No way to click in x-origin frame.
        assumeThat("Not in x-origin", iframe, not(equalTo("#oop")))

        // Wait for the accessibility nodes to populate.
        mainSession.loadUri(pageUrl)
        mainSession.waitForPageStop()

        // Get non-iframe position of input element
        var screenRect = Rect()
        mainSession.evaluateJS("document.querySelector('#pass2').focus()")

        sessionRule.waitUntilCalled(object : Autofill.Delegate {
            @AssertCalled(count = 1)
            override fun onNodeFocus(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {
                screenRect = node.screenRect
            }
        })

        mainSession.evaluateJS("document.querySelector('iframe').contentDocument.querySelector('#pass2').focus()")

        sessionRule.waitUntilCalled(object : Autofill.Delegate {
            @AssertCalled(count = 1)
            override fun onNodeFocus(
                session: GeckoSession,
                node: Autofill.Node,
                data: Autofill.NodeData,
            ) {
                assertThat("ID should be valid", node, notNullValue())
                // iframe's input element should consider iframe's offset. 200 is enough offset.
                assertThat("position is valid", node.getScreenRect().top, greaterThanOrEqualTo(screenRect.top + 200))
            }
        })
    }
}