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 #include "ios/chrome/browser/experimental_flags.h" |
20 #include "ios/chrome/browser/ui/ui_util.h" | 19 #include "ios/chrome/browser/ui/ui_util.h" |
21 #include "ios/web/public/test/test_web_thread_bundle.h" | 20 #include "ios/web/public/test/test_web_thread_bundle.h" |
22 #include "ios/web/public/web_thread.h" | 21 #include "ios/web/public/web_thread.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
24 #include "testing/gtest_mac.h" | 23 #include "testing/gtest_mac.h" |
25 #include "testing/platform_test.h" | 24 #include "testing/platform_test.h" |
26 | 25 |
27 static const NSUInteger kSessionCount = 10; | 26 static const NSUInteger kSessionCount = 10; |
28 static const NSUInteger kSnapshotPixelSize = 8; | 27 static const NSUInteger kSnapshotPixelSize = 8; |
29 | 28 |
30 // Promote some implementation methods to public. | 29 // Promote some implementation methods to public. |
sdefresne
2016/02/25 10:08:09
This is an anti-pattern. Please move this to snaps
jif
2016/02/25 12:42:28
Done.
| |
31 @interface SnapshotCache (Testing) | 30 @interface SnapshotCache (Testing) |
32 + (base::FilePath)imagePathForSessionID:(NSString*)sessionID; | 31 + (base::FilePath)imagePathForSessionID:(NSString*)sessionID; |
33 + (base::FilePath)greyImagePathForSessionID:(NSString*)sessionID; | 32 + (base::FilePath)greyImagePathForSessionID:(NSString*)sessionID; |
33 - (BOOL)cacheIsEnabled; | |
34 - (BOOL)useLRUCache; | |
34 - (void)handleLowMemory; | 35 - (void)handleLowMemory; |
35 @end | 36 @end |
36 | 37 |
37 namespace { | 38 namespace { |
38 | 39 |
39 class SnapshotCacheTest : public PlatformTest { | 40 class SnapshotCacheTest : public PlatformTest { |
40 protected: | 41 protected: |
41 // Build an array of session names and an array of UIImages filled with | 42 // Build an array of session names and an array of UIImages filled with |
42 // random colors. | 43 // random colors. |
43 void SetUp() override { | 44 void SetUp() override { |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
216 TEST_F(SnapshotCacheTest, Cache) { | 217 TEST_F(SnapshotCacheTest, Cache) { |
217 // Don't run on tablets because color snapshots are not cached so this test | 218 // Don't run on tablets because color snapshots are not cached so this test |
218 // can't compare the UIImage pointers directly. | 219 // can't compare the UIImage pointers directly. |
219 if (IsIPadIdiom() && !experimental_flags::IsTabSwitcherEnabled()) { | 220 if (IsIPadIdiom() && !experimental_flags::IsTabSwitcherEnabled()) { |
220 return; | 221 return; |
221 } | 222 } |
222 | 223 |
223 SnapshotCache* cache = GetSnapshotCache(); | 224 SnapshotCache* cache = GetSnapshotCache(); |
224 | 225 |
225 NSUInteger expectedCacheSize = kSessionCount; | 226 NSUInteger expectedCacheSize = kSessionCount; |
226 if (experimental_flags::IsLRUSnapshotCacheEnabled()) | 227 if ([cache useLRUCache]) |
227 expectedCacheSize = MIN(kSessionCount, [cache lruCacheMaxSize]); | 228 expectedCacheSize = MIN(kSessionCount, [cache lruCacheMaxSize]); |
228 | 229 |
229 // Put all images in the cache. | 230 // Put all images in the cache. |
230 for (NSUInteger i = 0; i < expectedCacheSize; ++i) { | 231 for (NSUInteger i = 0; i < expectedCacheSize; ++i) { |
231 UIImage* image = [testImages_ objectAtIndex:i]; | 232 UIImage* image = [testImages_ objectAtIndex:i]; |
232 NSString* sessionID = [testSessions_ objectAtIndex:i]; | 233 NSString* sessionID = [testSessions_ objectAtIndex:i]; |
233 [cache setImage:image withSessionID:sessionID]; | 234 [cache setImage:image withSessionID:sessionID]; |
234 } | 235 } |
235 | 236 |
236 // Get images back. | 237 // Get images back. |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
342 if (i == 0) | 343 if (i == 0) |
343 EXPECT_TRUE(base::PathExists(path)); | 344 EXPECT_TRUE(base::PathExists(path)); |
344 else | 345 else |
345 EXPECT_FALSE(base::PathExists(path)); | 346 EXPECT_FALSE(base::PathExists(path)); |
346 } | 347 } |
347 } | 348 } |
348 | 349 |
349 // Loads the color images into the cache, and pins two of them. Ensures that | 350 // 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. | 351 // only the two pinned IDs remain in memory after a call to -handleLowMemory. |
351 TEST_F(SnapshotCacheTest, HandleLowMemory) { | 352 TEST_F(SnapshotCacheTest, HandleLowMemory) { |
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); | 353 LoadAllColorImagesIntoCache(true); |
361 | 354 |
362 SnapshotCache* cache = GetSnapshotCache(); | 355 SnapshotCache* cache = GetSnapshotCache(); |
363 | 356 |
364 NSString* firstPinnedID = [testSessions_ objectAtIndex:4]; | 357 NSString* firstPinnedID = [testSessions_ objectAtIndex:4]; |
365 NSString* secondPinnedID = [testSessions_ objectAtIndex:6]; | 358 NSString* secondPinnedID = [testSessions_ objectAtIndex:6]; |
366 NSMutableSet* set = [NSMutableSet set]; | 359 NSMutableSet* set = [NSMutableSet set]; |
367 [set addObject:firstPinnedID]; | 360 [set addObject:firstPinnedID]; |
368 [set addObject:secondPinnedID]; | 361 [set addObject:secondPinnedID]; |
369 cache.pinnedIDs = set; | 362 cache.pinnedIDs = set; |
363 [cache handleLowMemory]; | |
370 | 364 |
371 if (!IsIPadIdiom() || experimental_flags::IsTabSwitcherEnabled()) | 365 EXPECT_EQ(YES, [cache hasImageInMemory:firstPinnedID]); |
372 [cache handleLowMemory]; | 366 EXPECT_EQ(YES, [cache hasImageInMemory:secondPinnedID]); |
373 | |
374 BOOL expectedValue = YES; | |
375 if (IsIPadIdiom() && !experimental_flags::IsTabSwitcherEnabled()) | |
376 expectedValue = NO; | |
377 | |
378 EXPECT_EQ(expectedValue, [cache hasImageInMemory:firstPinnedID]); | |
379 EXPECT_EQ(expectedValue, [cache hasImageInMemory:secondPinnedID]); | |
380 | 367 |
381 NSString* notPinnedID = [testSessions_ objectAtIndex:2]; | 368 NSString* notPinnedID = [testSessions_ objectAtIndex:2]; |
382 EXPECT_FALSE([cache hasImageInMemory:notPinnedID]); | 369 EXPECT_FALSE([cache hasImageInMemory:notPinnedID]); |
383 | 370 |
384 // Wait for the final image to be pulled off disk. | 371 // Wait for the final image to be pulled off disk. |
385 FlushRunLoops(); | 372 FlushRunLoops(); |
386 } | 373 } |
387 | 374 |
388 // Tests that createGreyCache creates the grey snapshots in the background, | 375 // 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 | 376 // from color images in the in-memory cache. When the grey images are all |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
582 EXPECT_TRUE(base::PathExists(retinaFile)); | 569 EXPECT_TRUE(base::PathExists(retinaFile)); |
583 | 570 |
584 // Delete the image. | 571 // Delete the image. |
585 [cache removeImageWithSessionID:kSession]; | 572 [cache removeImageWithSessionID:kSession]; |
586 FlushRunLoops(); // ensure the file is removed. | 573 FlushRunLoops(); // ensure the file is removed. |
587 | 574 |
588 EXPECT_FALSE(base::PathExists(retinaFile)); | 575 EXPECT_FALSE(base::PathExists(retinaFile)); |
589 } | 576 } |
590 | 577 |
591 } // namespace | 578 } // namespace |
OLD | NEW |