control_list_tile_test.dart 4.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2015 The Chromium 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_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 13 14 15 16 17
  return new MediaQuery(
    data: const MediaQueryData(),
    child: new Directionality(
      textDirection: TextDirection.ltr,
      child: new Material(child: child),
    ),
18 19 20
  );
}

21 22 23
void main() {
  testWidgets('CheckboxListTile control test', (WidgetTester tester) async {
    final List<dynamic> log = <dynamic>[];
24
    await tester.pumpWidget(wrap(
25 26 27 28 29 30 31 32 33 34 35 36 37 38
      child: new CheckboxListTile(
        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(Checkbox));
    expect(log, equals(<dynamic>[false, '-', false]));
  });

  testWidgets('RadioListTile control test', (WidgetTester tester) async {
    final List<dynamic> log = <dynamic>[];
39
    await tester.pumpWidget(wrap(
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
      child: new RadioListTile<bool>(
        value: true,
        groupValue: false,
        onChanged: (bool value) { log.add(value); },
        title: const Text('Hello'),
      ),
    ));
    await tester.tap(find.text('Hello'));
    log.add('-');
    await tester.tap(find.byType(const Radio<bool>(value: false, groupValue: false, onChanged: null).runtimeType));
    expect(log, equals(<dynamic>[true, '-', true]));
  });

  testWidgets('SwitchListTile control test', (WidgetTester tester) async {
    final List<dynamic> log = <dynamic>[];
55
    await tester.pumpWidget(wrap(
56 57 58 59 60 61 62 63 64 65 66 67 68 69
      child: new SwitchListTile(
        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 {
    final SemanticsTester semantics = new SemanticsTester(tester);
70
    await tester.pumpWidget(wrap(
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
      child: new Column(
        children: <Widget>[
          new SwitchListTile(
            value: true,
            onChanged: (bool value) { },
            title: const Text('AAA'),
            secondary: const Text('aaa'),
          ),
          new CheckboxListTile(
            value: true,
            onChanged: (bool value) { },
            title: const Text('BBB'),
            secondary: const Text('bbb'),
          ),
          new RadioListTile<bool>(
            value: true,
            groupValue: false,
            onChanged: (bool value) { },
            title: const Text('CCC'),
            secondary: const Text('ccc'),
          ),
        ],
      ),
    ));
95

96 97 98 99
    // This test verifies that the label and the control get merged.
    expect(semantics, hasSemantics(new TestSemantics.root(
      children: <TestSemantics>[
        new TestSemantics.rootChild(
100
          id: 1,
101 102
          rect: new Rect.fromLTWH(0.0, 0.0, 800.0, 56.0),
          transform: null,
103 104 105 106 107
          flags: <SemanticsFlag>[
            SemanticsFlag.hasCheckedState,
            SemanticsFlag.isChecked,
            SemanticsFlag.hasEnabledState,
            SemanticsFlag.isEnabled
108
          ],
109 110 111 112
          actions: SemanticsAction.tap.index,
          label: 'aaa\nAAA',
        ),
        new TestSemantics.rootChild(
113
          id: 3,
114 115
          rect: new Rect.fromLTWH(0.0, 0.0, 800.0, 56.0),
          transform: new Matrix4.translationValues(0.0, 56.0, 0.0),
116 117 118 119 120
          flags: <SemanticsFlag>[
            SemanticsFlag.hasCheckedState,
            SemanticsFlag.isChecked,
            SemanticsFlag.hasEnabledState,
            SemanticsFlag.isEnabled
121
          ],
122 123 124 125
          actions: SemanticsAction.tap.index,
          label: 'bbb\nBBB',
        ),
        new TestSemantics.rootChild(
126
          id: 5,
127 128
          rect: new Rect.fromLTWH(0.0, 0.0, 800.0, 56.0),
          transform: new Matrix4.translationValues(0.0, 112.0, 0.0),
129 130 131
          flags: <SemanticsFlag>[
            SemanticsFlag.hasCheckedState,
            SemanticsFlag.hasEnabledState,
132 133
            SemanticsFlag.isEnabled,
            SemanticsFlag.isInMutuallyExclusiveGroup,
134
          ],
135 136 137 138
          actions: SemanticsAction.tap.index,
          label: 'CCC\nccc',
        ),
      ],
139
    )));
140 141

    semantics.dispose();
142 143 144
  });

}