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