Home | History | Annotate | Download | only in loader
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "content/browser/loader/resource_buffer.h"
      6 #include "testing/gtest/include/gtest/gtest.h"
      7 
      8 namespace content {
      9 
     10 TEST(ResourceBufferTest, BasicAllocations) {
     11   scoped_refptr<ResourceBuffer> buf = new ResourceBuffer();
     12   EXPECT_TRUE(buf->Initialize(100, 5, 10));
     13   EXPECT_TRUE(buf->CanAllocate());
     14 
     15   // First allocation
     16   {
     17     int size;
     18     char* ptr = buf->Allocate(&size);
     19     EXPECT_TRUE(ptr);
     20     EXPECT_EQ(10, size);
     21     EXPECT_TRUE(buf->CanAllocate());
     22 
     23     EXPECT_EQ(0, buf->GetLastAllocationOffset());
     24 
     25     buf->ShrinkLastAllocation(2);  // Less than our min allocation size.
     26     EXPECT_EQ(0, buf->GetLastAllocationOffset());
     27     EXPECT_TRUE(buf->CanAllocate());
     28   }
     29 
     30   // Second allocation
     31   {
     32     int size;
     33     char* ptr = buf->Allocate(&size);
     34     EXPECT_TRUE(ptr);
     35     EXPECT_EQ(10, size);
     36     EXPECT_TRUE(buf->CanAllocate());
     37 
     38     EXPECT_EQ(5, buf->GetLastAllocationOffset());
     39 
     40     buf->ShrinkLastAllocation(4);
     41     EXPECT_EQ(5, buf->GetLastAllocationOffset());
     42 
     43     EXPECT_TRUE(buf->CanAllocate());
     44   }
     45 }
     46 
     47 TEST(ResourceBufferTest, AllocateAndRecycle) {
     48   scoped_refptr<ResourceBuffer> buf = new ResourceBuffer();
     49   EXPECT_TRUE(buf->Initialize(100, 5, 10));
     50 
     51   int size;
     52 
     53   buf->Allocate(&size);
     54   EXPECT_EQ(0, buf->GetLastAllocationOffset());
     55 
     56   buf->RecycleLeastRecentlyAllocated();
     57 
     58   // Offset should again be 0.
     59   buf->Allocate(&size);
     60   EXPECT_EQ(0, buf->GetLastAllocationOffset());
     61 }
     62 
     63 TEST(ResourceBufferTest, WrapAround) {
     64   scoped_refptr<ResourceBuffer> buf = new ResourceBuffer();
     65   EXPECT_TRUE(buf->Initialize(20, 10, 10));
     66 
     67   int size;
     68 
     69   buf->Allocate(&size);
     70   EXPECT_EQ(10, size);
     71 
     72   buf->Allocate(&size);
     73   EXPECT_EQ(10, size);
     74 
     75   // Create hole at the beginnning.  Next allocation should go there.
     76   buf->RecycleLeastRecentlyAllocated();
     77 
     78   buf->Allocate(&size);
     79   EXPECT_EQ(10, size);
     80 
     81   EXPECT_EQ(0, buf->GetLastAllocationOffset());
     82 }
     83 
     84 TEST(ResourceBufferTest, WrapAround2) {
     85   scoped_refptr<ResourceBuffer> buf = new ResourceBuffer();
     86   EXPECT_TRUE(buf->Initialize(30, 10, 10));
     87 
     88   int size;
     89 
     90   buf->Allocate(&size);
     91   EXPECT_EQ(10, size);
     92 
     93   buf->Allocate(&size);
     94   EXPECT_EQ(10, size);
     95 
     96   buf->Allocate(&size);
     97   EXPECT_EQ(10, size);
     98 
     99   EXPECT_FALSE(buf->CanAllocate());
    100 
    101   // Create holes at first and second slots.
    102   buf->RecycleLeastRecentlyAllocated();
    103   buf->RecycleLeastRecentlyAllocated();
    104 
    105   EXPECT_TRUE(buf->CanAllocate());
    106 
    107   buf->Allocate(&size);
    108   EXPECT_EQ(10, size);
    109   EXPECT_EQ(0, buf->GetLastAllocationOffset());
    110 
    111   buf->Allocate(&size);
    112   EXPECT_EQ(10, size);
    113   EXPECT_EQ(10, buf->GetLastAllocationOffset());
    114 
    115   EXPECT_FALSE(buf->CanAllocate());
    116 }
    117 
    118 TEST(ResourceBufferTest, Full) {
    119   scoped_refptr<ResourceBuffer> buf = new ResourceBuffer();
    120   EXPECT_TRUE(buf->Initialize(20, 10, 10));
    121 
    122   int size;
    123   buf->Allocate(&size);
    124   EXPECT_EQ(10, size);
    125 
    126   buf->Allocate(&size);
    127   EXPECT_EQ(10, size);
    128 
    129   // Full.
    130   EXPECT_FALSE(buf->CanAllocate());
    131 
    132   // Still full, even if there is a small hole at the end.
    133   buf->ShrinkLastAllocation(5);
    134   EXPECT_FALSE(buf->CanAllocate());
    135 }
    136 
    137 }  // namespace content
    138