semantics_elevation_test.dart 11.1 KB
Newer Older
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
// 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.

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

import '../widgets/semantics_tester.dart';

void main() {
  testWidgets('SemanticsNodes overlapping in z', (WidgetTester tester) async {
    // Cards are semantic boundaries that always own their own SemanticNode,
    // PhysicalModels merge their semantics information into parent.
    //
    // Side view of the widget tree:
    //
    //          Card('abs. elevation: 30') ---------------
    //                                            | 8                  ----------- Card('abs. elevation 25')
    //          Card('abs. elevation: 22') ---------------                  |
    //                                            | 7                       |
    // PhysicalModel('abs. elevation: 15') ---------------                  | 15
    //                                            | 5                       |
    //                                     --------------------------------------- Card('abs. elevation: 10')
    //                                                           | 10
    //                                                           |
    //                                     --------------------------------------- 'ground'
    final SemanticsTester semantics = SemanticsTester(tester);
    await tester.pumpWidget(MaterialApp(
      home: Column(
        children: <Widget>[
          const Text('ground'),
          Card(
            elevation: 10.0,
            child: Column(
              children: <Widget>[
                const Text('absolute elevation: 10'),
                PhysicalModel(
                  elevation: 5.0,
                  color: Colors.black,
                  child: Column(
                    children: <Widget>[
                      const Text('absolute elevation: 15'),
                      Card(
                        elevation: 7.0,
                        child: Column(
                          children: const <Widget>[
                            Text('absolute elevation: 22'),
                            Card(
                              elevation: 8.0,
                              child: Text('absolute elevation: 30'),
53
                            ),
54 55
                          ],
                        ),
56
                      ),
57 58 59 60 61 62
                    ],
                  ),
                ),
                const Card(
                  elevation: 15.0,
                  child: Text('absolute elevation: 25'),
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
        ],
      ),
    ));

    final SemanticsNode ground = tester.getSemantics(find.text('ground'));
    expect(ground.thickness, 0.0);
    expect(ground.elevation, 0.0);
    expect(ground.label, 'ground');

    final SemanticsNode elevation10 = tester.getSemantics(find.text('absolute elevation: 10'));
    final SemanticsNode elevation15 = tester.getSemantics(find.text('absolute elevation: 15'));
    expect(elevation10, same(elevation15)); // configs got merged into each other.
    expect(elevation10.thickness, 15.0);
    expect(elevation10.elevation, 0.0);
    expect(elevation10.label, 'absolute elevation: 10\nabsolute elevation: 15');

    final SemanticsNode elevation22 = tester.getSemantics(find.text('absolute elevation: 22'));
    expect(elevation22.thickness, 7.0);
    expect(elevation22.elevation, 15.0);
    expect(elevation22.label, 'absolute elevation: 22');

    final SemanticsNode elevation25 = tester.getSemantics(find.text('absolute elevation: 25'));
    expect(elevation25.thickness, 15.0);
    expect(elevation25.elevation, 10.0);
    expect(elevation22.label, 'absolute elevation: 22');

    final SemanticsNode elevation30 = tester.getSemantics(find.text('absolute elevation: 30'));
    expect(elevation30.thickness, 8.0);
    expect(elevation30.elevation, 7.0);
    expect(elevation30.label, 'absolute elevation: 30');

    semantics.dispose();
  });

  testWidgets('SemanticsNodes overlapping in z with switched children', (WidgetTester tester) async {
    // Same as 'SemanticsNodes overlapping in z', but the order of children
    // is reversed

    final SemanticsTester semantics = SemanticsTester(tester);
    await tester.pumpWidget(MaterialApp(
      home: Column(
        children: <Widget>[
          const Text('ground'),
          Card(
            elevation: 10.0,
            child: Column(
              children: <Widget>[
                const Card(
                  elevation: 15.0,
                  child: Text('absolute elevation: 25'),
                ),
                PhysicalModel(
                  elevation: 5.0,
                  color: Colors.black,
                  child: Column(
                    children: <Widget>[
                      const Text('absolute elevation: 15'),
                      Card(
                        elevation: 7.0,
                        child: Column(
                          children: const <Widget>[
                            Text('absolute elevation: 22'),
                            Card(
                              elevation: 8.0,
                              child: Text('absolute elevation: 30'),
132
                            ),
133 134
                          ],
                        ),
135
                      ),
136 137 138 139 140 141
                    ],
                  ),
                ),
                const Text('absolute elevation: 10'),
              ],
            ),
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
        ],
      ),
    ));

    final SemanticsNode ground = tester.getSemantics(find.text('ground'));
    expect(ground.thickness, 0.0);
    expect(ground.elevation, 0.0);
    expect(ground.label, 'ground');

    final SemanticsNode elevation10 = tester.getSemantics(find.text('absolute elevation: 10'));
    final SemanticsNode elevation15 = tester.getSemantics(find.text('absolute elevation: 15'));
    expect(elevation10, same(elevation15)); // configs got merged into each other.
    expect(elevation10.thickness, 15.0);
    expect(elevation10.elevation, 0.0);
    expect(elevation10.label, 'absolute elevation: 15\nabsolute elevation: 10');

    final SemanticsNode elevation22 = tester.getSemantics(find.text('absolute elevation: 22'));
    expect(elevation22.thickness, 7.0);
    expect(elevation22.elevation, 15.0);
    expect(elevation22.label, 'absolute elevation: 22');

    final SemanticsNode elevation25 = tester.getSemantics(find.text('absolute elevation: 25'));
    expect(elevation25.thickness, 15.0);
    expect(elevation25.elevation, 10.0);
    expect(elevation22.label, 'absolute elevation: 22');

    final SemanticsNode elevation30 = tester.getSemantics(find.text('absolute elevation: 30'));
    expect(elevation30.thickness, 8.0);
    expect(elevation30.elevation, 7.0);
    expect(elevation30.label, 'absolute elevation: 30');

    semantics.dispose();
  });

  testWidgets('single node thickness', (WidgetTester tester) async {
    final SemanticsTester semantics = SemanticsTester(tester);

    await tester.pumpWidget(const MaterialApp(
        home: Center(
            child: Material(
              elevation: 24.0,
              child: Text('Hello'),
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
    ));

    final SemanticsNode node = tester.getSemantics(find.text('Hello'));
    expect(node.thickness, 0.0);
    expect(node.elevation, 24.0);
    expect(node.label, 'Hello');

    semantics.dispose();
  });

  testWidgets('force-merge', (WidgetTester tester) async {
    final SemanticsTester semantics = SemanticsTester(tester);

    await tester.pumpWidget(MaterialApp(
        home: Card(
          elevation: 10.0,
          child: Column(
            children: <Widget>[
              const Text('abs. elevation: 10.0'),
              MergeSemantics(
                child: Semantics(
                  explicitChildNodes: true, // just to be sure that it's going to be an explicit merge
                  child: Column(
                    children: const <Widget>[
                      Card(
                        elevation: 15.0,
                        child: Text('abs. elevation 25.0'),
                      ),
                      Card(
                        elevation: 5.0,
                        child: Text('abs. elevation 15.0'),
                      ),
                    ],
                  ),
                ),
              ),
            ],
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
    ));


    final SemanticsNode elevation10 = tester.getSemantics(find.text('abs. elevation: 10.0'));
    expect(elevation10.thickness, 10.0);
    expect(elevation10.elevation, 0.0);
    expect(elevation10.label, 'abs. elevation: 10.0');
    expect(elevation10.childrenCount, 1);

    // TODO(goderbauer): remove awkward workaround when accessing force-merged
    //   SemanticsData becomes easier, https://github.com/flutter/flutter/issues/25669
    SemanticsData mergedChildData;
    elevation10.visitChildren((SemanticsNode child) {
      expect(mergedChildData, isNull);
      mergedChildData = child.getSemanticsData();
      return true;
    });

    expect(mergedChildData.thickness, 15.0);
    expect(mergedChildData.elevation, 10.0);
    expect(mergedChildData.label, 'abs. elevation 25.0\nabs. elevation 15.0');

    semantics.dispose();
  });

  testWidgets('force-merge with inversed children', (WidgetTester tester) async {
    final SemanticsTester semantics = SemanticsTester(tester);

    await tester.pumpWidget(MaterialApp(
        home: Card(
            elevation: 10.0,
            child: Column(
              children: <Widget>[
                const Text('abs. elevation: 10.0'),
                MergeSemantics(
                  child: Semantics(
                    explicitChildNodes: true, // just to be sure that it's going to be an explicit merge
                    child: Column(
                      children: const <Widget>[
                        Card(
                          elevation: 5.0,
                          child: Text('abs. elevation 15.0'),
                        ),
                        Card(
                          elevation: 15.0,
                          child: Text('abs. elevation 25.0'),
                        ),
                      ],

                    ),
                  ),
                ),
              ],
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
    ));


    final SemanticsNode elevation10 = tester.getSemantics(find.text('abs. elevation: 10.0'));
    expect(elevation10.thickness, 10.0);
    expect(elevation10.elevation, 0.0);
    expect(elevation10.label, 'abs. elevation: 10.0');
    expect(elevation10.childrenCount, 1);

    // TODO(goderbauer): remove awkward workaround when accessing force-merged
    //   SemanticsData becomes easier, https://github.com/flutter/flutter/issues/25669
    SemanticsData mergedChildData;
    elevation10.visitChildren((SemanticsNode child) {
      expect(mergedChildData, isNull);
      mergedChildData = child.getSemanticsData();
      return true;
    });

    expect(mergedChildData.thickness, 15.0);
    expect(mergedChildData.elevation, 10.0);
    expect(mergedChildData.label, 'abs. elevation 15.0\nabs. elevation 25.0');

    semantics.dispose();
  });
}