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

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

Issue 1896423002: [NTP Snippets] Add tests for scheduling/unscheduling (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/ntp_snippets/ntp_snippets_service.h" 5 #include "components/ntp_snippets/ntp_snippets_service.h"
6 6
7 #include "base/json/json_reader.h" 7 #include "base/json/json_reader.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "base/thread_task_runner_handle.h" 12 #include "base/thread_task_runner_handle.h"
13 #include "base/time/time.h" 13 #include "base/time/time.h"
14 #include "components/ntp_snippets/ntp_snippet.h" 14 #include "components/ntp_snippets/ntp_snippet.h"
15 #include "components/ntp_snippets/ntp_snippets_fetcher.h" 15 #include "components/ntp_snippets/ntp_snippets_fetcher.h"
16 #include "components/ntp_snippets/ntp_snippets_scheduler.h"
16 #include "components/prefs/testing_pref_service.h" 17 #include "components/prefs/testing_pref_service.h"
17 #include "net/url_request/url_request_test_util.h" 18 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
19 21
22 using testing::_;
23
20 namespace ntp_snippets { 24 namespace ntp_snippets {
21 25
22 namespace { 26 namespace {
23 27
24 const base::Time::Exploded kDefaultCreationTime = {2015, 11, 4, 25, 13, 46, 45}; 28 const base::Time::Exploded kDefaultCreationTime = {2015, 11, 4, 25, 13, 46, 45};
25 29
26 base::Time GetDefaultCreationTime() { 30 base::Time GetDefaultCreationTime() {
27 return base::Time::FromUTCExploded(kDefaultCreationTime); 31 return base::Time::FromUTCExploded(kDefaultCreationTime);
28 } 32 }
29 33
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 scoped_ptr<base::Value> value = json_reader.ReadToValue(json); 97 scoped_ptr<base::Value> value = json_reader.ReadToValue(json);
94 bool success = !!value; 98 bool success = !!value;
95 EXPECT_EQ(expect_success, success); 99 EXPECT_EQ(expect_success, success);
96 if (value) { 100 if (value) {
97 success_callback.Run(std::move(value)); 101 success_callback.Run(std::move(value));
98 } else { 102 } else {
99 error_callback.Run(json_reader.GetErrorMessage()); 103 error_callback.Run(json_reader.GetErrorMessage());
100 } 104 }
101 } 105 }
102 106
107 class MockScheduler : public NTPSnippetsScheduler {
108 public:
109 MockScheduler() {}
Bernhard Bauer 2016/04/20 11:25:57 Nit: I would just remove the default constructor a
Marc Treib 2016/04/20 12:03:04 Done.
110 ~MockScheduler() {}
111
112 MOCK_METHOD4(Schedule,
113 bool(base::TimeDelta period_wifi_charging,
114 base::TimeDelta period_wifi,
115 base::TimeDelta period_fallback,
116 base::Time reschedule_time));
117 MOCK_METHOD0(Unschedule, bool());
118 };
119
103 } // namespace 120 } // namespace
104 121
105 class NTPSnippetsServiceTest : public testing::Test { 122 class NTPSnippetsServiceTest : public testing::Test {
106 public: 123 public:
107 NTPSnippetsServiceTest() 124 NTPSnippetsServiceTest()
108 : pref_service_(new TestingPrefServiceSimple()) {} 125 : pref_service_(new TestingPrefServiceSimple()) {}
109 ~NTPSnippetsServiceTest() override {} 126 ~NTPSnippetsServiceTest() override {}
110 127
111 void SetUp() override { 128 void SetUp() override {
112 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry()); 129 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry());
113 130
114 CreateSnippetsService(); 131 CreateSnippetsService();
115 } 132 }
116 133
117 void CreateSnippetsService() { 134 virtual void CreateSnippetsService() {
135 CreateSnippetsServiceEnabled(true);
136 }
137
138 void CreateSnippetsServiceEnabled(bool enabled) {
139 scheduler_.reset(new MockScheduler);
118 scoped_refptr<base::SingleThreadTaskRunner> task_runner( 140 scoped_refptr<base::SingleThreadTaskRunner> task_runner(
119 base::ThreadTaskRunnerHandle::Get()); 141 base::ThreadTaskRunnerHandle::Get());
120 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = 142 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter =
121 new net::TestURLRequestContextGetter(task_runner.get()); 143 new net::TestURLRequestContextGetter(task_runner.get());
122 144
123 service_.reset(new NTPSnippetsService( 145 service_.reset(new NTPSnippetsService(
124 pref_service_.get(), nullptr, task_runner, std::string("fr"), nullptr, 146 pref_service_.get(), nullptr, task_runner, std::string("fr"),
147 scheduler_.get(),
125 make_scoped_ptr(new NTPSnippetsFetcher( 148 make_scoped_ptr(new NTPSnippetsFetcher(
126 task_runner, std::move(request_context_getter), true)), 149 task_runner, std::move(request_context_getter), true)),
127 base::Bind(&ParseJson, true))); 150 base::Bind(&ParseJson, true)));
128 service_->Init(true); 151 if (enabled)
152 EXPECT_CALL(*scheduler_, Schedule(_, _, _, _));
153 else
154 EXPECT_CALL(*scheduler_, Unschedule());
155 service_->Init(enabled);
129 } 156 }
130 157
131 protected: 158 protected:
132 NTPSnippetsService* service() { 159 NTPSnippetsService* service() { return service_.get(); }
133 return service_.get();
134 }
135 160
136 void LoadFromJSONString(const std::string& json) { 161 void LoadFromJSONString(const std::string& json) {
137 service_->OnSnippetsDownloaded(json); 162 service_->OnSnippetsDownloaded(json);
138 } 163 }
139 164
140 void SetExpectJsonParseSuccess(bool expect_success) { 165 void SetExpectJsonParseSuccess(bool expect_success) {
141 service_->parse_json_callback_ = base::Bind(&ParseJson, expect_success); 166 service_->parse_json_callback_ = base::Bind(&ParseJson, expect_success);
142 } 167 }
143 168
144 private: 169 private:
145 base::MessageLoop message_loop_; 170 base::MessageLoop message_loop_;
146 scoped_ptr<TestingPrefServiceSimple> pref_service_; 171 scoped_ptr<TestingPrefServiceSimple> pref_service_;
147 scoped_ptr<NTPSnippetsService> service_; 172 scoped_ptr<NTPSnippetsService> service_;
173 scoped_ptr<MockScheduler> scheduler_;
148 174
149 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); 175 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest);
150 }; 176 };
151 177
178 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest {
179 public:
180 void CreateSnippetsService() override {
181 CreateSnippetsServiceEnabled(false);
182 }
183 };
184
185 TEST_F(NTPSnippetsServiceTest, Schedule) {
186 // CreateSnippetsServiceEnabled checks that Schedule is called.
Bernhard Bauer 2016/04/20 11:25:57 This is a bit strange, but I guess it works...
Marc Treib 2016/04/20 12:03:04 Well, suggestions for improvement welcome :)
187 }
188
189 TEST_F(NTPSnippetsServiceDisabledTest, Unschedule) {
190 // CreateSnippetsServiceEnabled checks that Unschedule is called.
191 }
192
152 TEST_F(NTPSnippetsServiceTest, Loop) { 193 TEST_F(NTPSnippetsServiceTest, Loop) {
153 std::string json_str( 194 std::string json_str(
154 "{ \"recos\": [ " 195 "{ \"recos\": [ "
155 "{ \"contentInfo\": { \"url\" : \"http://localhost/foobar\" }}" 196 "{ \"contentInfo\": { \"url\" : \"http://localhost/foobar\" }}"
156 "]}"); 197 "]}");
157 LoadFromJSONString(json_str); 198 LoadFromJSONString(json_str);
158 199
159 // The same for loop without the '&' should not compile. 200 // The same for loop without the '&' should not compile.
160 for (auto& snippet : *service()) { 201 for (auto& snippet : *service()) {
161 // Snippet here is a const. 202 // Snippet here is a const.
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
295 } 336 }
296 337
297 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { 338 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) {
298 std::string json_str(GetTestExpiredJson()); 339 std::string json_str(GetTestExpiredJson());
299 340
300 LoadFromJSONString(json_str); 341 LoadFromJSONString(json_str);
301 EXPECT_EQ(service()->size(), 0u); 342 EXPECT_EQ(service()->size(), 0u);
302 } 343 }
303 344
304 } // namespace ntp_snippets 345 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698