| Index: chrome/browser/bookmarks/enhanced/image_store_unittest.cc
|
| diff --git a/chrome/browser/bookmarks/enhanced/image_store_unittest.cc b/chrome/browser/bookmarks/enhanced/image_store_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..f715438198a29c579bde4c5a0268de871085269a
|
| --- /dev/null
|
| +++ b/chrome/browser/bookmarks/enhanced/image_store_unittest.cc
|
| @@ -0,0 +1,203 @@
|
| +// Copyright 2013 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "base/files/scoped_temp_dir.h"
|
| +#include "chrome/browser/bookmarks/enhanced/image_store.h"
|
| +#include "testing/platform_test.h"
|
| +#include "third_party/skia/include/core/SkBitmap.h"
|
| +
|
| +#if defined(OS_IOS)
|
| +#include "chrome/browser/bookmarks/enhanced/image_store_ios.h"
|
| +#endif // defined(OS_IOS)
|
| +
|
| +namespace {
|
| +
|
| +const SkBitmap CreateBitmap(int width, int height) {
|
| + SkBitmap bitmap;
|
| + bitmap.setConfig(SkBitmap::kARGB_8888_Config, width, height);
|
| + bitmap.allocPixels();
|
| + bitmap.eraseARGB(rand()%255, rand()%255, rand()%255, rand()%255);
|
| + return bitmap;
|
| +}
|
| +
|
| +gfx::Image GenerateRandomImage() {
|
| + return gfx::Image::CreateFrom1xBitmap(CreateBitmap(42, 24));
|
| +}
|
| +
|
| +// Returns true if the two images are identical.
|
| +bool CompareImages(gfx::Image image_1, gfx::Image image_2) {
|
| + if (image_1.IsEmpty() && image_2.IsEmpty())
|
| + return true;
|
| +
|
| + if (image_1.IsEmpty() || image_2.IsEmpty())
|
| + return false;
|
| +
|
| +#if defined(OS_IOS)
|
| + // Forces the two images to have a UIImage representation.
|
| + image_1.ToUIImage();
|
| + image_2.ToUIImage();
|
| + scoped_refptr<base::RefCountedMemory> image_1_png(
|
| + image_store_ios::bytesForImage(image_1));
|
| + scoped_refptr<base::RefCountedMemory> image_2_png(
|
| + image_store_ios::bytesForImage(image_2));
|
| +#else
|
| + scoped_refptr<base::RefCountedMemory> image_1_png(image_1.As1xPNGBytes());
|
| + scoped_refptr<base::RefCountedMemory> image_2_png(image_2.As1xPNGBytes());
|
| +#endif // defined(OS_IOS)
|
| +
|
| + if (image_1_png->size() != image_2_png->size())
|
| + return false;
|
| +
|
| + return !memcmp(image_1_png->front(),
|
| + image_2_png->front(),
|
| + image_1_png->size());
|
| +}
|
| +
|
| +// Factory functions for creating instances of the implementations.
|
| +template <class T>
|
| +scoped_refptr<ImageStore> createStore(base::ScopedTempDir& folder);
|
| +
|
| +template <>
|
| +scoped_refptr<ImageStore> createStore<MemoryImageStore>(
|
| + base::ScopedTempDir& folder) {
|
| + return scoped_refptr<ImageStore>(new MemoryImageStore());
|
| +}
|
| +
|
| +template <>
|
| +scoped_refptr<ImageStore> createStore<PersistentImageStore>(
|
| + base::ScopedTempDir& folder) {
|
| + return scoped_refptr<ImageStore>(new PersistentImageStore(folder.path()));
|
| +}
|
| +
|
| +// Methods to check if persistence is on or not.
|
| +template <class T> bool shouldPersist();
|
| +template <> bool shouldPersist<MemoryImageStore>() { return false; }
|
| +template <> bool shouldPersist<PersistentImageStore>() { return true; }
|
| +
|
| +// Test fixture class template for the abstract API.
|
| +template <class T>
|
| +class ImageStoreUnitTest : public PlatformTest {
|
| + protected:
|
| + ImageStoreUnitTest() {
|
| + bool success = tempDir_.CreateUniqueTempDir();
|
| + EXPECT_TRUE(success);
|
| + store_ = createStore<T>(tempDir_);
|
| + }
|
| +
|
| + void TearDown() OVERRIDE {
|
| + if (store_ && usePersistentStore())
|
| + store_->ClearAll();
|
| + }
|
| +
|
| + bool usePersistentStore() { return shouldPersist<T>(); }
|
| + void resetStore() { store_ = createStore<T>(tempDir_); }
|
| + virtual ~ImageStoreUnitTest() {}
|
| + // The directory the database is saved into.
|
| + base::ScopedTempDir tempDir_;
|
| + // The object the fixture is testing, via its base interface.
|
| + scoped_refptr<ImageStore> store_;
|
| +};
|
| +
|
| +// The list of implementations of the abstract API that are going to be tested.
|
| +typedef testing::Types<MemoryImageStore,
|
| + PersistentImageStore> Implementations;
|
| +
|
| +TYPED_TEST_CASE(ImageStoreUnitTest, Implementations);
|
| +
|
| +// All those tests are run on all the implementations.
|
| +TYPED_TEST(ImageStoreUnitTest, startsEmpty) {
|
| + EXPECT_EQ(0ul, this->store_->AllKeys().size());
|
| +}
|
| +
|
| +TYPED_TEST(ImageStoreUnitTest, storeOne) {
|
| + this->store_->Insert(GURL("foo://bar"), GURL("a.jpg"), GenerateRandomImage());
|
| + EXPECT_EQ(1ul, this->store_->AllKeys().size());
|
| + EXPECT_EQ(GURL("foo://bar"), this->store_->AllKeys()[0]);
|
| + EXPECT_TRUE(this->store_->HasKey(GURL("foo://bar")));
|
| +}
|
| +
|
| +TYPED_TEST(ImageStoreUnitTest, retrieve) {
|
| + gfx::Image src_image = GenerateRandomImage();
|
| + const GURL url("foo://bar");
|
| + const GURL image_url("a.jpg");
|
| + this->store_->Insert(url, image_url, src_image);
|
| + GURL result;
|
| + gfx::Image dst_image = this->store_->Get(url, result);
|
| +
|
| + EXPECT_EQ(image_url, result);
|
| + EXPECT_TRUE(CompareImages(src_image, dst_image));
|
| +}
|
| +
|
| +TYPED_TEST(ImageStoreUnitTest, erase) {
|
| + gfx::Image src_image = GenerateRandomImage();
|
| + const GURL url("foo://bar");
|
| + const GURL image_url("a.jpg");
|
| + this->store_->Insert(url, image_url, src_image);
|
| +
|
| + this->store_->Erase(url);
|
| + EXPECT_FALSE(this->store_->HasKey(url));
|
| + EXPECT_EQ(0ul, this->store_->AllKeys().size());
|
| +}
|
| +
|
| +TYPED_TEST(ImageStoreUnitTest, update) {
|
| + gfx::Image src_image1 = GenerateRandomImage();
|
| + gfx::Image src_image2 = GenerateRandomImage();
|
| + const GURL url("foo://bar");
|
| + const GURL image_url1("1.jpg");
|
| + this->store_->Insert(url, image_url1, src_image1);
|
| +
|
| + const GURL image_url2("2.jpg");
|
| + this->store_->Insert(url, image_url2, src_image2);
|
| +
|
| + GURL result;
|
| + gfx::Image dst_image = this->store_->Get(url, result);
|
| +
|
| + EXPECT_TRUE(this->store_->HasKey(url));
|
| + EXPECT_EQ(1ul, this->store_->AllKeys().size());
|
| + EXPECT_EQ(image_url2, result);
|
| + EXPECT_TRUE(CompareImages(src_image2, dst_image));
|
| +}
|
| +
|
| +TYPED_TEST(ImageStoreUnitTest, persistence) {
|
| + gfx::Image src_image = GenerateRandomImage();
|
| + const GURL url("foo://bar");
|
| + const GURL image_url("a.jpg");
|
| + this->store_->Insert(url, image_url, src_image);
|
| +
|
| + this->resetStore();
|
| + if (this->usePersistentStore()) {
|
| + EXPECT_EQ(1ul, this->store_->AllKeys().size());
|
| + EXPECT_EQ(GURL("foo://bar"), this->store_->AllKeys()[0]);
|
| + EXPECT_TRUE(this->store_->HasKey(GURL("foo://bar")));
|
| + GURL result;
|
| + gfx::Image dst_image = this->store_->Get(url, result);
|
| +
|
| + EXPECT_EQ(image_url, result);
|
| + EXPECT_TRUE(CompareImages(src_image, dst_image));
|
| + } else {
|
| + EXPECT_EQ(0ul, this->store_->AllKeys().size());
|
| + EXPECT_FALSE(this->store_->HasKey(GURL("foo://bar")));
|
| + }
|
| +}
|
| +
|
| +#if defined(OS_IOS)
|
| +TYPED_TEST(ImageStoreUnitTest, retrieveUIImageWithScale) {
|
| + CGFloat scales[] = { 0.0, 1.0, 2.0 };
|
| + gfx::Size image_size(42, 24);
|
| + for (unsigned long i = 0; i < arraysize(scales); i++) {
|
| + gfx::Image src_image(image_store_ios::generateRandomUIImage(
|
| + image_size, scales[i]));
|
| + const GURL url("foo://bar");
|
| + const GURL image_url("a.jpg");
|
| + this->store_->Insert(url, image_url, src_image);
|
| + GURL result;
|
| + gfx::Image dst_image = this->store_->Get(url, result);
|
| +
|
| + EXPECT_EQ(image_url, result);
|
| + EXPECT_TRUE(CompareImages(src_image, dst_image));
|
| + }
|
| +}
|
| +#endif // defined(OS_IOS)
|
| +
|
| +} // namespace
|
|
|