| 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 "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 Loading... |
| 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 MOCK_METHOD4(Schedule, |
| 110 bool(base::TimeDelta period_wifi_charging, |
| 111 base::TimeDelta period_wifi, |
| 112 base::TimeDelta period_fallback, |
| 113 base::Time reschedule_time)); |
| 114 MOCK_METHOD0(Unschedule, bool()); |
| 115 }; |
| 116 |
| 103 } // namespace | 117 } // namespace |
| 104 | 118 |
| 105 class NTPSnippetsServiceTest : public testing::Test { | 119 class NTPSnippetsServiceTest : public testing::Test { |
| 106 public: | 120 public: |
| 107 NTPSnippetsServiceTest() | 121 NTPSnippetsServiceTest() |
| 108 : pref_service_(new TestingPrefServiceSimple()) {} | 122 : pref_service_(new TestingPrefServiceSimple()) {} |
| 109 ~NTPSnippetsServiceTest() override {} | 123 ~NTPSnippetsServiceTest() override {} |
| 110 | 124 |
| 111 void SetUp() override { | 125 void SetUp() override { |
| 112 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry()); | 126 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry()); |
| 113 | 127 |
| 114 CreateSnippetsService(); | 128 CreateSnippetsService(); |
| 115 } | 129 } |
| 116 | 130 |
| 117 void CreateSnippetsService() { | 131 virtual void CreateSnippetsService() { |
| 132 CreateSnippetsServiceEnabled(true); |
| 133 } |
| 134 |
| 135 void CreateSnippetsServiceEnabled(bool enabled) { |
| 136 scheduler_.reset(new MockScheduler); |
| 118 scoped_refptr<base::SingleThreadTaskRunner> task_runner( | 137 scoped_refptr<base::SingleThreadTaskRunner> task_runner( |
| 119 base::ThreadTaskRunnerHandle::Get()); | 138 base::ThreadTaskRunnerHandle::Get()); |
| 120 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = | 139 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = |
| 121 new net::TestURLRequestContextGetter(task_runner.get()); | 140 new net::TestURLRequestContextGetter(task_runner.get()); |
| 122 | 141 |
| 123 service_.reset(new NTPSnippetsService( | 142 service_.reset(new NTPSnippetsService( |
| 124 pref_service_.get(), nullptr, task_runner, std::string("fr"), nullptr, | 143 pref_service_.get(), nullptr, task_runner, std::string("fr"), |
| 144 scheduler_.get(), |
| 125 make_scoped_ptr(new NTPSnippetsFetcher( | 145 make_scoped_ptr(new NTPSnippetsFetcher( |
| 126 task_runner, std::move(request_context_getter), true)), | 146 task_runner, std::move(request_context_getter), true)), |
| 127 base::Bind(&ParseJson, true))); | 147 base::Bind(&ParseJson, true))); |
| 128 service_->Init(true); | 148 if (enabled) |
| 149 EXPECT_CALL(*scheduler_, Schedule(_, _, _, _)); |
| 150 else |
| 151 EXPECT_CALL(*scheduler_, Unschedule()); |
| 152 service_->Init(enabled); |
| 129 } | 153 } |
| 130 | 154 |
| 131 protected: | 155 protected: |
| 132 NTPSnippetsService* service() { | 156 NTPSnippetsService* service() { return service_.get(); } |
| 133 return service_.get(); | |
| 134 } | |
| 135 | 157 |
| 136 void LoadFromJSONString(const std::string& json) { | 158 void LoadFromJSONString(const std::string& json) { |
| 137 service_->OnSnippetsDownloaded(json); | 159 service_->OnSnippetsDownloaded(json); |
| 138 } | 160 } |
| 139 | 161 |
| 140 void SetExpectJsonParseSuccess(bool expect_success) { | 162 void SetExpectJsonParseSuccess(bool expect_success) { |
| 141 service_->parse_json_callback_ = base::Bind(&ParseJson, expect_success); | 163 service_->parse_json_callback_ = base::Bind(&ParseJson, expect_success); |
| 142 } | 164 } |
| 143 | 165 |
| 144 private: | 166 private: |
| 145 base::MessageLoop message_loop_; | 167 base::MessageLoop message_loop_; |
| 146 scoped_ptr<TestingPrefServiceSimple> pref_service_; | 168 scoped_ptr<TestingPrefServiceSimple> pref_service_; |
| 147 scoped_ptr<NTPSnippetsService> service_; | 169 scoped_ptr<NTPSnippetsService> service_; |
| 170 scoped_ptr<MockScheduler> scheduler_; |
| 148 | 171 |
| 149 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); | 172 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); |
| 150 }; | 173 }; |
| 151 | 174 |
| 175 class NTPSnippetsServiceDisabledTest : public NTPSnippetsServiceTest { |
| 176 public: |
| 177 void CreateSnippetsService() override { |
| 178 CreateSnippetsServiceEnabled(false); |
| 179 } |
| 180 }; |
| 181 |
| 182 TEST_F(NTPSnippetsServiceTest, Schedule) { |
| 183 // CreateSnippetsServiceEnabled checks that Schedule is called. |
| 184 } |
| 185 |
| 186 TEST_F(NTPSnippetsServiceDisabledTest, Unschedule) { |
| 187 // CreateSnippetsServiceEnabled checks that Unschedule is called. |
| 188 } |
| 189 |
| 152 TEST_F(NTPSnippetsServiceTest, Loop) { | 190 TEST_F(NTPSnippetsServiceTest, Loop) { |
| 153 std::string json_str( | 191 std::string json_str( |
| 154 "{ \"recos\": [ " | 192 "{ \"recos\": [ " |
| 155 "{ \"contentInfo\": { \"url\" : \"http://localhost/foobar\" }}" | 193 "{ \"contentInfo\": { \"url\" : \"http://localhost/foobar\" }}" |
| 156 "]}"); | 194 "]}"); |
| 157 LoadFromJSONString(json_str); | 195 LoadFromJSONString(json_str); |
| 158 | 196 |
| 159 // The same for loop without the '&' should not compile. | 197 // The same for loop without the '&' should not compile. |
| 160 for (auto& snippet : *service()) { | 198 for (auto& snippet : *service()) { |
| 161 // Snippet here is a const. | 199 // Snippet here is a const. |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 } | 333 } |
| 296 | 334 |
| 297 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { | 335 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { |
| 298 std::string json_str(GetTestExpiredJson()); | 336 std::string json_str(GetTestExpiredJson()); |
| 299 | 337 |
| 300 LoadFromJSONString(json_str); | 338 LoadFromJSONString(json_str); |
| 301 EXPECT_EQ(service()->size(), 0u); | 339 EXPECT_EQ(service()->size(), 0u); |
| 302 } | 340 } |
| 303 | 341 |
| 304 } // namespace ntp_snippets | 342 } // namespace ntp_snippets |
| OLD | NEW |