PageRenderTime 70ms CodeModel.GetById 1ms app.highlight 65ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/Core/KSMockFetcherFactory.m

http://macfuse.googlecode.com/
Objective C | 232 lines | 148 code | 52 blank | 32 comment | 14 complexity | c26262d410757de1da4986cae871badb MD5 | raw file
  1// Copyright 2008 Google Inc.
  2//
  3// Licensed under the Apache License, Version 2.0 (the "License");
  4// you may not use this file except in compliance with the License.
  5// You may obtain a copy of the License at
  6//
  7//     http://www.apache.org/licenses/LICENSE-2.0
  8//
  9// Unless required by applicable law or agreed to in writing, software
 10// distributed under the License is distributed on an "AS IS" BASIS,
 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12// See the License for the specific language governing permissions and
 13// limitations under the License.
 14
 15#import "KSMockFetcherFactory.h"
 16
 17
 18// Base class for mock fetchers, to be used in place of GDataHTTPFetcher.
 19@interface KSMockFetcher : NSObject {
 20  NSURLRequest *request_;
 21  id delegate_;
 22  SEL finishedSelector_;
 23  SEL failedWithErrorSelector_;
 24}
 25- (id)initWithURLRequest:(NSURLRequest *)request;
 26
 27// Let's try and look like a GDataHTTPFetcher; at least enough
 28// to fool KSUpdateChecker.
 29- (BOOL)beginFetchWithDelegate:(id)delegate
 30             didFinishSelector:(SEL)finishedSEL
 31               didFailSelector:(SEL)networkFailedSEL;
 32- (NSURLResponse *)response;
 33
 34// subclasses should override this, to perform a "response" on the run loop.
 35- (void)invoke;
 36
 37// so unit tests are happy
 38- (BOOL)isFetching;
 39- (void)stopFetching;
 40@end
 41
 42
 43// Fetcher which always claims to finish correctly, returning the
 44// given data.
 45@interface KSMockFetcherFinishWithData : KSMockFetcher {
 46  NSData *data_;
 47}
 48- (id)initWithURLRequest:(NSURLRequest *)request data:(NSData *)data;
 49- (void)invoke;
 50@end
 51
 52
 53// Fetcher which always fails with an error.
 54@interface KSMockFetcherFailWithError : KSMockFetcher {
 55  NSError *error_;
 56}
 57- (id)initWithURLRequest:(NSURLRequest *)request error:(NSError *)error;
 58- (void)invoke;
 59@end
 60
 61
 62/* --------------------------------------------------------------- */
 63
 64@implementation KSMockFetcher
 65
 66- (id)initWithURLRequest:(NSURLRequest *)request {
 67  if ((self = [super init]) != nil) {
 68    request_ = [request retain];
 69  }
 70  return self;
 71}
 72
 73- (void)dealloc {
 74  [request_ release];
 75  [delegate_ release];
 76  [super dealloc];
 77}
 78
 79- (BOOL)beginFetchWithDelegate:(id)delegate
 80             didFinishSelector:(SEL)finishedSEL
 81               didFailSelector:(SEL)networkFailedSEL {
 82  delegate_ = [delegate retain];
 83  finishedSelector_ = finishedSEL;
 84  failedWithErrorSelector_ = networkFailedSEL;
 85  NSArray *modes = [NSArray arrayWithObject:NSDefaultRunLoopMode];
 86  [[NSRunLoop currentRunLoop] performSelector:@selector(invoke) target:self
 87                                     argument:nil
 88                                        order:0
 89                                        modes:modes];
 90  return YES;
 91}
 92
 93- (NSURLResponse *)response {
 94  // KSUpdateChecker asks for this but ignores it's value.
 95  // Let's return something legit-looking so it's happy.
 96  NSURL *url = [NSURL URLWithString:@"http://foo.bar"];
 97  return [[[NSURLResponse alloc] initWithURL:url
 98                                    MIMEType:@"text"
 99                       expectedContentLength:0
100                            textEncodingName:nil] autorelease];
101}
102
103// COV_NF_START
104- (void)invoke {
105  // Can't do this unless we derive from SenTestCase
106  // STAssertNotNil(nil, nil);  // fail if not overridden
107  _GTMDevAssert(0, @"invoke must be overridden");  // COV_NF_LINE
108}
109// COV_NF_END
110
111- (BOOL)isFetching {
112  return YES;  // a lie
113}
114
115- (void)stopFetching {
116  // noop
117}
118
119@end
120
121
122/* --------------------------------------------------------------- */
123@implementation KSMockFetcherFinishWithData
124
125- (id)initWithURLRequest:(NSURLRequest *)request data:(NSData *)data {
126  if ((self = [super initWithURLRequest:request]) != nil) {
127    data_ = [data retain];
128  }
129  return self;
130}
131
132- (void)dealloc {
133  [data_ release];
134  [super dealloc];
135}
136
137- (void)invoke {
138  [delegate_ performSelector:finishedSelector_
139                  withObject:self
140                  withObject:data_];
141}
142
143@end
144
145
146/* --------------------------------------------------------------- */
147@implementation KSMockFetcherFailWithError
148
149- (id)initWithURLRequest:(NSURLRequest *)request error:(NSError *)error {
150  if ((self = [super initWithURLRequest:request]) != nil) {
151    error_ = [error retain];
152  }
153  return self;
154}
155
156- (void)dealloc {
157  [error_ release];
158  [super dealloc];
159}
160
161- (void)invoke {
162  [delegate_ performSelector:failedWithErrorSelector_
163                  withObject:self
164                  withObject:error_];
165}
166
167@end
168
169
170/* --------------------------------------------------------------- */
171
172@interface KSMockFetcherFactory (Private)
173- (id)initWithClass:(Class)class arg1:(id)arg1 arg2:(id)arg2 status:(int)status;
174@end
175
176
177@implementation KSMockFetcherFactory (Private)
178
179- (id)initWithClass:(Class)class arg1:(id)arg1 arg2:(id)arg2 status:(int)status {
180  if ((self = [super init]) != nil) {
181    class_ = class;
182    arg1_ = [arg1 retain];
183    arg2_ = [arg2 retain];
184    status_ = status;
185  }
186  return self;
187}
188
189@end
190
191
192@implementation KSMockFetcherFactory
193
194+ (KSMockFetcherFactory *)alwaysFinishWithData:(NSData *)data {
195  return [[[KSMockFetcherFactory alloc]
196           initWithClass:[KSMockFetcherFinishWithData class]
197            arg1:data arg2:nil status:0] autorelease];
198}
199
200+ (KSMockFetcherFactory *)alwaysFailWithError:(NSError *)error {
201  return [[[KSMockFetcherFactory alloc]
202           initWithClass:[KSMockFetcherFailWithError class]
203            arg1:error arg2:nil status:0] autorelease];
204}
205
206- (void)dealloc {
207  [arg1_ release];
208  [arg2_ release];
209  [super dealloc];
210}
211
212- (GDataHTTPFetcher *)createFetcherForRequest:(NSURLRequest *)request {
213  if (class_ == [KSMockFetcherFinishWithData class]) {
214    return [[[KSMockFetcherFinishWithData alloc] initWithURLRequest:request
215                                                                  data:arg1_]
216             autorelease];
217  } else if (class_ == [KSMockFetcherFailWithError class]) {
218    return [[[KSMockFetcherFailWithError alloc] initWithURLRequest:request
219                                                             error:arg1_]
220             autorelease];
221  } else {
222    _GTMDevAssert(0, @"can't decide what to mock");  // COV_NF_LINE
223    return nil;  // COV_NF_LINE
224  }
225}
226
227
228
229@end
230
231
232