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

http://macfuse.googlecode.com/ · Objective C · 94 lines · 57 code · 20 blank · 17 comment · 1 complexity · 5ddf22d66d130fa446b9c6ea26d22efb MD5 · raw file

  1. // Copyright 2009 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 <SenTestingKit/SenTestingKit.h>
  15. #import "KSMockFetcherFactory.h"
  16. #import "GDataHTTPFetcher.h"
  17. #import "KSFetcherFactory.h"
  18. @interface KSMockFetcherFactory (TestingFriend)
  19. - (id)initWithClass:(Class)class arg1:(id)arg1 arg2:(id)arg2 status:(int)status;
  20. @end
  21. @interface KSMockFetcherFactoryTest : SenTestCase {
  22. NSData *fetchedData_;
  23. NSError *fetchError_;
  24. }
  25. @end
  26. @implementation KSMockFetcherFactoryTest
  27. - (void)tearDown {
  28. [fetchedData_ release];
  29. fetchedData_ = nil;
  30. [fetchError_ release];
  31. fetchError_ = nil;
  32. }
  33. - (void)fetcher:(GDataHTTPFetcher *)fetcher finishedWithData:(NSData *)data {
  34. fetchedData_ = [data retain];
  35. }
  36. - (void)fetcher:(GDataHTTPFetcher *)fetcher failedWithError:(NSError *)error {
  37. fetchError_ = [error retain];
  38. }
  39. - (void)testDataFetcher {
  40. // Put some data into the mock fetcher and make sure we get that data out,
  41. // along with non-failure.
  42. const char *string = "Hoff";
  43. NSData *data = [NSData dataWithBytes:string length:strlen(string)];
  44. KSFetcherFactory *factory = [KSMockFetcherFactory alwaysFinishWithData:data];
  45. NSURL *url = [NSURL URLWithString:@"http://google.com"];
  46. NSURLRequest *req = [NSURLRequest requestWithURL:url];
  47. GDataHTTPFetcher *fetcher = [factory createFetcherForRequest:req];
  48. [fetcher beginFetchWithDelegate:self
  49. didFinishSelector:@selector(fetcher:finishedWithData:)
  50. didFailSelector:@selector(fetcher:failedWithError:)];
  51. // Let the fetcher do its thing.
  52. NSDate *quick = [NSDate dateWithTimeIntervalSinceNow:0.2];
  53. [[NSRunLoop currentRunLoop] runUntilDate:quick];
  54. STAssertNil(fetchError_, nil);
  55. STAssertNotNil(fetchedData_, nil);
  56. STAssertTrue(strcmp([fetchedData_ bytes], string) == 0, nil);
  57. }
  58. - (void)testErrorFetcher {
  59. NSError *error = [[[NSError alloc] init] autorelease];
  60. KSFetcherFactory *factory = [KSMockFetcherFactory alwaysFailWithError:error];
  61. NSURL *url = [NSURL URLWithString:@"http://google.com"];
  62. NSURLRequest *req = [NSURLRequest requestWithURL:url];
  63. GDataHTTPFetcher *fetcher = [factory createFetcherForRequest:req];
  64. [fetcher beginFetchWithDelegate:self
  65. didFinishSelector:@selector(fetcher:finishedWithData:)
  66. didFailSelector:@selector(fetcher:failedWithError:)];
  67. // Let the fetcher do its thing.
  68. NSDate *quick = [NSDate dateWithTimeIntervalSinceNow:0.2];
  69. [[NSRunLoop currentRunLoop] runUntilDate:quick];
  70. STAssertNotNil(fetchError_, nil);
  71. STAssertEquals(fetchError_, error, nil);
  72. STAssertNil(fetchedData_, nil);
  73. }
  74. @end