picker_test.dart 13.5 KB
Newer Older
1 2 3 4 5
// Copyright 2017 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/cupertino.dart';
import 'package:flutter/foundation.dart';
6
import 'package:flutter/rendering.dart';
7
import 'package:flutter/services.dart';
8 9 10
import 'package:flutter_test/flutter_test.dart';

void main() {
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
  testWidgets('Picker respects theme styling', (WidgetTester tester) async {
    await tester.pumpWidget(
      CupertinoApp(
        home: Align(
          alignment: Alignment.topLeft,
          child: SizedBox(
            height: 300.0,
            width: 300.0,
            child: CupertinoPicker(
              itemExtent: 50.0,
              onSelectedItemChanged: (_) { },
              children: List<Widget>.generate(3, (int index) {
                return Container(
                  height: 50.0,
                  width: 300.0,
                  child: Text(index.toString()),
                );
              }),
            ),
          ),
        ),
      ),
    );

    final RenderParagraph paragraph = tester.renderObject(find.text('1'));

    expect(paragraph.text.style, const TextStyle(
      inherit: false,
      fontFamily: '.SF Pro Display',
      fontSize: 25.0,
      fontWeight: FontWeight.w400,
      letterSpacing: -0.41,
      color: CupertinoColors.black,
    ));
  });

47 48 49
  group('layout', () {
    testWidgets('selected item is in the middle', (WidgetTester tester) async {
      final FixedExtentScrollController controller =
50
          FixedExtentScrollController(initialItem: 1);
51 52

      await tester.pumpWidget(
53
        Directionality(
54
          textDirection: TextDirection.ltr,
55
          child: Align(
56
            alignment: Alignment.topLeft,
57
            child: SizedBox(
58 59
              height: 300.0,
              width: 300.0,
60
              child: CupertinoPicker(
61 62
                scrollController: controller,
                itemExtent: 50.0,
63
                onSelectedItemChanged: (_) { },
64 65
                children: List<Widget>.generate(3, (int index) {
                  return Container(
66 67
                    height: 50.0,
                    width: 300.0,
68
                    child: Text(index.toString()),
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
                  );
                }),
              ),
            ),
          ),
        ),
      );

      expect(
        tester.getTopLeft(find.widgetWithText(Container, '1')),
        const Offset(0.0, 125.0),
      );

      controller.jumpToItem(0);
      await tester.pump();

      expect(
        tester.getTopLeft(find.widgetWithText(Container, '1')),
        const Offset(0.0, 175.0),
      );
      expect(
        tester.getTopLeft(find.widgetWithText(Container, '0')),
        const Offset(0.0, 125.0),
      );
    });
  });

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  group('gradient', () {
    testWidgets('gradient displays correctly with background color', (WidgetTester tester) async {
      const Color backgroundColor = Color.fromRGBO(255, 0, 0, 1.0);
      await tester.pumpWidget(
        Directionality(
          textDirection: TextDirection.ltr,
          child: Align(
            alignment: Alignment.topLeft,
            child: SizedBox(
              height: 300.0,
              width: 300.0,
              child: CupertinoPicker(
                backgroundColor: backgroundColor,
                itemExtent: 15.0,
                children: const <Widget>[
                  Text('1'),
                  Text('1'),
                  Text('1'),
                  Text('1'),
                  Text('1'),
                  Text('1'),
                  Text('1'),
                ],
                onSelectedItemChanged: (int i) { },
120 121 122 123
              ),
            ),
          ),
        ),
124 125 126 127 128 129 130 131 132 133 134 135 136 137
      );
      final Container container = tester.firstWidget(find.byType(Container));
      final BoxDecoration boxDecoration = container.decoration;
      expect(boxDecoration.gradient.colors, <Color>[
        backgroundColor,
        backgroundColor.withAlpha(0xF2),
        backgroundColor.withAlpha(0xDD),
        backgroundColor.withAlpha(0x00),
        backgroundColor.withAlpha(0x00),
        backgroundColor.withAlpha(0xDD),
        backgroundColor.withAlpha(0xF2),
        backgroundColor,
      ]);
    });
138

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    testWidgets('No gradient displays with transparent background color', (WidgetTester tester) async {
      const Color backgroundColor = Color.fromRGBO(255, 0, 0, 0.5);
      await tester.pumpWidget(
        Directionality(
          textDirection: TextDirection.ltr,
          child: Align(
            alignment: Alignment.topLeft,
            child: SizedBox(
              height: 300.0,
              width: 300.0,
              child: CupertinoPicker(
                backgroundColor: backgroundColor,
                itemExtent: 15.0,
                children: const <Widget>[
                  Text('1'),
                  Text('1'),
                  Text('1'),
                  Text('1'),
                  Text('1'),
                  Text('1'),
                  Text('1'),
                ],
                onSelectedItemChanged: (int i) { },
162 163 164 165
              ),
            ),
          ),
        ),
166 167 168 169 170 171
      );
      final DecoratedBox decoratedBox = tester.firstWidget(find.byType(DecoratedBox));
      final BoxDecoration boxDecoration = decoratedBox.decoration;
      expect(boxDecoration.gradient, isNull);
      expect(boxDecoration.color, isNotNull);
    });
172

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    testWidgets('gradient displays correctly with null background color', (WidgetTester tester) async {
      await tester.pumpWidget(
        Directionality(
          textDirection: TextDirection.ltr,
          child: Align(
            alignment: Alignment.topLeft,
            child: SizedBox(
              height: 300.0,
              width: 300.0,
              child: CupertinoPicker(
                backgroundColor: null,
                itemExtent: 15.0,
                children: const <Widget>[
                  Text('1'),
                  Text('1'),
                  Text('1'),
                  Text('1'),
                  Text('1'),
                  Text('1'),
                  Text('1'),
                ],
                onSelectedItemChanged: (int i) { },
              ),
            ),
          ),
        ),
      );
      // If the background color is null, the gradient color should be white.
      const Color backgroundColor = Color(0xFFFFFFFF);
      final Container container = tester.firstWidget(find.byType(Container));
      final BoxDecoration boxDecoration = container.decoration;
      expect(boxDecoration.gradient.colors, <Color>[
        backgroundColor,
        backgroundColor.withAlpha(0xF2),
        backgroundColor.withAlpha(0xDD),
        backgroundColor.withAlpha(0x00),
        backgroundColor.withAlpha(0x00),
        backgroundColor.withAlpha(0xDD),
        backgroundColor.withAlpha(0xF2),
        backgroundColor,
      ]);
    });
215 216
  });

217
  group('scroll', () {
218 219 220 221 222 223 224 225 226 227 228 229
    testWidgets(
      'scrolling calls onSelectedItemChanged and triggers haptic feedback',
      (WidgetTester tester) async {
        debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
        final List<int> selectedItems = <int>[];
        final List<MethodCall> systemCalls = <MethodCall>[];

        SystemChannels.platform.setMockMethodCallHandler((MethodCall methodCall) async {
          systemCalls.add(methodCall);
        });

        await tester.pumpWidget(
230
          Directionality(
231
            textDirection: TextDirection.ltr,
232
            child: CupertinoPicker(
233 234
              itemExtent: 100.0,
              onSelectedItemChanged: (int index) { selectedItems.add(index); },
235 236 237
              children: List<Widget>.generate(100, (int index) {
                return Center(
                  child: Container(
238 239
                    width: 400.0,
                    height: 100.0,
240
                    child: Text(index.toString()),
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
                  ),
                );
              }),
            ),
          ),
        );

        await tester.drag(find.text('0'), const Offset(0.0, -100.0));
        expect(selectedItems, <int>[1]);
        expect(
          systemCalls.single,
          isMethodCall(
            'HapticFeedback.vibrate',
            arguments: 'HapticFeedbackType.selectionClick',
          ),
        );

        await tester.drag(find.text('0'), const Offset(0.0, 100.0));
        expect(selectedItems, <int>[1, 0]);
        expect(systemCalls, hasLength(2));
        expect(
          systemCalls.last,
          isMethodCall(
            'HapticFeedback.vibrate',
            arguments: 'HapticFeedbackType.selectionClick',
          ),
        );

        debugDefaultTargetPlatformOverride = null;
      },
    );

    testWidgets(
      'do not trigger haptic effects on non-iOS devices',
      (WidgetTester tester) async {
        debugDefaultTargetPlatformOverride = TargetPlatform.android;
        final List<int> selectedItems = <int>[];
        final List<MethodCall> systemCalls = <MethodCall>[];

        SystemChannels.platform.setMockMethodCallHandler((MethodCall methodCall) async {
          systemCalls.add(methodCall);
        });

        await tester.pumpWidget(
285
          Directionality(
286
            textDirection: TextDirection.ltr,
287
            child: CupertinoPicker(
288 289
              itemExtent: 100.0,
              onSelectedItemChanged: (int index) { selectedItems.add(index); },
290 291 292
              children: List<Widget>.generate(100, (int index) {
                return Center(
                  child: Container(
293 294
                    width: 400.0,
                    height: 100.0,
295
                    child: Text(index.toString()),
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
                  ),
                );
              }),
            ),
          ),
        );

        await tester.drag(find.text('0'), const Offset(0.0, -100.0));
        expect(selectedItems, <int>[1]);
        expect(systemCalls, isEmpty);

        debugDefaultTargetPlatformOverride = null;
      },
    );

311 312 313
    testWidgets('a drag in between items settles back', (WidgetTester tester) async {
      debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
      final FixedExtentScrollController controller =
314
          FixedExtentScrollController(initialItem: 10);
315 316 317
      final List<int> selectedItems = <int>[];

      await tester.pumpWidget(
318
        Directionality(
319
          textDirection: TextDirection.ltr,
320
          child: CupertinoPicker(
321 322 323
            scrollController: controller,
            itemExtent: 100.0,
            onSelectedItemChanged: (int index) { selectedItems.add(index); },
324 325 326
            children: List<Widget>.generate(100, (int index) {
              return Center(
                child: Container(
327 328
                  width: 400.0,
                  height: 100.0,
329
                  child: Text(index.toString()),
330 331 332 333 334 335 336 337
                ),
              );
            }),
          ),
        ),
      );

      // Drag it by a bit but not enough to move to the next item.
338
      await tester.drag(find.text('10'), const Offset(0.0, 30.0), touchSlopY: 0.0);
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354

      // The item that was in the center now moved a bit.
      expect(
        tester.getTopLeft(find.widgetWithText(Container, '10')),
        const Offset(200.0, 280.0),
      );

      await tester.pumpAndSettle();

      expect(
        tester.getTopLeft(find.widgetWithText(Container, '10')).dy,
        moreOrLessEquals(250.0, epsilon: 0.5),
      );
      expect(selectedItems.isEmpty, true);

      // Drag it by enough to move to the next item.
355
      await tester.drag(find.text('10'), const Offset(0.0, 70.0), touchSlopY: 0.0);
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370

      await tester.pumpAndSettle();

      expect(
        tester.getTopLeft(find.widgetWithText(Container, '10')).dy,
        // It's down by 100.0 now.
        moreOrLessEquals(350.0, epsilon: 0.5),
      );
      expect(selectedItems, <int>[9]);
      debugDefaultTargetPlatformOverride = null;
    });

    testWidgets('a big fling that overscrolls springs back', (WidgetTester tester) async {
      debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
      final FixedExtentScrollController controller =
371
          FixedExtentScrollController(initialItem: 10);
372 373 374
      final List<int> selectedItems = <int>[];

      await tester.pumpWidget(
375
        Directionality(
376
          textDirection: TextDirection.ltr,
377
          child: CupertinoPicker(
378 379 380
            scrollController: controller,
            itemExtent: 100.0,
            onSelectedItemChanged: (int index) { selectedItems.add(index); },
381 382 383
            children: List<Widget>.generate(100, (int index) {
              return Center(
                child: Container(
384 385
                  width: 400.0,
                  height: 100.0,
386
                  child: Text(index.toString()),
387 388 389 390 391 392 393 394 395 396 397 398 399 400
                ),
              );
            }),
          ),
        ),
      );

      // A wild throw appears.
      await tester.fling(
        find.text('10'),
        const Offset(0.0, 10000.0),
        1000.0,
      );

401 402 403 404
      // Should have been flung far enough that even the first item goes off
      // screen and gets removed.
      expect(find.widgetWithText(Container, '0').evaluate().isEmpty, true);

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
      expect(
        selectedItems,
        // This specific throw was fast enough that each scroll update landed
        // on every second item.
        <int>[8, 6, 4, 2, 0],
      );

      // Let it spring back.
      await tester.pumpAndSettle();

      expect(
        tester.getTopLeft(find.widgetWithText(Container, '0')).dy,
        // Should have sprung back to the middle now.
        moreOrLessEquals(250.0),
      );
      expect(
        selectedItems,
        // Falling back to 0 shouldn't produce more callbacks.
        <int>[8, 6, 4, 2, 0],
      );

      debugDefaultTargetPlatformOverride = null;
    });
  });
}