blob: c3f898a294567e232d94ac66456d661eed384bdb [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2013, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#if !defined(__has_feature) || !__has_feature(objc_arc)
29#error "This file requires ARC support."
30#endif
31
32#import "RTCPeerConnectionSyncObserver.h"
33
34#import "RTCMediaStream.h"
35
36@implementation RTCPeerConnectionSyncObserver {
37 int _expectedErrors;
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000038 NSMutableArray* _expectedSignalingChanges;
39 NSMutableArray* _expectedAddStreamLabels;
40 NSMutableArray* _expectedRemoveStreamLabels;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000041 int _expectedICECandidates;
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000042 NSMutableArray* _receivedICECandidates;
43 NSMutableArray* _expectedICEConnectionChanges;
44 NSMutableArray* _expectedICEGatheringChanges;
tkchin@webrtc.orgff273322014-04-30 18:32:33 +000045 NSMutableArray* _expectedDataChannels;
46 NSMutableArray* _expectedStateChanges;
47 NSMutableArray* _expectedMessages;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048}
49
50- (id)init {
51 self = [super init];
52 if (self) {
53 _expectedSignalingChanges = [NSMutableArray array];
54 _expectedSignalingChanges = [NSMutableArray array];
55 _expectedAddStreamLabels = [NSMutableArray array];
56 _expectedRemoveStreamLabels = [NSMutableArray array];
57 _receivedICECandidates = [NSMutableArray array];
58 _expectedICEConnectionChanges = [NSMutableArray array];
59 _expectedICEGatheringChanges = [NSMutableArray array];
tkchin@webrtc.orgff273322014-04-30 18:32:33 +000060 _expectedDataChannels = [NSMutableArray array];
61 _expectedMessages = [NSMutableArray array];
62 _expectedStateChanges = [NSMutableArray array];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063 }
64 return self;
65}
66
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000067- (int)popFirstElementAsInt:(NSMutableArray*)array {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068 NSAssert([array count] > 0, @"Empty array");
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000069 NSNumber* boxedState = [array objectAtIndex:0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070 [array removeObjectAtIndex:0];
71 return [boxedState intValue];
72}
73
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000074- (NSString*)popFirstElementAsNSString:(NSMutableArray*)array {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075 NSAssert([array count] > 0, @"Empty expectation array");
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000076 NSString* string = [array objectAtIndex:0];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077 [array removeObjectAtIndex:0];
78 return string;
79}
80
81- (BOOL)areAllExpectationsSatisfied {
82 return _expectedICECandidates <= 0 && // See comment in gotICECandidate.
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000083 _expectedErrors == 0 && [_expectedSignalingChanges count] == 0 &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 [_expectedICEConnectionChanges count] == 0 &&
85 [_expectedICEGatheringChanges count] == 0 &&
86 [_expectedAddStreamLabels count] == 0 &&
tkchin@webrtc.orgff273322014-04-30 18:32:33 +000087 [_expectedRemoveStreamLabels count] == 0 &&
88 [_expectedDataChannels count] == 0 &&
89 [_expectedStateChanges count] == 0 &&
90 [_expectedMessages count] == 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091 // TODO(hughv): Test video state here too.
92}
93
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000094- (NSArray*)releaseReceivedICECandidates {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 NSArray* ret = _receivedICECandidates;
96 _receivedICECandidates = [NSMutableArray array];
97 return ret;
98}
99
100- (void)expectError {
101 ++_expectedErrors;
102}
103
104- (void)expectSignalingChange:(RTCSignalingState)state {
105 [_expectedSignalingChanges addObject:@((int)state)];
106}
107
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000108- (void)expectAddStream:(NSString*)label {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 [_expectedAddStreamLabels addObject:label];
110}
111
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000112- (void)expectRemoveStream:(NSString*)label {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000113 [_expectedRemoveStreamLabels addObject:label];
114}
115
116- (void)expectICECandidates:(int)count {
117 _expectedICECandidates += count;
118}
119
120- (void)expectICEConnectionChange:(RTCICEConnectionState)state {
121 [_expectedICEConnectionChanges addObject:@((int)state)];
122}
123
124- (void)expectICEGatheringChange:(RTCICEGatheringState)state {
125 [_expectedICEGatheringChanges addObject:@((int)state)];
126}
127
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000128- (void)expectDataChannel:(NSString*)label {
129 [_expectedDataChannels addObject:label];
130}
131
132- (void)expectStateChange:(RTCDataChannelState)state {
133 [_expectedStateChanges addObject:@(state)];
134}
135
136- (void)expectMessage:(NSData*)message isBinary:(BOOL)isBinary {
137 RTCDataBuffer* buffer = [[RTCDataBuffer alloc] initWithData:message
138 isBinary:isBinary];
139 [_expectedMessages addObject:buffer];
140}
141
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142- (void)waitForAllExpectationsToBeSatisfied {
143 // TODO (fischman): Revisit. Keeping in sync with the Java version, but
144 // polling is not optimal.
145 // https://code.google.com/p/libjingle/source/browse/trunk/talk/app/webrtc/javatests/src/org/webrtc/PeerConnectionTest.java?line=212#212
146 while (![self areAllExpectationsSatisfied]) {
147 [[NSRunLoop currentRunLoop]
148 runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
149 }
150}
151
152#pragma mark - RTCPeerConnectionDelegate methods
153
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000154- (void)peerConnectionOnError:(RTCPeerConnection*)peerConnection {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 NSLog(@"RTCPeerConnectionDelegate::onError");
156 NSAssert(--_expectedErrors >= 0, @"Unexpected error");
157}
158
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000159- (void)peerConnection:(RTCPeerConnection*)peerConnection
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 signalingStateChanged:(RTCSignalingState)stateChanged {
161 int expectedState = [self popFirstElementAsInt:_expectedSignalingChanges];
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000162 NSString* message =
163 [NSString stringWithFormat:@"RTCPeerConnectionDelegate::"
164 @"onSignalingStateChange [%d] expected[%d]",
165 stateChanged,
166 expectedState];
167 NSAssert(expectedState == (int)stateChanged, message);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168}
169
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000170- (void)peerConnection:(RTCPeerConnection*)peerConnection
171 addedStream:(RTCMediaStream*)stream {
172 NSString* expectedLabel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173 [self popFirstElementAsNSString:_expectedAddStreamLabels];
174 NSAssert([expectedLabel isEqual:stream.label], @"Stream not expected");
175}
176
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000177- (void)peerConnection:(RTCPeerConnection*)peerConnection
178 removedStream:(RTCMediaStream*)stream {
179 NSString* expectedLabel =
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180 [self popFirstElementAsNSString:_expectedRemoveStreamLabels];
181 NSAssert([expectedLabel isEqual:stream.label], @"Stream not expected");
182}
183
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000184- (void)peerConnectionOnRenegotiationNeeded:(RTCPeerConnection*)peerConnection {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185}
186
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000187- (void)peerConnection:(RTCPeerConnection*)peerConnection
188 gotICECandidate:(RTCICECandidate*)candidate {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 --_expectedICECandidates;
190 // We don't assert expectedICECandidates >= 0 because it's hard to know
191 // how many to expect, in general. We only use expectICECandidates to
192 // assert a minimal count.
193 [_receivedICECandidates addObject:candidate];
194}
195
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000196- (void)peerConnection:(RTCPeerConnection*)peerConnection
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000197 iceGatheringChanged:(RTCICEGatheringState)newState {
198 // It's fine to get a variable number of GATHERING messages before
199 // COMPLETE fires (depending on how long the test runs) so we don't assert
200 // any particular count.
201 if (newState == RTCICEGatheringGathering) {
202 return;
203 }
204 int expectedState = [self popFirstElementAsInt:_expectedICEGatheringChanges];
205 NSAssert(expectedState == (int)newState, @"Empty expectation array");
206}
207
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000208- (void)peerConnection:(RTCPeerConnection*)peerConnection
fischman@webrtc.org385a7222014-03-25 05:16:29 +0000209 iceConnectionChanged:(RTCICEConnectionState)newState {
fischman@webrtc.orga01daf02014-03-08 03:17:55 +0000210 // See TODO(fischman) in RTCPeerConnectionTest.mm about Completed.
211 if (newState == RTCICEConnectionCompleted)
212 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000213 int expectedState = [self popFirstElementAsInt:_expectedICEConnectionChanges];
214 NSAssert(expectedState == (int)newState, @"Empty expectation array");
215}
216
tkchin@webrtc.orgff273322014-04-30 18:32:33 +0000217- (void)peerConnection:(RTCPeerConnection*)peerConnection
218 didOpenDataChannel:(RTCDataChannel*)dataChannel {
219 NSString* expectedLabel =
220 [self popFirstElementAsNSString:_expectedDataChannels];
221 NSAssert([expectedLabel isEqual:dataChannel.label],
222 @"Data channel not expected");
223 self.dataChannel = dataChannel;
224 dataChannel.delegate = self;
225 NSAssert(kRTCDataChannelStateConnecting == dataChannel.state,
226 @"Unexpected state");
227}
228
229#pragma mark - RTCDataChannelDelegate
230
231- (void)channelDidChangeState:(RTCDataChannel*)channel {
232 NSAssert([_expectedStateChanges count] > 0,
233 @"Unexpected state change");
234 int expectedState = [self popFirstElementAsInt:_expectedStateChanges];
235 NSAssert(expectedState == channel.state, @"Channel state should match");
236}
237
238- (void)channel:(RTCDataChannel*)channel
239 didReceiveMessageWithBuffer:(RTCDataBuffer*)buffer {
240 NSAssert([_expectedMessages count] > 0,
241 @"Unexpected message received");
242 RTCDataBuffer* expectedBuffer = [_expectedMessages objectAtIndex:0];
243 NSAssert(expectedBuffer.isBinary == buffer.isBinary,
244 @"Buffer isBinary should match");
245 NSAssert([expectedBuffer.data isEqual:buffer.data],
246 @"Buffer data should match");
247 [_expectedMessages removeObjectAtIndex:0];
248}
249
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000250@end