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 "core/platform/graphics/Image.h"
     36 #include "core/platform/graphics/IntSize.h"
     37 #include "core/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 unsigned decodedSize() const OVERRIDE
     87     {
     88         return 0u;
     89     }
     90 
     91     virtual bool currentFrameKnownToBeOpaque() OVERRIDE
     92     {
     93         return false;
     94     }
     95 
     96     virtual void draw(GraphicsContext*, const FloatRect&, const FloatRect&, CompositeOperator, BlendMode) OVERRIDE
     97     {
     98     }
     99 
    100 private:
    101 
    102     IntSize m_size;
    103 
    104     RefPtr<NativeImageSkia> m_nativeImage;
    105 };
    106 
    107 TEST(DragImageTest, NullHandling)
    108 {
    109     EXPECT_FALSE(DragImage::create(0));
    110 
    111     RefPtr<TestImage> nullTestImage(TestImage::create(IntSize()));
    112     EXPECT_FALSE(DragImage::create(nullTestImage.get()));
    113 }
    114 
    115 TEST(DragImageTest, NonNullHandling)
    116 {
    117     RefPtr<TestImage> testImage(TestImage::create(IntSize(2, 2)));
    118     OwnPtr<DragImage> dragImage = DragImage::create(testImage.get());
    119     ASSERT_TRUE(dragImage);
    120 
    121     dragImage->scale(0.5, 0.5);
    122     IntSize size = dragImage->size();
    123     EXPECT_EQ(1, size.width());
    124     EXPECT_EQ(1, size.height());
    125 
    126     dragImage->dissolveToFraction(0.5);
    127 }
    128 
    129 TEST(DragImageTest, CreateDragImage)
    130 {
    131     {
    132         // Tests that the DrageImage implementation doesn't choke on null values
    133         // of nativeImageForCurrentFrame().
    134         RefPtr<TestImage> testImage(TestImage::create(IntSize()));
    135         EXPECT_FALSE(DragImage::create(testImage.get()));
    136     }
    137 
    138     {
    139         // Tests that the drag image is a deep copy.
    140         RefPtr<TestImage> testImage(TestImage::create(IntSize(1, 1)));
    141         OwnPtr<DragImage> dragImage = DragImage::create(testImage.get());
    142         ASSERT_TRUE(dragImage);
    143         SkAutoLockPixels lock1(dragImage->bitmap()), lock2(testImage->nativeImageForCurrentFrame()->bitmap());
    144         EXPECT_NE(dragImage->bitmap().getPixels(), testImage->nativeImageForCurrentFrame()->bitmap().getPixels());
    145     }
    146 }
    147 
    148 } // anonymous namespace
    149