slider_test.dart 6.38 KB
Newer Older
1 2 3 4 5 6
// Copyright 2016 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/material.dart';
7
import 'package:flutter/rendering.dart';
8 9 10
import 'package:flutter/scheduler.dart';
import 'package:flutter_test/flutter_test.dart';

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

13
void main() {
14
  testWidgets('Slider does not move when tapped (LTR)', (WidgetTester tester) async {
15 16 17
    final Key sliderKey = new UniqueKey();
    double value = 0.0;

18 19 20
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new StatefulBuilder(
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
        builder: (BuildContext context, StateSetter setState) {
          return new Material(
            child: new Center(
              child: new CupertinoSlider(
                key: sliderKey,
                value: value,
                onChanged: (double newValue) {
                  setState(() {
                    value = newValue;
                  });
                },
              ),
            ),
          );
        },
      ),
37 38 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
    ));

    expect(value, equals(0.0));
    await tester.tap(find.byKey(sliderKey));
    expect(value, equals(0.0));
    await tester.pump(); // No animation should start.
    // Check the transientCallbackCount before tearing down the widget to ensure
    // that no animation is running.
    expect(SchedulerBinding.instance.transientCallbackCount, equals(0));
  });

  testWidgets('Slider does not move when tapped (RTL)', (WidgetTester tester) async {
    final Key sliderKey = new UniqueKey();
    double value = 0.0;

    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.rtl,
      child: new StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
          return new Material(
            child: new Center(
              child: new CupertinoSlider(
                key: sliderKey,
                value: value,
                onChanged: (double newValue) {
                  setState(() {
                    value = newValue;
                  });
                },
              ),
            ),
          );
        },
      ),
    ));
72 73 74 75 76 77 78 79 80 81

    expect(value, equals(0.0));
    await tester.tap(find.byKey(sliderKey));
    expect(value, equals(0.0));
    await tester.pump(); // No animation should start.
    // Check the transientCallbackCount before tearing down the widget to ensure
    // that no animation is running.
    expect(SchedulerBinding.instance.transientCallbackCount, equals(0));
  });

82 83

  testWidgets('Slider moves when dragged (LTR)', (WidgetTester tester) async {
84 85 86
    final Key sliderKey = new UniqueKey();
    double value = 0.0;

87 88 89
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new StatefulBuilder(
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        builder: (BuildContext context, StateSetter setState) {
          return new Material(
            child: new Center(
              child: new CupertinoSlider(
                key: sliderKey,
                value: value,
                onChanged: (double newValue) {
                  setState(() {
                    value = newValue;
                  });
                },
              ),
            ),
          );
        },
      ),
106
    ));
107 108

    expect(value, equals(0.0));
109
    final Offset topLeft = tester.getTopLeft(find.byKey(sliderKey));
110 111
    const double unit = CupertinoThumbPainter.radius;
    const double delta = 3.0 * unit;
112
    await tester.dragFrom(topLeft + const Offset(unit, unit), const Offset(delta, 0.0));
113 114 115 116 117 118 119
    final Size size = tester.getSize(find.byKey(sliderKey));
    expect(value, equals(delta / (size.width - 2.0 * (8.0 + CupertinoThumbPainter.radius))));
    await tester.pump(); // No animation should start.
    // Check the transientCallbackCount before tearing down the widget to ensure
    // that no animation is running.
    expect(SchedulerBinding.instance.transientCallbackCount, equals(0));
  });
120

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
  testWidgets('Slider moves when dragged (RTL)', (WidgetTester tester) async {
    final Key sliderKey = new UniqueKey();
    double value = 0.0;

    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.rtl,
      child: new StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
          return new Material(
            child: new Center(
              child: new CupertinoSlider(
                key: sliderKey,
                value: value,
                onChanged: (double newValue) {
                  setState(() {
                    value = newValue;
                  });
                },
              ),
            ),
          );
        },
      ),
    ));

    expect(value, equals(0.0));
    final Offset bottomRight = tester.getBottomRight(find.byKey(sliderKey));
    const double unit = CupertinoThumbPainter.radius;
    const double delta = 3.0 * unit;
    await tester.dragFrom(bottomRight - const Offset(unit, unit), const Offset(-delta, 0.0));
    final Size size = tester.getSize(find.byKey(sliderKey));
    expect(value, equals(delta / (size.width - 2.0 * (8.0 + CupertinoThumbPainter.radius))));
    await tester.pump(); // No animation should start.
    // Check the transientCallbackCount before tearing down the widget to ensure
    // that no animation is running.
    expect(SchedulerBinding.instance.transientCallbackCount, equals(0));
  });

159 160 161
  testWidgets('Slider Semantics', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

162 163 164
    await tester.pumpWidget(new Directionality(
      textDirection: TextDirection.ltr,
      child: new CupertinoSlider(
165 166 167
        value: 0.5,
        onChanged: (double v) {},
      ),
168
    ));
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

    expect(semantics, hasSemantics(
      new TestSemantics.root(
        children: <TestSemantics>[
          new TestSemantics.rootChild(
            id: 1,
            actions: SemanticsAction.decrease.index | SemanticsAction.increase.index,
          ),
        ]
      ),
      ignoreRect: true,
      ignoreTransform: true,
    ));

    // Disable slider
184
    await tester.pumpWidget(const Directionality(
185
      textDirection: TextDirection.ltr,
186
      child: const CupertinoSlider(
187 188 189
        value: 0.5,
        onChanged: null,
      ),
190
    ));
191 192 193 194 195 196 197 198 199

    expect(semantics, hasSemantics(
      new TestSemantics.root(),
      ignoreRect: true,
      ignoreTransform: true,
    ));

    semantics.dispose();
  });
200
}