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..cdff9980bd7e2a3a10b3da7bd6ccd13ebf973d54 |
--- /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::ARTICLE_CONTENT_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 |