00001
00002
00003
00004
00005
00006
00007
00008
00009 #import "RMCallTests.h"
00010 #import "MockProtocol.h"
00011 #import "MockProtocol2.h"
00012 #import "MockProtocolShouldSend.h"
00013 #import "RMResultDelegateWrapper.h"
00014 #import "WIRemoting.h"
00015
00016 @implementation RMCallTests
00017
00018 #pragma mark test set up
00019
00020 - (void) setUp
00021 {
00022 protocol = [[MockProtocol alloc] init];
00023 call = [[RMCall alloc] initWithProtocol:protocol];
00024 }
00025
00026 - (void) tearDown
00027 {
00028 [call release];
00029 [protocol release];
00030 }
00031
00032 #pragma mark unit tests
00033
00034 - (void) testSingleProtocol {
00035 RMResultDelegateWrapper *m = [[RMResultDelegateWrapper alloc] initWithObject:self
00036 finished:@selector(callEchoFinished:)
00037 failed:@selector(failed:error:)];
00038 [call addDelegate:m];
00039 [call call:@"echo" arguments:
00040 [NSDictionary
00041 dictionaryWithObjectsAndKeys:
00042 @"hello world", @"echo",
00043 nil]];
00044 [call removeDelegate:m];
00045 [m release];
00046 }
00047
00048 - (void) testMultipleProtocols
00049 {
00050 RMResultDelegateWrapper *m = [[RMResultDelegateWrapper alloc] initWithObject:self
00051 finished:@selector(callMultipleJsonEchoFinished:)
00052 failed:@selector(failed:error:)];
00053 MockProtocol2 *proto2 = [[MockProtocol2 alloc] init];
00054
00055 [call pushProtocol:proto2];
00056
00057
00058 [call addDelegate:m];
00059 [call call:@"echo" arguments:
00060 [NSDictionary
00061 dictionaryWithObjectsAndKeys:
00062 @"hello world", @"echo",
00063 @"hello world 2", @"echo2",
00064 nil]];
00065
00066 [call removeDelegate:m];
00067 [proto2 release];
00068 [m release];
00069
00070
00071 STAssertTrue([[call topProtocol] isKindOfClass:[MockProtocol2 class]],
00072 @"The top protocol is expected to be MockProtocol2, but it is %@.",
00073 [MockProtocol2 class]);
00074
00075
00076 [call popProtocol];
00077
00078 m = [[RMResultDelegateWrapper alloc] initWithObject:self
00079 finished:@selector(callEchoFinished:)
00080 failed:@selector(failed:error:)];
00081
00082 [call addDelegate:m];
00083 [call call:@"echo" arguments:
00084 [NSDictionary
00085 dictionaryWithObjectsAndKeys:
00086 @"hello world", @"echo",
00087 nil]];
00088
00089 [call removeDelegate:m];
00090 [m release];
00091 }
00092
00093 - (void) testStraightCall
00094 {
00095 RMResultDelegateWrapper *m = [[RMResultDelegateWrapper alloc] initWithObject:self
00096 finished:@selector(callMultipleJsonEchoFinished:)
00097 failed:@selector(failed:error:)];
00098 MockProtocol2 *proto2 = [[MockProtocol2 alloc] init];
00099
00100
00101 [call addDelegate:m];
00102 [call call:@"echo"
00103 arguments:[NSDictionary
00104 dictionaryWithObjectsAndKeys:
00105 @"hello world", @"echo",
00106 @"hello world 2", @"echo2",
00107 nil]
00108 protocol:proto2];
00109
00110 [proto2 release];
00111 [call removeDelegate:m];
00112 [m release];
00113 }
00114
00115 - (void) testRequestShouldSend
00116 {
00117 RMResultDelegateWrapper *m = [[RMResultDelegateWrapper alloc] initWithObject:self
00118 finished:@selector(callEchoFinished:)
00119 failed:@selector(failed:error:)];
00120
00121 MockProtocolShouldSend *p2 = [[MockProtocolShouldSend alloc] init];
00122
00123 [call pushProtocol:p2];
00124
00125 [call addDelegate:m];
00126 BOOL r = [call call:@"echo"
00127 arguments:[NSDictionary
00128 dictionaryWithObjectsAndKeys:
00129 @"hello world", @"echo",
00130 nil]];
00131 if (r) {
00132 STFail(@"The call shouldn't be sent.");
00133 }
00134
00135 [call popProtocol];
00136 [p2 release];
00137 [call removeDelegate:m];
00138 [m release];
00139 }
00140
00141 #pragma mark assistant methods
00142
00143 - (void) callMultipleJsonEchoFinished:(RMResponse *) response
00144 {
00145 id obj = [[response responseString] JSONValue];
00146 if (nil != obj && [obj isKindOfClass:[NSDictionary class]]) {
00147 STAssertTrue([[obj valueForKey:@"echo"] isEqual:@"hello world"],
00148 @"Call json_echo2 should receive what we sent, but \"%@\" received",
00149 [obj valueForKey:@"echo"]);
00150 STAssertTrue([[obj valueForKey:@"echo2"] isEqual:@"hello world 2"],
00151 @"Call json_echo2 should receive what we sent, but \"%@\" received",
00152 [obj valueForKey:@"echo2"]);
00153 }
00154 else {
00155 STFail(@"The response should contain a dictionary object.");
00156 }
00157 }
00158
00159 - (void) callEchoFinished:(RMResponse *) response
00160 {
00161 STAssertTrue([[response responseString]
00162 isEqualToString:@"hello world"],
00163 @"Call Echo should receive what we sent, but \"%@\" received.",
00164 [response responseString]);
00165 }
00166
00167 - (void) failed:(RMResponse *) response
00168 error:(NSError *) error
00169 {
00170 NSLog(@"%@\n", [error localizedFailureReason]);
00171 NSLog(@"%@\n", [response responseString]);
00172 STFail([error localizedFailureReason]);
00173 }
00174
00175 @end