app_builder_test.dart 1.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright 2018 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_test/flutter_test.dart';
import 'package:flutter/material.dart';

void main() {
  testWidgets('builder doesn\'t get called if app doesn\'t change', (WidgetTester tester) async {
    final List<String> log = <String>[];
    final Widget app = new MaterialApp(
      theme: new ThemeData(
        primarySwatch: Colors.green,
      ),
      home: const Placeholder(),
      builder: (BuildContext context, Widget child) {
        log.add('build');
18
        expect(Theme.of(context).primaryColor, Colors.green);
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
        expect(Directionality.of(context), TextDirection.ltr);
        expect(child, const isInstanceOf<Navigator>());
        return const Placeholder();
      },
    );
    await tester.pumpWidget(
      new Directionality(
        textDirection: TextDirection.rtl,
        child: app,
      ),
    );
    expect(log, <String>['build']);
    await tester.pumpWidget(
      new Directionality(
        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(
      new MaterialApp(
        theme: new ThemeData(
          primarySwatch: Colors.yellow,
        ),
        home: new Builder(
          builder: (BuildContext context) {
            log.add('build');
50
            expect(Theme.of(context).primaryColor, Colors.yellow);
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
            expect(Directionality.of(context), TextDirection.rtl);
            return const Placeholder();
          },
        ),
        builder: (BuildContext context, Widget child) {
          return new Directionality(
            textDirection: TextDirection.rtl,
            child: child,
          );
        },
      ),
    );
    expect(log, <String>['build']);
  });
}