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

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 and minor polish 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 8
9 #include "base/json/json_reader.h" 9 #include "base/json/json_reader.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
16 #include "base/test/histogram_tester.h" 16 #include "base/test/histogram_tester.h"
17 #include "base/thread_task_runner_handle.h" 17 #include "base/thread_task_runner_handle.h"
18 #include "base/time/time.h" 18 #include "base/time/time.h"
19 #include "components/ntp_snippets/ntp_snippet.h" 19 #include "components/ntp_snippets/ntp_snippet.h"
20 #include "components/ntp_snippets/ntp_snippets_fetcher.h" 20 #include "components/ntp_snippets/ntp_snippets_fetcher.h"
21 #include "components/ntp_snippets/ntp_snippets_scheduler.h" 21 #include "components/ntp_snippets/ntp_snippets_scheduler.h"
22 #include "components/prefs/testing_pref_service.h" 22 #include "components/prefs/testing_pref_service.h"
23 #include "components/signin/core/browser/account_tracker_service.h"
24 #include "components/signin/core/browser/fake_profile_oauth2_token_service.h"
25 #include "components/signin/core/browser/fake_signin_manager.h"
26 #include "components/signin/core/browser/test_signin_client.h"
23 #include "net/url_request/url_request_test_util.h" 27 #include "net/url_request/url_request_test_util.h"
24 #include "testing/gmock/include/gmock/gmock.h" 28 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
26 30
27 using testing::ElementsAre; 31 using testing::ElementsAre;
28 using testing::IsEmpty; 32 using testing::IsEmpty;
29 using testing::_; 33 using testing::_;
30 34
31 namespace ntp_snippets { 35 namespace ntp_snippets {
32 36
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 base::TimeDelta period_fallback, 123 base::TimeDelta period_fallback,
120 base::Time reschedule_time)); 124 base::Time reschedule_time));
121 MOCK_METHOD0(Unschedule, bool()); 125 MOCK_METHOD0(Unschedule, bool());
122 }; 126 };
123 127
124 } // namespace 128 } // namespace
125 129
126 class NTPSnippetsServiceTest : public testing::Test { 130 class NTPSnippetsServiceTest : public testing::Test {
127 public: 131 public:
128 NTPSnippetsServiceTest() 132 NTPSnippetsServiceTest()
129 : pref_service_(new TestingPrefServiceSimple()) {} 133 : pref_service_(new TestingPrefServiceSimple()),
134 signin_client_(new TestSigninClient(nullptr)),
135 account_tracker_(new AccountTrackerService()) {}
130 ~NTPSnippetsServiceTest() override {} 136 ~NTPSnippetsServiceTest() override {}
131 137
132 void SetUp() override { 138 void SetUp() override {
133 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry()); 139 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry());
134 140
135 CreateSnippetsService(); 141 CreateSnippetsService();
136 } 142 }
137 143
138 virtual void CreateSnippetsService() { 144 virtual void CreateSnippetsService() {
139 CreateSnippetsServiceEnabled(true); 145 CreateSnippetsServiceEnabled(true);
140 } 146 }
141 147
142 void CreateSnippetsServiceEnabled(bool enabled) { 148 void CreateSnippetsServiceEnabled(bool enabled) {
143 scheduler_.reset(new MockScheduler); 149 scheduler_.reset(new MockScheduler);
144 scoped_refptr<base::SingleThreadTaskRunner> task_runner( 150 scoped_refptr<base::SingleThreadTaskRunner> task_runner(
145 base::ThreadTaskRunnerHandle::Get()); 151 base::ThreadTaskRunnerHandle::Get());
146 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = 152 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter =
147 new net::TestURLRequestContextGetter(task_runner.get()); 153 new net::TestURLRequestContextGetter(task_runner.get());
154 FakeSigninManagerBase* signin_manager = new FakeSigninManagerBase(
Bernhard Bauer 2016/05/04 11:46:58 Nit: superfluous space before `new`.
jkrcal 2016/05/04 14:04:09 Done.
155 signin_client_.get(), account_tracker_.get());
156 FakeProfileOAuth2TokenService* token_service =
157 new FakeProfileOAuth2TokenService();
148 158
149 service_.reset(new NTPSnippetsService( 159 service_.reset(new NTPSnippetsService(
150 pref_service_.get(), nullptr, task_runner, std::string("fr"), 160 pref_service_.get(), nullptr, task_runner, std::string("fr"),
151 scheduler_.get(), 161 scheduler_.get(),
152 base::WrapUnique(new NTPSnippetsFetcher( 162 base::WrapUnique(new NTPSnippetsFetcher(
163 signin_manager, token_service,
153 std::move(request_context_getter), true)), 164 std::move(request_context_getter), true)),
154 base::Bind(&ParseJson, true))); 165 base::Bind(&ParseJson, true)));
155 if (enabled) 166 if (enabled)
156 EXPECT_CALL(*scheduler_, Schedule(_, _, _, _)); 167 EXPECT_CALL(*scheduler_, Schedule(_, _, _, _));
157 else 168 else
158 EXPECT_CALL(*scheduler_, Unschedule()); 169 EXPECT_CALL(*scheduler_, Unschedule());
159 service_->Init(enabled); 170 service_->Init(enabled);
160 } 171 }
161 172
162 protected: 173 protected:
163 NTPSnippetsService* service() { return service_.get(); } 174 NTPSnippetsService* service() { return service_.get(); }
164 175
165 void LoadFromJSONString(const std::string& json) { 176 void LoadFromJSONString(const std::string& json) {
166 service_->OnSnippetsDownloaded(json, std::string()); 177 service_->OnSnippetsDownloaded(json, std::string());
167 } 178 }
168 179
169 void SetExpectJsonParseSuccess(bool expect_success) { 180 void SetExpectJsonParseSuccess(bool expect_success) {
170 service_->parse_json_callback_ = base::Bind(&ParseJson, expect_success); 181 service_->parse_json_callback_ = base::Bind(&ParseJson, expect_success);
171 } 182 }
172 183
173 private: 184 private:
174 base::MessageLoop message_loop_; 185 base::MessageLoop message_loop_;
175 std::unique_ptr<TestingPrefServiceSimple> pref_service_; 186 std::unique_ptr<TestingPrefServiceSimple> pref_service_;
187 std::unique_ptr<TestSigninClient> signin_client_;
188 std::unique_ptr<AccountTrackerService> account_tracker_;
176 std::unique_ptr<NTPSnippetsService> service_; 189 std::unique_ptr<NTPSnippetsService> service_;
177 std::unique_ptr<MockScheduler> scheduler_; 190 std::unique_ptr<MockScheduler> scheduler_;
178 191
179 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); 192 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest);
180 }; 193 };
181 194
182 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest { 195 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest {
183 public: 196 public:
184 void CreateSnippetsService() override { 197 void CreateSnippetsService() override {
185 CreateSnippetsServiceEnabled(false); 198 CreateSnippetsServiceEnabled(false);
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 EXPECT_THAT( 451 EXPECT_THAT(
439 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), 452 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"),
440 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); 453 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1)));
441 // Recreating the service and loading from prefs shouldn't count as fetched 454 // Recreating the service and loading from prefs shouldn't count as fetched
442 // articles. 455 // articles.
443 CreateSnippetsService(); 456 CreateSnippetsService();
444 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4); 457 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4);
445 } 458 }
446 459
447 } // namespace ntp_snippets 460 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698