Fix timestamp of touch events should use system startup time (flutter/engine#30422)

This commit is contained in:
wangying
2022-03-14 02:07:09 +08:00
committed by GitHub
parent 51456f9b07
commit 6a04696e6f
2 changed files with 28 additions and 10 deletions

View File

@@ -421,16 +421,24 @@ static UIView* GetViewOrPlaceholder(UIView* existing_view) {
_scrollView.reset(scrollView);
}
- (flutter::PointerData)generatePointerDataForFake {
flutter::PointerData pointer_data;
pointer_data.Clear();
pointer_data.kind = flutter::PointerData::DeviceKind::kTouch;
// `UITouch.timestamp` is defined as seconds since system startup. Synthesized events can get this
// time with `NSProcessInfo.systemUptime`. See
// https://developer.apple.com/documentation/uikit/uitouch/1618144-timestamp?language=objc
pointer_data.time_stamp = [[NSProcessInfo processInfo] systemUptime] * kMicrosecondsPerSecond;
return pointer_data;
}
static void SendFakeTouchEvent(FlutterEngine* engine,
CGPoint location,
flutter::PointerData::Change change) {
const CGFloat scale = [UIScreen mainScreen].scale;
flutter::PointerData pointer_data;
pointer_data.Clear();
flutter::PointerData pointer_data = [[engine viewController] generatePointerDataForFake];
pointer_data.physical_x = location.x * scale;
pointer_data.physical_y = location.y * scale;
pointer_data.kind = flutter::PointerData::DeviceKind::kTouch;
pointer_data.time_stamp = [[NSDate date] timeIntervalSince1970] * kMicrosecondsPerSecond;
auto packet = std::make_unique<flutter::PointerDataPacket>(/*count=*/1);
pointer_data.change = change;
packet->SetPointerData(0, pointer_data);
@@ -759,14 +767,9 @@ static void SendFakeTouchEvent(FlutterEngine* engine,
// touches to the framework so nothing gets orphaned.
for (NSNumber* device in _ongoingTouches.get()) {
// Create fake PointerData to balance out each previously started one for the framework.
flutter::PointerData pointer_data;
pointer_data.Clear();
// Use current time.
pointer_data.time_stamp = [[NSDate date] timeIntervalSince1970] * kMicrosecondsPerSecond;
flutter::PointerData pointer_data = [self generatePointerDataForFake];
pointer_data.change = flutter::PointerData::Change::kCancel;
pointer_data.kind = flutter::PointerData::DeviceKind::kTouch;
pointer_data.device = device.longLongValue;
pointer_data.pointer_identifier = 0;

View File

@@ -7,6 +7,7 @@
#include "flutter/fml/platform/darwin/message_loop_darwin.h"
#import "flutter/lib/ui/window/platform_configuration.h"
#include "flutter/lib/ui/window/pointer_data.h"
#import "flutter/lib/ui/window/viewport_metrics.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h"
@@ -138,6 +139,7 @@ typedef enum UIAccessibilityContrast : NSInteger {
- (void)ensureViewportMetricsIsCorrect;
- (void)invalidateDisplayLink;
- (void)addInternalPlugins;
- (flutter::PointerData)generatePointerDataForFake;
@end
@interface FlutterViewControllerTest : XCTestCase
@@ -1092,4 +1094,17 @@ typedef enum UIAccessibilityContrast : NSInteger {
dispatchPointerDataPacket:std::make_unique<flutter::PointerDataPacket>(0)];
}
- (void)testFakeEventTimeStamp {
FlutterViewController* vc = [[FlutterViewController alloc] initWithEngine:self.mockEngine
nibName:nil
bundle:nil];
XCTAssertNotNil(vc);
flutter::PointerData pointer_data = [vc generatePointerDataForFake];
int64_t current_micros = [[NSProcessInfo processInfo] systemUptime] * 1000 * 1000;
int64_t interval_micros = current_micros - pointer_data.time_stamp;
const int64_t tolerance_millis = 2;
XCTAssertTrue(interval_micros / 1000 < tolerance_millis,
@"PointerData.time_stamp should be equal to NSProcessInfo.systemUptime");
}
@end