[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> |
| 14 | #include "gfx/gtk_util.h" |
| 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 | |
| 22 | using namespace ui::gfx::test; |
| 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: |
| 32 | size_t GetRepCount(const ui::gfx::Image& image) { |
| 33 | return image.representations_.size(); |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | TEST_F(ImageTest, SkiaToSkia) { |
| 38 | ui::gfx::Image image(CreateBitmap()); |
| 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)); |
| 49 | } |
| 50 | |
| 51 | TEST_F(ImageTest, SkiaToSkiaRef) { |
| 52 | ui::gfx::Image image(CreateBitmap()); |
| 53 | |
| 54 | const SkBitmap& bitmap = static_cast<const SkBitmap&>(image); |
| 55 | EXPECT_FALSE(bitmap.isNull()); |
| 56 | EXPECT_EQ(1U, GetRepCount(image)); |
| 57 | |
| 58 | const SkBitmap* bitmap1 = static_cast<const SkBitmap*>(image); |
| 59 | EXPECT_FALSE(bitmap1->isNull()); |
| 60 | EXPECT_EQ(1U, GetRepCount(image)); |
| 61 | } |
| 62 | |
| 63 | TEST_F(ImageTest, SkiaToPlatform) { |
| 64 | ui::gfx::Image image(CreateBitmap()); |
| 65 | const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; |
| 66 | |
| 67 | EXPECT_TRUE(static_cast<PlatformImage>(image)); |
| 68 | EXPECT_EQ(kRepCount, GetRepCount(image)); |
| 69 | |
| 70 | const SkBitmap& bitmap = static_cast<const SkBitmap&>(image); |
| 71 | EXPECT_FALSE(bitmap.isNull()); |
| 72 | EXPECT_EQ(kRepCount, GetRepCount(image)); |
| 73 | } |
| 74 | |
| 75 | TEST_F(ImageTest, PlatformToSkia) { |
| 76 | ui::gfx::Image image(CreatePlatformImage()); |
| 77 | const size_t kRepCount = kUsesSkiaNatively ? 1U : 2U; |
| 78 | |
| 79 | const SkBitmap* bitmap = static_cast<const SkBitmap*>(image); |
| 80 | EXPECT_TRUE(bitmap); |
| 81 | EXPECT_FALSE(bitmap->isNull()); |
| 82 | EXPECT_EQ(kRepCount, GetRepCount(image)); |
| 83 | |
| 84 | EXPECT_TRUE(static_cast<PlatformImage>(image)); |
| 85 | EXPECT_EQ(kRepCount, GetRepCount(image)); |
| 86 | } |
| 87 | |
| 88 | TEST_F(ImageTest, PlatformToPlatform) { |
| 89 | ui::gfx::Image image(CreatePlatformImage()); |
| 90 | EXPECT_TRUE(static_cast<PlatformImage>(image)); |
| 91 | EXPECT_EQ(1U, GetRepCount(image)); |
| 92 | |
| 93 | // Make sure double conversion doesn't happen. |
| 94 | EXPECT_TRUE(static_cast<PlatformImage>(image)); |
| 95 | EXPECT_EQ(1U, GetRepCount(image)); |
| 96 | } |
| 97 | |
| 98 | TEST_F(ImageTest, CheckSkiaColor) { |
| 99 | ui::gfx::Image image(CreatePlatformImage()); |
| 100 | const SkBitmap& bitmap(image); |
[email protected] | 5d29a5ce | 2011-02-11 18:37:00 | [diff] [blame^] | 101 | |
| 102 | SkAutoLockPixels auto_lock(bitmap); |
[email protected] | 40a080e | 2011-02-11 17:42:28 | [diff] [blame] | 103 | uint32_t* pixel = bitmap.getAddr32(10, 10); |
| 104 | EXPECT_EQ(SK_ColorRED, *pixel); |
| 105 | } |
| 106 | |
| 107 | // Integration tests with UI toolkit frameworks require linking against the |
| 108 | // Views library and cannot be here (gfx_unittests doesn't include it). They |
| 109 | // instead live in /chrome/browser/ui/tests/ui_gfx_image_unittest.cc. |
| 110 | |
| 111 | } // namespace |