/
AEURLExampleTests.m
101 lines (83 loc) · 3.47 KB
/
AEURLExampleTests.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
//
// AEURLExampleTests.m
// AEURLExampleTests
//
// Created by Adam Ernst on 10/12/11.
// Copyright (c) 2011 cosmicsoft. All rights reserved.
//
#import "AEURLExampleTests.h"
#import "AEURLConnection.h"
#import "AEURLRequestFactory.h"
// This object simulates UIViewController, which can only be released on the
// main thread. (This is the root of "The Deallocation Problem"; search the
// web for more.)
@interface AEMainThreadReleaseOnlyObject : NSObject {
BOOL *gotResponse;
BOOL *dealloced;
BOOL *deallocedOnMainThread;
}
- (id)initWithGotResponse:(BOOL *)g dealloced:(BOOL *)d onMainThread:(BOOL *)main;
- (void)handleResponse:(NSURLResponse *)response;
@end
@implementation AEURLExampleTests
- (void)testMainThreadReleaseOnlyObject {
NSAssert([NSThread isMainThread], @"Expected unit test to run on main thread");
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:20.0];
BOOL gotResponse = NO;
BOOL dealloced = NO;
BOOL deallocedOnMainThread = NO;
AEMainThreadReleaseOnlyObject *obj;
obj = [[AEMainThreadReleaseOnlyObject alloc] initWithGotResponse:&gotResponse
dealloced:&dealloced
onMainThread:&deallocedOnMainThread];
[AEURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
[obj handleResponse:response];
}];
[obj release];
NSAssert(!dealloced, @"Object shouldn't be dealloced yet; should be dealloced during running of runloop");
int runs = 0;
while (dealloced == NO && runs < 20) {
NSLog(@"Running main run loop since object isn't deallocated yet");
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
runs++;
}
if (!gotResponse) {
STFail(@"Completion handler never called, although timeout should have expired so we should have received response or error");
} else if (!dealloced) {
STFail(@"Object captured in completion handler was never released");
} else if (!deallocedOnMainThread) {
STFail(@"Main-thread-release-only object was dealloced, but not on main thread");
}
}
- (void)testQueryStringEncode {
NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:
@"", @"empty_value",
@"blah", @"boring_value",
@"†é߆", @"aççén†é∂", nil];
NSString *s = AEQueryStringFromParameters(d);
NSDictionary *d2 = AEParametersFromQueryString(s);
STAssertEqualObjects(d, d2, @"Query string encoding or decoding failed");
}
@end
@implementation AEMainThreadReleaseOnlyObject
- (id)initWithGotResponse:(BOOL *)g dealloced:(BOOL *)d onMainThread:(BOOL *)main {
if (self = [super init]) {
gotResponse = g;
dealloced = d;
deallocedOnMainThread = main;
}
return self;
}
- (void)dealloc {
*deallocedOnMainThread = [NSThread isMainThread];
*dealloced = YES;
[super dealloc];
}
- (void)handleResponse:(NSURLResponse *)response {
*gotResponse = YES;
}
@end