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 "platform/DragImage.h" 34 35 #include "platform/fonts/FontDescription.h" 36 #include "platform/fonts/FontTraits.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" 46 47 #include <gtest/gtest.h> 48 49 using namespace blink; 50 51 namespace { 52 53 class TestImage : public Image { 54 public: 55 56 static PassRefPtr<TestImage> create(const IntSize& size) 57 { 58 return adoptRef(new TestImage(size)); 59 } 60 61 explicit TestImage(const IntSize& size) 62 : Image(0) 63 , m_size(size) 64 { 65 SkBitmap bitmap; 66 bitmap.allocN32Pixels(size.width(), size.height()); 67 m_nativeImage = NativeImageSkia::create(bitmap); 68 } 69 70 virtual IntSize size() const OVERRIDE 71 { 72 return m_size; 73 } 74 75 virtual PassRefPtr<NativeImageSkia> nativeImageForCurrentFrame() OVERRIDE 76 { 77 if (m_size.isZero()) 78 return nullptr; 79 80 return m_nativeImage; 81 } 82 83 // Stub implementations of pure virtual Image functions. 84 virtual void destroyDecodedData(bool) OVERRIDE 85 { 86 } 87 88 virtual bool currentFrameKnownToBeOpaque() OVERRIDE 89 { 90 return false; 91 } 92 93 virtual void draw(GraphicsContext*, const FloatRect&, const FloatRect&, CompositeOperator, blink::WebBlendMode) OVERRIDE 94 { 95 } 96 97 private: 98 99 IntSize m_size; 100 101 RefPtr<NativeImageSkia> m_nativeImage; 102 }; 103 104 TEST(DragImageTest, NullHandling) 105 { 106 EXPECT_FALSE(DragImage::create(0)); 107 108 RefPtr<TestImage> nullTestImage(TestImage::create(IntSize())); 109 EXPECT_FALSE(DragImage::create(nullTestImage.get())); 110 } 111 112 TEST(DragImageTest, NonNullHandling) 113 { 114 RefPtr<TestImage> testImage(TestImage::create(IntSize(2, 2))); 115 OwnPtr<DragImage> dragImage = DragImage::create(testImage.get()); 116 ASSERT_TRUE(dragImage); 117 118 dragImage->scale(0.5, 0.5); 119 IntSize size = dragImage->size(); 120 EXPECT_EQ(1, size.width()); 121 EXPECT_EQ(1, size.height()); 122 123 dragImage->dissolveToFraction(0.5); 124 } 125 126 TEST(DragImageTest, CreateDragImage) 127 { 128 { 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())); 133 } 134 135 { 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()); 142 } 143 } 144 145 TEST(DragImageTest, TrimWhitespace) 146 { 147 KURL url(ParsedURLString, "http://www.example.com/"); 148 String testLabel = " Example Example Example \n "; 149 String expectedLabel = "Example Example Example"; 150 float deviceScaleFactor = 1.0f; 151 152 FontDescription fontDescription; 153 fontDescription.firstFamily().setFamily("Arial"); 154 fontDescription.setSpecifiedSize(16); 155 fontDescription.setIsAbsoluteSize(true); 156 fontDescription.setGenericFamily(FontDescription::NoFamily); 157 fontDescription.setWeight(FontWeightNormal); 158 fontDescription.setStyle(FontStyleNormal); 159 160 OwnPtr<DragImage> testImage = 161 DragImage::create(url, testLabel, fontDescription, deviceScaleFactor); 162 OwnPtr<DragImage> expectedImage = 163 DragImage::create(url, expectedLabel, fontDescription, deviceScaleFactor); 164 165 EXPECT_EQ(testImage->size().width(), expectedImage->size().width()); 166 } 167 168 } // anonymous namespace 169