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

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: 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..0eab208bf4b9752bea2e8f5d3ef173ded3707440 100644
--- a/chrome/browser/engagement/site_engagement_eviction_policy_unittest.cc
+++ b/chrome/browser/engagement/site_engagement_eviction_policy_unittest.cc
@@ -2,16 +2,28 @@
// 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.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 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 +50,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()
+ : ui_thread_(content::BrowserThread::UI, &message_loop_),
+ 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[] = {
+ {"http://foo.com/", storage::kStorageTypeTemporary, 1},
+ {"http://foo.com:1/", storage::kStorageTypeTemporary, 20},
+ {"https://foo.com/", storage::kStorageTypeTemporary, 300},
+ };
+ 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;
+
+ // Use a TestSiteEngagementScoreProvider when retrieving the engagement
+ // scores.
+ SiteEngagementEvictionPolicy::SetSiteEngagementScoreProviderCallback(
+ base::Bind(&GetTestSiteEngagementScoreProvider));
+ }
+
+ void TearDown() override {
+ // 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:
+ base::MessageLoop message_loop_;
+ base::ScopedTempDir data_dir_;
+
+ scoped_refptr<storage::QuotaManager> quota_manager_;
+ scoped_refptr<content::MockSpecialStoragePolicy> mock_special_storage_policy_;
+
+ content::TestBrowserThread ui_thread_;
+
+ GURL eviction_origin_;
+
+ base::WeakPtrFactory<SiteEngagementEvictionPolicyWithQuotaManagerTest>
+ weak_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(SiteEngagementEvictionPolicyWithQuotaManagerTest);
+};
+
+// Ensure the SiteEngagementEvictionPolicy completes completes successfully
raymes 2015/09/17 04:14:31 nit: completes completes
calamity 2015/09/17 06:06:46 Done.
+// when used as a QuotaManager's eviction policy.
+TEST_F(SiteEngagementEvictionPolicyWithQuotaManagerTest, GetEvictionOrigin) {
+ GetEvictionOrigin();
+ base::RunLoop().RunUntilIdle();
+
+ EXPECT_FALSE(eviction_origin().is_empty());
raymes 2015/09/17 04:14:31 Can we actually verify what the expected origin sh
calamity 2015/09/17 06:06:46 I think that's the job of the unit tests. This tes
raymes 2015/09/17 06:58:21 I think it would be nice to verify that one of the
calamity 2015/09/21 03:27:51 Done.
+}
+
class SiteEngagementEvictionPolicyTest : public testing::Test {
public:
SiteEngagementEvictionPolicyTest()

Powered by Google App Engine
This is Rietveld 408576698