PageRenderTime 89ms CodeModel.GetById 30ms app.highlight 56ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://macfuse.googlecode.com/
Objective C | 189 lines | 139 code | 36 blank | 14 comment | 2 complexity | 932bbdded61c15988f14f30b03e583ef 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 <SenTestingKit/SenTestingKit.h>
 16#import "KSPrefetchAction.h"
 17#import "KSUpdateEngine.h"
 18#import "KSUpdateInfo.h"
 19#import "KSActionPipe.h"
 20#import "KSActionProcessor.h"
 21
 22
 23@interface KSPrefetchActionTest : SenTestCase
 24@end
 25
 26
 27static NSString *const kTicketStorePath = @"/tmp/KSPrefetchActionTest.ticketstore";
 28
 29
 30@implementation KSPrefetchActionTest
 31
 32- (void)setUp {
 33  [@"" writeToFile:kTicketStorePath atomically:YES];
 34  [KSUpdateEngine setDefaultTicketStorePath:kTicketStorePath];
 35}
 36
 37- (void)tearDown {
 38  [[NSFileManager defaultManager] removeFileAtPath:kTicketStorePath handler:nil];
 39  [KSUpdateEngine setDefaultTicketStorePath:nil];
 40}
 41
 42// KSUpdateEngineDelegate protocol method
 43- (NSArray *)engine:(KSUpdateEngine *)engine
 44shouldPrefetchProducts:(NSArray *)products {
 45  GTMLoggerInfo(@"products=%@", products);
 46  return [products filteredArrayUsingPredicate:
 47          [NSPredicate predicateWithFormat:
 48           @"%K like 'allow*'", kServerProductID]];
 49}
 50
 51- (void)loopUntilDone:(KSActionProcessor *)processor {
 52  int count = 50;
 53  while ([processor isProcessing] && (count > 0)) {
 54    NSDate *quick = [NSDate dateWithTimeIntervalSinceNow:0.2];
 55    [[NSRunLoop currentRunLoop] runUntilDate:quick];
 56    count--;
 57  }
 58  STAssertFalse([processor isProcessing], nil);
 59}
 60
 61- (void)testCreation {
 62  KSPrefetchAction *action = [KSPrefetchAction actionWithEngine:nil];
 63  STAssertNil(action, nil);
 64  
 65  action = [[[KSPrefetchAction alloc] init] autorelease];
 66  STAssertNil(action, nil);
 67  
 68  action = [[[KSPrefetchAction alloc] initWithEngine:nil] autorelease];
 69  STAssertNil(action, nil);
 70  
 71  KSUpdateEngine *engine = [KSUpdateEngine engineWithDelegate:nil];
 72  STAssertNotNil(engine, nil);
 73  action = [KSPrefetchAction actionWithEngine:engine];
 74  STAssertNotNil(action, nil);
 75}
 76
 77- (void)testPrefetching {
 78  KSUpdateEngine *engine = [KSUpdateEngine engineWithDelegate:self];
 79  STAssertNotNil(engine, nil);
 80  
 81  KSPrefetchAction *action = [KSPrefetchAction actionWithEngine:engine];
 82  STAssertNotNil(action, nil);
 83  
 84  NSArray *availableProducts =
 85  [[NSArray alloc] initWithObjects:
 86   [NSDictionary dictionaryWithObjectsAndKeys:
 87    @"allow1", kServerProductID,
 88    [NSURL URLWithString:@"a://b"], kServerCodebaseURL,
 89    [NSNumber numberWithInt:1], kServerCodeSize,
 90    @"zzz", kServerCodeHash,
 91    @"a://b", kServerMoreInfoURLString,
 92    nil],
 93   [NSDictionary dictionaryWithObjectsAndKeys:
 94    @"allow2", kServerProductID,
 95    [NSURL URLWithString:@"a://b"], kServerCodebaseURL,
 96    [NSNumber numberWithInt:2], kServerCodeSize,
 97    @"xxx", kServerCodeHash,
 98    @"a://b", kServerMoreInfoURLString,
 99    nil],
100   [NSDictionary dictionaryWithObjectsAndKeys:
101    @"allow3", kServerProductID,
102    [NSURL URLWithString:@"a://b"], kServerCodebaseURL,
103    [NSNumber numberWithInt:3], kServerCodeSize,
104    @"yyy", kServerCodeHash,
105    @"a://b", kServerMoreInfoURLString,
106    nil],             
107   [NSDictionary dictionaryWithObjectsAndKeys:
108    @"deny1", kServerProductID,
109    [NSURL URLWithString:@"a://b"], kServerCodebaseURL,
110    [NSNumber numberWithInt:3], kServerCodeSize,
111    @"vvv", kServerCodeHash,
112    @"a://b", kServerMoreInfoURLString,
113    nil],
114   nil];
115  
116  KSActionPipe *pipe = [KSActionPipe pipe];
117  [pipe setContents:availableProducts];
118  [action setInPipe:pipe];
119  
120  KSActionProcessor *ap = [[[KSActionProcessor alloc] init] autorelease];
121  [ap enqueueAction:action];
122  
123  [ap startProcessing];
124  [self loopUntilDone:ap];
125  STAssertFalse([ap isProcessing], nil);
126  
127  STAssertEquals([action subActionsProcessed], 3, nil);
128}
129
130- (void)testNegativeFiltering {
131  KSUpdateEngine *engine = [KSUpdateEngine engineWithDelegate:self];
132  STAssertNotNil(engine, nil);
133  
134  KSPrefetchAction *action = [KSPrefetchAction actionWithEngine:engine];
135  STAssertNotNil(action, nil);
136  
137  NSArray *availableProducts =
138  [[NSArray alloc] initWithObjects:
139   [NSDictionary dictionaryWithObjectsAndKeys:
140    @"deny1", kServerProductID,
141    [NSURL URLWithString:@"a://b"], kServerCodebaseURL,
142    [NSNumber numberWithInt:1], kServerCodeSize,
143    @"vvv", kServerCodeHash,
144    @"a://b", kServerMoreInfoURLString,
145    nil],
146   [NSDictionary dictionaryWithObjectsAndKeys:
147    @"deny2", kServerProductID,
148    [NSURL URLWithString:@"a://b"], kServerCodebaseURL,
149    [NSNumber numberWithInt:2], kServerCodeSize,
150    @"qqq", kServerCodeHash,
151    @"a://b", kServerMoreInfoURLString,
152    nil],
153   nil];
154  
155  KSActionPipe *pipe = [KSActionPipe pipe];
156  [pipe setContents:availableProducts];
157  [action setInPipe:pipe];
158  
159  KSActionProcessor *ap = [[[KSActionProcessor alloc] init] autorelease];
160  [ap enqueueAction:action];
161  
162  [ap startProcessing];
163  [self loopUntilDone:ap];
164  STAssertFalse([ap isProcessing], nil);
165  
166  STAssertEquals([action subActionsProcessed], 0, nil);
167}
168
169- (void)testNoUpdates {
170  KSUpdateEngine *engine = [KSUpdateEngine engineWithDelegate:self];
171  STAssertNotNil(engine, nil);
172  
173  KSPrefetchAction *action = [KSPrefetchAction actionWithEngine:engine];
174  STAssertNotNil(action, nil);
175  
176  KSActionPipe *pipe = [KSActionPipe pipe];
177  [action setInPipe:pipe];  // This pipe is empty
178  
179  KSActionProcessor *ap = [[[KSActionProcessor alloc] init] autorelease];
180  [ap enqueueAction:action];
181  
182  [ap startProcessing];
183  [self loopUntilDone:ap];
184  STAssertFalse([ap isProcessing], nil);
185  
186  STAssertEqualObjects([[action outPipe] contents], nil, nil);
187}
188
189@end