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 ntp_snippets { |
| 16 |
| 17 class RequestCounterTest : public testing::Test { |
| 18 public: |
| 19 RequestCounterTest() { |
| 20 RequestCounter::RegisterProfilePrefs(test_prefs_.registry()); |
| 21 // Use any arbitrary RequestType for this unittest. |
| 22 counter_.reset(new RequestCounter( |
| 23 &test_prefs_, RequestCounter::RequestType::ARTICLE_CONTENT_FETCHER, 2)); |
| 24 } |
| 25 |
| 26 protected: |
| 27 TestingPrefServiceSimple test_prefs_; |
| 28 std::unique_ptr<RequestCounter> counter_; |
| 29 |
| 30 private: |
| 31 DISALLOW_COPY_AND_ASSIGN(RequestCounterTest); |
| 32 }; |
| 33 |
| 34 TEST_F(RequestCounterTest, QuotaExceeded) { |
| 35 EXPECT_TRUE(counter_->GetQuota()); |
| 36 EXPECT_TRUE(counter_->GetQuota()); |
| 37 EXPECT_FALSE(counter_->GetQuota()); |
| 38 } |
| 39 |
| 40 TEST_F(RequestCounterTest, ForcedDoesNotCountInQuota) { |
| 41 EXPECT_TRUE(counter_->GetQuota()); |
| 42 counter_->ReportForcedRequest(); |
| 43 EXPECT_TRUE(counter_->GetQuota()); |
| 44 } |
| 45 |
| 46 TEST_F(RequestCounterTest, QuotaIsPerDay) { |
| 47 EXPECT_TRUE(counter_->GetQuota()); |
| 48 EXPECT_TRUE(counter_->GetQuota()); |
| 49 |
| 50 // Now fake the counter to believe the count comes from yesterday. |
| 51 int now_day = (base::Time::Now() - base::Time::UnixEpoch()).InDays(); |
| 52 test_prefs_.SetInteger(ntp_snippets::prefs::kSnippetFetcherQuotaDay, |
| 53 now_day - 1); |
| 54 |
| 55 // The quota should get reset as the day has changed. |
| 56 EXPECT_TRUE(counter_->GetQuota()); |
| 57 } |
| 58 |
| 59 } // namespace ntp_snippets |
OLD | NEW |