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" | |
13 #include "base/location.h" | 12 #include "base/location.h" |
14 #include "base/mac/bind_objc_block.h" | 13 #include "base/mac/bind_objc_block.h" |
15 #include "base/mac/scoped_nsautorelease_pool.h" | 14 #include "base/mac/scoped_nsautorelease_pool.h" |
16 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
17 #include "base/strings/sys_string_conversions.h" | 16 #include "base/strings/sys_string_conversions.h" |
18 #include "base/time/time.h" | 17 #include "base/time/time.h" |
19 #include "ios/chrome/browser/experimental_flags.h" | 18 #import "ios/chrome/browser/snapshots/snapshot_cache_internal.h" |
20 #include "ios/chrome/browser/ui/ui_util.h" | |
21 #include "ios/web/public/test/test_web_thread_bundle.h" | 19 #include "ios/web/public/test/test_web_thread_bundle.h" |
22 #include "ios/web/public/web_thread.h" | 20 #include "ios/web/public/web_thread.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
24 #include "testing/gtest_mac.h" | 22 #include "testing/gtest_mac.h" |
25 #include "testing/platform_test.h" | 23 #include "testing/platform_test.h" |
26 | 24 |
27 static const NSUInteger kSessionCount = 10; | 25 static const NSUInteger kSessionCount = 10; |
28 static const NSUInteger kSnapshotPixelSize = 8; | 26 static const NSUInteger kSnapshotPixelSize = 8; |
29 | 27 |
30 // Promote some implementation methods to public. | |
31 @interface SnapshotCache (Testing) | |
32 + (base::FilePath)imagePathForSessionID:(NSString*)sessionID; | |
33 + (base::FilePath)greyImagePathForSessionID:(NSString*)sessionID; | |
34 - (void)handleLowMemory; | |
35 @end | |
36 | 28 |
37 namespace { | 29 namespace { |
38 | 30 |
39 class SnapshotCacheTest : public PlatformTest { | 31 class SnapshotCacheTest : public PlatformTest { |
40 protected: | 32 protected: |
41 // Build an array of session names and an array of UIImages filled with | 33 // Build an array of session names and an array of UIImages filled with |
42 // random colors. | 34 // random colors. |
43 void SetUp() override { | 35 void SetUp() override { |
44 PlatformTest::SetUp(); | 36 PlatformTest::SetUp(); |
45 snapshotCache_.reset([[SnapshotCache alloc] init]); | 37 snapshotCache_.reset([[SnapshotCache alloc] init]); |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 *green = lastChannel - *green; | 189 *green = lastChannel - *green; |
198 *blue = lastChannel - *blue; | 190 *blue = lastChannel - *blue; |
199 } | 191 } |
200 } | 192 } |
201 | 193 |
202 const char* GetPixelData(CGImageRef cgImage) { | 194 const char* GetPixelData(CGImageRef cgImage) { |
203 CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(cgImage)); | 195 CFDataRef data = CGDataProviderCopyData(CGImageGetDataProvider(cgImage)); |
204 return reinterpret_cast<const char*>(CFDataGetBytePtr(data)); | 196 return reinterpret_cast<const char*>(CFDataGetBytePtr(data)); |
205 } | 197 } |
206 | 198 |
| 199 void TriggerMemoryWarning() { |
| 200 // _performMemoryWarning is a private API and musn't be compiled into |
| 201 // official builds. |
| 202 [[UIApplication sharedApplication] |
| 203 performSelector:@selector(_performMemoryWarning)]; |
| 204 } |
| 205 |
207 web::TestWebThreadBundle thread_bundle_; | 206 web::TestWebThreadBundle thread_bundle_; |
208 base::scoped_nsobject<SnapshotCache> snapshotCache_; | 207 base::scoped_nsobject<SnapshotCache> snapshotCache_; |
209 base::scoped_nsobject<NSMutableArray> testSessions_; | 208 base::scoped_nsobject<NSMutableArray> testSessions_; |
210 base::scoped_nsobject<NSMutableArray> testImages_; | 209 base::scoped_nsobject<NSMutableArray> testImages_; |
211 }; | 210 }; |
212 | 211 |
213 // This test simply put all the snapshots in the cache and then gets them back | 212 // 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. | 213 // 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. | 214 // This test also checks that images are correctly removed from the disk. |
216 TEST_F(SnapshotCacheTest, Cache) { | 215 TEST_F(SnapshotCacheTest, Cache) { |
217 // Don't run on tablets because color snapshots are not cached so this test | |
218 // can't compare the UIImage pointers directly. | |
219 if (IsIPadIdiom() && !experimental_flags::IsTabSwitcherEnabled()) { | |
220 return; | |
221 } | |
222 | |
223 SnapshotCache* cache = GetSnapshotCache(); | 216 SnapshotCache* cache = GetSnapshotCache(); |
224 | 217 |
| 218 if (![cache inMemoryCacheIsEnabled]) |
| 219 return; |
| 220 |
225 NSUInteger expectedCacheSize = kSessionCount; | 221 NSUInteger expectedCacheSize = kSessionCount; |
226 if (experimental_flags::IsLRUSnapshotCacheEnabled()) | 222 if ([cache usesLRUCache]) |
227 expectedCacheSize = MIN(kSessionCount, [cache lruCacheMaxSize]); | 223 expectedCacheSize = MIN(kSessionCount, [cache lruCacheMaxSize]); |
228 | 224 |
229 // Put all images in the cache. | 225 // Put all images in the cache. |
230 for (NSUInteger i = 0; i < expectedCacheSize; ++i) { | 226 for (NSUInteger i = 0; i < expectedCacheSize; ++i) { |
231 UIImage* image = [testImages_ objectAtIndex:i]; | 227 UIImage* image = [testImages_ objectAtIndex:i]; |
232 NSString* sessionID = [testSessions_ objectAtIndex:i]; | 228 NSString* sessionID = [testSessions_ objectAtIndex:i]; |
233 [cache setImage:image withSessionID:sessionID]; | 229 [cache setImage:image withSessionID:sessionID]; |
234 } | 230 } |
235 | 231 |
236 // Get images back. | 232 // Get images back. |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 | 336 |
341 base::FilePath path([SnapshotCache imagePathForSessionID:sessionID]); | 337 base::FilePath path([SnapshotCache imagePathForSessionID:sessionID]); |
342 if (i == 0) | 338 if (i == 0) |
343 EXPECT_TRUE(base::PathExists(path)); | 339 EXPECT_TRUE(base::PathExists(path)); |
344 else | 340 else |
345 EXPECT_FALSE(base::PathExists(path)); | 341 EXPECT_FALSE(base::PathExists(path)); |
346 } | 342 } |
347 } | 343 } |
348 | 344 |
349 // Loads the color images into the cache, and pins two of them. Ensures that | 345 // Loads the color images into the cache, and pins two of them. Ensures that |
350 // only the two pinned IDs remain in memory after a call to -handleLowMemory. | 346 // only the two pinned IDs remain in memory after a memory warning. |
351 TEST_F(SnapshotCacheTest, HandleLowMemory) { | 347 TEST_F(SnapshotCacheTest, HandleMemoryWarning) { |
352 // TODO(droger): This test fails on iPad iOS8 device: http://crbug.com/455209 | |
353 #if !TARGET_IPHONE_SIMULATOR | |
354 if (IsIPadIdiom() && base::ios::IsRunningOnIOS8OrLater()) { | |
355 LOG(WARNING) << "Test disabled on iPad iOS8 device."; | |
356 return; | |
357 } | |
358 #endif | |
359 | |
360 LoadAllColorImagesIntoCache(true); | 348 LoadAllColorImagesIntoCache(true); |
361 | 349 |
362 SnapshotCache* cache = GetSnapshotCache(); | 350 SnapshotCache* cache = GetSnapshotCache(); |
363 | 351 |
364 NSString* firstPinnedID = [testSessions_ objectAtIndex:4]; | 352 NSString* firstPinnedID = [testSessions_ objectAtIndex:4]; |
365 NSString* secondPinnedID = [testSessions_ objectAtIndex:6]; | 353 NSString* secondPinnedID = [testSessions_ objectAtIndex:6]; |
366 NSMutableSet* set = [NSMutableSet set]; | 354 NSMutableSet* set = [NSMutableSet set]; |
367 [set addObject:firstPinnedID]; | 355 [set addObject:firstPinnedID]; |
368 [set addObject:secondPinnedID]; | 356 [set addObject:secondPinnedID]; |
369 cache.pinnedIDs = set; | 357 cache.pinnedIDs = set; |
370 | 358 |
371 if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) | 359 TriggerMemoryWarning(); |
372 [cache handleLowMemory]; | |
373 | 360 |
374 BOOL expectedValue = YES; | 361 EXPECT_EQ(YES, [cache hasImageInMemory:firstPinnedID]); |
375 if (IsIPadIdiom() && !experimental_flags::IsTabSwitcherEnabled()) | 362 EXPECT_EQ(YES, [cache hasImageInMemory:secondPinnedID]); |
376 expectedValue = NO; | |
377 | |
378 EXPECT_EQ(expectedValue, [cache hasImageInMemory:firstPinnedID]); | |
379 EXPECT_EQ(expectedValue, [cache hasImageInMemory:secondPinnedID]); | |
380 | 363 |
381 NSString* notPinnedID = [testSessions_ objectAtIndex:2]; | 364 NSString* notPinnedID = [testSessions_ objectAtIndex:2]; |
382 EXPECT_FALSE([cache hasImageInMemory:notPinnedID]); | 365 EXPECT_FALSE([cache hasImageInMemory:notPinnedID]); |
383 | 366 |
384 // Wait for the final image to be pulled off disk. | 367 // Wait for the final image to be pulled off disk. |
385 FlushRunLoops(); | 368 FlushRunLoops(); |
386 } | 369 } |
387 | 370 |
388 // Tests that createGreyCache creates the grey snapshots in the background, | 371 // Tests that createGreyCache creates the grey snapshots in the background, |
389 // from color images in the in-memory cache. When the grey images are all | 372 // from color images in the in-memory cache. When the grey images are all |
(...skipping 26 matching lines...) Expand all Loading... |
416 | 399 |
417 // Same as previous test, except that all the color images are on disk, | 400 // Same as previous test, except that all the color images are on disk, |
418 // rather than in memory. | 401 // rather than in memory. |
419 // Disabled due to the greyImage crash. b/8048597 | 402 // Disabled due to the greyImage crash. b/8048597 |
420 TEST_F(SnapshotCacheTest, CreateGreyCacheFromDisk) { | 403 TEST_F(SnapshotCacheTest, CreateGreyCacheFromDisk) { |
421 LoadAllColorImagesIntoCache(true); | 404 LoadAllColorImagesIntoCache(true); |
422 | 405 |
423 // Remove color images from in-memory cache. | 406 // Remove color images from in-memory cache. |
424 SnapshotCache* cache = GetSnapshotCache(); | 407 SnapshotCache* cache = GetSnapshotCache(); |
425 | 408 |
426 if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) | 409 TriggerMemoryWarning(); |
427 [cache handleLowMemory]; | |
428 | 410 |
429 // Request the creation of a grey image cache for all images. | 411 // Request the creation of a grey image cache for all images. |
430 [cache createGreyCache:testSessions_]; | 412 [cache createGreyCache:testSessions_]; |
431 | 413 |
432 // Wait for them to be put into the grey image cache. | 414 // Wait for them to be put into the grey image cache. |
433 WaitForGreyImagesInCache(kSessionCount); | 415 WaitForGreyImagesInCache(kSessionCount); |
434 | 416 |
435 __block NSUInteger numberOfCallbacks = 0; | 417 __block NSUInteger numberOfCallbacks = 0; |
436 for (NSUInteger i = 0; i < kSessionCount; ++i) { | 418 for (NSUInteger i = 0; i < kSessionCount; ++i) { |
437 NSString* sessionID = [testSessions_ objectAtIndex:i]; | 419 NSString* sessionID = [testSessions_ objectAtIndex:i]; |
(...skipping 20 matching lines...) Expand all Loading... |
458 [sessionIDs addObject:[testSessions_ objectAtIndex:0]]; | 440 [sessionIDs addObject:[testSessions_ objectAtIndex:0]]; |
459 [sessionIDs addObject:[testSessions_ objectAtIndex:1]]; | 441 [sessionIDs addObject:[testSessions_ objectAtIndex:1]]; |
460 [sessionIDs addObject:[testSessions_ objectAtIndex:2]]; | 442 [sessionIDs addObject:[testSessions_ objectAtIndex:2]]; |
461 | 443 |
462 SnapshotCache* cache = GetSnapshotCache(); | 444 SnapshotCache* cache = GetSnapshotCache(); |
463 | 445 |
464 // Put 3 images in the cache. | 446 // Put 3 images in the cache. |
465 LoadColorImagesIntoCache(kNumImages, true); | 447 LoadColorImagesIntoCache(kNumImages, true); |
466 // Make sure the color images are only on disk, to ensure the background | 448 // Make sure the color images are only on disk, to ensure the background |
467 // thread is slow enough to queue up the requests. | 449 // thread is slow enough to queue up the requests. |
468 if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) | 450 TriggerMemoryWarning(); |
469 [cache handleLowMemory]; | |
470 | 451 |
471 // Enable the grey image cache. | 452 // Enable the grey image cache. |
472 [cache createGreyCache:sessionIDs]; | 453 [cache createGreyCache:sessionIDs]; |
473 | 454 |
474 // Request the grey versions | 455 // Request the grey versions |
475 __block BOOL firstCallbackCalled = NO; | 456 __block BOOL firstCallbackCalled = NO; |
476 __block BOOL secondCallbackCalled = NO; | 457 __block BOOL secondCallbackCalled = NO; |
477 __block BOOL thirdCallbackCalled = NO; | 458 __block BOOL thirdCallbackCalled = NO; |
478 [cache greyImageForSessionID:[testSessions_ objectAtIndex:0] | 459 [cache greyImageForSessionID:[testSessions_ objectAtIndex:0] |
479 callback:^(UIImage*) { | 460 callback:^(UIImage*) { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
530 CGContextRef context = UIGraphicsGetCurrentContext(); | 511 CGContextRef context = UIGraphicsGetCurrentContext(); |
531 UIImage* image = GenerateRandomImage(context); | 512 UIImage* image = GenerateRandomImage(context); |
532 UIGraphicsEndImageContext(); | 513 UIGraphicsEndImageContext(); |
533 | 514 |
534 // Add the image to the cache then call handle low memory to ensure the image | 515 // 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. | 516 // is read from disk instead of the in-memory cache. |
536 SnapshotCache* cache = GetSnapshotCache(); | 517 SnapshotCache* cache = GetSnapshotCache(); |
537 NSString* const kSession = @"foo"; | 518 NSString* const kSession = @"foo"; |
538 [cache setImage:image withSessionID:kSession]; | 519 [cache setImage:image withSessionID:kSession]; |
539 FlushRunLoops(); // ensure the file is written to disk. | 520 FlushRunLoops(); // ensure the file is written to disk. |
540 if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) | 521 TriggerMemoryWarning(); |
541 [cache handleLowMemory]; | |
542 | 522 |
543 // Retrive the image and have the callback verify the size and scale. | 523 // Retrive the image and have the callback verify the size and scale. |
544 __block BOOL callbackComplete = NO; | 524 __block BOOL callbackComplete = NO; |
545 [cache retrieveImageForSessionID:kSession | 525 [cache retrieveImageForSessionID:kSession |
546 callback:^(UIImage* imageFromDisk) { | 526 callback:^(UIImage* imageFromDisk) { |
547 EXPECT_EQ(image.size.width, | 527 EXPECT_EQ(image.size.width, |
548 imageFromDisk.size.width); | 528 imageFromDisk.size.width); |
549 EXPECT_EQ(image.size.height, | 529 EXPECT_EQ(image.size.height, |
550 imageFromDisk.size.height); | 530 imageFromDisk.size.height); |
551 EXPECT_EQ(image.scale, imageFromDisk.scale); | 531 EXPECT_EQ(image.scale, imageFromDisk.scale); |
(...skipping 15 matching lines...) Expand all Loading... |
567 CGContextRef context = UIGraphicsGetCurrentContext(); | 547 CGContextRef context = UIGraphicsGetCurrentContext(); |
568 UIImage* image = GenerateRandomImage(context); | 548 UIImage* image = GenerateRandomImage(context); |
569 UIGraphicsEndImageContext(); | 549 UIGraphicsEndImageContext(); |
570 | 550 |
571 // Add the image to the cache then call handle low memory to ensure the image | 551 // 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. | 552 // is read from disk instead of the in-memory cache. |
573 SnapshotCache* cache = GetSnapshotCache(); | 553 SnapshotCache* cache = GetSnapshotCache(); |
574 NSString* const kSession = @"foo"; | 554 NSString* const kSession = @"foo"; |
575 [cache setImage:image withSessionID:kSession]; | 555 [cache setImage:image withSessionID:kSession]; |
576 FlushRunLoops(); // ensure the file is written to disk. | 556 FlushRunLoops(); // ensure the file is written to disk. |
577 if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) | 557 TriggerMemoryWarning(); |
578 [cache handleLowMemory]; | |
579 | 558 |
580 // Verify the file was writted with @2x in the file name. | 559 // Verify the file was writted with @2x in the file name. |
581 base::FilePath retinaFile = [SnapshotCache imagePathForSessionID:kSession]; | 560 base::FilePath retinaFile = [SnapshotCache imagePathForSessionID:kSession]; |
582 EXPECT_TRUE(base::PathExists(retinaFile)); | 561 EXPECT_TRUE(base::PathExists(retinaFile)); |
583 | 562 |
584 // Delete the image. | 563 // Delete the image. |
585 [cache removeImageWithSessionID:kSession]; | 564 [cache removeImageWithSessionID:kSession]; |
586 FlushRunLoops(); // ensure the file is removed. | 565 FlushRunLoops(); // ensure the file is removed. |
587 | 566 |
588 EXPECT_FALSE(base::PathExists(retinaFile)); | 567 EXPECT_FALSE(base::PathExists(retinaFile)); |
589 } | 568 } |
590 | 569 |
591 } // namespace | 570 } // namespace |
OLD | NEW |