2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "platform/DragImage.h"
34 #include "platform/fonts/FontDescription.h"
35 #include "platform/fonts/FontTraits.h"
36 #include "platform/geometry/IntSize.h"
37 #include "platform/graphics/BitmapImage.h"
38 #include "platform/graphics/Image.h"
39 #include "platform/weborigin/KURL.h"
40 #include "third_party/skia/include/core/SkColor.h"
41 #include "third_party/skia/include/core/SkImage.h"
42 #include "third_party/skia/include/core/SkPixelRef.h"
43 #include "third_party/skia/include/core/SkSurface.h"
44 #include "wtf/OwnPtr.h"
45 #include "wtf/PassOwnPtr.h"
46 #include "wtf/PassRefPtr.h"
47 #include "wtf/RefPtr.h"
48 #include <gtest/gtest.h>
52 class TestImage
: public Image
{
55 static PassRefPtr
<TestImage
> create(const IntSize
& size
)
57 return adoptRef(new TestImage(size
));
60 explicit TestImage(const IntSize
& size
)
62 RefPtr
<SkSurface
> surface
= adoptRef(SkSurface::NewRaster(
63 SkImageInfo::MakeN32(size
.width(), size
.height(), kPremul_SkAlphaType
)));
67 surface
->getCanvas()->clear(SK_ColorTRANSPARENT
);
68 m_image
= adoptRef(surface
->newImageSnapshot());
71 explicit TestImage(PassRefPtr
<SkImage
> image
)
74 IntSize
size() const override
77 return IntSize(m_image
->width(), m_image
->height());
80 PassRefPtr
<SkImage
> imageForCurrentFrame() override
85 // Stub implementations of pure virtual Image functions.
86 void destroyDecodedData(bool) override
90 bool currentFrameKnownToBeOpaque() override
95 void draw(SkCanvas
*, const SkPaint
&, const FloatRect
&, const FloatRect
&, RespectImageOrientationEnum
, ImageClampingMode
) override
100 RefPtr
<SkImage
> m_image
;
103 TEST(DragImageTest
, NullHandling
)
105 EXPECT_FALSE(DragImage::create(0));
107 RefPtr
<TestImage
> nullTestImage(TestImage::create(IntSize()));
108 EXPECT_FALSE(DragImage::create(nullTestImage
.get()));
111 TEST(DragImageTest
, NonNullHandling
)
113 RefPtr
<TestImage
> testImage(TestImage::create(IntSize(2, 2)));
114 OwnPtr
<DragImage
> dragImage
= DragImage::create(testImage
.get());
115 ASSERT_TRUE(dragImage
);
117 dragImage
->scale(0.5, 0.5);
118 IntSize size
= dragImage
->size();
119 EXPECT_EQ(1, size
.width());
120 EXPECT_EQ(1, size
.height());
123 TEST(DragImageTest
, CreateDragImage
)
126 // Tests that the DrageImage implementation doesn't choke on null values
127 // of imageForCurrentFrame().
128 RefPtr
<TestImage
> testImage(TestImage::create(IntSize()));
129 EXPECT_FALSE(DragImage::create(testImage
.get()));
133 TEST(DragImageTest
, TrimWhitespace
)
135 KURL
url(ParsedURLString
, "http://www.example.com/");
136 String testLabel
= " Example Example Example \n ";
137 String expectedLabel
= "Example Example Example";
138 float deviceScaleFactor
= 1.0f
;
140 FontDescription fontDescription
;
141 fontDescription
.firstFamily().setFamily("Arial");
142 fontDescription
.setSpecifiedSize(16);
143 fontDescription
.setIsAbsoluteSize(true);
144 fontDescription
.setGenericFamily(FontDescription::NoFamily
);
145 fontDescription
.setWeight(FontWeightNormal
);
146 fontDescription
.setStyle(FontStyleNormal
);
148 OwnPtr
<DragImage
> testImage
=
149 DragImage::create(url
, testLabel
, fontDescription
, deviceScaleFactor
);
150 OwnPtr
<DragImage
> expectedImage
=
151 DragImage::create(url
, expectedLabel
, fontDescription
, deviceScaleFactor
);
153 EXPECT_EQ(testImage
->size().width(), expectedImage
->size().width());
156 // SkPixelRef which fails to lock, as a lazy pixel ref might if its pixels
157 // cannot be generated.
158 class InvalidPixelRef
: public SkPixelRef
{
160 InvalidPixelRef(const SkImageInfo
& info
) : SkPixelRef(info
) { }
162 bool onNewLockPixels(LockRec
*) override
{ return false; }
163 void onUnlockPixels() override
{ ASSERT_NOT_REACHED(); }
166 TEST(DragImageTest
, InvalidRotatedBitmapImage
)
168 // This test is mostly useful with MSAN builds, which can actually detect
169 // the use of uninitialized memory.
171 // Create a BitmapImage which will fail to produce pixels, and hence not
173 SkImageInfo info
= SkImageInfo::MakeN32Premul(100, 100);
174 RefPtr
<SkPixelRef
> pixelRef
= adoptRef(new InvalidPixelRef(info
));
175 SkBitmap invalidBitmap
;
176 invalidBitmap
.setInfo(info
);
177 invalidBitmap
.setPixelRef(pixelRef
.get());
178 RefPtr
<BitmapImage
> image
= BitmapImage::createWithOrientationForTesting(invalidBitmap
, OriginRightTop
);
180 // Create a DragImage from it. In MSAN builds, this will cause a failure if
181 // the pixel memory is not initialized, if we have to respect non-default
183 OwnPtr
<DragImage
> dragImage
= DragImage::create(image
.get(), RespectImageOrientation
);
185 // With an invalid pixel ref, BitmapImage should have no backing SkImage => we don't allocate
187 ASSERT_FALSE(dragImage
);
190 TEST(DragImageTest
, InterpolationNone
)
192 SkBitmap expectedBitmap
;
193 expectedBitmap
.allocN32Pixels(4, 4);
195 SkAutoLockPixels
lock(expectedBitmap
);
196 expectedBitmap
.eraseArea(SkIRect::MakeXYWH(0, 0, 2, 2), 0xFFFFFFFF);
197 expectedBitmap
.eraseArea(SkIRect::MakeXYWH(0, 2, 2, 2), 0xFF000000);
198 expectedBitmap
.eraseArea(SkIRect::MakeXYWH(2, 0, 2, 2), 0xFF000000);
199 expectedBitmap
.eraseArea(SkIRect::MakeXYWH(2, 2, 2, 2), 0xFFFFFFFF);
203 testBitmap
.allocN32Pixels(2, 2);
205 SkAutoLockPixels
lock(testBitmap
);
206 testBitmap
.eraseArea(SkIRect::MakeXYWH(0, 0, 1, 1), 0xFFFFFFFF);
207 testBitmap
.eraseArea(SkIRect::MakeXYWH(0, 1, 1, 1), 0xFF000000);
208 testBitmap
.eraseArea(SkIRect::MakeXYWH(1, 0, 1, 1), 0xFF000000);
209 testBitmap
.eraseArea(SkIRect::MakeXYWH(1, 1, 1, 1), 0xFFFFFFFF);
212 RefPtr
<TestImage
> testImage
= adoptRef(new TestImage(adoptRef(SkImage::NewFromBitmap(testBitmap
))));
213 OwnPtr
<DragImage
> dragImage
= DragImage::create(testImage
.get(), DoNotRespectImageOrientation
, 1, InterpolationNone
);
214 ASSERT_TRUE(dragImage
);
215 dragImage
->scale(2, 2);
216 const SkBitmap
& dragBitmap
= dragImage
->bitmap();
218 SkAutoLockPixels
lock1(dragBitmap
);
219 SkAutoLockPixels
lock2(expectedBitmap
);
220 for (int x
= 0; x
< dragBitmap
.width(); ++x
)
221 for (int y
= 0; y
< dragBitmap
.height(); ++y
)
222 EXPECT_EQ(expectedBitmap
.getColor(x
, y
), dragBitmap
.getColor(x
, y
));