Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Unified Diff: chrome/browser/engagement/site_engagement_eviction_policy_unittest.cc

Issue 1343273003: Integrate SiteEngagementEvictionPolicy with QuotaManager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@add_eviction_policy
Patch Set: rebase Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/engagement/site_engagement_eviction_policy_unittest.cc
diff --git a/chrome/browser/engagement/site_engagement_eviction_policy_unittest.cc b/chrome/browser/engagement/site_engagement_eviction_policy_unittest.cc
index 49757f2b67911c57eac4425e70bc17e4c0c25fe6..dcdf4191069bacb67f8049761ac573ce3e3e0d5b 100644
--- a/chrome/browser/engagement/site_engagement_eviction_policy_unittest.cc
+++ b/chrome/browser/engagement/site_engagement_eviction_policy_unittest.cc
@@ -2,16 +2,30 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/files/scoped_temp_dir.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/memory/weak_ptr.h"
+#include "base/run_loop.h"
+#include "base/thread_task_runner_handle.h"
#include "chrome/browser/engagement/site_engagement_eviction_policy.h"
#include "chrome/browser/engagement/site_engagement_service.h"
#include "content/public/test/mock_special_storage_policy.h"
+#include "content/public/test/mock_storage_client.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"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
+const char* kFooOrigin = "http://foo.com/";
+
const int64 kGlobalQuota = 25 * 1024;
-} // namespace
+// Returns a deterministic value for the amount of available disk space.
+int64 GetAvailableDiskSpaceForTest(const base::FilePath&) {
+ return kGlobalQuota;
+}
class TestSiteEngagementScoreProvider : public SiteEngagementScoreProvider {
public:
@@ -38,6 +52,103 @@ class TestSiteEngagementScoreProvider : public SiteEngagementScoreProvider {
DISALLOW_COPY_AND_ASSIGN(TestSiteEngagementScoreProvider);
};
+SiteEngagementScoreProvider* GetTestSiteEngagementScoreProvider(
+ content::BrowserContext*) {
+ CR_DEFINE_STATIC_LOCAL(TestSiteEngagementScoreProvider,
+ kGlobalTestScoreProvider, ());
+ return &kGlobalTestScoreProvider;
+}
+
+} // namespace
+
+class SiteEngagementEvictionPolicyWithQuotaManagerTest : public testing::Test {
+ public:
+ SiteEngagementEvictionPolicyWithQuotaManagerTest() : weak_factory_(this) {}
+
+ void SetUp() override {
+ ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
+ mock_special_storage_policy_ = new content::MockSpecialStoragePolicy;
+
+ quota_manager_ = new storage::QuotaManager(
+ false, data_dir_.path(), base::ThreadTaskRunnerHandle::Get().get(),
+ base::ThreadTaskRunnerHandle::Get().get(),
+ mock_special_storage_policy_.get());
+
+ // Populate the QuotaManager with some data.
+ static const content::MockOriginData kData[] = {
+ {kFooOrigin, storage::kStorageTypeTemporary, 1},
+ };
+ quota_manager_->proxy()->RegisterClient(new content::MockStorageClient(
+ quota_manager_->proxy(), kData, storage::QuotaClient::kFileSystem,
+ arraysize(kData)));
+
+ // Set the eviction policy.
+ quota_manager_->SetQuotaEvictionPolicy(
+ storage::kStorageTypeTemporary,
+ make_scoped_ptr(new SiteEngagementEvictionPolicy(
+ storage::kStorageTypeTemporary, quota_manager_.get(), nullptr)));
+
+ // Don't (automatically) start the eviction for testing.
+ quota_manager_->eviction_disabled_ = true;
+ // Don't query the hard disk for remaining capacity.
+ quota_manager_->get_disk_space_fn_ = &GetAvailableDiskSpaceForTest;
+
+ score_provider_callback_ = base::Bind(&GetTestSiteEngagementScoreProvider);
+
+ // Use a TestSiteEngagementScoreProvider when retrieving the engagement
+ // scores.
+ SiteEngagementEvictionPolicy::
+ SetSiteEngagementScoreProviderCallbackForTests(
+ &score_provider_callback_);
+ }
+
+ void TearDown() override {
+ SiteEngagementEvictionPolicy::
+ SetSiteEngagementScoreProviderCallbackForTests(nullptr);
+ // Make sure the quota manager cleans up correctly.
+ quota_manager_ = nullptr;
+ base::RunLoop().RunUntilIdle();
+ }
+
+ protected:
+ void GetEvictionOrigin() {
+ quota_manager_->GetEvictionOrigin(
+ storage::kStorageTypeTemporary,
+ base::Bind(&SiteEngagementEvictionPolicyWithQuotaManagerTest::
+ OnDidGetEvictionOrigin,
+ weak_factory_.GetWeakPtr()));
+ }
+
+ void OnDidGetEvictionOrigin(const GURL& origin) { eviction_origin_ = origin; }
+
+ const GURL& eviction_origin() { return eviction_origin_; }
+
+ private:
+ content::TestBrowserThreadBundle thread_bundle_;
+ base::ScopedTempDir data_dir_;
+
+ scoped_refptr<storage::QuotaManager> quota_manager_;
+ scoped_refptr<content::MockSpecialStoragePolicy> mock_special_storage_policy_;
+ base::Callback<SiteEngagementScoreProvider*(content::BrowserContext*)>
+ score_provider_callback_;
+
+ GURL eviction_origin_;
+
+ base::WeakPtrFactory<SiteEngagementEvictionPolicyWithQuotaManagerTest>
+ weak_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(SiteEngagementEvictionPolicyWithQuotaManagerTest);
+};
+
+// Ensure the SiteEngagementEvictionPolicy completes successfully
+// when used as a QuotaManager's eviction policy.
+TEST_F(SiteEngagementEvictionPolicyWithQuotaManagerTest, GetEvictionOrigin) {
+ GetEvictionOrigin();
+ base::RunLoop().RunUntilIdle();
+
+ EXPECT_EQ(GURL(kFooOrigin), eviction_origin());
+}
+
class SiteEngagementEvictionPolicyTest : public testing::Test {
public:
SiteEngagementEvictionPolicyTest()
@@ -47,7 +158,7 @@ class SiteEngagementEvictionPolicyTest : public testing::Test {
~SiteEngagementEvictionPolicyTest() override {}
GURL CalculateEvictionOrigin(const std::map<GURL, int64>& usage) {
- return SiteEngagementEvictionPolicy::CalculateEvictionOrigin(
+ return SiteEngagementEvictionPolicy::CalculateEvictionOriginForTests(
storage_policy_, score_provider_.get(), usage, kGlobalQuota);
}

Powered by Google App Engine
This is Rietveld 408576698