1/*
2 *  Copyright 2018 The WebRTC project authors. All Rights Reserved.
3 *
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
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#import "api/logging/RTCCallbackLogger.h"
12
13#import <XCTest/XCTest.h>
14
15@interface RTCCallbackLoggerTests : XCTestCase
16
17@property(nonatomic, strong) RTC_OBJC_TYPE(RTCCallbackLogger) * logger;
18
19@end
20
21@implementation RTCCallbackLoggerTests
22
23@synthesize logger;
24
25- (void)setUp {
26  self.logger = [[RTC_OBJC_TYPE(RTCCallbackLogger) alloc] init];
27}
28
29- (void)tearDown {
30  self.logger = nil;
31}
32
33- (void)testDefaultSeverityLevel {
34  XCTAssertEqual(self.logger.severity, RTCLoggingSeverityInfo);
35}
36
37- (void)testCallbackGetsCalledForAppropriateLevel {
38  self.logger.severity = RTCLoggingSeverityWarning;
39
40  XCTestExpectation *callbackExpectation = [self expectationWithDescription:@"callbackWarning"];
41
42  [self.logger start:^(NSString *message) {
43    XCTAssertTrue([message hasSuffix:@"Horrible error\n"]);
44    [callbackExpectation fulfill];
45  }];
46
47  RTCLogError("Horrible error");
48
49  [self waitForExpectations:@[ callbackExpectation ] timeout:10.0];
50}
51
52- (void)testCallbackWithSeverityGetsCalledForAppropriateLevel {
53  self.logger.severity = RTCLoggingSeverityWarning;
54
55  XCTestExpectation *callbackExpectation = [self expectationWithDescription:@"callbackWarning"];
56
57  [self.logger
58      startWithMessageAndSeverityHandler:^(NSString *message, RTCLoggingSeverity severity) {
59        XCTAssertTrue([message hasSuffix:@"Horrible error\n"]);
60        XCTAssertEqual(severity, RTCLoggingSeverityError);
61        [callbackExpectation fulfill];
62      }];
63
64  RTCLogError("Horrible error");
65
66  [self waitForExpectations:@[ callbackExpectation ] timeout:10.0];
67}
68
69- (void)testCallbackDoesNotGetCalledForOtherLevels {
70  self.logger.severity = RTCLoggingSeverityError;
71
72  XCTestExpectation *callbackExpectation = [self expectationWithDescription:@"callbackError"];
73
74  [self.logger start:^(NSString *message) {
75    XCTAssertTrue([message hasSuffix:@"Horrible error\n"]);
76    [callbackExpectation fulfill];
77  }];
78
79  RTCLogInfo("Just some info");
80  RTCLogWarning("Warning warning");
81  RTCLogError("Horrible error");
82
83  [self waitForExpectations:@[ callbackExpectation ] timeout:10.0];
84}
85
86- (void)testCallbackWithSeverityDoesNotGetCalledForOtherLevels {
87  self.logger.severity = RTCLoggingSeverityError;
88
89  XCTestExpectation *callbackExpectation = [self expectationWithDescription:@"callbackError"];
90
91  [self.logger
92      startWithMessageAndSeverityHandler:^(NSString *message, RTCLoggingSeverity severity) {
93        XCTAssertTrue([message hasSuffix:@"Horrible error\n"]);
94        XCTAssertEqual(severity, RTCLoggingSeverityError);
95        [callbackExpectation fulfill];
96      }];
97
98  RTCLogInfo("Just some info");
99  RTCLogWarning("Warning warning");
100  RTCLogError("Horrible error");
101
102  [self waitForExpectations:@[ callbackExpectation ] timeout:10.0];
103}
104
105- (void)testCallbackDoesNotgetCalledForSeverityNone {
106  self.logger.severity = RTCLoggingSeverityNone;
107
108  XCTestExpectation *callbackExpectation = [self expectationWithDescription:@"unexpectedCallback"];
109
110  [self.logger start:^(NSString *message) {
111    [callbackExpectation fulfill];
112    XCTAssertTrue(false);
113  }];
114
115  RTCLogInfo("Just some info");
116  RTCLogWarning("Warning warning");
117  RTCLogError("Horrible error");
118
119  XCTWaiter *waiter = [[XCTWaiter alloc] init];
120  XCTWaiterResult result = [waiter waitForExpectations:@[ callbackExpectation ] timeout:1.0];
121  XCTAssertEqual(result, XCTWaiterResultTimedOut);
122}
123
124- (void)testCallbackWithSeverityDoesNotgetCalledForSeverityNone {
125  self.logger.severity = RTCLoggingSeverityNone;
126
127  XCTestExpectation *callbackExpectation = [self expectationWithDescription:@"unexpectedCallback"];
128
129  [self.logger
130      startWithMessageAndSeverityHandler:^(NSString *message, RTCLoggingSeverity severity) {
131        [callbackExpectation fulfill];
132        XCTAssertTrue(false);
133      }];
134
135  RTCLogInfo("Just some info");
136  RTCLogWarning("Warning warning");
137  RTCLogError("Horrible error");
138
139  XCTWaiter *waiter = [[XCTWaiter alloc] init];
140  XCTWaiterResult result = [waiter waitForExpectations:@[ callbackExpectation ] timeout:1.0];
141  XCTAssertEqual(result, XCTWaiterResultTimedOut);
142}
143
144- (void)testStartingWithNilCallbackDoesNotCrash {
145  [self.logger start:nil];
146
147  RTCLogError("Horrible error");
148}
149
150- (void)testStartingWithNilCallbackWithSeverityDoesNotCrash {
151  [self.logger startWithMessageAndSeverityHandler:nil];
152
153  RTCLogError("Horrible error");
154}
155
156- (void)testStopCallbackLogger {
157  XCTestExpectation *callbackExpectation = [self expectationWithDescription:@"stopped"];
158
159  [self.logger start:^(NSString *message) {
160    [callbackExpectation fulfill];
161  }];
162
163  [self.logger stop];
164
165  RTCLogInfo("Just some info");
166
167  XCTWaiter *waiter = [[XCTWaiter alloc] init];
168  XCTWaiterResult result = [waiter waitForExpectations:@[ callbackExpectation ] timeout:1.0];
169  XCTAssertEqual(result, XCTWaiterResultTimedOut);
170}
171
172- (void)testStopCallbackWithSeverityLogger {
173  XCTestExpectation *callbackExpectation = [self expectationWithDescription:@"stopped"];
174
175  [self.logger
176      startWithMessageAndSeverityHandler:^(NSString *message, RTCLoggingSeverity loggingServerity) {
177        [callbackExpectation fulfill];
178      }];
179
180  [self.logger stop];
181
182  RTCLogInfo("Just some info");
183
184  XCTWaiter *waiter = [[XCTWaiter alloc] init];
185  XCTWaiterResult result = [waiter waitForExpectations:@[ callbackExpectation ] timeout:1.0];
186  XCTAssertEqual(result, XCTWaiterResultTimedOut);
187}
188
189- (void)testDestroyingCallbackLogger {
190  XCTestExpectation *callbackExpectation = [self expectationWithDescription:@"destroyed"];
191
192  [self.logger start:^(NSString *message) {
193    [callbackExpectation fulfill];
194  }];
195
196  self.logger = nil;
197
198  RTCLogInfo("Just some info");
199
200  XCTWaiter *waiter = [[XCTWaiter alloc] init];
201  XCTWaiterResult result = [waiter waitForExpectations:@[ callbackExpectation ] timeout:1.0];
202  XCTAssertEqual(result, XCTWaiterResultTimedOut);
203}
204
205- (void)testDestroyingCallbackWithSeverityLogger {
206  XCTestExpectation *callbackExpectation = [self expectationWithDescription:@"destroyed"];
207
208  [self.logger
209      startWithMessageAndSeverityHandler:^(NSString *message, RTCLoggingSeverity loggingServerity) {
210        [callbackExpectation fulfill];
211      }];
212
213  self.logger = nil;
214
215  RTCLogInfo("Just some info");
216
217  XCTWaiter *waiter = [[XCTWaiter alloc] init];
218  XCTWaiterResult result = [waiter waitForExpectations:@[ callbackExpectation ] timeout:1.0];
219  XCTAssertEqual(result, XCTWaiterResultTimedOut);
220}
221
222- (void)testCallbackWithSeverityLoggerCannotStartTwice {
223  self.logger.severity = RTCLoggingSeverityWarning;
224
225  XCTestExpectation *callbackExpectation = [self expectationWithDescription:@"callbackWarning"];
226
227  [self.logger
228      startWithMessageAndSeverityHandler:^(NSString *message, RTCLoggingSeverity loggingServerity) {
229        XCTAssertTrue([message hasSuffix:@"Horrible error\n"]);
230        XCTAssertEqual(loggingServerity, RTCLoggingSeverityError);
231        [callbackExpectation fulfill];
232      }];
233
234  [self.logger start:^(NSString *message) {
235    [callbackExpectation fulfill];
236    XCTAssertTrue(false);
237  }];
238
239  RTCLogError("Horrible error");
240
241  [self waitForExpectations:@[ callbackExpectation ] timeout:10.0];
242}
243
244@end
245