From 0629030a058eca69d57c08f11951eea9908fc8ad Mon Sep 17 00:00:00 2001 From: "Ming Lyu (CareF)" Date: Thu, 13 Aug 2020 00:01:03 -0400 Subject: [PATCH] WidgetTester.drag with time duration (#63410) --- .../test/picture_cache_perf_e2e.dart | 37 ++++++ .../picture_cache_perf__e2e_summary.dart | 14 +++ dev/devicelab/lib/tasks/perf_tests.dart | 7 ++ dev/devicelab/manifest.yaml | 7 ++ packages/flutter_test/lib/src/controller.dart | 114 +++++++++++++++++- .../flutter_test/test/controller_test.dart | 37 ++++++ 6 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 dev/benchmarks/macrobenchmarks/test/picture_cache_perf_e2e.dart create mode 100644 dev/devicelab/bin/tasks/picture_cache_perf__e2e_summary.dart diff --git a/dev/benchmarks/macrobenchmarks/test/picture_cache_perf_e2e.dart b/dev/benchmarks/macrobenchmarks/test/picture_cache_perf_e2e.dart new file mode 100644 index 0000000000..e39385c112 --- /dev/null +++ b/dev/benchmarks/macrobenchmarks/test/picture_cache_perf_e2e.dart @@ -0,0 +1,37 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter/gestures.dart'; +import 'package:flutter/widgets.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:macrobenchmarks/common.dart'; + +import 'util.dart'; + +void main() { + macroPerfTestE2E( + 'picture_cache_perf', + kPictureCacheRouteName, + pageDelay: const Duration(seconds: 1), + body: (WidgetController controller) async { + final Finder nestedScroll = find.byKey(const ValueKey('tabbar_view')); + expect(nestedScroll, findsOneWidget); + Future _scrollOnce(double offset) async { + await controller.timedDrag( + nestedScroll, + Offset(-offset, 0.0), + const Duration(milliseconds: 300), + ); + await Future.delayed(const Duration(milliseconds: 500)); + } + for (int i = 0; i < 3; i += 1) { + await _scrollOnce(-300.0); + await _scrollOnce(-300.0); + await _scrollOnce(300.0); + await _scrollOnce(300.0); + } + }, + ); +} diff --git a/dev/devicelab/bin/tasks/picture_cache_perf__e2e_summary.dart b/dev/devicelab/bin/tasks/picture_cache_perf__e2e_summary.dart new file mode 100644 index 0000000000..1b5ef751f4 --- /dev/null +++ b/dev/devicelab/bin/tasks/picture_cache_perf__e2e_summary.dart @@ -0,0 +1,14 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:async'; + +import 'package:flutter_devicelab/tasks/perf_tests.dart'; +import 'package:flutter_devicelab/framework/adb.dart'; +import 'package:flutter_devicelab/framework/framework.dart'; + +Future main() async { + deviceOperatingSystem = DeviceOperatingSystem.android; + await task(createPictureCachePerfE2ETest()); +} diff --git a/dev/devicelab/lib/tasks/perf_tests.dart b/dev/devicelab/lib/tasks/perf_tests.dart index db42eeb9cf..0b2b40c507 100644 --- a/dev/devicelab/lib/tasks/perf_tests.dart +++ b/dev/devicelab/lib/tasks/perf_tests.dart @@ -158,6 +158,13 @@ TaskFunction createPictureCachePerfTest() { ).run; } +TaskFunction createPictureCachePerfE2ETest() { + return E2EPerfTest( + '${flutterDirectory.path}/dev/benchmarks/macrobenchmarks', + 'test/picture_cache_perf_e2e.dart', + ).run; +} + TaskFunction createFlutterGalleryStartupTest() { return StartupTest( '${flutterDirectory.path}/dev/integration_tests/flutter_gallery', diff --git a/dev/devicelab/manifest.yaml b/dev/devicelab/manifest.yaml index 48a9e1d31c..9f60c8c347 100644 --- a/dev/devicelab/manifest.yaml +++ b/dev/devicelab/manifest.yaml @@ -190,6 +190,13 @@ tasks: stage: devicelab required_agent_capabilities: ["mac/android"] + picture_cache_perf__e2e_summary: + description: > + Measures the runtime performance of raster caching many pictures on Android. + stage: devicelab + required_agent_capabilities: ["linux/android"] + flaky: true + cubic_bezier_perf__timeline_summary: description: > Measures the runtime performance of cubic bezier animations on Android. diff --git a/packages/flutter_test/lib/src/controller.dart b/packages/flutter_test/lib/src/controller.dart index bc8e25b7dc..ec8b619e9d 100644 --- a/packages/flutter_test/lib/src/controller.dart +++ b/packages/flutter_test/lib/src/controller.dart @@ -476,6 +476,9 @@ abstract class WidgetController { /// If you want the drag to end with a speed so that the gesture recognition /// system identifies the gesture as a fling, consider using [fling] instead. /// + /// The operation happens at once. If you want the drag to last for a period + /// of time, consider using [timedDrag]. + /// /// {@template flutter.flutter_test.drag} /// By default, if the x or y component of offset is greater than /// [kDragSlopDefault], the gesture is broken up into two separate moves @@ -501,7 +504,6 @@ abstract class WidgetController { double touchSlopX = kDragSlopDefault, double touchSlopY = kDragSlopDefault, }) { - assert(kDragSlopDefault > kTouchSlop); return dragFrom( getCenter(finder), offset, @@ -519,6 +521,9 @@ abstract class WidgetController { /// system identifies the gesture as a fling, consider using [flingFrom] /// instead. /// + /// The operation happens at once. If you want the drag to last for a period + /// of time, consider using [timedDragFrom]. + /// /// {@macro flutter.flutter_test.drag} Future dragFrom( Offset startLocation, @@ -601,6 +606,113 @@ abstract class WidgetController { }); } + /// Attempts to drag the given widget by the given offset in the `duration` + /// time, starting in the middle of the widget. + /// + /// If the middle of the widget is not exposed, this might send + /// events to another object. + /// + /// This is the timed version of [drag]. This may or may not result in a + /// [fling] or ballistic animation, depending on the speed from + /// `offset/duration`. + /// + /// {@template flutter.flutter_test.timeddrag} + /// The move events are sent at a given `frequency` in Hz (or events per + /// second). It defaults to 60Hz. + /// + /// The movement is linear in time. + /// + /// See also [LiveTestWidgetsFlutterBindingFramePolicy.benchmarkLive] for + /// more accurate time control. + /// {@endtemplate} + Future timedDrag( + Finder finder, + Offset offset, + Duration duration, { + int pointer, + int buttons = kPrimaryButton, + double frequency = 60.0, + }) { + return timedDragFrom( + getCenter(finder), + offset, + duration, + pointer: pointer, + buttons: buttons, + frequency: frequency, + ); + } + + /// Attempts a series of [PointerEvent]s to simulate a drag operation in the + /// `duration` time. + /// + /// This is the timed version of [dragFrom]. This may or may not result in a + /// [flingFrom] or ballistic animation, depending on the speed from + /// `offset/duration`. + /// + /// {@macro flutter.flutter_test.timeddrag} + Future timedDragFrom( + Offset startLocation, + Offset offset, + Duration duration, { + int pointer, + int buttons = kPrimaryButton, + double frequency = 60.0, + }) { + assert(frequency > 0); + final int intervals = duration.inMicroseconds * frequency ~/ 1E6; + assert(intervals > 1); + final List timeStamps = [ + for (int t = 0; t <= intervals; t += 1) + duration * t ~/ intervals, + ]; + final List offsets = [ + startLocation, + for (int t = 0; t <= intervals; t += 1) + startLocation + offset * (t / intervals), + ]; + final List records = [ + PointerEventRecord(Duration.zero, [ + PointerAddedEvent( + timeStamp: Duration.zero, + position: startLocation, + ), + PointerDownEvent( + timeStamp: Duration.zero, + position: startLocation, + pointer: pointer, + buttons: buttons, + ), + ]), + ...[ + for(int t = 0; t <= intervals; t += 1) + PointerEventRecord(timeStamps[t], [ + PointerMoveEvent( + timeStamp: timeStamps[t], + position: offsets[t+1], + delta: offsets[t+1] - offsets[t], + pointer: pointer, + buttons: buttons, + ) + ]), + ], + PointerEventRecord(duration, [ + PointerUpEvent( + timeStamp: duration, + position: offsets.last, + pointer: pointer, + // The PointerData recieved from the engine with + // chagne = PointerChnage.up, which translates to PointerUpEvent, + // doesn't provide the button field. + // buttons: buttons, + ) + ]), + ]; + return TestAsyncUtils.guard(() async { + return handlePointerEventRecord(records); + }); + } + /// The next available pointer identifier. /// /// This is the default pointer identifier that will be used the next time the diff --git a/packages/flutter_test/test/controller_test.dart b/packages/flutter_test/test/controller_test.dart index afbffdf396..0c3f45e199 100644 --- a/packages/flutter_test/test/controller_test.dart +++ b/packages/flutter_test/test/controller_test.dart @@ -577,6 +577,43 @@ void main() { } }); + testWidgets( + 'WidgetTester.timedDrag must respect buttons', + (WidgetTester tester) async { + final List logs = []; + + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Listener( + onPointerDown: (PointerDownEvent event) => logs.add('down ${event.buttons}'), + onPointerMove: (PointerMoveEvent event) => logs.add('move ${event.buttons}'), + onPointerUp: (PointerUpEvent event) => logs.add('up ${event.buttons}'), + child: const Text('test'), + ), + ), + ); + + await tester.timedDrag( + find.text('test'), + const Offset(-200.0, 0.0), + const Duration(seconds: 1), + buttons: kSecondaryMouseButton, + ); + await tester.pumpAndSettle(); + + const String b = '$kSecondaryMouseButton'; + for(int i = 0; i < logs.length; i++) { + if (i == 0) + expect(logs[i], 'down $b'); + else if (i != logs.length - 1) + expect(logs[i], 'move $b'); + else + expect(logs[i], 'up 0'); + } + }, + ); + testWidgets( 'ensureVisible: scrolls to make widget visible', (WidgetTester tester) async {