| Index: chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapterTest.java
|
| diff --git a/chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapterTest.java b/chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapterTest.java
|
| index 1cc00284d7e5e508c1212f520d9ab3f462dec659..d427cdc85f48c70db06b232f71a22fb758381130 100644
|
| --- a/chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapterTest.java
|
| +++ b/chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapterTest.java
|
| @@ -4,12 +4,17 @@
|
|
|
| package org.chromium.chrome.browser.ntp.cards;
|
|
|
| +import static org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils.createDummySuggestions;
|
| import static org.junit.Assert.assertEquals;
|
| import static org.junit.Assert.assertFalse;
|
| -import static org.junit.Assert.assertNotNull;
|
| -import static org.junit.Assert.assertNull;
|
| import static org.junit.Assert.assertTrue;
|
| import static org.junit.Assert.fail;
|
| +import static org.mockito.Mockito.any;
|
| +import static org.mockito.Mockito.anyString;
|
| +import static org.mockito.Mockito.doNothing;
|
| +import static org.mockito.Mockito.spy;
|
| +import static org.mockito.Mockito.times;
|
| +import static org.mockito.Mockito.verify;
|
|
|
| import android.support.annotation.Nullable;
|
| import android.view.ContextMenu;
|
| @@ -214,6 +219,7 @@ public void setUp() {
|
| new SuggestionsCategoryInfo("Articles for you",
|
| ContentSuggestionsCardLayout.FULL_CARD, false, true));
|
| mAdapter = new NewTabPageAdapter(new MockNewTabPageManager(mSource), null, null);
|
| + mAdapter.initializeSections();
|
| }
|
|
|
| /**
|
| @@ -397,20 +403,20 @@ public void testSectionClearingWhenUnavailable() {
|
| assertItemsFor();
|
|
|
| // Same when loading a new NTP.
|
| - mAdapter = new NewTabPageAdapter(new MockNewTabPageManager(mSource), null, null);
|
| + mAdapter.initializeSections();
|
| assertItemsFor();
|
|
|
| // Same for CATEGORY_EXPLICITLY_DISABLED.
|
| mSource.setStatusForCategory(KnownCategories.ARTICLES, CategoryStatus.AVAILABLE);
|
| mSource.setSuggestionsForCategory(KnownCategories.ARTICLES, snippets);
|
| - mAdapter = new NewTabPageAdapter(new MockNewTabPageManager(mSource), null, null);
|
| + mAdapter.initializeSections();
|
| assertItemsFor(section(5));
|
| mSource.setStatusForCategory(
|
| KnownCategories.ARTICLES, CategoryStatus.CATEGORY_EXPLICITLY_DISABLED);
|
| assertItemsFor();
|
|
|
| // Same when loading a new NTP.
|
| - mAdapter = new NewTabPageAdapter(new MockNewTabPageManager(mSource), null, null);
|
| + mAdapter.initializeSections();
|
| assertItemsFor();
|
| }
|
|
|
| @@ -431,7 +437,7 @@ public void testUIUntouchedWhenNotProvided() {
|
| assertItemsFor(section(4));
|
|
|
| // But it disappears when loading a new NTP.
|
| - mAdapter = new NewTabPageAdapter(new MockNewTabPageManager(mSource), null, null);
|
| + mAdapter.initializeSections();
|
| assertItemsFor();
|
| }
|
|
|
| @@ -443,6 +449,7 @@ public void testSectionVisibleIfEmpty() {
|
| final List<SnippetArticle> articles =
|
| Collections.unmodifiableList(createDummySuggestions(3));
|
| FakeSuggestionsSource suggestionsSource;
|
| + MockNewTabPageManager newTabPageManager;
|
|
|
| // Part 1: VisibleIfEmpty = true
|
| suggestionsSource = new FakeSuggestionsSource();
|
| @@ -450,9 +457,11 @@ public void testSectionVisibleIfEmpty() {
|
| suggestionsSource.setInfoForCategory(
|
| category, new SuggestionsCategoryInfo(
|
| "", ContentSuggestionsCardLayout.MINIMAL_CARD, false, true));
|
| + newTabPageManager = new MockNewTabPageManager(suggestionsSource);
|
| + mAdapter = new NewTabPageAdapter(newTabPageManager, null, null);
|
|
|
| // 1.1 - Initial state
|
| - mAdapter = new NewTabPageAdapter(new MockNewTabPageManager(suggestionsSource), null, null);
|
| + mAdapter.initializeSections();
|
| assertItemsFor(sectionWithStatusCard());
|
|
|
| // 1.2 - With suggestions
|
| @@ -477,7 +486,8 @@ public void testSectionVisibleIfEmpty() {
|
| "", ContentSuggestionsCardLayout.MINIMAL_CARD, false, false));
|
|
|
| // 2.1 - Initial state
|
| - mAdapter = new NewTabPageAdapter(new MockNewTabPageManager(suggestionsSource), null, null);
|
| + newTabPageManager.setSuggestionsSource(suggestionsSource);
|
| + mAdapter.initializeSections();
|
| assertItemsFor();
|
|
|
| // 2.2 - With suggestions
|
| @@ -500,6 +510,7 @@ public void testMoreButton() {
|
| Collections.unmodifiableList(createDummySuggestions(3));
|
| FakeSuggestionsSource suggestionsSource;
|
| SuggestionsSection section42;
|
| + MockNewTabPageManager newTabPageManager;
|
|
|
| // Part 1: ShowMoreButton = true
|
| suggestionsSource = new FakeSuggestionsSource();
|
| @@ -507,9 +518,12 @@ public void testMoreButton() {
|
| suggestionsSource.setInfoForCategory(
|
| category, new SuggestionsCategoryInfo(
|
| "", ContentSuggestionsCardLayout.MINIMAL_CARD, true, true));
|
| + newTabPageManager = new MockNewTabPageManager(suggestionsSource);
|
| + mAdapter = new NewTabPageAdapter(newTabPageManager, null, null);
|
|
|
| // 1.1 - Initial state.
|
| - mAdapter = new NewTabPageAdapter(new MockNewTabPageManager(suggestionsSource), null, null);
|
| + newTabPageManager.setSuggestionsSource(suggestionsSource);
|
| + mAdapter.initializeSections();
|
| assertItemsFor(sectionWithStatusCardAndMoreButton());
|
|
|
| // 1.2 - With suggestions.
|
| @@ -534,7 +548,8 @@ public void testMoreButton() {
|
| "", ContentSuggestionsCardLayout.MINIMAL_CARD, false, true));
|
|
|
| // 2.1 - Initial state.
|
| - mAdapter = new NewTabPageAdapter(new MockNewTabPageManager(suggestionsSource), null, null);
|
| + newTabPageManager.setSuggestionsSource(suggestionsSource);
|
| + mAdapter.initializeSections();
|
| assertItemsFor(sectionWithStatusCard());
|
|
|
| // 2.2 - With suggestions.
|
| @@ -587,7 +602,7 @@ public void testDynamicCategories() {
|
| ContentSuggestionsCardLayout.MINIMAL_CARD, true, false));
|
| mSource.setStatusForCategory(dynamicCategory1, CategoryStatus.AVAILABLE);
|
| mSource.setSuggestionsForCategory(dynamicCategory1, dynamics1);
|
| - mAdapter = new NewTabPageAdapter(new MockNewTabPageManager(mSource), null, null); // Reload
|
| + mAdapter.initializeSections(); // Reload
|
| assertItemsFor(section(3), sectionWithMoreButton(5));
|
|
|
| int dynamicCategory2 = 1011;
|
| @@ -597,7 +612,7 @@ public void testDynamicCategories() {
|
| ContentSuggestionsCardLayout.MINIMAL_CARD, false, false));
|
| mSource.setStatusForCategory(dynamicCategory2, CategoryStatus.AVAILABLE);
|
| mSource.setSuggestionsForCategory(dynamicCategory2, dynamics2);
|
| - mAdapter = new NewTabPageAdapter(new MockNewTabPageManager(mSource), null, null); // Reload
|
| + mAdapter.initializeSections(); // Reload
|
| assertItemsFor(section(3), sectionWithMoreButton(5), section(11));
|
| }
|
|
|
| @@ -612,9 +627,10 @@ public void testCategoryOrder() {
|
| registerCategory(suggestionsSource, KnownCategories.BOOKMARKS, 0);
|
| registerCategory(suggestionsSource, KnownCategories.PHYSICAL_WEB_PAGES, 0);
|
| registerCategory(suggestionsSource, KnownCategories.DOWNLOADS, 0);
|
| + MockNewTabPageManager newTabPageManager = new MockNewTabPageManager(suggestionsSource);
|
|
|
| - NewTabPageAdapter ntpAdapter = new NewTabPageAdapter(
|
| - new MockNewTabPageManager(suggestionsSource), null, null);
|
| + NewTabPageAdapter ntpAdapter = new NewTabPageAdapter(newTabPageManager, null, null);
|
| + ntpAdapter.initializeSections();
|
| List<ItemGroup> groups = ntpAdapter.getGroups();
|
|
|
| assertEquals(7, groups.size());
|
| @@ -635,8 +651,8 @@ public void testCategoryOrder() {
|
| registerCategory(suggestionsSource, KnownCategories.DOWNLOADS, 0);
|
| registerCategory(suggestionsSource, KnownCategories.BOOKMARKS, 0);
|
|
|
| - ntpAdapter = new NewTabPageAdapter(
|
| - new MockNewTabPageManager(suggestionsSource), null, null);
|
| + newTabPageManager.setSuggestionsSource(suggestionsSource);
|
| + ntpAdapter.initializeSections();
|
| groups = ntpAdapter.getGroups();
|
|
|
| assertEquals(7, groups.size());
|
| @@ -656,8 +672,8 @@ public void testCategoryOrder() {
|
| registerCategory(suggestionsSource, KnownCategories.PHYSICAL_WEB_PAGES, 0);
|
| registerCategory(suggestionsSource, KnownCategories.DOWNLOADS, 0);
|
|
|
| - ntpAdapter = new NewTabPageAdapter(
|
| - new MockNewTabPageManager(suggestionsSource), null, null);
|
| + newTabPageManager.setSuggestionsSource(suggestionsSource);
|
| + ntpAdapter.initializeSections();
|
|
|
| // The adapter is already initialised, it will not accept new categories anymore.
|
| registerCategory(suggestionsSource, 42, 1);
|
| @@ -677,53 +693,67 @@ public void testCategoryOrder() {
|
|
|
| @Test
|
| @Feature({"Ntp"})
|
| - public void testDismissSibling() {
|
| - List<SnippetArticle> snippets = createDummySuggestions(3);
|
| - SuggestionsSection section;
|
| -
|
| - // Part 1: ShowMoreButton = true
|
| - section = new SuggestionsSection(42,
|
| - new SuggestionsCategoryInfo("", ContentSuggestionsCardLayout.FULL_CARD, true, true),
|
| - null);
|
| - section.setStatus(CategoryStatus.AVAILABLE);
|
| - assertNotNull(section.getActionItem());
|
| -
|
| - // 1.1: Without snippets
|
| - assertEquals(-1, section.getDismissSiblingPosDelta(section.getActionItem()));
|
| - assertEquals(1, section.getDismissSiblingPosDelta(section.getStatusItem()));
|
| -
|
| - // 1.2: With snippets
|
| - section.setSuggestions(snippets, CategoryStatus.AVAILABLE);
|
| - assertEquals(0, section.getDismissSiblingPosDelta(section.getActionItem()));
|
| - assertEquals(0, section.getDismissSiblingPosDelta(section.getStatusItem()));
|
| - assertEquals(0, section.getDismissSiblingPosDelta(snippets.get(0)));
|
| -
|
| - // Part 2: ShowMoreButton = false
|
| - section = new SuggestionsSection(42,
|
| - new SuggestionsCategoryInfo("", ContentSuggestionsCardLayout.FULL_CARD, false,
|
| - true),
|
| - null);
|
| - section.setStatus(CategoryStatus.AVAILABLE);
|
| - assertNull(section.getActionItem());
|
| -
|
| - // 2.1: Without snippets
|
| - assertEquals(0, section.getDismissSiblingPosDelta(section.getStatusItem()));
|
| -
|
| - // 2.2: With snippets
|
| - section.setSuggestions(snippets, CategoryStatus.AVAILABLE);
|
| - assertEquals(0, section.getDismissSiblingPosDelta(section.getStatusItem()));
|
| - assertEquals(0, section.getDismissSiblingPosDelta(snippets.get(0)));
|
| - }
|
| -
|
| - private List<SnippetArticle> createDummySuggestions(int count) {
|
| - List<SnippetArticle> suggestions = new ArrayList<>();
|
| - for (int index = 0; index < count; index++) {
|
| - suggestions.add(new SnippetArticle(0, "https://site.com/url" + index, "title" + index,
|
| - "pub" + index, "txt" + index, "https://site.com/url" + index,
|
| - "https://amp.site.com/url" + index, 0, 0, 0,
|
| - ContentSuggestionsCardLayout.FULL_CARD));
|
| - }
|
| - return suggestions;
|
| + public void testChangeNotifications() {
|
| + FakeSuggestionsSource suggestionsSource = spy(new FakeSuggestionsSource());
|
| + // Allow using dismissSuggestion() instead of throwing UnsupportedOperationException.
|
| + doNothing().when(suggestionsSource).dismissSuggestion(any(SnippetArticle.class));
|
| +
|
| + registerCategory(suggestionsSource, KnownCategories.ARTICLES, 3);
|
| + NewTabPageAdapter adapter = spy(
|
| + new NewTabPageAdapter(new MockNewTabPageManager(suggestionsSource), null, null));
|
| + adapter.initializeSections();
|
| + doNothing().when(adapter).announceItemRemoved(anyString());
|
| +
|
| + // Adapter content:
|
| + // Idx | Item
|
| + // ----|----------------
|
| + // 0 | Above-the-fold
|
| + // 1 | Header
|
| + // 2-4 | Sugg*3
|
| + // 5 | Footer
|
| + // 6 | Spacer
|
| +
|
| + adapter.dismissItem(3); // Dismiss the second suggestion of the second section.
|
| + verify(adapter).notifyItemRemoved(3);
|
| +
|
| + // Make sure the call with the updated position works properly.
|
| + adapter.dismissItem(3);
|
| + verify(adapter, times(2)).notifyItemRemoved(3);
|
| +
|
| + // Dismiss the last suggestion in the section. We should now show the status card.
|
| + adapter.dismissItem(2);
|
| + verify(adapter).notifyItemRangeChanged(1, 2);
|
| + verify(adapter).notifyItemRangeInserted(3, 1);
|
| +
|
| + // Adapter content:
|
| + // Idx | Item
|
| + // ----|----------------
|
| + // 0 | Above-the-fold
|
| + // 1 | Header
|
| + // 2 | Status
|
| + // 3 | Progress Indicator
|
| + // 4 | Footer
|
| + // 5 | Spacer
|
| +
|
| + final int newSuggestionCount = 7;
|
| + suggestionsSource.setSuggestionsForCategory(
|
| + KnownCategories.ARTICLES, createDummySuggestions(newSuggestionCount));
|
| + adapter.onNewSuggestions(KnownCategories.ARTICLES);
|
| + verify(adapter).notifyItemRangeChanged(1, 3); // Header stays, 2 items replaced by articles.
|
| + verify(adapter).notifyItemRangeInserted(4, newSuggestionCount - 2);
|
| +
|
| + // Adapter content:
|
| + // Idx | Item
|
| + // ----|----------------
|
| + // 0 | Above-the-fold
|
| + // 1 | Header
|
| + // 2-8 | Sugg*7
|
| + // 9 | Footer
|
| + // 10 | Spacer
|
| +
|
| + adapter.onCategoryStatusChanged(KnownCategories.ARTICLES, CategoryStatus.SIGNED_OUT);
|
| + verify(adapter, times(2)).notifyItemRangeChanged(1, 3);
|
| + verify(adapter).notifyItemRangeRemoved(4, newSuggestionCount - 2);
|
| }
|
|
|
| /** Registers the category with hasMoreButton=false and showIfEmpty=true*/
|
| @@ -900,5 +930,9 @@ public void onLearnMoreClicked() {
|
| @Nullable public SuggestionsSource getSuggestionsSource() {
|
| return mSuggestionsSource;
|
| }
|
| +
|
| + public void setSuggestionsSource(SuggestionsSource suggestionsSource) {
|
| + mSuggestionsSource = suggestionsSource;
|
| + }
|
| }
|
| }
|
|
|