Cast: Add an option to turn on non-blocking IO on Windows
[chromium-blink-merge.git] / components / enhanced_bookmarks / image_store_ios_unittest.mm
blob19dd5fae2b5119943d6c5da7682556d00248fc7f
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"
18 #include "url/gurl.h"
20 namespace {
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 gfx::Image GenerateRandomUIImage(const gfx::Size& size, float scale) {
26   UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width(),
27                                                     size.height()),
28                                          YES,  // opaque.
29                                          scale);
30   // Create the gradient's colors.
31   CGFloat locations[] = { 0.0, 1.0 };
32   CGFloat components[] = { rand()/CGFloat(RAND_MAX),  // Start color r
33                            rand()/CGFloat(RAND_MAX),  // g
34                            rand()/CGFloat(RAND_MAX),  // b
35                            1.0,                       // Alpha
36                            rand()/CGFloat(RAND_MAX),  // End color r
37                            rand()/CGFloat(RAND_MAX),  // g
38                            rand()/CGFloat(RAND_MAX),  // b
39                            1.0 };                     // Alpha
40   CGPoint center = CGPointMake(size.width() / 3, size.height() / 3);
41   CGFloat radius = MAX(size.width(), size.height());
43   base::ScopedCFTypeRef<CGColorSpaceRef>
44       colorspace(CGColorSpaceCreateDeviceRGB());
45   base::ScopedCFTypeRef<CGGradientRef>
46       gradient(CGGradientCreateWithColorComponents(colorspace,
47                                                    components,
48                                                    locations,
49                                                    arraysize(locations)));
50   CGContextDrawRadialGradient(UIGraphicsGetCurrentContext(),
51                               gradient,
52                               center,
53                               0,
54                               center,
55                               radius,
56                               kCGGradientDrawsAfterEndLocation);
57   UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
58   UIGraphicsEndImageContext();
59   return gfx::Image([image retain]);
62 // Returns true if the two images are identical.
63 bool CompareImages(const gfx::Image& image_1, const gfx::Image& image_2) {
64   if (image_1.IsEmpty() && image_2.IsEmpty())
65     return true;
66   if (image_1.IsEmpty() || image_2.IsEmpty())
67     return false;
69   scoped_refptr<base::RefCountedMemory> image_1_bytes =
70       enhanced_bookmarks::BytesForImage(image_1);
71   scoped_refptr<base::RefCountedMemory> image_2_bytes =
72       enhanced_bookmarks::BytesForImage(image_2);
74   if (image_1_bytes->size() != image_2_bytes->size())
75     return false;
77   return !memcmp(image_1_bytes->front(),
78                  image_2_bytes->front(),
79                  image_1_bytes->size());
82 // Factory functions for creating instances of the implementations.
83 template <class T>
84 ImageStore* CreateStore(base::ScopedTempDir& folder);
86 template <>
87 ImageStore* CreateStore<TestImageStore>(
88     base::ScopedTempDir& folder) {
89   return new TestImageStore();
92 template <>
93 ImageStore* CreateStore<PersistentImageStore>(
94     base::ScopedTempDir& folder) {
95   return new PersistentImageStore(folder.path());
98 // Methods to check if persistence is on or not.
99 template <class T> bool ShouldPersist();
100 template <> bool ShouldPersist<TestImageStore>() { return false; }
101 template <> bool ShouldPersist<PersistentImageStore>() { return true; }
103 // Test fixture class template for the abstract API.
104 template <class T>
105 class ImageStoreUnitTestIOS : public PlatformTest {
106  protected:
107   ImageStoreUnitTestIOS() {}
108   virtual ~ImageStoreUnitTestIOS() {}
110   virtual void SetUp() override {
111     bool success = temp_dir_.CreateUniqueTempDir();
112     ASSERT_TRUE(success);
113     store_.reset(CreateStore<T>(temp_dir_));
114   }
116   virtual void TearDown() override {
117     if (store_ && use_persistent_store())
118       store_->ClearAll();
119   }
121   bool use_persistent_store() const { return ShouldPersist<T>(); }
122   void ResetStore() { store_.reset(CreateStore<T>(temp_dir_)); }
124   // The directory the database is saved into.
125   base::ScopedTempDir temp_dir_;
126   // The object the fixture is testing, via its base interface.
127   scoped_ptr<ImageStore> store_;
129  private:
130   DISALLOW_COPY_AND_ASSIGN(ImageStoreUnitTestIOS);
133 // The list of implementations of the abstract API that are going to be tested.
134 typedef testing::Types<TestImageStore,
135                        PersistentImageStore> Implementations;
137 TYPED_TEST_CASE(ImageStoreUnitTestIOS, Implementations);
139 TYPED_TEST(ImageStoreUnitTestIOS, StoringImagesPreservesScale) {
140   const CGFloat scales[] = {0.0, 1.0, 2.0};
141   const gfx::Size image_size(42, 24);
142   for (unsigned long i = 0; i < arraysize(scales); i++) {
143     const GURL url("foo://bar");
144     const enhanced_bookmarks::ImageRecord image_in(
145         GenerateRandomUIImage(image_size, scales[i]), GURL("http://a.jpg"),
146         SK_ColorGREEN);
147     this->store_->Insert(url, image_in);
148     const enhanced_bookmarks::ImageRecord image_out = this->store_->Get(url);
150     EXPECT_EQ(image_in.url, image_out.url);
151     EXPECT_TRUE(CompareImages(image_in.image, image_out.image));
152     EXPECT_EQ(image_in.dominant_color, image_out.dominant_color);
153   }
156 }  // namespace