Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Side by Side Diff: components/ntp_snippets/request_counter_unittest.cc

Issue 2158843002: Introduce a request throttler for limiting requests in mobile NTP. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: A minor fix #3 Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "base/strings/stringprintf.h"
8 #include "components/ntp_snippets/pref_names.h"
9 #include "components/prefs/testing_pref_service.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace ntp_snippets {
13 namespace {
14 const char kTestId[] = "test_id";
15 } // namespace
16
17 class RequestCounterTest : public testing::Test {
18 public:
19 RequestCounterTest()
20 : 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.
21 RequestCounter::RegisterProfilePrefs(test_prefs_.registry(), kTestId);
22 }
23
24 protected:
25 TestingPrefServiceSimple test_prefs_;
26 RequestCounter counter_;
27
28 private:
29 DISALLOW_COPY_AND_ASSIGN(RequestCounterTest);
30 };
31
32 TEST_F(RequestCounterTest, QuotaExceeded) {
33 EXPECT_TRUE(counter_.IsQuotaAvailable());
34 EXPECT_TRUE(counter_.IsQuotaAvailable());
35 EXPECT_FALSE(counter_.IsQuotaAvailable());
36 }
37
38 TEST_F(RequestCounterTest, ForcedDoesNotCountInQuota) {
39 EXPECT_TRUE(counter_.IsQuotaAvailable());
40 counter_.ReportForcedRequest();
41 EXPECT_TRUE(counter_.IsQuotaAvailable());
42 }
43
44 TEST_F(RequestCounterTest, QuotaIsPerDay) {
45 EXPECT_TRUE(counter_.IsQuotaAvailable());
46 EXPECT_TRUE(counter_.IsQuotaAvailable());
47
48 // Now fake the counter to believe the count comes from yesterday.
49 base::Time time_yesterday = base::Time::Now() - base::TimeDelta::FromDays(1);
50 int yesterday =
51 base::TimeDelta::FromMilliseconds(time_yesterday.ToJavaTime()).InDays();
52 test_prefs_.SetInteger(base::StringPrintf(
53 ntp_snippets::prefs::kSnippetQuotaDayFormat, kTestId), yesterday);
54
55
56 EXPECT_TRUE(counter_.IsQuotaAvailable());
57 }
58
59 } // namespace ntp_snippets
OLDNEW
« components/ntp_snippets/request_counter.cc ('K') | « components/ntp_snippets/request_counter.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698