Chromium Code Reviews| 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)); | |
|
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.
| |
| 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_->DemandQuota()); | |
| 36 EXPECT_TRUE(counter_->DemandQuota()); | |
| 37 EXPECT_FALSE(counter_->DemandQuota()); | |
| 38 } | |
| 39 | |
| 40 TEST_F(RequestCounterTest, ForcedDoesNotCountInQuota) { | |
| 41 EXPECT_TRUE(counter_->DemandQuota()); | |
| 42 counter_->ReportForcedRequest(); | |
| 43 EXPECT_TRUE(counter_->DemandQuota()); | |
| 44 } | |
| 45 | |
| 46 TEST_F(RequestCounterTest, QuotaIsPerDay) { | |
| 47 EXPECT_TRUE(counter_->DemandQuota()); | |
| 48 EXPECT_TRUE(counter_->DemandQuota()); | |
| 49 | |
| 50 // 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
| |
| 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_->DemandQuota()); | |
| 57 } | |
| 58 | |
| 59 } // namespace ntp_snippets | |
| OLD | NEW |