PageRenderTime 39ms CodeModel.GetById 14ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

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