radio_test.dart 7.25 KB
Newer Older
1 2 3 4
// 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.

5 6 7
import 'dart:ui';

import 'package:flutter/rendering.dart';
8
import 'package:flutter/services.dart';
9 10 11
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';

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

14 15
void main() {
  testWidgets('Radio control test', (WidgetTester tester) async {
16
    final Key key = UniqueKey();
17
    final List<int> log = <int>[];
18

19 20 21
    await tester.pumpWidget(Material(
      child: Center(
        child: Radio<int>(
22 23 24
          key: key,
          value: 1,
          groupValue: 2,
25
          onChanged: log.add,
26 27 28 29 30 31 32 33 34
        ),
      ),
    ));

    await tester.tap(find.byKey(key));

    expect(log, equals(<int>[1]));
    log.clear();

35 36 37
    await tester.pumpWidget(Material(
      child: Center(
        child: Radio<int>(
38 39 40
          key: key,
          value: 1,
          groupValue: 1,
41
          onChanged: log.add,
42 43 44 45 46 47 48 49 50
          activeColor: Colors.green[500],
        ),
      ),
    ));

    await tester.tap(find.byKey(key));

    expect(log, isEmpty);

51 52 53
    await tester.pumpWidget(Material(
      child: Center(
        child: Radio<int>(
54 55 56 57 58 59 60 61 62 63 64 65
          key: key,
          value: 1,
          groupValue: 2,
          onChanged: null,
        ),
      ),
    ));

    await tester.tap(find.byKey(key));

    expect(log, isEmpty);
  });
66

67
  testWidgets('Radio size is configurable by ThemeData.materialTapTargetSize', (WidgetTester tester) async {
68
    final Key key1 = UniqueKey();
69
    await tester.pumpWidget(
70 71 72
      Theme(
        data: ThemeData(materialTapTargetSize: MaterialTapTargetSize.padded),
        child: Directionality(
73
          textDirection: TextDirection.ltr,
74 75 76
          child: Material(
            child: Center(
              child: Radio<bool>(
77 78 79
                key: key1,
                groupValue: true,
                value: true,
80
                onChanged: (bool newValue) { },
81 82 83 84 85 86 87 88
              ),
            ),
          ),
        ),
      ),
    );

    expect(tester.getSize(find.byKey(key1)), const Size(48.0, 48.0));
89

90
    final Key key2 = UniqueKey();
91
    await tester.pumpWidget(
92 93 94
      Theme(
        data: ThemeData(materialTapTargetSize: MaterialTapTargetSize.shrinkWrap),
        child: Directionality(
95
          textDirection: TextDirection.ltr,
96 97 98
          child: Material(
            child: Center(
              child: Radio<bool>(
99 100 101
                key: key2,
                groupValue: true,
                value: true,
102
                onChanged: (bool newValue) { },
103 104
              ),
            ),
105 106 107 108 109
          ),
        ),
      ),
    );

110
    expect(tester.getSize(find.byKey(key2)), const Size(40.0, 40.0));
111 112 113
  });


114
  testWidgets('Radio semantics', (WidgetTester tester) async {
115
    final SemanticsTester semantics = SemanticsTester(tester);
116

117 118
    await tester.pumpWidget(Material(
      child: Radio<int>(
119 120 121 122 123 124
        value: 1,
        groupValue: 2,
        onChanged: (int i) { },
      ),
    ));

125
    expect(semantics, hasSemantics(TestSemantics.root(
126
      children: <TestSemantics>[
127
        TestSemantics.rootChild(
128 129 130 131 132 133 134 135 136 137 138 139 140 141
          id: 1,
          flags: <SemanticsFlag>[
            SemanticsFlag.isInMutuallyExclusiveGroup,
            SemanticsFlag.hasCheckedState,
            SemanticsFlag.hasEnabledState,
            SemanticsFlag.isEnabled,
          ],
          actions: <SemanticsAction>[
            SemanticsAction.tap,
          ],
        ),
      ],
    ), ignoreRect: true, ignoreTransform: true));

142 143
    await tester.pumpWidget(Material(
      child: Radio<int>(
144 145 146 147 148 149
        value: 2,
        groupValue: 2,
        onChanged: (int i) { },
      ),
    ));

150
    expect(semantics, hasSemantics(TestSemantics.root(
151
      children: <TestSemantics>[
152
        TestSemantics.rootChild(
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
          id: 1,
          flags: <SemanticsFlag>[
            SemanticsFlag.isInMutuallyExclusiveGroup,
            SemanticsFlag.hasCheckedState,
            SemanticsFlag.isChecked,
            SemanticsFlag.hasEnabledState,
            SemanticsFlag.isEnabled,
          ],
          actions: <SemanticsAction>[
            SemanticsAction.tap,
          ],
        ),
      ],
    ), ignoreRect: true, ignoreTransform: true));

    await tester.pumpWidget(const Material(
169
      child: Radio<int>(
170 171 172 173 174 175
        value: 1,
        groupValue: 2,
        onChanged: null,
      ),
    ));

176
    expect(semantics, hasSemantics(TestSemantics.root(
177
      children: <TestSemantics>[
178
        TestSemantics.rootChild(
179 180 181 182 183 184 185 186 187 188 189
          id: 1,
          flags: <SemanticsFlag>[
            SemanticsFlag.isInMutuallyExclusiveGroup,
            SemanticsFlag.hasCheckedState,
            SemanticsFlag.hasEnabledState,
          ],
        ),
      ],
    ), ignoreRect: true, ignoreTransform: true));

    await tester.pumpWidget(const Material(
190
      child: Radio<int>(
191 192 193 194 195 196
        value: 2,
        groupValue: 2,
        onChanged: null,
      ),
    ));

197
    expect(semantics, hasSemantics(TestSemantics.root(
198
      children: <TestSemantics>[
199
        TestSemantics.rootChild(
200 201 202 203 204 205 206 207 208 209 210 211 212
          id: 1,
          flags: <SemanticsFlag>[
            SemanticsFlag.isInMutuallyExclusiveGroup,
            SemanticsFlag.hasCheckedState,
            SemanticsFlag.isChecked,
            SemanticsFlag.hasEnabledState,
          ],
        ),
      ],
    ), ignoreRect: true, ignoreTransform: true));

    semantics.dispose();
  });
213 214

  testWidgets('has semantic events', (WidgetTester tester) async {
215 216
    final SemanticsTester semantics = SemanticsTester(tester);
    final Key key = UniqueKey();
217 218
    dynamic semanticEvent;
    int radioValue = 2;
219
    SystemChannels.accessibility.setMockMessageHandler((dynamic message) async {
220 221 222
      semanticEvent = message;
    });

223 224
    await tester.pumpWidget(Material(
      child: Radio<int>(
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
        key: key,
        value: 1,
        groupValue: radioValue,
        onChanged: (int i) {
          radioValue = i;
        },
      ),
    ));

    await tester.tap(find.byKey(key));
    final RenderObject object = tester.firstRenderObject(find.byKey(key));

    expect(radioValue, 1);
    expect(semanticEvent, <String, dynamic>{
      'type': 'tap',
      'nodeId': object.debugSemantics.id,
      'data': <String, dynamic>{},
    });
    expect(object.debugSemantics.getSemanticsData().hasAction(SemanticsAction.tap), true);

    semantics.dispose();
    SystemChannels.accessibility.setMockMessageHandler(null);
  });
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

  testWidgets('Radio ink ripple is displayed correctly', (WidgetTester tester) async {
    final Key painterKey = UniqueKey();
    const Key radioKey = Key('radio');

    await tester.pumpWidget(MaterialApp(
      theme: ThemeData(),
      home: Scaffold(
        body: RepaintBoundary(
          key: painterKey,
          child: Center(
            child: Container(
              width: 100,
              height: 100,
              color: Colors.white,
              child: Radio<int>(
                key: radioKey,
                value: 1,
                groupValue: 1,
267
                onChanged: (int value) { },
268
              ),
269
            ),
270 271 272 273 274 275 276 277 278
          ),
        ),
      ),
    ));

    await tester.press(find.byKey(radioKey));
    await tester.pumpAndSettle();
    await expectLater(
      find.byKey(painterKey),
279 280 281 282
      matchesGoldenFile(
        'radio.ink_ripple.png',
        version: null,
      ),
283
    );
284
  }, skip: isBrowser);
285
}
286