| 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 - (BOOL)hasImageInMemory:(NSString*)sessionID; | |
| 38 - (BOOL)hasGreyImageInMemory:(NSString*)sessionID; | |
| 39 @end | |
| 40 | |
| 41 @implementation SnapshotCache (TestingAdditions) | |
| 42 - (BOOL)hasImageInMemory:(NSString*)sessionID { | |
| 43 return [imageDictionary_ objectForKey:sessionID] != nil; | |
| 44 } | |
| 45 - (BOOL)hasGreyImageInMemory:(NSString*)sessionID { | |
| 46 return [greyImageDictionary_ objectForKey:sessionID] != nil; | |
| 47 } | |
| 48 @end | |
| 49 | |
| 50 namespace { | 37 namespace { |
| 51 | 38 |
| 52 class SnapshotCacheTest : public PlatformTest { | 39 class SnapshotCacheTest : public PlatformTest { |
| 53 protected: | 40 protected: |
| 54 // Build an array of session names and an array of UIImages filled with | 41 // Build an array of session names and an array of UIImages filled with |
| 55 // random colors. | 42 // random colors. |
| 56 void SetUp() override { | 43 void SetUp() override { |
| 57 PlatformTest::SetUp(); | 44 PlatformTest::SetUp(); |
| 58 snapshotCache_.reset([[SnapshotCache alloc] init]); | 45 snapshotCache_.reset([[SnapshotCache alloc] init]); |
| 59 testImages_.reset([[NSMutableArray alloc] initWithCapacity:kSessionCount]); | 46 testImages_.reset([[NSMutableArray alloc] initWithCapacity:kSessionCount]); |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 // This test also checks that images are correctly removed from the disk. | 215 // This test also checks that images are correctly removed from the disk. |
| 229 TEST_F(SnapshotCacheTest, Cache) { | 216 TEST_F(SnapshotCacheTest, Cache) { |
| 230 // Don't run on tablets because color snapshots are not cached so this test | 217 // Don't run on tablets because color snapshots are not cached so this test |
| 231 // can't compare the UIImage pointers directly. | 218 // can't compare the UIImage pointers directly. |
| 232 if (IsIPadIdiom()) { | 219 if (IsIPadIdiom()) { |
| 233 return; | 220 return; |
| 234 } | 221 } |
| 235 | 222 |
| 236 SnapshotCache* cache = GetSnapshotCache(); | 223 SnapshotCache* cache = GetSnapshotCache(); |
| 237 | 224 |
| 225 NSUInteger expectedCacheSize = kSessionCount; |
| 226 if (experimental_flags::IsLRUSnapshotCacheEnabled()) |
| 227 expectedCacheSize = MIN(kSessionCount, [cache lruCacheMaxSize]); |
| 228 |
| 238 // Put all images in the cache. | 229 // Put all images in the cache. |
| 239 for (NSUInteger i = 0; i < kSessionCount; ++i) { | 230 for (NSUInteger i = 0; i < expectedCacheSize; ++i) { |
| 240 UIImage* image = [testImages_ objectAtIndex:i]; | 231 UIImage* image = [testImages_ objectAtIndex:i]; |
| 241 NSString* sessionID = [testSessions_ objectAtIndex:i]; | 232 NSString* sessionID = [testSessions_ objectAtIndex:i]; |
| 242 [cache setImage:image withSessionID:sessionID]; | 233 [cache setImage:image withSessionID:sessionID]; |
| 243 } | 234 } |
| 244 | 235 |
| 245 // Get images back. | 236 // Get images back. |
| 246 __block NSUInteger numberOfCallbacks = 0; | 237 __block NSUInteger numberOfCallbacks = 0; |
| 247 for (NSUInteger i = 0; i < kSessionCount; ++i) { | 238 for (NSUInteger i = 0; i < expectedCacheSize; ++i) { |
| 248 NSString* sessionID = [testSessions_ objectAtIndex:i]; | 239 NSString* sessionID = [testSessions_ objectAtIndex:i]; |
| 249 UIImage* expectedImage = [testImages_ objectAtIndex:i]; | 240 UIImage* expectedImage = [testImages_ objectAtIndex:i]; |
| 250 EXPECT_TRUE(expectedImage != nil); | 241 EXPECT_TRUE(expectedImage != nil); |
| 251 [cache retrieveImageForSessionID:sessionID | 242 [cache retrieveImageForSessionID:sessionID |
| 252 callback:^(UIImage* image) { | 243 callback:^(UIImage* image) { |
| 253 // Images have not been removed from the | 244 // Images have not been removed from the |
| 254 // dictionnary. We expect the same pointer. | 245 // dictionnary. We expect the same pointer. |
| 255 EXPECT_EQ(expectedImage, image); | 246 EXPECT_EQ(expectedImage, image); |
| 256 ++numberOfCallbacks; | 247 ++numberOfCallbacks; |
| 257 }]; | 248 }]; |
| 258 } | 249 } |
| 259 EXPECT_EQ(kSessionCount, numberOfCallbacks); | 250 EXPECT_EQ(expectedCacheSize, numberOfCallbacks); |
| 260 } | 251 } |
| 261 | 252 |
| 262 // This test puts all the snapshots in the cache and flushes them to disk. | 253 // 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. | 254 // The snapshots are then reloaded from the disk, and the colors are compared. |
| 264 TEST_F(SnapshotCacheTest, SaveToDisk) { | 255 TEST_F(SnapshotCacheTest, SaveToDisk) { |
| 265 SnapshotCache* cache = GetSnapshotCache(); | 256 SnapshotCache* cache = GetSnapshotCache(); |
| 266 | 257 |
| 267 // Put all images in the cache. | 258 // Put all images in the cache. |
| 268 for (NSUInteger i = 0; i < kSessionCount; ++i) { | 259 for (NSUInteger i = 0; i < kSessionCount; ++i) { |
| 269 UIImage* image = [testImages_ objectAtIndex:i]; | 260 UIImage* image = [testImages_ objectAtIndex:i]; |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 591 EXPECT_TRUE(base::PathExists(retinaFile)); | 582 EXPECT_TRUE(base::PathExists(retinaFile)); |
| 592 | 583 |
| 593 // Delete the image. | 584 // Delete the image. |
| 594 [cache removeImageWithSessionID:kSession]; | 585 [cache removeImageWithSessionID:kSession]; |
| 595 FlushRunLoops(); // ensure the file is removed. | 586 FlushRunLoops(); // ensure the file is removed. |
| 596 | 587 |
| 597 EXPECT_FALSE(base::PathExists(retinaFile)); | 588 EXPECT_FALSE(base::PathExists(retinaFile)); |
| 598 } | 589 } |
| 599 | 590 |
| 600 } // namespace | 591 } // namespace |
| OLD | NEW |