[email protected] | 40a080e | 2011-02-11 17:42:28 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ui/gfx/image.h" |
| 6 | |
| 7 | #include "base/scoped_ptr.h" |
| 8 | #include "testing/gtest/include/gtest/gtest.h" |
| 9 | #include "third_party/skia/include/core/SkBitmap.h" |
| 10 | #include "ui/gfx/image_unittest.h" |
| 11 | |
| 12 | #if defined(OS_LINUX) |
| 13 | #include <gtk/gtk.h> |
[email protected] | 00ed945 | 2011-02-13 00:13:44 | [diff] [blame] | 14 | #include "ui/gfx/gtk_util.h" |
[email protected] | 40a080e | 2011-02-11 17:42:28 | [diff] [blame] | 15 | #elif defined(OS_MACOSX) |
| 16 | #include "base/mac/mac_util.h" |
| 17 | #include "skia/ext/skia_utils_mac.h" |
| 18 | #endif |
| 19 | |
| 20 | namespace { |
| 21 | |
[email protected] | 06ec9a2 | 2011-02-17 17:24:07 | [diff] [blame] | 22 | using namespace gfx::test; |
[email protected] | 40a080e | 2011-02-11 17:42:28 | [diff] [blame] | 23 | |
| 24 | #if defined(TOOLKIT_VIEWS) |
| 25 | const bool kUsesSkiaNatively = true; |
| 26 | #else |
| 27 | const bool kUsesSkiaNatively = false; |
| 28 | #endif |
| 29 | |
| 30 | class ImageTest : public testing::Test { |
| 31 | public: |
[email protected] | 06ec9a2 | 2011-02-17 17:24:07 | [diff] [blame] | 32 | size_t GetRepCount(const gfx::Image& image) { |
[email protected] | 40a080e | 2011-02-11 17:42:28 | [diff] [blame] | 33 | return image.representations_.size(); |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | TEST_F(ImageTest, SkiaToSkia) { |
[email protected] | 06ec9a2 | 2011-02-17 17:24:07 | [diff] [blame] | 38 | gfx::Image image(CreateBitmap()); |
[email protected] | 40a080e | 2011-02-11 17:42:28 | [diff] [blame] | 39 | const SkBitmap* bitmap = static_cast<const SkBitmap*>(image); |
| 40 | EXPECT_TRUE(bitmap); |
| 41 | EXPECT_FALSE(bitmap->isNull()); |
| 42 | EXPECT_EQ(1U, GetRepCount(image)); |
| 43 | |
| 44 | // Make sure double conversion doesn't happen. |
| 45 | bitmap = static_cast<const SkBitmap*>(image); |
| 46 | EXPECT_TRUE(bitmap); |
| 47 | EXPECT_FALSE(bitmap->isNull()); |
| 48 | EXPECT_EQ(1U, GetRepCount(image)); |
[email protected] | c41b06f | 2011-02-24 15:53:26 | [diff] [blame^] | 49 | |
| 50 | EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep)); |
| 51 | if (!kUsesSkiaNatively) |
| 52 | EXPECT_FALSE(image.HasRepresentation(GetPlatformRepresentationType())); |
[email protected] | 40a080e | 2011-02-11 17:42:28 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | TEST_F(ImageTest, SkiaToSkiaRef) { |
[email protected] | 06ec9a2 | 2011-02-17 17:24:07 | [diff] [blame] | 56 | gfx::Image image(CreateBitmap()); |
[email protected] | 40a080e | 2011-02-11 17:42:28 | [diff] [blame] | 57 | |
| 58 | const SkBitmap& bitmap = static_cast<const SkBitmap&>(image); |
| 59 | EXPECT_FALSE(bitmap.isNull()); |
| 60 | EXPECT_EQ(1U, GetRepCount(image)); |
| 61 | |
| 62 | const SkBitmap* bitmap1 = static_cast<const SkBitmap*>(image); |
| 63 | EXPECT_FALSE(bitmap1->isNull()); |
| 64 | EXPECT_EQ(1U, GetRepCount(image)); |
[email protected] | c41b06f | 2011-02-24 15:53:26 | [diff] [blame^] | 65 | |
| 66 | EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep)); |
| 67 | if (!kUsesSkiaNatively) |
| 68 | EXPECT_FALSE(image.HasRepresentation(GetPlatformRepresentationType())); |
[email protected] | 40a080e | 2011-02-11 17:42:28 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | TEST_F(ImageTest, SkiaToPlatform) { |
[email protected] | 06ec9a2 | 2011-02-17 17:24:07 | [diff] [blame] | 72 | gfx::Image image(CreateBitmap()); |
[email protected] | 40a080e | 2011-02-11 17:42:28 | [diff] [blame] | 73 | const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; |
| 74 | |
[email protected] | c41b06f | 2011-02-24 15:53:26 | [diff] [blame^] | 75 | EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep)); |
| 76 | if (!kUsesSkiaNatively) |
| 77 | EXPECT_FALSE(image.HasRepresentation(GetPlatformRepresentationType())); |
| 78 | |
[email protected] | 40a080e | 2011-02-11 17:42:28 | [diff] [blame] | 79 | EXPECT_TRUE(static_cast<PlatformImage>(image)); |
| 80 | EXPECT_EQ(kRepCount, GetRepCount(image)); |
| 81 | |
| 82 | const SkBitmap& bitmap = static_cast<const SkBitmap&>(image); |
| 83 | EXPECT_FALSE(bitmap.isNull()); |
| 84 | EXPECT_EQ(kRepCount, GetRepCount(image)); |
[email protected] | c41b06f | 2011-02-24 15:53:26 | [diff] [blame^] | 85 | |
| 86 | EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep)); |
| 87 | EXPECT_TRUE(image.HasRepresentation(GetPlatformRepresentationType())); |
[email protected] | 40a080e | 2011-02-11 17:42:28 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | TEST_F(ImageTest, PlatformToSkia) { |
[email protected] | 06ec9a2 | 2011-02-17 17:24:07 | [diff] [blame] | 91 | gfx::Image image(CreatePlatformImage()); |
[email protected] | 40a080e | 2011-02-11 17:42:28 | [diff] [blame] | 92 | const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; |
| 93 | |
[email protected] | c41b06f | 2011-02-24 15:53:26 | [diff] [blame^] | 94 | EXPECT_TRUE(image.HasRepresentation(GetPlatformRepresentationType())); |
| 95 | if (!kUsesSkiaNatively) |
| 96 | EXPECT_FALSE(image.HasRepresentation(gfx::Image::kSkBitmapRep)); |
| 97 | |
[email protected] | 40a080e | 2011-02-11 17:42:28 | [diff] [blame] | 98 | const SkBitmap* bitmap = static_cast<const SkBitmap*>(image); |
| 99 | EXPECT_TRUE(bitmap); |
| 100 | EXPECT_FALSE(bitmap->isNull()); |
| 101 | EXPECT_EQ(kRepCount, GetRepCount(image)); |
| 102 | |
| 103 | EXPECT_TRUE(static_cast<PlatformImage>(image)); |
| 104 | EXPECT_EQ(kRepCount, GetRepCount(image)); |
[email protected] | c41b06f | 2011-02-24 15:53:26 | [diff] [blame^] | 105 | |
| 106 | EXPECT_TRUE(image.HasRepresentation(gfx::Image::kSkBitmapRep)); |
[email protected] | 40a080e | 2011-02-11 17:42:28 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | TEST_F(ImageTest, PlatformToPlatform) { |
[email protected] | 06ec9a2 | 2011-02-17 17:24:07 | [diff] [blame] | 110 | gfx::Image image(CreatePlatformImage()); |
[email protected] | 40a080e | 2011-02-11 17:42:28 | [diff] [blame] | 111 | EXPECT_TRUE(static_cast<PlatformImage>(image)); |
| 112 | EXPECT_EQ(1U, GetRepCount(image)); |
| 113 | |
| 114 | // Make sure double conversion doesn't happen. |
| 115 | EXPECT_TRUE(static_cast<PlatformImage>(image)); |
| 116 | EXPECT_EQ(1U, GetRepCount(image)); |
[email protected] | c41b06f | 2011-02-24 15:53:26 | [diff] [blame^] | 117 | |
| 118 | EXPECT_TRUE(image.HasRepresentation(GetPlatformRepresentationType())); |
| 119 | if (!kUsesSkiaNatively) |
| 120 | EXPECT_FALSE(image.HasRepresentation(gfx::Image::kSkBitmapRep)); |
[email protected] | 40a080e | 2011-02-11 17:42:28 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | TEST_F(ImageTest, CheckSkiaColor) { |
[email protected] | 06ec9a2 | 2011-02-17 17:24:07 | [diff] [blame] | 124 | gfx::Image image(CreatePlatformImage()); |
[email protected] | 40a080e | 2011-02-11 17:42:28 | [diff] [blame] | 125 | const SkBitmap& bitmap(image); |
[email protected] | 5d29a5ce | 2011-02-11 18:37:00 | [diff] [blame] | 126 | |
| 127 | SkAutoLockPixels auto_lock(bitmap); |
[email protected] | 40a080e | 2011-02-11 17:42:28 | [diff] [blame] | 128 | uint32_t* pixel = bitmap.getAddr32(10, 10); |
| 129 | EXPECT_EQ(SK_ColorRED, *pixel); |
| 130 | } |
| 131 | |
| 132 | // Integration tests with UI toolkit frameworks require linking against the |
| 133 | // Views library and cannot be here (gfx_unittests doesn't include it). They |
| 134 | // instead live in /chrome/browser/ui/tests/ui_gfx_image_unittest.cc. |
| 135 | |
| 136 | } // namespace |