Home | History | Annotate | Download | only in quota
      1 // Copyright 2013 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 <set>
      6 
      7 #include "base/bind.h"
      8 #include "base/file_util.h"
      9 #include "base/files/scoped_temp_dir.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/message_loop/message_loop_proxy.h"
     12 #include "base/run_loop.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 #include "webkit/browser/quota/mock_quota_manager.h"
     15 #include "webkit/browser/quota/mock_special_storage_policy.h"
     16 #include "webkit/browser/quota/mock_storage_client.h"
     17 
     18 namespace quota {
     19 
     20 const char kTestOrigin1[] = "http://host1:1/";
     21 const char kTestOrigin2[] = "http://host2:1/";
     22 const char kTestOrigin3[] = "http://host3:1/";
     23 
     24 const GURL kOrigin1(kTestOrigin1);
     25 const GURL kOrigin2(kTestOrigin2);
     26 const GURL kOrigin3(kTestOrigin3);
     27 
     28 const StorageType kTemporary = kStorageTypeTemporary;
     29 const StorageType kPersistent = kStorageTypePersistent;
     30 
     31 const QuotaClient::ID kClientFile = QuotaClient::kFileSystem;
     32 const QuotaClient::ID kClientDB = QuotaClient::kIndexedDatabase;
     33 
     34 class MockQuotaManagerTest : public testing::Test {
     35  public:
     36   MockQuotaManagerTest()
     37     : deletion_callback_count_(0),
     38       weak_factory_(this) {
     39   }
     40 
     41   virtual void SetUp() {
     42     ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
     43     policy_ = new MockSpecialStoragePolicy;
     44     manager_ = new MockQuotaManager(false /* is_incognito */,
     45                                     data_dir_.path(),
     46                                     base::MessageLoopProxy::current().get(),
     47                                     base::MessageLoopProxy::current().get(),
     48                                     policy_.get());
     49   }
     50 
     51   virtual void TearDown() {
     52     // Make sure the quota manager cleans up correctly.
     53     manager_ = NULL;
     54     base::RunLoop().RunUntilIdle();
     55   }
     56 
     57   void GetModifiedOrigins(StorageType type, base::Time since) {
     58     manager_->GetOriginsModifiedSince(
     59         type, since,
     60         base::Bind(&MockQuotaManagerTest::GotModifiedOrigins,
     61                    weak_factory_.GetWeakPtr()));
     62   }
     63 
     64   void GotModifiedOrigins(const std::set<GURL>& origins, StorageType type) {
     65     origins_ = origins;
     66     type_ = type;
     67   }
     68 
     69   void DeleteOriginData(const GURL& origin, StorageType type,
     70       int quota_client_mask) {
     71     manager_->DeleteOriginData(
     72         origin, type, quota_client_mask,
     73         base::Bind(&MockQuotaManagerTest::DeletedOriginData,
     74                    weak_factory_.GetWeakPtr()));
     75   }
     76 
     77   void DeletedOriginData(QuotaStatusCode status) {
     78     ++deletion_callback_count_;
     79     EXPECT_EQ(quota::kQuotaStatusOk, status);
     80   }
     81 
     82   int deletion_callback_count() const {
     83     return deletion_callback_count_;
     84   }
     85 
     86   MockQuotaManager* manager() const {
     87     return manager_.get();
     88   }
     89 
     90   const std::set<GURL>& origins() const {
     91     return origins_;
     92   }
     93 
     94   const StorageType& type() const {
     95     return type_;
     96   }
     97 
     98  private:
     99   base::MessageLoop message_loop_;
    100   base::ScopedTempDir data_dir_;
    101   scoped_refptr<MockQuotaManager> manager_;
    102   scoped_refptr<MockSpecialStoragePolicy> policy_;
    103 
    104   int deletion_callback_count_;
    105 
    106   std::set<GURL> origins_;
    107   StorageType type_;
    108 
    109   base::WeakPtrFactory<MockQuotaManagerTest> weak_factory_;
    110 
    111   DISALLOW_COPY_AND_ASSIGN(MockQuotaManagerTest);
    112 };
    113 
    114 TEST_F(MockQuotaManagerTest, BasicOriginManipulation) {
    115   EXPECT_FALSE(manager()->OriginHasData(kOrigin1, kTemporary, kClientFile));
    116   EXPECT_FALSE(manager()->OriginHasData(kOrigin1, kTemporary, kClientDB));
    117   EXPECT_FALSE(manager()->OriginHasData(kOrigin1, kPersistent, kClientFile));
    118   EXPECT_FALSE(manager()->OriginHasData(kOrigin1, kPersistent, kClientDB));
    119   EXPECT_FALSE(manager()->OriginHasData(kOrigin2, kTemporary, kClientFile));
    120   EXPECT_FALSE(manager()->OriginHasData(kOrigin2, kTemporary, kClientDB));
    121   EXPECT_FALSE(manager()->OriginHasData(kOrigin2, kPersistent, kClientFile));
    122   EXPECT_FALSE(manager()->OriginHasData(kOrigin2, kPersistent, kClientDB));
    123 
    124   manager()->AddOrigin(kOrigin1, kTemporary, kClientFile, base::Time::Now());
    125   EXPECT_TRUE(manager()->OriginHasData(kOrigin1, kTemporary, kClientFile));
    126   EXPECT_FALSE(manager()->OriginHasData(kOrigin1, kTemporary, kClientDB));
    127   EXPECT_FALSE(manager()->OriginHasData(kOrigin1, kPersistent, kClientFile));
    128   EXPECT_FALSE(manager()->OriginHasData(kOrigin1, kPersistent, kClientDB));
    129   EXPECT_FALSE(manager()->OriginHasData(kOrigin2, kTemporary, kClientFile));
    130   EXPECT_FALSE(manager()->OriginHasData(kOrigin2, kTemporary, kClientDB));
    131   EXPECT_FALSE(manager()->OriginHasData(kOrigin2, kPersistent, kClientFile));
    132   EXPECT_FALSE(manager()->OriginHasData(kOrigin2, kPersistent, kClientDB));
    133 
    134   manager()->AddOrigin(kOrigin1, kPersistent, kClientFile, base::Time::Now());
    135   EXPECT_TRUE(manager()->OriginHasData(kOrigin1, kTemporary, kClientFile));
    136   EXPECT_FALSE(manager()->OriginHasData(kOrigin1, kTemporary, kClientDB));
    137   EXPECT_TRUE(manager()->OriginHasData(kOrigin1, kPersistent, kClientFile));
    138   EXPECT_FALSE(manager()->OriginHasData(kOrigin1, kPersistent, kClientDB));
    139   EXPECT_FALSE(manager()->OriginHasData(kOrigin2, kTemporary, kClientFile));
    140   EXPECT_FALSE(manager()->OriginHasData(kOrigin2, kTemporary, kClientDB));
    141   EXPECT_FALSE(manager()->OriginHasData(kOrigin2, kPersistent, kClientFile));
    142   EXPECT_FALSE(manager()->OriginHasData(kOrigin2, kPersistent, kClientDB));
    143 
    144   manager()->AddOrigin(kOrigin2, kTemporary, kClientFile | kClientDB,
    145       base::Time::Now());
    146   EXPECT_TRUE(manager()->OriginHasData(kOrigin1, kTemporary, kClientFile));
    147   EXPECT_FALSE(manager()->OriginHasData(kOrigin1, kTemporary, kClientDB));
    148   EXPECT_TRUE(manager()->OriginHasData(kOrigin1, kPersistent, kClientFile));
    149   EXPECT_FALSE(manager()->OriginHasData(kOrigin1, kPersistent, kClientDB));
    150   EXPECT_TRUE(manager()->OriginHasData(kOrigin2, kTemporary, kClientFile));
    151   EXPECT_TRUE(manager()->OriginHasData(kOrigin2, kTemporary, kClientDB));
    152   EXPECT_FALSE(manager()->OriginHasData(kOrigin2, kPersistent, kClientFile));
    153   EXPECT_FALSE(manager()->OriginHasData(kOrigin2, kPersistent, kClientDB));
    154 }
    155 
    156 TEST_F(MockQuotaManagerTest, OriginDeletion) {
    157   manager()->AddOrigin(kOrigin1, kTemporary, kClientFile, base::Time::Now());
    158   manager()->AddOrigin(kOrigin2, kTemporary, kClientFile | kClientDB,
    159       base::Time::Now());
    160   manager()->AddOrigin(kOrigin3, kTemporary, kClientFile | kClientDB,
    161       base::Time::Now());
    162 
    163   DeleteOriginData(kOrigin2, kTemporary, kClientFile);
    164   base::RunLoop().RunUntilIdle();
    165 
    166   EXPECT_EQ(1, deletion_callback_count());
    167   EXPECT_TRUE(manager()->OriginHasData(kOrigin1, kTemporary, kClientFile));
    168   EXPECT_FALSE(manager()->OriginHasData(kOrigin2, kTemporary, kClientFile));
    169   EXPECT_TRUE(manager()->OriginHasData(kOrigin2, kTemporary, kClientDB));
    170   EXPECT_TRUE(manager()->OriginHasData(kOrigin3, kTemporary, kClientFile));
    171   EXPECT_TRUE(manager()->OriginHasData(kOrigin3, kTemporary, kClientDB));
    172 
    173   DeleteOriginData(kOrigin3, kTemporary, kClientFile | kClientDB);
    174   base::RunLoop().RunUntilIdle();
    175 
    176   EXPECT_EQ(2, deletion_callback_count());
    177   EXPECT_TRUE(manager()->OriginHasData(kOrigin1, kTemporary, kClientFile));
    178   EXPECT_FALSE(manager()->OriginHasData(kOrigin2, kTemporary, kClientFile));
    179   EXPECT_TRUE(manager()->OriginHasData(kOrigin2, kTemporary, kClientDB));
    180   EXPECT_FALSE(manager()->OriginHasData(kOrigin3, kTemporary, kClientFile));
    181   EXPECT_FALSE(manager()->OriginHasData(kOrigin3, kTemporary, kClientDB));
    182 }
    183 
    184 TEST_F(MockQuotaManagerTest, ModifiedOrigins) {
    185   base::Time now = base::Time::Now();
    186   base::Time then = base::Time();
    187   base::TimeDelta an_hour = base::TimeDelta::FromMilliseconds(3600000);
    188   base::TimeDelta a_minute = base::TimeDelta::FromMilliseconds(60000);
    189 
    190   GetModifiedOrigins(kTemporary, then);
    191   base::RunLoop().RunUntilIdle();
    192   EXPECT_TRUE(origins().empty());
    193 
    194   manager()->AddOrigin(kOrigin1, kTemporary, kClientFile, now - an_hour);
    195 
    196   GetModifiedOrigins(kTemporary, then);
    197   base::RunLoop().RunUntilIdle();
    198 
    199   EXPECT_EQ(kTemporary, type());
    200   EXPECT_EQ(1UL, origins().size());
    201   EXPECT_EQ(1UL, origins().count(kOrigin1));
    202   EXPECT_EQ(0UL, origins().count(kOrigin2));
    203 
    204   manager()->AddOrigin(kOrigin2, kTemporary, kClientFile, now);
    205 
    206   GetModifiedOrigins(kTemporary, then);
    207   base::RunLoop().RunUntilIdle();
    208 
    209   EXPECT_EQ(kTemporary, type());
    210   EXPECT_EQ(2UL, origins().size());
    211   EXPECT_EQ(1UL, origins().count(kOrigin1));
    212   EXPECT_EQ(1UL, origins().count(kOrigin2));
    213 
    214   GetModifiedOrigins(kTemporary, now - a_minute);
    215   base::RunLoop().RunUntilIdle();
    216 
    217   EXPECT_EQ(kTemporary, type());
    218   EXPECT_EQ(1UL, origins().size());
    219   EXPECT_EQ(0UL, origins().count(kOrigin1));
    220   EXPECT_EQ(1UL, origins().count(kOrigin2));
    221 }
    222 }  // Namespace quota
    223