-
Notifications
You must be signed in to change notification settings - Fork 30.6k
Expand file tree
/
Copy pathbasic_test.dart
More file actions
1983 lines (1826 loc) · 70.2 KB
/
Copy pathbasic_test.dart
File metadata and controls
1983 lines (1826 loc) · 70.2 KB
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 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file is run as part of a reduced test set in CI on Mac and Windows
// machines.
@Tags(<String>['reduced-test-set'])
library;
import 'dart:math' as math;
import 'dart:ui' as ui;
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'button_tester.dart';
import 'semantics_tester.dart';
import 'widgets_app_tester.dart';
void main() {
group('RawImage', () {
testWidgets('properties', (WidgetTester tester) async {
final ui.Image image1 = (await tester.runAsync<ui.Image>(() => createTestImage()))!;
addTearDown(image1.dispose);
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: RawImage(image: image1),
),
);
final RenderImage renderObject = tester.firstRenderObject<RenderImage>(find.byType(RawImage));
// Expect default values
expect(renderObject.image!.isCloneOf(image1), true);
expect(renderObject.debugImageLabel, null);
expect(renderObject.width, null);
expect(renderObject.height, null);
expect(renderObject.scale, 1.0);
expect(renderObject.color, null);
expect(renderObject.opacity, null);
expect(renderObject.colorBlendMode, null);
expect(renderObject.fit, null);
expect(renderObject.alignment, Alignment.center);
expect(renderObject.repeat, ImageRepeat.noRepeat);
expect(renderObject.centerSlice, null);
expect(renderObject.matchTextDirection, false);
expect(renderObject.invertColors, false);
expect(renderObject.filterQuality, FilterQuality.medium);
expect(renderObject.isAntiAlias, false);
final ui.Image image2 = (await tester.runAsync<ui.Image>(
() => createTestImage(width: 2, height: 2),
))!;
addTearDown(image2.dispose);
const debugImageLabel = 'debugImageLabel';
const double width = 1;
const double height = 1;
const scale = 2.0;
const color = Color(0xFF000000);
const Animation<double> opacity = AlwaysStoppedAnimation<double>(0.0);
const BlendMode colorBlendMode = BlendMode.difference;
const BoxFit fit = BoxFit.contain;
const AlignmentGeometry alignment = Alignment.topCenter;
const ImageRepeat repeat = ImageRepeat.repeat;
const centerSlice = Rect.fromLTWH(0, 0, width, height);
const matchTextDirection = true;
const invertColors = true;
const FilterQuality filterQuality = FilterQuality.high;
const isAntiAlias = true;
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: RawImage(
image: image2,
debugImageLabel: debugImageLabel,
width: width,
height: height,
scale: scale,
color: color,
opacity: opacity,
colorBlendMode: colorBlendMode,
fit: fit,
alignment: alignment,
repeat: repeat,
centerSlice: centerSlice,
matchTextDirection: matchTextDirection,
invertColors: invertColors,
filterQuality: filterQuality,
isAntiAlias: isAntiAlias,
),
),
);
expect(renderObject.image!.isCloneOf(image2), true);
expect(renderObject.debugImageLabel, debugImageLabel);
expect(renderObject.width, width);
expect(renderObject.height, height);
expect(renderObject.scale, scale);
expect(renderObject.color, color);
expect(renderObject.opacity, opacity);
expect(renderObject.colorBlendMode, colorBlendMode);
expect(renderObject.fit, fit);
expect(renderObject.alignment, alignment);
expect(renderObject.repeat, repeat);
expect(renderObject.centerSlice, centerSlice);
expect(renderObject.matchTextDirection, matchTextDirection);
expect(renderObject.invertColors, invertColors);
expect(renderObject.filterQuality, filterQuality);
expect(renderObject.isAntiAlias, isAntiAlias);
});
});
group('PhysicalShape', () {
testWidgets('properties', (WidgetTester tester) async {
await tester.pumpWidget(
const PhysicalShape(
clipper: ShapeBorderClipper(shape: CircleBorder()),
elevation: 2.0,
color: Color(0xFF0000FF),
shadowColor: Color(0xFF00FF00),
),
);
final RenderPhysicalShape renderObject = tester.renderObject(find.byType(PhysicalShape));
expect(renderObject.clipper, const ShapeBorderClipper(shape: CircleBorder()));
expect(renderObject.color, const Color(0xFF0000FF));
expect(renderObject.shadowColor, const Color(0xFF00FF00));
expect(renderObject.elevation, 2.0);
});
testWidgets('hit test', (WidgetTester tester) async {
await tester.pumpWidget(
PhysicalShape(
clipper: const ShapeBorderClipper(shape: CircleBorder()),
elevation: 2.0,
color: const Color(0xFF0000FF),
shadowColor: const Color(0xFF00FF00),
child: Container(color: const Color(0xFF0000FF)),
),
);
final RenderPhysicalShape renderPhysicalShape = tester.renderObject(
find.byType(PhysicalShape),
);
// The viewport is 800x600, the CircleBorder is centered and fits
// the shortest edge, so we get a circle of radius 300, centered at
// (400, 300).
//
// We test by sampling a few points around the left-most point of the
// circle (100, 300).
expect(tester.hitTestOnBinding(const Offset(99.0, 300.0)), doesNotHit(renderPhysicalShape));
expect(tester.hitTestOnBinding(const Offset(100.0, 300.0)), hits(renderPhysicalShape));
expect(tester.hitTestOnBinding(const Offset(100.0, 299.0)), doesNotHit(renderPhysicalShape));
expect(tester.hitTestOnBinding(const Offset(100.0, 301.0)), doesNotHit(renderPhysicalShape));
});
});
group('FractionalTranslation', () {
testWidgets('hit test - entirely inside the bounding box', (WidgetTester tester) async {
final GlobalKey key1 = GlobalKey();
var pointerDown = false;
await tester.pumpWidget(
Center(
child: FractionalTranslation(
translation: Offset.zero,
child: Listener(
onPointerDown: (PointerDownEvent event) {
pointerDown = true;
},
child: SizedBox(
key: key1,
width: 100.0,
height: 100.0,
child: Container(color: const Color(0xFF0000FF)),
),
),
),
),
);
expect(pointerDown, isFalse);
await tester.tap(find.byKey(key1));
expect(pointerDown, isTrue);
});
testWidgets('hit test - partially inside the bounding box', (WidgetTester tester) async {
final GlobalKey key1 = GlobalKey();
var pointerDown = false;
await tester.pumpWidget(
Center(
child: FractionalTranslation(
translation: const Offset(0.5, 0.5),
child: Listener(
onPointerDown: (PointerDownEvent event) {
pointerDown = true;
},
child: SizedBox(
key: key1,
width: 100.0,
height: 100.0,
child: Container(color: const Color(0xFF0000FF)),
),
),
),
),
);
expect(pointerDown, isFalse);
await tester.tap(find.byKey(key1));
expect(pointerDown, isTrue);
});
testWidgets('hit test - completely outside the bounding box', (WidgetTester tester) async {
final GlobalKey key1 = GlobalKey();
var pointerDown = false;
await tester.pumpWidget(
Center(
child: FractionalTranslation(
translation: const Offset(1.0, 1.0),
child: Listener(
onPointerDown: (PointerDownEvent event) {
pointerDown = true;
},
child: SizedBox(
key: key1,
width: 100.0,
height: 100.0,
child: Container(color: const Color(0xFF0000FF)),
),
),
),
),
);
expect(pointerDown, isFalse);
await tester.tap(find.byKey(key1));
expect(pointerDown, isTrue);
});
testWidgets('semantics bounds are updated', (WidgetTester tester) async {
final GlobalKey fractionalTranslationKey = GlobalKey();
final GlobalKey textKey = GlobalKey();
var offset = const Offset(0.4, 0.4);
await tester.pumpWidget(
StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return Directionality(
textDirection: TextDirection.ltr,
child: Center(
child: Semantics(
explicitChildNodes: true,
child: FractionalTranslation(
key: fractionalTranslationKey,
translation: offset,
child: GestureDetector(
onTap: () {
setState(() {
offset = const Offset(0.8, 0.8);
});
},
child: SizedBox.square(dimension: 100.0, child: Text('foo', key: textKey)),
),
),
),
),
);
},
),
);
expect(
tester.getSemantics(find.byKey(textKey)).transform,
Matrix4(
3.0,
0.0,
0.0,
0.0,
0.0,
3.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
1170.0,
870.0,
0.0,
1.0,
),
);
await tester.tap(
find.byKey(fractionalTranslationKey),
warnIfMissed: false,
); // RenderFractionalTranslation can't be hit
await tester.pump();
expect(
tester.getSemantics(find.byKey(textKey)).transform,
Matrix4(
3.0,
0.0,
0.0,
0.0,
0.0,
3.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
1290.0,
990.0,
0.0,
1.0,
),
);
});
testWidgets('FractionalTranslation does not crash at zero area', (WidgetTester tester) async {
tester.view.physicalSize = Size.zero;
const offset = Offset(0.4, 0.4);
addTearDown(tester.view.reset);
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: Center(child: FractionalTranslation(translation: offset)),
),
);
expect(tester.getSize(find.byType(FractionalTranslation)), Size.zero);
});
});
group('Semantics', () {
testWidgets('Semantics can set attributed Text', (WidgetTester tester) async {
final key = UniqueKey();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Semantics(
key: key,
attributedLabel: AttributedString(
'label',
attributes: <StringAttribute>[
SpellOutStringAttribute(range: const TextRange(start: 0, end: 5)),
],
),
attributedValue: AttributedString(
'value',
attributes: <StringAttribute>[
LocaleStringAttribute(
range: const TextRange(start: 0, end: 5),
locale: const Locale('en', 'MX'),
),
],
),
attributedHint: AttributedString(
'hint',
attributes: <StringAttribute>[
SpellOutStringAttribute(range: const TextRange(start: 1, end: 2)),
],
),
child: const Placeholder(),
),
),
);
final AttributedString attributedLabel = tester.getSemantics(find.byKey(key)).attributedLabel;
expect(attributedLabel.string, 'label');
expect(attributedLabel.attributes.length, 1);
expect(attributedLabel.attributes[0] is SpellOutStringAttribute, isTrue);
expect(attributedLabel.attributes[0].range, const TextRange(start: 0, end: 5));
final AttributedString attributedValue = tester.getSemantics(find.byKey(key)).attributedValue;
expect(attributedValue.string, 'value');
expect(attributedValue.attributes.length, 1);
expect(attributedValue.attributes[0] is LocaleStringAttribute, isTrue);
final valueLocale = attributedValue.attributes[0] as LocaleStringAttribute;
expect(valueLocale.range, const TextRange(start: 0, end: 5));
expect(valueLocale.locale, const Locale('en', 'MX'));
final AttributedString attributedHint = tester.getSemantics(find.byKey(key)).attributedHint;
expect(attributedHint.string, 'hint');
expect(attributedHint.attributes.length, 1);
expect(attributedHint.attributes[0] is SpellOutStringAttribute, isTrue);
expect(attributedHint.attributes[0].range, const TextRange(start: 1, end: 2));
});
testWidgets('Semantics does not merge role', (WidgetTester tester) async {
final key1 = UniqueKey();
final key2 = UniqueKey();
await tester.pumpWidget(
Semantics(
key: key1,
role: SemanticsRole.dialog,
child: Semantics(key: key2, role: SemanticsRole.alertDialog, child: const Placeholder()),
),
);
final SemanticsNode node1 = tester.getSemantics(find.byKey(key1));
final SemanticsNode node2 = tester.getSemantics(find.byKey(key2));
expect(node1 != node2, isTrue);
expect(node1.role, SemanticsRole.dialog);
expect(node2.role, SemanticsRole.alertDialog);
});
testWidgets('Semantics does not merge role - text field', (WidgetTester tester) async {
final key1 = UniqueKey();
final key2 = UniqueKey();
await tester.pumpWidget(
Semantics(
key: key1,
role: SemanticsRole.dialog,
child: Semantics(key: key2, textField: true, child: const Placeholder()),
),
);
final SemanticsNode node1 = tester.getSemantics(find.byKey(key1));
final SemanticsNode node2 = tester.getSemantics(find.byKey(key2));
expect(node1 != node2, isTrue);
expect(node1.role, SemanticsRole.dialog);
expect(node2.flagsCollection.isTextField, isTrue);
});
testWidgets('Semantics does not merge role - link', (WidgetTester tester) async {
final key1 = UniqueKey();
final key2 = UniqueKey();
await tester.pumpWidget(
Semantics(
key: key1,
role: SemanticsRole.dialog,
child: Semantics(key: key2, link: true, child: const Placeholder()),
),
);
final SemanticsNode node1 = tester.getSemantics(find.byKey(key1));
final SemanticsNode node2 = tester.getSemantics(find.byKey(key2));
expect(node1 != node2, isTrue);
expect(node1.role, SemanticsRole.dialog);
expect(node2.flagsCollection.isLink, isTrue);
});
testWidgets('Semantics does not merge role - scopes route', (WidgetTester tester) async {
final key1 = UniqueKey();
final key2 = UniqueKey();
await tester.pumpWidget(
Semantics(
key: key1,
role: SemanticsRole.dialog,
child: Semantics(
key: key2,
scopesRoute: true,
explicitChildNodes: true,
child: const Placeholder(),
),
),
);
final SemanticsNode node1 = tester.getSemantics(find.byKey(key1));
final SemanticsNode node2 = tester.getSemantics(find.byKey(key2));
expect(node1 != node2, isTrue);
expect(node1.role, SemanticsRole.dialog);
expect(node2.flagsCollection.scopesRoute, isTrue);
});
testWidgets('Semantics does not merge role - header on web', (WidgetTester tester) async {
final key1 = UniqueKey();
final key2 = UniqueKey();
await tester.pumpWidget(
Semantics(
key: key1,
role: SemanticsRole.dialog,
child: Semantics(key: key2, header: true, child: const Placeholder()),
),
);
final SemanticsNode node1 = tester.getSemantics(find.byKey(key1));
final SemanticsNode node2 = tester.getSemantics(find.byKey(key2));
if (kIsWeb) {
expect(node1 != node2, isTrue);
expect(node1.role, SemanticsRole.dialog);
expect(node2.flagsCollection.isHeader, isTrue);
} else {
expect(node1 == node2, isTrue);
}
});
testWidgets('Semantics does not merge role - image', (WidgetTester tester) async {
final key1 = UniqueKey();
final key2 = UniqueKey();
await tester.pumpWidget(
Semantics(
key: key1,
role: SemanticsRole.dialog,
child: Semantics(key: key2, image: true, child: const Placeholder()),
),
);
final SemanticsNode node1 = tester.getSemantics(find.byKey(key1));
final SemanticsNode node2 = tester.getSemantics(find.byKey(key2));
expect(node1 != node2, isTrue);
expect(node1.role, SemanticsRole.dialog);
expect(node2.flagsCollection.isImage, isTrue);
});
testWidgets('Semantics does not merge role - slider', (WidgetTester tester) async {
final key1 = UniqueKey();
final key2 = UniqueKey();
await tester.pumpWidget(
Semantics(
key: key1,
role: SemanticsRole.dialog,
child: Semantics(key: key2, slider: true, child: const Placeholder()),
),
);
final SemanticsNode node1 = tester.getSemantics(find.byKey(key1));
final SemanticsNode node2 = tester.getSemantics(find.byKey(key2));
expect(node1 != node2, isTrue);
expect(node1.role, SemanticsRole.dialog);
expect(node2.flagsCollection.isSlider, isTrue);
});
testWidgets('Semantics does not merge role - keyboard key', (WidgetTester tester) async {
final key1 = UniqueKey();
final key2 = UniqueKey();
await tester.pumpWidget(
Semantics(
key: key1,
role: SemanticsRole.dialog,
child: Semantics(key: key2, keyboardKey: true, child: const Placeholder()),
),
);
final SemanticsNode node1 = tester.getSemantics(find.byKey(key1));
final SemanticsNode node2 = tester.getSemantics(find.byKey(key2));
expect(node1 != node2, isTrue);
expect(node1.role, SemanticsRole.dialog);
expect(node2.flagsCollection.isKeyboardKey, isTrue);
});
testWidgets('Semantics does not merge role - scopes route', (WidgetTester tester) async {
final key1 = UniqueKey();
final key2 = UniqueKey();
await tester.pumpWidget(
Semantics(
key: key1,
role: SemanticsRole.dialog,
child: Semantics(key: key2, slider: true, child: const Placeholder()),
),
);
final SemanticsNode node1 = tester.getSemantics(find.byKey(key1));
final SemanticsNode node2 = tester.getSemantics(find.byKey(key2));
expect(node1 != node2, isTrue);
expect(node1.role, SemanticsRole.dialog);
expect(node2.flagsCollection.isSlider, isTrue);
});
testWidgets('Semantics can set controls visibility of nodes', (WidgetTester tester) async {
final key = UniqueKey();
await tester.pumpWidget(
Semantics(key: key, controlsNodes: const <String>{'abc'}, child: const Placeholder()),
);
final SemanticsNode node = tester.getSemantics(find.byKey(key));
final SemanticsData data = node.getSemanticsData();
expect(data.controlsNodes!.length, 1);
expect(data.controlsNodes!.first, 'abc');
});
testWidgets('Semantics can set controls visibility of nodes', (WidgetTester tester) async {
final key = UniqueKey();
await tester.pumpWidget(
Semantics(
key: key,
controlsNodes: const <String>{'abc', 'ghi'},
child: Semantics(controlsNodes: const <String>{'abc', 'def'}, child: const Placeholder()),
),
);
final SemanticsNode node = tester.getSemantics(find.byKey(key));
final SemanticsData data = node.getSemanticsData();
expect(data.controlsNodes!.length, 3);
expect(data.controlsNodes, <String>{'abc', 'ghi', 'def'});
});
testWidgets('Semantics can set semantics input type', (WidgetTester tester) async {
final key1 = UniqueKey();
await tester.pumpWidget(
Semantics(
key: key1,
inputType: SemanticsInputType.phone,
child: const SizedBox(width: 10, height: 10),
),
);
final SemanticsNode node1 = tester.getSemantics(find.byKey(key1));
expect(node1.inputType, SemanticsInputType.phone);
});
testWidgets('Semantics can set alert rule', (WidgetTester tester) async {
final key = UniqueKey();
await tester.pumpWidget(
Semantics(key: key, role: SemanticsRole.alert, child: const Placeholder()),
);
final SemanticsNode node = tester.getSemantics(find.byKey(key));
final SemanticsData data = node.getSemanticsData();
expect(data.role, SemanticsRole.alert);
});
testWidgets('Semantics can set status rule', (WidgetTester tester) async {
final key = UniqueKey();
await tester.pumpWidget(
Semantics(key: key, role: SemanticsRole.status, child: const Placeholder()),
);
final SemanticsNode node = tester.getSemantics(find.byKey(key));
final SemanticsData data = node.getSemanticsData();
expect(data.role, SemanticsRole.status);
});
testWidgets('Semantics can merge attributed strings', (WidgetTester tester) async {
final key = UniqueKey();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Semantics(
key: key,
attributedLabel: AttributedString(
'label',
attributes: <StringAttribute>[
SpellOutStringAttribute(range: const TextRange(start: 0, end: 5)),
],
),
attributedHint: AttributedString(
'hint',
attributes: <StringAttribute>[
SpellOutStringAttribute(range: const TextRange(start: 1, end: 2)),
],
),
child: Semantics(
attributedLabel: AttributedString(
'label',
attributes: <StringAttribute>[
SpellOutStringAttribute(range: const TextRange(start: 0, end: 5)),
],
),
attributedHint: AttributedString(
'hint',
attributes: <StringAttribute>[
SpellOutStringAttribute(range: const TextRange(start: 1, end: 2)),
],
),
child: const Placeholder(),
),
),
),
);
final AttributedString attributedLabel = tester.getSemantics(find.byKey(key)).attributedLabel;
expect(attributedLabel.string, 'label\nlabel');
expect(attributedLabel.attributes.length, 2);
expect(attributedLabel.attributes[0] is SpellOutStringAttribute, isTrue);
expect(attributedLabel.attributes[0].range, const TextRange(start: 0, end: 5));
expect(attributedLabel.attributes[1] is SpellOutStringAttribute, isTrue);
expect(attributedLabel.attributes[1].range, const TextRange(start: 6, end: 11));
final AttributedString attributedHint = tester.getSemantics(find.byKey(key)).attributedHint;
expect(attributedHint.string, 'hint\nhint');
expect(attributedHint.attributes.length, 2);
expect(attributedHint.attributes[0] is SpellOutStringAttribute, isTrue);
expect(attributedHint.attributes[0].range, const TextRange(start: 1, end: 2));
expect(attributedHint.attributes[1] is SpellOutStringAttribute, isTrue);
expect(attributedHint.attributes[1].range, const TextRange(start: 6, end: 7));
});
testWidgets('Semantics can use list and list item', (WidgetTester tester) async {
final key1 = UniqueKey();
final key2 = UniqueKey();
await tester.pumpWidget(
Semantics(
key: key1,
role: SemanticsRole.list,
container: true,
child: Semantics(
key: key2,
role: SemanticsRole.listItem,
container: true,
child: const Placeholder(),
),
),
);
final SemanticsNode listNode = tester.getSemantics(find.byKey(key1));
final SemanticsNode listItemNode = tester.getSemantics(find.byKey(key2));
expect(listNode.role, SemanticsRole.list);
expect(listItemNode.role, SemanticsRole.listItem);
});
testWidgets('Semantics can use form', (WidgetTester tester) async {
final key1 = UniqueKey();
await tester.pumpWidget(Semantics(key: key1, role: SemanticsRole.form, container: true));
final SemanticsNode formNode = tester.getSemantics(find.byKey(key1));
expect(formNode.role, SemanticsRole.form);
});
testWidgets('Semantics can merge attributed strings with non attributed string', (
WidgetTester tester,
) async {
final key = UniqueKey();
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Semantics(
key: key,
attributedLabel: AttributedString(
'label1',
attributes: <StringAttribute>[
SpellOutStringAttribute(range: const TextRange(start: 0, end: 5)),
],
),
child: Semantics(
label: 'label2',
child: Semantics(
attributedLabel: AttributedString(
'label3',
attributes: <StringAttribute>[
SpellOutStringAttribute(range: const TextRange(start: 1, end: 3)),
],
),
child: const Placeholder(),
),
),
),
),
);
final AttributedString attributedLabel = tester.getSemantics(find.byKey(key)).attributedLabel;
expect(attributedLabel.string, 'label1\nlabel2\nlabel3');
expect(attributedLabel.attributes.length, 2);
expect(attributedLabel.attributes[0] is SpellOutStringAttribute, isTrue);
expect(attributedLabel.attributes[0].range, const TextRange(start: 0, end: 5));
expect(attributedLabel.attributes[1] is SpellOutStringAttribute, isTrue);
expect(attributedLabel.attributes[1].range, const TextRange(start: 15, end: 17));
});
testWidgets(
'Semantics with attributedValue should be recognized as containing text and not fail',
(WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Semantics(
attributedValue: AttributedString('test value'),
child: const Placeholder(),
),
),
);
expect(tester.takeException(), isNull);
},
);
testWidgets(
'Semantics with attributedDecreasedValue should be recognized as containing text and not fail',
(WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Semantics(
attributedDecreasedValue: AttributedString('test value'),
child: const Placeholder(),
),
),
);
expect(tester.takeException(), isNull);
},
);
testWidgets(
'Semantics with attributedIncreasedValue should be recognized as containing text and not fail',
(WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Semantics(
attributedIncreasedValue: AttributedString('test value'),
child: const Placeholder(),
),
),
);
expect(tester.takeException(), isNull);
},
);
testWidgets(
'Semantics with decreasedValue should be recognized as containing text and not fail',
(WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Semantics(decreasedValue: 'test value', child: const Placeholder()),
),
);
expect(tester.takeException(), isNull);
},
);
testWidgets(
'Semantics with increasedValue should be recognized as containing text and not fail',
(WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Semantics(increasedValue: 'test value', child: const Placeholder()),
),
);
expect(tester.takeException(), isNull);
},
);
testWidgets(
'Semantics with attributedHint should be recognized as containing text and not fail',
(WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: Semantics(
attributedHint: AttributedString('test value'),
child: const Placeholder(),
),
),
);
expect(tester.takeException(), isNull);
},
);
});
group('Row', () {
testWidgets('multiple baseline aligned children', (WidgetTester tester) async {
final key1 = UniqueKey();
final key2 = UniqueKey();
// The point size of the font must be a multiple of 4 until
// https://github.com/flutter/flutter/issues/122066 is resolved.
const double fontSize1 = 52;
const double fontSize2 = 12;
await tester.pumpWidget(
TestWidgetsApp(
home: Align(
alignment: Alignment.topLeft,
child: Row(
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: <Widget>[
Text(
'big text',
key: key1,
style: const TextStyle(
fontFamily: 'FlutterTest',
fontSize: fontSize1,
height: 1.0,
),
),
Text(
'one\ntwo\nthree\nfour\nfive\nsix\nseven',
key: key2,
style: const TextStyle(
fontFamily: 'FlutterTest',
fontSize: fontSize2,
height: 1.0,
),
),
],
),
),
),
);
final RenderBox textBox1 = tester.renderObject(find.byKey(key1));
final RenderBox textBox2 = tester.renderObject(find.byKey(key2));
final RenderBox rowBox = tester.renderObject(find.byType(Row));
// The two Texts are baseline aligned, so some portion of them extends
// both above and below the baseline. The first has a huge font size, so
// it extends higher above the baseline than usual. The second has many
// lines, but being aligned by the first line's baseline, they hang far
// below the baseline. The size of the parent row is just enough to
// contain both of them.
const ascentRatio = 0.75;
const double aboveBaseline1 = fontSize1 * ascentRatio;
const double belowBaseline1 = fontSize1 * (1 - ascentRatio);
const double aboveBaseline2 = fontSize2 * ascentRatio;
const double belowBaseline2 = fontSize2 * (1 - ascentRatio) + fontSize2 * 6;
final double aboveBaseline = math.max(aboveBaseline1, aboveBaseline2);
final double belowBaseline = math.max(belowBaseline1, belowBaseline2);
expect(rowBox.size.height, greaterThan(textBox1.size.height));
expect(rowBox.size.height, greaterThan(textBox2.size.height));
expect(rowBox.size.height, aboveBaseline + belowBaseline);
expect(tester.getTopLeft(find.byKey(key1)).dy, 0);
expect(tester.getTopLeft(find.byKey(key2)).dy, aboveBaseline1 - aboveBaseline2);
});
testWidgets('baseline aligned children account for a larger, no-baseline child size', (
WidgetTester tester,
) async {
// Regression test for https://github.com/flutter/flutter/issues/58898
final key1 = UniqueKey();
final key2 = UniqueKey();
// The point size of the font must be a multiple of 4 until
// https://github.com/flutter/flutter/issues/122066 is resolved.
const double fontSize1 = 52;
const double fontSize2 = 12;
await tester.pumpWidget(
TestWidgetsApp(
home: Align(
alignment: Alignment.topLeft,
child: Row(
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
children: <Widget>[
Text(
'big text',
key: key1,
style: const TextStyle(
fontFamily: 'FlutterTest',
fontSize: fontSize1,
height: 1.0,
),
),
Text(
'one\ntwo\nthree\nfour\nfive\nsix\nseven',
key: key2,
style: const TextStyle(
fontFamily: 'FlutterTest',
fontSize: fontSize2,
height: 1.0,
),
),
const FlutterLogo(size: 250),
],
),
),
),
);
final RenderBox textBox1 = tester.renderObject(find.byKey(key1));
final RenderBox textBox2 = tester.renderObject(find.byKey(key2));
final RenderBox rowBox = tester.renderObject(find.byType(Row));
// The two Texts are baseline aligned, so some portion of them extends
// both above and below the baseline. The first has a huge font size, so
// it extends higher above the baseline than usual. The second has many
// lines, but being aligned by the first line's baseline, they hang far
// below the baseline. The FlutterLogo extends further than both Texts,
// so the size of the parent row should contain the FlutterLogo as well.
const ascentRatio = 0.75;
const double aboveBaseline1 = fontSize1 * ascentRatio;
const double aboveBaseline2 = fontSize2 * ascentRatio;
expect(rowBox.size.height, greaterThan(textBox1.size.height));
expect(rowBox.size.height, greaterThan(textBox2.size.height));
expect(rowBox.size.height, 250);
expect(tester.getTopLeft(find.byKey(key1)).dy, 0);
expect(tester.getTopLeft(find.byKey(key2)).dy, aboveBaseline1 - aboveBaseline2);
});
});
group('UnconstrainedBox', () {
test('UnconstrainedBox toString', () {
expect(
const UnconstrainedBox(constrainedAxis: Axis.vertical).toString(),
equals('UnconstrainedBox(alignment: Alignment.center, constrainedAxis: vertical)'),
);
expect(
const UnconstrainedBox(
constrainedAxis: Axis.horizontal,
textDirection: TextDirection.rtl,
alignment: Alignment.topRight,
).toString(),
equals(
'UnconstrainedBox(alignment: Alignment.topRight, constrainedAxis: horizontal, textDirection: rtl)',
),
);
});
testWidgets('UnconstrainedBox can set and update clipBehavior', (WidgetTester tester) async {
await tester.pumpWidget(const UnconstrainedBox());
final RenderConstraintsTransformBox renderObject = tester.allRenderObjects
.whereType<RenderConstraintsTransformBox>()
.first;
expect(renderObject.clipBehavior, equals(Clip.none));
await tester.pumpWidget(const UnconstrainedBox(clipBehavior: Clip.antiAlias));
expect(renderObject.clipBehavior, equals(Clip.antiAlias));
});
testWidgets('UnconstrainedBox warns only when clipBehavior is Clip.none', (
WidgetTester tester,
) async {
for (final clip in <Clip?>[null, ...Clip.values]) {
// Clear any render objects that were there before so that we can see more
// than one error. Otherwise, it just throws the first one and skips the
// rest, since the render objects haven't changed.
await tester.pumpWidget(const SizedBox());