/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. #import "KSMockFetcherFactory.h"
  15. // Base class for mock fetchers, to be used in place of GDataHTTPFetcher.
  16. @interface KSMockFetcher : NSObject {
  17. NSURLRequest *request_;
  18. id delegate_;
  19. SEL finishedSelector_;
  20. SEL failedWithErrorSelector_;
  21. }
  22. - (id)initWithURLRequest:(NSURLRequest *)request;
  23. // Let's try and look like a GDataHTTPFetcher; at least enough
  24. // to fool KSUpdateChecker.
  25. - (BOOL)beginFetchWithDelegate:(id)delegate
  26. didFinishSelector:(SEL)finishedSEL
  27. didFailSelector:(SEL)networkFailedSEL;
  28. - (NSURLResponse *)response;
  29. // subclasses should override this, to perform a "response" on the run loop.
  30. - (void)invoke;
  31. // so unit tests are happy
  32. - (BOOL)isFetching;
  33. - (void)stopFetching;
  34. @end
  35. // Fetcher which always claims to finish correctly, returning the
  36. // given data.
  37. @interface KSMockFetcherFinishWithData : KSMockFetcher {
  38. NSData *data_;
  39. }
  40. - (id)initWithURLRequest:(NSURLRequest *)request data:(NSData *)data;
  41. - (void)invoke;
  42. @end
  43. // Fetcher which always fails with an error.
  44. @interface KSMockFetcherFailWithError : KSMockFetcher {
  45. NSError *error_;
  46. }
  47. - (id)initWithURLRequest:(NSURLRequest *)request error:(NSError *)error;
  48. - (void)invoke;
  49. @end
  50. /* --------------------------------------------------------------- */
  51. @implementation KSMockFetcher
  52. - (id)initWithURLRequest:(NSURLRequest *)request {
  53. if ((self = [super init]) != nil) {
  54. request_ = [request retain];
  55. }
  56. return self;
  57. }
  58. - (void)dealloc {
  59. [request_ release];
  60. [delegate_ release];
  61. [super dealloc];
  62. }
  63. - (BOOL)beginFetchWithDelegate:(id)delegate
  64. didFinishSelector:(SEL)finishedSEL
  65. didFailSelector:(SEL)networkFailedSEL {
  66. delegate_ = [delegate retain];
  67. finishedSelector_ = finishedSEL;
  68. failedWithErrorSelector_ = networkFailedSEL;
  69. NSArray *modes = [NSArray arrayWithObject:NSDefaultRunLoopMode];
  70. [[NSRunLoop currentRunLoop] performSelector:@selector(invoke) target:self
  71. argument:nil
  72. order:0
  73. modes:modes];
  74. return YES;
  75. }
  76. - (NSURLResponse *)response {
  77. // KSUpdateChecker asks for this but ignores it's value.
  78. // Let's return something legit-looking so it's happy.
  79. NSURL *url = [NSURL URLWithString:@"http://foo.bar"];
  80. return [[[NSURLResponse alloc] initWithURL:url
  81. MIMEType:@"text"
  82. expectedContentLength:0
  83. textEncodingName:nil] autorelease];
  84. }
  85. // COV_NF_START
  86. - (void)invoke {
  87. // Can't do this unless we derive from SenTestCase
  88. // STAssertNotNil(nil, nil); // fail if not overridden
  89. _GTMDevAssert(0, @"invoke must be overridden"); // COV_NF_LINE
  90. }
  91. // COV_NF_END
  92. - (BOOL)isFetching {
  93. return YES; // a lie
  94. }
  95. - (void)stopFetching {
  96. // noop
  97. }
  98. @end
  99. /* --------------------------------------------------------------- */
  100. @implementation KSMockFetcherFinishWithData
  101. - (id)initWithURLRequest:(NSURLRequest *)request data:(NSData *)data {
  102. if ((self = [super initWithURLRequest:request]) != nil) {
  103. data_ = [data retain];
  104. }
  105. return self;
  106. }
  107. - (void)dealloc {
  108. [data_ release];
  109. [super dealloc];
  110. }
  111. - (void)invoke {
  112. [delegate_ performSelector:finishedSelector_
  113. withObject:self
  114. withObject:data_];
  115. }
  116. @end
  117. /* --------------------------------------------------------------- */
  118. @implementation KSMockFetcherFailWithError
  119. - (id)initWithURLRequest:(NSURLRequest *)request error:(NSError *)error {
  120. if ((self = [super initWithURLRequest:request]) != nil) {
  121. error_ = [error retain];
  122. }
  123. return self;
  124. }
  125. - (void)dealloc {
  126. [error_ release];
  127. [super dealloc];
  128. }
  129. - (void)invoke {
  130. [delegate_ performSelector:failedWithErrorSelector_
  131. withObject:self
  132. withObject:error_];
  133. }
  134. @end
  135. /* --------------------------------------------------------------- */
  136. @interface KSMockFetcherFactory (Private)
  137. - (id)initWithClass:(Class)class arg1:(id)arg1 arg2:(id)arg2 status:(int)status;
  138. @end
  139. @implementation KSMockFetcherFactory (Private)
  140. - (id)initWithClass:(Class)class arg1:(id)arg1 arg2:(id)arg2 status:(int)status {
  141. if ((self = [super init]) != nil) {
  142. class_ = class;
  143. arg1_ = [arg1 retain];
  144. arg2_ = [arg2 retain];
  145. status_ = status;
  146. }
  147. return self;
  148. }
  149. @end
  150. @implementation KSMockFetcherFactory
  151. + (KSMockFetcherFactory *)alwaysFinishWithData:(NSData *)data {
  152. return [[[KSMockFetcherFactory alloc]
  153. initWithClass:[KSMockFetcherFinishWithData class]
  154. arg1:data arg2:nil status:0] autorelease];
  155. }
  156. + (KSMockFetcherFactory *)alwaysFailWithError:(NSError *)error {
  157. return [[[KSMockFetcherFactory alloc]
  158. initWithClass:[KSMockFetcherFailWithError class]
  159. arg1:error arg2:nil status:0] autorelease];
  160. }
  161. - (void)dealloc {
  162. [arg1_ release];
  163. [arg2_ release];
  164. [super dealloc];
  165. }
  166. - (GDataHTTPFetcher *)createFetcherForRequest:(NSURLRequest *)request {
  167. if (class_ == [KSMockFetcherFinishWithData class]) {
  168. return [[[KSMockFetcherFinishWithData alloc] initWithURLRequest:request
  169. data:arg1_]
  170. autorelease];
  171. } else if (class_ == [KSMockFetcherFailWithError class]) {
  172. return [[[KSMockFetcherFailWithError alloc] initWithURLRequest:request
  173. error:arg1_]
  174. autorelease];
  175. } else {
  176. _GTMDevAssert(0, @"can't decide what to mock"); // COV_NF_LINE
  177. return nil; // COV_NF_LINE
  178. }
  179. }
  180. @end