navigation_drawer_theme_test.dart 12.4 KB
Newer Older
1 2 3 4 5
// 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/material.dart';
6
import 'package:flutter/rendering.dart';
7 8 9 10 11 12 13 14 15 16 17 18 19
import 'package:flutter_test/flutter_test.dart';

void main() {
  test('NavigationDrawerThemeData copyWith, ==, hashCode, basics', () {
    expect(const NavigationDrawerThemeData(), const NavigationDrawerThemeData().copyWith());
    expect(const NavigationDrawerThemeData().hashCode, const NavigationDrawerThemeData().copyWith().hashCode);
  });

  test('NavigationDrawerThemeData lerp special cases', () {
    expect(NavigationDrawerThemeData.lerp(null, null, 0), null);
    const NavigationDrawerThemeData data = NavigationDrawerThemeData();
    expect(identical(NavigationDrawerThemeData.lerp(data, data, 0.5), data), true);
  });
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 103 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 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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 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 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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309

  testWidgets('Default debugFillProperties', (WidgetTester tester) async {
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
    const NavigationDrawerThemeData().debugFillProperties(builder);

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

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

  testWidgets('NavigationDrawerThemeData implements debugFillProperties', (WidgetTester tester) async {
    final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder();
    const NavigationDrawerThemeData(
      tileHeight: 50,
      backgroundColor: Color(0x00000099),
      elevation: 5.0,
      shadowColor: Color(0x00000098),
      surfaceTintColor: Color(0x00000097),
      indicatorColor: Color(0x00000096),
      indicatorShape: RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(2.0))),
      indicatorSize: Size(10, 10),
      labelTextStyle: MaterialStatePropertyAll<TextStyle>(TextStyle(fontSize: 7.0)),
      iconTheme: MaterialStatePropertyAll<IconThemeData>(IconThemeData(color: Color(0x00000095))),
    ).debugFillProperties(builder);

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

    expect(description, equalsIgnoringHashCodes(
      <String>[
        'tileHeight: 50.0',
        'backgroundColor: Color(0x00000099)',
        'elevation: 5.0',
        'shadowColor: Color(0x00000098)',
        'surfaceTintColor: Color(0x00000097)',
        'indicatorColor: Color(0x00000096)',
        'indicatorShape: RoundedRectangleBorder(BorderSide(width: 0.0, style: none), BorderRadius.circular(2.0))',
        'indicatorSize: Size(10.0, 10.0)',
        'labelTextStyle: MaterialStatePropertyAll(TextStyle(inherit: true, size: 7.0))',
        'iconTheme: MaterialStatePropertyAll(IconThemeData#00000(color: Color(0x00000095)))'
      ],
    ));
  });

  testWidgets(
    'NavigationDrawerThemeData values are used when no NavigationDrawer properties are specified',
    (WidgetTester tester) async {
      final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
      const NavigationDrawerThemeData navigationDrawerTheme = NavigationDrawerThemeData(
        backgroundColor: Color(0x00000001),
        elevation: 7.0,
        shadowColor: Color(0x00000002),
        surfaceTintColor: Color(0x00000003),
        indicatorColor: Color(0x00000004),
        indicatorShape: RoundedRectangleBorder(borderRadius: BorderRadius.only(topRight: Radius.circular(16.0))),
        labelTextStyle:MaterialStatePropertyAll<TextStyle>(TextStyle(fontSize: 7.0)),
        iconTheme: MaterialStatePropertyAll<IconThemeData>(IconThemeData(color: Color(0x00000005))),
      );

      await tester.pumpWidget(
        _buildWidget(
          scaffoldKey,
          NavigationDrawer(
            children: const <Widget>[
              Text('Headline'),
              NavigationDrawerDestination(
                icon: Icon(Icons.ac_unit),
                label: Text('AC'),
              ),
              NavigationDrawerDestination(
                icon: Icon(Icons.access_alarm),
                label: Text('Alarm'),
              ),
            ],
            onDestinationSelected: (int i) {},
          ),
          theme: ThemeData(
            navigationDrawerTheme: navigationDrawerTheme,
          ),
        ),
      );
      scaffoldKey.currentState!.openDrawer();
      await tester.pump(const Duration(seconds: 1));

      // Test drawer Material.
      expect(_getMaterial(tester).color, navigationDrawerTheme.backgroundColor);
      expect(_getMaterial(tester).surfaceTintColor, navigationDrawerTheme.surfaceTintColor);
      expect(_getMaterial(tester).shadowColor, navigationDrawerTheme.shadowColor);
      expect(_getMaterial(tester).elevation, 7);
      // Test indicator decoration.
      expect(_getIndicatorDecoration(tester)?.color, navigationDrawerTheme.indicatorColor);
      expect(_getIndicatorDecoration(tester)?.shape, navigationDrawerTheme.indicatorShape);
      // Test icon.
      expect(
        _iconStyle(tester, Icons.ac_unit)?.color,
        navigationDrawerTheme.iconTheme?.resolve(<MaterialState>{})?.color,
      );
      expect(
        _iconStyle(tester, Icons.access_alarm)?.color,
        navigationDrawerTheme.iconTheme?.resolve(<MaterialState>{})?.color,
      );
      // Test label.
      expect(
        _labelStyle(tester, 'AC'),
        navigationDrawerTheme.labelTextStyle?.resolve(<MaterialState>{})
      );
      expect(
        _labelStyle(tester, 'Alarm'),
        navigationDrawerTheme.labelTextStyle?.resolve(<MaterialState>{})
      );
  });

  testWidgets(
    'NavigationDrawer values take priority over NavigationDrawerThemeData values when both properties are specified',
    (WidgetTester tester) async {
      final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
      const NavigationDrawerThemeData navigationDrawerTheme = NavigationDrawerThemeData(
        backgroundColor: Color(0x00000001),
        elevation: 7.0,
        shadowColor: Color(0x00000002),
        surfaceTintColor: Color(0x00000003),
        indicatorColor: Color(0x00000004),
        indicatorShape: RoundedRectangleBorder(borderRadius: BorderRadius.only(topRight: Radius.circular(16.0))),
        labelTextStyle:MaterialStatePropertyAll<TextStyle>(TextStyle(fontSize: 7.0)),
        iconTheme: MaterialStatePropertyAll<IconThemeData>(IconThemeData(color: Color(0x00000005))),
      );
      const Color backgroundColor = Color(0x00000009);
      const double elevation = 14.0;
      const Color shadowColor = Color(0x00000008);
      const Color surfaceTintColor = Color(0x00000007);
      const RoundedRectangleBorder indicatorShape = RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(32.0)));
      const Color indicatorColor = Color(0x00000006);

      await tester.pumpWidget(
        _buildWidget(
          scaffoldKey,
          NavigationDrawer(
            backgroundColor: backgroundColor,
            elevation: elevation,
            shadowColor: shadowColor,
            surfaceTintColor: surfaceTintColor,
            indicatorShape: indicatorShape,
            indicatorColor: indicatorColor,
            children: const <Widget>[
              Text('Headline'),
              NavigationDrawerDestination(
                icon: Icon(Icons.ac_unit),
                label: Text('AC'),
              ),
              NavigationDrawerDestination(
                icon: Icon(Icons.access_alarm),
                label: Text('Alarm'),
              ),
            ],
            onDestinationSelected: (int i) {},
          ),
          theme: ThemeData(
            navigationDrawerTheme: navigationDrawerTheme,
          ),
        ),
      );
      scaffoldKey.currentState!.openDrawer();
      await tester.pump(const Duration(seconds: 1));

      // Test drawer Material.
      expect(_getMaterial(tester).color, backgroundColor);
      expect(_getMaterial(tester).surfaceTintColor, surfaceTintColor);
      expect(_getMaterial(tester).shadowColor, shadowColor);
      expect(_getMaterial(tester).elevation, elevation);
      // Test indicator decoration.
      expect(_getIndicatorDecoration(tester)?.color, indicatorColor);
      expect(_getIndicatorDecoration(tester)?.shape, indicatorShape);
  });

  testWidgets('Local NavigationDrawerTheme takes priority over ThemeData.navigationDrawerTheme', (WidgetTester tester) async {
    final GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
    const Color backgroundColor = Color(0x00000009);
    const double elevation = 7.0;
    const Color shadowColor = Color(0x00000008);
    const Color surfaceTintColor = Color(0x00000007);
    const Color iconColor = Color(0x00000006);
    const TextStyle labelStyle = TextStyle(fontSize: 7.0);
    const ShapeBorder indicatorShape = CircleBorder();
    const Color indicatorColor = Color(0x00000005);

    await tester.pumpWidget(
      _buildWidget(
        scaffoldKey,
        NavigationDrawerTheme(
          data: const NavigationDrawerThemeData(
            backgroundColor: backgroundColor,
            elevation: elevation,
            shadowColor: shadowColor,
            surfaceTintColor: surfaceTintColor,
            indicatorShape: indicatorShape,
            indicatorColor: indicatorColor,
            labelTextStyle:MaterialStatePropertyAll<TextStyle>(TextStyle(fontSize: 7.0)),
            iconTheme: MaterialStatePropertyAll<IconThemeData>(IconThemeData(color: iconColor)),
          ),
          child: NavigationDrawer(
            children: const <Widget>[
              Text('Headline'),
              NavigationDrawerDestination(
                icon: Icon(Icons.ac_unit),
                label: Text('AC'),
              ),
              NavigationDrawerDestination(
                icon: Icon(Icons.access_alarm),
                label: Text('Alarm'),
              ),
            ],
            onDestinationSelected: (int i) {},
          ),
        ),
        theme: ThemeData(
          navigationDrawerTheme: const NavigationDrawerThemeData(
            backgroundColor: Color(0x00000001),
            elevation: 7.0,
            shadowColor: Color(0x00000002),
            surfaceTintColor: Color(0x00000003),
            indicatorColor: Color(0x00000004),
            indicatorShape: RoundedRectangleBorder(borderRadius: BorderRadius.only(topRight: Radius.circular(16.0))),
            labelTextStyle:MaterialStatePropertyAll<TextStyle>(TextStyle(fontSize: 7.0)),
            iconTheme: MaterialStatePropertyAll<IconThemeData>(IconThemeData(color: Color(0x00000005))),
          ),
        ),
      ),
    );
    scaffoldKey.currentState!.openDrawer();
    await tester.pump(const Duration(seconds: 1));

    // Test drawer Material.
    expect(_getMaterial(tester).color, backgroundColor);
    expect(_getMaterial(tester).surfaceTintColor, surfaceTintColor);
    expect(_getMaterial(tester).shadowColor, shadowColor);
    expect(_getMaterial(tester).elevation, elevation);
    // Test indicator decoration.
    expect(_getIndicatorDecoration(tester)?.color, indicatorColor);
    expect(_getIndicatorDecoration(tester)?.shape, indicatorShape);
    // Test icon.
    expect(_iconStyle(tester, Icons.ac_unit)?.color, iconColor);
    expect(_iconStyle(tester, Icons.access_alarm)?.color, iconColor);
    // Test label.
    expect(_labelStyle(tester, 'AC'), labelStyle);
    expect(_labelStyle(tester, 'Alarm'), labelStyle);
  });
}

Widget _buildWidget(GlobalKey<ScaffoldState> scaffoldKey, Widget child, { ThemeData? theme }) {
  return MaterialApp(
    theme: theme,
    home: Scaffold(
      key: scaffoldKey,
      drawer: child,
      body: Container(),
    ),
  );
}

Material _getMaterial(WidgetTester tester) {
  return tester.firstWidget<Material>(find.descendant(
    of: find.byType(NavigationDrawer),
    matching: find.byType(Material),
  ));
}

ShapeDecoration? _getIndicatorDecoration(WidgetTester tester) {
  return tester.firstWidget<Container>(find.descendant(
    of: find.byType(FadeTransition),
    matching: find.byType(Container),
  )).decoration as ShapeDecoration?;
}

TextStyle? _iconStyle(WidgetTester tester, IconData icon) {
  return tester.widget<RichText>(
    find.descendant(of: find.byIcon(icon),
    matching: find.byType(RichText)),
  ).text.style;
}

TextStyle? _labelStyle(WidgetTester tester, String label) {
  return tester.widget<RichText>(find.descendant(
    of: find.text(label),
    matching: find.byType(RichText),
  )).text.style;
310
}