live_smoketest.dart 4.71 KB
Newer Older
1 2 3 4
// Copyright 2017 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 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 13 14 15 16 17 18
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_test/flutter_test.dart';

19 20
import 'package:flutter_gallery/gallery/demos.dart';
import 'package:flutter_gallery/gallery/app.dart' show GalleryApp;
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
  'Pull to refresh',
37 38 39
  'Progress indicators',
  'Activity Indicator',
  'Video',
40 41
];

42 43
Future<Null> main() async {
  try {
44 45
    // Verify that _kUnsynchronizedDemos and _kSkippedDemos identify
    // demos that actually exist.
46 47 48 49 50
    final List<String> allDemoTitles = kAllGalleryDemos.map((GalleryDemo demo) => demo.title).toList();
    if (!new Set<String>.from(allDemoTitles).containsAll(_kUnsynchronizedDemoTitles))
      fail('Unrecognized demo titles in _kUnsynchronizedDemosTitles: $_kUnsynchronizedDemoTitles');
    if (!new Set<String>.from(allDemoTitles).containsAll(_kSkippedDemoTitles))
      fail('Unrecognized demo names in _kSkippedDemoTitles: $_kSkippedDemoTitles');
51

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

84 85
class _LiveWidgetController extends LiveWidgetController {
  _LiveWidgetController(WidgetsBinding binding) : super(binding);
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

  /// 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].
  Future<Null> _waitUntilFrame(bool condition(), [Completer<Null> completer]) {
    completer ??= new Completer<Null>();
    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)
107
      await _waitUntilFrame(() => binding.transientCallbackCount == 0);
108 109
    await _waitUntilFrame(() => finder.precache());
    if (frameSync)
110
      await _waitUntilFrame(() => binding.transientCallbackCount == 0);
111 112 113
    return finder;
  }

114 115
  @override
  Future<Null> tap(Finder finder, { int pointer }) async {
116
    await super.tap(await _waitForElement(finder), pointer: pointer);
117 118 119 120 121 122 123
  }

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