semantics_test.dart 32.1 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/rendering.dart';
6
import 'package:flutter_test/flutter_test.dart';
7
import 'package:vector_math/vector_math_64.dart';
8

9
import '../rendering/rendering_tester.dart';
10

11 12
const int kMaxFrameworkAccessibilityIdentifier = (1<<16) - 1;

13
void main() {
14 15
  TestRenderingFlutterBinding.ensureInitialized();

16 17 18 19
  setUp(() {
    debugResetSemanticsIdCounter();
  });

20
  group('SemanticsNode', () {
21 22 23
    const SemanticsTag tag1 = SemanticsTag('Tag One');
    const SemanticsTag tag2 = SemanticsTag('Tag Two');
    const SemanticsTag tag3 = SemanticsTag('Tag Three');
24 25

    test('tagging', () {
26
      final SemanticsNode node = SemanticsNode();
27

28 29
      expect(node.isTagged(tag1), isFalse);
      expect(node.isTagged(tag2), isFalse);
30

31
      node.tags = <SemanticsTag>{tag1};
32 33
      expect(node.isTagged(tag1), isTrue);
      expect(node.isTagged(tag2), isFalse);
34

35
      node.tags!.add(tag2);
36 37
      expect(node.isTagged(tag1), isTrue);
      expect(node.isTagged(tag2), isTrue);
38 39 40
    });

    test('getSemanticsData includes tags', () {
41
      final Set<SemanticsTag> tags = <SemanticsTag>{tag1, tag2};
42

43
      final SemanticsNode node = SemanticsNode()
Dan Field's avatar
Dan Field committed
44
        ..rect = const Rect.fromLTRB(0.0, 0.0, 10.0, 10.0)
45
        ..tags = tags;
46

47
      expect(node.getSemanticsData().tags, tags);
48

49
      tags.add(tag3);
50

51
      final SemanticsConfiguration config = SemanticsConfiguration()
52
        ..isSemanticBoundary = true
53
        ..isMergingSemanticsOfDescendants = true;
54

55 56 57
      node.updateWith(
        config: config,
        childrenInInversePaintOrder: <SemanticsNode>[
58
          SemanticsNode()
59
            ..isMergedIntoParent = true
Dan Field's avatar
Dan Field committed
60
            ..rect = const Rect.fromLTRB(5.0, 5.0, 10.0, 10.0)
61 62 63
            ..tags = tags,
        ],
      );
64

65
      expect(node.getSemanticsData().tags, tags);
66
    });
67

68 69 70 71 72 73
    test('SemanticsConfiguration can set both string label/value/hint and attributed version', () {
      final SemanticsConfiguration config = SemanticsConfiguration();
      config.label = 'label1';
      expect(config.label, 'label1');
      expect(config.attributedLabel.string, 'label1');
      expect(config.attributedLabel.attributes.isEmpty, isTrue);
74 75 76 77
      expect(
        (SemanticsNode()..updateWith(config: config)).toString(),
        'SemanticsNode#1(STALE, owner: null, Rect.fromLTRB(0.0, 0.0, 0.0, 0.0), invisible, label: "label1")',
      );
78 79 80 81 82 83 84 85 86 87 88 89

      config.attributedLabel = AttributedString(
        'label2',
        attributes: <StringAttribute>[
          SpellOutStringAttribute(range: const TextRange(start: 0, end:1)),
        ]
      );
      expect(config.label, 'label2');
      expect(config.attributedLabel.string, 'label2');
      expect(config.attributedLabel.attributes.length, 1);
      expect(config.attributedLabel.attributes[0] is SpellOutStringAttribute, isTrue);
      expect(config.attributedLabel.attributes[0].range, const TextRange(start: 0, end: 1));
90 91 92 93
      expect(
        (SemanticsNode()..updateWith(config: config)).toString(),
        'SemanticsNode#2(STALE, owner: null, Rect.fromLTRB(0.0, 0.0, 0.0, 0.0), invisible, label: "label2" [SpellOutStringAttribute(TextRange(start: 0, end: 1))])',
      );
94 95 96 97 98

      config.label = 'label3';
      expect(config.label, 'label3');
      expect(config.attributedLabel.string, 'label3');
      expect(config.attributedLabel.attributes.isEmpty, isTrue);
99 100 101 102
      expect(
        (SemanticsNode()..updateWith(config: config)).toString(),
        'SemanticsNode#3(STALE, owner: null, Rect.fromLTRB(0.0, 0.0, 0.0, 0.0), invisible, label: "label3")',
      );
103 104 105 106 107

      config.value = 'value1';
      expect(config.value, 'value1');
      expect(config.attributedValue.string, 'value1');
      expect(config.attributedValue.attributes.isEmpty, isTrue);
108 109 110 111
      expect(
        (SemanticsNode()..updateWith(config: config)).toString(),
        'SemanticsNode#4(STALE, owner: null, Rect.fromLTRB(0.0, 0.0, 0.0, 0.0), invisible, label: "label3", value: "value1")',
      );
112 113 114 115 116 117 118 119 120 121 122 123

      config.attributedValue = AttributedString(
          'value2',
          attributes: <StringAttribute>[
            SpellOutStringAttribute(range: const TextRange(start: 0, end:1)),
          ]
      );
      expect(config.value, 'value2');
      expect(config.attributedValue.string, 'value2');
      expect(config.attributedValue.attributes.length, 1);
      expect(config.attributedValue.attributes[0] is SpellOutStringAttribute, isTrue);
      expect(config.attributedValue.attributes[0].range, const TextRange(start: 0, end: 1));
124 125 126 127
      expect(
        (SemanticsNode()..updateWith(config: config)).toString(),
        'SemanticsNode#5(STALE, owner: null, Rect.fromLTRB(0.0, 0.0, 0.0, 0.0), invisible, label: "label3", value: "value2" [SpellOutStringAttribute(TextRange(start: 0, end: 1))])',
      );
128 129 130 131 132

      config.value = 'value3';
      expect(config.value, 'value3');
      expect(config.attributedValue.string, 'value3');
      expect(config.attributedValue.attributes.isEmpty, isTrue);
133 134 135 136
      expect(
        (SemanticsNode()..updateWith(config: config)).toString(),
        'SemanticsNode#6(STALE, owner: null, Rect.fromLTRB(0.0, 0.0, 0.0, 0.0), invisible, label: "label3", value: "value3")',
      );
137 138 139 140 141

      config.hint = 'hint1';
      expect(config.hint, 'hint1');
      expect(config.attributedHint.string, 'hint1');
      expect(config.attributedHint.attributes.isEmpty, isTrue);
142 143 144 145
      expect(
        (SemanticsNode()..updateWith(config: config)).toString(),
        'SemanticsNode#7(STALE, owner: null, Rect.fromLTRB(0.0, 0.0, 0.0, 0.0), invisible, label: "label3", value: "value3", hint: "hint1")',
      );
146 147 148 149 150 151 152 153 154 155 156 157

      config.attributedHint = AttributedString(
          'hint2',
          attributes: <StringAttribute>[
            SpellOutStringAttribute(range: const TextRange(start: 0, end:1)),
          ]
      );
      expect(config.hint, 'hint2');
      expect(config.attributedHint.string, 'hint2');
      expect(config.attributedHint.attributes.length, 1);
      expect(config.attributedHint.attributes[0] is SpellOutStringAttribute, isTrue);
      expect(config.attributedHint.attributes[0].range, const TextRange(start: 0, end: 1));
158 159 160 161
      expect(
        (SemanticsNode()..updateWith(config: config)).toString(),
        'SemanticsNode#8(STALE, owner: null, Rect.fromLTRB(0.0, 0.0, 0.0, 0.0), invisible, label: "label3", value: "value3", hint: "hint2" [SpellOutStringAttribute(TextRange(start: 0, end: 1))])',
      );
162 163 164 165 166

      config.hint = 'hint3';
      expect(config.hint, 'hint3');
      expect(config.attributedHint.string, 'hint3');
      expect(config.attributedHint.attributes.isEmpty, isTrue);
167 168 169 170
      expect(
        (SemanticsNode()..updateWith(config: config)).toString(),
        'SemanticsNode#9(STALE, owner: null, Rect.fromLTRB(0.0, 0.0, 0.0, 0.0), invisible, label: "label3", value: "value3", hint: "hint3")',
      );
171 172
    });

173 174 175 176 177 178 179 180 181 182 183
    test('mutate existing semantic node list errors', () {
      final SemanticsNode node = SemanticsNode()
        ..rect = const Rect.fromLTRB(0.0, 0.0, 10.0, 10.0);

      final SemanticsConfiguration config = SemanticsConfiguration()
        ..isSemanticBoundary = true
        ..isMergingSemanticsOfDescendants = true;

      final List<SemanticsNode> children = <SemanticsNode>[
        SemanticsNode()
          ..isMergedIntoParent = true
184
          ..rect = const Rect.fromLTRB(5.0, 5.0, 10.0, 10.0),
185 186 187 188
      ];

      node.updateWith(
        config: config,
189
        childrenInInversePaintOrder: children,
190 191
      );

192 193 194 195
      children.add(
        SemanticsNode()
          ..isMergedIntoParent = true
          ..rect = const Rect.fromLTRB(42.0, 42.0, 10.0, 10.0),
196 197 198
      );

      {
199
        late FlutterError error;
200 201 202
        try {
          node.updateWith(
            config: config,
203
            childrenInInversePaintOrder: children,
204 205 206 207 208 209 210 211
          );
        } on FlutterError catch (e) {
          error = e;
        }
        expect(error.toString(), equalsIgnoringHashCodes(
          'Failed to replace child semantics nodes because the list of `SemanticsNode`s was mutated.\n'
          'Instead of mutating the existing list, create a new list containing the desired `SemanticsNode`s.\n'
          'Error details:\n'
212
          "The list's length has changed from 1 to 2.",
213 214 215
        ));
        expect(
          error.diagnostics.singleWhere((DiagnosticsNode node) => node.level == DiagnosticLevel.hint).toString(),
216
          'Instead of mutating the existing list, create a new list containing the desired `SemanticsNode`s.',
217 218 219 220
        );
      }

      {
221
        late FlutterError error;
222 223 224 225 226 227
        final List<SemanticsNode> modifiedChildren = <SemanticsNode>[
          SemanticsNode()
            ..isMergedIntoParent = true
            ..rect = const Rect.fromLTRB(5.0, 5.0, 10.0, 10.0),
          SemanticsNode()
            ..isMergedIntoParent = true
228
            ..rect = const Rect.fromLTRB(10.0, 10.0, 20.0, 20.0),
229 230 231 232 233 234 235 236 237 238 239 240 241 242
        ];
        node.updateWith(
          config: config,
          childrenInInversePaintOrder: modifiedChildren,
        );
        try {
          modifiedChildren[0] = SemanticsNode()
            ..isMergedIntoParent = true
            ..rect = const Rect.fromLTRB(0.0, 0.0, 20.0, 20.0);
          modifiedChildren[1] = SemanticsNode()
            ..isMergedIntoParent = true
            ..rect = const Rect.fromLTRB(40.0, 14.0, 20.0, 20.0);
          node.updateWith(
            config: config,
243
            childrenInInversePaintOrder: modifiedChildren,
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
          );
        } on FlutterError catch (e) {
          error = e;
        }
        expect(error.toStringDeep(), equalsIgnoringHashCodes(
          'FlutterError\n'
          '   Failed to replace child semantics nodes because the list of\n'
          '   `SemanticsNode`s was mutated.\n'
          '   Instead of mutating the existing list, create a new list\n'
          '   containing the desired `SemanticsNode`s.\n'
          '   Error details:\n'
          '   Child node at position 0 was replaced:\n'
          '   Previous child: SemanticsNode#6(STALE, owner: null, merged up ⬆️, Rect.fromLTRB(0.0, 0.0, 20.0, 20.0))\n'
          '   New child: SemanticsNode#4(STALE, owner: null, merged up ⬆️, Rect.fromLTRB(5.0, 5.0, 10.0, 10.0))\n'
          '\n'
          '   Child node at position 1 was replaced:\n'
          '   Previous child: SemanticsNode#7(STALE, owner: null, merged up ⬆️, Rect.fromLTRB(40.0, 14.0, 20.0, 20.0))\n'
261
          '   New child: SemanticsNode#5(STALE, owner: null, merged up ⬆️, Rect.fromLTRB(10.0, 10.0, 20.0, 20.0))\n',
262 263 264 265
        ));

        expect(
          error.diagnostics.singleWhere((DiagnosticsNode node) => node.level == DiagnosticLevel.hint).toString(),
266
          'Instead of mutating the existing list, create a new list containing the desired `SemanticsNode`s.',
267 268 269 270 271 272
        );
        // Two previous children and two new children.
        expect(error.diagnostics.where((DiagnosticsNode node) => node.value is SemanticsNode).length, 4);
      }
    });

273
    test('after markNeedsSemanticsUpdate() all render objects between two semantic boundaries are asked for annotations', () {
274
      TestRenderingFlutterBinding.instance.pipelineOwner.ensureSemantics();
275 276

      TestRender middle;
277
      final TestRender root = TestRender(
278
        hasTapAction: true,
279
        isSemanticBoundary: true,
280
        child: TestRender(
281
          hasLongPressAction: true,
282
          child: middle = TestRender(
283
            hasScrollLeftAction: true,
284
            child: TestRender(
285
              hasScrollRightAction: true,
286
              child: TestRender(
287
                hasScrollUpAction: true,
288
                isSemanticBoundary: true,
289 290 291 292
              ),
            ),
          ),
        ),
293 294 295 296 297 298
      );

      layout(root);
      pumpFrame(phase: EnginePhase.flushSemantics);

      int expectedActions = SemanticsAction.tap.index | SemanticsAction.longPress.index | SemanticsAction.scrollLeft.index | SemanticsAction.scrollRight.index;
299
      expect(root.debugSemantics!.getSemanticsData().actions, expectedActions);
300

301 302 303
      middle
        ..hasScrollLeftAction = false
        ..hasScrollDownAction = true;
304
      middle.markNeedsSemanticsUpdate();
305 306 307 308

      pumpFrame(phase: EnginePhase.flushSemantics);

      expectedActions = SemanticsAction.tap.index | SemanticsAction.longPress.index | SemanticsAction.scrollDown.index | SemanticsAction.scrollRight.index;
309
      expect(root.debugSemantics!.getSemanticsData().actions, expectedActions);
310
    });
311
  });
312 313

  test('toStringDeep() does not throw with transform == null', () {
314
    final SemanticsNode child1 = SemanticsNode()
Dan Field's avatar
Dan Field committed
315
      ..rect = const Rect.fromLTRB(0.0, 0.0, 5.0, 5.0);
316
    final SemanticsNode child2 = SemanticsNode()
Dan Field's avatar
Dan Field committed
317
      ..rect = const Rect.fromLTRB(5.0, 0.0, 10.0, 5.0);
318
    final SemanticsNode root = SemanticsNode()
Dan Field's avatar
Dan Field committed
319
      ..rect = const Rect.fromLTRB(0.0, 0.0, 10.0, 5.0);
320 321 322 323
    root.updateWith(
      config: null,
      childrenInInversePaintOrder: <SemanticsNode>[child1, child2],
    );
324 325 326 327 328

    expect(root.transform, isNull);
    expect(child1.transform, isNull);
    expect(child2.transform, isNull);

329
    expect(
330
      root.toStringDeep(),
331 332 333 334 335 336 337 338 339 340 341 342 343
      'SemanticsNode#3\n'
      ' │ STALE\n'
      ' │ owner: null\n'
      ' │ Rect.fromLTRB(0.0, 0.0, 10.0, 5.0)\n'
      ' │\n'
      ' ├─SemanticsNode#1\n'
      ' │   STALE\n'
      ' │   owner: null\n'
      ' │   Rect.fromLTRB(0.0, 0.0, 5.0, 5.0)\n'
      ' │\n'
      ' └─SemanticsNode#2\n'
      '     STALE\n'
      '     owner: null\n'
344
      '     Rect.fromLTRB(5.0, 0.0, 10.0, 5.0)\n',
345 346 347
    );
  });

348 349 350 351 352 353 354
  test('Incompatible OrdinalSortKey throw AssertionError when compared', () {
    // Different types.
    expect(() {
      const OrdinalSortKey(0.0).compareTo(const CustomSortKey(0.0));
    }, throwsAssertionError);
  });

355
  test('OrdinalSortKey compares correctly when names are the same', () {
356 357 358 359 360
    const List<List<SemanticsSortKey>> tests = <List<SemanticsSortKey>>[
      <SemanticsSortKey>[OrdinalSortKey(0.0), OrdinalSortKey(0.0)],
      <SemanticsSortKey>[OrdinalSortKey(0.0), OrdinalSortKey(1.0)],
      <SemanticsSortKey>[OrdinalSortKey(1.0), OrdinalSortKey(0.0)],
      <SemanticsSortKey>[OrdinalSortKey(1.0), OrdinalSortKey(1.0)],
361 362 363 364
      <SemanticsSortKey>[OrdinalSortKey(0.0, name: 'a'), OrdinalSortKey(0.0, name: 'a')],
      <SemanticsSortKey>[OrdinalSortKey(0.0, name: 'a'), OrdinalSortKey(1.0, name: 'a')],
      <SemanticsSortKey>[OrdinalSortKey(1.0, name: 'a'), OrdinalSortKey(0.0, name: 'a')],
      <SemanticsSortKey>[OrdinalSortKey(1.0, name: 'a'), OrdinalSortKey(1.0, name: 'a')],
365
    ];
366
    final List<int> expectedResults = <int>[0, -1, 1, 0, 0, -1, 1, 0];
367
    assert(tests.length == expectedResults.length);
368
    final List<int> results = <int>[
369
      for (final List<SemanticsSortKey> tuple in tests) tuple[0].compareTo(tuple[1]),
370
    ];
371
    expect(results, orderedEquals(expectedResults));
372 373 374

    // Differing types should throw an assertion.
    expect(() => const OrdinalSortKey(0.0).compareTo(const CustomSortKey(0.0)), throwsAssertionError);
375 376
  });

377
  test('OrdinalSortKey compares correctly when the names are different', () {
378
    const List<List<SemanticsSortKey>> tests = <List<SemanticsSortKey>>[
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
      <SemanticsSortKey>[OrdinalSortKey(0.0), OrdinalSortKey(0.0, name: 'bar')],
      <SemanticsSortKey>[OrdinalSortKey(0.0), OrdinalSortKey(1.0, name: 'bar')],
      <SemanticsSortKey>[OrdinalSortKey(1.0), OrdinalSortKey(0.0, name: 'bar')],
      <SemanticsSortKey>[OrdinalSortKey(1.0), OrdinalSortKey(1.0, name: 'bar')],
      <SemanticsSortKey>[OrdinalSortKey(0.0, name: 'foo'), OrdinalSortKey(0.0)],
      <SemanticsSortKey>[OrdinalSortKey(0.0, name: 'foo'), OrdinalSortKey(1.0)],
      <SemanticsSortKey>[OrdinalSortKey(1.0, name: 'foo'), OrdinalSortKey(0.0)],
      <SemanticsSortKey>[OrdinalSortKey(1.0, name: 'foo'), OrdinalSortKey(1.0)],
      <SemanticsSortKey>[OrdinalSortKey(0.0, name: 'foo'), OrdinalSortKey(0.0, name: 'bar')],
      <SemanticsSortKey>[OrdinalSortKey(0.0, name: 'foo'), OrdinalSortKey(1.0, name: 'bar')],
      <SemanticsSortKey>[OrdinalSortKey(1.0, name: 'foo'), OrdinalSortKey(0.0, name: 'bar')],
      <SemanticsSortKey>[OrdinalSortKey(1.0, name: 'foo'), OrdinalSortKey(1.0, name: 'bar')],
      <SemanticsSortKey>[OrdinalSortKey(0.0, name: 'bar'), OrdinalSortKey(0.0, name: 'foo')],
      <SemanticsSortKey>[OrdinalSortKey(0.0, name: 'bar'), OrdinalSortKey(1.0, name: 'foo')],
      <SemanticsSortKey>[OrdinalSortKey(1.0, name: 'bar'), OrdinalSortKey(0.0, name: 'foo')],
      <SemanticsSortKey>[OrdinalSortKey(1.0, name: 'bar'), OrdinalSortKey(1.0, name: 'foo')],
395
    ];
396
    final List<int> expectedResults = <int>[ -1, -1, -1, -1, 1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1];
397
    assert(tests.length == expectedResults.length);
398
    final List<int> results = <int>[
399
      for (final List<SemanticsSortKey> tuple in tests) tuple[0].compareTo(tuple[1]),
400
    ];
401 402 403
    expect(results, orderedEquals(expectedResults));
  });

404
  test('toStringDeep respects childOrder parameter', () {
405
    final SemanticsNode child1 = SemanticsNode()
Dan Field's avatar
Dan Field committed
406
      ..rect = const Rect.fromLTRB(15.0, 0.0, 20.0, 5.0);
407
    final SemanticsNode child2 = SemanticsNode()
Dan Field's avatar
Dan Field committed
408
      ..rect = const Rect.fromLTRB(10.0, 0.0, 15.0, 5.0);
409
    final SemanticsNode root = SemanticsNode()
Dan Field's avatar
Dan Field committed
410
      ..rect = const Rect.fromLTRB(0.0, 0.0, 20.0, 5.0);
411 412 413 414
    root.updateWith(
      config: null,
      childrenInInversePaintOrder: <SemanticsNode>[child1, child2],
    );
415
    expect(
416
      root.toStringDeep(),
417 418 419 420 421
      'SemanticsNode#3\n'
      ' │ STALE\n'
      ' │ owner: null\n'
      ' │ Rect.fromLTRB(0.0, 0.0, 20.0, 5.0)\n'
      ' │\n'
422
      ' ├─SemanticsNode#1\n'
423 424
      ' │   STALE\n'
      ' │   owner: null\n'
425
      ' │   Rect.fromLTRB(15.0, 0.0, 20.0, 5.0)\n'
426
      ' │\n'
427
      ' └─SemanticsNode#2\n'
428 429
      '     STALE\n'
      '     owner: null\n'
430
      '     Rect.fromLTRB(10.0, 0.0, 15.0, 5.0)\n',
431 432 433 434
    );

    expect(
      root.toStringDeep(childOrder: DebugSemanticsDumpOrder.inverseHitTest),
435 436 437 438 439 440 441 442 443 444 445 446 447
      'SemanticsNode#3\n'
      ' │ STALE\n'
      ' │ owner: null\n'
      ' │ Rect.fromLTRB(0.0, 0.0, 20.0, 5.0)\n'
      ' │\n'
      ' ├─SemanticsNode#1\n'
      ' │   STALE\n'
      ' │   owner: null\n'
      ' │   Rect.fromLTRB(15.0, 0.0, 20.0, 5.0)\n'
      ' │\n'
      ' └─SemanticsNode#2\n'
      '     STALE\n'
      '     owner: null\n'
448
      '     Rect.fromLTRB(10.0, 0.0, 15.0, 5.0)\n',
449 450
    );

451
    final SemanticsNode child3 = SemanticsNode()
Dan Field's avatar
Dan Field committed
452
      ..rect = const Rect.fromLTRB(0.0, 0.0, 10.0, 5.0);
453 454 455
    child3.updateWith(
      config: null,
      childrenInInversePaintOrder: <SemanticsNode>[
456
        SemanticsNode()
Dan Field's avatar
Dan Field committed
457
          ..rect = const Rect.fromLTRB(5.0, 0.0, 10.0, 5.0),
458
        SemanticsNode()
Dan Field's avatar
Dan Field committed
459
          ..rect = const Rect.fromLTRB(0.0, 0.0, 5.0, 5.0),
460 461
      ],
    );
462

463
    final SemanticsNode rootComplex = SemanticsNode()
Dan Field's avatar
Dan Field committed
464
      ..rect = const Rect.fromLTRB(0.0, 0.0, 25.0, 5.0);
465 466
    rootComplex.updateWith(
        config: null,
467
        childrenInInversePaintOrder: <SemanticsNode>[child1, child2, child3],
468
    );
469 470

    expect(
471
      rootComplex.toStringDeep(),
472 473 474 475 476
      'SemanticsNode#7\n'
      ' │ STALE\n'
      ' │ owner: null\n'
      ' │ Rect.fromLTRB(0.0, 0.0, 25.0, 5.0)\n'
      ' │\n'
477 478 479 480
      ' ├─SemanticsNode#1\n'
      ' │   STALE\n'
      ' │   owner: null\n'
      ' │   Rect.fromLTRB(15.0, 0.0, 20.0, 5.0)\n'
481 482 483 484 485 486
      ' │\n'
      ' ├─SemanticsNode#2\n'
      ' │   STALE\n'
      ' │   owner: null\n'
      ' │   Rect.fromLTRB(10.0, 0.0, 15.0, 5.0)\n'
      ' │\n'
487 488 489 490 491 492 493 494 495 496 497 498 499
      ' └─SemanticsNode#4\n'
      '   │ STALE\n'
      '   │ owner: null\n'
      '   │ Rect.fromLTRB(0.0, 0.0, 10.0, 5.0)\n'
      '   │\n'
      '   ├─SemanticsNode#5\n'
      '   │   STALE\n'
      '   │   owner: null\n'
      '   │   Rect.fromLTRB(5.0, 0.0, 10.0, 5.0)\n'
      '   │\n'
      '   └─SemanticsNode#6\n'
      '       STALE\n'
      '       owner: null\n'
500
      '       Rect.fromLTRB(0.0, 0.0, 5.0, 5.0)\n',
501 502 503 504
    );

    expect(
      rootComplex.toStringDeep(childOrder: DebugSemanticsDumpOrder.inverseHitTest),
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
      'SemanticsNode#7\n'
      ' │ STALE\n'
      ' │ owner: null\n'
      ' │ Rect.fromLTRB(0.0, 0.0, 25.0, 5.0)\n'
      ' │\n'
      ' ├─SemanticsNode#1\n'
      ' │   STALE\n'
      ' │   owner: null\n'
      ' │   Rect.fromLTRB(15.0, 0.0, 20.0, 5.0)\n'
      ' │\n'
      ' ├─SemanticsNode#2\n'
      ' │   STALE\n'
      ' │   owner: null\n'
      ' │   Rect.fromLTRB(10.0, 0.0, 15.0, 5.0)\n'
      ' │\n'
      ' └─SemanticsNode#4\n'
      '   │ STALE\n'
      '   │ owner: null\n'
      '   │ Rect.fromLTRB(0.0, 0.0, 10.0, 5.0)\n'
      '   │\n'
      '   ├─SemanticsNode#5\n'
      '   │   STALE\n'
      '   │   owner: null\n'
      '   │   Rect.fromLTRB(5.0, 0.0, 10.0, 5.0)\n'
      '   │\n'
      '   └─SemanticsNode#6\n'
      '       STALE\n'
      '       owner: null\n'
533
      '       Rect.fromLTRB(0.0, 0.0, 5.0, 5.0)\n',
534 535 536 537
    );
  });

  test('debug properties', () {
538
    final SemanticsNode minimalProperties = SemanticsNode();
539
    expect(
540
      minimalProperties.toStringDeep(),
541 542
      'SemanticsNode#1\n'
      '   Rect.fromLTRB(0.0, 0.0, 0.0, 0.0)\n'
543
      '   invisible\n',
544 545
    );

546 547
    expect(
      minimalProperties.toStringDeep(minLevel: DiagnosticLevel.hidden),
548 549 550 551 552
      'SemanticsNode#1\n'
      '   owner: null\n'
      '   isMergedIntoParent: false\n'
      '   mergeAllDescendantsIntoThisNode: false\n'
      '   Rect.fromLTRB(0.0, 0.0, 0.0, 0.0)\n'
553
      '   tags: null\n'
554
      '   actions: []\n'
555
      '   customActions: []\n'
556 557
      '   flags: []\n'
      '   invisible\n'
558
      '   isHidden: false\n'
559 560 561 562 563
      '   label: ""\n'
      '   value: ""\n'
      '   increasedValue: ""\n'
      '   decreasedValue: ""\n'
      '   hint: ""\n'
564
      '   tooltip: ""\n'
565
      '   textDirection: null\n'
566
      '   sortKey: null\n'
567
      '   platformViewId: null\n'
568 569
      '   maxValueLength: null\n'
      '   currentValueLength: null\n'
570 571
      '   scrollChildren: null\n'
      '   scrollIndex: null\n'
572 573 574
      '   scrollExtentMin: null\n'
      '   scrollPosition: null\n'
      '   scrollExtentMax: null\n'
575
      '   elevation: 0.0\n'
576
      '   thickness: 0.0\n',
577 578
    );

579
    final SemanticsConfiguration config = SemanticsConfiguration()
580
      ..isSemanticBoundary = true
581
      ..isMergingSemanticsOfDescendants = true
582 583 584
      ..onScrollUp = () { }
      ..onLongPress = () { }
      ..onShowOnScreen = () { }
585 586
      ..isChecked = false
      ..isSelected = true
587
      ..isButton = true
588
      ..label = 'Use all the properties'
589
      ..textDirection = TextDirection.rtl
590
      ..sortKey = const OrdinalSortKey(1.0);
591
    final SemanticsNode allProperties = SemanticsNode()
Dan Field's avatar
Dan Field committed
592
      ..rect = const Rect.fromLTWH(50.0, 10.0, 20.0, 30.0)
593
      ..transform = Matrix4.translation(Vector3(10.0, 10.0, 0.0))
594
      ..updateWith(config: config);
595 596
    expect(
      allProperties.toStringDeep(),
597
      equalsIgnoringHashCodes(
598 599 600 601 602 603 604 605 606 607
        'SemanticsNode#2\n'
        '   STALE\n'
        '   owner: null\n'
        '   merge boundary ⛔️\n'
        '   Rect.fromLTRB(60.0, 20.0, 80.0, 50.0)\n'
        '   actions: longPress, scrollUp, showOnScreen\n'
        '   flags: hasCheckedState, isSelected, isButton\n'
        '   label: "Use all the properties"\n'
        '   textDirection: rtl\n'
        '   sortKey: OrdinalSortKey#19df5(order: 1.0)\n',
608
      ),
609 610 611
    );
    expect(
      allProperties.getSemanticsData().toString(),
612
      'SemanticsData(Rect.fromLTRB(50.0, 10.0, 70.0, 40.0), [1.0,0.0,0.0,10.0; 0.0,1.0,0.0,10.0; 0.0,0.0,1.0,0.0; 0.0,0.0,0.0,1.0], actions: [longPress, scrollUp, showOnScreen], flags: [hasCheckedState, isSelected, isButton], label: "Use all the properties", textDirection: rtl)',
613 614
    );

615
    final SemanticsNode scaled = SemanticsNode()
Dan Field's avatar
Dan Field committed
616
      ..rect = const Rect.fromLTWH(50.0, 10.0, 20.0, 30.0)
617
      ..transform = Matrix4.diagonal3(Vector3(10.0, 10.0, 1.0));
618 619
    expect(
      scaled.toStringDeep(),
620 621 622 623
      'SemanticsNode#3\n'
      '   STALE\n'
      '   owner: null\n'
      '   Rect.fromLTRB(50.0, 10.0, 70.0, 40.0) scaled by 10.0x\n',
624 625 626 627
    );
    expect(
      scaled.getSemanticsData().toString(),
      'SemanticsData(Rect.fromLTRB(50.0, 10.0, 70.0, 40.0), [10.0,0.0,0.0,0.0; 0.0,10.0,0.0,0.0; 0.0,0.0,1.0,0.0; 0.0,0.0,0.0,1.0])',
628 629
    );
  });
630

631
  test('Custom actions debug properties', () {
632
    final SemanticsConfiguration configuration = SemanticsConfiguration();
633 634 635
    const CustomSemanticsAction action1 = CustomSemanticsAction(label: 'action1');
    const CustomSemanticsAction action2 = CustomSemanticsAction(label: 'action2');
    const CustomSemanticsAction action3 = CustomSemanticsAction(label: 'action3');
636
    configuration.customSemanticsActions = <CustomSemanticsAction, VoidCallback>{
637 638 639
      action1: () { },
      action2: () { },
      action3: () { },
640
    };
641
    final SemanticsNode actionNode = SemanticsNode();
642 643 644 645 646 647 648 649 650 651
    actionNode.updateWith(config: configuration);

    expect(
      actionNode.toStringDeep(minLevel: DiagnosticLevel.hidden),
      'SemanticsNode#1\n'
      '   STALE\n'
      '   owner: null\n'
      '   isMergedIntoParent: false\n'
      '   mergeAllDescendantsIntoThisNode: false\n'
      '   Rect.fromLTRB(0.0, 0.0, 0.0, 0.0)\n'
652
      '   tags: null\n'
653 654 655 656 657 658 659 660 661 662
      '   actions: customAction\n'
      '   customActions: action1, action2, action3\n'
      '   flags: []\n'
      '   invisible\n'
      '   isHidden: false\n'
      '   label: ""\n'
      '   value: ""\n'
      '   increasedValue: ""\n'
      '   decreasedValue: ""\n'
      '   hint: ""\n'
663
      '   tooltip: ""\n'
664 665
      '   textDirection: null\n'
      '   sortKey: null\n'
666
      '   platformViewId: null\n'
667 668
      '   maxValueLength: null\n'
      '   currentValueLength: null\n'
669 670
      '   scrollChildren: null\n'
      '   scrollIndex: null\n'
671 672 673
      '   scrollExtentMin: null\n'
      '   scrollPosition: null\n'
      '   scrollExtentMax: null\n'
674
      '   elevation: 0.0\n'
675
      '   thickness: 0.0\n',
676
    );
677 678
  });

679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
  test('Attributed String can concate', () {
    final AttributedString string1 = AttributedString(
      'string1',
      attributes: <StringAttribute>[
        SpellOutStringAttribute(range: const TextRange(start:0, end:4)),
      ]
    );
    final AttributedString string2 = AttributedString(
        'string2',
        attributes: <StringAttribute>[
          LocaleStringAttribute(locale: const Locale('es', 'MX'), range: const TextRange(start:0, end:4)),
        ]
    );
    final AttributedString result = string1 + string2;
    expect(result.string, 'string1string2');
    expect(result.attributes.length, 2);
    expect(result.attributes[0].range, const TextRange(start:0, end:4));
    expect(result.attributes[0] is SpellOutStringAttribute, isTrue);
697
    expect(result.toString(), "AttributedString('string1string2', attributes: [SpellOutStringAttribute(TextRange(start: 0, end: 4)), LocaleStringAttribute(TextRange(start: 7, end: 11), es-MX)])");
698 699
  });

700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
  test('Semantics id does not repeat', () {
    final SemanticsOwner owner = SemanticsOwner();
    const int expectId = 1400;
    SemanticsNode? nodeToRemove;
    for (int i = 0; i < kMaxFrameworkAccessibilityIdentifier; i++) {
      final SemanticsNode node = SemanticsNode();
      node.attach(owner);
      if (node.id == expectId) {
        nodeToRemove = node;
      }
    }
    nodeToRemove!.detach();
    final SemanticsNode newNode = SemanticsNode();
    newNode.attach(owner);
    // Id is reused.
    expect(newNode.id, expectId);
  });

718 719 720
  test('Tags show up in debug properties', () {
    final SemanticsNode actionNode = SemanticsNode()
      ..tags = <SemanticsTag>{RenderViewport.useTwoPaneSemantics};
721

722 723 724 725
    expect(
      actionNode.toStringDeep(),
      contains('\n   tags: RenderViewport.twoPane\n'),
    );
726 727
  });

728
  test('SemanticsConfiguration getter/setter', () {
729
    final SemanticsConfiguration config = SemanticsConfiguration();
730
    const CustomSemanticsAction customAction = CustomSemanticsAction(label: 'test');
731 732 733

    expect(config.isSemanticBoundary, isFalse);
    expect(config.isButton, isFalse);
734
    expect(config.isLink, isFalse);
735
    expect(config.isMergingSemanticsOfDescendants, isFalse);
736 737
    expect(config.isEnabled, null);
    expect(config.isChecked, null);
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
    expect(config.isSelected, isFalse);
    expect(config.isBlockingSemanticsOfPreviouslyPaintedNodes, isFalse);
    expect(config.isFocused, isFalse);
    expect(config.isTextField, isFalse);

    expect(config.onShowOnScreen, isNull);
    expect(config.onScrollDown, isNull);
    expect(config.onScrollUp, isNull);
    expect(config.onScrollLeft, isNull);
    expect(config.onScrollRight, isNull);
    expect(config.onLongPress, isNull);
    expect(config.onDecrease, isNull);
    expect(config.onIncrease, isNull);
    expect(config.onMoveCursorForwardByCharacter, isNull);
    expect(config.onMoveCursorBackwardByCharacter, isNull);
    expect(config.onTap, isNull);
754
    expect(config.customSemanticsActions[customAction], isNull);
755 756 757

    config.isSemanticBoundary = true;
    config.isButton = true;
758
    config.isLink = true;
759
    config.isMergingSemanticsOfDescendants = true;
760
    config.isEnabled = true;
761 762 763 764 765 766
    config.isChecked = true;
    config.isSelected = true;
    config.isBlockingSemanticsOfPreviouslyPaintedNodes = true;
    config.isFocused = true;
    config.isTextField = true;

767 768 769 770 771 772 773 774 775 776 777 778
    void onShowOnScreen() { }
    void onScrollDown() { }
    void onScrollUp() { }
    void onScrollLeft() { }
    void onScrollRight() { }
    void onLongPress() { }
    void onDecrease() { }
    void onIncrease() { }
    void onMoveCursorForwardByCharacter(bool _) { }
    void onMoveCursorBackwardByCharacter(bool _) { }
    void onTap() { }
    void onCustomAction() { }
779 780 781 782 783 784 785 786 787 788 789 790

    config.onShowOnScreen = onShowOnScreen;
    config.onScrollDown = onScrollDown;
    config.onScrollUp = onScrollUp;
    config.onScrollLeft = onScrollLeft;
    config.onScrollRight = onScrollRight;
    config.onLongPress = onLongPress;
    config.onDecrease = onDecrease;
    config.onIncrease = onIncrease;
    config.onMoveCursorForwardByCharacter = onMoveCursorForwardByCharacter;
    config.onMoveCursorBackwardByCharacter = onMoveCursorBackwardByCharacter;
    config.onTap = onTap;
791
    config.customSemanticsActions[customAction] = onCustomAction;
792 793 794

    expect(config.isSemanticBoundary, isTrue);
    expect(config.isButton, isTrue);
795
    expect(config.isLink, isTrue);
796
    expect(config.isMergingSemanticsOfDescendants, isTrue);
797
    expect(config.isEnabled, isTrue);
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
    expect(config.isChecked, isTrue);
    expect(config.isSelected, isTrue);
    expect(config.isBlockingSemanticsOfPreviouslyPaintedNodes, isTrue);
    expect(config.isFocused, isTrue);
    expect(config.isTextField, isTrue);

    expect(config.onShowOnScreen, same(onShowOnScreen));
    expect(config.onScrollDown, same(onScrollDown));
    expect(config.onScrollUp, same(onScrollUp));
    expect(config.onScrollLeft, same(onScrollLeft));
    expect(config.onScrollRight, same(onScrollRight));
    expect(config.onLongPress, same(onLongPress));
    expect(config.onDecrease, same(onDecrease));
    expect(config.onIncrease, same(onIncrease));
    expect(config.onMoveCursorForwardByCharacter, same(onMoveCursorForwardByCharacter));
    expect(config.onMoveCursorBackwardByCharacter, same(onMoveCursorBackwardByCharacter));
    expect(config.onTap, same(onTap));
815
    expect(config.customSemanticsActions[customAction], same(onCustomAction));
816
  });
817 818 819 820
}

class TestRender extends RenderProxyBox {

821
  TestRender({
822 823 824 825 826 827
    this.hasTapAction = false,
    this.hasLongPressAction = false,
    this.hasScrollLeftAction = false,
    this.hasScrollRightAction = false,
    this.hasScrollUpAction = false,
    this.hasScrollDownAction = false,
828 829
    this.isSemanticBoundary = false,
    RenderBox? child,
830 831 832 833 834 835 836 837 838
  }) : super(child);

  bool hasTapAction;
  bool hasLongPressAction;
  bool hasScrollLeftAction;
  bool hasScrollRightAction;
  bool hasScrollUpAction;
  bool hasScrollDownAction;
  bool isSemanticBoundary;
839 840

  @override
841 842
  void describeSemanticsConfiguration(SemanticsConfiguration config) {
    super.describeSemanticsConfiguration(config);
843

844
    config.isSemanticBoundary = isSemanticBoundary;
845
    if (hasTapAction) {
846
      config.onTap = () { };
847 848
    }
    if (hasLongPressAction) {
849
      config.onLongPress = () { };
850 851
    }
    if (hasScrollLeftAction) {
852
      config.onScrollLeft = () { };
853 854
    }
    if (hasScrollRightAction) {
855
      config.onScrollRight = () { };
856 857
    }
    if (hasScrollUpAction) {
858
      config.onScrollUp = () { };
859 860
    }
    if (hasScrollDownAction) {
861
      config.onScrollDown = () { };
862
    }
863
  }
864
}
865 866

class CustomSortKey extends OrdinalSortKey {
867
  const CustomSortKey(super.order, {super.name});
868
}