| 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 "base/json/json_reader.h" | 5 #include "base/json/json_reader.h" |
| 6 #include "base/macros.h" | 6 #include "base/macros.h" |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/thread_task_runner_handle.h" | 10 #include "base/thread_task_runner_handle.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 const ntp_snippets::NTPSnippetsService::ErrorCallback& error_callback) { | 72 const ntp_snippets::NTPSnippetsService::ErrorCallback& error_callback) { |
| 73 base::JSONReader json_reader; | 73 base::JSONReader json_reader; |
| 74 scoped_ptr<base::Value> value = json_reader.ReadToValue(json); | 74 scoped_ptr<base::Value> value = json_reader.ReadToValue(json); |
| 75 if (value) { | 75 if (value) { |
| 76 success_callback.Run(std::move(value)); | 76 success_callback.Run(std::move(value)); |
| 77 } else { | 77 } else { |
| 78 error_callback.Run(json_reader.GetErrorMessage()); | 78 error_callback.Run(json_reader.GetErrorMessage()); |
| 79 } | 79 } |
| 80 } | 80 } |
| 81 | 81 |
| 82 class SnippetObserver : public NTPSnippetsServiceObserver { | |
| 83 public: | |
| 84 SnippetObserver() : loaded_(false), shutdown_(false) {} | |
| 85 ~SnippetObserver() override {} | |
| 86 | |
| 87 void NTPSnippetsServiceLoaded(NTPSnippetsService* service) override { | |
| 88 loaded_ = true; | |
| 89 } | |
| 90 | |
| 91 void NTPSnippetsServiceShutdown(NTPSnippetsService* service) override { | |
| 92 shutdown_ = true; | |
| 93 loaded_ = false; | |
| 94 } | |
| 95 | |
| 96 bool loaded_; | |
| 97 bool shutdown_; | |
| 98 | |
| 99 private: | |
| 100 DISALLOW_COPY_AND_ASSIGN(SnippetObserver); | |
| 101 }; | |
| 102 | |
| 103 } // namespace | 82 } // namespace |
| 104 | 83 |
| 105 class NTPSnippetsServiceTest : public testing::Test { | 84 class NTPSnippetsServiceTest : public testing::Test { |
| 106 public: | 85 public: |
| 107 NTPSnippetsServiceTest() | 86 NTPSnippetsServiceTest() |
| 108 : pref_service_(new TestingPrefServiceSimple()) {} | 87 : pref_service_(new TestingPrefServiceSimple()) {} |
| 109 ~NTPSnippetsServiceTest() override {} | 88 ~NTPSnippetsServiceTest() override {} |
| 110 | 89 |
| 111 void SetUp() override { | 90 void SetUp() override { |
| 112 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry()); | 91 NTPSnippetsService::RegisterProfilePrefs(pref_service_->registry()); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 141 } | 120 } |
| 142 | 121 |
| 143 private: | 122 private: |
| 144 base::MessageLoop message_loop_; | 123 base::MessageLoop message_loop_; |
| 145 scoped_ptr<TestingPrefServiceSimple> pref_service_; | 124 scoped_ptr<TestingPrefServiceSimple> pref_service_; |
| 146 scoped_ptr<NTPSnippetsService> service_; | 125 scoped_ptr<NTPSnippetsService> service_; |
| 147 | 126 |
| 148 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); | 127 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); |
| 149 }; | 128 }; |
| 150 | 129 |
| 151 TEST_F(NTPSnippetsServiceTest, Create) { | |
| 152 EXPECT_FALSE(service()->is_loaded()); | |
| 153 } | |
| 154 | |
| 155 TEST_F(NTPSnippetsServiceTest, Loop) { | 130 TEST_F(NTPSnippetsServiceTest, Loop) { |
| 156 EXPECT_FALSE(service()->is_loaded()); | |
| 157 | |
| 158 std::string json_str( | 131 std::string json_str( |
| 159 "{ \"recos\": [ " | 132 "{ \"recos\": [ " |
| 160 "{ \"contentInfo\": { \"url\" : \"http://localhost/foobar\" }}" | 133 "{ \"contentInfo\": { \"url\" : \"http://localhost/foobar\" }}" |
| 161 "]}"); | 134 "]}"); |
| 162 ASSERT_TRUE(LoadFromJSONString(json_str)); | 135 ASSERT_TRUE(LoadFromJSONString(json_str)); |
| 163 | 136 |
| 164 EXPECT_TRUE(service()->is_loaded()); | |
| 165 | |
| 166 // The same for loop without the '&' should not compile. | 137 // The same for loop without the '&' should not compile. |
| 167 for (auto& snippet : *service()) { | 138 for (auto& snippet : *service()) { |
| 168 // Snippet here is a const. | 139 // Snippet here is a const. |
| 169 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); | 140 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); |
| 170 } | 141 } |
| 171 // Without the const, this should not compile. | 142 // Without the const, this should not compile. |
| 172 for (const NTPSnippet& snippet : *service()) { | 143 for (const NTPSnippet& snippet : *service()) { |
| 173 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); | 144 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); |
| 174 } | 145 } |
| 175 } | 146 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 193 base::Time then = | 164 base::Time then = |
| 194 base::Time::FromUTCExploded({2015, 11, 4, 25, 13, 46, 45}); | 165 base::Time::FromUTCExploded({2015, 11, 4, 25, 13, 46, 45}); |
| 195 EXPECT_EQ(then, snippet.publish_date()); | 166 EXPECT_EQ(then, snippet.publish_date()); |
| 196 } | 167 } |
| 197 } | 168 } |
| 198 | 169 |
| 199 TEST_F(NTPSnippetsServiceTest, Discard) { | 170 TEST_F(NTPSnippetsServiceTest, Discard) { |
| 200 std::string json_str( | 171 std::string json_str( |
| 201 "{ \"recos\": [ { \"contentInfo\": { \"url\" : \"http://site.com\" }}]}"); | 172 "{ \"recos\": [ { \"contentInfo\": { \"url\" : \"http://site.com\" }}]}"); |
| 202 ASSERT_TRUE(LoadFromJSONString(json_str)); | 173 ASSERT_TRUE(LoadFromJSONString(json_str)); |
| 203 ASSERT_TRUE(service()->is_loaded()); | |
| 204 | 174 |
| 205 ASSERT_EQ(1u, service()->size()); | 175 ASSERT_EQ(1u, service()->size()); |
| 206 | 176 |
| 207 // Discarding a non-existent snippet shouldn't do anything. | 177 // Discarding a non-existent snippet shouldn't do anything. |
| 208 EXPECT_FALSE(service()->DiscardSnippet(GURL("http://othersite.com"))); | 178 EXPECT_FALSE(service()->DiscardSnippet(GURL("http://othersite.com"))); |
| 209 EXPECT_EQ(1u, service()->size()); | 179 EXPECT_EQ(1u, service()->size()); |
| 210 | 180 |
| 211 // Discard the snippet. | 181 // Discard the snippet. |
| 212 EXPECT_TRUE(service()->DiscardSnippet(GURL("http://site.com"))); | 182 EXPECT_TRUE(service()->DiscardSnippet(GURL("http://site.com"))); |
| 213 EXPECT_EQ(0u, service()->size()); | 183 EXPECT_EQ(0u, service()->size()); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 241 } | 211 } |
| 242 } | 212 } |
| 243 | 213 |
| 244 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { | 214 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { |
| 245 std::string json_str(GetTestExpiredJson(1448459205)); | 215 std::string json_str(GetTestExpiredJson(1448459205)); |
| 246 | 216 |
| 247 ASSERT_TRUE(LoadFromJSONString(json_str)); | 217 ASSERT_TRUE(LoadFromJSONString(json_str)); |
| 248 EXPECT_EQ(service()->size(), 0u); | 218 EXPECT_EQ(service()->size(), 0u); |
| 249 } | 219 } |
| 250 | 220 |
| 251 TEST_F(NTPSnippetsServiceTest, ObserverNotLoaded) { | |
| 252 SnippetObserver observer; | |
| 253 ScopedObserver<NTPSnippetsService, SnippetObserver> scoped_observer( | |
| 254 &observer); | |
| 255 scoped_observer.Add(service()); | |
| 256 EXPECT_FALSE(observer.loaded_); | |
| 257 | |
| 258 std::string json_str( | |
| 259 "{ \"recos\": [ " | |
| 260 "{ \"contentInfo\": { \"url\" : \"http://localhost/foobar\" }}" | |
| 261 "]}"); | |
| 262 ASSERT_TRUE(LoadFromJSONString(json_str)); | |
| 263 EXPECT_TRUE(observer.loaded_); | |
| 264 } | |
| 265 | |
| 266 TEST_F(NTPSnippetsServiceTest, ObserverLoaded) { | |
| 267 std::string json_str( | |
| 268 "{ \"recos\": [ " | |
| 269 "{ \"contentInfo\": { \"url\" : \"http://localhost/foobar\" }}" | |
| 270 "]}"); | |
| 271 ASSERT_TRUE(LoadFromJSONString(json_str)); | |
| 272 | |
| 273 SnippetObserver observer; | |
| 274 ScopedObserver<NTPSnippetsService, SnippetObserver> scoped_observer( | |
| 275 &observer); | |
| 276 scoped_observer.Add(service()); | |
| 277 | |
| 278 EXPECT_TRUE(observer.loaded_); | |
| 279 } | |
| 280 | |
| 281 } // namespace ntp_snippets | 221 } // namespace ntp_snippets |
| OLD | NEW |