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 |