PageRenderTime 44ms CodeModel.GetById 14ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 0ms

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

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