smoke_test.dart 4.29 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
import 'package:flutter/material.dart';
8
import 'package:flutter/rendering.dart';
9
import 'package:flutter_test/flutter_test.dart';
10
import 'package:flutter_gallery/gallery/item.dart' show GalleryItem, kAllGalleryItems;
11
import 'package:flutter_gallery/gallery/app.dart' show GalleryApp;
12

13 14
const String kCaption = 'Flutter Gallery';

15 16 17 18 19 20
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();
21 22

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

// Start a gallery demo and then go back. This function assumes that the
29 30
// 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.
31
Future<Null> smokeDemo(WidgetTester tester, String routeName) async {
32
  // Ensure that we're (likely to be) on the home page
33
  final Finder menuItem = findGalleryItemByRouteName(tester, routeName);
34
  expect(menuItem, findsOneWidget);
35

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

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

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

51
Future<Null> runSmokeTest(WidgetTester tester) async {
52 53 54 55 56 57
  bool hasFeedback = false;
  void mockOnSendFeedback() {
    hasFeedback = true;
  }

  await tester.pumpWidget(new GalleryApp(onSendFeedback: mockOnSendFeedback));
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
  await tester.pump(); // see https://github.com/flutter/flutter/issues/1865
  await tester.pump(); // triggers a frame

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

  final List<double> scrollDeltas = new List<double>();
  double previousY = tester.getTopRight(find.text(demoCategories[0])).y;
  for (String routeName in routeNames) {
    final double y = tester.getTopRight(findGalleryItemByRouteName(tester, routeName)).y;
    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];
    await smokeDemo(tester, routeName);
    await tester.scroll(findGalleryItemByRouteName(tester, routeName), new Offset(0.0, scrollDeltas[i]));
    await tester.pump(); // start the scroll
    await tester.pump(const Duration(milliseconds: 500)); // wait for overscroll to timeout, if necessary
    await tester.pump(const Duration(seconds: 3)); // wait for overscroll to fade away, if necessary
    tester.binding.debugAssertNoTransientCallbacks('A transient callback was still active after leaving route $routeName');
  }

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

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

  // switch theme
  await tester.tap(find.text('Light'));
  await tester.pump();
  await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
97 98 99 100 101 102

  // send feedback
  expect(hasFeedback, false);
  await tester.tap(find.text('Send feedback'));
  await tester.pump();
  expect(hasFeedback, true);
103 104
}

105
void main() {
106 107
  testWidgets('Flutter Gallery app smoke test', runSmokeTest);

108
  testWidgets('Flutter Gallery app smoke test', (WidgetTester tester) async {
109 110 111
    RendererBinding.instance.setSemanticsEnabled(true);
    await runSmokeTest(tester);
    RendererBinding.instance.setSemanticsEnabled(false);
112
  });
113
}