Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 package org.chromium.chrome.browser.ntp.cards; | 5 package org.chromium.chrome.browser.ntp.cards; |
| 6 | 6 |
| 7 import static org.junit.Assert.assertEquals; | 7 import static org.junit.Assert.assertEquals; |
| 8 import static org.junit.Assert.assertFalse; | 8 import static org.junit.Assert.assertFalse; |
| 9 import static org.junit.Assert.assertTrue; | 9 import static org.junit.Assert.assertTrue; |
| 10 | 10 |
| 11 import org.chromium.base.metrics.RecordHistogram; | 11 import org.chromium.base.metrics.RecordHistogram; |
| 12 import org.chromium.base.metrics.RecordUserAction; | 12 import org.chromium.base.metrics.RecordUserAction; |
| 13 import org.chromium.base.test.util.Feature; | 13 import org.chromium.base.test.util.Feature; |
| 14 import org.chromium.chrome.browser.ntp.snippets.CategoryInt; | |
| 14 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus; | 15 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus; |
| 15 import org.chromium.chrome.browser.ntp.snippets.ContentSuggestionsCardLayout; | 16 import org.chromium.chrome.browser.ntp.snippets.ContentSuggestionsCardLayout; |
| 16 import org.chromium.chrome.browser.ntp.snippets.FakeSuggestionsSource; | 17 import org.chromium.chrome.browser.ntp.snippets.FakeSuggestionsSource; |
| 17 import org.chromium.chrome.browser.ntp.snippets.KnownCategories; | 18 import org.chromium.chrome.browser.ntp.snippets.KnownCategories; |
| 18 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; | 19 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; |
| 19 import org.chromium.testing.local.LocalRobolectricTestRunner; | 20 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 20 import org.junit.Before; | 21 import org.junit.Before; |
| 21 import org.junit.Test; | 22 import org.junit.Test; |
| 22 import org.junit.runner.RunWith; | 23 import org.junit.runner.RunWith; |
| 23 import org.robolectric.annotation.Config; | 24 import org.robolectric.annotation.Config; |
| (...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 436 mSnippetsSource.setStatusForCategory(KnownCategories.ARTICLES, CategoryS tatus.AVAILABLE); | 437 mSnippetsSource.setStatusForCategory(KnownCategories.ARTICLES, CategoryS tatus.AVAILABLE); |
| 437 mSnippetsSource.setSuggestionsForCategory(KnownCategories.ARTICLES, arti cles); | 438 mSnippetsSource.setSuggestionsForCategory(KnownCategories.ARTICLES, arti cles); |
| 438 assertItemsFor(section(3)); | 439 assertItemsFor(section(3)); |
| 439 assertEquals(articles, mNtpAdapter.getItems().subList(2, 5)); | 440 assertEquals(articles, mNtpAdapter.getItems().subList(2, 5)); |
| 440 | 441 |
| 441 SnippetArticle removed = articles.remove(1); | 442 SnippetArticle removed = articles.remove(1); |
| 442 mSnippetsSource.fireSuggestionInvalidated(KnownCategories.ARTICLES, remo ved.mId); | 443 mSnippetsSource.fireSuggestionInvalidated(KnownCategories.ARTICLES, remo ved.mId); |
| 443 assertEquals(articles, mNtpAdapter.getItems().subList(2, 4)); | 444 assertEquals(articles, mNtpAdapter.getItems().subList(2, 4)); |
| 444 } | 445 } |
| 445 | 446 |
| 447 /** | |
| 448 * Tests that the order of the categories is kept. | |
| 449 */ | |
| 450 @Test | |
| 451 @Feature({"Ntp"}) | |
| 452 public void testCategoryOrder() { | |
| 453 FakeSuggestionsSource suggestionsSource = new FakeSuggestionsSource(); | |
| 454 registerCategory(suggestionsSource, KnownCategories.ARTICLES, 0); | |
| 455 registerCategory(suggestionsSource, KnownCategories.BOOKMARKS, 0); | |
| 456 registerCategory(suggestionsSource, KnownCategories.PHYSICAL_WEB_PAGES, 0); | |
| 457 registerCategory(suggestionsSource, KnownCategories.DOWNLOADS, 0); | |
| 458 | |
| 459 NewTabPageAdapter ntpAdapter = new NewTabPageAdapter(null, null, suggest ionsSource, null); | |
| 460 List<ItemGroup> groups = ntpAdapter.getGroups(); | |
| 461 | |
| 462 assertEquals(6, groups.size()); | |
| 463 assertEquals(AboveTheFoldItem.class, groups.get(0).getClass()); | |
| 464 assertEquals(SuggestionsSection.class, groups.get(1).getClass()); | |
| 465 assertEquals(KnownCategories.ARTICLES, getCategory(groups.get(1))); | |
| 466 assertEquals(SuggestionsSection.class, groups.get(2).getClass()); | |
| 467 assertEquals(KnownCategories.BOOKMARKS, getCategory(groups.get(2))); | |
| 468 assertEquals(SuggestionsSection.class, groups.get(3).getClass()); | |
| 469 assertEquals(KnownCategories.PHYSICAL_WEB_PAGES, getCategory(groups.get( 3))); | |
| 470 assertEquals(SuggestionsSection.class, groups.get(4).getClass()); | |
| 471 assertEquals(KnownCategories.DOWNLOADS, getCategory(groups.get(4))); | |
| 472 | |
| 473 // With a different order | |
| 474 suggestionsSource = new FakeSuggestionsSource(); | |
| 475 registerCategory(suggestionsSource, KnownCategories.ARTICLES, 0); | |
| 476 registerCategory(suggestionsSource, KnownCategories.PHYSICAL_WEB_PAGES, 0); | |
| 477 registerCategory(suggestionsSource, KnownCategories.DOWNLOADS, 0); | |
| 478 registerCategory(suggestionsSource, KnownCategories.BOOKMARKS, 0); | |
| 479 | |
| 480 ntpAdapter = new NewTabPageAdapter(null, null, suggestionsSource, null); | |
| 481 groups = ntpAdapter.getGroups(); | |
| 482 | |
| 483 assertEquals(6, groups.size()); | |
| 484 assertEquals(AboveTheFoldItem.class, groups.get(0).getClass()); | |
| 485 assertEquals(SuggestionsSection.class, groups.get(1).getClass()); | |
| 486 assertEquals(KnownCategories.ARTICLES, getCategory(groups.get(1))); | |
| 487 assertEquals(SuggestionsSection.class, groups.get(2).getClass()); | |
| 488 assertEquals(KnownCategories.PHYSICAL_WEB_PAGES, getCategory(groups.get( 2))); | |
| 489 assertEquals(SuggestionsSection.class, groups.get(3).getClass()); | |
| 490 assertEquals(KnownCategories.DOWNLOADS, getCategory(groups.get(3))); | |
| 491 assertEquals(SuggestionsSection.class, groups.get(4).getClass()); | |
| 492 assertEquals(KnownCategories.BOOKMARKS, getCategory(groups.get(4))); | |
| 493 | |
| 494 // With unknown categories | |
| 495 suggestionsSource = new FakeSuggestionsSource(); | |
| 496 registerCategory(suggestionsSource, KnownCategories.ARTICLES, 0); | |
| 497 registerCategory(suggestionsSource, KnownCategories.PHYSICAL_WEB_PAGES, 0); | |
| 498 registerCategory(suggestionsSource, KnownCategories.DOWNLOADS, 0); | |
| 499 | |
| 500 ntpAdapter = new NewTabPageAdapter(null, null, suggestionsSource, null); | |
| 501 | |
| 502 // The adapter is already initialised, it will now only accept new categ ories if they have | |
| 503 // suggestions/ | |
| 504 registerCategory(suggestionsSource, 42, 1); | |
| 505 registerCategory(suggestionsSource, KnownCategories.BOOKMARKS, 1); | |
| 506 | |
| 507 groups = ntpAdapter.getGroups(); | |
| 508 | |
| 509 assertEquals(7, groups.size()); | |
| 510 assertEquals(AboveTheFoldItem.class, groups.get(0).getClass()); | |
| 511 assertEquals(SuggestionsSection.class, groups.get(1).getClass()); | |
| 512 assertEquals(KnownCategories.ARTICLES, getCategory(groups.get(1))); | |
| 513 assertEquals(SuggestionsSection.class, groups.get(2).getClass()); | |
| 514 assertEquals(KnownCategories.PHYSICAL_WEB_PAGES, getCategory(groups.get( 2))); | |
| 515 assertEquals(SuggestionsSection.class, groups.get(3).getClass()); | |
| 516 assertEquals(KnownCategories.DOWNLOADS, getCategory(groups.get(3))); | |
| 517 assertEquals(SuggestionsSection.class, groups.get(4).getClass()); | |
| 518 assertEquals(42, getCategory(groups.get(4))); | |
| 519 assertEquals(SuggestionsSection.class, groups.get(5).getClass()); | |
| 520 assertEquals(KnownCategories.BOOKMARKS, getCategory(groups.get(5))); | |
| 521 } | |
| 522 | |
| 446 private List<SnippetArticle> createDummySnippets(int count) { | 523 private List<SnippetArticle> createDummySnippets(int count) { |
| 447 List<SnippetArticle> snippets = new ArrayList<>(); | 524 List<SnippetArticle> snippets = new ArrayList<>(); |
| 448 for (int index = 0; index < count; index++) { | 525 for (int index = 0; index < count; index++) { |
| 449 snippets.add(new SnippetArticle(0, "https://site.com/url" + index, " title" + index, | 526 snippets.add(new SnippetArticle(0, "https://site.com/url" + index, " title" + index, |
| 450 "pub" + index, "txt" + index, "https://site.com/url" + index , | 527 "pub" + index, "txt" + index, "https://site.com/url" + index , |
| 451 "https://amp.site.com/url" + index, 0, 0, 0, | 528 "https://amp.site.com/url" + index, 0, 0, 0, |
| 452 ContentSuggestionsCardLayout.FULL_CARD)); | 529 ContentSuggestionsCardLayout.FULL_CARD)); |
| 453 } | 530 } |
| 454 return snippets; | 531 return snippets; |
| 455 } | 532 } |
| 533 | |
| 534 private void registerCategory(FakeSuggestionsSource suggestionsSource, | |
| 535 @CategoryInt int category, int suggestionCount) { | |
| 536 // Use available since the Adapter does not add fake suggestion source d oes not return | |
|
Bernhard Bauer
2016/08/25 13:23:45
Nit: This sentence does not parse.
dgn
2016/08/25 15:09:40
Done. I have no memory of writing that. I was prob
Bernhard Bauer
2016/08/25 15:20:31
https://www.youtube.com/watch?v=SSUXXzN26zg
| |
| 537 // snippets if it's not an available status | |
| 538 suggestionsSource.setStatusForCategory(category, CategoryStatus.AVAILABL E); | |
| 539 // Important: showIfEmptry flag to true. | |
|
Bernhard Bauer
2016/08/25 13:23:45
Nit: showIfEmpty
dgn
2016/08/25 15:09:40
Done.
| |
| 540 suggestionsSource.setInfoForCategory( | |
| 541 category, new SuggestionsCategoryInfo( | |
| 542 "", ContentSuggestionsCardLayout.FULL_CARD, fa lse, true)); | |
| 543 suggestionsSource.setSuggestionsForCategory(category, createDummySnippet s(suggestionCount)); | |
| 544 } | |
| 545 | |
| 546 private int getCategory(ItemGroup itemGroup) { | |
| 547 return ((SuggestionsSection) itemGroup).getCategory(); | |
| 548 } | |
| 456 } | 549 } |
| OLD | NEW |