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..418a51bd5427f143bb154147e0b39b37a43c6704 |
--- /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 <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 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, 2)); |
Marc Treib
2016/07/20 12:58:27
I'd make the "2" a constant, so it's clearer what
jkrcal
2016/07/20 14:20:17
Done.
|
+ } |
+ |
+ protected: |
+ TestingPrefServiceSimple test_prefs_; |
+ std::unique_ptr<RequestCounter> counter_; |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(RequestCounterTest); |
+}; |
+ |
+TEST_F(RequestCounterTest, QuotaExceeded) { |
+ EXPECT_TRUE(counter_->DemandQuota()); |
+ EXPECT_TRUE(counter_->DemandQuota()); |
+ EXPECT_FALSE(counter_->DemandQuota()); |
+} |
+ |
+TEST_F(RequestCounterTest, ForcedDoesNotCountInQuota) { |
+ EXPECT_TRUE(counter_->DemandQuota()); |
+ counter_->ReportForcedRequest(); |
+ EXPECT_TRUE(counter_->DemandQuota()); |
+} |
+ |
+TEST_F(RequestCounterTest, QuotaIsPerDay) { |
+ EXPECT_TRUE(counter_->DemandQuota()); |
+ EXPECT_TRUE(counter_->DemandQuota()); |
+ |
+ // Now fake the counter to believe the count comes from yesterday. |
Marc Treib
2016/07/20 12:58:27
nit: You're faking the "day" pref, not the counter
jkrcal
2016/07/20 14:20:17
I wanted to have that but did not find anything re
Marc Treib
2016/07/20 15:13:57
Hah, found it: https://cs.chromium.org/chromium/sr
jkrcal
2016/07/20 15:46:22
Thanks for the pointer! I would like to move on. T
|
+ 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_->DemandQuota()); |
+} |
+ |
+} // namespace ntp_snippets |