form_row_test.dart 6.16 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/cupertino.dart';
6 7
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
8 9 10
import 'package:flutter_test/flutter_test.dart';

void main() {
11
  testWidgets('Shows prefix', (WidgetTester tester) async {
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
    const Widget prefix = Text('Enter Value');

    await tester.pumpWidget(
      const CupertinoApp(
        home: Center(
          child: CupertinoFormRow(
            prefix: prefix,
            child: CupertinoTextField(),
          ),
        ),
      ),
    );

    expect(prefix, tester.widget(find.byType(Text)));
  });

28
  testWidgets('Shows child', (WidgetTester tester) async {
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    const Widget child = CupertinoTextField();

    await tester.pumpWidget(
      const CupertinoApp(
        home: Center(
          child: CupertinoFormRow(
            child: child,
          ),
        ),
      ),
    );

    expect(child, tester.widget(find.byType(CupertinoTextField)));
  });

44
  testWidgets('RTL puts prefix after child', (WidgetTester tester) async {
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    const Widget prefix = Text('Enter Value');
    const Widget child = CupertinoTextField();

    await tester.pumpWidget(
      const CupertinoApp(
        home: Center(
          child: Directionality(
            textDirection: TextDirection.rtl,
            child: CupertinoFormRow(
              prefix: prefix,
              child: child,
            ),
          ),
        ),
      ),
    );

62
    expect(tester.getTopLeft(find.byType(Text)).dx > tester.getTopLeft(find.byType(CupertinoTextField)).dx, true);
63 64
  });

65
  testWidgets('LTR puts child after prefix', (WidgetTester tester) async {
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    const Widget prefix = Text('Enter Value');
    const Widget child = CupertinoTextField();

    await tester.pumpWidget(
      const CupertinoApp(
        home: Center(
          child: Directionality(
            textDirection: TextDirection.ltr,
            child: CupertinoFormRow(
              prefix: prefix,
              child: child,
            ),
          ),
        ),
      ),
    );

83
    expect(tester.getTopLeft(find.byType(Text)).dx > tester.getTopLeft(find.byType(CupertinoTextField)).dx, false);
84 85
  });

86
  testWidgets('Shows error widget', (WidgetTester tester) async {
87 88 89 90 91 92 93
    const Widget error = Text('Error');

    await tester.pumpWidget(
      const CupertinoApp(
        home: Center(
          child: CupertinoFormRow(
            error: error,
94
            child: CupertinoTextField(),
95 96 97 98 99 100 101 102
          ),
        ),
      ),
    );

    expect(error, tester.widget(find.byType(Text)));
  });

103
  testWidgets('Shows helper widget', (WidgetTester tester) async {
104 105 106 107 108 109 110
    const Widget helper = Text('Helper');

    await tester.pumpWidget(
      const CupertinoApp(
        home: Center(
          child: CupertinoFormRow(
            helper: helper,
111
            child: CupertinoTextField(),
112 113 114 115 116 117 118 119
          ),
        ),
      ),
    );

    expect(helper, tester.widget(find.byType(Text)));
  });

120
  testWidgets('Shows helper text above error text', (WidgetTester tester) async {
121 122 123 124 125 126 127 128 129
    const Widget helper = Text('Helper');
    const Widget error = CupertinoActivityIndicator();

    await tester.pumpWidget(
      const CupertinoApp(
        home: Center(
          child: CupertinoFormRow(
            helper: helper,
            error: error,
130
            child: CupertinoTextField(),
131 132 133 134 135 136
          ),
        ),
      ),
    );

    expect(
137 138 139
      tester.getTopLeft(find.byType(CupertinoActivityIndicator)).dy > tester.getTopLeft(find.byType(Text)).dy,
      true,
    );
140 141
  });

142
  testWidgets('Shows helper in label color and error text in red color', (WidgetTester tester) async {
143 144 145 146 147 148 149 150 151
    const Widget helper = Text('Helper');
    const Widget error = Text('Error');

    await tester.pumpWidget(
      const CupertinoApp(
        home: Center(
          child: CupertinoFormRow(
            helper: helper,
            error: error,
152
            child: CupertinoTextField(),
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
          ),
        ),
      ),
    );

    final DefaultTextStyle helperTextStyle =
        tester.widget(find.byType(DefaultTextStyle).first);

    expect(helperTextStyle.style.color, CupertinoColors.label);

    final DefaultTextStyle errorTextStyle =
        tester.widget(find.byType(DefaultTextStyle).last);

    expect(errorTextStyle.style.color, CupertinoColors.destructiveRed);
  });
168

169
  testWidgets('CupertinoFormRow adapts to MaterialApp dark mode', (WidgetTester tester) async {
170 171 172
    const Widget prefix = Text('Prefix');
    const Widget helper = Text('Helper');

173
    Widget buildFormRow(Brightness brightness) {
174 175 176 177 178 179 180 181 182 183 184 185 186
      return MaterialApp(
        theme: ThemeData(brightness: brightness),
        home: const Center(
          child: CupertinoFormRow(
            prefix: prefix,
            helper: helper,
            child: CupertinoTextField(),
          ),
        ),
      );
    }

    // CupertinoFormRow with light theme.
187
    await tester.pumpWidget(buildFormRow(Brightness.light));
188 189 190 191 192 193 194 195 196 197
    RenderParagraph helperParagraph = tester.renderObject(find.text('Helper'));
    expect(helperParagraph.text.style!.color, CupertinoColors.label);
    // Text style should not return unresolved color.
    expect(helperParagraph.text.style!.color.toString().contains('UNRESOLVED'), isFalse);
    RenderParagraph prefixParagraph = tester.renderObject(find.text('Prefix'));
    expect(prefixParagraph.text.style!.color, CupertinoColors.label);
    // Text style should not return unresolved color.
    expect(prefixParagraph.text.style!.color.toString().contains('UNRESOLVED'), isFalse);

    // CupertinoFormRow with light theme.
198
    await tester.pumpWidget(buildFormRow(Brightness.dark));
199 200 201 202 203 204 205 206 207
    helperParagraph = tester.renderObject(find.text('Helper'));
    expect(helperParagraph.text.style!.color, CupertinoColors.label);
    // Text style should not return unresolved color.
    expect(helperParagraph.text.style!.color.toString().contains('UNRESOLVED'), isFalse);
    prefixParagraph = tester.renderObject(find.text('Prefix'));
    expect(prefixParagraph.text.style!.color, CupertinoColors.label);
    // Text style should not return unresolved color.
    expect(prefixParagraph.text.style!.color.toString().contains('UNRESOLVED'), isFalse);
  });
208
}