1/*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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
13 * distribution.
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.
17 *
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.
29 */
30
31#include "config.h"
32
33#include <gtest/gtest.h>
34
35#include "DragImage.h"
36#include "Image.h"
37#include "NativeImageSkia.h"
38
39using namespace WebCore;
40
41namespace {
42
43class TestImage : public Image {
44public:
45
46    static PassRefPtr<TestImage> create(const IntSize& size)
47    {
48        return adoptRef(new TestImage(size));
49    }
50
51    explicit TestImage(const IntSize& size)
52        : Image(0)
53        , m_size(size)
54    {
55        m_nativeImage = new NativeImageSkia();
56        m_nativeImage->setConfig(SkBitmap::kARGB_8888_Config,
57                                 size.width(), size.height(), 0);
58        m_nativeImage->allocPixels();
59    }
60
61    virtual ~TestImage()
62    {
63        delete m_nativeImage;
64    }
65
66    virtual IntSize size() const
67    {
68        return m_size;
69    }
70
71    virtual NativeImagePtr nativeImageForCurrentFrame()
72    {
73        if (m_size.isZero())
74            return 0;
75
76        return m_nativeImage;
77    }
78
79    // Stub implementations of pure virtual Image functions.
80    virtual void destroyDecodedData(bool)
81    {
82    }
83
84    virtual unsigned int decodedSize() const
85    {
86        return 0u;
87    }
88
89    virtual void draw(WebCore::GraphicsContext*, const WebCore::FloatRect&,
90                      const WebCore::FloatRect&, WebCore::ColorSpace,
91                      WebCore::CompositeOperator)
92    {
93    }
94
95private:
96
97    IntSize m_size;
98
99    NativeImagePtr m_nativeImage;
100};
101
102TEST(DragImageTest, NullHandling)
103{
104    EXPECT_FALSE(createDragImageFromImage(0));
105
106    deleteDragImage(0);
107    EXPECT_TRUE(dragImageSize(0).isZero());
108    EXPECT_FALSE(scaleDragImage(0, FloatSize(0.5, 0.5)));
109    EXPECT_FALSE(dissolveDragImageToFraction(0, 0.5));
110    EXPECT_FALSE(createDragImageFromImage(0));
111    EXPECT_FALSE(createDragImageIconForCachedImage(0));
112}
113
114TEST(DragImageTest, NonNullHandling)
115{
116    RefPtr<TestImage> testImage(TestImage::create(IntSize(2, 2)));
117    DragImageRef dragImage = createDragImageFromImage(testImage.get());
118    ASSERT_TRUE(dragImage);
119
120    dragImage = scaleDragImage(dragImage, FloatSize(0.5, 0.5));
121    ASSERT_TRUE(dragImage);
122    IntSize size = dragImageSize(dragImage);
123    EXPECT_EQ(1, size.width());
124    EXPECT_EQ(1, size.height());
125
126    dragImage = dissolveDragImageToFraction(dragImage, 0.5);
127    ASSERT_TRUE(dragImage);
128
129    deleteDragImage(dragImage);
130}
131
132TEST(DragImageTest, CreateDragImage)
133{
134    {
135        // Tests that the DrageImage implementation doesn't choke on null values
136        // of nativeImageForCurrentFrame().
137        RefPtr<TestImage> testImage(TestImage::create(IntSize()));
138        EXPECT_FALSE(createDragImageFromImage(testImage.get()));
139    }
140
141    {
142        // Tests that the drag image is a deep copy.
143        RefPtr<TestImage> testImage(TestImage::create(IntSize(1, 1)));
144        DragImageRef dragImage = createDragImageFromImage(testImage.get());
145        ASSERT_TRUE(dragImage);
146        SkAutoLockPixels lock1(*dragImage), lock2(*(testImage->nativeImageForCurrentFrame()));
147        EXPECT_NE(dragImage->getPixels(), testImage->nativeImageForCurrentFrame()->getPixels());
148    }
149}
150
151} // anonymous namespace
152