3 * Copyright 2013, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
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.
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.
28 #if !defined(__has_feature) || !__has_feature(objc_arc)
29 #error "This file requires ARC support."
32 #import "RTCPeerConnectionSyncObserver.h"
34 #import "RTCMediaStream.h"
36 @implementation RTCPeerConnectionSyncObserver {
38 NSMutableArray* _expectedSignalingChanges;
39 NSMutableArray* _expectedAddStreamLabels;
40 NSMutableArray* _expectedRemoveStreamLabels;
41 int _expectedICECandidates;
42 NSMutableArray* _receivedICECandidates;
43 NSMutableArray* _expectedICEConnectionChanges;
44 NSMutableArray* _expectedICEGatheringChanges;
45 NSMutableArray* _expectedDataChannels;
46 NSMutableArray* _expectedStateChanges;
47 NSMutableArray* _expectedMessages;
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];
60 _expectedDataChannels = [NSMutableArray array];
61 _expectedMessages = [NSMutableArray array];
62 _expectedStateChanges = [NSMutableArray array];
67 - (int)popFirstElementAsInt:(NSMutableArray*)array {
68 NSAssert([array count] > 0, @"Empty array");
69 NSNumber* boxedState = [array objectAtIndex:0];
70 [array removeObjectAtIndex:0];
71 return [boxedState intValue];
74 - (NSString*)popFirstElementAsNSString:(NSMutableArray*)array {
75 NSAssert([array count] > 0, @"Empty expectation array");
76 NSString* string = [array objectAtIndex:0];
77 [array removeObjectAtIndex:0];
81 - (BOOL)areAllExpectationsSatisfied {
82 return _expectedICECandidates <= 0 && // See comment in gotICECandidate.
83 _expectedErrors == 0 && [_expectedSignalingChanges count] == 0 &&
84 [_expectedICEConnectionChanges count] == 0 &&
85 [_expectedICEGatheringChanges count] == 0 &&
86 [_expectedAddStreamLabels count] == 0 &&
87 [_expectedRemoveStreamLabels count] == 0 &&
88 [_expectedDataChannels count] == 0 &&
89 [_expectedStateChanges count] == 0 &&
90 [_expectedMessages count] == 0;
91 // TODO(hughv): Test video state here too.
94 - (NSArray*)releaseReceivedICECandidates {
95 NSArray* ret = _receivedICECandidates;
96 _receivedICECandidates = [NSMutableArray array];
100 - (void)expectError {
104 - (void)expectSignalingChange:(RTCSignalingState)state {
105 [_expectedSignalingChanges addObject:@((int)state)];
108 - (void)expectAddStream:(NSString*)label {
109 [_expectedAddStreamLabels addObject:label];
112 - (void)expectRemoveStream:(NSString*)label {
113 [_expectedRemoveStreamLabels addObject:label];
116 - (void)expectICECandidates:(int)count {
117 _expectedICECandidates += count;
120 - (void)expectICEConnectionChange:(RTCICEConnectionState)state {
121 [_expectedICEConnectionChanges addObject:@((int)state)];
124 - (void)expectICEGatheringChange:(RTCICEGatheringState)state {
125 [_expectedICEGatheringChanges addObject:@((int)state)];
128 - (void)expectDataChannel:(NSString*)label {
129 [_expectedDataChannels addObject:label];
132 - (void)expectStateChange:(RTCDataChannelState)state {
133 [_expectedStateChanges addObject:@(state)];
136 - (void)expectMessage:(NSData*)message isBinary:(BOOL)isBinary {
137 RTCDataBuffer* buffer = [[RTCDataBuffer alloc] initWithData:message
139 [_expectedMessages addObject:buffer];
142 - (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]];
152 #pragma mark - RTCPeerConnectionDelegate methods
154 - (void)peerConnectionOnError:(RTCPeerConnection*)peerConnection {
155 NSLog(@"RTCPeerConnectionDelegate::onError");
156 NSAssert(--_expectedErrors >= 0, @"Unexpected error");
159 - (void)peerConnection:(RTCPeerConnection*)peerConnection
160 signalingStateChanged:(RTCSignalingState)stateChanged {
161 int expectedState = [self popFirstElementAsInt:_expectedSignalingChanges];
163 [NSString stringWithFormat:@"RTCPeerConnectionDelegate::"
164 @"onSignalingStateChange [%d] expected[%d]",
167 NSAssert(expectedState == (int)stateChanged, message);
170 - (void)peerConnection:(RTCPeerConnection*)peerConnection
171 addedStream:(RTCMediaStream*)stream {
172 NSString* expectedLabel =
173 [self popFirstElementAsNSString:_expectedAddStreamLabels];
174 NSAssert([expectedLabel isEqual:stream.label], @"Stream not expected");
177 - (void)peerConnection:(RTCPeerConnection*)peerConnection
178 removedStream:(RTCMediaStream*)stream {
179 NSString* expectedLabel =
180 [self popFirstElementAsNSString:_expectedRemoveStreamLabels];
181 NSAssert([expectedLabel isEqual:stream.label], @"Stream not expected");
184 - (void)peerConnectionOnRenegotiationNeeded:(RTCPeerConnection*)peerConnection {
187 - (void)peerConnection:(RTCPeerConnection*)peerConnection
188 gotICECandidate:(RTCICECandidate*)candidate {
189 --_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];
196 - (void)peerConnection:(RTCPeerConnection*)peerConnection
197 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) {
204 int expectedState = [self popFirstElementAsInt:_expectedICEGatheringChanges];
205 NSAssert(expectedState == (int)newState, @"Empty expectation array");
208 - (void)peerConnection:(RTCPeerConnection*)peerConnection
209 iceConnectionChanged:(RTCICEConnectionState)newState {
210 // See TODO(fischman) in RTCPeerConnectionTest.mm about Completed.
211 if (newState == RTCICEConnectionCompleted)
213 int expectedState = [self popFirstElementAsInt:_expectedICEConnectionChanges];
214 NSAssert(expectedState == (int)newState, @"Empty expectation array");
217 - (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");
229 #pragma mark - RTCDataChannelDelegate
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");
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];