appium-webdriveragent 16.18.3 → 16.18.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,357 @@
1
+ /**
2
+ * Copyright (c) 2015-present, Facebook, Inc.
3
+ * All rights reserved.
4
+ *
5
+ * This source code is licensed under the BSD-style license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ #import <XCTest/XCTest.h>
10
+
11
+ #import "FBConfiguration.h"
12
+ #import "FBMjpegServer.h"
13
+ #import "FBTCPSocket.h"
14
+
15
+ typedef void (^FBFrameCompletion)(NSData * _Nullable, NSError * _Nullable);
16
+
17
+ // Redeclared here, the implementations live in FBMjpegServer.m.
18
+ @interface FBMjpegServer (Testing)
19
+ @property (atomic, copy) BOOL (^frameRequest)(FBFrameCompletion completion, NSError **error);
20
+ @property (atomic, assign) NSTimeInterval frameTimeout;
21
+ @property (atomic, assign) NSTimeInterval lateFrameLimit;
22
+ @property (atomic, assign) NSTimeInterval failureBackoffMin;
23
+ - (void)sendScreenshot:(NSData *)screenshotData forFrame:(id)frame;
24
+ @end
25
+
26
+ @interface NSObject (FBMjpegPendingFrameTesting)
27
+ - (instancetype)initWithEpoch:(NSUInteger)epoch displayId:(nullable NSNumber *)displayId;
28
+ @end
29
+
30
+ // Counts the frames that reach a client instead of writing them anywhere.
31
+ @interface FBMjpegRecordingSocket : FBTCPSocket
32
+ @property (atomic, assign) NSUInteger framesCount;
33
+ @end
34
+
35
+ @implementation FBMjpegRecordingSocket
36
+
37
+ - (void)writeData:(NSData *)data toClient:(nw_connection_t)client
38
+ {
39
+ }
40
+
41
+ - (void)writeData:(NSData *)data toClient:(nw_connection_t)client completion:(void (^)(BOOL))completion
42
+ {
43
+ self.framesCount++;
44
+ if (nil != completion) {
45
+ completion(YES);
46
+ }
47
+ }
48
+
49
+ @end
50
+
51
+
52
+ @interface FBMjpegServerTests : XCTestCase
53
+ @property (nonatomic, strong) FBMjpegServer *server;
54
+ @property (nonatomic, strong) FBMjpegRecordingSocket *socket;
55
+ @property (nonatomic, strong) NSMutableArray<FBFrameCompletion> *requests;
56
+ @property (nonatomic, strong) NSLock *requestsLock;
57
+ @property (nonatomic, strong) NSData *frameData;
58
+ @property (nonatomic, strong, nullable) NSError *requestError;
59
+ @property (nonatomic, strong) NSMutableArray<id> *clients;
60
+ @end
61
+
62
+ @implementation FBMjpegServerTests
63
+
64
+ - (void)setUp
65
+ {
66
+ [super setUp];
67
+ self.requests = [NSMutableArray array];
68
+ self.requestsLock = [[NSLock alloc] init];
69
+ self.clients = [NSMutableArray array];
70
+ self.requestError = nil;
71
+ UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:CGSizeMake(4, 4)];
72
+ self.frameData = [renderer JPEGDataWithCompressionQuality:0.9 actions:^(UIGraphicsImageRendererContext *ctx) {
73
+ [UIColor.redColor setFill];
74
+ [ctx fillRect:CGRectMake(0, 0, 4, 4)];
75
+ }];
76
+
77
+ self.socket = [[FBMjpegRecordingSocket alloc] initWithPort:0];
78
+ self.server = [[FBMjpegServer alloc] init];
79
+ self.server.socket = self.socket;
80
+ self.server.frameTimeout = 0.05;
81
+ self.server.lateFrameLimit = 5;
82
+ // The first failure waits 2 s, which is much more than any check below waits for.
83
+ self.server.failureBackoffMin = 1.0;
84
+ __weak typeof(self) weakSelf = self;
85
+ self.server.frameRequest = ^BOOL(FBFrameCompletion completion, NSError **error) {
86
+ typeof(self) strongSelf = weakSelf;
87
+ if (nil == strongSelf) {
88
+ return NO;
89
+ }
90
+ if (nil != strongSelf.requestError) {
91
+ *error = strongSelf.requestError;
92
+ [strongSelf recordRequest:nil];
93
+ return NO;
94
+ }
95
+ [strongSelf recordRequest:completion];
96
+ return YES;
97
+ };
98
+ }
99
+
100
+ - (void)tearDown
101
+ {
102
+ FBConfiguration.sharedInstance.currentDisplayId = nil;
103
+ [self.server stopStreaming];
104
+ [super tearDown];
105
+ }
106
+
107
+ #pragma mark - Helpers
108
+
109
+ - (void)recordRequest:(nullable FBFrameCompletion)completion
110
+ {
111
+ [self.requestsLock lock];
112
+ [self.requests addObject:completion ?: ^(NSData *data, NSError *error) {}];
113
+ [self.requestsLock unlock];
114
+ }
115
+
116
+ - (NSUInteger)requestsCount
117
+ {
118
+ [self.requestsLock lock];
119
+ NSUInteger count = self.requests.count;
120
+ [self.requestsLock unlock];
121
+ return count;
122
+ }
123
+
124
+ - (FBFrameCompletion)requestAtIndex:(NSUInteger)index
125
+ {
126
+ [self.requestsLock lock];
127
+ FBFrameCompletion request = self.requests[index];
128
+ [self.requestsLock unlock];
129
+ return request;
130
+ }
131
+
132
+ - (BOOL)waitUntil:(BOOL (^)(void))condition timeout:(NSTimeInterval)timeout
133
+ {
134
+ NSDate *deadline = [NSDate dateWithTimeIntervalSinceNow:timeout];
135
+ while (!condition()) {
136
+ if ([deadline timeIntervalSinceNow] <= 0) {
137
+ return NO;
138
+ }
139
+ [NSThread sleepForTimeInterval:0.01];
140
+ }
141
+ return YES;
142
+ }
143
+
144
+ - (void)waitFor:(NSTimeInterval)seconds
145
+ {
146
+ [NSThread sleepForTimeInterval:seconds];
147
+ }
148
+
149
+ - (nw_connection_t)connectClient
150
+ {
151
+ nw_endpoint_t endpoint = nw_endpoint_create_host("127.0.0.1", "9");
152
+ nw_parameters_t parameters = nw_parameters_create_secure_tcp(NW_PARAMETERS_DISABLE_PROTOCOL,
153
+ NW_PARAMETERS_DEFAULT_CONFIGURATION);
154
+ // Never started, so nothing is actually connected.
155
+ nw_connection_t client = nw_connection_create(endpoint, parameters);
156
+ [self.clients addObject:client];
157
+ [self.server client:client didReceiveData:[NSData data]];
158
+ return client;
159
+ }
160
+
161
+ #pragma mark - Tests
162
+
163
+ - (void)testDoesNotRequestFramesWithoutClients
164
+ {
165
+ [self waitFor:0.3];
166
+ XCTAssertEqual(0, self.requestsCount);
167
+ }
168
+
169
+ - (void)testSendsAnsweredFrameAndRequestsNext
170
+ {
171
+ [self connectClient];
172
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
173
+ [self requestAtIndex:0](self.frameData, nil);
174
+
175
+ XCTAssertTrue([self waitUntil:^BOOL { return self.socket.framesCount >= 1; } timeout:5]);
176
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 2; } timeout:2],
177
+ @"The next frame must be requested after an answer within the timeout");
178
+ }
179
+
180
+ - (void)testLateFrameIsSentAfterTimeoutWithoutBackoff
181
+ {
182
+ [self connectClient];
183
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
184
+ // Several frame timeouts pass with no answer.
185
+ [self waitFor:0.4];
186
+ XCTAssertEqual(0, self.socket.framesCount);
187
+
188
+ [self requestAtIndex:0](self.frameData, nil);
189
+ XCTAssertTrue([self waitUntil:^BOOL { return self.socket.framesCount >= 1; } timeout:1],
190
+ @"The late answer must go out as a frame");
191
+ // A backoff would keep the next request away for 2 s.
192
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 2; } timeout:1],
193
+ @"The loop must go on at once after the late frame");
194
+ }
195
+
196
+ - (void)testSendsNoOtherRequestWhileFrameIsUnanswered
197
+ {
198
+ [self connectClient];
199
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
200
+ [self waitFor:0.5];
201
+ XCTAssertEqual(1, self.requestsCount);
202
+ XCTAssertEqual(0, self.socket.framesCount);
203
+ }
204
+
205
+ - (void)testGivesUpOnFrameAfterLimitAndBacksOff
206
+ {
207
+ self.server.lateFrameLimit = 0.2;
208
+ [self connectClient];
209
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
210
+ [self waitFor:1.0];
211
+ XCTAssertEqual(1, self.requestsCount, @"The failed frame must back off before the next request");
212
+
213
+ // The abandoned request is not sent even if it is answered later.
214
+ [self requestAtIndex:0](self.frameData, nil);
215
+ [self waitFor:0.2];
216
+ XCTAssertEqual(0, self.socket.framesCount);
217
+ }
218
+
219
+ - (void)testErrorAnswerBacksOff
220
+ {
221
+ [self connectClient];
222
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
223
+ NSError *error = [NSError errorWithDomain:@"test" code:1 userInfo:nil];
224
+ [self requestAtIndex:0](nil, error);
225
+ [self waitFor:1.0];
226
+ XCTAssertEqual(1, self.requestsCount);
227
+ XCTAssertEqual(0, self.socket.framesCount);
228
+ }
229
+
230
+ - (void)testFailedRequestBacksOff
231
+ {
232
+ self.requestError = [NSError errorWithDomain:@"test" code:2 userInfo:nil];
233
+ [self connectClient];
234
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
235
+ [self waitFor:1.0];
236
+ XCTAssertEqual(1, self.requestsCount);
237
+ XCTAssertEqual(0, self.socket.framesCount);
238
+ }
239
+
240
+ - (void)testDropsPendingFrameWhenLastClientLeaves
241
+ {
242
+ nw_connection_t client = [self connectClient];
243
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
244
+ [self.server didClientDisconnect:client];
245
+ // Let the loop notice that nobody listens.
246
+ [self waitFor:0.4];
247
+
248
+ [self requestAtIndex:0](self.frameData, nil);
249
+ [self waitFor:0.2];
250
+ XCTAssertEqual(0, self.socket.framesCount, @"A frame for a vanished audience must not be kept");
251
+
252
+ [self connectClient];
253
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 2; } timeout:2],
254
+ @"A returning client gets a fresh request");
255
+ [self requestAtIndex:1](self.frameData, nil);
256
+ XCTAssertTrue([self waitUntil:^BOOL { return self.socket.framesCount >= 1; } timeout:5]);
257
+ }
258
+
259
+ - (void)testDoesNotReuseFrameRequestedForPreviousListener
260
+ {
261
+ nw_connection_t first = [self connectClient];
262
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
263
+ // Swap the clients at once, before the loop sees that nobody listens.
264
+ [self.server didClientDisconnect:first];
265
+ [self connectClient];
266
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 2; } timeout:2],
267
+ @"The new client must not wait for a frame requested for the previous one");
268
+
269
+ [self requestAtIndex:0](self.frameData, nil);
270
+ [self waitFor:0.2];
271
+ XCTAssertEqual(0, self.socket.framesCount);
272
+ [self requestAtIndex:1](self.frameData, nil);
273
+ XCTAssertTrue([self waitUntil:^BOOL { return self.socket.framesCount >= 1; } timeout:5]);
274
+ }
275
+
276
+ - (void)testDoesNotReuseFrameRequestedForPreviousDisplay
277
+ {
278
+ [self connectClient];
279
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
280
+ FBConfiguration.sharedInstance.currentDisplayId = @1;
281
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 2; } timeout:2],
282
+ @"A display switch must request the frame again");
283
+
284
+ [self requestAtIndex:0](self.frameData, nil);
285
+ [self waitFor:0.2];
286
+ XCTAssertEqual(0, self.socket.framesCount);
287
+ }
288
+
289
+ - (void)testDropsFrameAnsweredAfterListenerSwitchedWhileWaiting
290
+ {
291
+ nw_connection_t first = [self connectClient];
292
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
293
+ [self waitFor:0.2];
294
+ // The frame is late now, so the next wait is a long one.
295
+ self.server.frameTimeout = 2;
296
+ [self waitFor:0.2];
297
+ [self.server didClientDisconnect:first];
298
+ [self connectClient];
299
+
300
+ [self requestAtIndex:0](self.frameData, nil);
301
+ [self waitFor:0.3];
302
+ XCTAssertEqual(0, self.socket.framesCount, @"The answer was requested for the previous listener");
303
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 2; } timeout:2],
304
+ @"Dropping the stale answer is not a failure, no backoff");
305
+ }
306
+
307
+ - (void)testDropsFrameAnsweredAfterDisplaySwitchedWhileWaiting
308
+ {
309
+ [self connectClient];
310
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
311
+ [self waitFor:0.2];
312
+ self.server.frameTimeout = 2;
313
+ [self waitFor:0.2];
314
+ FBConfiguration.sharedInstance.currentDisplayId = @1;
315
+
316
+ [self requestAtIndex:0](self.frameData, nil);
317
+ [self waitFor:0.3];
318
+ XCTAssertEqual(0, self.socket.framesCount, @"The answer was requested for the previous display");
319
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 2; } timeout:2]);
320
+ }
321
+
322
+ - (void)testDoesNotSendFrameThatWentStaleDuringScaling
323
+ {
324
+ [self connectClient];
325
+ Class frameClass = NSClassFromString(@"FBMjpegPendingFrame");
326
+ NSUInteger epoch = [[self.server valueForKey:@"listenersEpoch"] unsignedIntegerValue];
327
+
328
+ [self.server sendScreenshot:self.frameData forFrame:[[frameClass alloc] initWithEpoch:epoch + 1 displayId:nil]];
329
+ XCTAssertEqual(0, self.socket.framesCount, @"Other listeners");
330
+ [self.server sendScreenshot:self.frameData forFrame:[[frameClass alloc] initWithEpoch:epoch displayId:@7]];
331
+ XCTAssertEqual(0, self.socket.framesCount, @"Other display");
332
+ [self.server sendScreenshot:self.frameData forFrame:[[frameClass alloc] initWithEpoch:epoch displayId:nil]];
333
+ XCTAssertEqual(1, self.socket.framesCount);
334
+ }
335
+
336
+ - (void)testEmptyAnswerBacksOff
337
+ {
338
+ [self connectClient];
339
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
340
+ [self requestAtIndex:0](nil, nil);
341
+ [self waitFor:1.0];
342
+ XCTAssertEqual(1, self.requestsCount);
343
+ XCTAssertEqual(0, self.socket.framesCount);
344
+ }
345
+
346
+ - (void)testStopsRequestingAfterStopStreaming
347
+ {
348
+ [self connectClient];
349
+ XCTAssertTrue([self waitUntil:^BOOL { return self.requestsCount >= 1; } timeout:2]);
350
+ [self.server stopStreaming];
351
+ [self requestAtIndex:0](self.frameData, nil);
352
+ [self waitFor:0.3];
353
+ XCTAssertEqual(1, self.requestsCount);
354
+ XCTAssertEqual(0, self.socket.framesCount);
355
+ }
356
+
357
+ @end
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "appium-webdriveragent",
3
- "version": "16.18.3",
3
+ "version": "16.18.5",
4
4
  "description": "Package bundling WebDriverAgent",
5
5
  "keywords": [
6
6
  "Appium",
@@ -83,5 +83,5 @@
83
83
  "node": "^20.19.0 || ^22.12.0 || >=24.0.0",
84
84
  "npm": ">=10"
85
85
  },
86
- "gitHead": "20c3bde38f8413bd760187c521ace065a62538a6"
86
+ "gitHead": "83c02537ecaaee4964476ba35243e822cc378d00"
87
87
  }