directionality_test.dart 3.16 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/widgets.dart';
6
import 'package:flutter_test/flutter_test.dart';
7 8

void main() {
9
  testWidgets('Directionality', (WidgetTester tester) async {
10
    final List<TextDirection> log = <TextDirection>[];
11
    final Widget inner = Builder(
12
      builder: (BuildContext context) {
13
        log.add(Directionality.of(context));
14
        return const Placeholder();
15
      },
16 17
    );
    await tester.pumpWidget(
18
      Directionality(
19 20 21 22 23 24
        textDirection: TextDirection.ltr,
        child: inner,
      ),
    );
    expect(log, <TextDirection>[TextDirection.ltr]);
    await tester.pumpWidget(
25
      Directionality(
26 27 28 29 30 31
        textDirection: TextDirection.ltr,
        child: inner,
      ),
    );
    expect(log, <TextDirection>[TextDirection.ltr]);
    await tester.pumpWidget(
32
      Directionality(
33 34 35 36 37 38
        textDirection: TextDirection.rtl,
        child: inner,
      ),
    );
    expect(log, <TextDirection>[TextDirection.ltr, TextDirection.rtl]);
    await tester.pumpWidget(
39
      Directionality(
40 41 42 43 44 45
        textDirection: TextDirection.rtl,
        child: inner,
      ),
    );
    expect(log, <TextDirection>[TextDirection.ltr, TextDirection.rtl]);
    await tester.pumpWidget(
46
      Directionality(
47 48 49 50 51 52 53
        textDirection: TextDirection.ltr,
        child: inner,
      ),
    );
    expect(log, <TextDirection>[TextDirection.ltr, TextDirection.rtl, TextDirection.ltr]);
  });

54
  testWidgets('Directionality default', (WidgetTester tester) async {
55
    bool good = false;
56
    await tester.pumpWidget(Builder(
57
      builder: (BuildContext context) {
58
        expect(Directionality.maybeOf(context), isNull);
59
        good = true;
60
        return const Placeholder();
61 62 63 64
      },
    ));
    expect(good, isTrue);
  });
65

66
  testWidgets('Directionality.maybeOf', (WidgetTester tester) async {
67 68 69 70 71 72 73 74 75 76
    final GlobalKey hasDirectionality = GlobalKey();
    final GlobalKey noDirectionality = GlobalKey();
    await tester.pumpWidget(
      Container(
        key: noDirectionality,
        child: Directionality(
          textDirection: TextDirection.rtl,
          child: Container(
            key: hasDirectionality,
          ),
77 78
        ),
      ),
79 80 81 82
    );
    expect(Directionality.maybeOf(noDirectionality.currentContext!), isNull);
    expect(Directionality.maybeOf(hasDirectionality.currentContext!), TextDirection.rtl);
  });
83

84
  testWidgets('Directionality.of', (WidgetTester tester) async {
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    final GlobalKey hasDirectionality = GlobalKey();
    final GlobalKey noDirectionality = GlobalKey();
    await tester.pumpWidget(
      Container(
        key: noDirectionality,
        child: Directionality(
          textDirection: TextDirection.rtl,
          child: Container(
            key: hasDirectionality,
          ),
        ),
      ),
    );
    expect(() => Directionality.of(noDirectionality.currentContext!), throwsA(isAssertionError.having(
      (AssertionError e) => e.message,
      'message',
      contains('No Directionality widget found.'),
    )));
    expect(Directionality.of(hasDirectionality.currentContext!), TextDirection.rtl);
  });
105
}