| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 #include "third_party/skia/include/core/SkBitmap.h" | 7 #include "third_party/skia/include/core/SkBitmap.h" |
| 8 #include "ui/gfx/image.h" | 8 #include "ui/gfx/image.h" |
| 9 #include "ui/gfx/image_unittest.h" | 9 #include "ui/gfx/image_unittest_util.h" |
| 10 | 10 |
| 11 #if defined(OS_LINUX) | 11 #if defined(OS_LINUX) |
| 12 #include <gtk/gtk.h> | 12 #include <gtk/gtk.h> |
| 13 #include "ui/gfx/gtk_util.h" | 13 #include "ui/gfx/gtk_util.h" |
| 14 #elif defined(OS_MACOSX) | 14 #elif defined(OS_MACOSX) |
| 15 #include "base/mac/mac_util.h" | 15 #include "base/mac/mac_util.h" |
| 16 #include "skia/ext/skia_utils_mac.h" | 16 #include "skia/ext/skia_utils_mac.h" |
| 17 #endif | 17 #endif |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 #if defined(TOOLKIT_VIEWS) | 21 #if defined(TOOLKIT_VIEWS) |
| 22 const bool kUsesSkiaNatively = true; | 22 const bool kUsesSkiaNatively = true; |
| 23 #else | 23 #else |
| 24 const bool kUsesSkiaNatively = false; | 24 const bool kUsesSkiaNatively = false; |
| 25 #endif | 25 #endif |
| 26 | 26 |
| 27 class ImageTest : public testing::Test { | 27 class ImageTest : public testing::Test { |
| 28 public: | 28 public: |
| 29 size_t GetRepCount(const gfx::Image& image) { | 29 size_t GetRepCount(const gfx::Image& image) { |
| 30 return image.representations_.size(); | 30 return image.representations_.size(); |
| 31 } | 31 } |
| 32 }; | 32 }; |
| 33 | 33 |
| 34 namespace gt = gfx::test; | 34 namespace gt = gfx::test; |
| 35 | 35 |
| 36 TEST_F(ImageTest, SkiaToSkia) { | 36 TEST_F(ImageTest, SkiaToSkia) { |
| 37 gfx::Image image(gt::CreateBitmap()); | 37 gfx::Image image(gt::CreateBitmap(25, 25)); |
| 38 const SkBitmap* bitmap = static_cast<const SkBitmap*>(image); | 38 const SkBitmap* bitmap = static_cast<const SkBitmap*>(image); |
| 39 EXPECT_TRUE(bitmap); | 39 EXPECT_TRUE(bitmap); |
| 40 EXPECT_FALSE(bitmap->isNull()); | 40 EXPECT_FALSE(bitmap->isNull()); |
| 41 EXPECT_EQ(1U, GetRepCount(image)); | 41 EXPECT_EQ(1U, GetRepCount(image)); |
| 42 | 42 |
| 43 // Make sure double conversion doesn't happen. | 43 // Make sure double conversion doesn't happen. |
| 44 bitmap = static_cast<const SkBitmap*>(image); | 44 bitmap = static_cast<const SkBitmap*>(image); |
| 45 EXPECT_TRUE(bitmap); | 45 EXPECT_TRUE(bitmap); |
| 46 EXPECT_FALSE(bitmap->isNull()); | 46 EXPECT_FALSE(bitmap->isNull()); |
| 47 EXPECT_EQ(1U, GetRepCount(image)); | 47 EXPECT_EQ(1U, GetRepCount(image)); |
| 48 | 48 |
| 49 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep)); | 49 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep)); |
| 50 if (!kUsesSkiaNatively) | 50 if (!kUsesSkiaNatively) |
| 51 EXPECT_FALSE(image.HasRepresentation(gt::GetPlatformRepresentationType())); | 51 EXPECT_FALSE(image.HasRepresentation(gt::GetPlatformRepresentationType())); |
| 52 } | 52 } |
| 53 | 53 |
| 54 TEST_F(ImageTest, SkiaToSkiaRef) { | 54 TEST_F(ImageTest, SkiaToSkiaRef) { |
| 55 gfx::Image image(gt::CreateBitmap()); | 55 gfx::Image image(gt::CreateBitmap(25, 25)); |
| 56 | 56 |
| 57 const SkBitmap& bitmap = static_cast<const SkBitmap&>(image); | 57 const SkBitmap& bitmap = static_cast<const SkBitmap&>(image); |
| 58 EXPECT_FALSE(bitmap.isNull()); | 58 EXPECT_FALSE(bitmap.isNull()); |
| 59 EXPECT_EQ(1U, GetRepCount(image)); | 59 EXPECT_EQ(1U, GetRepCount(image)); |
| 60 | 60 |
| 61 const SkBitmap* bitmap1 = static_cast<const SkBitmap*>(image); | 61 const SkBitmap* bitmap1 = static_cast<const SkBitmap*>(image); |
| 62 EXPECT_FALSE(bitmap1->isNull()); | 62 EXPECT_FALSE(bitmap1->isNull()); |
| 63 EXPECT_EQ(1U, GetRepCount(image)); | 63 EXPECT_EQ(1U, GetRepCount(image)); |
| 64 | 64 |
| 65 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep)); | 65 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep)); |
| 66 if (!kUsesSkiaNatively) | 66 if (!kUsesSkiaNatively) |
| 67 EXPECT_FALSE(image.HasRepresentation(gt::GetPlatformRepresentationType())); | 67 EXPECT_FALSE(image.HasRepresentation(gt::GetPlatformRepresentationType())); |
| 68 } | 68 } |
| 69 | 69 |
| 70 TEST_F(ImageTest, SkiaToPlatform) { | 70 TEST_F(ImageTest, SkiaToPlatform) { |
| 71 gfx::Image image(gt::CreateBitmap()); | 71 gfx::Image image(gt::CreateBitmap(25, 25)); |
| 72 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; | 72 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; |
| 73 | 73 |
| 74 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep)); | 74 EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep)); |
| 75 if (!kUsesSkiaNatively) | 75 if (!kUsesSkiaNatively) |
| 76 EXPECT_FALSE(image.HasRepresentation(gt::GetPlatformRepresentationType())); | 76 EXPECT_FALSE(image.HasRepresentation(gt::GetPlatformRepresentationType())); |
| 77 | 77 |
| 78 EXPECT_TRUE(static_cast<gt::PlatformImage>(image)); | 78 EXPECT_TRUE(static_cast<gt::PlatformImage>(image)); |
| 79 EXPECT_EQ(kRepCount, GetRepCount(image)); | 79 EXPECT_EQ(kRepCount, GetRepCount(image)); |
| 80 | 80 |
| 81 const SkBitmap& bitmap = static_cast<const SkBitmap&>(image); | 81 const SkBitmap& bitmap = static_cast<const SkBitmap&>(image); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 const SkBitmap& bitmap(image); | 124 const SkBitmap& bitmap(image); |
| 125 | 125 |
| 126 SkAutoLockPixels auto_lock(bitmap); | 126 SkAutoLockPixels auto_lock(bitmap); |
| 127 uint32_t* pixel = bitmap.getAddr32(10, 10); | 127 uint32_t* pixel = bitmap.getAddr32(10, 10); |
| 128 EXPECT_EQ(SK_ColorRED, *pixel); | 128 EXPECT_EQ(SK_ColorRED, *pixel); |
| 129 } | 129 } |
| 130 | 130 |
| 131 TEST_F(ImageTest, SwapRepresentations) { | 131 TEST_F(ImageTest, SwapRepresentations) { |
| 132 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; | 132 const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; |
| 133 | 133 |
| 134 gfx::Image image1(gt::CreateBitmap()); | 134 gfx::Image image1(gt::CreateBitmap(25, 25)); |
| 135 const SkBitmap* bitmap1 = image1; | 135 const SkBitmap* bitmap1 = image1; |
| 136 EXPECT_EQ(1U, GetRepCount(image1)); | 136 EXPECT_EQ(1U, GetRepCount(image1)); |
| 137 | 137 |
| 138 gfx::Image image2(gt::CreatePlatformImage()); | 138 gfx::Image image2(gt::CreatePlatformImage()); |
| 139 const SkBitmap* bitmap2 = image2; | 139 const SkBitmap* bitmap2 = image2; |
| 140 gt::PlatformImage platform_image = static_cast<gt::PlatformImage>(image2); | 140 gt::PlatformImage platform_image = static_cast<gt::PlatformImage>(image2); |
| 141 EXPECT_EQ(kRepCount, GetRepCount(image2)); | 141 EXPECT_EQ(kRepCount, GetRepCount(image2)); |
| 142 | 142 |
| 143 image1.SwapRepresentations(&image2); | 143 image1.SwapRepresentations(&image2); |
| 144 | 144 |
| 145 EXPECT_EQ(bitmap2, static_cast<const SkBitmap*>(image1)); | 145 EXPECT_EQ(bitmap2, static_cast<const SkBitmap*>(image1)); |
| 146 EXPECT_EQ(platform_image, static_cast<gt::PlatformImage>(image1)); | 146 EXPECT_EQ(platform_image, static_cast<gt::PlatformImage>(image1)); |
| 147 EXPECT_EQ(bitmap1, static_cast<const SkBitmap*>(image2)); | 147 EXPECT_EQ(bitmap1, static_cast<const SkBitmap*>(image2)); |
| 148 EXPECT_EQ(kRepCount, GetRepCount(image1)); | 148 EXPECT_EQ(kRepCount, GetRepCount(image1)); |
| 149 EXPECT_EQ(1U, GetRepCount(image2)); | 149 EXPECT_EQ(1U, GetRepCount(image2)); |
| 150 } | 150 } |
| 151 | 151 |
| 152 TEST_F(ImageTest, MultiResolutionSkBitmap) { |
| 153 const int width1 = 10; |
| 154 const int height1 = 12; |
| 155 const int width2 = 20; |
| 156 const int height2 = 24; |
| 157 |
| 158 std::vector<const SkBitmap*> bitmaps; |
| 159 bitmaps.push_back(gt::CreateBitmap(width1, height1)); |
| 160 bitmaps.push_back(gt::CreateBitmap(width2, height2)); |
| 161 gfx::Image image(bitmaps); |
| 162 |
| 163 EXPECT_EQ(1u, GetRepCount(image)); |
| 164 EXPECT_EQ(2u, image.GetNumberOfSkBitmaps()); |
| 165 |
| 166 const SkBitmap* bitmap1 = image.GetSkBitmapAtIndex(0); |
| 167 EXPECT_TRUE(bitmap1); |
| 168 const SkBitmap* bitmap2 = image.GetSkBitmapAtIndex(1); |
| 169 EXPECT_TRUE(bitmap2); |
| 170 |
| 171 if (bitmap1->width() == width1) { |
| 172 EXPECT_EQ(bitmap1->height(), height1); |
| 173 EXPECT_EQ(bitmap2->width(), width2); |
| 174 EXPECT_EQ(bitmap2->height(), height2); |
| 175 } else { |
| 176 EXPECT_EQ(bitmap1->width(), width2); |
| 177 EXPECT_EQ(bitmap1->height(), height2); |
| 178 EXPECT_EQ(bitmap2->width(), width1); |
| 179 EXPECT_EQ(bitmap2->height(), height1); |
| 180 } |
| 181 |
| 182 // Sanity check. |
| 183 EXPECT_EQ(1u, GetRepCount(image)); |
| 184 EXPECT_EQ(2u, image.GetNumberOfSkBitmaps()); |
| 185 } |
| 186 |
| 152 // Integration tests with UI toolkit frameworks require linking against the | 187 // Integration tests with UI toolkit frameworks require linking against the |
| 153 // Views library and cannot be here (gfx_unittests doesn't include it). They | 188 // Views library and cannot be here (gfx_unittests doesn't include it). They |
| 154 // instead live in /chrome/browser/ui/tests/ui_gfx_image_unittest.cc. | 189 // instead live in /chrome/browser/ui/tests/ui_gfx_image_unittest.cc. |
| 155 | 190 |
| 156 } // namespace | 191 } // namespace |
| OLD | NEW |