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

void main() {
  testWidgets('Shows prefix', (WidgetTester tester) async {
    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)));
  });

  testWidgets('Shows child', (WidgetTester tester) async {
    const Widget child = CupertinoTextField();

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

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

  testWidgets('RTL puts prefix after child', (WidgetTester tester) async {
    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,
            ),
          ),
        ),
      ),
    );

60
    expect(tester.getTopLeft(find.byType(Text)).dx > tester.getTopLeft(find.byType(CupertinoTextField)).dx, true);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  });

  testWidgets('LTR puts child after prefix', (WidgetTester tester) async {
    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,
            ),
          ),
        ),
      ),
    );

81
    expect(tester.getTopLeft(find.byType(Text)).dx > tester.getTopLeft(find.byType(CupertinoTextField)).dx, false);
82 83 84 85 86 87 88 89 90 91
  });

  testWidgets('Shows error widget', (WidgetTester tester) async {
    const Widget error = Text('Error');

    await tester.pumpWidget(
      const CupertinoApp(
        home: Center(
          child: CupertinoFormRow(
            error: error,
92
            child: CupertinoTextField(),
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
          ),
        ),
      ),
    );

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

  testWidgets('Shows helper widget', (WidgetTester tester) async {
    const Widget helper = Text('Helper');

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

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

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

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

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

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

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

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