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
3 // found in the LICENSE file.
5 #include "components/enhanced_bookmarks/image_store.h"
7 #import <UIKit/UIKit.h>
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/mac/scoped_cftyperef.h"
11 #include "components/enhanced_bookmarks/image_record.h"
12 #include "components/enhanced_bookmarks/image_store_util.h"
13 #include "components/enhanced_bookmarks/persistent_image_store.h"
14 #include "components/enhanced_bookmarks/test_image_store.h"
15 #include "testing/platform_test.h"
16 #include "ui/gfx/geometry/size.h"
17 #include "ui/gfx/image/image.h"
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
24 // small image alterations.
25 scoped_ptr<gfx::Image> GenerateRandomUIImage(const gfx::Size& size,
27 UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width(),
31 // Create the gradient's colors.
32 CGFloat locations[] = { 0.0, 1.0 };
33 CGFloat components[] = { rand()/CGFloat(RAND_MAX), // Start color r
34 rand()/CGFloat(RAND_MAX), // g
35 rand()/CGFloat(RAND_MAX), // b
37 rand()/CGFloat(RAND_MAX), // End color r
38 rand()/CGFloat(RAND_MAX), // g
39 rand()/CGFloat(RAND_MAX), // b
41 CGPoint center = CGPointMake(size.width() / 3, size.height() / 3);
42 CGFloat radius = MAX(size.width(), size.height());
44 base::ScopedCFTypeRef<CGColorSpaceRef>
45 colorspace(CGColorSpaceCreateDeviceRGB());
46 base::ScopedCFTypeRef<CGGradientRef>
47 gradient(CGGradientCreateWithColorComponents(colorspace,
50 arraysize(locations)));
51 CGContextDrawRadialGradient(UIGraphicsGetCurrentContext(),
57 kCGGradientDrawsAfterEndLocation);
58 UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
59 UIGraphicsEndImageContext();
60 return scoped_ptr<gfx::Image>(new gfx::Image([image retain]));
63 // Returns true if the two images are identical.
64 bool CompareImages(const gfx::Image& image_1, const gfx::Image& image_2) {
65 if (image_1.IsEmpty() && image_2.IsEmpty())
67 if (image_1.IsEmpty() || image_2.IsEmpty())
70 scoped_refptr<base::RefCountedMemory> image_1_bytes =
71 enhanced_bookmarks::BytesForImage(image_1);
72 scoped_refptr<base::RefCountedMemory> image_2_bytes =
73 enhanced_bookmarks::BytesForImage(image_2);
75 if (image_1_bytes->size() != image_2_bytes->size())
78 return !memcmp(image_1_bytes->front(),
79 image_2_bytes->front(),
80 image_1_bytes->size());
83 // Factory functions for creating instances of the implementations.
85 ImageStore* CreateStore(base::ScopedTempDir& folder);
88 ImageStore* CreateStore<TestImageStore>(
89 base::ScopedTempDir& folder) {
90 return new TestImageStore();
94 ImageStore* CreateStore<PersistentImageStore>(
95 base::ScopedTempDir& folder) {
96 return new PersistentImageStore(folder.path());
99 // Methods to check if persistence is on or not.
100 template <class T> bool ShouldPersist();
101 template <> bool ShouldPersist<TestImageStore>() { return false; }
102 template <> bool ShouldPersist<PersistentImageStore>() { return true; }
104 // Test fixture class template for the abstract API.
106 class ImageStoreUnitTestIOS : public PlatformTest {
108 ImageStoreUnitTestIOS() {}
109 ~ImageStoreUnitTestIOS() override {}
111 void SetUp() override {
112 bool success = temp_dir_.CreateUniqueTempDir();
113 ASSERT_TRUE(success);
114 store_.reset(CreateStore<T>(temp_dir_));
117 void TearDown() override {
118 if (store_ && use_persistent_store())
122 bool use_persistent_store() const { return ShouldPersist<T>(); }
123 void ResetStore() { store_.reset(CreateStore<T>(temp_dir_)); }
125 // The directory the database is saved into.
126 base::ScopedTempDir temp_dir_;
127 // The object the fixture is testing, via its base interface.
128 scoped_ptr<ImageStore> store_;
131 DISALLOW_COPY_AND_ASSIGN(ImageStoreUnitTestIOS);
134 // The list of implementations of the abstract API that are going to be tested.
135 typedef testing::Types<TestImageStore,
136 PersistentImageStore> Implementations;
138 TYPED_TEST_CASE(ImageStoreUnitTestIOS, Implementations);
140 TYPED_TEST(ImageStoreUnitTestIOS, StoringImagesPreservesScale) {
141 const CGFloat scales[] = {0.0, 1.0, 2.0};
142 const gfx::Size image_size(42, 24);
143 for (unsigned long i = 0; i < arraysize(scales); i++) {
144 const GURL url("foo://bar");
145 scoped_refptr<enhanced_bookmarks::ImageRecord> image_in(
146 new enhanced_bookmarks::ImageRecord(
147 GenerateRandomUIImage(image_size, scales[i]),
148 GURL("http://a.jpg"),
150 this->store_->Insert(url, image_in);
151 scoped_refptr<enhanced_bookmarks::ImageRecord> image_out =
152 this->store_->Get(url);
154 EXPECT_EQ(image_in->url, image_out->url);
155 EXPECT_TRUE(CompareImages(*image_in->image, *image_out->image));
156 EXPECT_EQ(image_in->dominant_color, image_out->dominant_color);