card_test.dart 6.3 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/material.dart';
6
import 'package:flutter/rendering.dart';
7 8 9 10 11 12
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 {
13
    final SemanticsTester semantics = SemanticsTester(tester);
14
    await tester.pumpWidget(
15
      Directionality(
16
        textDirection: TextDirection.ltr,
17 18 19
        child: Material(
          child: Center(
            child: Card(
20
              semanticContainer: false,
21
              child: Column(
22 23 24
                children: <Widget>[
                  const Text('I am text!'),
                  const Text('Moar text!!1'),
25
                  MaterialButton(
26 27
                    child: const Text('Button'),
                    onPressed: () { },
28
                  ),
29
                ],
30
              ),
31 32 33 34 35 36 37
            ),
          ),
        ),
      ),
    );

    expect(semantics, hasSemantics(
38
      TestSemantics.root(
39
        children: <TestSemantics>[
40
          TestSemantics(
41
            id: 1,
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
            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.hasEnabledState,
64
                  SemanticsFlag.isButton,
65
                  SemanticsFlag.isEnabled,
66
                  SemanticsFlag.isFocusable,
67 68
                ],
              ),
69
            ],
70
          ),
71 72 73 74 75 76 77 78 79 80
        ],
      ),
      ignoreTransform: true,
      ignoreRect: true,
    ));

    semantics.dispose();
  });

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

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

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

    semantics.dispose();
  });

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

    await tester.pumpWidget(
123
      Container(
124
        alignment: Alignment.topLeft,
125 126
        child: Card(
          child: Container(
127 128 129 130 131 132 133 134 135 136
            key: contentsKey,
            color: const Color(0xFF00FF00),
            width: 100.0,
            height: 100.0,
          ),
        ),
      ),
    );

    // Default margin is 4
137
    expect(tester.getTopLeft(find.byType(Card)), Offset.zero);
138 139 140 141 142 143
    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(
144
      Container(
145
        alignment: Alignment.topLeft,
146
        child: Card(
147
          margin: EdgeInsets.zero,
148
          child: Container(
149 150 151 152 153 154 155 156 157 158
            key: contentsKey,
            color: const Color(0xFF00FF00),
            width: 100.0,
            height: 100.0,
          ),
        ),
      ),
    );

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

162
    expect(tester.getTopLeft(find.byKey(contentsKey)), Offset.zero);
163 164 165
    expect(tester.getSize(find.byKey(contentsKey)), const Size(100.0, 100.0));
  });

166 167 168 169 170 171 172
  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);
  });
173 174 175

  testWidgets('Card clipBehavior property defers to theme when null', (WidgetTester tester) async {
    await tester.pumpWidget(Builder(builder: (BuildContext context) {
176
      final ThemeData themeData = Theme.of(context);
177 178 179 180 181 182
      return Theme(
        data: themeData.copyWith(
          cardTheme: themeData.cardTheme.copyWith(
            clipBehavior: Clip.antiAliasWithSaveLayer,
          ),
        ),
183
        child: const Card(),
184 185 186 187
      );
    }));
    expect(tester.widget<Material>(find.byType(Material)).clipBehavior, Clip.antiAliasWithSaveLayer);
  });
188 189

  testWidgets('Card shadowColor', (WidgetTester tester) async {
190
    Material getCardMaterial(WidgetTester tester) {
191 192 193 194 195 196 197 198
      return tester.widget<Material>(
        find.descendant(
          of: find.byType(Card),
          matching: find.byType(Material),
        ),
      );
    }

199
    Card getCard(WidgetTester tester) {
200
      return tester.widget<Card>(
201
        find.byType(Card),
202 203 204 205 206 207 208
      );
    }

    await tester.pumpWidget(
      const Card(),
    );

209 210
    expect(getCard(tester).shadowColor, null);
    expect(getCardMaterial(tester).shadowColor, const Color(0xFF000000));
211 212 213 214 215 216 217

    await tester.pumpWidget(
      const Card(
        shadowColor: Colors.red,
      ),
    );

218 219
    expect(getCardMaterial(tester).shadowColor, getCard(tester).shadowColor);
    expect(getCardMaterial(tester).shadowColor, Colors.red);
220
  });
221
}