card_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
import 'package:flutter/painting.dart';
6 7 8 9 10 11 12 13 14
import 'package:flutter/rendering.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

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

void main() {
  testWidgets('Card can take semantic text from multiple children', (WidgetTester tester) async {
15
    final SemanticsTester semantics = SemanticsTester(tester);
16
    await tester.pumpWidget(
17
      Directionality(
18
        textDirection: TextDirection.ltr,
19 20 21
        child: Material(
          child: Center(
            child: Card(
22
              semanticContainer: false,
23
              child: Column(
24 25 26
                children: <Widget>[
                  const Text('I am text!'),
                  const Text('Moar text!!1'),
27
                  MaterialButton(
28 29
                    child: const Text('Button'),
                    onPressed: () { },
30
                  ),
31
                ],
32
              ),
33 34 35 36 37 38 39
            ),
          ),
        ),
      ),
    );

    expect(semantics, hasSemantics(
40
      TestSemantics.root(
41
        children: <TestSemantics>[
42
          TestSemantics(
43
            id: 1,
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
            elevation: 1.0,
            thickness: 0.0,
            children: <TestSemantics>[
              TestSemantics(
                id: 2,
                label: 'I am text!',
                textDirection: TextDirection.ltr,
              ),
              TestSemantics(
                id: 3,
                label: 'Moar text!!1',
                textDirection: TextDirection.ltr,
              ),
              TestSemantics(
                id: 4,
                label: 'Button',
                textDirection: TextDirection.ltr,
                actions: <SemanticsAction>[
                  SemanticsAction.tap,
                ],
                flags: <SemanticsFlag>[
                  SemanticsFlag.isButton,
                  SemanticsFlag.hasEnabledState,
                  SemanticsFlag.isEnabled,
                ],
              ),
70
            ],
71
          ),
72 73 74 75 76 77 78 79 80 81
        ],
      ),
      ignoreTransform: true,
      ignoreRect: true,
    ));

    semantics.dispose();
  });

  testWidgets('Card merges children when it is a semanticContainer', (WidgetTester tester) async {
82
    final SemanticsTester semantics = SemanticsTester(tester);
83 84 85
    debugResetSemanticsIdCounter();

    await tester.pumpWidget(
86
      Directionality(
87
        textDirection: TextDirection.ltr,
88 89 90
        child: Material(
          child: Center(
            child: Card(
91
              semanticContainer: true,
92
              child: Column(
93 94
                children: const <Widget>[
                  Text('First child'),
95
                  Text('Second child'),
96
                ],
97
              ),
98 99 100 101 102 103 104
            ),
          ),
        ),
      ),
    );

    expect(semantics, hasSemantics(
105
      TestSemantics.root(
106
        children: <TestSemantics>[
107
          TestSemantics(
108 109 110
            id: 1,
            label: 'First child\nSecond child',
            textDirection: TextDirection.ltr,
111 112 113 114 115 116 117 118 119 120
          ),
        ],
      ),
      ignoreTransform: true,
      ignoreRect: true,
    ));

    semantics.dispose();
  });

121
  testWidgets('Card margin', (WidgetTester tester) async {
122
    const Key contentsKey = ValueKey<String>('contents');
123 124

    await tester.pumpWidget(
125
      Container(
126
        alignment: Alignment.topLeft,
127 128
        child: Card(
          child: Container(
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
            key: contentsKey,
            color: const Color(0xFF00FF00),
            width: 100.0,
            height: 100.0,
          ),
        ),
      ),
    );

    // Default margin is 4
    expect(tester.getTopLeft(find.byType(Card)), const Offset(0.0, 0.0));
    expect(tester.getSize(find.byType(Card)), const Size(108.0, 108.0));

    expect(tester.getTopLeft(find.byKey(contentsKey)), const Offset(4.0, 4.0));
    expect(tester.getSize(find.byKey(contentsKey)), const Size(100.0, 100.0));

    await tester.pumpWidget(
146
      Container(
147
        alignment: Alignment.topLeft,
148
        child: Card(
149
          margin: EdgeInsets.zero,
150
          child: Container(
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
            key: contentsKey,
            color: const Color(0xFF00FF00),
            width: 100.0,
            height: 100.0,
          ),
        ),
      ),
    );

    // Specified margin is zero
    expect(tester.getTopLeft(find.byType(Card)), const Offset(0.0, 0.0));
    expect(tester.getSize(find.byType(Card)), const Size(100.0, 100.0));

    expect(tester.getTopLeft(find.byKey(contentsKey)), const Offset(0.0, 0.0));
    expect(tester.getSize(find.byKey(contentsKey)), const Size(100.0, 100.0));
  });

168 169 170 171 172 173 174
  testWidgets('Card clipBehavior property passes through to the Material', (WidgetTester tester) async {
    await tester.pumpWidget(const Card());
    expect(tester.widget<Material>(find.byType(Material)).clipBehavior, Clip.none);

    await tester.pumpWidget(const Card(clipBehavior: Clip.antiAlias));
    expect(tester.widget<Material>(find.byType(Material)).clipBehavior, Clip.antiAlias);
  });
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

  testWidgets('Card clipBehavior property defers to theme when null', (WidgetTester tester) async {
    await tester.pumpWidget(Builder(builder: (BuildContext context) {
      final ThemeData themeData = Theme.of(context);
      return Theme(
        data: themeData.copyWith(
          cardTheme: themeData.cardTheme.copyWith(
            clipBehavior: Clip.antiAliasWithSaveLayer,
          ),
        ),
        child: const Card(clipBehavior: null),
      );
    }));
    expect(tester.widget<Material>(find.byType(Material)).clipBehavior, Clip.antiAliasWithSaveLayer);
  });
190
}