forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ax_language_detection_unittest.cc
1702 lines (1457 loc) · 51.7 KB
/
ax_language_detection_unittest.cc
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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/accessibility/ax_language_detection.h"
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include "base/command_line.h"
#include "base/test/metrics/histogram_tester.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/accessibility/accessibility_switches.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node.h"
#include "ui/accessibility/ax_tree.h"
namespace ui {
const std::string kTextEnglish =
"This is text created using Google Translate, it is unlikely to be "
"idiomatic in the given target language. This text is only used to "
"test language detection";
const std::string kTextFrench =
"Ce texte a été créé avec Google Translate, il est peu probable qu'il "
"soit idiomatique dans la langue cible indiquée Ce texte est "
"uniquement utilisé pour tester la détection de la langue.";
const std::string kTextGerman =
"Dies ist ein mit Google Translate erstellter Text. Es ist "
"unwahrscheinlich, dass er in der angegebenen Zielsprache idiomatisch "
"ist. Dieser Text wird nur zum Testen der Spracherkennung verwendet.";
const std::string kTextSpanish =
"Este es un texto creado usando Google Translate, es poco probable que sea "
"idiomático en el idioma de destino dado. Este texto solo se usa para "
"probar la detección de idioma.";
// This test fixture is a friend of classes in ax_language_detection.h in order
// to enable testing of internals.
//
// When used with TEST_F, the test body is a subclass of this fixture, so we
// need to re-expose any members through this fixture in order for them to
// be accessible from within the test body.
class AXLanguageDetectionTestFixture : public testing::Test {
public:
AXLanguageDetectionTestFixture() = default;
~AXLanguageDetectionTestFixture() override = default;
AXLanguageDetectionTestFixture(const AXLanguageDetectionTestFixture&) =
delete;
AXLanguageDetectionTestFixture& operator=(
const AXLanguageDetectionTestFixture&) = delete;
protected:
AXLanguageDetectionObserver* getObserver(AXTree& tree) {
return tree.language_detection_manager->language_detection_observer_.get();
}
int get_score(AXTree& tree, const std::string& lang) {
return tree.language_detection_manager->lang_info_stats_.GetScore(lang);
}
// Accessors for testing metric data.
void disable_metric_clearing(AXTree& tree) {
tree.language_detection_manager->lang_info_stats_.disable_metric_clearing_ =
true;
}
int count_detection_attempted(AXTree& tree) const {
return tree.language_detection_manager->lang_info_stats_
.count_detection_attempted_;
}
int count_detection_results(AXTree& tree) const {
return tree.language_detection_manager->lang_info_stats_
.count_detection_results_;
}
int count_labelled(AXTree& tree) const {
return tree.language_detection_manager->lang_info_stats_.count_labelled_;
}
int count_labelled_with_top_result(AXTree& tree) const {
return tree.language_detection_manager->lang_info_stats_
.count_labelled_with_top_result_;
}
int count_overridden(AXTree& tree) const {
return tree.language_detection_manager->lang_info_stats_.count_overridden_;
}
const std::unordered_set<std::string>& unique_top_lang_detected(
AXTree& tree) const {
return tree.language_detection_manager->lang_info_stats_
.unique_top_lang_detected_;
}
};
class AXLanguageDetectionTestStaticContent
: public AXLanguageDetectionTestFixture {
public:
AXLanguageDetectionTestStaticContent() = default;
~AXLanguageDetectionTestStaticContent() override = default;
AXLanguageDetectionTestStaticContent(
const AXLanguageDetectionTestStaticContent&) = delete;
AXLanguageDetectionTestStaticContent& operator=(
const AXLanguageDetectionTestStaticContent&) = delete;
void SetUp() override {
AXLanguageDetectionTestFixture::SetUp();
base::CommandLine::ForCurrentProcess()->AppendSwitch(
::switches::kEnableExperimentalAccessibilityLanguageDetection);
}
};
class AXLanguageDetectionTestDynamicContent
: public AXLanguageDetectionTestStaticContent {
public:
AXLanguageDetectionTestDynamicContent() = default;
~AXLanguageDetectionTestDynamicContent() override = default;
AXLanguageDetectionTestDynamicContent(
const AXLanguageDetectionTestDynamicContent&) = delete;
AXLanguageDetectionTestDynamicContent& operator=(
const AXLanguageDetectionTestDynamicContent&) = delete;
void SetUp() override {
AXLanguageDetectionTestStaticContent::SetUp();
base::CommandLine::ForCurrentProcess()->AppendSwitch(
::switches::kEnableExperimentalAccessibilityLanguageDetectionDynamic);
}
};
TEST(AXLanguageDetectionTest, StaticContentFeatureFlag) {
// TODO(crbug/889370): Remove this test once this feature is stable
EXPECT_FALSE(
::switches::IsExperimentalAccessibilityLanguageDetectionEnabled());
base::CommandLine::ForCurrentProcess()->AppendSwitch(
::switches::kEnableExperimentalAccessibilityLanguageDetection);
EXPECT_TRUE(
::switches::IsExperimentalAccessibilityLanguageDetectionEnabled());
}
TEST(AXLanguageDetectionTest, DynamicContentFeatureFlag) {
// TODO(crbug/889370): Remove this test once this feature is stable
EXPECT_FALSE(
::switches::IsExperimentalAccessibilityLanguageDetectionDynamicEnabled());
base::CommandLine::ForCurrentProcess()->AppendSwitch(
::switches::kEnableExperimentalAccessibilityLanguageDetectionDynamic);
EXPECT_TRUE(
::switches::IsExperimentalAccessibilityLanguageDetectionDynamicEnabled());
}
TEST(AXLanguageDetectionTest, LangAttrInheritanceFeatureFlagOff) {
// Test lang attribute inheritance when feature flag is off.
//
// Lang attribute inheritance is handled by GetLanguage.
//
// Tree:
// 1
// 2 3
// 4
// 5
//
// 1 - English lang attribute
// 2 - French lang attribute
//
// Expected:
// 3 - inherit English from 1
// 4 - inherit French from 2
// 5 - inherit Frnech from 4/2
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(5);
{
AXNodeData& node1 = initial_state.nodes[0];
node1.id = 1;
node1.role = ax::mojom::Role::kGenericContainer;
node1.child_ids.resize(2);
node1.child_ids[0] = 2;
node1.child_ids[1] = 3;
node1.AddStringAttribute(ax::mojom::StringAttribute::kLanguage, "en");
}
{
AXNodeData& node2 = initial_state.nodes[1];
node2.id = 2;
node2.role = ax::mojom::Role::kGenericContainer;
node2.child_ids.resize(1);
node2.child_ids[0] = 4;
node2.AddStringAttribute(ax::mojom::StringAttribute::kLanguage, "fr");
}
{
AXNodeData& node3 = initial_state.nodes[2];
node3.id = 3;
node3.role = ax::mojom::Role::kStaticText;
}
{
AXNodeData& node4 = initial_state.nodes[3];
node4.id = 4;
node4.role = ax::mojom::Role::kStaticText;
node4.child_ids.resize(1);
node4.child_ids[0] = 5;
}
{
AXNodeData& node5 = initial_state.nodes[4];
node5.id = 5;
node5.role = ax::mojom::Role::kInlineTextBox;
}
AXTree tree(initial_state);
ASSERT_NE(tree.language_detection_manager, nullptr);
tree.language_detection_manager->DetectLanguages();
tree.language_detection_manager->LabelLanguages();
{
AXNode* node1 = tree.GetFromId(1);
EXPECT_EQ(node1->GetLanguageInfo(), nullptr);
EXPECT_EQ(node1->GetLanguage(), "en");
}
{
AXNode* node2 = tree.GetFromId(2);
EXPECT_EQ(node2->GetLanguageInfo(), nullptr);
EXPECT_EQ(node2->GetLanguage(), "fr");
}
{
AXNode* node3 = tree.GetFromId(3);
EXPECT_EQ(node3->GetLanguageInfo(), nullptr);
EXPECT_EQ(node3->GetLanguage(), "en");
}
{
AXNode* node4 = tree.GetFromId(4);
EXPECT_EQ(node4->GetLanguageInfo(), nullptr);
EXPECT_EQ(node4->GetLanguage(), "fr");
}
{
AXNode* node5 = tree.GetFromId(5);
EXPECT_EQ(node5->GetLanguageInfo(), nullptr);
EXPECT_EQ(node5->GetLanguage(), "fr");
}
}
TEST(AXLanguageDetectionTest, LangAttrInheritanceFeatureFlagOn) {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
::switches::kEnableExperimentalAccessibilityLanguageDetection);
// Test lang attribute inheritance in the absence of any detected language.
//
// Lang attribute inheritance is handled by the Label step.
//
// Tree:
// 1
// 2 3
// 4
// 5
//
// 1 - English lang attribute
// 2 - French lang attribute
//
// Expected:
// 3 - inherit English from 1
// 4 - inherit French from 2
// 5 - inherit Frnech from 4/2
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(5);
{
AXNodeData& node1 = initial_state.nodes[0];
node1.id = 1;
node1.role = ax::mojom::Role::kGenericContainer;
node1.child_ids.resize(2);
node1.child_ids[0] = 2;
node1.child_ids[1] = 3;
node1.AddStringAttribute(ax::mojom::StringAttribute::kLanguage, "en");
}
{
AXNodeData& node2 = initial_state.nodes[1];
node2.id = 2;
node2.role = ax::mojom::Role::kGenericContainer;
node2.child_ids.resize(1);
node2.child_ids[0] = 4;
node2.AddStringAttribute(ax::mojom::StringAttribute::kLanguage, "fr");
}
{
AXNodeData& node3 = initial_state.nodes[2];
node3.id = 3;
node3.role = ax::mojom::Role::kStaticText;
}
{
AXNodeData& node4 = initial_state.nodes[3];
node4.id = 4;
node4.role = ax::mojom::Role::kStaticText;
node4.child_ids.resize(1);
node4.child_ids[0] = 5;
}
{
AXNodeData& node5 = initial_state.nodes[4];
node5.id = 5;
node5.role = ax::mojom::Role::kInlineTextBox;
}
AXTree tree(initial_state);
ASSERT_NE(tree.language_detection_manager, nullptr);
tree.language_detection_manager->DetectLanguages();
tree.language_detection_manager->LabelLanguages();
{
AXNode* node1 = tree.GetFromId(1);
// No detection for non text nodes.
EXPECT_EQ(node1->GetLanguageInfo(), nullptr);
EXPECT_EQ(node1->GetLanguage(), "en");
}
{
AXNode* node2 = tree.GetFromId(2);
EXPECT_EQ(node2->GetLanguageInfo(), nullptr);
EXPECT_EQ(node2->GetLanguage(), "fr");
}
{
AXNode* node3 = tree.GetFromId(3);
// Inherited languages are not stored in lang info.
EXPECT_EQ(node3->GetLanguageInfo(), nullptr);
EXPECT_EQ(node3->GetLanguage(), "en");
}
{
AXNode* node4 = tree.GetFromId(4);
// Inherited languages are not stored in lang info.
EXPECT_EQ(node4->GetLanguageInfo(), nullptr);
EXPECT_EQ(node4->GetLanguage(), "fr");
}
{
AXNode* node5 = tree.GetFromId(5);
// Inherited languages are not stored in lang info.
EXPECT_EQ(node5->GetLanguageInfo(), nullptr);
EXPECT_EQ(node5->GetLanguage(), "fr");
}
}
// Tests that AXNode::GetLanguage() terminates when there is no lang attribute.
TEST_F(AXLanguageDetectionTestStaticContent, GetLanguageBoringTree) {
// This test checks the behaviour of Detect, Label, and GetLanguage on a
// 'boring' tree.
//
// The tree built here contains no lang attributes, nor does it contain any
// text to perform detection on.
//
// Tree:
// 1
// 2 3
// 4
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(4);
initial_state.nodes[0].id = 1;
initial_state.nodes[0].child_ids.resize(2);
initial_state.nodes[0].child_ids[0] = 2;
initial_state.nodes[0].child_ids[1] = 3;
initial_state.nodes[1].id = 2;
initial_state.nodes[1].child_ids.resize(1);
initial_state.nodes[1].child_ids[0] = 4;
initial_state.nodes[2].id = 3;
initial_state.nodes[3].id = 4;
AXTree tree(initial_state);
ASSERT_NE(tree.language_detection_manager, nullptr);
tree.language_detection_manager->DetectLanguages();
tree.language_detection_manager->LabelLanguages();
// Check that tree parenting conforms to expected shape.
AXNode* node1 = tree.GetFromId(1);
EXPECT_EQ(node1->parent(), nullptr);
AXNode* node2 = tree.GetFromId(2);
ASSERT_EQ(node2->parent(), node1);
EXPECT_EQ(node2->parent()->parent(), nullptr);
AXNode* node3 = tree.GetFromId(3);
ASSERT_EQ(node3->parent(), node1);
EXPECT_EQ(node3->parent()->parent(), nullptr);
AXNode* node4 = tree.GetFromId(4);
ASSERT_EQ(node4->parent(), node2);
ASSERT_EQ(node4->parent()->parent(), node1);
EXPECT_EQ(node4->parent()->parent()->parent(), nullptr);
EXPECT_EQ(node1->GetLanguage(), "");
EXPECT_EQ(node2->GetLanguage(), "");
EXPECT_EQ(node3->GetLanguage(), "");
EXPECT_EQ(node4->GetLanguage(), "");
}
TEST_F(AXLanguageDetectionTestStaticContent, Basic) {
// Tree:
// 1
// 2 3
// 4
// 5
//
// 1 - German lang attribute, no text
// 2 - French lang attribute, no text
// 3 - no attribute, French text
// 4 - no attribute, English text
// 5 - no attribute, no text
//
// Expected:
// 3 - French detected
// 4 - English detected
// 5 - inherit English from 4
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(5);
{
AXNodeData& node1 = initial_state.nodes[0];
node1.id = 1;
node1.role = ax::mojom::Role::kGenericContainer;
node1.child_ids.resize(2);
node1.child_ids[0] = 2;
node1.child_ids[1] = 3;
node1.AddStringAttribute(ax::mojom::StringAttribute::kLanguage, "de");
}
{
AXNodeData& node2 = initial_state.nodes[1];
node2.id = 2;
node2.role = ax::mojom::Role::kGenericContainer;
node2.child_ids.resize(1);
node2.child_ids[0] = 4;
node2.AddStringAttribute(ax::mojom::StringAttribute::kLanguage, "fr");
}
{
AXNodeData& node3 = initial_state.nodes[2];
node3.id = 3;
node3.role = ax::mojom::Role::kStaticText;
node3.AddStringAttribute(ax::mojom::StringAttribute::kName, kTextFrench);
}
{
AXNodeData& node4 = initial_state.nodes[3];
node4.id = 4;
node4.child_ids.resize(1);
node4.child_ids[0] = 5;
node4.role = ax::mojom::Role::kStaticText;
node4.AddStringAttribute(ax::mojom::StringAttribute::kName, kTextEnglish);
}
{
AXNodeData& node5 = initial_state.nodes[4];
node5.id = 5;
node5.role = ax::mojom::Role::kInlineTextBox;
node5.AddStringAttribute(ax::mojom::StringAttribute::kName, kTextEnglish);
}
AXTree tree(initial_state);
ASSERT_NE(tree.language_detection_manager, nullptr);
tree.language_detection_manager->DetectLanguages();
tree.language_detection_manager->LabelLanguages();
{
AXNode* node1 = tree.GetFromId(1);
// node1 is not a text node, so no lang info should be attached.
EXPECT_EQ(node1->GetLanguageInfo(), nullptr);
EXPECT_EQ(node1->GetLanguage(), "de");
}
{
AXNode* node2 = tree.GetFromId(2);
// node2 is not a text node, so no lang info should be attached.
EXPECT_EQ(node2->GetLanguageInfo(), nullptr);
EXPECT_EQ(node2->GetLanguage(), "fr");
}
{
AXNode* node3 = tree.GetFromId(3);
EXPECT_TRUE(node3->IsText());
EXPECT_NE(node3->GetLanguageInfo(), nullptr);
EXPECT_EQ(node3->GetLanguage(), "fr");
}
{
AXNode* node4 = tree.GetFromId(4);
EXPECT_TRUE(node4->IsText());
EXPECT_NE(node4->GetLanguageInfo(), nullptr);
EXPECT_EQ(node4->GetLanguage(), "en");
}
{
AXNode* node5 = tree.GetFromId(5);
EXPECT_TRUE(node5->IsText());
// Inherited languages are not stored in lang info.
EXPECT_EQ(node5->GetLanguageInfo(), nullptr);
EXPECT_EQ(node5->GetLanguage(), "en");
}
}
TEST_F(AXLanguageDetectionTestStaticContent, MetricCollection) {
// Tree:
// 1
// 2 3 4 5 6
//
// 1 - German lang attribute, no text
// 2 - no attribute, German text
// 3 - no attribute, French text
// 4 - no attribute, English text
// 5 - no attribute, Spanish text
// 6 - no attribute, text too short to get detection results.
//
// Expected:
// 2 - German detected
// 3 - French detected
// 4 - English detected
// 5 - Spanish detected
// 6 - too short for results
//
// only 3 of these languages can be labelled due to heuristics.
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(6);
{
AXNodeData& node1 = initial_state.nodes[0];
node1.id = 1;
node1.role = ax::mojom::Role::kGenericContainer;
node1.child_ids.resize(5);
node1.child_ids[0] = 2;
node1.child_ids[1] = 3;
node1.child_ids[2] = 4;
node1.child_ids[3] = 5;
node1.child_ids[4] = 6;
node1.AddStringAttribute(ax::mojom::StringAttribute::kLanguage, "de");
}
{
AXNodeData& node2 = initial_state.nodes[1];
node2.id = 2;
node2.role = ax::mojom::Role::kStaticText;
node2.AddStringAttribute(ax::mojom::StringAttribute::kName, kTextGerman);
}
{
AXNodeData& node3 = initial_state.nodes[2];
node3.id = 3;
node3.role = ax::mojom::Role::kStaticText;
node3.AddStringAttribute(ax::mojom::StringAttribute::kName, kTextFrench);
}
{
AXNodeData& node4 = initial_state.nodes[3];
node4.id = 4;
node4.role = ax::mojom::Role::kStaticText;
node4.AddStringAttribute(ax::mojom::StringAttribute::kName, kTextEnglish);
}
{
AXNodeData& node5 = initial_state.nodes[4];
node5.id = 5;
node5.role = ax::mojom::Role::kStaticText;
node5.AddStringAttribute(ax::mojom::StringAttribute::kName, kTextSpanish);
}
{
AXNodeData& node6 = initial_state.nodes[5];
node6.id = 6;
node6.role = ax::mojom::Role::kStaticText;
node6.AddStringAttribute(ax::mojom::StringAttribute::kName,
"too short for detection.");
}
AXTree tree(initial_state);
ASSERT_NE(tree.language_detection_manager, nullptr);
// Specifically disable clearing of metrics.
disable_metric_clearing(tree);
// Our histogram for testing.
base::HistogramTester histograms;
tree.language_detection_manager->DetectLanguages();
tree.language_detection_manager->LabelLanguages();
// All 4 of our languages should have been detected for one node each, scoring
// a maximum 3 points.
EXPECT_EQ(3, get_score(tree, "de"));
EXPECT_EQ(3, get_score(tree, "en"));
EXPECT_EQ(3, get_score(tree, "fr"));
EXPECT_EQ(3, get_score(tree, "es"));
// 5 nodes (2, 3, 4, 5, 6) should have had detection attempted.
EXPECT_EQ(5, count_detection_attempted(tree));
histograms.ExpectUniqueSample(
"Accessibility.LanguageDetection.CountDetectionAttempted", 5, 1);
// 4 nodes (2, 3, 4, 5) should have had detection results.
EXPECT_EQ(4, count_detection_results(tree));
// 5 nodes attempted, 4 got results = 4*100/5 = 80%
histograms.ExpectUniqueSample(
"Accessibility.LanguageDetection.PercentageLanguageDetected", 80, 1);
// 3 nodes (any of 2, 3, 4, 5) should have been labelled.
EXPECT_EQ(3, count_labelled(tree));
histograms.ExpectUniqueSample("Accessibility.LanguageDetection.CountLabelled",
3, 1);
// 3 nodes (any of 2, 3, 4, 5) should have been given top label.
EXPECT_EQ(3, count_labelled_with_top_result(tree));
// 3 nodes labelled, all of them given top result = 100%.
histograms.ExpectUniqueSample(
"Accessibility.LanguageDetection.PercentageLabelledWithTop", 100, 1);
// 3 nodes (3, 4, 5) should have been labelled to disagree with node1 author
// provided language.
EXPECT_EQ(3, count_overridden(tree));
// 3 nodes labelled, all 3 disagree with node1 = 100%.
histograms.ExpectUniqueSample(
"Accessibility.LanguageDetection.PercentageOverridden", 100, 1);
// There should be 4 unique languages (de, en, fr, es).
{
const auto& top_lang = unique_top_lang_detected(tree);
const std::unordered_set<std::string> expected_top_lang = {"de", "en", "es",
"fr"};
EXPECT_EQ(top_lang, expected_top_lang);
}
histograms.ExpectUniqueSample("Accessibility.LanguageDetection.LangsPerPage",
4, 1);
}
TEST_F(AXLanguageDetectionTestStaticContent, DetectOnly) {
// This tests a Detect step without any matching Label step.
//
// Tree:
// 1
// 2 3
// 4
// 5
//
// 1 - German lang attribute, no text
// 2 - French lang attribute, no text
// 3 - no attribute, French text
// 4 - no attribute, English text
// 5 - no attribute, no text
//
// Expected:
// 3 - French detected, never labelled, so still inherits German from 1
// 4 - English detected, never labelled, so still inherits French from 2
// 5 - English inherited from 4, still inherits French from 4
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(5);
{
AXNodeData& node1 = initial_state.nodes[0];
node1.id = 1;
node1.role = ax::mojom::Role::kGenericContainer;
node1.child_ids.resize(2);
node1.child_ids[0] = 2;
node1.child_ids[1] = 3;
node1.AddStringAttribute(ax::mojom::StringAttribute::kLanguage, "de");
}
{
AXNodeData& node2 = initial_state.nodes[1];
node2.id = 2;
node2.role = ax::mojom::Role::kGenericContainer;
node2.child_ids.resize(1);
node2.child_ids[0] = 4;
node2.AddStringAttribute(ax::mojom::StringAttribute::kLanguage, "fr");
}
{
AXNodeData& node3 = initial_state.nodes[2];
node3.id = 3;
node3.role = ax::mojom::Role::kStaticText;
node3.AddStringAttribute(ax::mojom::StringAttribute::kName, kTextFrench);
}
{
AXNodeData& node4 = initial_state.nodes[3];
node4.id = 4;
node4.child_ids.resize(1);
node4.child_ids[0] = 5;
node4.role = ax::mojom::Role::kStaticText;
node4.AddStringAttribute(ax::mojom::StringAttribute::kName, kTextEnglish);
}
{
AXNodeData& node5 = initial_state.nodes[4];
node5.id = 5;
node5.role = ax::mojom::Role::kInlineTextBox;
node5.AddStringAttribute(ax::mojom::StringAttribute::kName, kTextEnglish);
}
AXTree tree(initial_state);
ASSERT_NE(tree.language_detection_manager, nullptr);
tree.language_detection_manager->DetectLanguages();
// Purposefully not calling Label so we can test Detect in isolation.
{
AXNode* node1 = tree.GetFromId(1);
// node1 is not a text node, so no lang info should be attached.
EXPECT_EQ(node1->GetLanguageInfo(), nullptr);
EXPECT_EQ(node1->GetLanguage(), "de");
}
{
AXNode* node2 = tree.GetFromId(2);
// node2 is not a text node, so no lang info should be attached.
EXPECT_EQ(node2->GetLanguageInfo(), nullptr);
EXPECT_EQ(node2->GetLanguage(), "fr");
}
{
AXNode* node3 = tree.GetFromId(3);
EXPECT_TRUE(node3->IsText());
ASSERT_NE(node3->GetLanguageInfo(), nullptr);
ASSERT_GT(node3->GetLanguageInfo()->detected_languages.size(), (unsigned)0);
ASSERT_EQ(node3->GetLanguageInfo()->detected_languages[0], "fr");
EXPECT_TRUE(node3->GetLanguageInfo()->language.empty());
EXPECT_EQ(node3->GetLanguage(), "de");
}
{
AXNode* node4 = tree.GetFromId(4);
EXPECT_TRUE(node4->IsText());
ASSERT_NE(node4->GetLanguageInfo(), nullptr);
ASSERT_GT(node4->GetLanguageInfo()->detected_languages.size(), (unsigned)0);
ASSERT_EQ(node4->GetLanguageInfo()->detected_languages[0], "en");
EXPECT_TRUE(node4->GetLanguageInfo()->language.empty());
EXPECT_EQ(node4->GetLanguage(), "fr");
}
{
AXNode* node5 = tree.GetFromId(5);
EXPECT_TRUE(node5->IsText());
// Inherited languages are not stored in lang info.
ASSERT_EQ(node5->GetLanguageInfo(), nullptr);
EXPECT_EQ(node5->GetLanguage(), "fr");
}
}
TEST_F(AXLanguageDetectionTestStaticContent, kLanguageUntouched) {
// This test is to ensure that the kLanguage string attribute is not updated
// during language detection and labelling, even when it disagrees with the
// detected language.
// Built tree:
// 1
// 2 3
//
// 1 - German lang attribute, no text
// 2 - English lang attribute, French text
// 3 - French lang attribute, English text
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(3);
{
AXNodeData& node1 = initial_state.nodes[0];
node1.id = 1;
node1.role = ax::mojom::Role::kGenericContainer;
node1.child_ids.resize(2);
node1.child_ids[0] = 2;
node1.child_ids[1] = 3;
node1.AddStringAttribute(ax::mojom::StringAttribute::kLanguage, "de");
}
{
AXNodeData& node2 = initial_state.nodes[1];
node2.id = 2;
node2.role = ax::mojom::Role::kStaticText;
node2.AddStringAttribute(ax::mojom::StringAttribute::kLanguage, "en");
node2.AddStringAttribute(ax::mojom::StringAttribute::kName, kTextFrench);
}
{
AXNodeData& node3 = initial_state.nodes[2];
node3.id = 3;
node3.role = ax::mojom::Role::kStaticText;
node3.AddStringAttribute(ax::mojom::StringAttribute::kLanguage, "fr");
node3.AddStringAttribute(ax::mojom::StringAttribute::kName, kTextEnglish);
}
AXTree tree(initial_state);
ASSERT_NE(tree.language_detection_manager, nullptr);
tree.language_detection_manager->DetectLanguages();
tree.language_detection_manager->LabelLanguages();
{
AXNode* node1 = tree.GetFromId(1);
ASSERT_EQ(node1->GetLanguageInfo(), nullptr);
EXPECT_EQ(node1->GetLanguage(), "de");
}
{
// French should be detected, original English attr should be untouched.
AXNode* node2 = tree.GetFromId(2);
ASSERT_NE(node2->GetLanguageInfo(), nullptr);
EXPECT_EQ(node2->GetLanguageInfo()->language, "fr");
EXPECT_EQ(node2->GetStringAttribute(ax::mojom::StringAttribute::kLanguage),
"en");
EXPECT_EQ(node2->GetLanguage(), "fr");
}
{
// English should be detected, original French attr should be untouched.
AXNode* node3 = tree.GetFromId(3);
ASSERT_NE(node3->GetLanguageInfo(), nullptr);
EXPECT_EQ(node3->GetLanguageInfo()->language, "en");
EXPECT_EQ(node3->GetStringAttribute(ax::mojom::StringAttribute::kLanguage),
"fr");
EXPECT_EQ(node3->GetLanguage(), "en");
}
}
// Test RegisterLanguageDetectionObserver correctly respects the feature flag.
TEST_F(AXLanguageDetectionTestFixture, ObserverRegistrationObeysFlag) {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
::switches::kEnableExperimentalAccessibilityLanguageDetection);
// Construct empty tree and check initialisation.
AXTree tree;
ASSERT_NE(tree.language_detection_manager, nullptr);
ASSERT_EQ(getObserver(tree), nullptr);
// Try registration without enabling Dynamic feature flag, should be a no-op.
tree.language_detection_manager->RegisterLanguageDetectionObserver();
ASSERT_EQ(getObserver(tree), nullptr);
base::CommandLine::ForCurrentProcess()->AppendSwitch(
::switches::kEnableExperimentalAccessibilityLanguageDetectionDynamic);
// Try registration without turning on Dynamic feature flag, this should
// do nothing.
tree.language_detection_manager->RegisterLanguageDetectionObserver();
// Check our observer was constructed.
ASSERT_NE(getObserver(tree), nullptr);
// Check our observer was registered in our tree.
ASSERT_TRUE(tree.HasObserver(getObserver(tree)));
}
TEST_F(AXLanguageDetectionTestDynamicContent, Basic) {
// Tree:
// 1
// 2
//
// 1 - kStaticText - English text.
// 2 - kInlineTextBox - English text.
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(2);
// TODO(chrishall): Create more realistic kStaticText with multiple
// kInlineTextBox(es) children. Look at the real-world behaviour of
// kStaticText, kInlineText and kLineBreak around empty divs and empty lines
// within paragraphs of text.
{
AXNodeData& node1 = initial_state.nodes[0];
node1.id = 1;
node1.role = ax::mojom::Role::kStaticText;
node1.AddStringAttribute(ax::mojom::StringAttribute::kName, kTextEnglish);
node1.child_ids.resize(1);
node1.child_ids[0] = 2;
}
{
AXNodeData& node2 = initial_state.nodes[1];
node2.id = 2;
node2.role = ax::mojom::Role::kInlineTextBox;
node2.AddStringAttribute(ax::mojom::StringAttribute::kName, kTextEnglish);
}
AXTree tree(initial_state);
ASSERT_NE(tree.language_detection_manager, nullptr);
// Manually run initial language detection and labelling.
tree.language_detection_manager->DetectLanguages();
tree.language_detection_manager->LabelLanguages();
// Quickly verify "before" state
{
AXNode* node1 = tree.GetFromId(1);
ASSERT_NE(node1, nullptr);
ASSERT_NE(node1->GetLanguageInfo(), nullptr);
ASSERT_EQ(node1->GetLanguage(), "en");
AXNode* node2 = tree.GetFromId(2);
ASSERT_NE(node2, nullptr);
// Inherited language not stored in lang info.
ASSERT_EQ(node2->GetLanguageInfo(), nullptr);
// Should still inherit language from parent.
ASSERT_EQ(node2->GetLanguage(), "en");
}
// Manually register observer.
AXLanguageDetectionObserver observer(&tree);
// Observer constructor is responsible for attaching itself to tree.
ASSERT_TRUE(tree.HasObserver(&observer));
// Dynamic update
//
// New tree:
// 1
// 2
//
// 1 - Text changed to German.
// 2 - Text changed to German.
AXTreeUpdate update_state;
update_state.root_id = 1;
update_state.nodes.resize(2);
// Change text to German.
{
AXNodeData& node1 = update_state.nodes[0];
node1.id = 1;
node1.role = ax::mojom::Role::kStaticText;
node1.AddStringAttribute(ax::mojom::StringAttribute::kName, kTextGerman);
node1.child_ids.resize(1);
node1.child_ids[0] = 2;
}
{
AXNodeData& node2 = update_state.nodes[1];
node2.id = 2;
node2.role = ax::mojom::Role::kInlineTextBox;
node2.AddStringAttribute(ax::mojom::StringAttribute::kName, kTextGerman);
}
// Perform update.
ASSERT_TRUE(tree.Unserialize(update_state));
// Check language detection was re-run on new content.
{
AXNode* node1 = tree.GetFromId(1);
ASSERT_NE(node1, nullptr);
ASSERT_NE(node1->GetLanguageInfo(), nullptr);
ASSERT_EQ(node1->GetLanguage(), "de");
}
{
AXNode* node2 = tree.GetFromId(2);
ASSERT_NE(node2, nullptr);
// Inherited language not stored in lang info.
ASSERT_EQ(node2->GetLanguageInfo(), nullptr);
// Should inherit new language from parent.
ASSERT_EQ(node2->GetLanguage(), "de");
}
}
TEST_F(AXLanguageDetectionTestDynamicContent, MetricCollection) {
// Tree:
// 1
// 2 3
//
// 1 - kGenericContainer, French lang attribute.
// 2 - kStaticText - English text.
// 3 - kSTaticText - German text.
AXTreeUpdate initial_state;
initial_state.root_id = 1;
initial_state.nodes.resize(3);
// TODO(chrishall): Create more realistic kStaticText with multiple
// kInlineTextBox(es) children. Look at the real-world behaviour of
// kStaticText, kInlineText and kLineBreak around empty divs and empty lines
// within paragraphs of text.
{
AXNodeData& node1 = initial_state.nodes[0];