Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/browser/snapshots/snapshot_cache.h" | 5 #import "ios/chrome/browser/snapshots/snapshot_cache.h" |
| 6 | 6 |
| 7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
| 12 #include "base/ios/ios_util.h" | 12 #include "base/ios/ios_util.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/mac/bind_objc_block.h" | 14 #include "base/mac/bind_objc_block.h" |
| 15 #include "base/mac/scoped_nsautorelease_pool.h" | 15 #include "base/mac/scoped_nsautorelease_pool.h" |
| 16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 17 #include "base/strings/sys_string_conversions.h" | 17 #include "base/strings/sys_string_conversions.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "ios/chrome/browser/experimental_flags.h" | |
| 19 #include "ios/chrome/browser/ui/ui_util.h" | 20 #include "ios/chrome/browser/ui/ui_util.h" |
| 20 #include "ios/web/public/test/test_web_thread_bundle.h" | 21 #include "ios/web/public/test/test_web_thread_bundle.h" |
| 21 #include "ios/web/public/web_thread.h" | 22 #include "ios/web/public/web_thread.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "testing/gtest_mac.h" | 24 #include "testing/gtest_mac.h" |
| 24 #include "testing/platform_test.h" | 25 #include "testing/platform_test.h" |
| 25 | 26 |
| 26 static const NSUInteger kSessionCount = 10; | 27 static const NSUInteger kSessionCount = 10; |
| 27 static const NSUInteger kSnapshotPixelSize = 8; | 28 static const NSUInteger kSnapshotPixelSize = 8; |
| 28 | 29 |
| 29 // Promote some implementation methods to public. | 30 // Promote some implementation methods to public. |
| 30 @interface SnapshotCache (Testing) | 31 @interface SnapshotCache (Testing) |
| 31 + (base::FilePath)imagePathForSessionID:(NSString*)sessionID; | 32 + (base::FilePath)imagePathForSessionID:(NSString*)sessionID; |
| 32 + (base::FilePath)greyImagePathForSessionID:(NSString*)sessionID; | 33 + (base::FilePath)greyImagePathForSessionID:(NSString*)sessionID; |
| 33 - (void)handleLowMemory; | 34 - (void)handleLowMemory; |
| 34 @end | 35 @end |
| 35 | 36 |
| 36 @interface SnapshotCache (TestingAdditions) | 37 @interface SnapshotCache (TestingAdditions) |
| 37 - (BOOL)hasImageInMemory:(NSString*)sessionID; | 38 - (BOOL)hasImageInMemory:(NSString*)sessionID; |
| 38 - (BOOL)hasGreyImageInMemory:(NSString*)sessionID; | 39 - (BOOL)hasGreyImageInMemory:(NSString*)sessionID; |
| 39 @end | 40 @end |
| 40 | 41 |
| 41 @implementation SnapshotCache (TestingAdditions) | 42 @implementation SnapshotCache (TestingAdditions) |
|
sdefresne
2015/11/03 11:25:19
Please avoid adding methods to the class in the te
jbbegue
2015/11/03 13:37:19
Done.
| |
| 42 - (BOOL)hasImageInMemory:(NSString*)sessionID { | 43 - (BOOL)hasImageInMemory:(NSString*)sessionID { |
| 43 return [imageDictionary_ objectForKey:sessionID] != nil; | 44 if (experimental_flags::IsLRUSnapshotCacheEnabled()) |
| 45 return [lruCache_ objectForKey:sessionID] != nil; | |
| 46 else | |
| 47 return [imageDictionary_ objectForKey:sessionID] != nil; | |
| 44 } | 48 } |
| 45 - (BOOL)hasGreyImageInMemory:(NSString*)sessionID { | 49 - (BOOL)hasGreyImageInMemory:(NSString*)sessionID { |
| 46 return [greyImageDictionary_ objectForKey:sessionID] != nil; | 50 return [greyImageDictionary_ objectForKey:sessionID] != nil; |
| 47 } | 51 } |
| 52 | |
| 53 - (NSUInteger)lruCacheMaxSize { | |
| 54 return [lruCache_ maxCacheSize]; | |
| 55 } | |
| 56 | |
| 48 @end | 57 @end |
| 49 | 58 |
| 50 namespace { | 59 namespace { |
| 51 | 60 |
| 52 class SnapshotCacheTest : public PlatformTest { | 61 class SnapshotCacheTest : public PlatformTest { |
| 53 protected: | 62 protected: |
| 54 // Build an array of session names and an array of UIImages filled with | 63 // Build an array of session names and an array of UIImages filled with |
| 55 // random colors. | 64 // random colors. |
| 56 void SetUp() override { | 65 void SetUp() override { |
| 57 PlatformTest::SetUp(); | 66 PlatformTest::SetUp(); |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 228 // This test also checks that images are correctly removed from the disk. | 237 // This test also checks that images are correctly removed from the disk. |
| 229 TEST_F(SnapshotCacheTest, Cache) { | 238 TEST_F(SnapshotCacheTest, Cache) { |
| 230 // Don't run on tablets because color snapshots are not cached so this test | 239 // Don't run on tablets because color snapshots are not cached so this test |
| 231 // can't compare the UIImage pointers directly. | 240 // can't compare the UIImage pointers directly. |
| 232 if (IsIPadIdiom()) { | 241 if (IsIPadIdiom()) { |
| 233 return; | 242 return; |
| 234 } | 243 } |
| 235 | 244 |
| 236 SnapshotCache* cache = GetSnapshotCache(); | 245 SnapshotCache* cache = GetSnapshotCache(); |
| 237 | 246 |
| 247 NSUInteger expectedCacheSize = kSessionCount; | |
| 248 if (experimental_flags::IsLRUSnapshotCacheEnabled()) | |
| 249 expectedCacheSize = MIN(kSessionCount, [cache lruCacheMaxSize]); | |
| 250 | |
| 238 // Put all images in the cache. | 251 // Put all images in the cache. |
| 239 for (NSUInteger i = 0; i < kSessionCount; ++i) { | 252 for (NSUInteger i = 0; i < expectedCacheSize; ++i) { |
| 240 UIImage* image = [testImages_ objectAtIndex:i]; | 253 UIImage* image = [testImages_ objectAtIndex:i]; |
| 241 NSString* sessionID = [testSessions_ objectAtIndex:i]; | 254 NSString* sessionID = [testSessions_ objectAtIndex:i]; |
| 242 [cache setImage:image withSessionID:sessionID]; | 255 [cache setImage:image withSessionID:sessionID]; |
| 243 } | 256 } |
| 244 | 257 |
| 245 // Get images back. | 258 // Get images back. |
| 246 __block NSUInteger numberOfCallbacks = 0; | 259 __block NSUInteger numberOfCallbacks = 0; |
| 247 for (NSUInteger i = 0; i < kSessionCount; ++i) { | 260 for (NSUInteger i = 0; i < expectedCacheSize; ++i) { |
| 248 NSString* sessionID = [testSessions_ objectAtIndex:i]; | 261 NSString* sessionID = [testSessions_ objectAtIndex:i]; |
| 249 UIImage* expectedImage = [testImages_ objectAtIndex:i]; | 262 UIImage* expectedImage = [testImages_ objectAtIndex:i]; |
| 250 EXPECT_TRUE(expectedImage != nil); | 263 EXPECT_TRUE(expectedImage != nil); |
| 251 [cache retrieveImageForSessionID:sessionID | 264 [cache retrieveImageForSessionID:sessionID |
| 252 callback:^(UIImage* image) { | 265 callback:^(UIImage* image) { |
| 253 // Images have not been removed from the | 266 // Images have not been removed from the |
| 254 // dictionnary. We expect the same pointer. | 267 // dictionnary. We expect the same pointer. |
| 255 EXPECT_EQ(expectedImage, image); | 268 EXPECT_EQ(expectedImage, image); |
| 256 ++numberOfCallbacks; | 269 ++numberOfCallbacks; |
| 257 }]; | 270 }]; |
| 258 } | 271 } |
| 259 EXPECT_EQ(kSessionCount, numberOfCallbacks); | 272 EXPECT_EQ(MIN(expectedCacheSize, [cache lruCacheMaxSize]), numberOfCallbacks); |
| 260 } | 273 } |
| 261 | 274 |
| 262 // This test puts all the snapshots in the cache and flushes them to disk. | 275 // This test puts all the snapshots in the cache and flushes them to disk. |
| 263 // The snapshots are then reloaded from the disk, and the colors are compared. | 276 // The snapshots are then reloaded from the disk, and the colors are compared. |
| 264 TEST_F(SnapshotCacheTest, SaveToDisk) { | 277 TEST_F(SnapshotCacheTest, SaveToDisk) { |
| 265 SnapshotCache* cache = GetSnapshotCache(); | 278 SnapshotCache* cache = GetSnapshotCache(); |
| 266 | 279 |
| 267 // Put all images in the cache. | 280 // Put all images in the cache. |
| 268 for (NSUInteger i = 0; i < kSessionCount; ++i) { | 281 for (NSUInteger i = 0; i < kSessionCount; ++i) { |
| 269 UIImage* image = [testImages_ objectAtIndex:i]; | 282 UIImage* image = [testImages_ objectAtIndex:i]; |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 591 EXPECT_TRUE(base::PathExists(retinaFile)); | 604 EXPECT_TRUE(base::PathExists(retinaFile)); |
| 592 | 605 |
| 593 // Delete the image. | 606 // Delete the image. |
| 594 [cache removeImageWithSessionID:kSession]; | 607 [cache removeImageWithSessionID:kSession]; |
| 595 FlushRunLoops(); // ensure the file is removed. | 608 FlushRunLoops(); // ensure the file is removed. |
| 596 | 609 |
| 597 EXPECT_FALSE(base::PathExists(retinaFile)); | 610 EXPECT_FALSE(base::PathExists(retinaFile)); |
| 598 } | 611 } |
| 599 | 612 |
| 600 } // namespace | 613 } // namespace |
| OLD | NEW |