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

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

void main() {
9
  testWidgets("builder doesn't get called if app doesn't change", (WidgetTester tester) async {
10
    final List<String> log = <String>[];
11 12
    final Widget app = MaterialApp(
      theme: ThemeData(
13 14 15
        primarySwatch: Colors.green,
      ),
      home: const Placeholder(),
16
      builder: (BuildContext context, Widget? child) {
17
        log.add('build');
18
        expect(Theme.of(context).primaryColor, Colors.green);
19
        expect(Directionality.of(context), TextDirection.ltr);
Dan Field's avatar
Dan Field committed
20
        expect(child, isA<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
        textDirection: TextDirection.ltr,
        child: app,
      ),
    );
    expect(log, <String>['build']);
  });

40
  testWidgets("builder doesn't get called if app doesn't change", (WidgetTester tester) async {
41 42
    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
            expect(Directionality.of(context), TextDirection.rtl);
            return const Placeholder();
          },
        ),
55
        builder: (BuildContext context, Widget? child) {
56
          return Directionality(
57
            textDirection: TextDirection.rtl,
58
            child: child!,
59 60 61 62 63 64 65
          );
        },
      ),
    );
    expect(log, <String>['build']);
  });
}