Extend platform layer so it can pass blend modes to the compositing calls
[WebKit-https.git] / Source / WebKit / chromium / tests / DragImageTest.cpp
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 "DragImage.h"
34
35 #include "Image.h"
36 #include "NativeImageSkia.h"
37 #include <gtest/gtest.h>
38 #include <wtf/PassOwnPtr.h>
39
40 using namespace WebCore;
41
42 namespace {
43
44 class TestImage : public Image {
45 public:
46
47     static PassRefPtr<TestImage> create(const IntSize& size)
48     {
49         return adoptRef(new TestImage(size));
50     }
51
52     explicit TestImage(const IntSize& size)
53         : Image(0)
54         , m_size(size)
55     {
56         m_nativeImage = adoptPtr(new NativeImageSkia());
57         m_nativeImage->bitmap().setConfig(SkBitmap::kARGB_8888_Config,
58                                           size.width(), size.height(), 0);
59         m_nativeImage->bitmap().allocPixels();
60     }
61
62     virtual IntSize size() const
63     {
64         return m_size;
65     }
66
67     virtual NativeImagePtr nativeImageForCurrentFrame()
68     {
69         if (m_size.isZero())
70             return 0;
71
72         return m_nativeImage.get();
73     }
74
75     // Stub implementations of pure virtual Image functions.
76     virtual void destroyDecodedData(bool)
77     {
78     }
79
80     virtual unsigned int decodedSize() const
81     {
82         return 0u;
83     }
84
85     virtual void draw(WebCore::GraphicsContext*, const WebCore::FloatRect&,
86                       const WebCore::FloatRect&, WebCore::ColorSpace,
87                       WebCore::CompositeOperator, WebCore::BlendMode)
88     {
89     }
90
91 private:
92
93     IntSize m_size;
94
95     OwnPtr<NativeImagePtr> m_nativeImage;
96 };
97
98 TEST(DragImageTest, NullHandling)
99 {
100     EXPECT_FALSE(createDragImageFromImage(0));
101
102     deleteDragImage(0);
103     EXPECT_TRUE(dragImageSize(0).isZero());
104     EXPECT_FALSE(scaleDragImage(0, FloatSize(0.5, 0.5)));
105     EXPECT_FALSE(dissolveDragImageToFraction(0, 0.5));
106     EXPECT_FALSE(createDragImageFromImage(0));
107     EXPECT_FALSE(createDragImageIconForCachedImage(0));
108 }
109
110 TEST(DragImageTest, NonNullHandling)
111 {
112     RefPtr<TestImage> testImage(TestImage::create(IntSize(2, 2)));
113     DragImageRef dragImage = createDragImageFromImage(testImage.get());
114     ASSERT_TRUE(dragImage);
115
116     dragImage = scaleDragImage(dragImage, FloatSize(0.5, 0.5));
117     ASSERT_TRUE(dragImage);
118     IntSize size = dragImageSize(dragImage);
119     EXPECT_EQ(1, size.width());
120     EXPECT_EQ(1, size.height());
121
122     dragImage = dissolveDragImageToFraction(dragImage, 0.5);
123     ASSERT_TRUE(dragImage);
124
125     deleteDragImage(dragImage);
126 }
127
128 TEST(DragImageTest, CreateDragImage)
129 {
130     {
131         // Tests that the DrageImage implementation doesn't choke on null values
132         // of nativeImageForCurrentFrame().
133         RefPtr<TestImage> testImage(TestImage::create(IntSize()));
134         EXPECT_FALSE(createDragImageFromImage(testImage.get()));
135     }
136
137     {
138         // Tests that the drag image is a deep copy.
139         RefPtr<TestImage> testImage(TestImage::create(IntSize(1, 1)));
140         DragImageRef dragImage = createDragImageFromImage(testImage.get());
141         ASSERT_TRUE(dragImage);
142         SkAutoLockPixels lock1(*dragImage->bitmap), lock2(testImage->nativeImageForCurrentFrame()->bitmap());
143         EXPECT_NE(dragImage->bitmap->getPixels(), testImage->nativeImageForCurrentFrame()->bitmap().getPixels());
144     }
145 }
146
147 } // anonymous namespace