smoke_test.dart 3.85 KB
Newer Older
1 2 3 4
// Copyright 2016 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.

5 6
import 'dart:collection' show LinkedHashSet;

7 8
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
9
import 'package:flutter_gallery/gallery/item.dart' show GalleryItem, kAllGalleryItems;
10
import 'package:flutter_gallery/main.dart' as flutter_gallery_main;
11

12 13
const String kCaption = 'Flutter Gallery';

14 15 16 17 18 19
final List<String> demoCategories = new LinkedHashSet<String>.from(
  kAllGalleryItems.map((GalleryItem item) => item.category)
).toList();

final List<String> routeNames =
  kAllGalleryItems.map((GalleryItem item) => item.routeName).toList();
20 21

Finder findGalleryItemByRouteName(WidgetTester tester, String routeName) {
22
  return find.byWidgetPredicate((Widget widget) {
23
    return widget is GalleryItem && widget.routeName == routeName;
24 25
  });
}
26 27

// Start a gallery demo and then go back. This function assumes that the
28 29
// we're starting on the home route and that the submenu that contains
// the item for a demo that pushes route 'routeName' is already open.
30
Future<Null> smokeDemo(WidgetTester tester, String routeName) async {
31
  // Ensure that we're (likely to be) on the home page
32
  final Finder menuItem = findGalleryItemByRouteName(tester, routeName);
33
  expect(menuItem, findsOneWidget);
34

35 36
  await tester.tap(menuItem);
  await tester.pump(); // Launch the demo.
37 38 39
  await tester.pump(const Duration(seconds: 1)); // Wait until the demo has opened.

  expect(find.text(kCaption), findsNothing);
40 41

  // Go back
42
  Finder backButton = find.byTooltip('Back');
43 44 45
  expect(backButton, findsOneWidget);
  await tester.tap(backButton);
  await tester.pump(); // Start the pop "back" operation.
46 47
  await tester.pump(const Duration(seconds: 1)); // Wait until it has finished.
  return null;
48 49
}

50
void main() {
51
  testWidgets('Flutter Gallery app smoke test', (WidgetTester tester) async {
52 53
    flutter_gallery_main
        .main(); // builds the app and schedules a frame but doesn't trigger one
54 55
    await tester.pump(); // see https://github.com/flutter/flutter/issues/1865
    await tester.pump(); // triggers a frame
56

57 58
    expect(find.text(kCaption), findsOneWidget);

59 60 61
    final List<double> scrollDeltas = new List<double>();
    double previousY = tester.getTopRight(find.text(demoCategories[0])).y;
    for (String routeName in routeNames) {
62
      final double y = tester.getTopRight(findGalleryItemByRouteName(tester, routeName)).y;
63 64 65 66 67 68 69
      scrollDeltas.add(previousY - y);
      previousY = y;
    }

    // Launch each demo and then scroll that item out of the way.
    for (int i = 0; i < routeNames.length; i += 1) {
      final String routeName = routeNames[i];
70
      await smokeDemo(tester, routeName);
71
      await tester.scroll(findGalleryItemByRouteName(tester, routeName), new Offset(0.0, scrollDeltas[i]));
72 73
      await tester.pump(); // start the scroll
      await tester.pump(const Duration(milliseconds: 500)); // wait for overscroll to timeout, if necessary
74
      await tester.pump(const Duration(seconds: 3)); // wait for overscroll to fade away, if necessary
75
      tester.binding.debugAssertNoTransientCallbacks('A transient callback was still active after leaving route $routeName');
76 77
    }

78
    Finder navigationMenuButton = find.byTooltip('Open navigation menu');
79
    expect(navigationMenuButton, findsOneWidget);
80 81
    await tester.tap(navigationMenuButton);
    await tester.pump(); // Start opening drawer.
82
    await tester.pump(const Duration(seconds: 1)); // Wait until it's really opened.
83 84

    // switch theme
85 86 87
    await tester.tap(find.text('Dark'));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
88 89

    // switch theme
90 91 92
    await tester.tap(find.text('Light'));
    await tester.pump();
    await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
93
  });
94
}