This source file includes following definitions.
- SetUp
- TearDown
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "config.h"
#include "core/frame/ImageBitmap.h"
#include "SkPixelRef.h"
#include "core/dom/Document.h"
#include "core/fetch/ImageResource.h"
#include "core/fetch/MemoryCache.h"
#include "core/fetch/MockImageResourceClient.h"
#include "core/fetch/ResourcePtr.h"
#include "core/html/HTMLCanvasElement.h"
#include "core/html/HTMLImageElement.h"
#include "core/html/canvas/CanvasRenderingContext2D.h"
#include "heap/Handle.h"
#include "platform/graphics/BitmapImage.h"
#include "platform/graphics/skia/NativeImageSkia.h"
#include "platform/network/ResourceRequest.h"
#include "wtf/OwnPtr.h"
#include <gtest/gtest.h>
namespace WebCore {
class ImageBitmapTest : public ::testing::Test {
protected:
virtual void SetUp()
{
ASSERT_TRUE(m_bitmap.allocN32Pixels(10, 10));
m_bitmap.eraseColor(0xFFFFFFFF);
ASSERT_TRUE(m_bitmap2.allocN32Pixels(5, 5));
m_bitmap2.eraseColor(0xAAAAAAAA);
m_globalMemoryCache = adoptPtr(memoryCache());
m_testingMemoryCache = adoptPtr(new MemoryCache());
setMemoryCacheForTesting(m_testingMemoryCache.leakPtr());
}
virtual void TearDown()
{
Heap::collectGarbage(ThreadState::NoHeapPointersOnStack, Heap::ForcedForTesting);
m_testingMemoryCache = adoptPtr(memoryCache());
setMemoryCacheForTesting(m_globalMemoryCache.leakPtr());
}
SkBitmap m_bitmap, m_bitmap2;
OwnPtr<MemoryCache> m_testingMemoryCache;
OwnPtr<MemoryCache> m_globalMemoryCache;
};
TEST_F(ImageBitmapTest, ImageResourceConsistency)
{
RefPtr<HTMLImageElement> imageElement = HTMLImageElement::create(*Document::create().get());
imageElement->setImageResource(new ImageResource(BitmapImage::create(NativeImageSkia::create(m_bitmap)).get()));
RefPtrWillBeRawPtr<ImageBitmap> imageBitmapNoCrop = ImageBitmap::create(imageElement.get(), IntRect(0, 0, m_bitmap.width(), m_bitmap.height()));
RefPtrWillBeRawPtr<ImageBitmap> imageBitmapInteriorCrop = ImageBitmap::create(imageElement.get(), IntRect(m_bitmap.width() / 2, m_bitmap.height() / 2, m_bitmap.width() / 2, m_bitmap.height() / 2));
RefPtrWillBeRawPtr<ImageBitmap> imageBitmapExteriorCrop = ImageBitmap::create(imageElement.get(), IntRect(-m_bitmap.width() / 2, -m_bitmap.height() / 2, m_bitmap.width(), m_bitmap.height()));
RefPtrWillBeRawPtr<ImageBitmap> imageBitmapOutsideCrop = ImageBitmap::create(imageElement.get(), IntRect(-m_bitmap.width(), -m_bitmap.height(), m_bitmap.width(), m_bitmap.height()));
ASSERT_EQ(imageBitmapNoCrop->bitmapImage().get(), imageElement->cachedImage()->image());
ASSERT_EQ(imageBitmapInteriorCrop->bitmapImage().get(), imageElement->cachedImage()->image());
ASSERT_EQ(imageBitmapExteriorCrop->bitmapImage().get(), imageElement->cachedImage()->image());
RefPtr<Image> emptyImage = imageBitmapOutsideCrop->bitmapImage();
ASSERT_NE(emptyImage.get(), imageElement->cachedImage()->image());
}
TEST_F(ImageBitmapTest, ImageBitmapLiveResourcePriority)
{
RefPtr<HTMLImageElement> imageNoCrop = HTMLImageElement::create(*Document::create().get());
ResourcePtr<ImageResource> cachedImageNoCrop = new ImageResource(ResourceRequest("http://foo.com/1"), BitmapImage::create(NativeImageSkia::create(m_bitmap)).get());
imageNoCrop->setImageResource(cachedImageNoCrop.get());
RefPtr<HTMLImageElement> imageInteriorCrop = HTMLImageElement::create(*Document::create().get());
ResourcePtr<ImageResource> cachedImageInteriorCrop = new ImageResource(ResourceRequest("http://foo.com/2"), BitmapImage::create(NativeImageSkia::create(m_bitmap)).get());
imageInteriorCrop->setImageResource(cachedImageInteriorCrop.get());
RefPtr<HTMLImageElement> imageExteriorCrop = HTMLImageElement::create(*Document::create().get());
ResourcePtr<ImageResource> cachedImageExteriorCrop = new ImageResource(ResourceRequest("http://foo.com/3"), BitmapImage::create(NativeImageSkia::create(m_bitmap)).get());
imageExteriorCrop->setImageResource(cachedImageExteriorCrop.get());
RefPtr<HTMLImageElement> imageOutsideCrop = HTMLImageElement::create(*Document::create().get());
ResourcePtr<ImageResource> cachedImageOutsideCrop = new ImageResource(ResourceRequest("http://foo.com/4"), BitmapImage::create(NativeImageSkia::create(m_bitmap)).get());
imageOutsideCrop->setImageResource(cachedImageOutsideCrop.get());
MockImageResourceClient mockClient1, mockClient2, mockClient3, mockClient4;
cachedImageNoCrop->addClient(&mockClient1);
cachedImageInteriorCrop->addClient(&mockClient2);
cachedImageExteriorCrop->addClient(&mockClient3);
cachedImageOutsideCrop->addClient(&mockClient4);
memoryCache()->add(cachedImageNoCrop.get());
memoryCache()->add(cachedImageInteriorCrop.get());
memoryCache()->add(cachedImageExteriorCrop.get());
memoryCache()->add(cachedImageOutsideCrop.get());
memoryCache()->insertInLiveDecodedResourcesList(cachedImageNoCrop.get());
memoryCache()->insertInLiveDecodedResourcesList(cachedImageInteriorCrop.get());
memoryCache()->insertInLiveDecodedResourcesList(cachedImageExteriorCrop.get());
memoryCache()->insertInLiveDecodedResourcesList(cachedImageOutsideCrop.get());
ASSERT_EQ(imageNoCrop->cachedImage()->cacheLiveResourcePriority(), Resource::CacheLiveResourcePriorityLow);
ASSERT_EQ(imageInteriorCrop->cachedImage()->cacheLiveResourcePriority(), Resource::CacheLiveResourcePriorityLow);
ASSERT_EQ(imageExteriorCrop->cachedImage()->cacheLiveResourcePriority(), Resource::CacheLiveResourcePriorityLow);
ASSERT_EQ(imageOutsideCrop->cachedImage()->cacheLiveResourcePriority(), Resource::CacheLiveResourcePriorityLow);
RefPtrWillBeRawPtr<ImageBitmap> imageBitmapInteriorCrop = ImageBitmap::create(imageInteriorCrop.get(), IntRect(m_bitmap.width() / 2, m_bitmap.height() / 2, m_bitmap.width(), m_bitmap.height()));
{
RefPtrWillBeRawPtr<ImageBitmap> imageBitmapNoCrop = ImageBitmap::create(imageNoCrop.get(), IntRect(0, 0, m_bitmap.width(), m_bitmap.height()));
RefPtrWillBeRawPtr<ImageBitmap> imageBitmapInteriorCrop2 = ImageBitmap::create(imageInteriorCrop.get(), IntRect(m_bitmap.width() / 2, m_bitmap.height() / 2, m_bitmap.width(), m_bitmap.height()));
RefPtrWillBeRawPtr<ImageBitmap> imageBitmapExteriorCrop = ImageBitmap::create(imageExteriorCrop.get(), IntRect(-m_bitmap.width() / 2, -m_bitmap.height() / 2, m_bitmap.width(), m_bitmap.height()));
RefPtrWillBeRawPtr<ImageBitmap> imageBitmapOutsideCrop = ImageBitmap::create(imageOutsideCrop.get(), IntRect(-m_bitmap.width(), -m_bitmap.height(), m_bitmap.width(), m_bitmap.height()));
ASSERT_EQ(imageNoCrop->cachedImage()->cacheLiveResourcePriority(), Resource::CacheLiveResourcePriorityHigh);
ASSERT_EQ(imageInteriorCrop->cachedImage()->cacheLiveResourcePriority(), Resource::CacheLiveResourcePriorityHigh);
ASSERT_EQ(imageExteriorCrop->cachedImage()->cacheLiveResourcePriority(), Resource::CacheLiveResourcePriorityHigh);
ASSERT_EQ(imageOutsideCrop->cachedImage()->cacheLiveResourcePriority(), Resource::CacheLiveResourcePriorityLow);
imageBitmapNoCrop = nullptr;
imageBitmapInteriorCrop2 = nullptr;
imageBitmapExteriorCrop = nullptr;
imageBitmapOutsideCrop = nullptr;
Heap::collectGarbage(ThreadState::HeapPointersOnStack, Heap::ForcedForTesting);
}
ASSERT_EQ(imageNoCrop->cachedImage()->cacheLiveResourcePriority(), Resource::CacheLiveResourcePriorityLow);
ASSERT_EQ(imageExteriorCrop->cachedImage()->cacheLiveResourcePriority(), Resource::CacheLiveResourcePriorityLow);
ASSERT_EQ(imageOutsideCrop->cachedImage()->cacheLiveResourcePriority(), Resource::CacheLiveResourcePriorityLow);
ASSERT_EQ(imageInteriorCrop->cachedImage()->cacheLiveResourcePriority(), Resource::CacheLiveResourcePriorityHigh);
imageBitmapInteriorCrop = nullptr;
}
TEST_F(ImageBitmapTest, ImageBitmapSourceChanged)
{
RefPtr<HTMLImageElement> image = HTMLImageElement::create(*Document::create().get());
ResourcePtr<ImageResource> originalImageResource = new ImageResource(BitmapImage::create(NativeImageSkia::create(m_bitmap)).get());
image->setImageResource(originalImageResource.get());
RefPtrWillBeRawPtr<ImageBitmap> imageBitmap = ImageBitmap::create(image.get(), IntRect(0, 0, m_bitmap.width(), m_bitmap.height()));
ASSERT_EQ(imageBitmap->bitmapImage().get(), originalImageResource->image());
ResourcePtr<ImageResource> newImageResource = new ImageResource(BitmapImage::create(NativeImageSkia::create(m_bitmap2)).get());
image->setImageResource(newImageResource.get());
ASSERT_NE(imageBitmap->bitmapImage().get(), originalImageResource->image());
ASSERT_EQ(imageBitmap->bitmapImage()->nativeImageForCurrentFrame()->bitmap().pixelRef()->pixels(),
originalImageResource->image()->nativeImageForCurrentFrame()->bitmap().pixelRef()->pixels());
ASSERT_NE(imageBitmap->bitmapImage().get(), newImageResource->image());
ASSERT_NE(imageBitmap->bitmapImage()->nativeImageForCurrentFrame()->bitmap().pixelRef()->pixels(),
newImageResource->image()->nativeImageForCurrentFrame()->bitmap().pixelRef()->pixels());
}
TEST_F(ImageBitmapTest, ImageResourceLifetime)
{
RefPtr<HTMLCanvasElement> canvasElement = HTMLCanvasElement::create(*Document::create().get());
canvasElement->setHeight(40);
canvasElement->setWidth(40);
RefPtrWillBeRawPtr<ImageBitmap> imageBitmapDerived = nullptr;
{
RefPtrWillBeRawPtr<ImageBitmap> imageBitmapFromCanvas = ImageBitmap::create(canvasElement.get(), IntRect(0, 0, canvasElement->width(), canvasElement->height()));
imageBitmapDerived = ImageBitmap::create(imageBitmapFromCanvas.get(), IntRect(0, 0, 20, 20));
}
CanvasRenderingContext* context = canvasElement->getContext("2d");
TrackExceptionState exceptionState;
toCanvasRenderingContext2D(context)->drawImage(imageBitmapDerived.get(), 0, 0, exceptionState);
}
}