dialog_theme_test.dart 12.1 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 54
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
    const DialogTheme(
      backgroundColor: Color(0xff123456),
      elevation: 8.0,
      shape: null,
55
      alignment: Alignment.bottomLeft,
56
      titleTextStyle: TextStyle(color: Color(0xffffffff)),
57
      contentTextStyle: TextStyle(color: Color(0xff000000)),
58 59 60 61 62 63
    ).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)',
64
      'elevation: 8.0',
65
      'alignment: Alignment.bottomLeft',
66 67
      'titleTextStyle: TextStyle(inherit: true, color: Color(0xffffffff))',
      'contentTextStyle: TextStyle(inherit: true, color: Color(0xff000000))',
68 69 70 71 72 73 74 75 76 77 78
    ]);
  });

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

79
    await tester.pumpWidget(_appWithDialog(tester, dialog, theme: theme));
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    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(
96
      _appWithDialog(tester, dialog, theme: theme),
97 98 99 100 101 102 103 104
    );
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

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

105 106 107 108 109 110 111 112 113 114
  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(
115
      _appWithDialog(tester, dialog, theme: theme),
116 117
    );
    await tester.tap(find.text('X'));
118
    await tester.pumpAndSettle();
119

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

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 164
  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);
  });

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

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

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

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

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

197
    final RenderParagraph title = _getTextRenderObject(tester, titleText);
198 199 200 201 202 203 204 205 206 207 208 209
    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));

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

214
    final RenderParagraph title = _getTextRenderObject(tester, titleText);
215 216 217 218 219 220 221 222 223 224
    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>[ ],
    );
225
    final ThemeData theme = ThemeData(textTheme: const TextTheme(headline6: titleTextStyle));
226

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

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

  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);
280
    expect(title.text.style!.color, titleTextStyle.color);
281 282 283 284 285 286 287 288 289 290 291
  });

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

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

296
    final RenderParagraph content = _getTextRenderObject(tester, contentText);
297 298 299 300 301 302 303 304 305 306 307 308
    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));

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

313
    final RenderParagraph content = _getTextRenderObject(tester, contentText);
314 315 316 317 318 319 320 321 322 323
    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>[ ],
    );
324
    final ThemeData theme = ThemeData(textTheme: const TextTheme(subtitle1: contentTextStyle));
325

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

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