File: node_navigation_utils_unittest.js

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (911 lines) | stat: -rw-r--r-- 38,025 bytes parent folder | download | duplicates (4)
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

GEN_INCLUDE(['../select_to_speak/mv2/select_to_speak_e2e_test_base.js']);

/**
 * Test fixture for navigation_utils.js.
 */
SelectToSpeakNodeNavigationUtilsUnitTest = class extends SelectToSpeakE2ETest {
};

AX_TEST_F(
    'SelectToSpeakNodeNavigationUtilsUnitTest', 'GetNodesForNextParagraph',
    function() {
      const root = createMockNode({role: 'rootWebArea'});
      const paragraph1 = createMockNode(
          {role: 'paragraph', display: 'block', parent: root, root});
      const text1 = createMockNode(
          {role: 'staticText', parent: paragraph1, root, name: 'Line 1'});
      const text2 = createMockNode(
          {role: 'staticText', parent: paragraph1, root, name: 'Line 2'});
      const paragraph2 = createMockNode(
          {role: 'paragraph', display: 'block', parent: root, root});
      const text3 = createMockNode(
          {role: 'staticText', parent: paragraph2, root, name: 'Line 3'});

      const nodeGroupForParagraph1 = ParagraphUtils.buildNodeGroup(
          [text1, text2], 0 /* index */, {splitOnLanguage: false});
      const nodeGroupForParagraph2 = ParagraphUtils.buildNodeGroup(
          [text3], 0 /* index */, {splitOnLanguage: false});

      // Navigating forward from paragraph 1 returns the nodes of paragraph 2.
      let result = NodeNavigationUtils.getNodesForNextParagraph(
          nodeGroupForParagraph1, constants.Dir.FORWARD,
          () => true /* does not filter any paragraph */);
      assertEquals(result.length, 1);
      assertEquals(result[0], text3);

      // Navigating backward from paragraph 1 returns no nodes.
      result = NodeNavigationUtils.getNodesForNextParagraph(
          nodeGroupForParagraph1, constants.Dir.BACKWARD,
          () => true /* does not filter any paragraph */);
      assertEquals(result.length, 0);

      // Navigating backward from paragraph 2 returns the nodes of paragraph 1.
      result = NodeNavigationUtils.getNodesForNextParagraph(
          nodeGroupForParagraph2, constants.Dir.BACKWARD,
          () => true /* does not filter any paragraph */);
      assertEquals(result.length, 2);
      assertEquals(result[0], text1);
      assertEquals(result[1], text2);

      // Navigating forward from paragraph 2 returns no nodes.
      result = NodeNavigationUtils.getNodesForNextParagraph(
          nodeGroupForParagraph2, constants.Dir.FORWARD,
          () => true /* does not filter any paragraph */);
      assertEquals(result.length, 0);

      // Navigates forward from paragraph 1 with a pred that filters out
      // paragraph 2.
      result = NodeNavigationUtils.getNodesForNextParagraph(
          nodeGroupForParagraph1, constants.Dir.FORWARD,
          nodes => !(nodes.find(
              n => n.parent ===
                  paragraph2) /* filter out nodes belong to paragraph 2 */));
      assertEquals(result.length, 0);
    });

AX_TEST_F(
    'SelectToSpeakNodeNavigationUtilsUnitTest', 'GetNextParagraphWithNode',
    function() {
      const root = createMockNode({role: 'rootWebArea'});
      const paragraph1 = createMockNode(
          {role: 'paragraph', display: 'block', parent: root, root});
      const text1 = createMockNode(
          {role: 'staticText', parent: paragraph1, root, name: 'Line 1'});
      const text2 = createMockNode(
          {role: 'staticText', parent: paragraph1, root, name: 'Line 2'});
      const paragraph2 = createMockNode(
          {role: 'paragraph', display: 'block', parent: root, root});
      const text3 = createMockNode(
          {role: 'staticText', parent: paragraph2, root, name: 'Line 3'});

      // Forward from first text node of paragraph 1 gives paragraph 2 nodes.
      let result = NodeNavigationUtils.getNextParagraphWithNode_(
          text1, constants.Dir.FORWARD);
      assertEquals(result.length, 1);
      assertEquals(result[0], text3);

      // Forward from second text node of paragraph 1 gives paragraph 2 nodes.
      result = NodeNavigationUtils.getNextParagraphWithNode_(
          text2, constants.Dir.FORWARD);
      assertEquals(result.length, 1);
      assertEquals(result[0], text3);

      // Forward from paragraph 1 node gives paragraph 2 nodes.
      result = NodeNavigationUtils.getNextParagraphWithNode_(
          paragraph1, constants.Dir.FORWARD);
      assertEquals(result.length, 1);
      assertEquals(result[0], text3);

      // Forward from text node of paragraph 2 returns no nodes.
      result = NodeNavigationUtils.getNextParagraphWithNode_(
          text3, constants.Dir.FORWARD);
      assertEquals(result.length, 0);

      // Forward from paragraph 2 returns no nodes.
      result = NodeNavigationUtils.getNextParagraphWithNode_(
          paragraph2, constants.Dir.FORWARD);
      assertEquals(result.length, 0);

      // Backward from text node of paragraph 2 returns paragraph 1 nodes.
      result = NodeNavigationUtils.getNextParagraphWithNode_(
          text3, constants.Dir.BACKWARD);
      assertEquals(result.length, 2);
      assertEquals(result[0], text1);
      assertEquals(result[1], text2);

      // Backward from paragraph 2 node returns paragraph 1 nodes.
      result = NodeNavigationUtils.getNextParagraphWithNode_(
          paragraph2, constants.Dir.BACKWARD);
      assertEquals(result.length, 2);
      assertEquals(result[0], text1);
      assertEquals(result[1], text2);

      // Backward from text node of paragraph 1 returns no nodes.
      result = NodeNavigationUtils.getNextParagraphWithNode_(
          text2, constants.Dir.BACKWARD);
      assertEquals(result.length, 0);

      // Backward from text node of paragraph 1 returns no nodes.
      result = NodeNavigationUtils.getNextParagraphWithNode_(
          text1, constants.Dir.BACKWARD);
      assertEquals(result.length, 0);

      // Backward from paragraph 1 node returns no nodes.
      result = NodeNavigationUtils.getNextParagraphWithNode_(
          paragraph1, constants.Dir.BACKWARD);
      assertEquals(result.length, 0);
    });

AX_TEST_F(
    'SelectToSpeakNodeNavigationUtilsUnitTest',
    'GetNextParagraphWithNodeContainedWithinRoot', function() {
      const desktop = createMockNode({role: 'desktop'});

      const root = createMockNode({role: 'rootWebArea', parent: desktop});
      const paragraph1 = createMockNode(
          {role: 'paragraph', display: 'block', parent: root, root});
      const text1 = createMockNode(
          {role: 'staticText', parent: paragraph1, root, name: 'Line 1'});
      const otherRoot = createMockNode({role: 'rootWebArea', parent: desktop});
      const paragraph2 = createMockNode(
          {role: 'paragraph', display: 'block', parent: otherRoot, root});
      createMockNode({
        role: 'staticText',
        parent: paragraph2,
        root: otherRoot,
        name: 'Line 2',
      });

      let result = NodeNavigationUtils.getNextParagraphWithNode_(
          text1, constants.Dir.FORWARD);
      assertEquals(result.length, 0);

      NodeNavigationUtils.getNextParagraphWithNode_(
          text1, constants.Dir.BACKWARD);
      assertEquals(result.length, 0);

      result = NodeNavigationUtils.getNextParagraphWithNode_(
          paragraph1, constants.Dir.FORWARD);
      assertEquals(result.length, 0);

      result = NodeNavigationUtils.getNextParagraphWithNode_(
          paragraph1, constants.Dir.BACKWARD);
      assertEquals(result.length, 0);
    });

AX_TEST_F(
    'SelectToSpeakNodeNavigationUtilsUnitTest',
    'GetNextParagraphWithNodeThroughIframe', function() {
      const desktop = createMockNode({role: 'desktop'});

      const root =
          createMockNode({role: 'rootWebArea', parent: desktop, root: desktop});
      const paragraph1 = createMockNode(
          {role: 'paragraph', display: 'block', parent: root, root});
      const text1 = createMockNode(
          {role: 'staticText', parent: paragraph1, root, name: 'Line 1'});
      const iframeRoot =
          createMockNode({role: 'rootWebArea', parent: root, root});
      const paragraph2 = createMockNode({
        role: 'paragraph',
        display: 'block',
        parent: iframeRoot,
        root: iframeRoot,
      });
      const text2 = createMockNode({
        role: 'staticText',
        parent: paragraph2,
        root: iframeRoot,
        name: 'Line 2',
      });
      const paragraph3 = createMockNode(
          {role: 'paragraph', display: 'block', parent: root, root});
      const text3 = createMockNode(
          {role: 'staticText', parent: paragraph3, root, name: 'Line 3'});

      let result = NodeNavigationUtils.getNextParagraphWithNode_(
          text1, constants.Dir.FORWARD);
      assertEquals(result.length, 1);
      assertEquals(result[0], text2);

      result = NodeNavigationUtils.getNextParagraphWithNode_(
          text2, constants.Dir.FORWARD);
      assertEquals(result.length, 1);
      assertEquals(result[0], text3);
    });

AX_TEST_F(
    'SelectToSpeakNodeNavigationUtilsUnitTest',
    'GetNextParagraphWithNodeNonBlockNodes', function() {
      /**
       * Example below is roughly similar to:
       * <p>Line 1</p>
       * <span>Line 2</span>
       * <p><span>Line 2</span><span>Line 3</span></p>
       */
      const root = createMockNode({role: 'rootWebArea'});
      const paragraph1 = createMockNode(
          {role: 'paragraph', display: 'block', parent: root, root});
      const text1 = createMockNode(
          {role: 'staticText', parent: paragraph1, root, name: 'Line 1'});
      const text2 = createMockNode(
          {role: 'staticText', parent: root, root, name: 'Line 2'});
      const paragraph2 = createMockNode(
          {role: 'paragraph', display: 'block', parent: root, root});
      const text3 = createMockNode(
          {role: 'staticText', parent: paragraph2, root, name: 'Line 3'});
      const text4 = createMockNode(
          {role: 'staticText', parent: paragraph2, root, name: 'Line 4'});

      // Forward from first text node of paragraph 1 gives inline text node.
      let result = NodeNavigationUtils.getNextParagraphWithNode_(
          text1, constants.Dir.FORWARD);
      assertEquals(result.length, 1);
      assertEquals(result[0], text2);

      // Forward from inline text node gives paragraph 2 nodes.
      result = NodeNavigationUtils.getNextParagraphWithNode_(
          text2, constants.Dir.FORWARD);
      assertEquals(result.length, 2);
      assertEquals(result[0], text3);
      assertEquals(result[1], text4);

      // Backwards from paragraph 2 inline text node.
      result = NodeNavigationUtils.getNextParagraphWithNode_(
          paragraph2, constants.Dir.BACKWARD);
      assertEquals(result.length, 1);
      assertEquals(result[0], text2);
    });

AX_TEST_F(
    'SelectToSpeakNodeNavigationUtilsUnitTest',
    'GetNextParagraphWithNodeNestedBlocks', function() {
      const root = createMockNode({role: 'rootWebArea'});
      const paragraph1 = createMockNode(
          {role: 'paragraph', display: 'block', parent: root, root});
      const text1 = createMockNode(
          {role: 'staticText', parent: paragraph1, root, name: 'Before text'});
      const nestedParagraph = createMockNode(
          {role: 'paragraph', display: 'block', parent: paragraph1, root});
      const text2 = createMockNode({
        role: 'staticText',
        parent: nestedParagraph,
        root,
        name: 'Middle text',
      });
      const text3 = createMockNode(
          {role: 'staticText', parent: paragraph1, root, name: 'After text'});

      // Getting next paragraph from nested paragraph only includes nodes in
      // the forward direction (does not include itself)
      let result = NodeNavigationUtils.getNextParagraphWithNode_(
          text2, constants.Dir.FORWARD);
      assertEquals(result.length, 1);
      assertEquals(result[0], text3);

      // Getting next paragraph from nested paragraph only includes nodes in
      // the backward direction (does not include itself)
      result = NodeNavigationUtils.getNextParagraphWithNode_(
          text2, constants.Dir.BACKWARD);
      assertEquals(result.length, 1);
      assertEquals(result[0], text1);
    });

AX_TEST_F(
    'SelectToSpeakNodeNavigationUtilsUnitTest',
    'GetNextParagraphWithNodeAndroid', function() {
      const root = createMockNode({role: 'application'});
      const container1 =
          createMockNode({role: 'genericContainer', parent: root, root});
      const text1 = createMockNode(
          {role: 'staticText', parent: container1, root, name: 'Line 1'});
      const text2 = createMockNode(
          {role: 'staticText', parent: container1, root, name: 'Line 2'});
      const container2 =
          createMockNode({role: 'genericContainer', parent: root, root});
      const text3 = createMockNode(
          {role: 'staticText', parent: container2, root, name: 'Line 3'});

      // Without paragraphs, navigate node to node.
      let result = NodeNavigationUtils.getNextParagraphWithNode_(
          text1, constants.Dir.FORWARD);
      assertEquals(result.length, 1);
      assertEquals(result[0], text2);

      result = NodeNavigationUtils.getNextParagraphWithNode_(
          text2, constants.Dir.FORWARD);
      assertEquals(result.length, 1);
      assertEquals(result[0], text3);

      result = NodeNavigationUtils.getNextParagraphWithNode_(
          container1, constants.Dir.FORWARD);
      assertEquals(result.length, 1);
      assertEquals(result[0], text3);

      result = NodeNavigationUtils.getNextParagraphWithNode_(
          text3, constants.Dir.FORWARD);
      assertEquals(result.length, 0);

      result = NodeNavigationUtils.getNextParagraphWithNode_(
          container2, constants.Dir.FORWARD);
      assertEquals(result.length, 0);

      result = NodeNavigationUtils.getNextParagraphWithNode_(
          text3, constants.Dir.BACKWARD);
      assertEquals(result.length, 1);
      assertEquals(result[0], text2);

      result = NodeNavigationUtils.getNextParagraphWithNode_(
          container2, constants.Dir.BACKWARD);
      assertEquals(result.length, 1);
      assertEquals(result[0], text2);

      result = NodeNavigationUtils.getNextParagraphWithNode_(
          text2, constants.Dir.BACKWARD);
      assertEquals(result.length, 1);
      assertEquals(result[0], text1);

      result = NodeNavigationUtils.getNextParagraphWithNode_(
          text1, constants.Dir.BACKWARD);
      assertEquals(result.length, 0);

      result = NodeNavigationUtils.getNextParagraphWithNode_(
          container1, constants.Dir.BACKWARD);
      assertEquals(result.length, 0);
    });

AX_TEST_F(
    'SelectToSpeakNodeNavigationUtilsUnitTest',
    'GetNextNodesInParagraphFromNodeGroupEmptyNodeGroup', function() {
      const nodeGroup = {nodes: []};
      const result = NodeNavigationUtils.getNextNodesInParagraphFromNodeGroup(
          nodeGroup, 0 /* charIndex */, constants.Dir.FORWARD /* direction */);
      assertEquals(result.nodes.length, 0);
      assertEquals(result.offset, -1);
    });

AX_TEST_F(
    'SelectToSpeakNodeNavigationUtilsUnitTest',
    'GetNextNodesInParagraphFromNodeGroupForward', function() {
      // The nodeGroup has four inline text nodes and one static text node.
      // Their starting indexes are 0, 9, 20, 30, and 51.
      const nodeGroup = generateTestNodeGroup();

      let result = NodeNavigationUtils.getNextNodesInParagraphFromNodeGroup(
          nodeGroup, 0 /* charIndex */, constants.Dir.FORWARD /* direction */);
      assertEquals(result.nodes.length, 5);
      assertEquals(result.offset, 0);

      result = NodeNavigationUtils.getNextNodesInParagraphFromNodeGroup(
          nodeGroup, 5 /* charIndex */, constants.Dir.FORWARD /* direction */);
      assertEquals(result.nodes.length, 5);
      assertEquals(result.offset, 5);

      result = NodeNavigationUtils.getNextNodesInParagraphFromNodeGroup(
          nodeGroup, 9 /* charIndex */, constants.Dir.FORWARD /* direction */);
      assertEquals(result.nodes.length, 4);
      assertEquals(result.offset, 0);

      result = NodeNavigationUtils.getNextNodesInParagraphFromNodeGroup(
          nodeGroup, 25 /* charIndex */, constants.Dir.FORWARD /* direction */);
      assertEquals(result.nodes.length, 3);
      assertEquals(result.offset, 5);

      result = NodeNavigationUtils.getNextNodesInParagraphFromNodeGroup(
          nodeGroup, 51 /* charIndex */, constants.Dir.FORWARD /* direction */);
      assertEquals(result.nodes.length, 1);
      assertEquals(result.offset, 0);

      result = NodeNavigationUtils.getNextNodesInParagraphFromNodeGroup(
          nodeGroup, 100 /* charIndex */,
          constants.Dir.FORWARD /* direction */);
      assertEquals(result.nodes.length, 0);
    });

AX_TEST_F(
    'SelectToSpeakNodeNavigationUtilsUnitTest',
    'GetNextNodesInParagraphFromNodeGroupBackward', function() {
      // The nodeGroup has four inline text nodes and one static text node.
      // Their starting indexes are 0, 9, 20, 30, and 51.
      const nodeGroup = generateTestNodeGroup();

      let result = NodeNavigationUtils.getNextNodesInParagraphFromNodeGroup(
          nodeGroup, 0 /* charIndex */, constants.Dir.BACKWARD /* direction */);
      assertEquals(result.nodes.length, 0);

      result = NodeNavigationUtils.getNextNodesInParagraphFromNodeGroup(
          nodeGroup, 5 /* charIndex */, constants.Dir.BACKWARD /* direction */);
      assertEquals(result.nodes.length, 1);
      assertEquals(result.offset, 5);

      result = NodeNavigationUtils.getNextNodesInParagraphFromNodeGroup(
          nodeGroup, 9 /* charIndex */, constants.Dir.BACKWARD /* direction */);
      assertEquals(result.nodes.length, 1);
      assertEquals(result.offset, 9);

      result = NodeNavigationUtils.getNextNodesInParagraphFromNodeGroup(
          nodeGroup, 25 /* charIndex */,
          constants.Dir.BACKWARD /* direction */);
      assertEquals(result.nodes.length, 3);
      assertEquals(result.offset, 5);

      result = NodeNavigationUtils.getNextNodesInParagraphFromNodeGroup(
          nodeGroup, 51 /* charIndex */,
          constants.Dir.BACKWARD /* direction */);
      assertEquals(result.nodes.length, 4);
      assertEquals(result.offset, 20);

      // The charIndex is out of the range of nodeGroup. It will try to get all
      // the content before the nodeGroup. In this case, there is nothing.
      result = NodeNavigationUtils.getNextNodesInParagraphFromNodeGroup(
          nodeGroup, 100 /* charIndex */,
          constants.Dir.BACKWARD /* direction */);
      assertEquals(result.nodes.length, 0);
    });

AX_TEST_F(
    'SelectToSpeakNodeNavigationUtilsUnitTest',
    'GetNextNodesInParagraphFromNodeGroupForwardWithEmptyTail', function() {
      // The nodeGroup consists of three inline text nodes: "Hello", "world ",
      // and " ".
      const nodeGroup = generateTestNodeGroupWithEmptyTail();

      // We can find the non-empty node at this charIndex but there is actually
      // no text content afterwards.
      const nodeWithOffset = ParagraphUtils.findNodeFromNodeGroupByCharIndex(
          nodeGroup, 11 /* charIndex */);
      assertEquals(nodeWithOffset.node.name, 'world ');
      assertEquals(nodeWithOffset.offset, 5);

      let result = NodeNavigationUtils.getNextNodesInParagraphFromNodeGroup(
          nodeGroup, 11 /* charIndex */, constants.Dir.FORWARD /* direction */);
      assertEquals(result.nodes.length, 0);

      // If we decrease the charIndex, we will get the node with 'world ' but
      // not any empty node.
      result = NodeNavigationUtils.getNextNodesInParagraphFromNodeGroup(
          nodeGroup, 10 /* charIndex */, constants.Dir.FORWARD /* direction */);
      assertEquals(result.nodes.length, 1);
      assertEquals(result.nodes[0].name, 'world ');
    });

AX_TEST_F(
    'SelectToSpeakNodeNavigationUtilsUnitTest',
    'GetNextNodesInParagraphFromNodeGroupBackwardWithEmptyHeads', function() {
      // The nodeGroup consists of three inline text nodes: " ", " Hello",
      // "world".
      const nodeGroup = generateTestNodeGroupWithEmptyHead();

      // We can find the non-empty node at this charIndex but there is actually
      // no text content before this position.
      const nodeWithOffset = ParagraphUtils.findNodeFromNodeGroupByCharIndex(
          nodeGroup, 2 /* charIndex */);
      assertEquals(nodeWithOffset.node.name, ' Hello');
      assertEquals(nodeWithOffset.offset, 1);

      let result = NodeNavigationUtils.getNextNodesInParagraphFromNodeGroup(
          nodeGroup, 2 /* charIndex */, constants.Dir.BACKWARD /* direction */);
      assertEquals(result.nodes.length, 0);

      // If we increase the charIndex, we will get the node with ' Hello' but
      // not any empty node.
      result = NodeNavigationUtils.getNextNodesInParagraphFromNodeGroup(
          nodeGroup, 3 /* charIndex */, constants.Dir.BACKWARD /* direction */);
      assertEquals(result.nodes.length, 1);
      assertEquals(result.nodes[0].name, ' Hello');
    });

AX_TEST_F(
    'SelectToSpeakNodeNavigationUtilsUnitTest',
    'GetNextNodesInParagraphFromNodeGroupForwardFromPartialParagraph',
    function() {
      // The nodeGroup consists only one static text node, which is "one". The
      // entire paragraph has three static text nodes: "Sentence", "one",
      // "here".
      const nodeGroup = generateTestNodeGroupFromPartialParagraph();

      // After reading "one", the TTS events will set charIndex to 3. Finding
      // the static text node from the node group will return null.
      const nodeWithOffset = ParagraphUtils.findNodeFromNodeGroupByCharIndex(
          nodeGroup, 3 /* charIndex */);
      assertEquals(nodeWithOffset.node, null);

      const result = NodeNavigationUtils.getNextNodesInParagraphFromNodeGroup(
          nodeGroup, 3 /* charIndex */, constants.Dir.FORWARD /* direction */);
      assertEquals(result.nodes.length, 1);
      assertEquals(result.offset, 0);
    });

AX_TEST_F(
    'SelectToSpeakNodeNavigationUtilsUnitTest',
    'GetNextNodesInParagraphFromNodeGroupBackwardFromPartialParagraph',
    function() {
      // The nodeGroup consists only one static text node, which is "one". The
      // entire paragraph has three static text nodes: "Sentence", "one",
      // "here".
      const nodeGroup = generateTestNodeGroupFromPartialParagraph();

      // After reading "one", the TTS events will set charIndex to 3. Finding
      // the static text node from the node group will return null.
      const nodeWithOffset = ParagraphUtils.findNodeFromNodeGroupByCharIndex(
          nodeGroup, 3 /* charIndex */);
      assertEquals(nodeWithOffset.node, null);

      const result = NodeNavigationUtils.getNextNodesInParagraphFromNodeGroup(
          nodeGroup, 3 /* charIndex */, constants.Dir.BACKWARD /* direction */);
      assertEquals(result.nodes.length, 1);
      assertEquals(result.nodes[0].name, 'Sentence');
      assertEquals(result.offset, 8);
    });

AX_TEST_F(
    'SelectToSpeakNodeNavigationUtilsUnitTest', 'GetNextNodesInParagraph',
    function() {
      const root = createMockNode({role: 'rootWebArea'});
      createMockNode({role: 'paragraph', display: 'block', parent: root, root});
      const paragraph2 = createMockNode(
          {role: 'paragraph', display: 'block', parent: root, root});
      const text1 = createMockNode(
          {role: 'staticText', parent: paragraph2, root, name: 'Line 1'});
      const text2 = createMockNode(
          {role: 'staticText', parent: paragraph2, root, name: 'Line 2'});
      const text3 = createMockNode(
          {role: 'staticText', parent: paragraph2, root, name: 'Line 3'});
      createMockNode({role: 'paragraph', display: 'block', parent: root, root});

      let result = NodeNavigationUtils.getNextNodesInParagraph_(
          text2, constants.Dir.FORWARD);
      assertEquals(result.length, 1);
      assertEquals(result[0], text3);

      result = NodeNavigationUtils.getNextNodesInParagraph_(
          text1, constants.Dir.FORWARD);
      assertEquals(result.length, 2);
      assertEquals(result[0], text2);
      assertEquals(result[1], text3);

      result = NodeNavigationUtils.getNextNodesInParagraph_(
          text3, constants.Dir.FORWARD);
      assertEquals(result.length, 0);

      result = NodeNavigationUtils.getNextNodesInParagraph_(
          text3, constants.Dir.BACKWARD);
      assertEquals(result.length, 2);
      assertEquals(result[0], text1);
      assertEquals(result[1], text2);

      result = NodeNavigationUtils.getNextNodesInParagraph_(
          text2, constants.Dir.BACKWARD);
      assertEquals(result.length, 1);
      assertEquals(result[0], text1);

      result = NodeNavigationUtils.getNextNodesInParagraph_(
          text1, constants.Dir.BACKWARD);
      assertEquals(result.length, 0);
    });

AX_TEST_F(
    'SelectToSpeakNodeNavigationUtilsUnitTest', 'GetNodesForNextSentence',
    function() {
      const root = createMockNode({role: 'rootWebArea'});
      const paragraph1 = createMockNode(
          {role: 'paragraph', display: 'block', parent: root, root});
      const text1 = createMockNode({
        role: 'staticText',
        parent: paragraph1,
        root,
        name: 'Line 1.',
        sentenceStarts: [0],
      });
      const text2 = createMockNode({
        role: 'staticText',
        parent: paragraph1,
        root,
        name: 'Line 2.',
        sentenceStarts: [0],
      });
      const paragraph2 = createMockNode(
          {role: 'paragraph', display: 'block', parent: root, root});
      const text3 = createMockNode({
        role: 'staticText',
        parent: paragraph2,
        root,
        name: 'Line 3.',
        sentenceStarts: [0],
      });
      const text4 = createMockNode({
        role: 'staticText',
        parent: paragraph2,
        root,
        name: 'Line 4.',
        sentenceStarts: [0],
      });
      const nodeGroupForParagraph1 = ParagraphUtils.buildNodeGroup(
          [text1, text2], 0 /* index */, {splitOnLanguage: false});
      const nodeGroupForParagraph2 = ParagraphUtils.buildNodeGroup(
          [text3, text4], 0 /* index */, {splitOnLanguage: false});

      // First paragraph has two sentences.
      assertEquals(nodeGroupForParagraph1.text, 'Line 1. Line 2. ');
      // Second paragraph has another two sentences.
      assertEquals(nodeGroupForParagraph2.text, 'Line 3. Line 4. ');

      let nodes;
      let offset;
      // Navigating forward from the first sentence returns the second sentence.
      ({nodes, offset} = NodeNavigationUtils.getNodesForNextSentence(
           nodeGroupForParagraph1, 0 /* currentCharIndex */,
           constants.Dir.FORWARD,
           () => true /* does not filter any paragraph */));
      assertEquals(nodes.length, 1);
      assertEquals(nodes[0], text2);
      assertEquals(offset, 0);

      // Navigating forward from the second sentence returns the second
      // paragraph. Index 8 points to the word "Line" in the second sentence.
      ({nodes, offset} = NodeNavigationUtils.getNodesForNextSentence(
           nodeGroupForParagraph1, 8 /* currentCharIndex */,
           constants.Dir.FORWARD,
           () => true /* does not filter any paragraph */));
      assertEquals(nodes.length, 2);
      assertEquals(nodes[0], text3);
      assertEquals(nodes[1], text4);
      assertEquals(offset, undefined);

      // Navigating forward from the third sentence returns the fourth
      // sentence. Index 4 points to the word "3" in the third sentence.
      ({nodes, offset} = NodeNavigationUtils.getNodesForNextSentence(
           nodeGroupForParagraph2, 4 /* currentCharIndex */,
           constants.Dir.FORWARD,
           () => true /* does not filter any paragraph */));
      assertEquals(nodes.length, 1);
      assertEquals(nodes[0], text4);
      assertEquals(offset, 0);

      // Navigating forward from the fourth sentence returns an empty result.
      // Index 8 points to the word "Line" in the fourth sentence.
      ({nodes, offset} = NodeNavigationUtils.getNodesForNextSentence(
           nodeGroupForParagraph2, 8 /* currentCharIndex */,
           constants.Dir.FORWARD,
           () => true /* does not filter any paragraph */));
      assertEquals(nodes.length, 0);

      // Navigates forward from second sentence with a pred that filters out
      // paragraph 2. Index 8 points to the word "Line" in the second sentence.
      ({nodes, offset} = NodeNavigationUtils.getNodesForNextSentence(
           nodeGroupForParagraph1, 8 /* currentCharIndex */,
           constants.Dir.FORWARD,
           nodes => !(nodes.find(
               n => n.parent ===
                   paragraph2) /* filter out nodes belong to paragraph 2 */)));
      assertEquals(nodes.length, 0);

      // Navigating backward from the first sentence returns an empty result.
      ({nodes, offset} = NodeNavigationUtils.getNodesForNextSentence(
           nodeGroupForParagraph1, 0 /* currentCharIndex */,
           constants.Dir.BACKWARD,
           () => true /* does not filter any paragraph */));
      assertEquals(nodes.length, 0);

      // Navigating backward from the second sentence returns the first
      // paragraph. Index 8 points to the word "Line" in the second sentence.
      ({nodes, offset} = NodeNavigationUtils.getNodesForNextSentence(
           nodeGroupForParagraph1, 8 /* currentCharIndex */,
           constants.Dir.BACKWARD,
           () => true /* does not filter any paragraph */));
      assertEquals(nodes.length, 2);
      assertEquals(nodes[0], text1);
      assertEquals(nodes[1], text2);
      assertEquals(offset, 0);

      // Navigating backward from the third sentence returns the second
      // sentence. Index 4 points to the word "3" in the third sentence.
      ({nodes, offset} = NodeNavigationUtils.getNodesForNextSentence(
           nodeGroupForParagraph2, 4 /* currentCharIndex */,
           constants.Dir.BACKWARD,
           () => true /* does not filter any paragraph */));
      assertEquals(nodes.length, 1);
      assertEquals(nodes[0], text2);
      assertEquals(offset, 0);

      // Navigating backward from the fourth sentence returns the second
      // paragraph. Index 8 points to the word "Line" in the fourth sentence.
      ({nodes, offset} = NodeNavigationUtils.getNodesForNextSentence(
           nodeGroupForParagraph2, 8 /* currentCharIndex */,
           constants.Dir.BACKWARD,
           () => true /* does not filter any paragraph */));
      assertEquals(nodes.length, 2);
      assertEquals(nodes[0], text3);
      assertEquals(nodes[1], text4);
      assertEquals(offset, 0);

      // Navigates backward from third sentence with a pred that filters out
      // paragraph 1.
      ({nodes, offset} = NodeNavigationUtils.getNodesForNextSentence(
           nodeGroupForParagraph2, 0 /* currentCharIndex */,
           constants.Dir.BACKWARD,
           nodes => !(nodes.find(
               n => n.parent ===
                   paragraph1) /* filter out nodes belong to paragraph 1 */)));
      assertEquals(nodes.length, 0);
    });

AX_TEST_F(
    'SelectToSpeakNodeNavigationUtilsUnitTest',
    'GetNodesForNextSentenceWithChoppedNodes', function() {
      const root = createMockNode({role: 'rootWebArea'});
      const paragraph1 = createMockNode(
          {role: 'paragraph', display: 'block', parent: root, root});
      const text1 = createMockNode({
        role: 'staticText',
        parent: paragraph1,
        root,
        name: 'Line 1.',
        sentenceStarts: [0],
      });
      const text2 = createMockNode({
        role: 'staticText',
        parent: paragraph1,
        root,
        name: 'This sentence',
        sentenceStarts: [0],
      });
      const text3 = createMockNode({
        role: 'staticText',
        parent: paragraph1,
        root,
        name: 'is chopped. Another',
        sentenceStarts: [12],
      });
      const text4 = createMockNode({
        role: 'staticText',
        parent: paragraph1,
        root,
        name: 'chopped',
        sentenceStarts: [],
      });
      const text5 = createMockNode({
        role: 'staticText',
        parent: paragraph1,
        root,
        name: 'sentence.',
        sentenceStarts: [],
      });
      const nodeGroup = ParagraphUtils.buildNodeGroup(
          [text1, text2, text3, text4, text5], 0 /* index */,
          {splitOnLanguage: false});

      // One paragraph has three sentences.
      assertEquals(
          nodeGroup.text,
          'Line 1. This sentence is chopped. Another chopped sentence. ');

      let nodes;
      let offset;
      let result;
      // Navigating forward from the first word returns the content starting
      // from the second sentence.
      ({nodes, offset} = NodeNavigationUtils.getNodesForNextSentence(
           nodeGroup, 0 /* currentCharIndex */, constants.Dir.FORWARD,
           () => true /* does not filter any paragraph */));
      result = ParagraphUtils.buildNodeGroup(
          nodes, 0 /* index */, {splitOnLanguage: false});
      assertEquals(nodes.length, 4);
      assertEquals(offset, 0);
      assertEquals(
          result.text, 'This sentence is chopped. Another chopped sentence. ');

      // Navigating forward from the third word returns the content starting
      // from the third sentence. Index 8 points to the word "This" in the
      // second sentence.
      ({nodes, offset} = NodeNavigationUtils.getNodesForNextSentence(
           nodeGroup, 8 /* currentCharIndex */, constants.Dir.FORWARD,
           () => true /* does not filter any paragraph */));
      result = ParagraphUtils.buildNodeGroup(
          nodes, 0 /* index */, {splitOnLanguage: false});
      assertEquals(nodes.length, 3);
      assertEquals(offset, 12);
      assertEquals(result.text, 'is chopped. Another chopped sentence. ');

      // Navigating backward from the third sentence returns the content
      // starting from the second sentence. Index 42 points to the word
      // "chopped" in the third sentence.
      ({nodes, offset} = NodeNavigationUtils.getNodesForNextSentence(
           nodeGroup, 42 /* currentCharIndex */, constants.Dir.BACKWARD,
           () => true /* does not filter any paragraph */));
      result = ParagraphUtils.buildNodeGroup(
          nodes, 0 /* index */, {splitOnLanguage: false});
      assertEquals(nodes.length, 4);
      assertEquals(offset, 0);
      assertEquals(
          result.text, 'This sentence is chopped. Another chopped sentence. ');

      // Navigating backward from the third sentence returns the content
      // starting from the second sentence. Index 34 points to the word
      // "Another" in the third sentence.
      ({nodes, offset} = NodeNavigationUtils.getNodesForNextSentence(
           nodeGroup, 34 /* currentCharIndex */, constants.Dir.BACKWARD,
           () => true /* does not filter any paragraph */));
      result = ParagraphUtils.buildNodeGroup(
          nodes, 0 /* index */, {splitOnLanguage: false});
      assertEquals(nodes.length, 4);
      assertEquals(offset, 0);
      assertEquals(
          result.text, 'This sentence is chopped. Another chopped sentence. ');
    });

/**
 * Creates a nodeGroup that has an empty tail (i.e., "Hello world  ").
 * @return {!ParagraphUtils.NodeGroup}
 */
function generateTestNodeGroupWithEmptyTail() {
  const root = createMockNode({role: 'rootWebArea'});
  const paragraph =
      createMockNode({role: 'paragraph', display: 'block', parent: root, root});
  const text1 =
      createMockNode({name: 'Hello', role: 'staticText', parent: paragraph});
  const inlineText1 = createMockNode(
      {role: 'inlineTextBox', name: 'Hello', indexInParent: 0, parent: text1});

  const text2 =
      createMockNode({name: 'world  ', role: 'staticText', parent: paragraph});
  const inlineText2 = createMockNode(
      {role: 'inlineTextBox', name: 'world ', indexInParent: 0, parent: text2});
  const inlineText3 = createMockNode(
      {role: 'inlineTextBox', name: ' ', indexInParent: 1, parent: text2});

  return ParagraphUtils.buildNodeGroup(
      [inlineText1, inlineText2, inlineText3], 0,
      false /* do not split on language */);
}

/**
 * Creates a nodeGroup that has an empty head (i.e., "  Hello world").
 * @return {!ParagraphUtils.NodeGroup}
 */
function generateTestNodeGroupWithEmptyHead() {
  const root = createMockNode({role: 'rootWebArea'});
  const paragraph =
      createMockNode({role: 'paragraph', display: 'block', parent: root, root});
  const text1 =
      createMockNode({name: '  Hello', role: 'staticText', parent: paragraph});
  const inlineText1 = createMockNode(
      {role: 'inlineTextBox', name: ' ', indexInParent: 0, parent: text1});
  const inlineText2 = createMockNode(
      {role: 'inlineTextBox', name: ' Hello', indexInParent: 1, parent: text1});

  const text2 =
      createMockNode({name: 'world  ', role: 'staticText', parent: paragraph});
  const inlineText3 = createMockNode(
      {role: 'inlineTextBox', name: 'world', indexInParent: 1, parent: text2});

  return ParagraphUtils.buildNodeGroup(
      [inlineText1, inlineText2, inlineText3], 0,
      false /* do not split on language */);
}

/**
 * Creates a nodeGroup that only has a part of the paragraph (e.g., the "one"
 * in <p>Sentence <span>one</span> here</p>).
 * @return {!ParagraphUtils.NodeGroup}
 */
function generateTestNodeGroupFromPartialParagraph() {
  const root = createMockNode({role: 'rootWebArea'});
  const paragraph =
      createMockNode({role: 'paragraph', display: 'block', parent: root, root});
  const text1 =
      createMockNode({name: 'Sentence', role: 'staticText', parent: paragraph});

  const text2 =
      createMockNode({name: 'one', role: 'staticText', parent: paragraph});

  const text3 =
      createMockNode({name: 'here', role: 'staticText', parent: paragraph});

  return ParagraphUtils.buildNodeGroup(
      [text2], 0, false /* do not split on language */);
}