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

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: A minor fix #3 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..6d9e194dd336e6a94b4489f71a466954446ab1f9
--- /dev/null
+++ b/components/ntp_snippets/request_counter_unittest.cc
@@ -0,0 +1,59 @@
+// 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 "base/strings/stringprintf.h"
+#include "components/ntp_snippets/pref_names.h"
+#include "components/prefs/testing_pref_service.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace ntp_snippets {
+namespace {
+ const char kTestId[] = "test_id";
+} // namespace
+
+class RequestCounterTest : public testing::Test {
+ public:
+ RequestCounterTest()
+ : counter_(&test_prefs_, kTestId, 2) {
Marc Treib 2016/07/19 09:06:17 nit: I think this should be indented by four more
jkrcal 2016/07/20 09:40:40 Done.
+ RequestCounter::RegisterProfilePrefs(test_prefs_.registry(), kTestId);
+ }
+
+ protected:
+ TestingPrefServiceSimple test_prefs_;
+ RequestCounter counter_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(RequestCounterTest);
+};
+
+TEST_F(RequestCounterTest, QuotaExceeded) {
+ EXPECT_TRUE(counter_.IsQuotaAvailable());
+ EXPECT_TRUE(counter_.IsQuotaAvailable());
+ EXPECT_FALSE(counter_.IsQuotaAvailable());
+}
+
+TEST_F(RequestCounterTest, ForcedDoesNotCountInQuota) {
+ EXPECT_TRUE(counter_.IsQuotaAvailable());
+ counter_.ReportForcedRequest();
+ EXPECT_TRUE(counter_.IsQuotaAvailable());
+}
+
+TEST_F(RequestCounterTest, QuotaIsPerDay) {
+ EXPECT_TRUE(counter_.IsQuotaAvailable());
+ EXPECT_TRUE(counter_.IsQuotaAvailable());
+
+ // Now fake the counter to believe the count comes from yesterday.
+ base::Time time_yesterday = base::Time::Now() - base::TimeDelta::FromDays(1);
+ int yesterday =
+ base::TimeDelta::FromMilliseconds(time_yesterday.ToJavaTime()).InDays();
+ test_prefs_.SetInteger(base::StringPrintf(
+ ntp_snippets::prefs::kSnippetQuotaDayFormat, kTestId), yesterday);
+
+
+ EXPECT_TRUE(counter_.IsQuotaAvailable());
+}
+
+} // namespace ntp_snippets
« components/ntp_snippets/request_counter.cc ('K') | « components/ntp_snippets/request_counter.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698