app_builder_test.dart 1.95 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_test/flutter_test.dart';
6
import 'package:flutter/material.dart';
7 8 9 10

void main() {
  testWidgets('builder doesn\'t get called if app doesn\'t change', (WidgetTester tester) async {
    final List<String> log = <String>[];
11 12
    final Widget app = MaterialApp(
      theme: ThemeData(
13 14 15 16 17
        primarySwatch: Colors.green,
      ),
      home: const Placeholder(),
      builder: (BuildContext context, Widget child) {
        log.add('build');
18
        expect(Theme.of(context).primaryColor, Colors.green);
19
        expect(Directionality.of(context), TextDirection.ltr);
20
        expect(child, isInstanceOf<Navigator>());
21 22 23 24
        return const Placeholder();
      },
    );
    await tester.pumpWidget(
25
      Directionality(
26 27 28 29 30 31
        textDirection: TextDirection.rtl,
        child: app,
      ),
    );
    expect(log, <String>['build']);
    await tester.pumpWidget(
32
      Directionality(
33 34 35 36 37 38 39 40 41 42
        textDirection: TextDirection.ltr,
        child: app,
      ),
    );
    expect(log, <String>['build']);
  });

  testWidgets('builder doesn\'t get called if app doesn\'t change', (WidgetTester tester) async {
    final List<String> log = <String>[];
    await tester.pumpWidget(
43 44
      MaterialApp(
        theme: ThemeData(
45 46
          primarySwatch: Colors.yellow,
        ),
47
        home: Builder(
48 49
          builder: (BuildContext context) {
            log.add('build');
50
            expect(Theme.of(context).primaryColor, Colors.yellow);
51 52 53 54 55
            expect(Directionality.of(context), TextDirection.rtl);
            return const Placeholder();
          },
        ),
        builder: (BuildContext context, Widget child) {
56
          return Directionality(
57 58 59 60 61 62 63 64 65
            textDirection: TextDirection.rtl,
            child: child,
          );
        },
      ),
    );
    expect(log, <String>['build']);
  });
}