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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "testing/gtest/include/gtest/gtest.h"
#include "base/bind.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_loop_proxy.h"
#include "chrome/browser/browsing_data/browsing_data_quota_helper_impl.h"
#include "content/public/test/mock_storage_client.h"
#include "content/public/test/test_browser_thread.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "storage/browser/quota/quota_manager.h"
#include "storage/browser/quota/quota_manager_proxy.h"
using content::BrowserThread;
using content::MockOriginData;
using content::MockStorageClient;
class BrowsingDataQuotaHelperTest : public testing::Test {
public:
typedef BrowsingDataQuotaHelper::QuotaInfo QuotaInfo;
typedef BrowsingDataQuotaHelper::QuotaInfoArray QuotaInfoArray;
BrowsingDataQuotaHelperTest()
: fetching_completed_(true),
quota_(-1),
weak_factory_(this) {}
~BrowsingDataQuotaHelperTest() override {}
void SetUp() override {
EXPECT_TRUE(dir_.CreateUniqueTempDir());
quota_manager_ = new storage::QuotaManager(
false,
dir_.path(),
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(),
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(),
NULL);
helper_ = new BrowsingDataQuotaHelperImpl(
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI).get(),
BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(),
quota_manager_.get());
}
void TearDown() override {
helper_ = NULL;
quota_manager_ = NULL;
quota_info_.clear();
base::MessageLoop::current()->RunUntilIdle();
}
protected:
const QuotaInfoArray& quota_info() const {
return quota_info_;
}
bool fetching_completed() const {
return fetching_completed_;
}
void StartFetching() {
fetching_completed_ = false;
helper_->StartFetching(
base::Bind(&BrowsingDataQuotaHelperTest::FetchCompleted,
weak_factory_.GetWeakPtr()));
}
void RegisterClient(const MockOriginData* data, std::size_t data_len) {
MockStorageClient* client =
new MockStorageClient(quota_manager_->proxy(),
data,
storage::QuotaClient::kFileSystem,
data_len);
quota_manager_->proxy()->RegisterClient(client);
client->TouchAllOriginsAndNotify();
}
void SetPersistentHostQuota(const std::string& host, int64 quota) {
quota_ = -1;
quota_manager_->SetPersistentHostQuota(
host, quota,
base::Bind(&BrowsingDataQuotaHelperTest::GotPersistentHostQuota,
weak_factory_.GetWeakPtr()));
}
void GetPersistentHostQuota(const std::string& host) {
quota_ = -1;
quota_manager_->GetPersistentHostQuota(
host,
base::Bind(&BrowsingDataQuotaHelperTest::GotPersistentHostQuota,
weak_factory_.GetWeakPtr()));
}
void GotPersistentHostQuota(storage::QuotaStatusCode status, int64 quota) {
EXPECT_EQ(storage::kQuotaStatusOk, status);
quota_ = quota;
}
void RevokeHostQuota(const std::string& host) {
helper_->RevokeHostQuota(host);
}
int64 quota() {
return quota_;
}
private:
void FetchCompleted(const QuotaInfoArray& quota_info) {
quota_info_ = quota_info;
fetching_completed_ = true;
}
content::TestBrowserThreadBundle thread_bundle_;
scoped_refptr<storage::QuotaManager> quota_manager_;
base::ScopedTempDir dir_;
scoped_refptr<BrowsingDataQuotaHelper> helper_;
bool fetching_completed_;
QuotaInfoArray quota_info_;
int64 quota_;
base::WeakPtrFactory<BrowsingDataQuotaHelperTest> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(BrowsingDataQuotaHelperTest);
};
TEST_F(BrowsingDataQuotaHelperTest, Empty) {
StartFetching();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_TRUE(fetching_completed());
EXPECT_TRUE(quota_info().empty());
}
TEST_F(BrowsingDataQuotaHelperTest, FetchData) {
const MockOriginData kOrigins[] = {
{"http://example.com/", storage::kStorageTypeTemporary, 1},
{"https://example.com/", storage::kStorageTypeTemporary, 10},
{"http://example.com/", storage::kStorageTypePersistent, 100},
{"https://example.com/", storage::kStorageTypeSyncable, 1},
{"http://example2.com/", storage::kStorageTypeTemporary, 1000},
};
RegisterClient(kOrigins, arraysize(kOrigins));
StartFetching();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_TRUE(fetching_completed());
std::set<QuotaInfo> expected, actual;
actual.insert(quota_info().begin(), quota_info().end());
expected.insert(QuotaInfo("example.com", 11, 100, 1));
expected.insert(QuotaInfo("example2.com", 1000, 0, 0));
EXPECT_TRUE(expected == actual);
}
TEST_F(BrowsingDataQuotaHelperTest, IgnoreExtensionsAndDevTools) {
const MockOriginData kOrigins[] = {
{"http://example.com/", storage::kStorageTypeTemporary, 1},
{"https://example.com/", storage::kStorageTypeTemporary, 10},
{"http://example.com/", storage::kStorageTypePersistent, 100},
{"https://example.com/", storage::kStorageTypeSyncable, 1},
{"http://example2.com/", storage::kStorageTypeTemporary, 1000},
{"chrome-extension://abcdefghijklmnopqrstuvwxyz/",
storage::kStorageTypeTemporary, 10000},
{"chrome-extension://abcdefghijklmnopqrstuvwxyz/",
storage::kStorageTypePersistent, 100000},
{"chrome-devtools://abcdefghijklmnopqrstuvwxyz/",
storage::kStorageTypeTemporary, 10000},
{"chrome-devtools://abcdefghijklmnopqrstuvwxyz/",
storage::kStorageTypePersistent, 100000},
};
RegisterClient(kOrigins, arraysize(kOrigins));
StartFetching();
base::MessageLoop::current()->RunUntilIdle();
EXPECT_TRUE(fetching_completed());
std::set<QuotaInfo> expected, actual;
actual.insert(quota_info().begin(), quota_info().end());
expected.insert(QuotaInfo("example.com", 11, 100, 1));
expected.insert(QuotaInfo("example2.com", 1000, 0, 0));
EXPECT_TRUE(expected == actual);
}
TEST_F(BrowsingDataQuotaHelperTest, RevokeHostQuota) {
const std::string kHost1("example1.com");
const std::string kHost2("example2.com");
SetPersistentHostQuota(kHost1, 1);
SetPersistentHostQuota(kHost2, 10);
base::MessageLoop::current()->RunUntilIdle();
RevokeHostQuota(kHost1);
base::MessageLoop::current()->RunUntilIdle();
GetPersistentHostQuota(kHost1);
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(0, quota());
GetPersistentHostQuota(kHost2);
base::MessageLoop::current()->RunUntilIdle();
EXPECT_EQ(10, quota());
}
|