1313
1414static NSString * _Nullable RunnerRequireTextClass (Class cls, NSString *name);
1515static NSString * _Nullable RunnerRequireTextSelector (Class cls, SEL selector, NSString *name);
16- static NSString * _Nullable RunnerSynthesizeTextWithMode (
16+ static RunnerSynthesizedTextEntryResult *RunnerTextEntryResult (
17+ RunnerSynthesizedTextEntryStatus status,
18+ NSString * _Nullable message
19+ );
20+ static RunnerSynthesizedTextEntryResult *RunnerSynthesizeTextWithMode (
1721 id application,
1822 NSString *text,
1923 BOOL replace
2024);
2125
26+ @interface RunnerSynthesizedTextEntryResult ()
27+
28+ @property (nonatomic , readwrite ) RunnerSynthesizedTextEntryStatus status;
29+ @property (nonatomic , readwrite , nullable ) NSString *message;
30+
31+ @end
32+
33+
34+ @implementation RunnerSynthesizedTextEntryResult
35+ @end
36+
2237@implementation RunnerSynthesizedTextEntry
2338
24- + (NSString * _Nullable )synthesizeTextWithApplication : (id )application
25- text : (NSString *)text {
39+ + (RunnerSynthesizedTextEntryResult * )synthesizeTextWithApplication : (id )application
40+ text : (NSString *)text {
2641 return RunnerSynthesizeTextWithMode (application, text, NO );
2742}
2843
29- + (NSString * _Nullable )replaceTextWithApplication : (id )application
30- text : (NSString *)text {
44+ + (RunnerSynthesizedTextEntryResult * )replaceTextWithApplication : (id )application
45+ text : (NSString *)text {
3146 return RunnerSynthesizeTextWithMode (application, text, YES );
3247}
3348
3449@end
3550
36- static NSString * _Nullable RunnerSynthesizeTextWithMode (
51+ static RunnerSynthesizedTextEntryResult * RunnerSynthesizeTextWithMode (
3752 id application,
3853 NSString *text,
3954 BOOL replace
@@ -51,33 +66,39 @@ + (NSString * _Nullable)replaceTextWithApplication:(id)application
5166 SEL processID = NSSelectorFromString (@" processID" );
5267
5368 NSString *missing = RunnerRequireTextClass (recordClass, @" XCSynthesizedEventRecord" );
54- if (missing != nil ) return missing;
69+ if (missing != nil ) return RunnerTextEntryResult (RunnerSynthesizedTextEntryStatusUnavailable, missing) ;
5570 missing = RunnerRequireTextClass (pathClass, @" XCPointerEventPath" );
56- if (missing != nil ) return missing;
71+ if (missing != nil ) return RunnerTextEntryResult (RunnerSynthesizedTextEntryStatusUnavailable, missing) ;
5772 missing = RunnerRequireTextSelector (recordClass, initRecord, @" initWithName:" );
58- if (missing != nil ) return missing;
73+ if (missing != nil ) return RunnerTextEntryResult (RunnerSynthesizedTextEntryStatusUnavailable, missing) ;
5974 missing = RunnerRequireTextSelector (recordClass, addPath, @" addPointerEventPath:" );
60- if (missing != nil ) return missing;
75+ if (missing != nil ) return RunnerTextEntryResult (RunnerSynthesizedTextEntryStatusUnavailable, missing) ;
6176 missing = RunnerRequireTextSelector (recordClass, setTargetProcessID, @" setTargetProcessID:" );
62- if (missing != nil ) return missing;
77+ if (missing != nil ) return RunnerTextEntryResult (RunnerSynthesizedTextEntryStatusUnavailable, missing) ;
6378 missing = RunnerRequireTextSelector (recordClass, synthesize, @" synthesizeWithError:" );
64- if (missing != nil ) return missing;
79+ if (missing != nil ) return RunnerTextEntryResult (RunnerSynthesizedTextEntryStatusUnavailable, missing) ;
6580 missing = RunnerRequireTextSelector (pathClass, initPath, @" initForTextInput" );
66- if (missing != nil ) return missing;
81+ if (missing != nil ) return RunnerTextEntryResult (RunnerSynthesizedTextEntryStatusUnavailable, missing) ;
6782 missing = RunnerRequireTextSelector (pathClass, typeText, @" typeText:atOffset:typingSpeed:shouldRedact:" );
68- if (missing != nil ) return missing;
83+ if (missing != nil ) return RunnerTextEntryResult (RunnerSynthesizedTextEntryStatusUnavailable, missing) ;
6984 if (replace) {
7085 missing = RunnerRequireTextSelector (pathClass, typeKey, @" typeKey:modifiers:atOffset:" );
71- if (missing != nil ) return missing;
86+ if (missing != nil ) return RunnerTextEntryResult (RunnerSynthesizedTextEntryStatusUnavailable, missing) ;
7287 }
7388 if (![application respondsToSelector: processID]) {
74- return @" private XCTest text synthesis unavailable: XCUIApplication missing processID" ;
89+ return RunnerTextEntryResult (
90+ RunnerSynthesizedTextEntryStatusUnavailable,
91+ @" private XCTest text synthesis unavailable: XCUIApplication missing processID"
92+ );
7593 }
7694
7795 NSInteger targetProcessID =
7896 ((RunnerTextMsgSendInteger)objc_msgSend)(application, processID);
7997 if (targetProcessID <= 0 ) {
80- return @" private XCTest text synthesis unavailable: could not resolve target process ID" ;
98+ return RunnerTextEntryResult (
99+ RunnerSynthesizedTextEntryStatusUnavailable,
100+ @" private XCTest text synthesis unavailable: could not resolve target process ID"
101+ );
81102 }
82103
83104 if (replace) {
@@ -86,7 +107,10 @@ + (NSString * _Nullable)replaceTextWithApplication:(id)application
86107 );
87108 id selectionPath = ((RunnerTextMsgSendInitPath)objc_msgSend)([pathClass alloc ], initPath);
88109 if (selectionRecord == nil || selectionPath == nil ) {
89- return @" private XCTest text synthesis failed: could not create the selection event" ;
110+ return RunnerTextEntryResult (
111+ RunnerSynthesizedTextEntryStatusFailed,
112+ @" private XCTest text synthesis failed: could not create the selection event"
113+ );
90114 }
91115 ((RunnerTextMsgSendSetInteger)objc_msgSend)(
92116 selectionRecord, setTargetProcessID, targetProcessID
@@ -106,10 +130,10 @@ + (NSString * _Nullable)replaceTextWithApplication:(id)application
106130 );
107131 if (!selected) {
108132 NSString *detail = selectionError.localizedDescription ?: @" synthesizeWithError returned false" ;
109- return [ NSString stringWithFormat:
110- @" private XCTest text selection failed: %@ " ,
111- detail
112- ] ;
133+ return RunnerTextEntryResult (
134+ RunnerSynthesizedTextEntryStatusFailed ,
135+ [ NSString stringWithFormat: @" private XCTest text selection failed: %@ " , detail]
136+ ) ;
113137 }
114138 }
115139
@@ -118,7 +142,10 @@ + (NSString * _Nullable)replaceTextWithApplication:(id)application
118142 );
119143 id path = ((RunnerTextMsgSendInitPath)objc_msgSend)([pathClass alloc ], initPath);
120144 if (record == nil || path == nil ) {
121- return @" private XCTest text synthesis failed: could not create the text event" ;
145+ return RunnerTextEntryResult (
146+ RunnerSynthesizedTextEntryStatusFailed,
147+ @" private XCTest text synthesis failed: could not create the text event"
148+ );
122149 }
123150 ((RunnerTextMsgSendSetInteger)objc_msgSend)(record, setTargetProcessID, targetProcessID);
124151 ((RunnerTextMsgSendType)objc_msgSend)(path, typeText, text, 0.0 , 60 , YES );
@@ -128,16 +155,32 @@ + (NSString * _Nullable)replaceTextWithApplication:(id)application
128155 BOOL ok = ((RunnerTextMsgSendSynthesize)objc_msgSend)(record, synthesize, &error);
129156 if (!ok) {
130157 NSString *detail = error.localizedDescription ?: @" synthesizeWithError returned false" ;
131- return [NSString stringWithFormat: @" private XCTest text synthesis failed: %@ " , detail];
158+ return RunnerTextEntryResult (
159+ RunnerSynthesizedTextEntryStatusFailed,
160+ [NSString stringWithFormat: @" private XCTest text synthesis failed: %@ " , detail]
161+ );
132162 }
133- return nil ;
163+ return RunnerTextEntryResult (RunnerSynthesizedTextEntryStatusSucceeded, nil ) ;
134164 } @catch (NSException *exception) {
135165 NSString *name = exception.name ?: @" NSException" ;
136166 NSString *reason = exception.reason ?: @" private XCTest text synthesis failed" ;
137- return [NSString stringWithFormat: @" %@ : %@ " , name, reason];
167+ return RunnerTextEntryResult (
168+ RunnerSynthesizedTextEntryStatusFailed,
169+ [NSString stringWithFormat: @" %@ : %@ " , name, reason]
170+ );
138171 }
139172}
140173
174+ static RunnerSynthesizedTextEntryResult *RunnerTextEntryResult (
175+ RunnerSynthesizedTextEntryStatus status,
176+ NSString * _Nullable message
177+ ) {
178+ RunnerSynthesizedTextEntryResult *result = [RunnerSynthesizedTextEntryResult new ];
179+ result.status = status;
180+ result.message = message;
181+ return result;
182+ }
183+
141184static NSString * _Nullable RunnerRequireTextClass (Class cls, NSString *name) {
142185 if (cls == Nil ) {
143186 return [NSString stringWithFormat: @" private XCTest text synthesis unavailable: missing %@ " , name];
0 commit comments