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

Unified Diff: components/ntp_snippets/request_counter_unittest.cc

Issue 2158843002: Introduce a request throttler for limiting requests in mobile NTP. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix to compile unittests Created 4 years, 5 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: components/ntp_snippets/request_counter_unittest.cc
diff --git a/components/ntp_snippets/request_counter_unittest.cc b/components/ntp_snippets/request_counter_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..a438271577afe6f7cab24eb38e22cc294d87c429
--- /dev/null
+++ b/components/ntp_snippets/request_counter_unittest.cc
@@ -0,0 +1,65 @@
+// Copyright 2016 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 "components/ntp_snippets/request_counter.h"
+
+#include <memory>
+
+#include "base/strings/stringprintf.h"
+#include "components/ntp_snippets/pref_names.h"
+#include "components/prefs/pref_registry_simple.h"
+#include "components/prefs/testing_pref_service.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+const int kCounterQuota = 2;
+} // namespace
+
+namespace ntp_snippets {
+
+class RequestCounterTest : public testing::Test {
+ public:
+ RequestCounterTest() {
+ RequestCounter::RegisterProfilePrefs(test_prefs_.registry());
+ // Use any arbitrary RequestType for this unittest.
+ counter_.reset(new RequestCounter(
+ &test_prefs_, RequestCounter::RequestType::CONTENT_SUGGESTION_FETCHER,
+ kCounterQuota));
+ }
+
+ protected:
+ TestingPrefServiceSimple test_prefs_;
+ std::unique_ptr<RequestCounter> counter_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(RequestCounterTest);
+};
+
+TEST_F(RequestCounterTest, QuotaExceeded) {
+ EXPECT_TRUE(counter_->DemandQuotaForRequest());
+ EXPECT_TRUE(counter_->DemandQuotaForRequest());
+ EXPECT_FALSE(counter_->DemandQuotaForRequest());
+}
+
+TEST_F(RequestCounterTest, ForcedDoesNotCountInQuota) {
+ EXPECT_TRUE(counter_->DemandQuotaForRequest());
+ counter_->ReportForcedRequest();
+ EXPECT_TRUE(counter_->DemandQuotaForRequest());
+}
+
+TEST_F(RequestCounterTest, QuotaIsPerDay) {
+ EXPECT_TRUE(counter_->DemandQuotaForRequest());
+ EXPECT_TRUE(counter_->DemandQuotaForRequest());
+
+ // Now fake the day pref so that the counter believes the count comes from
+ // yesterday.
+ int now_day = (base::Time::Now() - base::Time::UnixEpoch()).InDays();
+ test_prefs_.SetInteger(ntp_snippets::prefs::kSnippetFetcherQuotaDay,
+ now_day - 1);
+
+ // The quota should get reset as the day has changed.
+ EXPECT_TRUE(counter_->DemandQuotaForRequest());
+}
+
+} // namespace ntp_snippets

Powered by Google App Engine
This is Rietveld 408576698