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.
33 #include "platform/DragImage.h"
35 #include "URLTestHelpers.h"
36 #include "core/rendering/RenderTheme.h"
37 #include "platform/geometry/IntSize.h"
38 #include "platform/graphics/Image.h"
39 #include "platform/graphics/skia/NativeImageSkia.h"
40 #include "platform/weborigin/KURL.h"
41 #include "third_party/skia/include/core/SkBitmap.h"
42 #include "wtf/OwnPtr.h"
43 #include "wtf/PassOwnPtr.h"
44 #include "wtf/PassRefPtr.h"
45 #include "wtf/RefPtr.h"
47 #include <gtest/gtest.h>
49 using namespace WebCore;
50 using blink::URLTestHelpers::toKURL;
54 class TestImage : public Image {
57 static PassRefPtr<TestImage> create(const IntSize& size)
59 return adoptRef(new TestImage(size));
62 explicit TestImage(const IntSize& size)
66 m_nativeImage = NativeImageSkia::create();
67 EXPECT_TRUE(m_nativeImage->bitmap().allocN32Pixels(size.width(), size.height()));
70 virtual IntSize size() const OVERRIDE
75 virtual PassRefPtr<NativeImageSkia> nativeImageForCurrentFrame() OVERRIDE
83 // Stub implementations of pure virtual Image functions.
84 virtual void destroyDecodedData(bool) OVERRIDE
88 virtual bool currentFrameKnownToBeOpaque() OVERRIDE
93 virtual void draw(GraphicsContext*, const FloatRect&, const FloatRect&, CompositeOperator, blink::WebBlendMode) OVERRIDE
101 RefPtr<NativeImageSkia> m_nativeImage;
104 TEST(DragImageTest, NullHandling)
106 EXPECT_FALSE(DragImage::create(0));
108 RefPtr<TestImage> nullTestImage(TestImage::create(IntSize()));
109 EXPECT_FALSE(DragImage::create(nullTestImage.get()));
112 TEST(DragImageTest, NonNullHandling)
114 RefPtr<TestImage> testImage(TestImage::create(IntSize(2, 2)));
115 OwnPtr<DragImage> dragImage = DragImage::create(testImage.get());
116 ASSERT_TRUE(dragImage);
118 dragImage->scale(0.5, 0.5);
119 IntSize size = dragImage->size();
120 EXPECT_EQ(1, size.width());
121 EXPECT_EQ(1, size.height());
123 dragImage->dissolveToFraction(0.5);
126 TEST(DragImageTest, CreateDragImage)
129 // Tests that the DrageImage implementation doesn't choke on null values
130 // of nativeImageForCurrentFrame().
131 RefPtr<TestImage> testImage(TestImage::create(IntSize()));
132 EXPECT_FALSE(DragImage::create(testImage.get()));
136 // Tests that the drag image is a deep copy.
137 RefPtr<TestImage> testImage(TestImage::create(IntSize(1, 1)));
138 OwnPtr<DragImage> dragImage = DragImage::create(testImage.get());
139 ASSERT_TRUE(dragImage);
140 SkAutoLockPixels lock1(dragImage->bitmap()), lock2(testImage->nativeImageForCurrentFrame()->bitmap());
141 EXPECT_NE(dragImage->bitmap().getPixels(), testImage->nativeImageForCurrentFrame()->bitmap().getPixels());
145 TEST(DragImageTest, TrimWhitspace)
147 KURL url = toKURL("http://www.example.com/");
148 String testLabel = " Example Example Example \n ";
149 String expectedLabel = "Example Example Example";
150 float deviceScaleFactor = 1.0f;
151 FontDescription fontDescription;
152 RenderTheme::theme().systemFont(WebCore::CSSValueNone, fontDescription);
154 OwnPtr<DragImage> testImage =
155 DragImage::create(url, testLabel, fontDescription, deviceScaleFactor);
156 OwnPtr<DragImage> expectedImage =
157 DragImage::create(url, expectedLabel, fontDescription, deviceScaleFactor);
159 EXPECT_EQ(testImage->size().width(), expectedImage->size().width());
162 } // anonymous namespace