control_list_tile_test.dart 4.4 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
  return MediaQuery(
13
    data: const MediaQueryData(),
14
    child: Directionality(
15
      textDirection: TextDirection.ltr,
16
      child: Material(child: child),
17
    ),
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
      child: CheckboxListTile(
26 27 28 29 30 31 32 33 34 35 36 37 38
        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
      child: RadioListTile<bool>(
41 42 43 44 45 46 47 48 49 50 51 52 53 54
        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
      child: SwitchListTile(
57 58 59 60 61 62 63 64 65 66 67 68
        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 {
69
    final SemanticsTester semantics = SemanticsTester(tester);
70
    await tester.pumpWidget(wrap(
71
      child: Column(
72
        children: <Widget>[
73
          SwitchListTile(
74 75 76 77 78
            value: true,
            onChanged: (bool value) { },
            title: const Text('AAA'),
            secondary: const Text('aaa'),
          ),
79
          CheckboxListTile(
80 81 82 83 84
            value: true,
            onChanged: (bool value) { },
            title: const Text('BBB'),
            secondary: const Text('bbb'),
          ),
85
          RadioListTile<bool>(
86 87 88 89 90 91 92 93 94
            value: true,
            groupValue: false,
            onChanged: (bool value) { },
            title: const Text('CCC'),
            secondary: const Text('ccc'),
          ),
        ],
      ),
    ));
95

96
    // This test verifies that the label and the control get merged.
97
    expect(semantics, hasSemantics(TestSemantics.root(
98
      children: <TestSemantics>[
99
        TestSemantics.rootChild(
100
          id: 1,
101
          rect: Rect.fromLTWH(0.0, 0.0, 800.0, 56.0),
102
          transform: null,
103
          flags: <SemanticsFlag>[
104 105
            SemanticsFlag.hasToggledState,
            SemanticsFlag.isToggled,
106 107
            SemanticsFlag.hasEnabledState,
            SemanticsFlag.isEnabled
108
          ],
109 110 111
          actions: SemanticsAction.tap.index,
          label: 'aaa\nAAA',
        ),
112
        TestSemantics.rootChild(
113
          id: 3,
114 115
          rect: Rect.fromLTWH(0.0, 0.0, 800.0, 56.0),
          transform: 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
          actions: SemanticsAction.tap.index,
          label: 'bbb\nBBB',
        ),
125
        TestSemantics.rootChild(
126
          id: 5,
127 128
          rect: Rect.fromLTWH(0.0, 0.0, 800.0, 56.0),
          transform: 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
  });

}