22#import " RunnerXCTestEventBridge.h"
33
44#import < CoreGraphics/CoreGraphics.h>
5- #import < TargetConditionals.h>
65#import < objc/message.h>
76
87static NSString *const RunnerGestureSynthesisSurface = @" event" ;
@@ -46,6 +45,17 @@ typedef id (*RunnerDragPointerPathFactory)(
4645 const RunnerGestureEventBridge *bridge,
4746 id record
4847);
48+ static id _Nullable RunnerAllocateEventRecord (
49+ const RunnerGestureEventBridge *bridge,
50+ NSString *recordName,
51+ NSInteger interfaceOrientation,
52+ NSInteger targetProcessID
53+ );
54+ static void RunnerPrepareSynthesizedInput (
55+ const RunnerGestureEventBridge *bridge,
56+ NSInteger interfaceOrientation,
57+ NSInteger targetProcessID
58+ );
4959static id RunnerSwipePointerPath (
5060 const RunnerGestureEventBridge *bridge,
5161 CGPoint start,
@@ -87,6 +97,8 @@ static id RunnerSampledDragPointerPath(
8797static const NSTimeInterval RunnerSwipeMovementDurationSeconds = 0.1 ;
8898static const double RunnerDragSampleIntervalMs = 16.0 ;
8999static const NSInteger RunnerControlledScrollMaxFrameCount = 30 ;
100+ // Set once the empty preparation record has synthesized in this process.
101+ static BOOL RunnerSynthesizedInputPrepared = NO ;
90102static id RunnerTapPointerPath (
91103 const RunnerGestureEventBridge *bridge,
92104 CGPoint point
@@ -215,6 +227,10 @@ + (NSString * _Nullable)synthesizeGestureWithApplication:(id)application
215227 }
216228}
217229
230+ + (void )resetSynthesizedInputPreparation {
231+ RunnerSynthesizedInputPrepared = NO ;
232+ }
233+
218234+ (NSInteger )interfaceOrientationForApplication : (id )application {
219235 SEL selector = NSSelectorFromString (@" interfaceOrientation" );
220236 if (![application respondsToSelector: selector]) {
@@ -310,21 +326,6 @@ + (NSInteger)interfaceOrientationForApplication:(id)application {
310326 return nil ;
311327}
312328
313- static id RunnerAllocateGestureRecord (
314- const RunnerGestureEventBridge *bridge,
315- NSString *name,
316- NSInteger orientation,
317- NSInteger processID
318- ) {
319- id record = ((RunnerMsgSendInitRecord)objc_msgSend)(
320- [bridge->core.recordClass alloc ], bridge->initRecordSelector , name, orientation
321- );
322- if (record != nil ) {
323- ((RunnerMsgSendSetInteger)objc_msgSend)(record, bridge->core .setTargetProcessIDSelector , processID);
324- }
325- return record;
326- }
327-
328329static NSString * _Nullable RunnerCreateEventRecord (
329330 id application,
330331 NSString *recordName,
@@ -342,41 +343,58 @@ static id RunnerAllocateGestureRecord(
342343 return @" private XCTest event synthesis unavailable: could not resolve target process ID" ;
343344 }
344345
345- #if TARGET_OS_IOS
346- // Prepare at the shared record boundary, before any route constructs timed
347- // pointer paths. An empty record carries no contact (including no status-bar
348- // tap), and reuses the caller's resolved orientation/PID without AX or images.
349- static BOOL didPrepare = NO ;
350- @synchronized ([RunnerSynthesizedGesture class ]) {
351- if (!didPrepare) {
352- NSTimeInterval startedAt = NSProcessInfo .processInfo .systemUptime ;
353- NSString *preparationError = nil ;
354- @try {
355- id preparation = RunnerAllocateGestureRecord (
356- bridge, @" agent-device-input-preparation" , interfaceOrientation, targetProcessID
357- );
358- preparationError = preparation == nil
359- ? @" could not create preparation record"
360- : RunnerSynthesizeEventRecord (bridge, preparation);
361- didPrepare = preparationError == nil ;
362- } @catch (NSException *exception) {
363- preparationError = RunnerFormatXCTestException (exception, @" input preparation failed" );
364- }
365- NSLog (
366- @" AGENT_DEVICE_RUNNER_SYNTHESIZED_INPUT_WARMUP outcome=%@ elapsedMs=%.0f detail=%@ " ,
367- didPrepare ? @" performed" : @" unsupported" ,
368- (NSProcessInfo .processInfo .systemUptime - startedAt) * 1000 ,
369- preparationError ?: @" "
370- );
371- }
346+ if (!RunnerSynthesizedInputPrepared) {
347+ RunnerPrepareSynthesizedInput (bridge, interfaceOrientation, targetProcessID);
372348 }
373- #endif
374- // Failed preparation is best-effort and remains eligible on the next request.
375- // The requested record is always fresh and retains its original timing.
376- *record = RunnerAllocateGestureRecord (bridge, recordName, interfaceOrientation, targetProcessID);
349+ *record = RunnerAllocateEventRecord (bridge, recordName, interfaceOrientation, targetProcessID);
377350 return *record == nil ? @" private XCTest event synthesis failed: could not create event record" : nil ;
378351}
379352
353+ static id _Nullable RunnerAllocateEventRecord (
354+ const RunnerGestureEventBridge *bridge,
355+ NSString *recordName,
356+ NSInteger interfaceOrientation,
357+ NSInteger targetProcessID
358+ ) {
359+ id record = ((RunnerMsgSendInitRecord)objc_msgSend)(
360+ [bridge->core.recordClass alloc ],
361+ bridge->initRecordSelector ,
362+ recordName,
363+ interfaceOrientation
364+ );
365+ if (record != nil ) {
366+ ((RunnerMsgSendSetInteger)objc_msgSend)(record, bridge->core .setTargetProcessIDSelector , targetProcessID);
367+ }
368+ return record;
369+ }
370+
371+ // Pays XCTest's one-time synthesis setup with a record that carries no pointer path, so no
372+ // contact reaches the target, before the first timed record. A failure is logged and retried
373+ // on the next record; the requested record is created afterwards either way.
374+ static void RunnerPrepareSynthesizedInput (
375+ const RunnerGestureEventBridge *bridge,
376+ NSInteger interfaceOrientation,
377+ NSInteger targetProcessID
378+ ) {
379+ NSTimeInterval startedAt = NSProcessInfo .processInfo .systemUptime ;
380+ NSString *error = nil ;
381+ @try {
382+ id record = RunnerAllocateEventRecord (
383+ bridge, @" agent-device-input-preparation" , interfaceOrientation, targetProcessID
384+ );
385+ error = record == nil ? @" could not create event record" : RunnerSynthesizeEventRecord (bridge, record);
386+ } @catch (NSException *exception) {
387+ error = RunnerFormatXCTestException (exception, @" input preparation failed" );
388+ }
389+ RunnerSynthesizedInputPrepared = error == nil ;
390+ NSLog (
391+ @" AGENT_DEVICE_RUNNER_SYNTHESIZED_INPUT_PREPARATION outcome=%@ elapsedMs=%.0f detail=%@ " ,
392+ error == nil ? @" performed" : @" failed" ,
393+ (NSProcessInfo .processInfo .systemUptime - startedAt) * 1000 ,
394+ error ?: @" "
395+ );
396+ }
397+
380398static NSString * _Nullable RunnerSynthesizeEventRecord (
381399 const RunnerGestureEventBridge *bridge,
382400 id record
0 commit comments