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
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7
import 'package:flutter/material.dart';
8
import 'package:flutter/rendering.dart';
9 10
import 'package:flutter_test/flutter_test.dart';

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

final Key _painterKey = UniqueKey();

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

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

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

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

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

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

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

116
    final Material materialWidget = _getMaterialFromDialog(tester);
117 118 119 120 121 122 123 124 125 126 127 128
    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));

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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