semantics_tester_generateTestSemanticsExpressionForCurrentSemanticsTree_test.dart 5.63 KB
Newer Older
1 2 3 4
// Copyright 2017 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.

5
@TestOn('!chrome')
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/semantics.dart';
import 'package:flutter_test/flutter_test.dart';

import 'semantics_tester.dart';

void main() {
  group('generateTestSemanticsExpressionForCurrentSemanticsTree', () {
    _tests();
  });
}

void _tests() {
  setUp(() {
    debugResetSemanticsIdCounter();
  });

26
  Future<void> pumpTestWidget(WidgetTester tester) async {
27 28
    await tester.pumpWidget(MaterialApp(
      home: ListView(
29 30
        children: <Widget>[
          const Text('Plain text'),
31
          Semantics(
32 33
            selected: true,
            checked: true,
34 35
            onTap: () { },
            onDecrease: () { },
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
            value: 'test-value',
            increasedValue: 'test-increasedValue',
            decreasedValue: 'test-decreasedValue',
            hint: 'test-hint',
            textDirection: TextDirection.rtl,
            child: const Text('Interactive text'),
          ),
        ],
      ),
    ));
  }

  // This test generates code using generateTestSemanticsExpressionForCurrentSemanticsTree
  // then compares it to the code used in the 'generated code is correct' test
  // below. When you update the implementation of generateTestSemanticsExpressionForCurrentSemanticsTree
  // also update this code to reflect the new output.
  //
  // This test is flexible w.r.t. leading and trailing whitespace.
  testWidgets('generates code', (WidgetTester tester) async {
55
    final SemanticsTester semantics = SemanticsTester(tester);
56 57
    await pumpTestWidget(tester);
    final String code = semantics
58
      .generateTestSemanticsExpressionForCurrentSemanticsTree(DebugSemanticsDumpOrder.inverseHitTest)
59
      .split('\n')
60
      .map<String>((String line) => line.trim())
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
      .join('\n')
      .trim() + ',';

    File findThisTestFile(Directory directory) {
      for (FileSystemEntity entity in directory.listSync()) {
        if (entity is Directory) {
          final File childSearch = findThisTestFile(entity);
          if (childSearch != null) {
            return childSearch;
          }
        } else if (entity is File && entity.path.endsWith('semantics_tester_generateTestSemanticsExpressionForCurrentSemanticsTree_test.dart')) {
          return entity;
        }
      }
      return null;
    }

    final File thisTestFile = findThisTestFile(Directory.current);
    expect(thisTestFile, isNotNull);
    String expectedCode = thisTestFile.readAsStringSync();
    expectedCode = expectedCode.substring(
82 83
      expectedCode.indexOf('v' * 12) + 12,
      expectedCode.indexOf('^' * 12) - 3,
84 85
    )
      .split('\n')
86
      .map<String>((String line) => line.trim())
87 88 89 90 91 92 93
      .join('\n')
      .trim();
    semantics.dispose();
    expect(code, expectedCode);
  });

  testWidgets('generated code is correct', (WidgetTester tester) async {
94
    final SemanticsTester semantics = SemanticsTester(tester);
95 96 97 98
    await pumpTestWidget(tester);
    expect(
      semantics,
      hasSemantics(
99
        // The code below delimited by "v" and "^" characters is generated by
100 101 102 103
        // generateTestSemanticsExpressionForCurrentSemanticsTree function.
        // You must update it when changing the output generated by
        // generateTestSemanticsExpressionForCurrentSemanticsTree. Otherwise,
        // the test 'generates code', defined above, will fail.
104
        // vvvvvvvvvvvv
105
        TestSemantics.root(
106
          children: <TestSemantics>[
107
            TestSemantics(
108
              id: 1,
109
              textDirection: TextDirection.ltr,
110
              children: <TestSemantics>[
111
                TestSemantics(
112
                  id: 2,
113
                  flags: <SemanticsFlag>[SemanticsFlag.scopesRoute],
114
                  children: <TestSemantics>[
115
                    TestSemantics(
116
                      id: 3,
117
                      children: <TestSemantics>[
118
                        TestSemantics(
119
                          id: 6,
120
                          flags: <SemanticsFlag>[SemanticsFlag.hasImplicitScrolling],
121
                          children: <TestSemantics>[
122
                            TestSemantics(
123 124 125 126 127
                              id: 4,
                              tags: <SemanticsTag>[const SemanticsTag('RenderViewport.twoPane')],
                              label: 'Plain text',
                              textDirection: TextDirection.ltr,
                            ),
128
                            TestSemantics(
129 130 131 132 133 134 135 136 137 138 139 140
                              id: 5,
                              tags: <SemanticsTag>[const SemanticsTag('RenderViewport.twoPane')],
                              flags: <SemanticsFlag>[SemanticsFlag.hasCheckedState, SemanticsFlag.isChecked, SemanticsFlag.isSelected],
                              actions: <SemanticsAction>[SemanticsAction.tap, SemanticsAction.decrease],
                              label: '‪Interactive text‬',
                              value: 'test-value',
                              increasedValue: 'test-increasedValue',
                              decreasedValue: 'test-decreasedValue',
                              hint: 'test-hint',
                              textDirection: TextDirection.rtl,
                            ),
                          ],
141 142
                        ),
                      ],
143 144 145 146 147 148 149
                    ),
                  ],
                ),
              ],
            ),
          ],
        ),
150
        // ^^^^^^^^^^^^
151 152 153
        ignoreRect: true,
        ignoreTransform: true,
        ignoreId: true,
154
      ),
155 156 157 158
    );
    semantics.dispose();
  });
}