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 891ca5b649b4673df33f359f92bc43b4779bf301..dda4635bab6dc2891bb365b2a442734f508e142e 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,16 +4,13 @@ |
package org.chromium.chrome.browser.ntp.cards; |
-import static org.chromium.base.test.util.Matchers.greaterThanOrEqualTo; |
-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.assertThat; |
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.ArgumentMatchers.any; |
+import static org.mockito.ArgumentMatchers.anyString; |
import static org.mockito.Mockito.doNothing; |
import static org.mockito.Mockito.mock; |
import static org.mockito.Mockito.spy; |
@@ -26,6 +23,18 @@ |
import android.view.Menu; |
import android.view.MenuItem.OnMenuItemClickListener; |
+import org.junit.After; |
+import org.junit.Before; |
+import org.junit.Test; |
+import org.junit.runner.RunWith; |
+import org.robolectric.RuntimeEnvironment; |
+import org.robolectric.annotation.Config; |
+ |
+import static org.chromium.base.test.util.Matchers.greaterThanOrEqualTo; |
+import static org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils |
+ .createDummySuggestions; |
+import static org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils.createInfo; |
+ |
import org.chromium.base.Callback; |
import org.chromium.base.metrics.RecordHistogram; |
import org.chromium.base.metrics.RecordUserAction; |
@@ -48,14 +57,7 @@ |
import org.chromium.chrome.browser.signin.SigninManager; |
import org.chromium.chrome.browser.signin.SigninManager.SignInStateObserver; |
import org.chromium.testing.local.LocalRobolectricTestRunner; |
-import org.junit.After; |
-import org.junit.Before; |
-import org.junit.Test; |
-import org.junit.runner.RunWith; |
-import org.robolectric.RuntimeEnvironment; |
-import org.robolectric.annotation.Config; |
-import java.nio.channels.UnsupportedAddressTypeException; |
import java.util.ArrayList; |
import java.util.Arrays; |
import java.util.Collections; |
@@ -118,9 +120,7 @@ public void expect(SectionDescriptor descriptor) { |
expect(NewTabPageItem.VIEW_TYPE_HEADER); |
if (descriptor.mStatusCard) { |
expect(NewTabPageItem.VIEW_TYPE_STATUS); |
- if (descriptor.mMoreButton) { |
- expect(NewTabPageItem.VIEW_TYPE_ACTION); |
- } |
+ expect(NewTabPageItem.VIEW_TYPE_ACTION); |
expect(NewTabPageItem.VIEW_TYPE_PROGRESS); |
} else { |
for (int i = 1; i <= descriptor.mNumSuggestions; i++) { |
@@ -223,11 +223,11 @@ public void setUp() { |
RecordHistogram.disableForTests(); |
RecordUserAction.disableForTests(); |
+ @CategoryInt |
+ final int category = KnownCategories.ARTICLES; |
mSource = new FakeSuggestionsSource(); |
- mSource.setStatusForCategory(KnownCategories.ARTICLES, CategoryStatus.INITIALIZING); |
- mSource.setInfoForCategory(KnownCategories.ARTICLES, |
- new SuggestionsCategoryInfo("Articles for you", |
- ContentSuggestionsCardLayout.FULL_CARD, false, true)); |
+ mSource.setStatusForCategory(category, CategoryStatus.INITIALIZING); |
+ mSource.setInfoForCategory(category, createInfo(category, false, true)); |
mAdapter = NewTabPageAdapter.create(new MockNewTabPageManager(mSource), null, null); |
} |
@@ -246,36 +246,20 @@ public void tearDown() { |
@Feature({"Ntp"}) |
public void testSuggestionLoading() { |
assertItemsFor(sectionWithStatusCard()); |
- assertEquals(NewTabPageItem.VIEW_TYPE_ABOVE_THE_FOLD, mAdapter.getItemViewType(0)); |
- assertEquals(NewTabPageItem.VIEW_TYPE_HEADER, mAdapter.getItemViewType(1)); |
- assertEquals(NewTabPageItem.VIEW_TYPE_STATUS, mAdapter.getItemViewType(2)); |
- assertEquals(NewTabPageItem.VIEW_TYPE_PROGRESS, mAdapter.getItemViewType(3)); |
- assertEquals(NewTabPageItem.VIEW_TYPE_FOOTER, mAdapter.getItemViewType(4)); |
- assertEquals(NewTabPageItem.VIEW_TYPE_SPACING, mAdapter.getItemViewType(5)); |
- List<SnippetArticle> suggestions = createDummySuggestions(3); |
+ final int numSuggestions = 3; |
+ List<SnippetArticle> suggestions = createDummySuggestions(numSuggestions); |
mSource.setStatusForCategory(KnownCategories.ARTICLES, CategoryStatus.AVAILABLE); |
mSource.setSuggestionsForCategory(KnownCategories.ARTICLES, suggestions); |
- int numItems = mAdapter.getItemCount(); |
- |
- // From the loaded items, cut out aboveTheFold and header from the front, |
- // and footer and bottom spacer from the back. |
- assertEquals(NewTabPageItem.VIEW_TYPE_ABOVE_THE_FOLD, mAdapter.getItemViewType(0)); |
- assertEquals(NewTabPageItem.VIEW_TYPE_HEADER, mAdapter.getItemViewType(1)); |
- assertArticlesEqual(suggestions, 2, numItems - 2); |
- assertEquals(NewTabPageItem.VIEW_TYPE_FOOTER, mAdapter.getItemViewType(numItems - 2)); |
- assertEquals(NewTabPageItem.VIEW_TYPE_SPACING, mAdapter.getItemViewType(numItems - 1)); |
+ assertItemsFor(section(numSuggestions)); |
// The adapter should ignore any new incoming data. |
mSource.setSuggestionsForCategory(KnownCategories.ARTICLES, |
Arrays.asList(new SnippetArticle[] {new SnippetArticle(0, "foo", "title1", "pub1", |
"txt1", "foo", "bar", 0, 0, 0, ContentSuggestionsCardLayout.FULL_CARD)})); |
- assertEquals(NewTabPageItem.VIEW_TYPE_ABOVE_THE_FOLD, mAdapter.getItemViewType(0)); |
- assertEquals(NewTabPageItem.VIEW_TYPE_HEADER, mAdapter.getItemViewType(1)); |
- assertArticlesEqual(suggestions, 2, numItems - 2); |
- assertEquals(NewTabPageItem.VIEW_TYPE_FOOTER, mAdapter.getItemViewType(numItems - 2)); |
- assertEquals(NewTabPageItem.VIEW_TYPE_SPACING, mAdapter.getItemViewType(numItems - 1)); |
+ |
+ assertItemsFor(section(numSuggestions)); |
} |
/** |
@@ -289,37 +273,20 @@ public void testSuggestionLoadingInitiallyEmpty() { |
mSource.setSuggestionsForCategory( |
KnownCategories.ARTICLES, new ArrayList<SnippetArticle>()); |
assertItemsFor(sectionWithStatusCard()); |
- assertEquals(NewTabPageItem.VIEW_TYPE_ABOVE_THE_FOLD, mAdapter.getItemViewType(0)); |
- assertEquals(NewTabPageItem.VIEW_TYPE_HEADER, mAdapter.getItemViewType(1)); |
- assertEquals(NewTabPageItem.VIEW_TYPE_STATUS, mAdapter.getItemViewType(2)); |
- assertEquals(NewTabPageItem.VIEW_TYPE_PROGRESS, mAdapter.getItemViewType(3)); |
- assertEquals(NewTabPageItem.VIEW_TYPE_FOOTER, mAdapter.getItemViewType(4)); |
- assertEquals(NewTabPageItem.VIEW_TYPE_SPACING, mAdapter.getItemViewType(5)); |
// We should load new suggestions when we get notified about them. |
- List<SnippetArticle> suggestions = createDummySuggestions(5); |
+ final int numSuggestions = 5; |
+ List<SnippetArticle> suggestions = createDummySuggestions(numSuggestions); |
mSource.setStatusForCategory(KnownCategories.ARTICLES, CategoryStatus.AVAILABLE); |
mSource.setSuggestionsForCategory(KnownCategories.ARTICLES, suggestions); |
- int numItems = mAdapter.getItemCount(); |
- |
- // From the loaded items, cut out aboveTheFold and header from the front, |
- // and footer and bottom spacer from the back. |
- assertEquals(NewTabPageItem.VIEW_TYPE_ABOVE_THE_FOLD, mAdapter.getItemViewType(0)); |
- assertEquals(NewTabPageItem.VIEW_TYPE_HEADER, mAdapter.getItemViewType(1)); |
- assertArticlesEqual(suggestions, 2, numItems - 2); |
- assertEquals(NewTabPageItem.VIEW_TYPE_FOOTER, mAdapter.getItemViewType(numItems - 2)); |
- assertEquals(NewTabPageItem.VIEW_TYPE_SPACING, mAdapter.getItemViewType(numItems - 1)); |
+ assertItemsFor(section(numSuggestions)); |
// The adapter should ignore any new incoming data. |
mSource.setSuggestionsForCategory(KnownCategories.ARTICLES, |
Arrays.asList(new SnippetArticle[] {new SnippetArticle(0, "foo", "title1", "pub1", |
"txt1", "foo", "bar", 0, 0, 0, ContentSuggestionsCardLayout.FULL_CARD)})); |
- assertEquals(NewTabPageItem.VIEW_TYPE_ABOVE_THE_FOLD, mAdapter.getItemViewType(0)); |
- assertEquals(NewTabPageItem.VIEW_TYPE_HEADER, mAdapter.getItemViewType(1)); |
- assertArticlesEqual(suggestions, 2, numItems - 2); |
- assertEquals(NewTabPageItem.VIEW_TYPE_FOOTER, mAdapter.getItemViewType(numItems - 2)); |
- assertEquals(NewTabPageItem.VIEW_TYPE_SPACING, mAdapter.getItemViewType(numItems - 1)); |
+ assertItemsFor(section(numSuggestions)); |
} |
/** |
@@ -470,6 +437,7 @@ public void testUIUntouchedWhenNotProvided() { |
@Test |
@Feature({"Ntp"}) |
public void testSectionVisibleIfEmpty() { |
+ @CategoryInt |
final int category = 42; |
final int sectionIdx = 1; // section 0 is the above-the-fold item, we test the one after. |
final List<SnippetArticle> articles = |
@@ -479,9 +447,7 @@ public void testSectionVisibleIfEmpty() { |
// Part 1: VisibleIfEmpty = true |
suggestionsSource = new FakeSuggestionsSource(); |
suggestionsSource.setStatusForCategory(category, CategoryStatus.INITIALIZING); |
- suggestionsSource.setInfoForCategory( |
- category, new SuggestionsCategoryInfo( |
- "", ContentSuggestionsCardLayout.MINIMAL_CARD, false, true)); |
+ suggestionsSource.setInfoForCategory(category, createInfo(category, false, true)); |
// 1.1 - Initial state |
mAdapter = |
@@ -505,9 +471,7 @@ public void testSectionVisibleIfEmpty() { |
// Part 2: VisibleIfEmpty = false |
suggestionsSource = new FakeSuggestionsSource(); |
suggestionsSource.setStatusForCategory(category, CategoryStatus.INITIALIZING); |
- suggestionsSource.setInfoForCategory( |
- category, new SuggestionsCategoryInfo( |
- "", ContentSuggestionsCardLayout.MINIMAL_CARD, false, false)); |
+ suggestionsSource.setInfoForCategory(category, createInfo(category, false, false)); |
// 2.1 - Initial state |
mAdapter = |
@@ -528,6 +492,7 @@ public void testSectionVisibleIfEmpty() { |
@Test |
@Feature({"Ntp"}) |
public void testMoreButton() { |
+ @CategoryInt |
final int category = 42; |
final int sectionIdx = 1; // section 0 is the above the fold, we test the one after. |
final List<SnippetArticle> articles = |
@@ -538,9 +503,7 @@ public void testMoreButton() { |
// Part 1: ShowMoreButton = true |
suggestionsSource = new FakeSuggestionsSource(); |
suggestionsSource.setStatusForCategory(category, CategoryStatus.INITIALIZING); |
- suggestionsSource.setInfoForCategory( |
- category, new SuggestionsCategoryInfo( |
- "", ContentSuggestionsCardLayout.MINIMAL_CARD, true, true)); |
+ suggestionsSource.setInfoForCategory(category, createInfo(category, true, true)); |
// 1.1 - Initial state. |
mAdapter = |
@@ -564,9 +527,7 @@ public void testMoreButton() { |
// Part 1: ShowMoreButton = false |
suggestionsSource = new FakeSuggestionsSource(); |
suggestionsSource.setStatusForCategory(category, CategoryStatus.INITIALIZING); |
- suggestionsSource.setInfoForCategory( |
- category, new SuggestionsCategoryInfo( |
- "", ContentSuggestionsCardLayout.MINIMAL_CARD, false, true)); |
+ suggestionsSource.setInfoForCategory(category, createInfo(category, false, true)); |
// 2.1 - Initial state. |
mAdapter = |
@@ -625,9 +586,7 @@ public void testDynamicCategories() { |
int dynamicCategory1 = 1010; |
List<SnippetArticle> dynamics1 = createDummySuggestions(5); |
- mSource.setInfoForCategory( |
- dynamicCategory1, new SuggestionsCategoryInfo("Dynamic 1", |
- ContentSuggestionsCardLayout.MINIMAL_CARD, true, false)); |
+ mSource.setInfoForCategory(dynamicCategory1, createInfo(dynamicCategory1, true, false)); |
mSource.setStatusForCategory(dynamicCategory1, CategoryStatus.AVAILABLE); |
mSource.setSuggestionsForCategory(dynamicCategory1, dynamics1); |
mAdapter = |
@@ -636,9 +595,7 @@ public void testDynamicCategories() { |
int dynamicCategory2 = 1011; |
List<SnippetArticle> dynamics2 = createDummySuggestions(11); |
- mSource.setInfoForCategory( |
- dynamicCategory2, new SuggestionsCategoryInfo("Dynamic 2", |
- ContentSuggestionsCardLayout.MINIMAL_CARD, false, false)); |
+ mSource.setInfoForCategory(dynamicCategory2, createInfo(dynamicCategory1, false, false)); |
mSource.setStatusForCategory(dynamicCategory2, CategoryStatus.AVAILABLE); |
mSource.setSuggestionsForCategory(dynamicCategory2, dynamics2); |
mAdapter = |
@@ -755,6 +712,7 @@ public void testChangeNotifications() { |
verify(adapter).notifyItemRemoved(2); |
verify(adapter).notifyItemInserted(2); |
verify(adapter).notifyItemInserted(3); |
+ verify(adapter).notifyItemInserted(4); |
// Adapter content: |
// Idx | Item |
@@ -762,16 +720,19 @@ public void testChangeNotifications() { |
// 0 | Above-the-fold |
// 1 | Header |
// 2 | Status |
- // 3 | Progress Indicator |
- // 4 | Footer |
- // 5 | Spacer |
+ // 3 | Action |
+ // 4 | Progress Indicator |
+ // 5 | Footer |
+ // 6 | Spacer |
final int newSuggestionCount = 7; |
+ final int changedCount = 3; // status, action and progress will be replaced by articles. |
suggestionsSource.setSuggestionsForCategory( |
KnownCategories.ARTICLES, createDummySuggestions(newSuggestionCount)); |
adapter.onNewSuggestions(KnownCategories.ARTICLES); |
- verify(adapter).notifyItemRangeChanged(2, 2); // status and progress replaced by articles. |
- verify(adapter).notifyItemRangeInserted(4, newSuggestionCount - 2); |
+ verify(adapter).notifyItemRangeChanged(2, changedCount); |
+ verify(adapter).notifyItemRangeInserted( |
+ 2 + changedCount, newSuggestionCount - changedCount); |
// Adapter content: |
// Idx | Item |
@@ -785,8 +746,8 @@ public void testChangeNotifications() { |
suggestionsSource.setSuggestionsForCategory( |
KnownCategories.ARTICLES, createDummySuggestions(0)); |
adapter.onCategoryStatusChanged(KnownCategories.ARTICLES, CategoryStatus.SIGNED_OUT); |
- verify(adapter, times(2)).notifyItemRangeChanged(2, 2); |
- verify(adapter).notifyItemRangeRemoved(4, newSuggestionCount - 2); |
+ verify(adapter, times(2)).notifyItemRangeChanged(2, changedCount); |
+ verify(adapter).notifyItemRangeRemoved(2 + changedCount, newSuggestionCount - changedCount); |
} |
@Test |
@@ -797,18 +758,19 @@ public void testSigninPromo() { |
NewTabPageAdapter adapter = NewTabPageAdapter.create(ntpManager, null, null); |
assertEquals(5, adapter.getGroups().size()); |
- ItemGroup signinPromoGroup = adapter.getGroup(4); |
+ ItemGroup signinPromoGroup = adapter.getGroup(5); |
// Adapter content: |
- // Idx | Item |
- // ----|---------------- |
- // 0 | Above-the-fold |
- // 1 | Header |
- // 2 | Status |
- // 3 | Progress Indicator |
- // 4 | Sign in promo |
- // 5 | Footer |
- // 6 | Spacer |
+ // Idx | Item | Group Index |
+ // ----|--------------------|------------- |
+ // 0 | Above-the-fold | 0 |
+ // 1 | Header | 1 |
+ // 2 | Status | 1 |
+ // 3 | Action | 1 |
+ // 4 | Progress Indicator | 1 |
+ // 5 | Sign in promo | 2 |
+ // 6 | Footer | 3 |
+ // 7 | Spacer | 4 |
assertEquals(1, signinPromoGroup.getItems().size()); |
assertEquals(NewTabPageItem.VIEW_TYPE_PROMO, signinPromoGroup.getItems().get(0).getType()); |
@@ -828,9 +790,10 @@ public void testSigninPromoDismissal() { |
.setNewTabPageSigninPromoDismissed(false); |
MockNewTabPageManager ntpManager = new MockNewTabPageManager(mSource); |
NewTabPageAdapter adapter = NewTabPageAdapter.create(ntpManager, null, null); |
+ final int signInPromoIndex = 5; |
assertEquals(5, adapter.getGroups().size()); |
- ItemGroup signinPromoGroup = adapter.getGroup(4); |
+ ItemGroup signinPromoGroup = adapter.getGroup(signInPromoIndex); |
// Adapter content: |
// Idx | Item |
@@ -838,14 +801,15 @@ public void testSigninPromoDismissal() { |
// 0 | Above-the-fold |
// 1 | Header |
// 2 | Status |
- // 3 | Progress Indicator |
- // 4 | Sign in promo |
- // 5 | Footer |
- // 6 | Spacer |
+ // 3 | Action |
+ // 4 | Progress Indicator |
+ // 5 | Sign in promo |
+ // 6 | Footer |
+ // 7 | Spacer |
assertEquals(NewTabPageItem.VIEW_TYPE_PROMO, signinPromoGroup.getItems().get(0).getType()); |
- adapter.dismissItem(4); |
+ adapter.dismissItem(signInPromoIndex); |
assertTrue(signinPromoGroup.getItems().isEmpty()); |
assertTrue(ChromePreferenceManager.getInstance(RuntimeEnvironment.application) |
.getNewTabPageSigninPromoDismissed()); |
@@ -853,7 +817,7 @@ public void testSigninPromoDismissal() { |
adapter = NewTabPageAdapter.create(ntpManager, null, null); |
assertEquals(5, adapter.getGroups().size()); |
// The items below the signin promo move up, footer is now at the position of the promo. |
- assertEquals(NewTabPageItem.VIEW_TYPE_FOOTER, adapter.getItemViewType(4)); |
+ assertEquals(NewTabPageItem.VIEW_TYPE_FOOTER, adapter.getItemViewType(signInPromoIndex)); |
} |
/** Registers the category with hasMoreButton=false and showIfEmpty=true*/ |
@@ -863,9 +827,7 @@ private void registerCategory(FakeSuggestionsSource suggestionsSource, |
// AVAILABLE. |
suggestionsSource.setStatusForCategory(category, CategoryStatus.AVAILABLE); |
// Important: showIfEmpty flag to true. |
- suggestionsSource.setInfoForCategory( |
- category, new SuggestionsCategoryInfo( |
- "", ContentSuggestionsCardLayout.FULL_CARD, false, true)); |
+ suggestionsSource.setInfoForCategory(category, createInfo(category, false, true)); |
suggestionsSource.setSuggestionsForCategory( |
category, createDummySuggestions(suggestionCount)); |
} |
@@ -1034,11 +996,7 @@ public void onLearnMoreClicked() { |
@Override |
public void closeContextMenu() { |
- throw new UnsupportedAddressTypeException(); |
- } |
- |
- public void setSuggestionsSource(SuggestionsSource suggestionsSource) { |
- mSuggestionsSource = suggestionsSource; |
+ throw new UnsupportedOperationException(); |
} |
@Override |