-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathAsyncSenTestingKitTests.m
207 lines (169 loc) · 7.97 KB
/
AsyncSenTestingKitTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//
// AsyncSenTestingKitTests.m
// AsyncSenTestingKitTests
//
// Created by 小野 将司 on 12/03/17.
// Copyright (c) 2012年 AppBankGames Inc. All rights reserved.
//
#import "AsyncSenTestingKitTests.h"
@implementation AsyncSenTestingKitTests
- (void)setUp
{
[super setUp];
NSLog(@"%s", __func__);
}
- (void)tearDown
{
NSLog(@"%s", __func__);
[super tearDown];
}
#pragma mark -
- (void)testAsyncTimeoutsProperly
{
[self waitForTimeout:3.0];
}
- (void)testAsyncTimeoutsProperly_EXPECT_FAIL
{
[self waitForStatus:SenAsyncTestCaseStatusSucceeded timeout:3.0];
}
#pragma mark -
- (void)testAsyncWithDelegate
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
[NSURLConnection connectionWithRequest:request delegate:self];
[self waitForStatus:SenAsyncTestCaseStatusSucceeded timeout:10.0];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Request Finished!");
[self notify:SenAsyncTestCaseStatusSucceeded];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Request failed with error: %@", error);
[self notify:SenAsyncTestCaseStatusFailed];
}
#pragma mark -
- (void)testAsyncWithBlocks200
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
NSLog(@"Request failed with error: %@", error);
[self notify:SenAsyncTestCaseStatusFailed];
return;
}
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
STAssertEquals([httpResponse statusCode], 200, @"");
NSLog(@"Request Finished!");
[self notify:SenAsyncTestCaseStatusSucceeded];
}];
[self waitForStatus:SenAsyncTestCaseStatusSucceeded timeout:10.0];
NSLog(@"Test Finished!");
}
- (void)testAsyncWithBlocks200_EXPECT_FAIL
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
NSLog(@"Request failed with error: %@", error);
[self notify:SenAsyncTestCaseStatusFailed];
return;
}
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
STAssertEquals([httpResponse statusCode], 404, @"Expected Fail");
NSLog(@"Request Finished!");
[self notify:SenAsyncTestCaseStatusSucceeded];
}];
[self waitForStatus:SenAsyncTestCaseStatusSucceeded timeout:10.0];
NSLog(@"Test Finished!");
}
- (void)testAsyncWithBlocksStatusDoesntMatch_EXPECT_FAIL
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
NSLog(@"Request failed with error: %@", error);
[self notify:SenAsyncTestCaseStatusFailed];
return;
}
NSLog(@"Request Finished!");
[self notify:SenAsyncTestCaseStatusCancelled];
}];
[self waitForStatus:SenAsyncTestCaseStatusSucceeded timeout:10.0];
NSLog(@"Test Finished!");
}
- (void)testAsyncWithBlocksError
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.there_should_be_no_such_domain_in_the_world.org"]];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (error) {
NSLog(@"Request failed with error: %@", error);
[self notify:SenAsyncTestCaseStatusFailed];
return;
}
STFail(@"Must fail before this statement");
}];
[self waitForStatus:SenAsyncTestCaseStatusFailed timeout:10.0];
}
- (void)testAsyncWithBlocksError_EXPECT_FAIL
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.there_should_be_no_such_domain_in_the_world.org"]];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
// DOES fail on events while not notifying
// This results in waiting until timeout
STFail(@"Must fail before this statement");
}];
[self waitForStatus:SenAsyncTestCaseStatusFailed timeout:5.0];
}
#pragma mark -
- (void)testAsyncMainQueue
{
/*
This is the preferred way if you're using iOS SDK 4 or above.
Test Case '-[AsyncSenTestingKitTests testAsyncMainQueue]' started.
2012-03-17 16:27:49.974 otest[939:7b03] -[AsyncSenTestingKitTests setUp]
2012-03-17 16:27:49.975 otest[939:7b03] Wait loop start
2012-03-17 16:27:51.976 otest[939:7b03] Notified
2012-03-17 16:27:51.977 otest[939:7b03] Wait loop finished
2012-03-17 16:27:51.979 otest[939:7b03] -[AsyncSenTestingKitTests tearDown]
Test Case '-[AsyncSenTestingKitTests testAsyncMainQueue]' passed (2.007 seconds).
*/
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^{
[self notify:SenAsyncTestCaseStatusSucceeded];
});
[self waitForStatus:SenAsyncTestCaseStatusSucceeded timeout:5.0];
}
- (void)testAsyncPerformSelector
{
/*
Using -performSelector:withObject:afterDelay is no longer recommended as the test has to wait until timeout, like below:
Test Case '-[AsyncSenTestingKitTests testAsyncPerformSelector]' started.
2012-03-17 16:27:51.980 otest[939:7b03] -[AsyncSenTestingKitTests setUp]
2012-03-17 16:27:51.981 otest[939:7b03] Wait loop start
2012-03-17 16:27:53.982 otest[939:7b03] Notified
2012-03-17 16:27:56.983 otest[939:7b03] Wait loop finished
2012-03-17 16:27:56.984 otest[939:7b03] -[AsyncSenTestingKitTests tearDown]
Test Case '-[AsyncSenTestingKitTests testAsyncPerformSelector]' passed (5.004 seconds).
This is because the -performSelector:withObject:afterDelay method internally uses timer. Timers are not considered to be
the input sources thus -runMode:beforeDate: doesn't return.
*/
[self performSelector:@selector(___internal___testAsyncPerformSelector) withObject:nil afterDelay:2.0];
[self waitForStatus:SenAsyncTestCaseStatusSucceeded timeout:5.0];
}
- (void)___internal___testAsyncPerformSelector
{
[self notify:SenAsyncTestCaseStatusSucceeded];
}
@end