live_smoketest.dart 5.62 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 6 7 8 9 10
// ATTENTION!
//
// This file is not named "*_test.dart", and as such will not run when you run
// "flutter test". It is only intended to be run as part of the
// flutter_gallery_instrumentation_test devicelab test.

11 12
import 'dart:async';

13
import 'package:flutter/cupertino.dart';
14 15
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
16 17
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
18
import 'package:flutter/gestures.dart' show kPrimaryButton;
19 20
import 'package:flutter_test/flutter_test.dart';

21 22
import 'package:flutter_gallery/gallery/demos.dart';
import 'package:flutter_gallery/gallery/app.dart' show GalleryApp;
23

24
// Reports success or failure to the native code.
25
const MethodChannel _kTestChannel = MethodChannel('io.flutter.demo.gallery/TestLifecycleListener');
26

27 28
// We don't want to wait for animations to complete before tapping the
// back button in the demos with these titles.
29
const List<String> _kUnsynchronizedDemoTitles = <String>[
30 31 32 33 34 35 36
  'Progress indicators',
  'Activity Indicator',
  'Video',
];

// These demos can't be backed out of by tapping a button whose
// tooltip is 'Back'.
37
const List<String> _kSkippedDemoTitles = <String>[
38 39 40
  'Progress indicators',
  'Activity Indicator',
  'Video',
41 42
];

43
// There are 3 places where the Gallery demos are traversed.
44 45 46
// 1- In widget tests such as dev/integration_tests/flutter_gallery/test/smoke_test.dart
// 2- In driver tests such as dev/integration_tests/flutter_gallery/test_driver/transitions_perf_test.dart
// 3- In on-device instrumentation tests such as dev/integration_tests/flutter_gallery/test/live_smoketest.dart
47 48 49 50
//
// If you change navigation behavior in the Gallery or in the framework, make
// sure all 3 are covered.

51
Future<void> main() async {
52
  try {
53 54
    // Verify that _kUnsynchronizedDemos and _kSkippedDemos identify
    // demos that actually exist.
55
    final List<String> allDemoTitles = kAllGalleryDemos.map((GalleryDemo demo) => demo.title).toList();
56
    if (!Set<String>.from(allDemoTitles).containsAll(_kUnsynchronizedDemoTitles))
57
      fail('Unrecognized demo titles in _kUnsynchronizedDemosTitles: $_kUnsynchronizedDemoTitles');
58
    if (!Set<String>.from(allDemoTitles).containsAll(_kSkippedDemoTitles))
59
      fail('Unrecognized demo names in _kSkippedDemoTitles: $_kSkippedDemoTitles');
60

61
    print('Starting app...');
62
    runApp(const GalleryApp(testMode: true));
63
    final _LiveWidgetController controller = _LiveWidgetController(WidgetsBinding.instance);
64
    for (final GalleryDemoCategory category in kAllGalleryDemoCategories) {
65
      print('Tapping "${category.name}" section...');
66
      await controller.tap(find.text(category.name));
67
      for (final GalleryDemo demo in kGalleryCategoryToDemos[category]) {
68
        final Finder demoItem = find.text(demo.title);
69
        print('Scrolling to "${demo.title}"...');
70
        await controller.scrollIntoView(demoItem, alignment: 0.5);
71
        if (_kSkippedDemoTitles.contains(demo.title))
72 73
          continue;
        for (int i = 0; i < 2; i += 1) {
74
          print('Tapping "${demo.title}"...');
75 76
          await controller.tap(demoItem); // Launch the demo
          controller.frameSync = !_kUnsynchronizedDemoTitles.contains(demo.title);
77
          print('Going back to demo list...');
78
          await controller.tap(backFinder);
79 80
          controller.frameSync = true;
        }
81
      }
82
      print('Going back to home screen...');
83
      await controller.tap(find.byTooltip('Back'));
84
    }
85
    print('Finished successfully!');
86
    _kTestChannel.invokeMethod<void>('success');
87 88
  } catch (error, stack) {
    print('Caught error: $error\n$stack');
89
    _kTestChannel.invokeMethod<void>('failure');
90 91 92
  }
}

93 94 95 96 97 98 99 100 101 102 103 104
final Finder backFinder = find.byElementPredicate(
  (Element element) {
    final Widget widget = element.widget;
    if (widget is Tooltip)
      return widget.message == 'Back';
    if (widget is CupertinoNavigationBarBackButton)
      return true;
    return false;
  },
  description: 'Material or Cupertino back button',
);

105 106
class _LiveWidgetController extends LiveWidgetController {
  _LiveWidgetController(WidgetsBinding binding) : super(binding);
107 108 109 110 111 112

  /// With [frameSync] enabled, Flutter Driver will wait to perform an action
  /// until there are no pending frames in the app under test.
  bool frameSync = true;

  /// Waits until at the end of a frame the provided [condition] is [true].
113 114
  Future<void> _waitUntilFrame(bool condition(), [Completer<void> completer]) {
    completer ??= Completer<void>();
115 116 117 118 119 120 121 122 123 124 125 126 127
    if (!condition()) {
      SchedulerBinding.instance.addPostFrameCallback((Duration timestamp) {
        _waitUntilFrame(condition, completer);
      });
    } else {
      completer.complete();
    }
    return completer.future;
  }

  /// Runs `finder` repeatedly until it finds one or more [Element]s.
  Future<Finder> _waitForElement(Finder finder) async {
    if (frameSync)
128
      await _waitUntilFrame(() => binding.transientCallbackCount == 0);
129 130
    await _waitUntilFrame(() => finder.precache());
    if (frameSync)
131
      await _waitUntilFrame(() => binding.transientCallbackCount == 0);
132 133 134
    return finder;
  }

135
  @override
136 137
  Future<void> tap(Finder finder, { int pointer, int buttons = kPrimaryButton }) async {
    await super.tap(await _waitForElement(finder), pointer: pointer, buttons: buttons);
138 139
  }

140
  Future<void> scrollIntoView(Finder finder, {double alignment}) async {
141 142 143 144
    final Finder target = await _waitForElement(finder);
    await Scrollable.ensureVisible(target.evaluate().single, duration: const Duration(milliseconds: 100), alignment: alignment);
  }
}