OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "components/enhanced_bookmarks/image_store.h" | 5 #include "components/enhanced_bookmarks/image_store.h" |
6 | 6 |
7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
8 | 8 |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/mac/scoped_cftyperef.h" | 10 #include "base/mac/scoped_cftyperef.h" |
11 #include "components/enhanced_bookmarks/image_record.h" | 11 #include "components/enhanced_bookmarks/image_record.h" |
12 #include "components/enhanced_bookmarks/image_store_util.h" | 12 #include "components/enhanced_bookmarks/image_store_util.h" |
13 #include "components/enhanced_bookmarks/persistent_image_store.h" | 13 #include "components/enhanced_bookmarks/persistent_image_store.h" |
14 #include "components/enhanced_bookmarks/test_image_store.h" | 14 #include "components/enhanced_bookmarks/test_image_store.h" |
15 #include "testing/platform_test.h" | 15 #include "testing/platform_test.h" |
16 #include "ui/gfx/geometry/size.h" | 16 #include "ui/gfx/geometry/size.h" |
17 #include "ui/gfx/image/image.h" | 17 #include "ui/gfx/image/image.h" |
18 #include "url/gurl.h" | 18 #include "url/gurl.h" |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 // Generates a gfx::Image with a random UIImage representation. Uses off-center | 22 // Generates a gfx::Image with a random UIImage representation. Uses off-center |
23 // circle gradient to make all pixels slightly different in order to detect | 23 // circle gradient to make all pixels slightly different in order to detect |
24 // small image alterations. | 24 // small image alterations. |
25 gfx::Image GenerateRandomUIImage(const gfx::Size& size, float scale) { | 25 scoped_ptr<gfx::Image> GenerateRandomUIImage(const gfx::Size& size, |
| 26 float scale) { |
26 UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width(), | 27 UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width(), |
27 size.height()), | 28 size.height()), |
28 YES, // opaque. | 29 YES, // opaque. |
29 scale); | 30 scale); |
30 // Create the gradient's colors. | 31 // Create the gradient's colors. |
31 CGFloat locations[] = { 0.0, 1.0 }; | 32 CGFloat locations[] = { 0.0, 1.0 }; |
32 CGFloat components[] = { rand()/CGFloat(RAND_MAX), // Start color r | 33 CGFloat components[] = { rand()/CGFloat(RAND_MAX), // Start color r |
33 rand()/CGFloat(RAND_MAX), // g | 34 rand()/CGFloat(RAND_MAX), // g |
34 rand()/CGFloat(RAND_MAX), // b | 35 rand()/CGFloat(RAND_MAX), // b |
35 1.0, // Alpha | 36 1.0, // Alpha |
(...skipping 13 matching lines...) Expand all Loading... |
49 arraysize(locations))); | 50 arraysize(locations))); |
50 CGContextDrawRadialGradient(UIGraphicsGetCurrentContext(), | 51 CGContextDrawRadialGradient(UIGraphicsGetCurrentContext(), |
51 gradient, | 52 gradient, |
52 center, | 53 center, |
53 0, | 54 0, |
54 center, | 55 center, |
55 radius, | 56 radius, |
56 kCGGradientDrawsAfterEndLocation); | 57 kCGGradientDrawsAfterEndLocation); |
57 UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); | 58 UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); |
58 UIGraphicsEndImageContext(); | 59 UIGraphicsEndImageContext(); |
59 return gfx::Image([image retain]); | 60 return scoped_ptr<gfx::Image>(new gfx::Image([image retain])); |
60 } | 61 } |
61 | 62 |
62 // Returns true if the two images are identical. | 63 // Returns true if the two images are identical. |
63 bool CompareImages(const gfx::Image& image_1, const gfx::Image& image_2) { | 64 bool CompareImages(const gfx::Image& image_1, const gfx::Image& image_2) { |
64 if (image_1.IsEmpty() && image_2.IsEmpty()) | 65 if (image_1.IsEmpty() && image_2.IsEmpty()) |
65 return true; | 66 return true; |
66 if (image_1.IsEmpty() || image_2.IsEmpty()) | 67 if (image_1.IsEmpty() || image_2.IsEmpty()) |
67 return false; | 68 return false; |
68 | 69 |
69 scoped_refptr<base::RefCountedMemory> image_1_bytes = | 70 scoped_refptr<base::RefCountedMemory> image_1_bytes = |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 typedef testing::Types<TestImageStore, | 135 typedef testing::Types<TestImageStore, |
135 PersistentImageStore> Implementations; | 136 PersistentImageStore> Implementations; |
136 | 137 |
137 TYPED_TEST_CASE(ImageStoreUnitTestIOS, Implementations); | 138 TYPED_TEST_CASE(ImageStoreUnitTestIOS, Implementations); |
138 | 139 |
139 TYPED_TEST(ImageStoreUnitTestIOS, StoringImagesPreservesScale) { | 140 TYPED_TEST(ImageStoreUnitTestIOS, StoringImagesPreservesScale) { |
140 const CGFloat scales[] = {0.0, 1.0, 2.0}; | 141 const CGFloat scales[] = {0.0, 1.0, 2.0}; |
141 const gfx::Size image_size(42, 24); | 142 const gfx::Size image_size(42, 24); |
142 for (unsigned long i = 0; i < arraysize(scales); i++) { | 143 for (unsigned long i = 0; i < arraysize(scales); i++) { |
143 const GURL url("foo://bar"); | 144 const GURL url("foo://bar"); |
144 const enhanced_bookmarks::ImageRecord image_in( | 145 scoped_refptr<enhanced_bookmarks::ImageRecord> image_in( |
145 GenerateRandomUIImage(image_size, scales[i]), GURL("http://a.jpg"), | 146 new enhanced_bookmarks::ImageRecord( |
146 SK_ColorGREEN); | 147 GenerateRandomUIImage(image_size, scales[i]), |
| 148 GURL("http://a.jpg"), |
| 149 SK_ColorGREEN)); |
147 this->store_->Insert(url, image_in); | 150 this->store_->Insert(url, image_in); |
148 const enhanced_bookmarks::ImageRecord image_out = this->store_->Get(url); | 151 scoped_refptr<enhanced_bookmarks::ImageRecord> image_out = |
| 152 this->store_->Get(url); |
149 | 153 |
150 EXPECT_EQ(image_in.url, image_out.url); | 154 EXPECT_EQ(image_in->url, image_out->url); |
151 EXPECT_TRUE(CompareImages(image_in.image, image_out.image)); | 155 EXPECT_TRUE(CompareImages(*image_in->image, *image_out->image)); |
152 EXPECT_EQ(image_in.dominant_color, image_out.dominant_color); | 156 EXPECT_EQ(image_in->dominant_color, image_out->dominant_color); |
153 } | 157 } |
154 } | 158 } |
155 | 159 |
156 } // namespace | 160 } // namespace |
OLD | NEW |