live_smoketest.dart 5.66 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
import 'package:flutter/gestures.dart' show kPrimaryButton;
15 16
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
17
import 'package:flutter/services.dart';
18
import 'package:flutter_gallery/gallery/app.dart' show GalleryApp;
19 20
import 'package:flutter_gallery/gallery/demos.dart';
import 'package:flutter_test/flutter_test.dart';
21

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

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

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

41
// There are 3 places where the Gallery demos are traversed.
42 43 44
// 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
45 46 47 48
//
// If you change navigation behavior in the Gallery or in the framework, make
// sure all 3 are covered.

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

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

91 92 93 94 95 96 97 98 99 100 101 102
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',
);

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

  /// 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].
111
  Future<void> _waitUntilFrame(bool Function() condition, [Completer<void>? completer]) {
112
    completer ??= Completer<void>();
113
    if (!condition()) {
114
      SchedulerBinding.instance!.addPostFrameCallback((Duration timestamp) {
115 116 117 118 119 120 121 122 123 124 125
        _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)
126
      await _waitUntilFrame(() => binding.transientCallbackCount == 0);
127 128
    await _waitUntilFrame(() => finder.precache());
    if (frameSync)
129
      await _waitUntilFrame(() => binding.transientCallbackCount == 0);
130 131 132
    return finder;
  }

133
  @override
134 135
  Future<void> tap(Finder finder, { int? pointer, int buttons = kPrimaryButton, bool warnIfMissed = true }) async {
    await super.tap(await _waitForElement(finder), pointer: pointer, buttons: buttons, warnIfMissed: warnIfMissed);
136 137
  }

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