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 eea29e3317c6fa1dde201043d11dc13af870eb75..a496b755eface470e7984ecce199aa9fed75a4aa 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 |
@@ -33,6 +33,7 @@ |
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; |
@@ -57,7 +58,6 @@ |
import org.chromium.chrome.browser.signin.SigninManager.SignInStateObserver; |
import org.chromium.testing.local.LocalRobolectricTestRunner; |
-import java.nio.channels.UnsupportedAddressTypeException; |
import java.util.ArrayList; |
import java.util.Arrays; |
import java.util.Collections; |
@@ -120,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++) { |
@@ -225,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); |
} |
@@ -248,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)); |
} |
/** |
@@ -291,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)); |
} |
/** |
@@ -472,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 = |
@@ -481,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 = |
@@ -507,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 = |
@@ -530,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 = |
@@ -540,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 = |
@@ -566,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 = |
@@ -627,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 = |
@@ -638,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 = |
@@ -757,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 |
@@ -764,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 |
@@ -787,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 |
@@ -800,18 +759,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()); |
@@ -832,9 +792,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 |
@@ -842,14 +803,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()); |
@@ -857,7 +819,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*/ |
@@ -867,9 +829,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)); |
} |
@@ -1038,11 +998,7 @@ public void onLearnMoreClicked() { |
@Override |
public void closeContextMenu() { |
- throw new UnsupportedAddressTypeException(); |
- } |
- |
- public void setSuggestionsSource(SuggestionsSource suggestionsSource) { |
- mSuggestionsSource = suggestionsSource; |
+ throw new UnsupportedOperationException(); |
} |
@Override |