OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/ntp_snippets/request_counter.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/strings/stringprintf.h" |
| 10 #include "components/ntp_snippets/pref_names.h" |
| 11 #include "components/prefs/pref_registry_simple.h" |
| 12 #include "components/prefs/testing_pref_service.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace { |
| 16 const int kCounterQuota = 2; |
| 17 } // namespace |
| 18 |
| 19 namespace ntp_snippets { |
| 20 |
| 21 class RequestCounterTest : public testing::Test { |
| 22 public: |
| 23 RequestCounterTest() { |
| 24 RequestCounter::RegisterProfilePrefs(test_prefs_.registry()); |
| 25 // Use any arbitrary RequestType for this unittest. |
| 26 counter_.reset(new RequestCounter( |
| 27 &test_prefs_, RequestCounter::RequestType::ARTICLE_CONTENT_FETCHER, |
| 28 kCounterQuota)); |
| 29 } |
| 30 |
| 31 protected: |
| 32 TestingPrefServiceSimple test_prefs_; |
| 33 std::unique_ptr<RequestCounter> counter_; |
| 34 |
| 35 private: |
| 36 DISALLOW_COPY_AND_ASSIGN(RequestCounterTest); |
| 37 }; |
| 38 |
| 39 TEST_F(RequestCounterTest, QuotaExceeded) { |
| 40 EXPECT_TRUE(counter_->DemandQuotaForRequest()); |
| 41 EXPECT_TRUE(counter_->DemandQuotaForRequest()); |
| 42 EXPECT_FALSE(counter_->DemandQuotaForRequest()); |
| 43 } |
| 44 |
| 45 TEST_F(RequestCounterTest, ForcedDoesNotCountInQuota) { |
| 46 EXPECT_TRUE(counter_->DemandQuotaForRequest()); |
| 47 counter_->ReportForcedRequest(); |
| 48 EXPECT_TRUE(counter_->DemandQuotaForRequest()); |
| 49 } |
| 50 |
| 51 TEST_F(RequestCounterTest, QuotaIsPerDay) { |
| 52 EXPECT_TRUE(counter_->DemandQuotaForRequest()); |
| 53 EXPECT_TRUE(counter_->DemandQuotaForRequest()); |
| 54 |
| 55 // Now fake the day pref so that the counter believes the count comes from |
| 56 // yesterday. |
| 57 int now_day = (base::Time::Now() - base::Time::UnixEpoch()).InDays(); |
| 58 test_prefs_.SetInteger(ntp_snippets::prefs::kSnippetFetcherQuotaDay, |
| 59 now_day - 1); |
| 60 |
| 61 // The quota should get reset as the day has changed. |
| 62 EXPECT_TRUE(counter_->DemandQuotaForRequest()); |
| 63 } |
| 64 |
| 65 } // namespace ntp_snippets |
OLD | NEW |