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

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

Issue 1922083004: Allow fetching personalized snippets from ChromeReader. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase update Created 4 years, 7 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
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 <memory> 7 #include <memory>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
11 #include "base/macros.h" 11 #include "base/macros.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/message_loop/message_loop.h" 13 #include "base/message_loop/message_loop.h"
14 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
17 #include "base/test/histogram_tester.h" 17 #include "base/test/histogram_tester.h"
18 #include "base/thread_task_runner_handle.h" 18 #include "base/thread_task_runner_handle.h"
19 #include "base/time/time.h" 19 #include "base/time/time.h"
20 #include "components/ntp_snippets/ntp_snippet.h" 20 #include "components/ntp_snippets/ntp_snippet.h"
21 #include "components/ntp_snippets/ntp_snippets_fetcher.h" 21 #include "components/ntp_snippets/ntp_snippets_fetcher.h"
22 #include "components/ntp_snippets/ntp_snippets_scheduler.h" 22 #include "components/ntp_snippets/ntp_snippets_scheduler.h"
23 #include "components/prefs/testing_pref_service.h" 23 #include "components/prefs/testing_pref_service.h"
24 #include "components/signin/core/browser/account_tracker_service.h"
25 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
26 #include "components/signin/core/browser/fake_signin_manager.h"
27 #include "components/signin/core/browser/test_signin_client.h"
24 #include "net/url_request/url_request_test_util.h" 28 #include "net/url_request/url_request_test_util.h"
25 #include "testing/gmock/include/gmock/gmock.h" 29 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h" 30 #include "testing/gtest/include/gtest/gtest.h"
27 31
28 using testing::ElementsAre; 32 using testing::ElementsAre;
29 using testing::IsEmpty; 33 using testing::IsEmpty;
30 using testing::_; 34 using testing::_;
31 35
32 namespace ntp_snippets { 36 namespace ntp_snippets {
33 37
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 base::TimeDelta period_fallback, 175 base::TimeDelta period_fallback,
172 base::Time reschedule_time)); 176 base::Time reschedule_time));
173 MOCK_METHOD0(Unschedule, bool()); 177 MOCK_METHOD0(Unschedule, bool());
174 }; 178 };
175 179
176 } // namespace 180 } // namespace
177 181
178 class NTPSnippetsServiceTest : public testing::Test { 182 class NTPSnippetsServiceTest : public testing::Test {
179 public: 183 public:
180 NTPSnippetsServiceTest() 184 NTPSnippetsServiceTest()
181 : pref_service_(new TestingPrefServiceSimple()) {} 185 : pref_service_(new TestingPrefServiceSimple()),
186 signin_client_(new TestSigninClient(nullptr)),
187 account_tracker_(new AccountTrackerService()) {}
182 ~NTPSnippetsServiceTest() override {} 188 ~NTPSnippetsServiceTest() override {}
183 189
184 void SetUp() override { 190 void SetUp() override {
185 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry()); 191 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry());
186 192
187 CreateSnippetsService(); 193 CreateSnippetsService();
188 } 194 }
189 195
190 virtual void CreateSnippetsService() { 196 virtual void CreateSnippetsService() {
191 CreateSnippetsServiceEnabled(true); 197 CreateSnippetsServiceEnabled(true);
192 } 198 }
193 199
194 void CreateSnippetsServiceEnabled(bool enabled) { 200 void CreateSnippetsServiceEnabled(bool enabled) {
195 scheduler_.reset(new MockScheduler); 201 scheduler_.reset(new MockScheduler);
196 scoped_refptr<base::SingleThreadTaskRunner> task_runner( 202 scoped_refptr<base::SingleThreadTaskRunner> task_runner(
197 base::ThreadTaskRunnerHandle::Get()); 203 base::ThreadTaskRunnerHandle::Get());
198 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = 204 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter =
199 new net::TestURLRequestContextGetter(task_runner.get()); 205 new net::TestURLRequestContextGetter(task_runner.get());
206 FakeSigninManagerBase* signin_manager = new FakeSigninManagerBase(
207 signin_client_.get(), account_tracker_.get());
208 FakeProfileOAuth2TokenService* token_service =
209 new FakeProfileOAuth2TokenService();
200 210
201 service_.reset(new NTPSnippetsService( 211 service_.reset(new NTPSnippetsService(
202 pref_service_.get(), nullptr, task_runner, std::string("fr"), 212 pref_service_.get(), nullptr, task_runner, std::string("fr"),
203 scheduler_.get(), 213 scheduler_.get(),
204 base::WrapUnique(new NTPSnippetsFetcher( 214 base::WrapUnique(new NTPSnippetsFetcher(
215 signin_manager, token_service,
205 std::move(request_context_getter), true)), 216 std::move(request_context_getter), true)),
206 base::Bind(&ParseJson, true))); 217 base::Bind(&ParseJson, true)));
207 if (enabled) 218 if (enabled)
208 EXPECT_CALL(*scheduler_, Schedule(_, _, _, _)); 219 EXPECT_CALL(*scheduler_, Schedule(_, _, _, _));
209 else 220 else
210 EXPECT_CALL(*scheduler_, Unschedule()); 221 EXPECT_CALL(*scheduler_, Unschedule());
211 service_->Init(enabled); 222 service_->Init(enabled);
212 } 223 }
213 224
214 protected: 225 protected:
215 NTPSnippetsService* service() { return service_.get(); } 226 NTPSnippetsService* service() { return service_.get(); }
216 227
217 void LoadFromJSONString(const std::string& json) { 228 void LoadFromJSONString(const std::string& json) {
218 service_->OnSnippetsDownloaded(json, std::string()); 229 service_->OnSnippetsDownloaded(json, std::string());
219 } 230 }
220 231
221 void SetExpectJsonParseSuccess(bool expect_success) { 232 void SetExpectJsonParseSuccess(bool expect_success) {
222 service_->parse_json_callback_ = base::Bind(&ParseJson, expect_success); 233 service_->parse_json_callback_ = base::Bind(&ParseJson, expect_success);
223 } 234 }
224 235
225 private: 236 private:
226 base::MessageLoop message_loop_; 237 base::MessageLoop message_loop_;
227 std::unique_ptr<TestingPrefServiceSimple> pref_service_; 238 std::unique_ptr<TestingPrefServiceSimple> pref_service_;
239 std::unique_ptr<TestSigninClient> signin_client_;
240 std::unique_ptr<AccountTrackerService> account_tracker_;
228 std::unique_ptr<NTPSnippetsService> service_; 241 std::unique_ptr<NTPSnippetsService> service_;
229 std::unique_ptr<MockScheduler> scheduler_; 242 std::unique_ptr<MockScheduler> scheduler_;
230 243
231 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); 244 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest);
232 }; 245 };
233 246
234 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest { 247 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest {
235 public: 248 public:
236 void CreateSnippetsService() override { 249 void CreateSnippetsService() override {
237 CreateSnippetsServiceEnabled(false); 250 CreateSnippetsServiceEnabled(false);
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after
749 base::Bucket(/*min=*/1, /*count=*/3))); 762 base::Bucket(/*min=*/1, /*count=*/3)));
750 EXPECT_THAT( 763 EXPECT_THAT(
751 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), 764 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"),
752 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); 765 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1)));
753 // Recreating the service and loading from prefs shouldn't count as fetched 766 // Recreating the service and loading from prefs shouldn't count as fetched
754 // articles. 767 // articles.
755 CreateSnippetsService(); 768 CreateSnippetsService();
756 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4); 769 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4);
757 } 770 }
758 } // namespace ntp_snippets 771 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698