smoke_test.dart 7.52 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
import 'dart:collection' show LinkedHashSet;
6
import 'dart:math' as math;
7

8
import 'package:flutter/material.dart';
9
import 'package:flutter/rendering.dart';
10
import 'package:flutter_test/flutter_test.dart';
11
import 'package:flutter_gallery/gallery/item.dart' show GalleryItem, kAllGalleryItems;
12
import 'package:flutter_gallery/gallery/app.dart' show GalleryApp;
13

14 15
const String kCaption = 'Flutter Gallery';

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

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

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

29 30 31 32 33 34
int errors = 0;

void reportToStringError(String name, String route, int lineNumber, List<String> lines, String message) {
  // If you're on line 12, then it has index 11.
  // If you want 1 line before and 1 line after, then you want lines with index 10, 11, and 12.
  // That's (lineNumber-1)-margin .. (lineNumber-1)+margin, or lineNumber-(margin+1) .. lineNumber+(margin-1)
35 36 37
  final int margin = 5;
  final int firstLine = math.max(0, lineNumber - margin);
  final int lastLine = math.min(lines.length, lineNumber + margin);
38 39 40 41 42 43
  print('$name : $route : line $lineNumber of ${lines.length} : $message; nearby lines were:\n  ${lines.sublist(firstLine, lastLine).join("\n  ")}');
  errors += 1;
}

void verifyToStringOutput(String name, String route, String testString) {
  int lineNumber = 0;
44
  final List<String> lines = testString.split('\n');
45 46 47 48 49 50 51 52 53 54 55 56 57 58
  if (!testString.endsWith('\n'))
    reportToStringError(name, route, lines.length, lines, 'does not end with a line feed');
  for (String line in lines) {
    lineNumber += 1;
    if (line == '' && lineNumber != lines.length) {
      reportToStringError(name, route, lineNumber, lines, 'found empty line');
    } else if (line.contains('Instance of ')) {
      reportToStringError(name, route, lineNumber, lines, 'found a class that does not have its own toString');
    } else if (line.endsWith(' ')) {
      reportToStringError(name, route, lineNumber, lines, 'found a line with trailing whitespace');
    }
  }
}

59
// Start a gallery demo and then go back. This function assumes that the
60 61
// 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.
62
Future<Null> smokeDemo(WidgetTester tester, String routeName) async {
63
  // Ensure that we're (likely to be) on the home page
64
  final Finder menuItem = findGalleryItemByRouteName(tester, routeName);
65
  expect(menuItem, findsOneWidget);
66

67 68 69 70
  // Don't use pumpUntilNoTransientCallbacks in this function, because some of
  // the smoketests have infinitely-running animations (e.g. the progress
  // indicators demo).

71 72
  await tester.tap(menuItem);
  await tester.pump(); // Launch the demo.
73
  await tester.pump(const Duration(milliseconds: 400)); // Wait until the demo has opened.
74
  expect(find.text(kCaption), findsNothing);
75 76 77 78 79

  // Leave the demo on the screen briefly for manual testing.
  await tester.pump(const Duration(milliseconds: 400));

  // Scroll the demo around a bit.
80 81
  await tester.flingFrom(const Offset(400.0, 300.0), const Offset(-100.0, 0.0), 500.0);
  await tester.flingFrom(const Offset(400.0, 300.0), const Offset(0.0, -100.0), 500.0);
82 83 84 85 86 87 88 89 90 91 92
  await tester.pump();
  await tester.pump(const Duration(milliseconds: 50));
  await tester.pump(const Duration(milliseconds: 200));
  await tester.pump(const Duration(milliseconds: 400));

  // Verify that the dumps are pretty.
  verifyToStringOutput('debugDumpApp', routeName, WidgetsBinding.instance.renderViewElement.toStringDeep());
  verifyToStringOutput('debugDumpRenderTree', routeName, RendererBinding.instance?.renderView?.toStringDeep());
  verifyToStringOutput('debugDumpLayerTree', routeName, RendererBinding.instance?.renderView?.debugLayer?.toStringDeep());

  // Scroll the demo around a bit more.
93
  await tester.flingFrom(const Offset(400.0, 300.0), const Offset(-200.0, 0.0), 500.0);
94 95 96 97
  await tester.pump();
  await tester.pump(const Duration(milliseconds: 50));
  await tester.pump(const Duration(milliseconds: 200));
  await tester.pump(const Duration(milliseconds: 400));
98
  await tester.flingFrom(const Offset(400.0, 300.0), const Offset(100.0, 0.0), 500.0);
99 100
  await tester.pump();
  await tester.pump(const Duration(milliseconds: 400));
101
  await tester.flingFrom(const Offset(400.0, 300.0), const Offset(0.0, 400.0), 1000.0);
102 103
  await tester.pump();
  await tester.pump(const Duration(milliseconds: 400));
104 105

  // Go back
106
  final Finder backButton = find.byTooltip('Back');
107 108 109
  expect(backButton, findsOneWidget);
  await tester.tap(backButton);
  await tester.pump(); // Start the pop "back" operation.
110
  await tester.pump(); // Complete the willPop() Future.
111
  await tester.pump(const Duration(milliseconds: 400)); // Wait until it has finished.
112
  return null;
113 114
}

115
Future<Null> runSmokeTest(WidgetTester tester) async {
116 117 118 119 120 121
  bool hasFeedback = false;
  void mockOnSendFeedback() {
    hasFeedback = true;
  }

  await tester.pumpWidget(new GalleryApp(onSendFeedback: mockOnSendFeedback));
122 123 124 125 126 127
  await tester.pump(); // see https://github.com/flutter/flutter/issues/1865
  await tester.pump(); // triggers a frame

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

  for (String routeName in routeNames) {
128
    final Finder finder = findGalleryItemByRouteName(tester, routeName);
Adam Barth's avatar
Adam Barth committed
129
    Scrollable.ensureVisible(tester.element(finder), alignment: 0.5);
130
    await tester.pumpAndSettle();
131 132 133
    await smokeDemo(tester, routeName);
    tester.binding.debugAssertNoTransientCallbacks('A transient callback was still active after leaving route $routeName');
  }
134 135
  expect(errors, 0);

136
  final Finder navigationMenuButton = find.byTooltip('Open navigation menu');
137 138 139 140 141
  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.

142
  // Switch theme.
143 144 145 146
  await tester.tap(find.text('Dark'));
  await tester.pump();
  await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.

147
  // Switch theme.
148 149 150
  await tester.tap(find.text('Light'));
  await tester.pump();
  await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
151

152 153 154 155 156 157 158 159 160 161 162
  // Switch font scale.
  await tester.tap(find.text('Small'));
  await tester.pump();
  await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.
  // Switch font scale back to default.
  await tester.tap(find.text('System Default'));
  await tester.pump();
  await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.

  // Scroll the 'Send feedback' item into view.
  await tester.drag(find.text('Small'), const Offset(0.0, -450.0));
163 164 165
  await tester.pump();
  await tester.pump(const Duration(seconds: 1)); // Wait until it's changed.

166
  // Send feedback.
167 168 169 170
  expect(hasFeedback, false);
  await tester.tap(find.text('Send feedback'));
  await tester.pump();
  expect(hasFeedback, true);
171 172
}

173
void main() {
174 175
  testWidgets('Flutter Gallery app smoke test', runSmokeTest);

176
  testWidgets('Flutter Gallery app smoke test with semantics', (WidgetTester tester) async {
177 178 179
    RendererBinding.instance.setSemanticsEnabled(true);
    await runSmokeTest(tester);
    RendererBinding.instance.setSemanticsEnabled(false);
180
  });
181
}