Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: webrtc/examples/objc/AppRTCMobile/tests/ARDAppClient_xctest.mm

Issue 2697603002: Move iOS tests to XCTest from gtest. (Closed)
Patch Set: fix nits Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2014 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #import <Foundation/Foundation.h> 11 #import <Foundation/Foundation.h>
12 #import <OCMock/OCMock.h> 12 #import <OCMock/OCMock.h>
13 #import <QuartzCore/CoreAnimation.h> 13 #import <QuartzCore/CoreAnimation.h>
14 #import <XCTest/XCTest.h>
14 15
15 #include "webrtc/base/gunit.h"
16 #include "webrtc/base/ssladapter.h" 16 #include "webrtc/base/ssladapter.h"
17 17
18 #import "WebRTC/RTCMediaConstraints.h" 18 #import "WebRTC/RTCMediaConstraints.h"
19 #import "WebRTC/RTCPeerConnectionFactory.h" 19 #import "WebRTC/RTCPeerConnectionFactory.h"
20 #import "WebRTC/RTCSessionDescription.h"
21 20
22 #import "ARDAppClient+Internal.h" 21 #import "ARDAppClient+Internal.h"
23 #import "ARDJoinResponse+Internal.h" 22 #import "ARDJoinResponse+Internal.h"
24 #import "ARDMessageResponse+Internal.h" 23 #import "ARDMessageResponse+Internal.h"
25 #import "ARDSDPUtils.h" 24 #import "ARDSDPUtils.h"
26 25
27 static NSString *kARDAppClientTestsDomain = @"org.webrtc.ARDAppClientTests"; 26 @interface ARDAppClientTest : XCTestCase
28 static NSInteger kARDAppClientTestsExpectationTimeoutError = 100;
29
30 // These classes mimic XCTest APIs, to make eventual conversion to XCTest
31 // easier. Conversion will happen once XCTest is supported well on build bots.
32 @interface ARDTestExpectation : NSObject
33
34 @property(nonatomic, readonly) NSString *description;
35 @property(nonatomic, readonly) BOOL isFulfilled;
36
37 - (instancetype)initWithDescription:(NSString *)description;
38 - (void)fulfill;
39
40 @end
41
42 @implementation ARDTestExpectation
43
44 @synthesize description = _description;
45 @synthesize isFulfilled = _isFulfilled;
46
47 - (instancetype)initWithDescription:(NSString *)description {
48 if (self = [super init]) {
49 _description = description;
50 }
51 return self;
52 }
53
54 - (void)fulfill {
55 _isFulfilled = YES;
56 }
57
58 @end
59
60 @interface ARDTestCase : NSObject
61
62 - (ARDTestExpectation *)expectationWithDescription:(NSString *)description;
63 - (void)waitForExpectationsWithTimeout:(NSTimeInterval)timeout
64 handler:(void (^)(NSError *error))handler;
65
66 @end
67
68 @implementation ARDTestCase {
69 NSMutableArray *_expectations;
70 }
71
72 - (instancetype)init {
73 if (self = [super init]) {
74 _expectations = [NSMutableArray array];
75 }
76 return self;
77 }
78
79 - (ARDTestExpectation *)expectationWithDescription:(NSString *)description {
80 ARDTestExpectation *expectation =
81 [[ARDTestExpectation alloc] initWithDescription:description];
82 [_expectations addObject:expectation];
83 return expectation;
84 }
85
86 - (void)waitForExpectationsWithTimeout:(NSTimeInterval)timeout
87 handler:(void (^)(NSError *error))handler {
88 CFTimeInterval startTime = CACurrentMediaTime();
89 NSError *error = nil;
90 while (![self areExpectationsFulfilled]) {
91 CFTimeInterval duration = CACurrentMediaTime() - startTime;
92 if (duration > timeout) {
93 error = [NSError errorWithDomain:kARDAppClientTestsDomain
94 code:kARDAppClientTestsExpectationTimeoutError
95 userInfo:@{}];
96 break;
97 }
98 [[NSRunLoop currentRunLoop]
99 runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
100 }
101 handler(error);
102 }
103
104 - (BOOL)areExpectationsFulfilled {
105 for (ARDTestExpectation *expectation in _expectations) {
106 if (!expectation.isFulfilled) {
107 return NO;
108 }
109 }
110 return YES;
111 }
112
113 @end
114
115 @interface ARDAppClientTest : ARDTestCase
116 @end 27 @end
117 28
118 @implementation ARDAppClientTest 29 @implementation ARDAppClientTest
119 30
120 #pragma mark - Mock helpers 31 #pragma mark - Mock helpers
121 32
122 - (id)mockRoomServerClientForRoomId:(NSString *)roomId 33 - (id)mockRoomServerClientForRoomId:(NSString *)roomId
123 clientId:(NSString *)clientId 34 clientId:(NSString *)clientId
124 isInitiator:(BOOL)isInitiator 35 isInitiator:(BOOL)isInitiator
125 messages:(NSArray *)messages 36 messages:(NSArray *)messages
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 // Need block arguments here because we're setting up a callbacks before we 159 // Need block arguments here because we're setting up a callbacks before we
249 // create the clients. 160 // create the clients.
250 ARDAppClient *caller = nil; 161 ARDAppClient *caller = nil;
251 ARDAppClient *answerer = nil; 162 ARDAppClient *answerer = nil;
252 __block __weak ARDAppClient *weakCaller = nil; 163 __block __weak ARDAppClient *weakCaller = nil;
253 __block __weak ARDAppClient *weakAnswerer = nil; 164 __block __weak ARDAppClient *weakAnswerer = nil;
254 NSString *roomId = @"testRoom"; 165 NSString *roomId = @"testRoom";
255 NSString *callerId = @"testCallerId"; 166 NSString *callerId = @"testCallerId";
256 NSString *answererId = @"testAnswererId"; 167 NSString *answererId = @"testAnswererId";
257 168
258 ARDTestExpectation *callerConnectionExpectation = 169 XCTestExpectation *callerConnectionExpectation =
259 [self expectationWithDescription:@"Caller PC connected."]; 170 [self expectationWithDescription:@"Caller PC connected."];
260 ARDTestExpectation *answererConnectionExpectation = 171 XCTestExpectation *answererConnectionExpectation =
261 [self expectationWithDescription:@"Answerer PC connected."]; 172 [self expectationWithDescription:@"Answerer PC connected."];
262 173
263 caller = [self createAppClientForRoomId:roomId 174 caller = [self createAppClientForRoomId:roomId
264 clientId:callerId 175 clientId:callerId
265 isInitiator:YES 176 isInitiator:YES
266 messages:[NSArray array] 177 messages:[NSArray array]
267 messageHandler:^(ARDSignalingMessage *message) { 178 messageHandler:^(ARDSignalingMessage *message) {
268 ARDAppClient *strongAnswerer = weakAnswerer; 179 ARDAppClient *strongAnswerer = weakAnswerer;
269 [strongAnswerer channel:strongAnswerer.channel didReceiveMessage:message]; 180 [strongAnswerer channel:strongAnswerer.channel didReceiveMessage:message];
270 } connectedHandler:^{ 181 } connectedHandler:^{
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 isAudioOnly:NO 213 isAudioOnly:NO
303 shouldMakeAecDump:NO 214 shouldMakeAecDump:NO
304 shouldUseLevelControl:NO]; 215 shouldUseLevelControl:NO];
305 [answerer connectToRoomWithId:roomId 216 [answerer connectToRoomWithId:roomId
306 isLoopback:NO 217 isLoopback:NO
307 isAudioOnly:NO 218 isAudioOnly:NO
308 shouldMakeAecDump:NO 219 shouldMakeAecDump:NO
309 shouldUseLevelControl:NO]; 220 shouldUseLevelControl:NO];
310 [self waitForExpectationsWithTimeout:20 handler:^(NSError *error) { 221 [self waitForExpectationsWithTimeout:20 handler:^(NSError *error) {
311 if (error) { 222 if (error) {
312 EXPECT_TRUE(0); 223 XCTFail(@"Expectation failed with error %@.", error);
313 } 224 }
314 }]; 225 }];
315 } 226 }
316 227
317 // Test to see that we get a local video connection 228 // Test to see that we get a local video connection
318 // Note this will currently pass even when no camera is connected as a local 229 // Note this will currently pass even when no camera is connected as a local
319 // video track is created regardless (Perhaps there should be a test for that... ) 230 // video track is created regardless (Perhaps there should be a test for that... )
231 #if !TARGET_IPHONE_SIMULATOR // Expect to fail on simulator due to no camera sup port
320 - (void)testSessionShouldGetLocalVideoTrackCallback { 232 - (void)testSessionShouldGetLocalVideoTrackCallback {
321 ARDAppClient *caller = nil; 233 ARDAppClient *caller = nil;
322 NSString *roomId = @"testRoom"; 234 NSString *roomId = @"testRoom";
323 NSString *callerId = @"testCallerId"; 235 NSString *callerId = @"testCallerId";
324 236
325 ARDTestExpectation *localVideoTrackExpectation = 237 XCTestExpectation *localVideoTrackExpectation =
326 [self expectationWithDescription:@"Caller got local video."]; 238 [self expectationWithDescription:@"Caller got local video."];
327 239
328 caller = [self createAppClientForRoomId:roomId 240 caller = [self createAppClientForRoomId:roomId
329 clientId:callerId 241 clientId:callerId
330 isInitiator:YES 242 isInitiator:YES
331 messages:[NSArray array] 243 messages:[NSArray array]
332 messageHandler:^(ARDSignalingMessage *message) { 244 messageHandler:^(ARDSignalingMessage *message) {}
333 } connectedHandler:^{ 245 connectedHandler:^{}
334 } localVideoTrackHandler:^{ 246 localVideoTrackHandler:^{ [localVideoTrackExpectation fulfill ]; }];
335 [localVideoTrackExpectation fulfill];
336 }];
337 caller.defaultPeerConnectionConstraints = 247 caller.defaultPeerConnectionConstraints =
338 [[RTCMediaConstraints alloc] initWithMandatoryConstraints:nil 248 [[RTCMediaConstraints alloc] initWithMandatoryConstraints:nil
339 optionalConstraints:nil]; 249 optionalConstraints:nil];
340 250
341 // Kick off connection. 251 // Kick off connection.
342 [caller connectToRoomWithId:roomId 252 [caller connectToRoomWithId:roomId
343 isLoopback:NO 253 isLoopback:NO
344 isAudioOnly:NO 254 isAudioOnly:NO
345 shouldMakeAecDump:NO 255 shouldMakeAecDump:NO
346 shouldUseLevelControl:NO]; 256 shouldUseLevelControl:NO];
347 [self waitForExpectationsWithTimeout:20 handler:^(NSError *error) { 257 [self waitForExpectationsWithTimeout:20 handler:^(NSError *error) {
348 if (error) { 258 if (error) {
349 EXPECT_TRUE(0); 259 XCTFail("Expectation timed out with error: %@.", error);
350 } 260 }
351 }]; 261 }];
352 } 262 }
263 #endif
353 264
354 @end 265 @end
355
356 @interface ARDSDPUtilsTest : ARDTestCase
357 - (void)testPreferVideoCodec:(NSString *)codec
358 sdp:(NSString *)sdp
359 expectedSdp:(NSString *)expectedSdp;
360 @end
361
362 @implementation ARDSDPUtilsTest
363
364 - (void)testPreferVideoCodec:(NSString *)codec
365 sdp:(NSString *)sdp
366 expectedSdp:(NSString *)expectedSdp {
367 RTCSessionDescription* desc =
368 [[RTCSessionDescription alloc] initWithType:RTCSdpTypeOffer sdp:sdp];
369 RTCSessionDescription *outputDesc =
370 [ARDSDPUtils descriptionForDescription:desc
371 preferredVideoCodec:codec];
372 EXPECT_TRUE([outputDesc.description rangeOfString:expectedSdp].location !=
373 NSNotFound);
374 }
375
376 @end
377
378 class SignalingTest : public ::testing::Test {
379 protected:
380 static void SetUpTestCase() {
381 rtc::InitializeSSL();
382 }
383 static void TearDownTestCase() {
384 rtc::CleanupSSL();
385 }
386 };
387
388 TEST_F(SignalingTest, SessionTest) {
389 @autoreleasepool {
390 ARDAppClientTest *test = [[ARDAppClientTest alloc] init];
391 [test testSession];
392 }
393 }
394
395 #if !TARGET_IPHONE_SIMULATOR
396 // Expected fail on iOS Simulator due to no camera support
397 TEST_F(SignalingTest, SessionLocalVideoCallbackTest) {
398 @autoreleasepool {
399 ARDAppClientTest *test = [[ARDAppClientTest alloc] init];
400 [test testSessionShouldGetLocalVideoTrackCallback];
401 }
402 }
403 #endif
404
405 TEST_F(SignalingTest, SdpH264Test) {
406 @autoreleasepool {
407 ARDSDPUtilsTest *test = [[ARDSDPUtilsTest alloc] init];
408 NSString *sdp = @("m=video 9 RTP/SAVPF 100 116 117 96 120 97\n"
409 "a=rtpmap:120 H264/90000\n"
410 "a=rtpmap:97 H264/90000\n");
411 NSString *expectedSdp = @("m=video 9 RTP/SAVPF 120 97 100 116 117 96\n"
412 "a=rtpmap:120 H264/90000\n"
413 "a=rtpmap:97 H264/90000\n");
414 [test testPreferVideoCodec:@"H264"
415 sdp:sdp
416 expectedSdp:expectedSdp];
417 }
418 }
419
420 TEST_F(SignalingTest, SdpVp8Test) {
421 @autoreleasepool {
422 ARDSDPUtilsTest *test = [[ARDSDPUtilsTest alloc] init];
423 NSString *sdp = @("m=video 9 RTP/SAVPF 100 116 117 96 120 97\n"
424 "a=rtpmap:116 VP8/90000\n");
425 NSString *expectedSdp = @("m=video 9 RTP/SAVPF 116 100 117 96 120 97\n"
426 "a=rtpmap:116 VP8/90000\n");
427 [test testPreferVideoCodec:@"VP8"
428 sdp:sdp
429 expectedSdp:expectedSdp];
430 }
431 }
432
433 TEST_F(SignalingTest, SdpNoMLineTest) {
434 @autoreleasepool {
435 ARDSDPUtilsTest *test = [[ARDSDPUtilsTest alloc] init];
436 NSString *sdp = @("a=rtpmap:116 VP8/90000\n");
437 [test testPreferVideoCodec:@"VP8"
438 sdp:sdp
439 expectedSdp:sdp];
440 }
441 }
442
443 TEST_F(SignalingTest, SdpMissingCodecTest) {
444 @autoreleasepool {
445 ARDSDPUtilsTest *test = [[ARDSDPUtilsTest alloc] init];
446 NSString *sdp = @("m=video 9 RTP/SAVPF 100 116 117 96 120 97\n"
447 "a=rtpmap:116 VP8/90000\n");
448 [test testPreferVideoCodec:@"foo"
449 sdp:sdp
450 expectedSdp:sdp];
451 }
452 }
453
454 int main(int argc, char **argv) {
455 ::testing::InitGoogleTest(&argc, argv);
456 return RUN_ALL_TESTS();
457 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698