semantics_debugger_test.dart 7.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('SemanticsDebugger smoke test', (WidgetTester tester) async {
    // This is a smoketest to verify that adding a debugger doesn't crash.
    await tester.pumpWidget(
      new Stack(
        children: <Widget>[
15 16
          const Semantics(),
          const Semantics(
17
            container: true,
18
          ),
19
          const Semantics(
20
            label: 'label',
21
          ),
22 23
        ],
      ),
24 25 26 27 28 29
    );

    await tester.pumpWidget(
      new SemanticsDebugger(
        child: new Stack(
          children: <Widget>[
30 31
            const Semantics(),
            const Semantics(
32
              container: true,
33
            ),
34
            const Semantics(
35
              label: 'label',
36
            ),
37 38 39
          ],
        ),
      ),
40 41 42 43 44
    );

    expect(true, isTrue); // expect that we reach here without crashing
  });

45 46
  testWidgets('SemanticsDebugger reparents subtree',
      (WidgetTester tester) async {
47
    final GlobalKey key = new GlobalKey();
48 49 50 51 52

    await tester.pumpWidget(
      new SemanticsDebugger(
        child: new Stack(
          children: <Widget>[
53
            const Semantics(label: 'label1'),
54
            new Positioned(
55 56 57 58 59
              key: key,
              left: 0.0,
              top: 0.0,
              width: 100.0,
              height: 100.0,
60
              child: const Semantics(label: 'label2'),
61
            ),
62 63 64
          ],
        ),
      ),
65 66 67 68 69 70
    );

    await tester.pumpWidget(
      new SemanticsDebugger(
        child: new Stack(
          children: <Widget>[
71
            const Semantics(label: 'label1'),
72 73 74 75 76
            new Semantics(
              container: true,
              child: new Stack(
                children: <Widget>[
                  new Positioned(
77 78 79 80 81
                    key: key,
                    left: 0.0,
                    top: 0.0,
                    width: 100.0,
                    height: 100.0,
82
                    child: const Semantics(label: 'label2'),
83
                  ),
84
                  const Semantics(label: 'label3'),
85 86 87 88 89 90
                ],
              ),
            ),
          ],
        ),
      ),
91 92 93 94 95 96
    );

    await tester.pumpWidget(
      new SemanticsDebugger(
        child: new Stack(
          children: <Widget>[
97
            const Semantics(label: 'label1'),
98 99 100 101 102
            new Semantics(
              container: true,
              child: new Stack(
                children: <Widget>[
                  new Positioned(
103 104 105 106 107
                      key: key,
                      left: 0.0,
                      top: 0.0,
                      width: 100.0,
                      height: 100.0,
108 109 110
                      child: const Semantics(label: 'label2')),
                  const Semantics(label: 'label3'),
                  const Semantics(label: 'label4'),
111 112 113 114 115 116
                ],
              ),
            ),
          ],
        ),
      ),
117 118 119 120
    );

    expect(tester.takeException(), isNull);
  });
121 122 123 124 125 126 127 128

  testWidgets('SemanticsDebugger interaction test',
      (WidgetTester tester) async {
    final List<String> log = <String>[];

    await tester.pumpWidget(
      new SemanticsDebugger(
        child: new Material(
129
          child: new ListView(
130 131 132 133 134
            children: <Widget>[
              new RaisedButton(
                onPressed: () {
                  log.add('top');
                },
135
                child: const Text('TOP'),
136 137 138 139 140
              ),
              new RaisedButton(
                onPressed: () {
                  log.add('bottom');
                },
141
                child: const Text('BOTTOM'),
142 143 144
              ),
            ],
          ),
145 146 147 148 149 150 151 152 153 154 155 156 157 158
        ),
      ),
    );

    await tester.tap(find.text('TOP'));
    expect(log, equals(<String>['top']));
    log.clear();

    await tester.tap(find.text('BOTTOM'));
    expect(log, equals(<String>['bottom']));
    log.clear();
  });

  testWidgets('SemanticsDebugger scroll test', (WidgetTester tester) async {
159
    final Key childKey = new UniqueKey();
160 161 162

    await tester.pumpWidget(
      new SemanticsDebugger(
163
        child: new ListView(
164 165 166 167
          children: <Widget>[
            new Container(
              key: childKey,
              height: 5000.0,
168
              color: Colors.green[500],
169 170 171 172 173 174
            ),
          ],
        ),
      ),
    );

175
    expect(tester.getTopLeft(find.byKey(childKey)).dy, equals(0.0));
176

177
    await tester.fling(find.byType(ListView), const Offset(0.0, -200.0), 200.0);
178 179
    await tester.pump();

180
    expect(tester.getTopLeft(find.byKey(childKey)).dy, equals(-480.0));
181

182
    await tester.fling(find.byType(ListView), const Offset(200.0, 0.0), 200.0);
183 184
    await tester.pump();

185
    expect(tester.getTopLeft(find.byKey(childKey)).dy, equals(-480.0));
186

187
    await tester.fling(find.byType(ListView), const Offset(-200.0, 0.0), 200.0);
188 189
    await tester.pump();

190
    expect(tester.getTopLeft(find.byKey(childKey)).dy, equals(-480.0));
191

192
    await tester.fling(find.byType(ListView), const Offset(0.0, 200.0), 200.0);
193 194
    await tester.pump();

195
    expect(tester.getTopLeft(find.byKey(childKey)).dy, equals(0.0));
196 197 198 199 200 201 202 203 204 205 206 207
  });

  testWidgets('SemanticsDebugger long press', (WidgetTester tester) async {
    bool didLongPress = false;

    await tester.pumpWidget(
      new SemanticsDebugger(
        child: new GestureDetector(
          onLongPress: () {
            expect(didLongPress, isFalse);
            didLongPress = true;
          },
208
          child: const Text('target'),
209 210 211 212 213 214 215 216 217 218 219 220 221
        ),
      ),
    );

    await tester.longPress(find.text('target'));
    expect(didLongPress, isTrue);
  });

  testWidgets('SemanticsDebugger slider', (WidgetTester tester) async {
    double value = 0.75;

    await tester.pumpWidget(
      new SemanticsDebugger(
222 223 224 225 226 227 228 229 230 231
        child: new Directionality(
          textDirection: TextDirection.ltr,
          child: new Material(
            child: new Center(
              child: new Slider(
                value: value,
                onChanged: (double newValue) {
                  value = newValue;
                },
              ),
232 233 234 235 236 237
            ),
          ),
        ),
      ),
    );

238 239 240 241 242 243
    // The fling below must be such that the velocity estimation examines an
    // offset greater than the kTouchSlop. Too slow or too short a distance, and
    // it won't trigger. The actual distance moved doesn't matter since this is
    // interpreted as a gesture by the semantics debugger and sent to the widget
    // as a semantic action that always moves by 10% of the complete track.
    await tester.fling(find.byType(Slider), const Offset(-100.0, 0.0), 2000.0);
244 245 246 247
    expect(value, equals(0.65));
  });

  testWidgets('SemanticsDebugger checkbox', (WidgetTester tester) async {
248 249
    final Key keyTop = new UniqueKey();
    final Key keyBottom = new UniqueKey();
250 251

    bool valueTop = false;
252
    final bool valueBottom = true;
253 254 255 256

    await tester.pumpWidget(
      new SemanticsDebugger(
        child: new Material(
257
          child: new ListView(
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 285 286 287
            children: <Widget>[
              new Checkbox(
                key: keyTop,
                value: valueTop,
                onChanged: (bool newValue) {
                  valueTop = newValue;
                },
              ),
              new Checkbox(
                key: keyBottom,
                value: valueBottom,
                onChanged: null,
              ),
            ],
          ),
        ),
      ),
    );

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

    expect(valueTop, isTrue);
    valueTop = false;
    expect(valueTop, isFalse);

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

    expect(valueTop, isFalse);
    expect(valueTop, isFalse);
  });
288
}