Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1352)

Unified Diff: chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapterTest.java

Issue 2470913009: 📰 Refactor SuggestionsSection change detection (Closed)
Patch Set: address comments Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 40725d84112e51e7cfbf4e16ec606198a46f9937..d1babe6bafb820beb46a74a8f94fd60d6de08d4c 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
@@ -85,11 +85,14 @@
private static class SectionDescriptor {
public final boolean mMoreButton;
public final boolean mStatusCard;
+ public boolean mProgressItem;
public final int mNumSuggestions;
- public SectionDescriptor(boolean moreButton, boolean statusCard, int numSuggestions) {
+ public SectionDescriptor(
+ boolean moreButton, boolean statusCard, boolean progressItem, int numSuggestions) {
mMoreButton = moreButton;
mStatusCard = statusCard;
+ mProgressItem = progressItem;
mNumSuggestions = numSuggestions;
if (statusCard) {
assertEquals(0, numSuggestions);
@@ -97,6 +100,11 @@ public SectionDescriptor(boolean moreButton, boolean statusCard, int numSuggesti
assertTrue(numSuggestions > 0);
}
}
+
+ public SectionDescriptor withProgress() {
+ mProgressItem = true;
+ return this;
+ }
}
/**
@@ -126,7 +134,6 @@ public void expect(SectionDescriptor descriptor) {
if (descriptor.mStatusCard) {
expect(ItemViewType.STATUS);
expect(ItemViewType.ACTION);
- expect(ItemViewType.PROGRESS);
} else {
for (int i = 1; i <= descriptor.mNumSuggestions; i++) {
expect(ItemViewType.SNIPPET);
@@ -135,6 +142,10 @@ public void expect(SectionDescriptor descriptor) {
expect(ItemViewType.ACTION);
}
}
+
+ if (descriptor.mProgressItem) {
+ expect(ItemViewType.PROGRESS);
+ }
}
public void expectPosition(int expectedPosition) {
@@ -184,7 +195,7 @@ private void assertItemsFor(SectionDescriptor... descriptors) {
*/
private SectionDescriptor section(int numSuggestions) {
assert numSuggestions > 0;
- return new SectionDescriptor(false, false, numSuggestions);
+ return new SectionDescriptor(false, false, false, numSuggestions);
}
/**
@@ -197,7 +208,7 @@ private SectionDescriptor section(int numSuggestions) {
* @return A descriptor for the section.
*/
private SectionDescriptor sectionWithMoreButton(int numSuggestions) {
- return new SectionDescriptor(true, false, numSuggestions);
+ return new SectionDescriptor(true, false, false, numSuggestions);
}
/**
@@ -206,7 +217,7 @@ private SectionDescriptor sectionWithMoreButton(int numSuggestions) {
* @return A descriptor for the section.
*/
private SectionDescriptor sectionWithStatusCard() {
- return new SectionDescriptor(false, true, 0);
+ return new SectionDescriptor(false, true, false, 0);
}
/**
@@ -215,7 +226,7 @@ private SectionDescriptor sectionWithStatusCard() {
* @return A descriptor for the section.
*/
private SectionDescriptor sectionWithStatusCardAndMoreButton() {
- return new SectionDescriptor(true, true, 0);
+ return new SectionDescriptor(true, true, false, 0);
}
@Before
@@ -259,7 +270,7 @@ public void tearDown() {
@Test
@Feature({"Ntp"})
public void testSuggestionLoading() {
- assertItemsFor(sectionWithStatusCard());
+ assertItemsFor(sectionWithStatusCard().withProgress());
final int numSuggestions = 3;
List<SnippetArticle> suggestions = createDummySuggestions(numSuggestions);
@@ -286,7 +297,7 @@ public void testSuggestionLoadingInitiallyEmpty() {
// If we don't get anything, we should be in the same situation as the initial one.
mSource.setSuggestionsForCategory(
KnownCategories.ARTICLES, new ArrayList<SnippetArticle>());
- assertItemsFor(sectionWithStatusCard());
+ assertItemsFor(sectionWithStatusCard().withProgress());
// We should load new suggestions when we get notified about them.
final int numSuggestions = 5;
@@ -360,7 +371,7 @@ public void testSuggestionLoadingBlock() {
// INITIALIZING lets us load snippets still.
mSource.setStatusForCategory(KnownCategories.ARTICLES, CategoryStatus.INITIALIZING);
mSource.setSuggestionsForCategory(KnownCategories.ARTICLES, suggestions);
- assertItemsFor(sectionWithStatusCard());
+ assertItemsFor(sectionWithStatusCard().withProgress());
// The adapter should now be waiting for new snippets and the fourth one should appear.
mSource.setStatusForCategory(KnownCategories.ARTICLES, CategoryStatus.AVAILABLE);
@@ -465,7 +476,7 @@ public void testSectionVisibleIfEmpty() {
// 1.1 - Initial state
when(mNewTabPageManager.getSuggestionsSource()).thenReturn(suggestionsSource);
mAdapter = new NewTabPageAdapter(mNewTabPageManager, null, null, mOfflinePageBridge);
- assertItemsFor(sectionWithStatusCard());
+ assertItemsFor(sectionWithStatusCard().withProgress());
// 1.2 - With suggestions
suggestionsSource.setStatusForCategory(category, CategoryStatus.AVAILABLE);
@@ -521,7 +532,7 @@ public void testMoreButton() {
// 1.1 - Initial state.
when(mNewTabPageManager.getSuggestionsSource()).thenReturn(suggestionsSource);
mAdapter = new NewTabPageAdapter(mNewTabPageManager, null, null, mOfflinePageBridge);
- assertItemsFor(sectionWithStatusCardAndMoreButton());
+ assertItemsFor(sectionWithStatusCardAndMoreButton().withProgress());
// 1.2 - With suggestions.
suggestionsSource.setStatusForCategory(category, CategoryStatus.AVAILABLE);
@@ -545,7 +556,7 @@ public void testMoreButton() {
// 2.1 - Initial state.
when(mNewTabPageManager.getSuggestionsSource()).thenReturn(suggestionsSource);
mAdapter = new NewTabPageAdapter(mNewTabPageManager, null, null, mOfflinePageBridge);
- assertItemsFor(sectionWithStatusCard());
+ assertItemsFor(sectionWithStatusCard().withProgress());
// 2.2 - With suggestions.
suggestionsSource.setStatusForCategory(category, CategoryStatus.AVAILABLE);
@@ -707,6 +718,7 @@ public void testChangeNotifications() {
mNewTabPageManager, null, null, mOfflinePageBridge);
AdapterDataObserver dataObserver = mock(AdapterDataObserver.class);
adapter.registerAdapterDataObserver(dataObserver);
+ reset(dataObserver); // reset notification changes from initialisation.
// Adapter content:
// Idx | Item
@@ -725,12 +737,16 @@ public void testChangeNotifications() {
adapter.dismissItem(3);
verify(dataObserver, times(2)).onItemRangeRemoved(3, 1);
verify(dataObserver).onItemRangeChanged(4, 1, null);
+ reset(dataObserver);
// Dismiss the last suggestion in the section. We should now show the status card.
adapter.dismissItem(2);
- verify(dataObserver).onItemRangeRemoved(2, 1);
- verify(dataObserver).onItemRangeInserted(2, 3);
- verify(dataObserver, times(2)).onItemRangeChanged(6, 1, null);
+ verify(dataObserver).onItemRangeRemoved(2, 1); // Suggestion removed
+ verify(dataObserver).onItemRangeChanged(3, 1, null); // Spacer refresh
+ verify(dataObserver).onItemRangeInserted(2, 1); // Status card added
+ verify(dataObserver).onItemRangeChanged(4, 1, null); // Spacer refresh
+ verify(dataObserver).onItemRangeInserted(3, 1); // Action item added
+ verify(dataObserver).onItemRangeChanged(5, 1, null); // Spacer refresh
// Adapter content:
// Idx | Item
@@ -744,14 +760,15 @@ public void testChangeNotifications() {
// 6 | Spacer
final int newSuggestionCount = 7;
- final int changedCount = 3; // status, action and progress will be replaced by articles.
+ reset(dataObserver);
suggestionsSource.setSuggestionsForCategory(
KnownCategories.ARTICLES, createDummySuggestions(newSuggestionCount));
adapter.onNewSuggestions(KnownCategories.ARTICLES);
- verify(dataObserver).onItemRangeChanged(2, changedCount, null);
- verify(dataObserver)
- .onItemRangeInserted(2 + changedCount, newSuggestionCount - changedCount);
- verify(dataObserver).onItemRangeChanged(newSuggestionCount + 3, 1, null);
+ verify(dataObserver).onItemRangeInserted(2, newSuggestionCount);
+ verify(dataObserver).onItemRangeChanged(5 + newSuggestionCount, 1, null); // Spacer refresh
+ verify(dataObserver, times(2)).onItemRangeRemoved(2 + newSuggestionCount, 1);
+ verify(dataObserver).onItemRangeChanged(4 + newSuggestionCount, 1, null); // Spacer refresh
+ verify(dataObserver).onItemRangeChanged(3 + newSuggestionCount, 1, null); // Spacer refresh
// Adapter content:
// Idx | Item
@@ -767,10 +784,12 @@ public void testChangeNotifications() {
suggestionsSource.setSuggestionsForCategory(
KnownCategories.ARTICLES, createDummySuggestions(0));
adapter.onCategoryStatusChanged(KnownCategories.ARTICLES, CategoryStatus.SIGNED_OUT);
- verify(dataObserver).onItemRangeChanged(2, changedCount, null);
- verify(dataObserver)
- .onItemRangeRemoved(2 + changedCount, newSuggestionCount - changedCount);
- verify(dataObserver).onItemRangeChanged(6, 1, null);
+ verify(dataObserver).onItemRangeRemoved(2, newSuggestionCount);
+ verify(dataObserver).onItemRangeChanged(3, 1, null); // Spacer refresh
+ verify(dataObserver).onItemRangeInserted(2, 1); // Status card added
+ verify(dataObserver).onItemRangeChanged(4, 1, null); // Spacer refresh
+ verify(dataObserver).onItemRangeInserted(3, 1); // Action item added
+ verify(dataObserver).onItemRangeChanged(5, 1, null); // Spacer refresh
}
@Test

Powered by Google App Engine
This is Rietveld 408576698