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

5 6 7 8
// This file is run as part of a reduced test set in CI on Mac and Windows
// machines.
@Tags(<String>['reduced-test-set'])

9
import 'package:flutter/material.dart';
10
import 'package:flutter/rendering.dart';
11 12
import 'package:flutter_test/flutter_test.dart';

13
MaterialApp _appWithDialog(WidgetTester tester, Widget dialog, { ThemeData? theme }) {
14 15 16
  return MaterialApp(
    theme: theme,
    home: Material(
17 18 19
      child: Builder(
        builder: (BuildContext context) {
          return Center(
20
            child: ElevatedButton(
21 22 23 24 25 26 27 28 29 30 31 32 33
              child: const Text('X'),
              onPressed: () {
                showDialog<void>(
                  context: context,
                  builder: (BuildContext context) {
                    return RepaintBoundary(key: _painterKey, child: dialog);
                  },
                );
              },
            ),
          );
        },
      ),
34 35 36 37 38 39
    ),
  );
}

final Key _painterKey = UniqueKey();

40 41 42 43
Material _getMaterialFromDialog(WidgetTester tester) {
  return tester.widget<Material>(find.descendant(of: find.byType(AlertDialog), matching: find.byType(Material)));
}

44
RenderParagraph _getTextRenderObject(WidgetTester tester, String text) {
45
  return tester.element<StatelessElement>(find.text(text)).renderObject! as RenderParagraph;
46 47
}

48
void main() {
49
  testWidgets('Dialog Theme implements debugFillProperties', (WidgetTester tester) async {
50 51 52 53
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
    const DialogTheme(
      backgroundColor: Color(0xff123456),
      elevation: 8.0,
54
      alignment: Alignment.bottomLeft,
55
      titleTextStyle: TextStyle(color: Color(0xffffffff)),
56
      contentTextStyle: TextStyle(color: Color(0xff000000)),
57 58 59 60 61 62
    ).debugFillProperties(builder);
    final List<String> description = builder.properties
        .where((DiagnosticsNode n) => !n.isFiltered(DiagnosticLevel.info))
        .map((DiagnosticsNode n) => n.toString()).toList();
    expect(description, <String>[
      'backgroundColor: Color(0xff123456)',
63
      'elevation: 8.0',
64
      'alignment: Alignment.bottomLeft',
65 66
      'titleTextStyle: TextStyle(inherit: true, color: Color(0xffffffff))',
      'contentTextStyle: TextStyle(inherit: true, color: Color(0xff000000))',
67 68 69 70 71 72 73 74 75 76 77
    ]);
  });

  testWidgets('Dialog background color', (WidgetTester tester) async {
    const Color customColor = Colors.pink;
    const AlertDialog dialog = AlertDialog(
      title: Text('Title'),
      actions: <Widget>[ ],
    );
    final ThemeData theme = ThemeData(dialogTheme: const DialogTheme(backgroundColor: customColor));

78
    await tester.pumpWidget(_appWithDialog(tester, dialog, theme: theme));
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

    final Material materialWidget = _getMaterialFromDialog(tester);
    expect(materialWidget.color, customColor);
  });

  testWidgets('Custom dialog elevation', (WidgetTester tester) async {
    const double customElevation = 12.0;
    const AlertDialog dialog = AlertDialog(
      title: Text('Title'),
      actions: <Widget>[ ],
    );
    final ThemeData theme = ThemeData(dialogTheme: const DialogTheme(elevation: customElevation));

    await tester.pumpWidget(
95
      _appWithDialog(tester, dialog, theme: theme),
96 97 98 99 100 101 102 103
    );
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

    final Material materialWidget = _getMaterialFromDialog(tester);
    expect(materialWidget.elevation, customElevation);
  });

104 105 106 107 108 109 110 111 112 113
  testWidgets('Custom dialog shape', (WidgetTester tester) async {
    const RoundedRectangleBorder customBorder =
      RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16.0)));
    const AlertDialog dialog = AlertDialog(
      title: Text('Title'),
      actions: <Widget>[ ],
    );
    final ThemeData theme = ThemeData(dialogTheme: const DialogTheme(shape: customBorder));

    await tester.pumpWidget(
114
      _appWithDialog(tester, dialog, theme: theme),
115 116
    );
    await tester.tap(find.text('X'));
117
    await tester.pumpAndSettle();
118

119
    final Material materialWidget = _getMaterialFromDialog(tester);
120 121 122
    expect(materialWidget.shape, customBorder);
  });

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
  testWidgets('Custom dialog alignment', (WidgetTester tester) async {
    const AlertDialog dialog = AlertDialog(
      title: Text('Title'),
      actions: <Widget>[ ],
    );
    final ThemeData theme = ThemeData(dialogTheme: const DialogTheme(alignment: Alignment.bottomLeft));

    await tester.pumpWidget(
      _appWithDialog(tester, dialog, theme: theme),
    );
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

    final Offset bottomLeft = tester.getBottomLeft(
      find.descendant(of: find.byType(Dialog), matching: find.byType(Material)),
    );
    expect(bottomLeft.dx, 40.0);
    expect(bottomLeft.dy, 576.0);
  });

  testWidgets('Dialog alignment takes priority over theme', (WidgetTester tester) async {
    const AlertDialog dialog = AlertDialog(
      title: Text('Title'),
      actions: <Widget>[ ],
      alignment: Alignment.topRight,
    );
    final ThemeData theme = ThemeData(dialogTheme: const DialogTheme(alignment: Alignment.bottomLeft));

    await tester.pumpWidget(
      _appWithDialog(tester, dialog, theme: theme),
    );
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

    final Offset bottomLeft = tester.getBottomLeft(
      find.descendant(of: find.byType(Dialog), matching: find.byType(Material)),
    );
    expect(bottomLeft.dx, 480.0);
    expect(bottomLeft.dy, 104.0);
  });

164 165 166 167 168 169 170 171 172
  testWidgets('Custom dialog shape matches golden', (WidgetTester tester) async {
    const RoundedRectangleBorder customBorder =
      RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(16.0)));
    const AlertDialog dialog = AlertDialog(
      title: Text('Title'),
      actions: <Widget>[ ],
    );
    final ThemeData theme = ThemeData(dialogTheme: const DialogTheme(shape: customBorder));

173
    await tester.pumpWidget(_appWithDialog(tester, dialog, theme: theme));
174 175 176 177 178
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

    await expectLater(
      find.byKey(_painterKey),
179
      matchesGoldenFile('dialog_theme.dialog_with_custom_border.png'),
180
    );
181
  });
182 183 184 185 186 187 188 189 190 191

  testWidgets('Custom Title Text Style - Constructor Param', (WidgetTester tester) async {
    const String titleText = 'Title';
    const TextStyle titleTextStyle = TextStyle(color: Colors.pink);
    const AlertDialog dialog = AlertDialog(
      title: Text(titleText),
      titleTextStyle: titleTextStyle,
      actions: <Widget>[ ],
    );

192
    await tester.pumpWidget(_appWithDialog(tester, dialog));
193 194 195
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

196
    final RenderParagraph title = _getTextRenderObject(tester, titleText);
197 198 199 200 201 202 203 204 205 206 207 208
    expect(title.text.style, titleTextStyle);
  });

  testWidgets('Custom Title Text Style - Dialog Theme', (WidgetTester tester) async {
    const String titleText = 'Title';
    const TextStyle titleTextStyle = TextStyle(color: Colors.pink);
    const AlertDialog dialog = AlertDialog(
      title: Text(titleText),
      actions: <Widget>[ ],
    );
    final ThemeData theme = ThemeData(dialogTheme: const DialogTheme(titleTextStyle: titleTextStyle));

209
    await tester.pumpWidget(_appWithDialog(tester, dialog, theme: theme));
210 211 212
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

213
    final RenderParagraph title = _getTextRenderObject(tester, titleText);
214 215 216 217 218 219 220 221 222 223
    expect(title.text.style, titleTextStyle);
  });

  testWidgets('Custom Title Text Style - Theme', (WidgetTester tester) async {
    const String titleText = 'Title';
    const TextStyle titleTextStyle = TextStyle(color: Colors.pink);
    const AlertDialog dialog = AlertDialog(
      title: Text(titleText),
      actions: <Widget>[ ],
    );
224
    final ThemeData theme = ThemeData(textTheme: const TextTheme(headline6: titleTextStyle));
225

226
    await tester.pumpWidget(_appWithDialog(tester, dialog, theme: theme));
227 228 229
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

230
    final RenderParagraph title = _getTextRenderObject(tester, titleText);
231
    expect(title.text.style!.color, titleTextStyle.color);
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
  });

  testWidgets('Simple Dialog - Custom Title Text Style - Constructor Param', (WidgetTester tester) async {
    const String titleText = 'Title';
    const TextStyle titleTextStyle = TextStyle(color: Colors.pink);
    const SimpleDialog dialog = SimpleDialog(
      title: Text(titleText),
      titleTextStyle: titleTextStyle,
    );

    await tester.pumpWidget(_appWithDialog(tester, dialog));
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

    final RenderParagraph title = _getTextRenderObject(tester, titleText);
    expect(title.text.style, titleTextStyle);
  });

  testWidgets('Simple Dialog - Custom Title Text Style - Dialog Theme', (WidgetTester tester) async {
    const String titleText = 'Title';
    const TextStyle titleTextStyle = TextStyle(color: Colors.pink);
    const SimpleDialog dialog = SimpleDialog(
      title: Text(titleText),
    );
    final ThemeData theme = ThemeData(dialogTheme: const DialogTheme(titleTextStyle: titleTextStyle));

    await tester.pumpWidget(_appWithDialog(tester, dialog, theme: theme));
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

    final RenderParagraph title = _getTextRenderObject(tester, titleText);
    expect(title.text.style, titleTextStyle);
  });

  testWidgets('Simple Dialog - Custom Title Text Style - Theme', (WidgetTester tester) async {
    const String titleText = 'Title';
    const TextStyle titleTextStyle = TextStyle(color: Colors.pink);
    const SimpleDialog dialog = SimpleDialog(
      title: Text(titleText),
    );
    final ThemeData theme = ThemeData(textTheme: const TextTheme(headline6: titleTextStyle));

    await tester.pumpWidget(_appWithDialog(tester, dialog, theme: theme));
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

    final RenderParagraph title = _getTextRenderObject(tester, titleText);
279
    expect(title.text.style!.color, titleTextStyle.color);
280 281 282 283 284 285 286 287 288 289 290
  });

  testWidgets('Custom Content Text Style - Constructor Param', (WidgetTester tester) async {
    const String contentText = 'Content';
    const TextStyle contentTextStyle = TextStyle(color: Colors.pink);
    const AlertDialog dialog = AlertDialog(
      content: Text(contentText),
      contentTextStyle: contentTextStyle,
      actions: <Widget>[ ],
    );

291
    await tester.pumpWidget(_appWithDialog(tester, dialog));
292 293 294
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

295
    final RenderParagraph content = _getTextRenderObject(tester, contentText);
296 297 298 299 300 301 302 303 304 305 306 307
    expect(content.text.style, contentTextStyle);
  });

  testWidgets('Custom Content Text Style - Dialog Theme', (WidgetTester tester) async {
    const String contentText = 'Content';
    const TextStyle contentTextStyle = TextStyle(color: Colors.pink);
    const AlertDialog dialog = AlertDialog(
      content: Text(contentText),
      actions: <Widget>[ ],
    );
    final ThemeData theme = ThemeData(dialogTheme: const DialogTheme(contentTextStyle: contentTextStyle));

308
    await tester.pumpWidget(_appWithDialog(tester, dialog, theme: theme));
309 310 311
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

312
    final RenderParagraph content = _getTextRenderObject(tester, contentText);
313 314 315 316 317 318 319 320 321 322
    expect(content.text.style, contentTextStyle);
  });

  testWidgets('Custom Content Text Style - Theme', (WidgetTester tester) async {
    const String contentText = 'Content';
    const TextStyle contentTextStyle = TextStyle(color: Colors.pink);
    const AlertDialog dialog = AlertDialog(
      content: Text(contentText),
      actions: <Widget>[ ],
    );
323
    final ThemeData theme = ThemeData(textTheme: const TextTheme(subtitle1: contentTextStyle));
324

325
    await tester.pumpWidget(_appWithDialog(tester, dialog, theme: theme));
326 327 328
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

329
    final RenderParagraph content = _getTextRenderObject(tester, contentText);
330
    expect(content.text.style!.color, contentTextStyle.color);
331
  });
332
}