Chromium Code Reviews| 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> | 7 #include <memory> |
| 8 #include <vector> | |
| 8 | 9 |
| 9 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 10 #include "base/macros.h" | 11 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 12 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 13 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 15 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 16 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
| 17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 34 base::Time GetDefaultCreationTime() { | 35 base::Time GetDefaultCreationTime() { |
| 35 return base::Time::FromUTCExploded(kDefaultCreationTime); | 36 return base::Time::FromUTCExploded(kDefaultCreationTime); |
| 36 } | 37 } |
| 37 | 38 |
| 38 std::string GetTestJson(const std::string& content_creation_time_str, | 39 std::string GetTestJson(const std::string& content_creation_time_str, |
| 39 const std::string& expiry_time_str) { | 40 const std::string& expiry_time_str) { |
| 40 char json_str_format[] = | 41 char json_str_format[] = |
| 41 "{ \"recos\": [ " | 42 "{ \"recos\": [ " |
| 42 "{ \"contentInfo\": {" | 43 "{ \"contentInfo\": {" |
| 43 "\"url\" : \"http://localhost/foobar\"," | 44 "\"url\" : \"http://localhost/foobar\"," |
| 44 "\"site_title\" : \"Site Title\"," | |
| 45 "\"favicon_url\" : \"http://localhost/favicon\"," | |
| 46 "\"title\" : \"Title\"," | 45 "\"title\" : \"Title\"," |
| 47 "\"snippet\" : \"Snippet\"," | 46 "\"snippet\" : \"Snippet\"," |
| 48 "\"thumbnailUrl\" : \"http://localhost/salient_image\"," | 47 "\"thumbnailUrl\" : \"http://localhost/salient_image\"," |
| 49 "\"creationTimestampSec\" : \"%s\"," | 48 "\"creationTimestampSec\" : \"%s\"," |
| 50 "\"expiryTimestampSec\" : \"%s\"," | 49 "\"expiryTimestampSec\" : \"%s\"," |
| 51 "\"sourceCorpusInfo\" : [ " | 50 "\"sourceCorpusInfo\" : [ " |
| 52 "{\"ampUrl\" : \"http://localhost/amp\"}," | 51 "{\"ampUrl\" : \"http://localhost/amp\"," |
| 53 "{\"corpusId\" : \"id\"}]" | 52 "\"corpusId\" : \"http://localhost/foobar\"," |
| 53 "\"publisherData\": { \"sourceName\" : \"Foo News\"}}]" | |
| 54 "}}" | 54 "}}" |
| 55 "]}"; | 55 "]}"; |
| 56 | 56 |
| 57 return base::StringPrintf(json_str_format, content_creation_time_str.c_str(), | 57 return base::StringPrintf(json_str_format, content_creation_time_str.c_str(), |
| 58 expiry_time_str.c_str()); | 58 expiry_time_str.c_str()); |
| 59 } | 59 } |
| 60 | 60 |
| 61 std::string GetTestJsonWithSources(const std::string& content_creation_time_str, | |
| 62 const std::string& expiry_time_str, | |
| 63 const std::vector<std::string>& source_urls, | |
| 64 const std::vector<std::string>& publishers, | |
| 65 const std::vector<std::string>& amp_urls) { | |
| 66 char json_str_format[] = | |
| 67 "{ \"recos\": [ " | |
| 68 "{ \"contentInfo\": {" | |
| 69 "\"url\" : \"http://localhost/foobar\"," | |
| 70 "\"title\" : \"Title\"," | |
| 71 "\"snippet\" : \"Snippet\"," | |
| 72 "\"thumbnailUrl\" : \"http://localhost/salient_image\"," | |
| 73 "\"creationTimestampSec\" : \"%s\"," | |
| 74 "\"expiryTimestampSec\" : \"%s\"," | |
| 75 "\"sourceCorpusInfo\" : [%s]" | |
| 76 "}}" | |
| 77 "]}"; | |
| 78 | |
| 79 char source_corpus_info_format[] = | |
| 80 "{\"corpusId\": \"%s\"," | |
| 81 "\"publisherData\": {" | |
| 82 "\"sourceName\": \"%s\"" | |
| 83 "}," | |
| 84 "\"ampUrl\": \"%s\"}"; | |
| 85 | |
| 86 std::string source_corpus_info_list_str; | |
| 87 for (size_t i = 0; i < source_urls.size(); ++i) { | |
| 88 std::string source_corpus_info_str = | |
| 89 base::StringPrintf(source_corpus_info_format, | |
| 90 source_urls[i].empty() ? "" : source_urls[i].c_str(), | |
| 91 publishers[i].empty() ? "" : publishers[i].c_str(), | |
| 92 amp_urls[i].empty() ? "" : amp_urls[i].c_str()); | |
| 93 source_corpus_info_list_str.append(source_corpus_info_str); | |
| 94 source_corpus_info_list_str.append(","); | |
| 95 } | |
| 96 // Remove the last comma | |
| 97 source_corpus_info_list_str.pop_back(); | |
| 98 return base::StringPrintf(json_str_format, content_creation_time_str.c_str(), | |
| 99 expiry_time_str.c_str(), | |
| 100 source_corpus_info_list_str.c_str()); | |
| 101 } | |
| 102 | |
| 103 std::string GetTestJsonWithSources(const std::vector<std::string>& source_urls, | |
| 104 const std::vector<std::string>& publishers, | |
| 105 const std::vector<std::string>& amp_urls) { | |
| 106 base::Time expiry_time = base::Time::Now() + base::TimeDelta::FromHours(1); | |
| 107 return GetTestJsonWithSources( | |
| 108 NTPSnippet::TimeToJsonString(GetDefaultCreationTime()), | |
| 109 NTPSnippet::TimeToJsonString(expiry_time), source_urls, publishers, | |
| 110 amp_urls); | |
| 111 } | |
| 112 | |
| 61 std::string GetTestJson(const std::string& content_creation_time_str) { | 113 std::string GetTestJson(const std::string& content_creation_time_str) { |
| 62 base::Time expiry_time = base::Time::Now() + base::TimeDelta::FromHours(1); | 114 base::Time expiry_time = base::Time::Now() + base::TimeDelta::FromHours(1); |
| 63 return GetTestJson(content_creation_time_str, | 115 return GetTestJson(content_creation_time_str, |
| 64 NTPSnippet::TimeToJsonString(expiry_time)); | 116 NTPSnippet::TimeToJsonString(expiry_time)); |
| 65 } | 117 } |
| 66 | 118 |
| 67 std::string GetTestJson() { | 119 std::string GetTestJson() { |
| 68 return GetTestJson(NTPSnippet::TimeToJsonString(GetDefaultCreationTime())); | 120 return GetTestJson(NTPSnippet::TimeToJsonString(GetDefaultCreationTime())); |
| 69 } | 121 } |
| 70 | 122 |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 212 TEST_F(NTPSnippetsServiceTest, Full) { | 264 TEST_F(NTPSnippetsServiceTest, Full) { |
| 213 std::string json_str(GetTestJson()); | 265 std::string json_str(GetTestJson()); |
| 214 | 266 |
| 215 LoadFromJSONString(json_str); | 267 LoadFromJSONString(json_str); |
| 216 EXPECT_EQ(service()->size(), 1u); | 268 EXPECT_EQ(service()->size(), 1u); |
| 217 | 269 |
| 218 // The same for loop without the '&' should not compile. | 270 // The same for loop without the '&' should not compile. |
| 219 for (auto& snippet : *service()) { | 271 for (auto& snippet : *service()) { |
| 220 // Snippet here is a const. | 272 // Snippet here is a const. |
| 221 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); | 273 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); |
| 222 EXPECT_EQ(snippet.site_title(), "Site Title"); | 274 EXPECT_EQ(snippet.site_title(), "Foo News"); |
| 223 EXPECT_EQ(snippet.favicon_url(), GURL("http://localhost/favicon")); | |
| 224 EXPECT_EQ(snippet.title(), "Title"); | 275 EXPECT_EQ(snippet.title(), "Title"); |
| 225 EXPECT_EQ(snippet.snippet(), "Snippet"); | 276 EXPECT_EQ(snippet.snippet(), "Snippet"); |
| 226 EXPECT_EQ(snippet.salient_image_url(), | 277 EXPECT_EQ(snippet.salient_image_url(), |
| 227 GURL("http://localhost/salient_image")); | 278 GURL("http://localhost/salient_image")); |
| 228 EXPECT_EQ(GetDefaultCreationTime(), snippet.publish_date()); | 279 EXPECT_EQ(GetDefaultCreationTime(), snippet.publish_date()); |
| 229 EXPECT_EQ(snippet.amp_url(), GURL("http://localhost/amp")); | 280 EXPECT_EQ(snippet.amp_url().spec(), GURL("http://localhost/amp").spec()); |
| 230 } | 281 } |
| 231 } | 282 } |
| 232 | 283 |
| 233 TEST_F(NTPSnippetsServiceTest, Clear) { | 284 TEST_F(NTPSnippetsServiceTest, Clear) { |
| 234 std::string json_str(GetTestJson()); | 285 std::string json_str(GetTestJson()); |
| 235 | 286 |
| 236 LoadFromJSONString(json_str); | 287 LoadFromJSONString(json_str); |
| 237 EXPECT_EQ(service()->size(), 1u); | 288 EXPECT_EQ(service()->size(), 1u); |
| 238 | 289 |
| 239 service()->ClearSnippets(); | 290 service()->ClearSnippets(); |
| 240 EXPECT_EQ(service()->size(), 0u); | 291 EXPECT_EQ(service()->size(), 0u); |
| 241 } | 292 } |
| 242 | 293 |
| 243 TEST_F(NTPSnippetsServiceTest, InsertAtFront) { | 294 TEST_F(NTPSnippetsServiceTest, InsertAtFront) { |
| 295 std::vector<std::string> source_urls; | |
|
Bernhard Bauer
2016/04/27 13:22:05
I think you could use an initializer list here to
Bernhard Bauer
2016/04/28 14:19:20
Ping :)
May
2016/04/28 18:01:52
Done.
| |
| 296 source_urls.push_back(std::string("http://first")); | |
| 297 std::vector<std::string> publishers; | |
| 298 publishers.push_back(std::string("Source 1")); | |
| 299 std::vector<std::string> amp_urls; | |
| 300 amp_urls.push_back(std::string("")); | |
|
Bernhard Bauer
2016/04/27 13:22:05
The empty string literal is unnecessary.
Bernhard Bauer
2016/04/28 14:19:20
Ping :)
May
2016/04/28 18:01:52
Done.
| |
| 244 std::string json_str( | 301 std::string json_str( |
| 245 "{ \"recos\": [ " | 302 GetTestJsonWithSources(source_urls, publishers, amp_urls)); |
| 246 " { \"contentInfo\": { \"url\" : \"http://first\" }}" | 303 |
| 247 "]}"); | |
| 248 LoadFromJSONString(json_str); | 304 LoadFromJSONString(json_str); |
| 305 | |
| 249 ASSERT_EQ(service()->size(), 1u); | 306 ASSERT_EQ(service()->size(), 1u); |
| 250 | 307 |
| 251 std::string json_str2( | 308 source_urls.clear(); |
| 252 "{ \"recos\": [ " | 309 source_urls.push_back(std::string("http://second")); |
| 253 " { \"contentInfo\": { \"url\" : \"http://second\" }}" | 310 publishers.clear(); |
| 254 "]}"); | 311 publishers.push_back(std::string("Source 1")); |
| 255 LoadFromJSONString(json_str2); | 312 amp_urls.clear(); |
| 313 amp_urls.push_back(std::string("")); | |
| 314 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls); | |
| 315 | |
| 316 LoadFromJSONString(json_str); | |
| 256 ASSERT_EQ(service()->size(), 2u); | 317 ASSERT_EQ(service()->size(), 2u); |
| 257 | 318 |
| 258 // The snippet loaded last should be at the first position in the list now. | 319 // The snippet loaded last should be at the first position in the list now. |
| 259 const NTPSnippet& first_snippet = *service()->begin(); | 320 const NTPSnippet& first_snippet = *service()->begin(); |
| 260 EXPECT_EQ(first_snippet.url(), GURL("http://second")); | 321 EXPECT_EQ(first_snippet.url(), GURL("http://second")); |
| 261 } | 322 } |
| 262 | 323 |
| 263 TEST_F(NTPSnippetsServiceTest, LimitNumSnippets) { | 324 TEST_F(NTPSnippetsServiceTest, LimitNumSnippets) { |
| 264 int max_snippet_count = NTPSnippetsService::GetMaxSnippetCountForTesting(); | 325 int max_snippet_count = NTPSnippetsService::GetMaxSnippetCountForTesting(); |
| 265 int snippets_per_load = max_snippet_count / 2 + 1; | 326 int snippets_per_load = max_snippet_count / 2 + 1; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 307 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJsonWithExistingSnippets) { | 368 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJsonWithExistingSnippets) { |
| 308 LoadFromJSONString(GetTestJson()); | 369 LoadFromJSONString(GetTestJson()); |
| 309 ASSERT_EQ(service()->size(), 1u); | 370 ASSERT_EQ(service()->size(), 1u); |
| 310 | 371 |
| 311 LoadFromJSONString(GetIncompleteJson()); | 372 LoadFromJSONString(GetIncompleteJson()); |
| 312 // This should not have changed the existing snippets. | 373 // This should not have changed the existing snippets. |
| 313 EXPECT_EQ(service()->size(), 1u); | 374 EXPECT_EQ(service()->size(), 1u); |
| 314 } | 375 } |
| 315 | 376 |
| 316 TEST_F(NTPSnippetsServiceTest, Discard) { | 377 TEST_F(NTPSnippetsServiceTest, Discard) { |
| 378 std::vector<std::string> source_urls; | |
| 379 source_urls.push_back(std::string("http://site.com")); | |
| 380 std::vector<std::string> publishers; | |
| 381 publishers.push_back(std::string("Source 1")); | |
| 382 std::vector<std::string> amp_urls; | |
| 383 amp_urls.push_back(std::string("")); | |
| 317 std::string json_str( | 384 std::string json_str( |
| 318 "{ \"recos\": [ { \"contentInfo\": { \"url\" : \"http://site.com\" }}]}"); | 385 GetTestJsonWithSources(source_urls, publishers, amp_urls)); |
| 386 | |
| 319 LoadFromJSONString(json_str); | 387 LoadFromJSONString(json_str); |
| 320 | 388 |
| 321 ASSERT_EQ(1u, service()->size()); | 389 ASSERT_EQ(1u, service()->size()); |
| 322 | 390 |
| 323 // Discarding a non-existent snippet shouldn't do anything. | 391 // Discarding a non-existent snippet shouldn't do anything. |
| 324 EXPECT_FALSE(service()->DiscardSnippet(GURL("http://othersite.com"))); | 392 EXPECT_FALSE(service()->DiscardSnippet(GURL("http://othersite.com"))); |
| 325 EXPECT_EQ(1u, service()->size()); | 393 EXPECT_EQ(1u, service()->size()); |
| 326 | 394 |
| 327 // Discard the snippet. | 395 // Discard the snippet. |
| 328 EXPECT_TRUE(service()->DiscardSnippet(GURL("http://site.com"))); | 396 EXPECT_TRUE(service()->DiscardSnippet(GURL("http://site.com"))); |
| 329 EXPECT_EQ(0u, service()->size()); | 397 EXPECT_EQ(0u, service()->size()); |
| 330 | 398 |
| 331 // Make sure that fetching the same snippet again does not re-add it. | 399 // Make sure that fetching the same snippet again does not re-add it. |
| 332 LoadFromJSONString(json_str); | 400 LoadFromJSONString(json_str); |
| 333 EXPECT_EQ(0u, service()->size()); | 401 EXPECT_EQ(0u, service()->size()); |
| 334 | 402 |
| 335 // The snippet should stay discarded even after re-creating the service. | 403 // The snippet should stay discarded even after re-creating the service. |
| 336 CreateSnippetsService(); | 404 CreateSnippetsService(); |
| 337 LoadFromJSONString(json_str); | 405 LoadFromJSONString(json_str); |
| 338 EXPECT_EQ(0u, service()->size()); | 406 EXPECT_EQ(0u, service()->size()); |
| 339 | 407 |
| 340 // The snippet can be added again after clearing discarded snippets. | 408 // The snippet can be added again after clearing discarded snippets. |
| 341 service()->ClearDiscardedSnippets(); | 409 service()->ClearDiscardedSnippets(); |
| 342 EXPECT_EQ(0u, service()->size()); | 410 EXPECT_EQ(0u, service()->size()); |
| 343 LoadFromJSONString(json_str); | 411 LoadFromJSONString(json_str); |
| 344 EXPECT_EQ(1u, service()->size()); | 412 EXPECT_EQ(1u, service()->size()); |
| 345 } | 413 } |
| 346 | 414 |
| 347 TEST_F(NTPSnippetsServiceTest, GetDiscarded) { | 415 TEST_F(NTPSnippetsServiceTest, GetDiscarded) { |
| 348 std::string json_str( | 416 LoadFromJSONString(GetTestJson()); |
| 349 "{ \"recos\": [ { \"contentInfo\": { \"url\" : \"http://site.com\" }}]}"); | |
| 350 LoadFromJSONString(json_str); | |
| 351 | 417 |
| 352 // For the test, we need the snippet to get discarded. | 418 // For the test, we need the snippet to get discarded. |
| 353 ASSERT_TRUE(service()->DiscardSnippet(GURL("http://site.com"))); | 419 ASSERT_TRUE(service()->DiscardSnippet(GURL("http://localhost/foobar"))); |
| 354 const NTPSnippetsService::NTPSnippetStorage& snippets = | 420 const NTPSnippetsService::NTPSnippetStorage& snippets = |
| 355 service()->discarded_snippets(); | 421 service()->discarded_snippets(); |
| 356 EXPECT_EQ(1u, snippets.size()); | 422 EXPECT_EQ(1u, snippets.size()); |
| 357 for (auto& snippet : snippets) { | 423 for (auto& snippet : snippets) { |
| 358 EXPECT_EQ(GURL("http://site.com"), snippet->url()); | 424 EXPECT_EQ(GURL("http://localhost/foobar"), snippet->url()); |
| 359 } | 425 } |
| 360 | 426 |
| 361 // There should be no discarded snippet after clearing the list. | 427 // There should be no discarded snippet after clearing the list. |
| 362 service()->ClearDiscardedSnippets(); | 428 service()->ClearDiscardedSnippets(); |
| 363 EXPECT_EQ(0u, service()->discarded_snippets().size()); | 429 EXPECT_EQ(0u, service()->discarded_snippets().size()); |
| 364 } | 430 } |
| 365 | 431 |
| 366 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) { | 432 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) { |
| 367 std::string json_str(GetTestJson("aaa1448459205")); | 433 std::string json_str(GetTestJson("aaa1448459205")); |
| 368 | 434 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 379 } | 445 } |
| 380 } | 446 } |
| 381 | 447 |
| 382 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { | 448 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { |
| 383 std::string json_str(GetTestExpiredJson()); | 449 std::string json_str(GetTestExpiredJson()); |
| 384 | 450 |
| 385 LoadFromJSONString(json_str); | 451 LoadFromJSONString(json_str); |
| 386 EXPECT_EQ(service()->size(), 0u); | 452 EXPECT_EQ(service()->size(), 0u); |
| 387 } | 453 } |
| 388 | 454 |
| 455 TEST_F(NTPSnippetsServiceTest, TestSingleSource) { | |
| 456 std::vector<std::string> source_urls; | |
| 457 source_urls.push_back(std::string("http://source1.com")); | |
| 458 std::vector<std::string> publishers; | |
| 459 publishers.push_back(std::string("Source 1")); | |
| 460 std::vector<std::string> amp_urls; | |
| 461 amp_urls.push_back(std::string("http://source1.amp.com")); | |
| 462 std::string json_str( | |
| 463 GetTestJsonWithSources(source_urls, publishers, amp_urls)); | |
| 464 | |
| 465 LoadFromJSONString(json_str); | |
| 466 EXPECT_EQ(service()->size(), 1u); | |
| 467 | |
| 468 for (auto& snippet : *service()) { | |
| 469 EXPECT_EQ(snippet.url(), GURL("http://source1.com")); | |
| 470 EXPECT_EQ(snippet.site_title(), std::string("Source 1")); | |
| 471 EXPECT_EQ(snippet.amp_url(), GURL("http://source1.amp.com")); | |
| 472 EXPECT_EQ(snippet.get_sources().size(), 1u); | |
| 473 } | |
| 474 } | |
| 475 | |
| 476 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMalformedUrl) { | |
| 477 std::vector<std::string> source_urls; | |
| 478 source_urls.push_back(std::string("aaaa")); | |
| 479 std::vector<std::string> publishers; | |
| 480 publishers.push_back(std::string("Source 1")); | |
| 481 std::vector<std::string> amp_urls; | |
| 482 amp_urls.push_back(std::string("http://source1.amp.com")); | |
| 483 std::string json_str( | |
| 484 GetTestJsonWithSources(source_urls, publishers, amp_urls)); | |
| 485 | |
| 486 LoadFromJSONString(json_str); | |
| 487 // If we're running a release build, we only add snippets that have a well- | |
| 488 // formed source url | |
| 489 #if !DCHECK_IS_ON() | |
| 490 EXPECT_EQ(service()->size(), 0u); | |
| 491 #else | |
| 492 EXPECT_EQ(service()->size(), 1u); | |
| 493 | |
| 494 for (auto& snippet : *service()) { | |
| 495 EXPECT_EQ(snippet.site_title(), std::string()); | |
| 496 EXPECT_EQ(snippet.get_sources().size(), 0u); | |
| 497 } | |
| 498 #endif | |
| 499 } | |
| 500 | |
| 501 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMissingData) { | |
| 502 std::vector<std::string> source_urls; | |
| 503 source_urls.push_back(std::string("http://source1.com")); | |
| 504 std::vector<std::string> publishers; | |
| 505 publishers.push_back(std::string("")); | |
| 506 std::vector<std::string> amp_urls; | |
| 507 amp_urls.push_back(std::string("")); | |
| 508 std::string json_str( | |
| 509 GetTestJsonWithSources(source_urls, publishers, amp_urls)); | |
| 510 | |
| 511 LoadFromJSONString(json_str); | |
| 512 // If we're running a release build, we only add snippets have a source url | |
| 513 // and publisher | |
| 514 #if !DCHECK_IS_ON() | |
| 515 EXPECT_EQ(service()->size(), 0u); | |
| 516 #else | |
| 517 EXPECT_EQ(service()->size(), 1u); | |
| 518 | |
| 519 for (auto& snippet : *service()) { | |
| 520 EXPECT_EQ(snippet.url(), GURL("http://source1.com")); | |
| 521 EXPECT_EQ(snippet.site_title(), std::string()); | |
| 522 EXPECT_EQ(snippet.amp_url(), GURL()); | |
| 523 EXPECT_EQ(snippet.get_sources().size(), 1u); | |
| 524 } | |
| 525 #endif | |
| 526 } | |
| 527 | |
| 528 TEST_F(NTPSnippetsServiceTest, TestMultipleSources) { | |
| 529 std::vector<std::string> source_urls; | |
| 530 source_urls.push_back(std::string("http://source1.com")); | |
| 531 source_urls.push_back(std::string("http://source2.com")); | |
| 532 std::vector<std::string> publishers; | |
| 533 publishers.push_back(std::string("Source 1")); | |
| 534 publishers.push_back(std::string("Source 2")); | |
| 535 std::vector<std::string> amp_urls; | |
| 536 amp_urls.push_back(std::string("http://source1.amp.com")); | |
| 537 amp_urls.push_back(std::string("http://source2.amp.com")); | |
| 538 std::string json_str( | |
| 539 GetTestJsonWithSources(source_urls, publishers, amp_urls)); | |
| 540 | |
| 541 LoadFromJSONString(json_str); | |
| 542 EXPECT_EQ(service()->size(), 1u); | |
| 543 | |
| 544 // Expect the first source to be chosen | |
| 545 for (auto& snippet : *service()) { | |
| 546 EXPECT_EQ(snippet.get_sources().size(), 2u); | |
| 547 EXPECT_EQ(snippet.url(), GURL("http://source1.com")); | |
| 548 EXPECT_EQ(snippet.site_title(), std::string("Source 1")); | |
| 549 EXPECT_EQ(snippet.amp_url(), GURL("http://source1.amp.com")); | |
| 550 } | |
| 551 } | |
| 552 | |
| 553 TEST_F(NTPSnippetsServiceTest, TestMultipleIncompleteSources) { | |
| 554 // Set Source 2 to have no AMP url, and Source 1 to have no publisher name | |
| 555 // Source 2 should win since we favor publisher name over amp url | |
| 556 std::vector<std::string> source_urls; | |
| 557 source_urls.push_back(std::string("http://source1.com")); | |
| 558 source_urls.push_back(std::string("http://source2.com")); | |
| 559 std::vector<std::string> publishers; | |
| 560 publishers.push_back(std::string("")); | |
| 561 publishers.push_back(std::string("Source 2")); | |
| 562 std::vector<std::string> amp_urls; | |
| 563 amp_urls.push_back(std::string("http://source1.amp.com")); | |
| 564 amp_urls.push_back(std::string("")); | |
| 565 std::string json_str( | |
| 566 GetTestJsonWithSources(source_urls, publishers, amp_urls)); | |
| 567 | |
| 568 LoadFromJSONString(json_str); | |
| 569 EXPECT_EQ(service()->size(), 1u); | |
| 570 | |
| 571 for (auto& snippet : *service()) { | |
| 572 EXPECT_EQ(snippet.get_sources().size(), 2u); | |
| 573 EXPECT_EQ(snippet.url(), GURL("http://source2.com")); | |
| 574 EXPECT_EQ(snippet.site_title(), std::string("Source 2")); | |
| 575 EXPECT_EQ(snippet.amp_url(), GURL()); | |
| 576 } | |
| 577 | |
| 578 service()->ClearSnippets(); | |
| 579 // Set Source 1 to have no AMP url, and Source 2 to have no publisher name | |
| 580 // Source 1 should win in this case since we prefer publisher name to AMP url | |
| 581 source_urls.clear(); | |
| 582 source_urls.push_back(std::string("http://source1.com")); | |
| 583 source_urls.push_back(std::string("http://source2.com")); | |
| 584 publishers.clear(); | |
| 585 publishers.push_back(std::string("Source 1")); | |
| 586 publishers.push_back(std::string("")); | |
| 587 amp_urls.clear(); | |
| 588 amp_urls.push_back(std::string("")); | |
| 589 amp_urls.push_back(std::string("http://source2.amp.com")); | |
| 590 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls); | |
| 591 | |
| 592 LoadFromJSONString(json_str); | |
| 593 EXPECT_EQ(service()->size(), 1u); | |
| 594 | |
| 595 for (auto& snippet : *service()) { | |
| 596 EXPECT_EQ(snippet.get_sources().size(), 2u); | |
| 597 EXPECT_EQ(snippet.url(), GURL("http://source1.com")); | |
| 598 EXPECT_EQ(snippet.site_title(), std::string("Source 1")); | |
| 599 EXPECT_EQ(snippet.amp_url(), GURL()); | |
| 600 } | |
| 601 | |
| 602 service()->ClearSnippets(); | |
| 603 // Set source 1 to have no AMP url and no source. Source 1 should win since | |
| 604 // all sources without publisher name are ranked equally | |
| 605 source_urls.clear(); | |
| 606 source_urls.push_back(std::string("http://source1.com")); | |
| 607 source_urls.push_back(std::string("http://source2.com")); | |
| 608 publishers.clear(); | |
| 609 publishers.push_back(std::string("")); | |
| 610 publishers.push_back(std::string("")); | |
| 611 amp_urls.clear(); | |
| 612 amp_urls.push_back(std::string("")); | |
| 613 amp_urls.push_back(std::string("http://source2.amp.com")); | |
| 614 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls); | |
| 615 | |
| 616 LoadFromJSONString(json_str); | |
| 617 #if !DCHECK_IS_ON() | |
| 618 EXPECT_EQ(service()->size(), 0u); | |
| 619 #else | |
| 620 EXPECT_EQ(service()->size(), 1u); | |
| 621 for (auto& snippet : *service()) { | |
| 622 EXPECT_EQ(snippet.get_sources().size(), 2u); | |
| 623 EXPECT_EQ(snippet.url(), GURL("http://source1.com")); | |
| 624 EXPECT_EQ(snippet.site_title(), std::string()); | |
| 625 EXPECT_EQ(snippet.amp_url(), GURL()); | |
| 626 } | |
| 627 #endif | |
| 628 } | |
| 629 | |
| 630 TEST_F(NTPSnippetsServiceTest, TestMultipleCompleteSources) { | |
| 631 // Test 2 complete sources, we should choose the first complete source | |
| 632 std::vector<std::string> source_urls; | |
| 633 source_urls.push_back(std::string("http://source1.com")); | |
| 634 source_urls.push_back(std::string("http://source2.com")); | |
| 635 source_urls.push_back(std::string("http://source3.com")); | |
| 636 std::vector<std::string> publishers; | |
| 637 publishers.push_back(std::string("Source 1")); | |
| 638 publishers.push_back(std::string("")); | |
| 639 publishers.push_back(std::string("Source 3")); | |
| 640 std::vector<std::string> amp_urls; | |
| 641 amp_urls.push_back(std::string("http://source1.amp.com")); | |
| 642 amp_urls.push_back(std::string("http://source2.amp.com")); | |
| 643 amp_urls.push_back(std::string("http://source3.amp.com")); | |
| 644 std::string json_str( | |
| 645 GetTestJsonWithSources(source_urls, publishers, amp_urls)); | |
| 646 | |
| 647 LoadFromJSONString(json_str); | |
| 648 EXPECT_EQ(service()->size(), 1u); | |
| 649 | |
| 650 for (auto& snippet : *service()) { | |
| 651 EXPECT_EQ(snippet.get_sources().size(), 3u); | |
| 652 EXPECT_EQ(snippet.url(), GURL("http://source1.com")); | |
| 653 EXPECT_EQ(snippet.site_title(), std::string("Source 1")); | |
| 654 EXPECT_EQ(snippet.amp_url(), GURL("http://source1.amp.com")); | |
| 655 } | |
| 656 | |
| 657 // Test 2 complete sources, we should choose the first complete source | |
| 658 service()->ClearSnippets(); | |
| 659 source_urls.clear(); | |
| 660 source_urls.push_back(std::string("http://source1.com")); | |
| 661 source_urls.push_back(std::string("http://source2.com")); | |
| 662 source_urls.push_back(std::string("http://source3.com")); | |
| 663 publishers.clear(); | |
| 664 publishers.push_back(std::string("")); | |
| 665 publishers.push_back(std::string("Source 2")); | |
| 666 publishers.push_back(std::string("Source 3")); | |
| 667 amp_urls.clear(); | |
| 668 amp_urls.push_back(std::string("http://source1.amp.com")); | |
| 669 amp_urls.push_back(std::string("http://source2.amp.com")); | |
| 670 amp_urls.push_back(std::string("http://source3.amp.com")); | |
| 671 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls); | |
| 672 | |
| 673 LoadFromJSONString(json_str); | |
| 674 EXPECT_EQ(service()->size(), 1u); | |
| 675 | |
| 676 for (auto& snippet : *service()) { | |
| 677 EXPECT_EQ(snippet.get_sources().size(), 3u); | |
| 678 EXPECT_EQ(snippet.url(), GURL("http://source2.com")); | |
| 679 EXPECT_EQ(snippet.site_title(), std::string("Source 2")); | |
| 680 EXPECT_EQ(snippet.amp_url(), GURL("http://source2.amp.com")); | |
| 681 } | |
| 682 | |
| 683 // Test 3 complete sources, we should choose the first complete source | |
| 684 service()->ClearSnippets(); | |
| 685 source_urls.clear(); | |
| 686 source_urls.push_back(std::string("http://source1.com")); | |
| 687 source_urls.push_back(std::string("http://source2.com")); | |
| 688 source_urls.push_back(std::string("http://source3.com")); | |
| 689 publishers.clear(); | |
| 690 publishers.push_back(std::string("Source 1")); | |
| 691 publishers.push_back(std::string("Source 2")); | |
| 692 publishers.push_back(std::string("Source 3")); | |
| 693 amp_urls.clear(); | |
| 694 amp_urls.push_back(std::string("")); | |
| 695 amp_urls.push_back(std::string("http://source2.amp.com")); | |
| 696 amp_urls.push_back(std::string("http://source3.amp.com")); | |
| 697 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls); | |
| 698 | |
| 699 LoadFromJSONString(json_str); | |
| 700 EXPECT_EQ(service()->size(), 1u); | |
| 701 | |
| 702 for (auto& snippet : *service()) { | |
| 703 EXPECT_EQ(snippet.get_sources().size(), 3u); | |
| 704 EXPECT_EQ(snippet.url(), GURL("http://source2.com")); | |
| 705 EXPECT_EQ(snippet.site_title(), std::string("Source 2")); | |
| 706 EXPECT_EQ(snippet.amp_url(), GURL("http://source2.amp.com")); | |
| 707 } | |
| 708 } | |
| 389 } // namespace ntp_snippets | 709 } // namespace ntp_snippets |
| OLD | NEW |