Home | History | Annotate | Download | only in tests
      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 "core/platform/DragImage.h"
     34 
     35 #include "platform/geometry/IntSize.h"
     36 #include "platform/graphics/Image.h"
     37 #include "platform/graphics/skia/NativeImageSkia.h"
     38 #include "third_party/skia/include/core/SkBitmap.h"
     39 #include "wtf/OwnPtr.h"
     40 #include "wtf/PassOwnPtr.h"
     41 #include "wtf/PassRefPtr.h"
     42 #include "wtf/RefPtr.h"
     43 
     44 #include <gtest/gtest.h>
     45 
     46 using namespace WebCore;
     47 
     48 namespace {
     49 
     50 class TestImage : public Image {
     51 public:
     52 
     53     static PassRefPtr<TestImage> create(const IntSize& size)
     54     {
     55         return adoptRef(new TestImage(size));
     56     }
     57 
     58     explicit TestImage(const IntSize& size)
     59         : Image(0)
     60         , m_size(size)
     61     {
     62         m_nativeImage = NativeImageSkia::create();
     63         m_nativeImage->bitmap().setConfig(SkBitmap::kARGB_8888_Config,
     64                                           size.width(), size.height(), 0);
     65         m_nativeImage->bitmap().allocPixels();
     66     }
     67 
     68     virtual IntSize size() const OVERRIDE
     69     {
     70         return m_size;
     71     }
     72 
     73     virtual PassRefPtr<NativeImageSkia> nativeImageForCurrentFrame() OVERRIDE
     74     {
     75         if (m_size.isZero())
     76             return 0;
     77 
     78         return m_nativeImage;
     79     }
     80 
     81     // Stub implementations of pure virtual Image functions.
     82     virtual void destroyDecodedData(bool) OVERRIDE
     83     {
     84     }
     85 
     86     virtual bool currentFrameKnownToBeOpaque() OVERRIDE
     87     {
     88         return false;
     89     }
     90 
     91     virtual void draw(GraphicsContext*, const FloatRect&, const FloatRect&, CompositeOperator, blink::WebBlendMode) OVERRIDE
     92     {
     93     }
     94 
     95 private:
     96 
     97     IntSize m_size;
     98 
     99     RefPtr<NativeImageSkia> m_nativeImage;
    100 };
    101 
    102 TEST(DragImageTest, NullHandling)
    103 {
    104     EXPECT_FALSE(DragImage::create(0));
    105 
    106     RefPtr<TestImage> nullTestImage(TestImage::create(IntSize()));
    107     EXPECT_FALSE(DragImage::create(nullTestImage.get()));
    108 }
    109 
    110 TEST(DragImageTest, NonNullHandling)
    111 {
    112     RefPtr<TestImage> testImage(TestImage::create(IntSize(2, 2)));
    113     OwnPtr<DragImage> dragImage = DragImage::create(testImage.get());
    114     ASSERT_TRUE(dragImage);
    115 
    116     dragImage->scale(0.5, 0.5);
    117     IntSize size = dragImage->size();
    118     EXPECT_EQ(1, size.width());
    119     EXPECT_EQ(1, size.height());
    120 
    121     dragImage->dissolveToFraction(0.5);
    122 }
    123 
    124 TEST(DragImageTest, CreateDragImage)
    125 {
    126     {
    127         // Tests that the DrageImage implementation doesn't choke on null values
    128         // of nativeImageForCurrentFrame().
    129         RefPtr<TestImage> testImage(TestImage::create(IntSize()));
    130         EXPECT_FALSE(DragImage::create(testImage.get()));
    131     }
    132 
    133     {
    134         // Tests that the drag image is a deep copy.
    135         RefPtr<TestImage> testImage(TestImage::create(IntSize(1, 1)));
    136         OwnPtr<DragImage> dragImage = DragImage::create(testImage.get());
    137         ASSERT_TRUE(dragImage);
    138         SkAutoLockPixels lock1(dragImage->bitmap()), lock2(testImage->nativeImageForCurrentFrame()->bitmap());
    139         EXPECT_NE(dragImage->bitmap().getPixels(), testImage->nativeImageForCurrentFrame()->bitmap().getPixels());
    140     }
    141 }
    142 
    143 } // anonymous namespace
    144