OLD | NEW |
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> |
| 8 |
7 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
8 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" |
9 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
10 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
12 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
13 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
14 #include "base/time/time.h" | 17 #include "base/time/time.h" |
15 #include "components/ntp_snippets/ntp_snippet.h" | 18 #include "components/ntp_snippets/ntp_snippet.h" |
16 #include "components/ntp_snippets/ntp_snippets_fetcher.h" | 19 #include "components/ntp_snippets/ntp_snippets_fetcher.h" |
17 #include "components/ntp_snippets/ntp_snippets_scheduler.h" | 20 #include "components/ntp_snippets/ntp_snippets_scheduler.h" |
18 #include "components/prefs/testing_pref_service.h" | 21 #include "components/prefs/testing_pref_service.h" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 json_str[pos + 1] = 'x'; | 91 json_str[pos + 1] = 'x'; |
89 return json_str; | 92 return json_str; |
90 } | 93 } |
91 | 94 |
92 void ParseJson( | 95 void ParseJson( |
93 bool expect_success, | 96 bool expect_success, |
94 const std::string& json, | 97 const std::string& json, |
95 const ntp_snippets::NTPSnippetsService::SuccessCallback& success_callback, | 98 const ntp_snippets::NTPSnippetsService::SuccessCallback& success_callback, |
96 const ntp_snippets::NTPSnippetsService::ErrorCallback& error_callback) { | 99 const ntp_snippets::NTPSnippetsService::ErrorCallback& error_callback) { |
97 base::JSONReader json_reader; | 100 base::JSONReader json_reader; |
98 scoped_ptr<base::Value> value = json_reader.ReadToValue(json); | 101 std::unique_ptr<base::Value> value = json_reader.ReadToValue(json); |
99 bool success = !!value; | 102 bool success = !!value; |
100 EXPECT_EQ(expect_success, success); | 103 EXPECT_EQ(expect_success, success); |
101 if (value) { | 104 if (value) { |
102 success_callback.Run(std::move(value)); | 105 success_callback.Run(std::move(value)); |
103 } else { | 106 } else { |
104 error_callback.Run(json_reader.GetErrorMessage()); | 107 error_callback.Run(json_reader.GetErrorMessage()); |
105 } | 108 } |
106 } | 109 } |
107 | 110 |
108 class MockScheduler : public NTPSnippetsScheduler { | 111 class MockScheduler : public NTPSnippetsScheduler { |
(...skipping 27 matching lines...) Expand all Loading... |
136 void CreateSnippetsServiceEnabled(bool enabled) { | 139 void CreateSnippetsServiceEnabled(bool enabled) { |
137 scheduler_.reset(new MockScheduler); | 140 scheduler_.reset(new MockScheduler); |
138 scoped_refptr<base::SingleThreadTaskRunner> task_runner( | 141 scoped_refptr<base::SingleThreadTaskRunner> task_runner( |
139 base::ThreadTaskRunnerHandle::Get()); | 142 base::ThreadTaskRunnerHandle::Get()); |
140 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = | 143 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = |
141 new net::TestURLRequestContextGetter(task_runner.get()); | 144 new net::TestURLRequestContextGetter(task_runner.get()); |
142 | 145 |
143 service_.reset(new NTPSnippetsService( | 146 service_.reset(new NTPSnippetsService( |
144 pref_service_.get(), nullptr, task_runner, std::string("fr"), | 147 pref_service_.get(), nullptr, task_runner, std::string("fr"), |
145 scheduler_.get(), | 148 scheduler_.get(), |
146 make_scoped_ptr(new NTPSnippetsFetcher( | 149 base::WrapUnique(new NTPSnippetsFetcher( |
147 task_runner, std::move(request_context_getter), true)), | 150 task_runner, std::move(request_context_getter), true)), |
148 base::Bind(&ParseJson, true))); | 151 base::Bind(&ParseJson, true))); |
149 if (enabled) | 152 if (enabled) |
150 EXPECT_CALL(*scheduler_, Schedule(_, _, _, _)); | 153 EXPECT_CALL(*scheduler_, Schedule(_, _, _, _)); |
151 else | 154 else |
152 EXPECT_CALL(*scheduler_, Unschedule()); | 155 EXPECT_CALL(*scheduler_, Unschedule()); |
153 service_->Init(enabled); | 156 service_->Init(enabled); |
154 } | 157 } |
155 | 158 |
156 protected: | 159 protected: |
157 NTPSnippetsService* service() { return service_.get(); } | 160 NTPSnippetsService* service() { return service_.get(); } |
158 | 161 |
159 void LoadFromJSONString(const std::string& json) { | 162 void LoadFromJSONString(const std::string& json) { |
160 service_->OnSnippetsDownloaded(json, std::string()); | 163 service_->OnSnippetsDownloaded(json, std::string()); |
161 } | 164 } |
162 | 165 |
163 void SetExpectJsonParseSuccess(bool expect_success) { | 166 void SetExpectJsonParseSuccess(bool expect_success) { |
164 service_->parse_json_callback_ = base::Bind(&ParseJson, expect_success); | 167 service_->parse_json_callback_ = base::Bind(&ParseJson, expect_success); |
165 } | 168 } |
166 | 169 |
167 private: | 170 private: |
168 base::MessageLoop message_loop_; | 171 base::MessageLoop message_loop_; |
169 scoped_ptr<TestingPrefServiceSimple> pref_service_; | 172 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
170 scoped_ptr<NTPSnippetsService> service_; | 173 std::unique_ptr<NTPSnippetsService> service_; |
171 scoped_ptr<MockScheduler> scheduler_; | 174 std::unique_ptr<MockScheduler> scheduler_; |
172 | 175 |
173 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); | 176 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); |
174 }; | 177 }; |
175 | 178 |
176 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest { | 179 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest { |
177 public: | 180 public: |
178 void CreateSnippetsService() override { | 181 void CreateSnippetsService() override { |
179 CreateSnippetsServiceEnabled(false); | 182 CreateSnippetsServiceEnabled(false); |
180 } | 183 } |
181 }; | 184 }; |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 } | 380 } |
378 | 381 |
379 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { | 382 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { |
380 std::string json_str(GetTestExpiredJson()); | 383 std::string json_str(GetTestExpiredJson()); |
381 | 384 |
382 LoadFromJSONString(json_str); | 385 LoadFromJSONString(json_str); |
383 EXPECT_EQ(service()->size(), 0u); | 386 EXPECT_EQ(service()->size(), 0u); |
384 } | 387 } |
385 | 388 |
386 } // namespace ntp_snippets | 389 } // namespace ntp_snippets |
OLD | NEW |