| 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" |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 base::scoped_nsobject<NSMutableArray> testSessions_; | 209 base::scoped_nsobject<NSMutableArray> testSessions_; |
| 210 base::scoped_nsobject<NSMutableArray> testImages_; | 210 base::scoped_nsobject<NSMutableArray> testImages_; |
| 211 }; | 211 }; |
| 212 | 212 |
| 213 // This test simply put all the snapshots in the cache and then gets them back | 213 // This test simply put all the snapshots in the cache and then gets them back |
| 214 // As the snapshots are kept in memory, the same pointer can be retrieved. | 214 // As the snapshots are kept in memory, the same pointer can be retrieved. |
| 215 // 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. |
| 216 TEST_F(SnapshotCacheTest, Cache) { | 216 TEST_F(SnapshotCacheTest, Cache) { |
| 217 // 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 |
| 218 // can't compare the UIImage pointers directly. | 218 // can't compare the UIImage pointers directly. |
| 219 if (IsIPadIdiom()) { | 219 if (IsIPadIdiom() && !experimental_flags::IsTabSwitcherEnabled()) { |
| 220 return; | 220 return; |
| 221 } | 221 } |
| 222 | 222 |
| 223 SnapshotCache* cache = GetSnapshotCache(); | 223 SnapshotCache* cache = GetSnapshotCache(); |
| 224 | 224 |
| 225 NSUInteger expectedCacheSize = kSessionCount; | 225 NSUInteger expectedCacheSize = kSessionCount; |
| 226 if (experimental_flags::IsLRUSnapshotCacheEnabled()) | 226 if (experimental_flags::IsLRUSnapshotCacheEnabled()) |
| 227 expectedCacheSize = MIN(kSessionCount, [cache lruCacheMaxSize]); | 227 expectedCacheSize = MIN(kSessionCount, [cache lruCacheMaxSize]); |
| 228 | 228 |
| 229 // Put all images in the cache. | 229 // Put all images in the cache. |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 | 361 |
| 362 SnapshotCache* cache = GetSnapshotCache(); | 362 SnapshotCache* cache = GetSnapshotCache(); |
| 363 | 363 |
| 364 NSString* firstPinnedID = [testSessions_ objectAtIndex:4]; | 364 NSString* firstPinnedID = [testSessions_ objectAtIndex:4]; |
| 365 NSString* secondPinnedID = [testSessions_ objectAtIndex:6]; | 365 NSString* secondPinnedID = [testSessions_ objectAtIndex:6]; |
| 366 NSMutableSet* set = [NSMutableSet set]; | 366 NSMutableSet* set = [NSMutableSet set]; |
| 367 [set addObject:firstPinnedID]; | 367 [set addObject:firstPinnedID]; |
| 368 [set addObject:secondPinnedID]; | 368 [set addObject:secondPinnedID]; |
| 369 cache.pinnedIDs = set; | 369 cache.pinnedIDs = set; |
| 370 | 370 |
| 371 if (!IsIPadIdiom()) | 371 if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) |
| 372 [cache handleLowMemory]; | 372 [cache handleLowMemory]; |
| 373 | 373 |
| 374 BOOL expectedValue = YES; | 374 BOOL expectedValue = YES; |
| 375 if (IsIPadIdiom()) { | 375 if (IsIPadIdiom() && !experimental_flags::IsTabSwitcherEnabled()) |
| 376 expectedValue = NO; | 376 expectedValue = NO; |
| 377 } | 377 |
| 378 EXPECT_EQ(expectedValue, [cache hasImageInMemory:firstPinnedID]); | 378 EXPECT_EQ(expectedValue, [cache hasImageInMemory:firstPinnedID]); |
| 379 EXPECT_EQ(expectedValue, [cache hasImageInMemory:secondPinnedID]); | 379 EXPECT_EQ(expectedValue, [cache hasImageInMemory:secondPinnedID]); |
| 380 | 380 |
| 381 NSString* notPinnedID = [testSessions_ objectAtIndex:2]; | 381 NSString* notPinnedID = [testSessions_ objectAtIndex:2]; |
| 382 EXPECT_FALSE([cache hasImageInMemory:notPinnedID]); | 382 EXPECT_FALSE([cache hasImageInMemory:notPinnedID]); |
| 383 | 383 |
| 384 // Wait for the final image to be pulled off disk. | 384 // Wait for the final image to be pulled off disk. |
| 385 FlushRunLoops(); | 385 FlushRunLoops(); |
| 386 } | 386 } |
| 387 | 387 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 416 | 416 |
| 417 // Same as previous test, except that all the color images are on disk, | 417 // Same as previous test, except that all the color images are on disk, |
| 418 // rather than in memory. | 418 // rather than in memory. |
| 419 // Disabled due to the greyImage crash. b/8048597 | 419 // Disabled due to the greyImage crash. b/8048597 |
| 420 TEST_F(SnapshotCacheTest, CreateGreyCacheFromDisk) { | 420 TEST_F(SnapshotCacheTest, CreateGreyCacheFromDisk) { |
| 421 LoadAllColorImagesIntoCache(true); | 421 LoadAllColorImagesIntoCache(true); |
| 422 | 422 |
| 423 // Remove color images from in-memory cache. | 423 // Remove color images from in-memory cache. |
| 424 SnapshotCache* cache = GetSnapshotCache(); | 424 SnapshotCache* cache = GetSnapshotCache(); |
| 425 | 425 |
| 426 if (!IsIPadIdiom()) | 426 if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) |
| 427 [cache handleLowMemory]; | 427 [cache handleLowMemory]; |
| 428 | 428 |
| 429 // Request the creation of a grey image cache for all images. | 429 // Request the creation of a grey image cache for all images. |
| 430 [cache createGreyCache:testSessions_]; | 430 [cache createGreyCache:testSessions_]; |
| 431 | 431 |
| 432 // Wait for them to be put into the grey image cache. | 432 // Wait for them to be put into the grey image cache. |
| 433 WaitForGreyImagesInCache(kSessionCount); | 433 WaitForGreyImagesInCache(kSessionCount); |
| 434 | 434 |
| 435 __block NSUInteger numberOfCallbacks = 0; | 435 __block NSUInteger numberOfCallbacks = 0; |
| 436 for (NSUInteger i = 0; i < kSessionCount; ++i) { | 436 for (NSUInteger i = 0; i < kSessionCount; ++i) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 458 [sessionIDs addObject:[testSessions_ objectAtIndex:0]]; | 458 [sessionIDs addObject:[testSessions_ objectAtIndex:0]]; |
| 459 [sessionIDs addObject:[testSessions_ objectAtIndex:1]]; | 459 [sessionIDs addObject:[testSessions_ objectAtIndex:1]]; |
| 460 [sessionIDs addObject:[testSessions_ objectAtIndex:2]]; | 460 [sessionIDs addObject:[testSessions_ objectAtIndex:2]]; |
| 461 | 461 |
| 462 SnapshotCache* cache = GetSnapshotCache(); | 462 SnapshotCache* cache = GetSnapshotCache(); |
| 463 | 463 |
| 464 // Put 3 images in the cache. | 464 // Put 3 images in the cache. |
| 465 LoadColorImagesIntoCache(kNumImages, true); | 465 LoadColorImagesIntoCache(kNumImages, true); |
| 466 // Make sure the color images are only on disk, to ensure the background | 466 // Make sure the color images are only on disk, to ensure the background |
| 467 // thread is slow enough to queue up the requests. | 467 // thread is slow enough to queue up the requests. |
| 468 if (!IsIPadIdiom()) | 468 if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) |
| 469 [cache handleLowMemory]; | 469 [cache handleLowMemory]; |
| 470 | 470 |
| 471 // Enable the grey image cache. | 471 // Enable the grey image cache. |
| 472 [cache createGreyCache:sessionIDs]; | 472 [cache createGreyCache:sessionIDs]; |
| 473 | 473 |
| 474 // Request the grey versions | 474 // Request the grey versions |
| 475 __block BOOL firstCallbackCalled = NO; | 475 __block BOOL firstCallbackCalled = NO; |
| 476 __block BOOL secondCallbackCalled = NO; | 476 __block BOOL secondCallbackCalled = NO; |
| 477 __block BOOL thirdCallbackCalled = NO; | 477 __block BOOL thirdCallbackCalled = NO; |
| 478 [cache greyImageForSessionID:[testSessions_ objectAtIndex:0] | 478 [cache greyImageForSessionID:[testSessions_ objectAtIndex:0] |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 CGContextRef context = UIGraphicsGetCurrentContext(); | 530 CGContextRef context = UIGraphicsGetCurrentContext(); |
| 531 UIImage* image = GenerateRandomImage(context); | 531 UIImage* image = GenerateRandomImage(context); |
| 532 UIGraphicsEndImageContext(); | 532 UIGraphicsEndImageContext(); |
| 533 | 533 |
| 534 // Add the image to the cache then call handle low memory to ensure the image | 534 // Add the image to the cache then call handle low memory to ensure the image |
| 535 // is read from disk instead of the in-memory cache. | 535 // is read from disk instead of the in-memory cache. |
| 536 SnapshotCache* cache = GetSnapshotCache(); | 536 SnapshotCache* cache = GetSnapshotCache(); |
| 537 NSString* const kSession = @"foo"; | 537 NSString* const kSession = @"foo"; |
| 538 [cache setImage:image withSessionID:kSession]; | 538 [cache setImage:image withSessionID:kSession]; |
| 539 FlushRunLoops(); // ensure the file is written to disk. | 539 FlushRunLoops(); // ensure the file is written to disk. |
| 540 if (!IsIPadIdiom()) | 540 if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) |
| 541 [cache handleLowMemory]; | 541 [cache handleLowMemory]; |
| 542 | 542 |
| 543 // Retrive the image and have the callback verify the size and scale. | 543 // Retrive the image and have the callback verify the size and scale. |
| 544 __block BOOL callbackComplete = NO; | 544 __block BOOL callbackComplete = NO; |
| 545 [cache retrieveImageForSessionID:kSession | 545 [cache retrieveImageForSessionID:kSession |
| 546 callback:^(UIImage* imageFromDisk) { | 546 callback:^(UIImage* imageFromDisk) { |
| 547 EXPECT_EQ(image.size.width, | 547 EXPECT_EQ(image.size.width, |
| 548 imageFromDisk.size.width); | 548 imageFromDisk.size.width); |
| 549 EXPECT_EQ(image.size.height, | 549 EXPECT_EQ(image.size.height, |
| 550 imageFromDisk.size.height); | 550 imageFromDisk.size.height); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 567 CGContextRef context = UIGraphicsGetCurrentContext(); | 567 CGContextRef context = UIGraphicsGetCurrentContext(); |
| 568 UIImage* image = GenerateRandomImage(context); | 568 UIImage* image = GenerateRandomImage(context); |
| 569 UIGraphicsEndImageContext(); | 569 UIGraphicsEndImageContext(); |
| 570 | 570 |
| 571 // Add the image to the cache then call handle low memory to ensure the image | 571 // Add the image to the cache then call handle low memory to ensure the image |
| 572 // is read from disk instead of the in-memory cache. | 572 // is read from disk instead of the in-memory cache. |
| 573 SnapshotCache* cache = GetSnapshotCache(); | 573 SnapshotCache* cache = GetSnapshotCache(); |
| 574 NSString* const kSession = @"foo"; | 574 NSString* const kSession = @"foo"; |
| 575 [cache setImage:image withSessionID:kSession]; | 575 [cache setImage:image withSessionID:kSession]; |
| 576 FlushRunLoops(); // ensure the file is written to disk. | 576 FlushRunLoops(); // ensure the file is written to disk. |
| 577 if (!IsIPadIdiom()) | 577 if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) |
| 578 [cache handleLowMemory]; | 578 [cache handleLowMemory]; |
| 579 | 579 |
| 580 // Verify the file was writted with @2x in the file name. | 580 // Verify the file was writted with @2x in the file name. |
| 581 base::FilePath retinaFile = [SnapshotCache imagePathForSessionID:kSession]; | 581 base::FilePath retinaFile = [SnapshotCache imagePathForSessionID:kSession]; |
| 582 EXPECT_TRUE(base::PathExists(retinaFile)); | 582 EXPECT_TRUE(base::PathExists(retinaFile)); |
| 583 | 583 |
| 584 // Delete the image. | 584 // Delete the image. |
| 585 [cache removeImageWithSessionID:kSession]; | 585 [cache removeImageWithSessionID:kSession]; |
| 586 FlushRunLoops(); // ensure the file is removed. | 586 FlushRunLoops(); // ensure the file is removed. |
| 587 | 587 |
| 588 EXPECT_FALSE(base::PathExists(retinaFile)); | 588 EXPECT_FALSE(base::PathExists(retinaFile)); |
| 589 } | 589 } |
| 590 | 590 |
| 591 } // namespace | 591 } // namespace |
| OLD | NEW |