diff options
author | rohitrao@chromium.org <rohitrao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-22 20:55:39 +0000 |
---|---|---|
committer | rohitrao@chromium.org <rohitrao@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-02-22 20:55:39 +0000 |
commit | 8db351a4f3543c4f70f8c04c1a8bab4efbad066a (patch) | |
tree | 465a006e31af16fe77ca72f62dc7bf0f0cf8dbae | |
parent | b0eb2152aaf774f4be92e2050c4e4795e09b679e (diff) | |
download | chromium_src-8db351a4f3543c4f70f8c04c1a8bab4efbad066a.zip chromium_src-8db351a4f3543c4f70f8c04c1a8bab4efbad066a.tar.gz chromium_src-8db351a4f3543c4f70f8c04c1a8bab4efbad066a.tar.bz2 |
Add a default constructor for gfx::Image.
BUG=None
TEST=None
Review URL: http://codereview.chromium.org/7101005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@123103 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | ui/gfx/image/image.cc | 19 | ||||
-rw-r--r-- | ui/gfx/image/image.h | 11 | ||||
-rw-r--r-- | ui/gfx/image/image_unittest.cc | 21 |
3 files changed, 48 insertions, 3 deletions
diff --git a/ui/gfx/image/image.cc b/ui/gfx/image/image.cc index d31e2d8..61a84e7 100644 --- a/ui/gfx/image/image.cc +++ b/ui/gfx/image/image.cc @@ -100,6 +100,7 @@ class ImageRepSkia : public ImageRep { explicit ImageRepSkia(const SkBitmap* bitmap) : ImageRep(Image::kImageRepSkia) { CHECK(bitmap); + // TODO(rohitrao): Add a CHECK to ensure that !bitmap->isNull(). bitmaps_.push_back(bitmap); } @@ -107,6 +108,8 @@ class ImageRepSkia : public ImageRep { : ImageRep(Image::kImageRepSkia), bitmaps_(bitmaps) { CHECK(!bitmaps_.empty()); + // TODO(rohitrao): Add a CHECK to ensure that !bitmap->isNull() for each + // vector element. } virtual ~ImageRepSkia() { @@ -230,6 +233,10 @@ class ImageStorage : public base::RefCounted<ImageStorage> { } // namespace internal +Image::Image() { + // |storage_| is NULL for empty Images. +} + Image::Image(const SkBitmap* bitmap) : storage_(new internal::ImageStorage(Image::kImageRepSkia)) { internal::ImageRepSkia* rep = new internal::ImageRepSkia(bitmap); @@ -334,18 +341,26 @@ Image::operator NSImage*() const { #endif bool Image::HasRepresentation(RepresentationType type) const { - return storage_->representations().count(type) != 0; + return storage_.get() && storage_->representations().count(type) != 0; } size_t Image::RepresentationCount() const { + if (!storage_.get()) + return 0; + return storage_->representations().size(); } +bool Image::IsEmpty() const { + return RepresentationCount() == 0; +} + void Image::SwapRepresentations(gfx::Image* other) { storage_.swap(other->storage_); } internal::ImageRep* Image::DefaultRepresentation() const { + CHECK(storage_.get()); RepresentationMap& representations = storage_->representations(); RepresentationMap::iterator it = representations.find(storage_->default_representation_type()); @@ -355,6 +370,7 @@ internal::ImageRep* Image::DefaultRepresentation() const { internal::ImageRep* Image::GetRepresentation( RepresentationType rep_type) const { + CHECK(storage_.get()); // If the requested rep is the default, return it. internal::ImageRep* default_rep = DefaultRepresentation(); if (rep_type == storage_->default_representation_type()) @@ -435,6 +451,7 @@ internal::ImageRep* Image::GetRepresentation( } void Image::AddRepresentation(internal::ImageRep* rep) const { + CHECK(storage_.get()); storage_->representations().insert(std::make_pair(rep->type(), rep)); } diff --git a/ui/gfx/image/image.h b/ui/gfx/image/image.h index dd4b8ec..d5e630b 100644 --- a/ui/gfx/image/image.h +++ b/ui/gfx/image/image.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -12,6 +12,9 @@ // cheaply passed around by value, the actual image data is stored in a ref- // counted member. When all Images referencing this storage are deleted, the // actual representations are deleted, too. +// +// Images can be empty, in which case they have no backing representation. +// Attempting to use an empty Image will result in a crash. #ifndef UI_GFX_IMAGE_IMAGE_H_ #define UI_GFX_IMAGE_IMAGE_H_ @@ -55,6 +58,9 @@ class UI_EXPORT Image { typedef std::map<RepresentationType, internal::ImageRep*> RepresentationMap; + // Creates an empty image with no representations. + Image(); + // Creates a new image with the default representation. The object will take // ownership of the image. explicit Image(const SkBitmap* bitmap); @@ -134,6 +140,9 @@ class UI_EXPORT Image { // Returns the number of representations. size_t RepresentationCount() const; + // Returns true if this Image has no representations. + bool IsEmpty() const; + // Swaps this image's internal representations with |other|. void SwapRepresentations(gfx::Image* other); diff --git a/ui/gfx/image/image_unittest.cc b/ui/gfx/image/image_unittest.cc index 2c82700..6cc622a 100644 --- a/ui/gfx/image/image_unittest.cc +++ b/ui/gfx/image/image_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -29,6 +29,25 @@ class ImageTest : public testing::Test { namespace gt = gfx::test; +TEST_F(ImageTest, EmptyImage) { + // Test the default constructor. + gfx::Image image; + EXPECT_EQ(0U, image.RepresentationCount()); + EXPECT_TRUE(image.IsEmpty()); + + // Test the copy constructor. + gfx::Image imageCopy(image); + EXPECT_TRUE(imageCopy.IsEmpty()); + + // Test calling SwapRepresentations() with an empty image. + gfx::Image image2(gt::CreateBitmap(25, 25)); + EXPECT_FALSE(image2.IsEmpty()); + + image.SwapRepresentations(&image2); + EXPECT_FALSE(image.IsEmpty()); + EXPECT_TRUE(image2.IsEmpty()); +} + TEST_F(ImageTest, SkiaToSkia) { gfx::Image image(gt::CreateBitmap(25, 25)); const SkBitmap* bitmap = image.ToSkBitmap(); |