This source file includes following definitions.
- appendData
- destroyDecodedDataIfPossible
- fakeEncodedSize
- SetUp
- TearDown
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- m_dead
- run
- run
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "config.h"
#include "core/fetch/MemoryCache.h"
#include "core/fetch/MockImageResourceClient.h"
#include "core/fetch/RawResource.h"
#include "core/fetch/ResourcePtr.h"
#include "platform/network/ResourceRequest.h"
#include "public/platform/Platform.h"
#include "wtf/OwnPtr.h"
#include <gtest/gtest.h>
namespace WebCore {
class MemoryCacheTest : public ::testing::Test {
public:
class FakeDecodedResource : public WebCore::Resource {
public:
FakeDecodedResource(const ResourceRequest& request, Type type)
: Resource(request, type)
{
}
virtual void appendData(const char* data, int len)
{
Resource::appendData(data, len);
setDecodedSize(this->size());
}
protected:
virtual void destroyDecodedDataIfPossible() OVERRIDE
{
setDecodedSize(0);
}
};
class FakeResource : public WebCore::Resource {
public:
FakeResource(const ResourceRequest& request, Type type)
: Resource(request, type)
{
}
void fakeEncodedSize(size_t size)
{
setEncodedSize(size);
}
};
protected:
virtual void SetUp()
{
m_globalMemoryCache = adoptPtr(memoryCache());
m_testingMemoryCache = adoptPtr(new MemoryCache());
setMemoryCacheForTesting(m_testingMemoryCache.leakPtr());
}
virtual void TearDown()
{
m_testingMemoryCache = adoptPtr(memoryCache());
setMemoryCacheForTesting(m_globalMemoryCache.leakPtr());
}
OwnPtr<MemoryCache> m_testingMemoryCache;
OwnPtr<MemoryCache> m_globalMemoryCache;
};
TEST_F(MemoryCacheTest, CapacityAccounting)
{
const size_t sizeMax = ~static_cast<size_t>(0);
const size_t totalCapacity = sizeMax / 4;
const size_t minDeadCapacity = sizeMax / 16;
const size_t maxDeadCapacity = sizeMax / 8;
memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity);
ASSERT_EQ(totalCapacity, memoryCache()->capacity());
ASSERT_EQ(minDeadCapacity, memoryCache()->minDeadCapacity());
ASSERT_EQ(maxDeadCapacity, memoryCache()->maxDeadCapacity());
}
TEST_F(MemoryCacheTest, VeryLargeResourceAccounting)
{
const size_t sizeMax = ~static_cast<size_t>(0);
const size_t totalCapacity = sizeMax / 4;
const size_t minDeadCapacity = sizeMax / 16;
const size_t maxDeadCapacity = sizeMax / 8;
const size_t resourceSize1 = sizeMax / 16;
const size_t resourceSize2 = sizeMax / 20;
memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity);
ResourcePtr<FakeResource> cachedResource =
new FakeResource(ResourceRequest(""), Resource::Raw);
cachedResource->fakeEncodedSize(resourceSize1);
ASSERT_EQ(0u, memoryCache()->deadSize());
ASSERT_EQ(0u, memoryCache()->liveSize());
memoryCache()->add(cachedResource.get());
ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize());
ASSERT_EQ(0u, memoryCache()->liveSize());
MockImageResourceClient client;
cachedResource->addClient(&client);
ASSERT_EQ(0u, memoryCache()->deadSize());
ASSERT_EQ(cachedResource->size(), memoryCache()->liveSize());
cachedResource->fakeEncodedSize(resourceSize2);
ASSERT_EQ(0u, memoryCache()->deadSize());
ASSERT_EQ(cachedResource->size(), memoryCache()->liveSize());
}
TEST_F(MemoryCacheTest, DeadResourceEviction)
{
memoryCache()->setDelayBeforeLiveDecodedPrune(0);
memoryCache()->setMaxPruneDeferralDelay(0);
const unsigned totalCapacity = 1000000;
const unsigned minDeadCapacity = 0;
const unsigned maxDeadCapacity = 0;
memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity);
ResourcePtr<Resource> cachedResource =
new Resource(ResourceRequest(""), Resource::Raw);
const char data[5] = "abcd";
cachedResource->appendData(data, 3);
ASSERT_GT(cachedResource->size(), 0u);
ASSERT_EQ(0u, memoryCache()->deadSize());
ASSERT_EQ(0u, memoryCache()->liveSize());
memoryCache()->add(cachedResource.get());
ASSERT_EQ(cachedResource->size(), memoryCache()->deadSize());
ASSERT_EQ(0u, memoryCache()->liveSize());
memoryCache()->prune();
ASSERT_EQ(0u, memoryCache()->deadSize());
ASSERT_EQ(0u, memoryCache()->liveSize());
}
TEST_F(MemoryCacheTest, LiveResourceEvictionAtEndOfTask)
{
memoryCache()->setDelayBeforeLiveDecodedPrune(0);
const unsigned totalCapacity = 1;
const unsigned minDeadCapacity = 0;
const unsigned maxDeadCapacity = 0;
memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity);
const char data[6] = "abcde";
ResourcePtr<Resource> cachedDeadResource =
new Resource(ResourceRequest("http://foo"), Resource::Raw);
cachedDeadResource->appendData(data, 3);
ResourcePtr<Resource> cachedLiveResource =
new FakeDecodedResource(ResourceRequest(""), Resource::Raw);
MockImageResourceClient client;
cachedLiveResource->addClient(&client);
cachedLiveResource->appendData(data, 4);
class Task1 : public blink::WebThread::Task {
public:
Task1(const ResourcePtr<Resource>& live, const ResourcePtr<Resource>& dead)
: m_live(live)
, m_dead(dead)
{ }
virtual void run() OVERRIDE
{
ASSERT_GT(m_live->size(), 0u);
ASSERT_GT(m_dead->size(), 0u);
ASSERT_EQ(0u, memoryCache()->deadSize());
ASSERT_EQ(0u, memoryCache()->liveSize());
memoryCache()->add(m_dead.get());
memoryCache()->add(m_live.get());
memoryCache()->insertInLiveDecodedResourcesList(m_live.get());
ASSERT_EQ(m_dead->size(), memoryCache()->deadSize());
ASSERT_EQ(m_live->size(), memoryCache()->liveSize());
ASSERT_GT(m_live->decodedSize(), 0u);
memoryCache()->prune();
ASSERT_EQ(m_dead->size(), memoryCache()->deadSize());
ASSERT_EQ(m_live->size(), memoryCache()->liveSize());
ASSERT_GT(m_live->decodedSize(), 0u);
}
private:
ResourcePtr<Resource> m_live, m_dead;
};
class Task2 : public blink::WebThread::Task {
public:
Task2(unsigned liveSizeWithoutDecode)
: m_liveSizeWithoutDecode(liveSizeWithoutDecode) { }
virtual void run() OVERRIDE
{
ASSERT_EQ(0u, memoryCache()->deadSize());
ASSERT_EQ(m_liveSizeWithoutDecode, memoryCache()->liveSize());
blink::Platform::current()->currentThread()->exitRunLoop();
}
private:
unsigned m_liveSizeWithoutDecode;
};
blink::Platform::current()->currentThread()->postTask(new Task1(cachedLiveResource, cachedDeadResource));
blink::Platform::current()->currentThread()->postTask(new Task2(cachedLiveResource->encodedSize() + cachedLiveResource->overheadSize()));
blink::Platform::current()->currentThread()->enterRunLoop();
cachedLiveResource->removeClient(&client);
}
TEST_F(MemoryCacheTest, ClientRemoval)
{
const char data[6] = "abcde";
ResourcePtr<Resource> resource1 =
new FakeDecodedResource(ResourceRequest("http://foo.com"), Resource::Raw);
MockImageResourceClient client1;
resource1->addClient(&client1);
resource1->appendData(data, 4);
ResourcePtr<Resource> resource2 =
new FakeDecodedResource(ResourceRequest(""), Resource::Raw);
MockImageResourceClient client2;
resource2->addClient(&client2);
resource2->appendData(data, 4);
const unsigned minDeadCapacity = 0;
const unsigned maxDeadCapacity = ((resource1->size() + resource2->size()) / 2) - 1;
const unsigned totalCapacity = maxDeadCapacity;
memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity);
memoryCache()->add(resource1.get());
memoryCache()->add(resource2.get());
memoryCache()->prune();
ASSERT_GT(resource1->decodedSize(), 0u);
ASSERT_GT(resource2->decodedSize(), 0u);
ASSERT_EQ(memoryCache()->deadSize(), 0u);
ASSERT_EQ(memoryCache()->liveSize(), resource1->size() + resource2->size());
resource1->removeClient(&client1);
ASSERT_GT(resource1->decodedSize(), 0u);
ASSERT_GT(resource2->decodedSize(), 0u);
ASSERT_EQ(memoryCache()->deadSize(), resource1->size());
ASSERT_EQ(memoryCache()->liveSize(), resource2->size());
ASSERT_TRUE(memoryCache()->contains(resource1.get()));
ASSERT_TRUE(memoryCache()->contains(resource2.get()));
resource2->removeClient(&client2);
ASSERT_GT(resource1->decodedSize(), 0u);
ASSERT_GT(resource2->decodedSize(), 0u);
ASSERT_EQ(memoryCache()->deadSize(), resource1->size());
ASSERT_EQ(memoryCache()->liveSize(), 0u);
ASSERT_TRUE(memoryCache()->contains(resource1.get()));
ASSERT_FALSE(memoryCache()->contains(resource2.get()));
}
TEST_F(MemoryCacheTest, DecodeCacheOrder)
{
memoryCache()->setDelayBeforeLiveDecodedPrune(0);
memoryCache()->setMaxPruneDeferralDelay(0);
ResourcePtr<FakeDecodedResource> cachedImageLowPriority =
new FakeDecodedResource(ResourceRequest("http://foo.com"), Resource::Raw);
ResourcePtr<FakeDecodedResource> cachedImageHighPriority =
new FakeDecodedResource(ResourceRequest(""), Resource::Raw);
MockImageResourceClient clientLowPriority;
MockImageResourceClient clientHighPriority;
cachedImageLowPriority->addClient(&clientLowPriority);
cachedImageHighPriority->addClient(&clientHighPriority);
const char data[5] = "abcd";
cachedImageLowPriority->appendData(data, 1);
cachedImageHighPriority->appendData(data, 4);
const unsigned lowPrioritySize = cachedImageLowPriority->size();
const unsigned highPrioritySize = cachedImageHighPriority->size();
const unsigned lowPriorityMockDecodeSize = cachedImageLowPriority->decodedSize();
const unsigned highPriorityMockDecodeSize = cachedImageHighPriority->decodedSize();
const unsigned totalSize = lowPrioritySize + highPrioritySize;
ASSERT_GT(lowPrioritySize, 0u);
ASSERT_NE(lowPrioritySize, highPrioritySize);
ASSERT_GT(lowPriorityMockDecodeSize, 0u);
ASSERT_NE(lowPriorityMockDecodeSize, highPriorityMockDecodeSize);
ASSERT_EQ(memoryCache()->deadSize(), 0u);
ASSERT_EQ(memoryCache()->liveSize(), 0u);
memoryCache()->add(cachedImageHighPriority.get());
ASSERT_EQ(memoryCache()->deadSize(), 0u);
ASSERT_EQ(memoryCache()->liveSize(), highPrioritySize);
memoryCache()->add(cachedImageLowPriority.get());
ASSERT_EQ(memoryCache()->deadSize(), 0u);
ASSERT_EQ(memoryCache()->liveSize(), highPrioritySize + lowPrioritySize);
memoryCache()->insertInLiveDecodedResourcesList(cachedImageHighPriority.get());
memoryCache()->insertInLiveDecodedResourcesList(cachedImageLowPriority.get());
ASSERT_EQ(memoryCache()->deadSize(), 0u);
ASSERT_EQ(memoryCache()->liveSize(), totalSize);
cachedImageLowPriority->setCacheLiveResourcePriority(Resource::CacheLiveResourcePriorityLow);
cachedImageHighPriority->setCacheLiveResourcePriority(Resource::CacheLiveResourcePriorityHigh);
memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache()->liveSize() - 10, memoryCache()->liveSize() - 10);
memoryCache()->prune();
ASSERT_EQ(memoryCache()->deadSize(), 0u);
ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize);
memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache()->liveSize() - 10, memoryCache()->liveSize() - 10);
memoryCache()->prune();
ASSERT_EQ(memoryCache()->deadSize(), 0u);
ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize - highPriorityMockDecodeSize);
}
TEST_F(MemoryCacheTest, MultipleReplace)
{
ResourcePtr<FakeResource> resource1 = new FakeResource(ResourceRequest(""), Resource::Raw);
memoryCache()->add(resource1.get());
ResourcePtr<FakeResource> resource2 = new FakeResource(ResourceRequest(""), Resource::Raw);
memoryCache()->replace(resource2.get(), resource1.get());
EXPECT_TRUE(memoryCache()->contains(resource2.get()));
EXPECT_FALSE(memoryCache()->contains(resource1.get()));
ResourcePtr<FakeResource> resource3 = new FakeResource(ResourceRequest(""), Resource::Raw);
memoryCache()->replace(resource3.get(), resource2.get());
EXPECT_TRUE(memoryCache()->contains(resource3.get()));
EXPECT_FALSE(memoryCache()->contains(resource2.get()));
}
TEST_F(MemoryCacheTest, RemoveDuringRevalidation)
{
ResourcePtr<FakeResource> resource1 = new FakeResource(ResourceRequest(""), Resource::Raw);
memoryCache()->add(resource1.get());
ResourcePtr<FakeResource> resource2 = new FakeResource(ResourceRequest(""), Resource::Raw);
memoryCache()->remove(resource1.get());
memoryCache()->add(resource2.get());
EXPECT_TRUE(memoryCache()->contains(resource2.get()));
EXPECT_FALSE(memoryCache()->contains(resource1.get()));
ResourcePtr<FakeResource> resource3 = new FakeResource(ResourceRequest(""), Resource::Raw);
memoryCache()->remove(resource2.get());
memoryCache()->add(resource3.get());
EXPECT_TRUE(memoryCache()->contains(resource3.get()));
EXPECT_FALSE(memoryCache()->contains(resource2.get()));
memoryCache()->replace(resource1.get(), resource2.get());
EXPECT_TRUE(memoryCache()->contains(resource1.get()));
EXPECT_FALSE(memoryCache()->contains(resource2.get()));
EXPECT_FALSE(memoryCache()->contains(resource3.get()));
}
}