control_list_tile_test.dart 9.03 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

import '../widgets/semantics_tester.dart';

11
Widget wrap({ Widget child }) {
12
  return MediaQuery(
13
    data: const MediaQueryData(),
14
    child: Directionality(
15
      textDirection: TextDirection.ltr,
16
      child: Material(child: child),
17
    ),
18 19 20
  );
}

21
void main() {
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
  testWidgets('RadioListTile should initialize according to groupValue', (WidgetTester tester) async {
    final List<int> values = <int>[0, 1, 2];
    int selectedValue;
    // Constructor parameters are required for [RadioListTile], but they are
    // irrelevant when searching with [find.byType].
    final Type radioListTileType = const RadioListTile<int>(
      value: 0,
      groupValue: 0,
      onChanged: null,
    ).runtimeType;

    List<RadioListTile<int>> generatedRadioListTiles;
    List<RadioListTile<int>> findTiles() => find
      .byType(radioListTileType)
      .evaluate()
37 38
      .map<Widget>((Element element) => element.widget)
      .cast<RadioListTile<int>>()
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
      .toList();

    Widget buildFrame() {
      return wrap(
        child: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
            return Scaffold(
              body: ListView.builder(
                itemCount: values.length,
                itemBuilder: (BuildContext context, int index) => RadioListTile<int>(
                  onChanged: (int value) {
                    setState(() { selectedValue = value; });
                  },
                  value: values[index],
                  groupValue: selectedValue,
                  title: Text(values[index].toString()),
                ),
              ),
            );
          },
        ),
      );
    }

    await tester.pumpWidget(buildFrame());
    generatedRadioListTiles = findTiles();

    expect(generatedRadioListTiles[0].checked, equals(false));
    expect(generatedRadioListTiles[1].checked, equals(false));
    expect(generatedRadioListTiles[2].checked, equals(false));

    selectedValue = 1;

    await tester.pumpWidget(buildFrame());
    generatedRadioListTiles = findTiles();

    expect(generatedRadioListTiles[0].checked, equals(false));
    expect(generatedRadioListTiles[1].checked, equals(true));
    expect(generatedRadioListTiles[2].checked, equals(false));
  });

  testWidgets('RadioListTile control tests', (WidgetTester tester) async {
    final List<int> values = <int>[0, 1, 2];
    int selectedValue;
    // Constructor parameters are required for [Radio], but they are irrelevant
    // when searching with [find.byType].
    final Type radioType = const Radio<int>(
      value: 0,
      groupValue: 0,
      onChanged: null,
    ).runtimeType;
90
    final List<dynamic> log = <dynamic>[];
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

    Widget buildFrame() {
      return wrap(
        child: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
            return Scaffold(
              body: ListView.builder(
                itemCount: values.length,
                itemBuilder: (BuildContext context, int index) => RadioListTile<int>(
                  onChanged: (int value) {
                    log.add(value);
                    setState(() { selectedValue = value; });
                  },
                  value: values[index],
                  groupValue: selectedValue,
                  title: Text(values[index].toString()),
                ),
              ),
            );
          },
        ),
      );
    }

    // Tests for tapping between [Radio] and [ListTile]
    await tester.pumpWidget(buildFrame());
    await tester.tap(find.text('1'));
    log.add('-');
    await tester.tap(find.byType(radioType).at(2));
    expect(log, equals(<dynamic>[1, '-', 2]));
    log.add('-');
    await tester.tap(find.text('1'));

    log.clear();
    selectedValue = null;

    // Tests for tapping across [Radio]s exclusively
    await tester.pumpWidget(buildFrame());
    await tester.tap(find.byType(radioType).at(1));
    log.add('-');
    await tester.tap(find.byType(radioType).at(2));
    expect(log, equals(<dynamic>[1, '-', 2]));

    log.clear();
    selectedValue = null;

    // Tests for tapping across [ListTile]s exclusively
    await tester.pumpWidget(buildFrame());
    await tester.tap(find.text('1'));
140
    log.add('-');
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
    await tester.tap(find.text('2'));
    expect(log, equals(<dynamic>[1, '-', 2]));
  });

  testWidgets('Selected RadioListTile should not trigger onChanged', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/30311
    final List<int> values = <int>[0, 1, 2];
    int selectedValue;
    // Constructor parameters are required for [Radio], but they are irrelevant
    // when searching with [find.byType].
    final Type radioType = const Radio<int>(
      value: 0,
      groupValue: 0,
      onChanged: null,
    ).runtimeType;
    final List<dynamic> log = <dynamic>[];

    Widget buildFrame() {
      return wrap(
        child: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
            return Scaffold(
              body: ListView.builder(
                itemCount: values.length,
                itemBuilder: (BuildContext context, int index) => RadioListTile<int>(
                  onChanged: (int value) {
                    log.add(value);
                    setState(() { selectedValue = value; });
                  },
                  value: values[index],
                  groupValue: selectedValue,
                  title: Text(values[index].toString()),
                ),
              ),
            );
          },
        ),
      );
    }

    await tester.pumpWidget(buildFrame());
    await tester.tap(find.text('0'));
    await tester.pump();
    expect(log, equals(<int>[0]));

    await tester.tap(find.text('0'));
    expect(log, equals(<int>[0]));

    await tester.tap(find.byType(radioType).at(0));
    await tester.pump();
    expect(log, equals(<int>[0]));
192 193 194 195
  });

  testWidgets('SwitchListTile control test', (WidgetTester tester) async {
    final List<dynamic> log = <dynamic>[];
196
    await tester.pumpWidget(wrap(
197
      child: SwitchListTile(
198 199 200 201 202 203 204 205 206 207 208 209
        value: true,
        onChanged: (bool value) { log.add(value); },
        title: const Text('Hello'),
      ),
    ));
    await tester.tap(find.text('Hello'));
    log.add('-');
    await tester.tap(find.byType(Switch));
    expect(log, equals(<dynamic>[false, '-', false]));
  });

  testWidgets('SwitchListTile control test', (WidgetTester tester) async {
210
    final SemanticsTester semantics = SemanticsTester(tester);
211
    await tester.pumpWidget(wrap(
212
      child: Column(
213
        children: <Widget>[
214
          SwitchListTile(
215 216 217 218 219
            value: true,
            onChanged: (bool value) { },
            title: const Text('AAA'),
            secondary: const Text('aaa'),
          ),
220
          CheckboxListTile(
221 222 223 224 225
            value: true,
            onChanged: (bool value) { },
            title: const Text('BBB'),
            secondary: const Text('bbb'),
          ),
226
          RadioListTile<bool>(
227 228 229 230 231 232 233 234 235
            value: true,
            groupValue: false,
            onChanged: (bool value) { },
            title: const Text('CCC'),
            secondary: const Text('ccc'),
          ),
        ],
      ),
    ));
236

237
    // This test verifies that the label and the control get merged.
238
    expect(semantics, hasSemantics(TestSemantics.root(
239
      children: <TestSemantics>[
240
        TestSemantics.rootChild(
241
          id: 1,
Dan Field's avatar
Dan Field committed
242
          rect: const Rect.fromLTWH(0.0, 0.0, 800.0, 56.0),
243
          transform: null,
244 245
          flags: <SemanticsFlag>[
            SemanticsFlag.hasEnabledState,
246
            SemanticsFlag.hasToggledState,
247
            SemanticsFlag.isEnabled,
248 249
            SemanticsFlag.isFocusable,
            SemanticsFlag.isToggled,
250
          ],
251 252 253
          actions: SemanticsAction.tap.index,
          label: 'aaa\nAAA',
        ),
254
        TestSemantics.rootChild(
255
          id: 3,
Dan Field's avatar
Dan Field committed
256
          rect: const Rect.fromLTWH(0.0, 0.0, 800.0, 56.0),
257
          transform: Matrix4.translationValues(0.0, 56.0, 0.0),
258 259 260
          flags: <SemanticsFlag>[
            SemanticsFlag.hasCheckedState,
            SemanticsFlag.hasEnabledState,
261
            SemanticsFlag.isChecked,
262
            SemanticsFlag.isEnabled,
263
            SemanticsFlag.isFocusable,
264
          ],
265 266 267
          actions: SemanticsAction.tap.index,
          label: 'bbb\nBBB',
        ),
268
        TestSemantics.rootChild(
269
          id: 5,
Dan Field's avatar
Dan Field committed
270
          rect: const Rect.fromLTWH(0.0, 0.0, 800.0, 56.0),
271
          transform: Matrix4.translationValues(0.0, 112.0, 0.0),
272 273 274
          flags: <SemanticsFlag>[
            SemanticsFlag.hasCheckedState,
            SemanticsFlag.hasEnabledState,
275
            SemanticsFlag.isEnabled,
276
            SemanticsFlag.isFocusable,
277
            SemanticsFlag.isInMutuallyExclusiveGroup,
278
          ],
279 280 281 282
          actions: SemanticsAction.tap.index,
          label: 'CCC\nccc',
        ),
      ],
283
    )));
284 285

    semantics.dispose();
286 287 288
  });

}