dialog_theme_test.dart 10.5 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
import 'package:flutter_test/flutter_test.dart';

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

final Key _painterKey = UniqueKey();

36 37 38 39
Material _getMaterialFromDialog(WidgetTester tester) {
  return tester.widget<Material>(find.descendant(of: find.byType(AlertDialog), matching: find.byType(Material)));
}

40
RenderParagraph _getTextRenderObject(WidgetTester tester, String text) {
41
  return tester.element<StatelessElement>(find.text(text)).renderObject! as RenderParagraph;
42 43
}

44
void main() {
45
  testWidgets('Dialog Theme implements debugFillProperties', (WidgetTester tester) async {
46 47 48 49 50
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
    const DialogTheme(
      backgroundColor: Color(0xff123456),
      elevation: 8.0,
      shape: null,
51
      titleTextStyle: TextStyle(color: Color(0xffffffff)),
52
      contentTextStyle: TextStyle(color: Color(0xff000000)),
53 54 55 56 57 58
    ).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)',
59 60 61
      'elevation: 8.0',
      'titleTextStyle: TextStyle(inherit: true, color: Color(0xffffffff))',
      'contentTextStyle: TextStyle(inherit: true, color: Color(0xff000000))',
62 63 64 65 66 67 68 69 70 71 72
    ]);
  });

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

73
    await tester.pumpWidget(_appWithDialog(tester, dialog, theme: theme));
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    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(
90
        _appWithDialog(tester, dialog, theme: theme)
91 92 93 94 95 96 97 98
    );
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

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

99 100 101 102 103 104 105 106 107 108
  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(
109
      _appWithDialog(tester, dialog, theme: theme)
110 111
    );
    await tester.tap(find.text('X'));
112
    await tester.pumpAndSettle();
113

114
    final Material materialWidget = _getMaterialFromDialog(tester);
115 116 117 118 119 120 121 122 123 124 125 126
    expect(materialWidget.shape, customBorder);
  });

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

127
    await tester.pumpWidget(_appWithDialog(tester, dialog, theme: theme));
128 129 130 131 132
    await tester.tap(find.text('X'));
    await tester.pumpAndSettle();

    await expectLater(
      find.byKey(_painterKey),
133
      matchesGoldenFile('dialog_theme.dialog_with_custom_border.png'),
134
    );
135
  });
136 137 138 139 140 141 142 143 144 145

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

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

150
    final RenderParagraph title = _getTextRenderObject(tester, titleText);
151 152 153 154 155 156 157 158 159 160 161 162
    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));

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

167
    final RenderParagraph title = _getTextRenderObject(tester, titleText);
168 169 170 171 172 173 174 175 176 177
    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>[ ],
    );
178
    final ThemeData theme = ThemeData(textTheme: const TextTheme(headline6: titleTextStyle));
179

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

184
    final RenderParagraph title = _getTextRenderObject(tester, titleText);
185
    expect(title.text.style!.color, titleTextStyle.color);
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 224 225 226 227 228 229 230 231 232
  });

  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);
233
    expect(title.text.style!.color, titleTextStyle.color);
234 235 236 237 238 239 240 241 242 243 244
  });

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

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

249
    final RenderParagraph content = _getTextRenderObject(tester, contentText);
250 251 252 253 254 255 256 257 258 259 260 261
    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));

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

266
    final RenderParagraph content = _getTextRenderObject(tester, contentText);
267 268 269 270 271 272 273 274 275 276
    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>[ ],
    );
277
    final ThemeData theme = ThemeData(textTheme: const TextTheme(subtitle1: contentTextStyle));
278

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

283
    final RenderParagraph content = _getTextRenderObject(tester, contentText);
284
    expect(content.text.style!.color, contentTextStyle.color);
285
  });
286
}