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