dropdown_menu_theme_test.dart 16.9 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
8
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
9

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
void main() {
  test('DropdownMenuThemeData copyWith, ==, hashCode basics', () {
    expect(const DropdownMenuThemeData(), const DropdownMenuThemeData().copyWith());
    expect(const DropdownMenuThemeData().hashCode, const DropdownMenuThemeData().copyWith().hashCode);

    const DropdownMenuThemeData custom = DropdownMenuThemeData(
      menuStyle: MenuStyle(backgroundColor: MaterialStatePropertyAll<Color>(Colors.green)),
      inputDecorationTheme: InputDecorationTheme(filled: true),
      textStyle: TextStyle(fontSize: 25.0),
    );
    final DropdownMenuThemeData copy = const DropdownMenuThemeData().copyWith(
      menuStyle: custom.menuStyle,
      inputDecorationTheme: custom.inputDecorationTheme,
      textStyle: custom.textStyle,
    );
    expect(copy, custom);
  });

28 29 30 31 32 33
  test('DropdownMenuThemeData lerp special cases', () {
    expect(DropdownMenuThemeData.lerp(null, null, 0), const DropdownMenuThemeData());
    const DropdownMenuThemeData data = DropdownMenuThemeData();
    expect(identical(DropdownMenuThemeData.lerp(data, data, 0.5), data), true);
  });

34
  testWidgetsWithLeakTracking('Default DropdownMenuThemeData debugFillProperties', (WidgetTester tester) async {
35 36 37 38 39 40 41 42 43 44 45
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
    const DropdownMenuThemeData().debugFillProperties(builder);

    final List<String> description = builder.properties
        .where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
        .map((DiagnosticsNode node) => node.toString())
        .toList();

    expect(description, <String>[]);
  });

46
  testWidgetsWithLeakTracking('With no other configuration, defaults are used', (WidgetTester tester) async {
47 48 49 50 51 52
    final ThemeData themeData = ThemeData();
    await tester.pumpWidget(
      MaterialApp(
        theme: themeData,
        home: const Scaffold(
          body: Center(
53 54 55 56 57
            child: DropdownMenu<int>(
              dropdownMenuEntries: <DropdownMenuEntry<int>>[
                DropdownMenuEntry<int>(value: 0, label: 'Item 0'),
                DropdownMenuEntry<int>(value: 1, label: 'Item 1'),
                DropdownMenuEntry<int>(value: 2, label: 'Item 2'),
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
              ],
            ),
          ),
        ),
      )
    );

    final EditableText editableText = tester.widget(find.byType(EditableText));
    expect(editableText.style.color, themeData.textTheme.labelLarge!.color);
    expect(editableText.style.background, themeData.textTheme.labelLarge!.background);
    expect(editableText.style.shadows, themeData.textTheme.labelLarge!.shadows);
    expect(editableText.style.decoration, themeData.textTheme.labelLarge!.decoration);
    expect(editableText.style.locale, themeData.textTheme.labelLarge!.locale);
    expect(editableText.style.wordSpacing, themeData.textTheme.labelLarge!.wordSpacing);

    final TextField textField = tester.widget(find.byType(TextField));
    expect(textField.decoration?.border, const OutlineInputBorder());

    await tester.tap(find.widgetWithIcon(IconButton, Icons.arrow_drop_down).first);
    await tester.pump();
    expect(find.byType(MenuAnchor), findsOneWidget);

    final Finder menuMaterial = find.ancestor(
      of: find.widgetWithText(TextButton, 'Item 0'),
      matching: find.byType(Material),
    ).last;
    Material material = tester.widget<Material>(menuMaterial);
    expect(material.color, themeData.colorScheme.surface);
    expect(material.shadowColor, themeData.colorScheme.shadow);
    expect(material.surfaceTintColor, themeData.colorScheme.surfaceTint);
    expect(material.elevation, 3.0);
    expect(material.shape, const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(4.0))));

    final Finder buttonMaterial = find.descendant(
      of: find.widgetWithText(TextButton, 'Item 0'),
      matching: find.byType(Material),
    ).last;

    material = tester.widget<Material>(buttonMaterial);
    expect(material.color, Colors.transparent);
    expect(material.elevation, 0.0);
    expect(material.shape, const RoundedRectangleBorder());
    expect(material.textStyle?.color, themeData.colorScheme.onSurface);
  });

103
  testWidgetsWithLeakTracking('ThemeData.dropdownMenuTheme overrides defaults', (WidgetTester tester) async {
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    final ThemeData theme = ThemeData(
      dropdownMenuTheme: DropdownMenuThemeData(
        textStyle: TextStyle(
          color: Colors.orange,
          backgroundColor: Colors.indigo,
          fontSize: 30.0,
          shadows: kElevationToShadow[1],
          decoration: TextDecoration.underline,
          wordSpacing: 2.0,
        ),
        menuStyle: const MenuStyle(
          backgroundColor: MaterialStatePropertyAll<Color>(Colors.grey),
          shadowColor: MaterialStatePropertyAll<Color>(Colors.brown),
          surfaceTintColor: MaterialStatePropertyAll<Color>(Colors.amberAccent),
          elevation: MaterialStatePropertyAll<double>(10.0),
          shape: MaterialStatePropertyAll<OutlinedBorder>(
            RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(10.0))),
          ),
        ),
        inputDecorationTheme: const InputDecorationTheme(filled: true, fillColor: Colors.lightGreen),
      )
    );

    await tester.pumpWidget(
      MaterialApp(
        theme: theme,
        home: const Scaffold(
          body: Center(
132 133 134 135 136
            child: DropdownMenu<int>(
              dropdownMenuEntries: <DropdownMenuEntry<int>>[
                DropdownMenuEntry<int>(value: 0, label: 'Item 0'),
                DropdownMenuEntry<int>(value: 1, label: 'Item 1'),
                DropdownMenuEntry<int>(value: 2, label: 'Item 2'),
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
              ],
            ),
          ),
        )
      )
    );

    final EditableText editableText = tester.widget(find.byType(EditableText));
    expect(editableText.style.color, Colors.orange);
    expect(editableText.style.backgroundColor, Colors.indigo);
    expect(editableText.style.shadows, kElevationToShadow[1]);
    expect(editableText.style.decoration, TextDecoration.underline);
    expect(editableText.style.wordSpacing, 2.0);

    final TextField textField = tester.widget(find.byType(TextField));
    expect(textField.decoration?.filled, isTrue);
    expect(textField.decoration?.fillColor, Colors.lightGreen);

    await tester.tap(find.widgetWithIcon(IconButton, Icons.arrow_drop_down).first);
    await tester.pump();
    expect(find.byType(MenuAnchor), findsOneWidget);

    final Finder menuMaterial = find.ancestor(
      of: find.widgetWithText(TextButton, 'Item 0'),
      matching: find.byType(Material),
    ).last;
    Material material = tester.widget<Material>(menuMaterial);
    expect(material.color, Colors.grey);
    expect(material.shadowColor, Colors.brown);
    expect(material.surfaceTintColor, Colors.amberAccent);
    expect(material.elevation, 10.0);
    expect(material.shape, const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(10.0))));

    final Finder buttonMaterial = find.descendant(
      of: find.widgetWithText(TextButton, 'Item 0'),
      matching: find.byType(Material),
    ).last;

    material = tester.widget<Material>(buttonMaterial);
    expect(material.color, Colors.transparent);
    expect(material.elevation, 0.0);
    expect(material.shape, const RoundedRectangleBorder());
    expect(material.textStyle?.color, theme.colorScheme.onSurface);
  });

182
  testWidgetsWithLeakTracking('DropdownMenuTheme overrides ThemeData and defaults', (WidgetTester tester) async {
183 184 185 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
    final DropdownMenuThemeData global = DropdownMenuThemeData(
      textStyle: TextStyle(
        color: Colors.orange,
        backgroundColor: Colors.indigo,
        fontSize: 30.0,
        shadows: kElevationToShadow[1],
        decoration: TextDecoration.underline,
        wordSpacing: 2.0,
      ),
      menuStyle: const MenuStyle(
        backgroundColor: MaterialStatePropertyAll<Color>(Colors.grey),
        shadowColor: MaterialStatePropertyAll<Color>(Colors.brown),
        surfaceTintColor: MaterialStatePropertyAll<Color>(Colors.amberAccent),
        elevation: MaterialStatePropertyAll<double>(10.0),
        shape: MaterialStatePropertyAll<OutlinedBorder>(
          RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(10.0))),
        ),
      ),
      inputDecorationTheme: const InputDecorationTheme(filled: true, fillColor: Colors.lightGreen),
    );

    final DropdownMenuThemeData dropdownMenuTheme = DropdownMenuThemeData(
      textStyle: TextStyle(
        color: Colors.red,
        backgroundColor: Colors.orange,
        fontSize: 27.0,
        shadows: kElevationToShadow[2],
        decoration: TextDecoration.lineThrough,
        wordSpacing: 5.0,
      ),
      menuStyle: const MenuStyle(
        backgroundColor: MaterialStatePropertyAll<Color>(Colors.yellow),
        shadowColor: MaterialStatePropertyAll<Color>(Colors.green),
        surfaceTintColor: MaterialStatePropertyAll<Color>(Colors.teal),
        elevation: MaterialStatePropertyAll<double>(15.0),
        shape: MaterialStatePropertyAll<OutlinedBorder>(
          RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
        ),
      ),
      inputDecorationTheme: const InputDecorationTheme(filled: true, fillColor: Colors.blue),
    );

    final ThemeData theme = ThemeData(dropdownMenuTheme: global);
    await tester.pumpWidget(
      MaterialApp(
        theme: theme,
        home: DropdownMenuTheme(
          data: dropdownMenuTheme,
          child: const Scaffold(
            body: Center(
233 234 235 236 237
              child: DropdownMenu<int>(
                dropdownMenuEntries: <DropdownMenuEntry<int>>[
                  DropdownMenuEntry<int>(value: 0, label: 'Item 0'),
                  DropdownMenuEntry<int>(value: 1, label: 'Item 1'),
                  DropdownMenuEntry<int>(value: 2, label: 'Item 2'),
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 280 281 282 283 284
                ],
              ),
            ),
          ),
        )
      )
    );

    final EditableText editableText = tester.widget(find.byType(EditableText));
    expect(editableText.style.color, Colors.red);
    expect(editableText.style.backgroundColor, Colors.orange);
    expect(editableText.style.fontSize, 27.0);
    expect(editableText.style.shadows, kElevationToShadow[2]);
    expect(editableText.style.decoration, TextDecoration.lineThrough);
    expect(editableText.style.wordSpacing, 5.0);

    final TextField textField = tester.widget(find.byType(TextField));
    expect(textField.decoration?.filled, isTrue);
    expect(textField.decoration?.fillColor, Colors.blue);

    await tester.tap(find.widgetWithIcon(IconButton, Icons.arrow_drop_down).first);
    await tester.pump();
    expect(find.byType(MenuAnchor), findsOneWidget);

    final Finder menuMaterial = find.ancestor(
      of: find.widgetWithText(TextButton, 'Item 0'),
      matching: find.byType(Material),
    ).last;
    Material material = tester.widget<Material>(menuMaterial);
    expect(material.color, Colors.yellow);
    expect(material.shadowColor, Colors.green);
    expect(material.surfaceTintColor, Colors.teal);
    expect(material.elevation, 15.0);
    expect(material.shape, const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))));

    final Finder buttonMaterial = find.descendant(
      of: find.widgetWithText(TextButton, 'Item 0'),
      matching: find.byType(Material),
    ).last;

    material = tester.widget<Material>(buttonMaterial);
    expect(material.color, Colors.transparent);
    expect(material.elevation, 0.0);
    expect(material.shape, const RoundedRectangleBorder());
    expect(material.textStyle?.color, theme.colorScheme.onSurface);
  });

285
  testWidgetsWithLeakTracking('Widget parameters overrides DropdownMenuTheme, ThemeData and defaults', (WidgetTester tester) async {
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    final DropdownMenuThemeData global = DropdownMenuThemeData(
      textStyle: TextStyle(
        color: Colors.orange,
        backgroundColor: Colors.indigo,
        fontSize: 30.0,
        shadows: kElevationToShadow[1],
        decoration: TextDecoration.underline,
        wordSpacing: 2.0,
      ),
      menuStyle: const MenuStyle(
        backgroundColor: MaterialStatePropertyAll<Color>(Colors.grey),
        shadowColor: MaterialStatePropertyAll<Color>(Colors.brown),
        surfaceTintColor: MaterialStatePropertyAll<Color>(Colors.amberAccent),
        elevation: MaterialStatePropertyAll<double>(10.0),
        shape: MaterialStatePropertyAll<OutlinedBorder>(
          RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(10.0))),
        ),
      ),
      inputDecorationTheme: const InputDecorationTheme(filled: true, fillColor: Colors.lightGreen),
    );

    final DropdownMenuThemeData dropdownMenuTheme = DropdownMenuThemeData(
      textStyle: TextStyle(
        color: Colors.red,
        backgroundColor: Colors.orange,
        fontSize: 27.0,
        shadows: kElevationToShadow[2],
        decoration: TextDecoration.lineThrough,
        wordSpacing: 5.0,
      ),
      menuStyle: const MenuStyle(
        backgroundColor: MaterialStatePropertyAll<Color>(Colors.yellow),
        shadowColor: MaterialStatePropertyAll<Color>(Colors.green),
        surfaceTintColor: MaterialStatePropertyAll<Color>(Colors.teal),
        elevation: MaterialStatePropertyAll<double>(15.0),
        shape: MaterialStatePropertyAll<OutlinedBorder>(
          RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(8.0))),
        ),
      ),
      inputDecorationTheme: const InputDecorationTheme(filled: true, fillColor: Colors.blue),
    );

    final ThemeData theme = ThemeData(dropdownMenuTheme: global);
    await tester.pumpWidget(
      MaterialApp(
        theme: theme,
        home: DropdownMenuTheme(
          data: dropdownMenuTheme,
          child: Scaffold(
            body: Center(
336
              child: DropdownMenu<int>(
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
                textStyle: TextStyle(
                  color: Colors.pink,
                  backgroundColor: Colors.cyan,
                  fontSize: 32.0,
                  shadows: kElevationToShadow[3],
                  decoration: TextDecoration.overline,
                  wordSpacing: 3.0,
                ),
                menuStyle: const MenuStyle(
                  backgroundColor: MaterialStatePropertyAll<Color>(Colors.limeAccent),
                  shadowColor: MaterialStatePropertyAll<Color>(Colors.deepOrangeAccent),
                  surfaceTintColor: MaterialStatePropertyAll<Color>(Colors.lightBlue),
                  elevation: MaterialStatePropertyAll<double>(21.0),
                  shape: MaterialStatePropertyAll<OutlinedBorder>(
                    RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(15.0))),
                  ),
                ),
                inputDecorationTheme: const InputDecorationTheme(filled: true, fillColor: Colors.deepPurple),
355 356 357 358
                dropdownMenuEntries: const <DropdownMenuEntry<int>>[
                  DropdownMenuEntry<int>(value: 0, label: 'Item 0'),
                  DropdownMenuEntry<int>(value: 1, label: 'Item 1'),
                  DropdownMenuEntry<int>(value: 2, label: 'Item 2'),
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
                ],
              ),
            ),
          ),
        )
      )
    );

    final EditableText editableText = tester.widget(find.byType(EditableText));
    expect(editableText.style.color, Colors.pink);
    expect(editableText.style.backgroundColor, Colors.cyan);
    expect(editableText.style.fontSize, 32.0);
    expect(editableText.style.shadows, kElevationToShadow[3]);
    expect(editableText.style.decoration, TextDecoration.overline);
    expect(editableText.style.wordSpacing, 3.0);

    final TextField textField = tester.widget(find.byType(TextField));
    expect(textField.decoration?.filled, isTrue);
    expect(textField.decoration?.fillColor, Colors.deepPurple);

    await tester.tap(find.widgetWithIcon(IconButton, Icons.arrow_drop_down).first);
    await tester.pump();
    expect(find.byType(MenuAnchor), findsOneWidget);

    final Finder menuMaterial = find.ancestor(
      of: find.widgetWithText(TextButton, 'Item 0'),
      matching: find.byType(Material),
    ).last;
    Material material = tester.widget<Material>(menuMaterial);
    expect(material.color, Colors.limeAccent);
    expect(material.shadowColor, Colors.deepOrangeAccent);
    expect(material.surfaceTintColor, Colors.lightBlue);
    expect(material.elevation, 21.0);
    expect(material.shape, const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(15.0))));

    final Finder buttonMaterial = find.descendant(
      of: find.widgetWithText(TextButton, 'Item 0'),
      matching: find.byType(Material),
    ).last;

    material = tester.widget<Material>(buttonMaterial);
    expect(material.color, Colors.transparent);
    expect(material.elevation, 0.0);
    expect(material.shape, const RoundedRectangleBorder());
    expect(material.textStyle?.color, theme.colorScheme.onSurface);
  });
}