Chromium Code Reviews| Index: ui/gfx/image/image_family_unittest.cc |
| diff --git a/ui/gfx/image/image_family_unittest.cc b/ui/gfx/image/image_family_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..114b776b769975d9fba1f4210e4e40aec72010e0 |
| --- /dev/null |
| +++ b/ui/gfx/image/image_family_unittest.cc |
| @@ -0,0 +1,177 @@ |
| +// 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 "testing/gtest/include/gtest/gtest.h" |
| +#include "ui/gfx/image/image.h" |
| +#include "ui/gfx/image/image_family.h" |
| +#include "ui/gfx/image/image_skia.h" |
| +#include "ui/gfx/image/image_unittest_util.h" |
| + |
| +namespace { |
| + |
| +namespace gt = gfx::test; |
| + |
| +// Tests that |image| != NULL, and has the given width and height. |
| +// This is a macro instead of a function, so that the correct line numbers are |
| +// reported when a test fails. |
| +#define EXPECT_IMAGE_NON_NULL_AND_SIZE(image, expected_width, expected_height) \ |
| +do { \ |
| + const gfx::Image* image_ = image; \ |
| + EXPECT_NE(static_cast<gfx::Image*>(NULL), image_); \ |
| + EXPECT_EQ(expected_width, image_->Width()); \ |
| + EXPECT_EQ(expected_height, image_->Height()); \ |
| +} while(0) |
| + |
| +class ImageFamilyTest : public testing::Test { |
| + public: |
| + // Construct an ImageFamily. Implicitly tests Add and Empty. |
| + virtual void SetUp() { |
|
Robert Sesek
2013/03/27 17:42:50
OVERRIDE
Matt Giuca
2013/03/28 03:54:03
Done.
|
| + EXPECT_TRUE(icon_family.empty()); |
| + |
| + // Aspect ratio 1:1. |
| + icon_family.Add(gt::CreateImageSkia(32, 32)); |
| + EXPECT_FALSE(icon_family.empty()); |
| + icon_family.Add(gt::CreateImageSkia(16, 16)); |
| + icon_family.Add(gt::CreateImageSkia(64, 64)); |
| + // Duplicate (should override previous one). |
| + // Insert an Image directly, instead of an ImageSkia. |
| + gfx::Image image(gt::CreateImageSkia(32, 32)); |
| + icon_family.Add(image); |
| + // Aspect ratio 1:4. |
| + icon_family.Add(gt::CreateImageSkia(3, 12)); |
| + icon_family.Add(gt::CreateImageSkia(12, 48)); |
| + // Aspect ratio 4:1. |
| + icon_family.Add(gt::CreateImageSkia(512, 128)); |
| + icon_family.Add(gt::CreateImageSkia(256, 64)); |
| + |
| + EXPECT_FALSE(icon_family.empty()); |
| + } |
| + |
| + gfx::ImageFamily icon_family; |
|
Robert Sesek
2013/03/27 17:42:50
Needs a trailing underscore
Matt Giuca
2013/03/28 03:54:03
Done. Also renamed to image_family_.
|
| +}; |
| + |
| +TEST_F(ImageFamilyTest, Clear) { |
| + icon_family.clear(); |
| + EXPECT_TRUE(icon_family.empty()); |
| +} |
| + |
| +// Tests iteration over an ImageFamily. |
| +TEST_F(ImageFamilyTest, Iteration) { |
| + gfx::ImageFamily::const_iterator it = icon_family.begin(); |
| + gfx::ImageFamily::const_iterator end = icon_family.end(); |
| + |
| + // Expect iteration in order of aspect ratio (from thinnest to widest), then |
| + // size. |
| + EXPECT_TRUE(it != end); |
| + EXPECT_EQ(gfx::Size(3, 12), it->Size()); |
| + ++it; |
| + EXPECT_TRUE(it != end); |
| + EXPECT_EQ(gfx::Size(12, 48), it->Size()); |
| + it++; // Test post-increment. |
| + EXPECT_TRUE(it != end); |
| + EXPECT_EQ(gfx::Size(16, 16), it->Size()); |
| + ++it; |
| + EXPECT_TRUE(it != end); |
| + EXPECT_EQ(gfx::Size(32, 32), it->Size()); |
| + --it; // Test decrement |
| + EXPECT_TRUE(it != end); |
| + EXPECT_EQ(gfx::Size(16, 16), it->Size()); |
| + ++it; |
| + ++it; |
| + EXPECT_TRUE(it != end); |
| + EXPECT_EQ(gfx::Size(64, 64), (*it).Size()); // Test operator*. |
| + ++it; |
| + EXPECT_TRUE(it != end); |
| + EXPECT_EQ(gfx::Size(256, 64), it->Size()); |
| + ++it; |
| + EXPECT_TRUE(it != end); |
| + EXPECT_EQ(gfx::Size(512, 128), it->Size()); |
| + ++it; |
| + |
| + EXPECT_TRUE(it == end); |
| +} |
| + |
| +TEST_F(ImageFamilyTest, Get) { |
| + // Get on an empty family. |
| + gfx::ImageFamily empty_family; |
| + EXPECT_TRUE(empty_family.empty()); |
| + EXPECT_EQ(static_cast<gfx::Image*>(NULL), empty_family.Get(32, 32)); |
| + EXPECT_EQ(static_cast<gfx::Image*>(NULL), empty_family.Get(0, 32)); |
|
Robert Sesek
2013/03/27 17:42:50
I generally use EXPECT_FALSE() for checking for NU
Matt Giuca
2013/03/28 03:54:03
Done.
|
| + EXPECT_EQ(static_cast<gfx::Image*>(NULL), empty_family.Get(32, 0)); |
| + |
| + // Get various aspect ratios and sizes on the sample family. |
| + |
| + // 0x0 (expect the smallest square image). |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(0, 0), 16, 16); |
| + // Get(0, N) or Get(N, 0) should be treated the same as Get(0, 0). |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(0, 16), 16, 16); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(0, 64), 16, 16); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(16, 0), 16, 16); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(64, 0), 16, 16); |
| + |
| + // Thinner than thinnest image. |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(2, 12), 3, 12); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(2, 13), 12, 48); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(10, 60), 12, 48); |
| + |
| + // Between two images' aspect ratio. |
| + // Note: Testing the boundary around 1:2 and 2:1, half way to 1:4 and 4:1. |
| + // Ties are broken by favouring the thinner aspect ratio. |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(63, 32), 64, 64); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(64, 32), 64, 64); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(65, 32), 256, 64); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(32, 63), 64, 64); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(32, 64), 12, 48); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(32, 65), 12, 48); |
| + |
| + // Exact match aspect ratio. |
| + // Exact match size. |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(32, 32), 32, 32); |
| + // Slightly smaller. |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(31, 31), 32, 32); |
| + // Much smaller. |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(17, 17), 32, 32); |
| + // Exact match size. |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(16, 16), 16, 16); |
| + // Smaller than any image. |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(3, 3), 16, 16); |
| + // Larger than any image. |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(512, 512), 64, 64); |
| + // 1:4 aspect ratio. |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(16, 64), 12, 48); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(2, 8), 3, 12); |
| + // 4:1 aspect ratio. |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(64, 16), 256, 64); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(260, 65), 512, 128); |
| + |
| + // Wider than widest image. |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(255, 51), 256, 64); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(260, 52), 512, 128); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(654, 129), 512, 128); |
| +} |
| + |
| +// Test adding and looking up images with 0 width and height. |
| +TEST_F(ImageFamilyTest, ZeroWidthAndHeight) { |
| + // An empty Image. Should be considered to have 0 width and height. |
| + icon_family.Add(gfx::Image()); |
| + // Images with 0 width OR height should be treated the same as an image with 0 |
| + // width AND height (in fact, the ImageSkias should be indistinguishable). |
| + icon_family.Add(gt::CreateImageSkia(32, 0)); |
| + icon_family.Add(gt::CreateImageSkia(0, 32)); |
| + |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(0, 0), 0, 0); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(1, 1), 16, 16); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(32, 32), 32, 32); |
| + |
| + // Get(0, N) or Get(N, 0) should be treated the same as Get(0, 0). |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(0, 1), 0, 0); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(0, 32), 0, 0); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(1, 0), 0, 0); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(32, 0), 0, 0); |
| + |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(1, 32), 12, 48); |
| + EXPECT_IMAGE_NON_NULL_AND_SIZE(icon_family.Get(32, 1), 256, 64); |
| +} |
| + |
| +} // namespace |