File: MemoryCacheTest.cpp

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (581 lines) | stat: -rw-r--r-- 25,138 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
 * Copyright (c) 2013, Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#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 blink {

class MemoryCacheTest : public ::testing::Test {
public:
    class FakeDecodedResource : public blink::Resource {
    public:
        FakeDecodedResource(const ResourceRequest& request, Type type)
            : Resource(request, type)
        {
        }

        virtual void appendData(const char* data, unsigned len)
        {
            Resource::appendData(data, len);
            setDecodedSize(this->size());
        }

    protected:
        virtual void destroyDecodedDataIfPossible() override
        {
            setDecodedSize(0);
        }
    };

    class FakeResource : public blink::Resource {
    public:
        FakeResource(const ResourceRequest& request, Type type)
            : Resource(request, type)
        {
        }

        void fakeEncodedSize(size_t size)
        {
            setEncodedSize(size);
        }
    };

protected:
    virtual void SetUp()
    {
        // Save the global memory cache to restore it upon teardown.
        m_globalMemoryCache = replaceMemoryCacheForTesting(MemoryCache::create());
    }

    virtual void TearDown()
    {
        replaceMemoryCacheForTesting(m_globalMemoryCache.release());
    }

    OwnPtrWillBePersistent<MemoryCache> m_globalMemoryCache;
};

// Verifies that setters and getters for cache capacities work correcty.
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("http://test/resource"), 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());

    cachedResource->removeClient(&client);
}

// Verifies that dead resources that exceed dead resource capacity are evicted
// from cache when pruning.
static void TestDeadResourceEviction(Resource* resource1, Resource* resource2)
{
    memoryCache()->setDelayBeforeLiveDecodedPrune(0);
    memoryCache()->setMaxPruneDeferralDelay(0);
    const unsigned totalCapacity = 1000000;
    const unsigned minDeadCapacity = 0;
    const unsigned maxDeadCapacity = 0;
    memoryCache()->setCapacities(minDeadCapacity, maxDeadCapacity, totalCapacity);

    const char data[5] = "abcd";
    resource1->appendData(data, 3u);
    resource2->appendData(data, 2u);

    // The resource size has to be nonzero for this test to be meaningful, but
    // we do not rely on it having any particular value.
    ASSERT_GT(resource1->size(), 0u);
    ASSERT_GT(resource2->size(), 0u);

    ASSERT_EQ(0u, memoryCache()->deadSize());
    ASSERT_EQ(0u, memoryCache()->liveSize());

    memoryCache()->add(resource1);
    ASSERT_EQ(resource1->size(), memoryCache()->deadSize());
    ASSERT_EQ(0u, memoryCache()->liveSize());

    memoryCache()->add(resource2);
    ASSERT_EQ(resource1->size() + resource2->size(), memoryCache()->deadSize());
    ASSERT_EQ(0u, memoryCache()->liveSize());

    memoryCache()->prune();
    ASSERT_EQ(0u, memoryCache()->deadSize());
    ASSERT_EQ(0u, memoryCache()->liveSize());
}

TEST_F(MemoryCacheTest, DeadResourceEviction_Basic)
{
    Resource* resource1 =
        new Resource(ResourceRequest("http://test/resource1"), Resource::Raw);
    Resource* resource2 =
        new Resource(ResourceRequest("http://test/resource2"), Resource::Raw);
    TestDeadResourceEviction(resource1, resource2);
}

TEST_F(MemoryCacheTest, DeadResourceEviction_MultipleResourceMaps)
{
    Resource* resource1 =
        new Resource(ResourceRequest("http://test/resource1"), Resource::Raw);
    Resource* resource2 =
        new Resource(ResourceRequest("http://test/resource2"), Resource::Raw);
    resource2->setCacheIdentifier("foo");
    TestDeadResourceEviction(resource1, resource2);
}

static void TestLiveResourceEvictionAtEndOfTask(Resource* cachedDeadResource, const ResourcePtr<Resource>& cachedLiveResource)
{
    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";
    cachedDeadResource->appendData(data, 3u);
    MockImageResourceClient client;
    cachedLiveResource->addClient(&client);
    cachedLiveResource->appendData(data, 4u);

    class Task1 : public blink::WebThread::Task {
    public:
        Task1(const ResourcePtr<Resource>& live, Resource* dead)
            : m_live(live)
            , m_dead(dead)
        { }

        virtual void run() override
        {
            // The resource size has to be nonzero for this test to be meaningful, but
            // we do not rely on it having any particular value.
            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);
            memoryCache()->add(m_live.get());
            memoryCache()->updateDecodedResource(m_live.get(), UpdateForPropertyChange);
            ASSERT_EQ(m_dead->size(), memoryCache()->deadSize());
            ASSERT_EQ(m_live->size(), memoryCache()->liveSize());
            ASSERT_GT(m_live->decodedSize(), 0u);

            memoryCache()->prune(); // Dead resources are pruned immediately
            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;
        Resource* m_dead;
    };

    class Task2 : public blink::WebThread::Task {
    public:
        Task2(unsigned liveSizeWithoutDecode)
            : m_liveSizeWithoutDecode(liveSizeWithoutDecode) { }

        virtual void run() override
        {
            // Next task: now, the live resource was evicted.
            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);
}

// Verified that when ordering a prune in a runLoop task, the prune
// is deferred to the end of the task.
TEST_F(MemoryCacheTest, LiveResourceEvictionAtEndOfTask_Basic)
{
    Resource* cachedDeadResource =
        new Resource(ResourceRequest("hhtp://foo"), Resource::Raw);
    ResourcePtr<Resource> cachedLiveResource =
        new FakeDecodedResource(ResourceRequest("http://test/resource"), Resource::Raw);
    TestLiveResourceEvictionAtEndOfTask(cachedDeadResource, cachedLiveResource);
}

TEST_F(MemoryCacheTest, LiveResourceEvictionAtEndOfTask_MultipleResourceMaps)
{
    {
        Resource* cachedDeadResource =
            new Resource(ResourceRequest("hhtp://foo"), Resource::Raw);
        cachedDeadResource->setCacheIdentifier("foo");
        ResourcePtr<Resource> cachedLiveResource =
            new FakeDecodedResource(ResourceRequest("http://test/resource"), Resource::Raw);
        TestLiveResourceEvictionAtEndOfTask(cachedDeadResource, cachedLiveResource);
        memoryCache()->evictResources();
    }
    {
        Resource* cachedDeadResource =
            new Resource(ResourceRequest("hhtp://foo"), Resource::Raw);
        ResourcePtr<Resource> cachedLiveResource =
            new FakeDecodedResource(ResourceRequest("http://test/resource"), Resource::Raw);
        cachedLiveResource->setCacheIdentifier("foo");
        TestLiveResourceEvictionAtEndOfTask(cachedDeadResource, cachedLiveResource);
        memoryCache()->evictResources();
    }
    {
        Resource* cachedDeadResource =
            new Resource(ResourceRequest("hhtp://test/resource"), Resource::Raw);
        cachedDeadResource->setCacheIdentifier("foo");
        ResourcePtr<Resource> cachedLiveResource =
            new FakeDecodedResource(ResourceRequest("http://test/resource"), Resource::Raw);
        cachedLiveResource->setCacheIdentifier("bar");
        TestLiveResourceEvictionAtEndOfTask(cachedDeadResource, cachedLiveResource);
        memoryCache()->evictResources();
    }
}

// Verifies that cached resources are evicted immediately after release when
// the total dead resource size is more than double the dead resource capacity.
static void TestClientRemoval(const ResourcePtr<Resource>& resource1, const ResourcePtr<Resource>& resource2)
{
    const char data[6] = "abcde";
    MockImageResourceClient client1;
    resource1->addClient(&client1);
    resource1->appendData(data, 4u);
    MockImageResourceClient client2;
    resource2->addClient(&client2);
    resource2->appendData(data, 4u);

    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());
    // Call prune. There is nothing to prune, but this will initialize
    // the prune timestamp, allowing future prunes to be deferred.
    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());

    // Removing the client from resource1 should result in all resources
    // remaining in cache since the prune is deferred.
    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()));

    // Removing the client from resource2 should result in immediate
    // eviction of resource2 because we are over the prune deferral limit.
    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, ClientRemoval_Basic)
{
    ResourcePtr<Resource> resource1 =
        new FakeDecodedResource(ResourceRequest("http://foo.com"), Resource::Raw);
    ResourcePtr<Resource> resource2 =
        new FakeDecodedResource(ResourceRequest("http://test/resource"), Resource::Raw);
    TestClientRemoval(resource1, resource2);
}

TEST_F(MemoryCacheTest, ClientRemoval_MultipleResourceMaps)
{
    {
        ResourcePtr<Resource> resource1 =
            new FakeDecodedResource(ResourceRequest("http://foo.com"), Resource::Raw);
        resource1->setCacheIdentifier("foo");
        ResourcePtr<Resource> resource2 =
            new FakeDecodedResource(ResourceRequest("http://test/resource"), Resource::Raw);
        TestClientRemoval(resource1, resource2);
        memoryCache()->evictResources();
    }
    {
        ResourcePtr<Resource> resource1 =
            new FakeDecodedResource(ResourceRequest("http://foo.com"), Resource::Raw);
        ResourcePtr<Resource> resource2 =
            new FakeDecodedResource(ResourceRequest("http://test/resource"), Resource::Raw);
        resource2->setCacheIdentifier("foo");
        TestClientRemoval(resource1, resource2);
        memoryCache()->evictResources();
    }
    {
        ResourcePtr<Resource> resource1 =
            new FakeDecodedResource(ResourceRequest("http://test/resource"), Resource::Raw);
        resource1->setCacheIdentifier("foo");
        ResourcePtr<Resource> resource2 =
            new FakeDecodedResource(ResourceRequest("http://test/resource"), Resource::Raw);
        resource2->setCacheIdentifier("bar");
        TestClientRemoval(resource1, resource2);
        memoryCache()->evictResources();
    }
}

// Verifies that CachedResources are evicted from the decode cache
// according to their DecodeCachePriority.
static void TestDecodeCacheOrder(const ResourcePtr<Resource>& cachedImageLowPriority, const ResourcePtr<Resource>& cachedImageHighPriority)
{
    memoryCache()->setDelayBeforeLiveDecodedPrune(0);
    memoryCache()->setMaxPruneDeferralDelay(0);

    MockImageResourceClient clientLowPriority;
    MockImageResourceClient clientHighPriority;
    cachedImageLowPriority->addClient(&clientLowPriority);
    cachedImageHighPriority->addClient(&clientHighPriority);

    const char data[5] = "abcd";
    cachedImageLowPriority->appendData(data, 1u);
    cachedImageHighPriority->appendData(data, 4u);
    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;

    // Verify that the sizes are different to ensure that we can test eviction order.
    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);

    // Add the items. The item added first would normally be evicted first.
    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);

    // Insert all items in the decoded items list with the same priority
    memoryCache()->updateDecodedResource(cachedImageHighPriority.get(), UpdateForPropertyChange);
    memoryCache()->updateDecodedResource(cachedImageLowPriority.get(), UpdateForPropertyChange);
    ASSERT_EQ(memoryCache()->deadSize(), 0u);
    ASSERT_EQ(memoryCache()->liveSize(), totalSize);

    // Now we will assign their priority and make sure they are moved to the correct buckets.
    memoryCache()->updateDecodedResource(cachedImageLowPriority.get(), UpdateForPropertyChange, MemoryCacheLiveResourcePriorityLow);
    memoryCache()->updateDecodedResource(cachedImageHighPriority.get(), UpdateForPropertyChange, MemoryCacheLiveResourcePriorityHigh);

    // Should first prune the LowPriority item.
    memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache()->liveSize() - 10, memoryCache()->liveSize() - 10);
    memoryCache()->prune();
    ASSERT_EQ(memoryCache()->deadSize(), 0u);
    ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize);

    // Should prune the HighPriority item.
    memoryCache()->setCapacities(memoryCache()->minDeadCapacity(), memoryCache()->liveSize() - 10, memoryCache()->liveSize() - 10);
    memoryCache()->prune();
    ASSERT_EQ(memoryCache()->deadSize(), 0u);
    ASSERT_EQ(memoryCache()->liveSize(), totalSize - lowPriorityMockDecodeSize - highPriorityMockDecodeSize);

    cachedImageLowPriority->removeClient(&clientLowPriority);
    cachedImageHighPriority->removeClient(&clientHighPriority);
}

TEST_F(MemoryCacheTest, DecodeCacheOrder_Basic)
{
    ResourcePtr<FakeDecodedResource> cachedImageLowPriority =
        new FakeDecodedResource(ResourceRequest("http://foo.com"), Resource::Raw);
    ResourcePtr<FakeDecodedResource> cachedImageHighPriority =
        new FakeDecodedResource(ResourceRequest("http://test/resource"), Resource::Raw);
    TestDecodeCacheOrder(cachedImageLowPriority, cachedImageHighPriority);
}

TEST_F(MemoryCacheTest, DecodeCacheOrder_MultipleResourceMaps)
{
    {
        ResourcePtr<FakeDecodedResource> cachedImageLowPriority =
            new FakeDecodedResource(ResourceRequest("http://foo.com"), Resource::Raw);
        ResourcePtr<FakeDecodedResource> cachedImageHighPriority =
            new FakeDecodedResource(ResourceRequest("http://test/resource"), Resource::Raw);
        cachedImageLowPriority->setCacheIdentifier("foo");
        TestDecodeCacheOrder(cachedImageLowPriority, cachedImageHighPriority);
        memoryCache()->evictResources();
    }
    {
        ResourcePtr<FakeDecodedResource> cachedImageLowPriority =
            new FakeDecodedResource(ResourceRequest("http://foo.com"), Resource::Raw);
        ResourcePtr<FakeDecodedResource> cachedImageHighPriority =
            new FakeDecodedResource(ResourceRequest("http://test/resource"), Resource::Raw);
        cachedImageHighPriority->setCacheIdentifier("foo");
        TestDecodeCacheOrder(cachedImageLowPriority, cachedImageHighPriority);
        memoryCache()->evictResources();
    }
    {
        ResourcePtr<FakeDecodedResource> cachedImageLowPriority =
            new FakeDecodedResource(ResourceRequest("http://test/resource"), Resource::Raw);
        cachedImageLowPriority->setCacheIdentifier("foo");
        ResourcePtr<FakeDecodedResource> cachedImageHighPriority =
            new FakeDecodedResource(ResourceRequest("http://test/resource"), Resource::Raw);
        cachedImageHighPriority->setCacheIdentifier("bar");
        TestDecodeCacheOrder(cachedImageLowPriority, cachedImageHighPriority);
        memoryCache()->evictResources();
    }
}

TEST_F(MemoryCacheTest, MultipleReplace)
{
    ResourcePtr<FakeResource> resource1 = new FakeResource(ResourceRequest("http://test/resource"), Resource::Raw);
    memoryCache()->add(resource1.get());

    ResourcePtr<FakeResource> resource2 = new FakeResource(ResourceRequest("http://test/resource"), 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("http://test/resource"), 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("http://test/resource"), Resource::Raw);
    memoryCache()->add(resource1.get());

    ResourcePtr<FakeResource> resource2 = new FakeResource(ResourceRequest("http://test/resource"), 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("http://test/resource"), 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()));
}

TEST_F(MemoryCacheTest, ResourceMapIsolation)
{
    ResourcePtr<FakeResource> resource1 = new FakeResource(ResourceRequest("http://test/resource"), Resource::Raw);
    memoryCache()->add(resource1.get());

    ResourcePtr<FakeResource> resource2 = new FakeResource(ResourceRequest("http://test/resource"), Resource::Raw);
    resource2->setCacheIdentifier("foo");
    memoryCache()->add(resource2.get());
    EXPECT_TRUE(memoryCache()->contains(resource1.get()));
    EXPECT_TRUE(memoryCache()->contains(resource2.get()));

    const KURL url = KURL(ParsedURLString, "http://test/resource");
    EXPECT_EQ(resource1.get(), memoryCache()->resourceForURL(url));
    EXPECT_EQ(resource1.get(), memoryCache()->resourceForURL(url, memoryCache()->defaultCacheIdentifier()));
    EXPECT_EQ(resource2.get(), memoryCache()->resourceForURL(url, "foo"));

    ResourcePtr<FakeResource> resource3 = new FakeResource(ResourceRequest("http://test/resource"), Resource::Raw);
    resource3->setCacheIdentifier("foo");
    memoryCache()->remove(resource2.get());
    memoryCache()->add(resource3.get());
    EXPECT_TRUE(memoryCache()->contains(resource1.get()));
    EXPECT_FALSE(memoryCache()->contains(resource2.get()));
    EXPECT_TRUE(memoryCache()->contains(resource3.get()));

    ResourcePtr<FakeResource> resource4 = new FakeResource(ResourceRequest("http://test/resource"), Resource::Raw);
    resource4->setCacheIdentifier("foo");
    memoryCache()->replace(resource4.get(), resource3.get());
    EXPECT_TRUE(memoryCache()->contains(resource1.get()));
    EXPECT_FALSE(memoryCache()->contains(resource3.get()));
    EXPECT_TRUE(memoryCache()->contains(resource4.get()));

    WillBeHeapVector<Member<Resource>> resources = memoryCache()->resourcesForURL(url);
    EXPECT_EQ(2u, resources.size());

    memoryCache()->evictResources();
    EXPECT_FALSE(memoryCache()->contains(resource1.get()));
    EXPECT_FALSE(memoryCache()->contains(resource3.get()));
}

} // namespace