| 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 c2e8c066bf5fc05525ec5b7766231e08af757b21..779707497126d88dd6424ec0e614eb52dbef1405 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
|
| @@ -7,28 +7,29 @@ package org.chromium.chrome.browser.ntp.cards;
|
| import static org.junit.Assert.assertEquals;
|
| import static org.junit.Assert.assertFalse;
|
| import static org.junit.Assert.assertTrue;
|
| -import static org.mockito.Matchers.any;
|
| -import static org.mockito.Mockito.doAnswer;
|
| -import static org.mockito.Mockito.mock;
|
|
|
| +import android.graphics.Bitmap;
|
| +
|
| +import org.chromium.base.Callback;
|
| import org.chromium.base.metrics.RecordHistogram;
|
| import org.chromium.base.test.util.Feature;
|
| -import org.chromium.chrome.browser.ntp.NewTabPageView.NewTabPageManager;
|
| import org.chromium.chrome.browser.ntp.snippets.CategoryStatus;
|
| +import org.chromium.chrome.browser.ntp.snippets.CategoryStatus.CategoryStatusEnum;
|
| +import org.chromium.chrome.browser.ntp.snippets.KnownCategories;
|
| +import org.chromium.chrome.browser.ntp.snippets.KnownCategories.KnownCategoriesEnum;
|
| import org.chromium.chrome.browser.ntp.snippets.SnippetArticleListItem;
|
| -import org.chromium.chrome.browser.ntp.snippets.SnippetsBridge;
|
| -import org.chromium.chrome.browser.ntp.snippets.SnippetsSource.SnippetsObserver;
|
| +import org.chromium.chrome.browser.ntp.snippets.SnippetsSource;
|
| import org.chromium.testing.local.LocalRobolectricTestRunner;
|
| import org.junit.Before;
|
| import org.junit.Test;
|
| import org.junit.runner.RunWith;
|
| -import org.mockito.invocation.InvocationOnMock;
|
| -import org.mockito.stubbing.Answer;
|
| import org.robolectric.annotation.Config;
|
|
|
| import java.util.ArrayList;
|
| import java.util.Arrays;
|
| +import java.util.HashMap;
|
| import java.util.List;
|
| +import java.util.Map;
|
|
|
|
|
| /**
|
| @@ -37,25 +38,58 @@ import java.util.List;
|
| @RunWith(LocalRobolectricTestRunner.class)
|
| @Config(manifest = Config.NONE)
|
| public class NewTabPageAdapterTest {
|
| + private static class FakeSnippetsSource implements SnippetsSource {
|
| + private SnippetsSource.SnippetsObserver mObserver;
|
| + private final Map<Integer, Integer> mCategoryStatus = new HashMap<>();
|
| +
|
| + public void setStatusForCategory(@KnownCategoriesEnum int category,
|
| + @CategoryStatusEnum int status) {
|
| + mCategoryStatus.put(category, status);
|
| + if (mObserver != null) mObserver.onCategoryStatusChanged(category, status);
|
| + }
|
| +
|
| + public void setSnippetsForCategory(@KnownCategoriesEnum int category,
|
| + List<SnippetArticleListItem> snippets) {
|
| + mObserver.onSuggestionsReceived(category, snippets);
|
| + }
|
| +
|
| + @Override
|
| + public void discardSnippet(SnippetArticleListItem snippet) {
|
| + throw new UnsupportedOperationException();
|
| + }
|
| +
|
| + @Override
|
| + public void fetchSnippetImage(SnippetArticleListItem snippet, Callback<Bitmap> callback) {
|
| + throw new UnsupportedOperationException();
|
| + }
|
| +
|
| + @Override
|
| + public void getSnippedVisited(SnippetArticleListItem snippet, Callback<Boolean> callback) {
|
| + throw new UnsupportedOperationException();
|
| + }
|
| +
|
| + @Override
|
| + public void setObserver(SnippetsObserver observer) {
|
| + mObserver = observer;
|
| + }
|
| +
|
| + @CategoryStatusEnum
|
| + @Override
|
| + public int getCategoryStatus(@KnownCategoriesEnum int category) {
|
| + return mCategoryStatus.get(category);
|
| + }
|
| + }
|
|
|
| - private NewTabPageManager mNewTabPageManager;
|
| - private SnippetsObserver mSnippetsObserver;
|
| - private SnippetsBridge mSnippetsBridge;
|
| + private FakeSnippetsSource mSnippetsSource = new FakeSnippetsSource();
|
| + private NewTabPageAdapter mNtpAdapter;
|
|
|
| @Before
|
| public void setUp() {
|
| RecordHistogram.disableForTests();
|
| - mNewTabPageManager = mock(NewTabPageManager.class);
|
| - mSnippetsObserver = null;
|
| - mSnippetsBridge = mock(SnippetsBridge.class);
|
| -
|
| - // Intercept the observers so that we can mock invocations.
|
| - doAnswer(new Answer<Void>() {
|
| - @Override
|
| - public Void answer(InvocationOnMock invocation) throws Throwable {
|
| - mSnippetsObserver = invocation.getArgumentAt(0, SnippetsObserver.class);
|
| - return null;
|
| - }}).when(mSnippetsBridge).setObserver(any(SnippetsObserver.class));
|
| +
|
| + mSnippetsSource = new FakeSnippetsSource();
|
| + mSnippetsSource.setStatusForCategory(KnownCategories.ARTICLES, CategoryStatus.INITIALIZING);
|
| + mNtpAdapter = new NewTabPageAdapter(null, null, mSnippetsSource, null);
|
| }
|
|
|
| /**
|
| @@ -65,31 +99,28 @@ public class NewTabPageAdapterTest {
|
| @Test
|
| @Feature({"Ntp"})
|
| public void testSnippetLoading() {
|
| - NewTabPageAdapter ntpa =
|
| - new NewTabPageAdapter(mNewTabPageManager, null, mSnippetsBridge, null);
|
| -
|
| - assertEquals(5, ntpa.getItemCount());
|
| - assertEquals(NewTabPageListItem.VIEW_TYPE_ABOVE_THE_FOLD, ntpa.getItemViewType(0));
|
| - assertEquals(NewTabPageListItem.VIEW_TYPE_HEADER, ntpa.getItemViewType(1));
|
| - assertEquals(NewTabPageListItem.VIEW_TYPE_STATUS, ntpa.getItemViewType(2));
|
| - assertEquals(NewTabPageListItem.VIEW_TYPE_PROGRESS, ntpa.getItemViewType(3));
|
| - assertEquals(NewTabPageListItem.VIEW_TYPE_SPACING, ntpa.getItemViewType(4));
|
| + assertEquals(5, mNtpAdapter.getItemCount());
|
| + assertEquals(NewTabPageListItem.VIEW_TYPE_ABOVE_THE_FOLD, mNtpAdapter.getItemViewType(0));
|
| + assertEquals(NewTabPageListItem.VIEW_TYPE_HEADER, mNtpAdapter.getItemViewType(1));
|
| + assertEquals(NewTabPageListItem.VIEW_TYPE_STATUS, mNtpAdapter.getItemViewType(2));
|
| + assertEquals(NewTabPageListItem.VIEW_TYPE_PROGRESS, mNtpAdapter.getItemViewType(3));
|
| + assertEquals(NewTabPageListItem.VIEW_TYPE_SPACING, mNtpAdapter.getItemViewType(4));
|
|
|
| List<SnippetArticleListItem> snippets = createDummySnippets();
|
| - mSnippetsObserver.onSnippetsReceived(snippets);
|
| + mSnippetsSource.setSnippetsForCategory(KnownCategories.ARTICLES, snippets);
|
|
|
| - List<NewTabPageListItem> loadedItems = new ArrayList<>(ntpa.getItemsForTesting());
|
| - assertEquals(NewTabPageListItem.VIEW_TYPE_ABOVE_THE_FOLD, ntpa.getItemViewType(0));
|
| - assertEquals(NewTabPageListItem.VIEW_TYPE_HEADER, ntpa.getItemViewType(1));
|
| + List<NewTabPageListItem> loadedItems = new ArrayList<>(mNtpAdapter.getItems());
|
| + assertEquals(NewTabPageListItem.VIEW_TYPE_ABOVE_THE_FOLD, mNtpAdapter.getItemViewType(0));
|
| + assertEquals(NewTabPageListItem.VIEW_TYPE_HEADER, mNtpAdapter.getItemViewType(1));
|
| assertEquals(snippets, loadedItems.subList(2, loadedItems.size() - 1));
|
| - assertEquals(
|
| - NewTabPageListItem.VIEW_TYPE_SPACING, ntpa.getItemViewType(loadedItems.size() - 1));
|
| + assertEquals(NewTabPageListItem.VIEW_TYPE_SPACING,
|
| + mNtpAdapter.getItemViewType(loadedItems.size() - 1));
|
|
|
| // The adapter should ignore any new incoming data.
|
| - mSnippetsObserver.onSnippetsReceived(
|
| - Arrays.asList(new SnippetArticleListItem[] {new SnippetArticleListItem(
|
| - "foo", "title1", "pub1", "txt1", "foo", "bar", 0, 0, 0)}));
|
| - assertEquals(loadedItems, ntpa.getItemsForTesting());
|
| + mSnippetsSource.setSnippetsForCategory(KnownCategories.ARTICLES,
|
| + Arrays.asList(new SnippetArticleListItem[] { new SnippetArticleListItem(
|
| + "foo", "title1", "pub1", "txt1", "foo", "bar", 0, 0, 0) }));
|
| + assertEquals(loadedItems, mNtpAdapter.getItems());
|
| }
|
|
|
| /**
|
| @@ -99,33 +130,32 @@ public class NewTabPageAdapterTest {
|
| @Test
|
| @Feature({"Ntp"})
|
| public void testSnippetLoadingInitiallyEmpty() {
|
| - NewTabPageAdapter ntpa =
|
| - new NewTabPageAdapter(mNewTabPageManager, null, mSnippetsBridge, null);
|
| -
|
| // If we don't get anything, we should be in the same situation as the initial one.
|
| - mSnippetsObserver.onSnippetsReceived(new ArrayList<SnippetArticleListItem>());
|
| - assertEquals(5, ntpa.getItemCount());
|
| - assertEquals(NewTabPageListItem.VIEW_TYPE_ABOVE_THE_FOLD, ntpa.getItemViewType(0));
|
| - assertEquals(NewTabPageListItem.VIEW_TYPE_HEADER, ntpa.getItemViewType(1));
|
| - assertEquals(NewTabPageListItem.VIEW_TYPE_STATUS, ntpa.getItemViewType(2));
|
| - assertEquals(NewTabPageListItem.VIEW_TYPE_PROGRESS, ntpa.getItemViewType(3));
|
| - assertEquals(NewTabPageListItem.VIEW_TYPE_SPACING, ntpa.getItemViewType(4));
|
| + mSnippetsSource.setSnippetsForCategory(KnownCategories.ARTICLES,
|
| + new ArrayList<SnippetArticleListItem>());
|
| + assertEquals(5, mNtpAdapter.getItemCount());
|
| + assertEquals(NewTabPageListItem.VIEW_TYPE_ABOVE_THE_FOLD, mNtpAdapter.getItemViewType(0));
|
| + assertEquals(NewTabPageListItem.VIEW_TYPE_HEADER, mNtpAdapter.getItemViewType(1));
|
| + assertEquals(NewTabPageListItem.VIEW_TYPE_STATUS, mNtpAdapter.getItemViewType(2));
|
| + assertEquals(NewTabPageListItem.VIEW_TYPE_PROGRESS, mNtpAdapter.getItemViewType(3));
|
| + assertEquals(NewTabPageListItem.VIEW_TYPE_SPACING, mNtpAdapter.getItemViewType(4));
|
|
|
| // We should load new snippets when we get notified about them.
|
| List<SnippetArticleListItem> snippets = createDummySnippets();
|
| - mSnippetsObserver.onSnippetsReceived(snippets);
|
| - List<NewTabPageListItem> loadedItems = new ArrayList<>(ntpa.getItemsForTesting());
|
| - assertEquals(NewTabPageListItem.VIEW_TYPE_ABOVE_THE_FOLD, ntpa.getItemViewType(0));
|
| - assertEquals(NewTabPageListItem.VIEW_TYPE_HEADER, ntpa.getItemViewType(1));
|
| + mSnippetsSource.setSnippetsForCategory(KnownCategories.ARTICLES, snippets);
|
| + List<NewTabPageListItem> loadedItems = new ArrayList<>(mNtpAdapter.getItems());
|
| + assertEquals(NewTabPageListItem.VIEW_TYPE_ABOVE_THE_FOLD, mNtpAdapter.getItemViewType(0));
|
| + assertEquals(NewTabPageListItem.VIEW_TYPE_HEADER, mNtpAdapter.getItemViewType(1));
|
| assertEquals(snippets, loadedItems.subList(2, loadedItems.size() - 1));
|
| - assertEquals(
|
| - NewTabPageListItem.VIEW_TYPE_SPACING, ntpa.getItemViewType(loadedItems.size() - 1));
|
| + assertEquals(NewTabPageListItem.VIEW_TYPE_SPACING,
|
| + mNtpAdapter.getItemViewType(loadedItems.size() - 1));
|
|
|
| // The adapter should ignore any new incoming data.
|
| - mSnippetsObserver.onSnippetsReceived(
|
| + mSnippetsSource.setSnippetsForCategory(
|
| + KnownCategories.ARTICLES,
|
| Arrays.asList(new SnippetArticleListItem[] {new SnippetArticleListItem(
|
| "foo", "title1", "pub1", "txt1", "foo", "bar", 0, 0, 0)}));
|
| - assertEquals(loadedItems, ntpa.getItemsForTesting());
|
| + assertEquals(loadedItems, mNtpAdapter.getItems());
|
| }
|
|
|
| /**
|
| @@ -134,27 +164,27 @@ public class NewTabPageAdapterTest {
|
| @Test
|
| @Feature({"Ntp"})
|
| public void testSnippetClearing() {
|
| - NewTabPageAdapter ntpa =
|
| - new NewTabPageAdapter(mNewTabPageManager, null, mSnippetsBridge, null);
|
| -
|
| List<SnippetArticleListItem> snippets = createDummySnippets();
|
| - mSnippetsObserver.onSnippetsReceived(snippets);
|
| - assertEquals(3 + snippets.size(), ntpa.getItemCount());
|
| + mSnippetsSource.setSnippetsForCategory(KnownCategories.ARTICLES, snippets);
|
| + assertEquals(3 + snippets.size(), mNtpAdapter.getItemCount());
|
|
|
| // If we get told that snippets are enabled, we just leave the current
|
| // ones there and not clear.
|
| - mSnippetsObserver.onCategoryStatusChanged(CategoryStatus.AVAILABLE);
|
| - assertEquals(3 + snippets.size(), ntpa.getItemCount());
|
| + mSnippetsSource.setStatusForCategory(KnownCategories.ARTICLES,
|
| + CategoryStatus.AVAILABLE);
|
| + assertEquals(3 + snippets.size(), mNtpAdapter.getItemCount());
|
|
|
| // When snippets are disabled, we clear them and we should go back to
|
| // the situation with the status card.
|
| - mSnippetsObserver.onCategoryStatusChanged(CategoryStatus.SIGNED_OUT);
|
| - assertEquals(5, ntpa.getItemCount());
|
| + mSnippetsSource.setStatusForCategory(KnownCategories.ARTICLES,
|
| + CategoryStatus.SIGNED_OUT);
|
| + assertEquals(5, mNtpAdapter.getItemCount());
|
|
|
| // The adapter should now be waiting for new snippets.
|
| - mSnippetsObserver.onCategoryStatusChanged(CategoryStatus.AVAILABLE);
|
| - mSnippetsObserver.onSnippetsReceived(snippets);
|
| - assertEquals(3 + snippets.size(), ntpa.getItemCount());
|
| + mSnippetsSource.setStatusForCategory(KnownCategories.ARTICLES,
|
| + CategoryStatus.AVAILABLE);
|
| + mSnippetsSource.setSnippetsForCategory(KnownCategories.ARTICLES, snippets);
|
| + assertEquals(3 + snippets.size(), mNtpAdapter.getItemCount());
|
| }
|
|
|
| /**
|
| @@ -163,35 +193,35 @@ public class NewTabPageAdapterTest {
|
| @Test
|
| @Feature({"Ntp"})
|
| public void testSnippetLoadingBlock() {
|
| - NewTabPageAdapter ntpa =
|
| - new NewTabPageAdapter(mNewTabPageManager, null, mSnippetsBridge, null);
|
| -
|
| List<SnippetArticleListItem> snippets = createDummySnippets();
|
|
|
| // By default, status is INITIALIZING, so we can load snippets
|
| - mSnippetsObserver.onSnippetsReceived(snippets);
|
| - assertEquals(3 + snippets.size(), ntpa.getItemCount());
|
| + mSnippetsSource.setSnippetsForCategory(KnownCategories.ARTICLES, snippets);
|
| + assertEquals(3 + snippets.size(), mNtpAdapter.getItemCount());
|
|
|
| // If we have snippets, we should not load the new list.
|
| snippets.add(new SnippetArticleListItem("https://site.com/url1", "title1", "pub1", "txt1",
|
| "https://site.com/url1", "https://amp.site.com/url1", 0, 0, 0));
|
| - mSnippetsObserver.onSnippetsReceived(snippets);
|
| - assertEquals(3 + snippets.size() - 1, ntpa.getItemCount());
|
| + mSnippetsSource.setSnippetsForCategory(KnownCategories.ARTICLES, snippets);
|
| + assertEquals(3 + snippets.size() - 1, mNtpAdapter.getItemCount());
|
|
|
| // When snippets are disabled, we should not be able to load them
|
| - mSnippetsObserver.onCategoryStatusChanged(CategoryStatus.SIGNED_OUT);
|
| - mSnippetsObserver.onSnippetsReceived(snippets);
|
| - assertEquals(5, ntpa.getItemCount());
|
| + mSnippetsSource.setStatusForCategory(KnownCategories.ARTICLES,
|
| + CategoryStatus.SIGNED_OUT);
|
| + mSnippetsSource.setSnippetsForCategory(KnownCategories.ARTICLES, snippets);
|
| + assertEquals(5, mNtpAdapter.getItemCount());
|
|
|
| // INITIALIZING lets us load snippets still.
|
| - mSnippetsObserver.onCategoryStatusChanged(CategoryStatus.INITIALIZING);
|
| - mSnippetsObserver.onSnippetsReceived(snippets);
|
| - assertEquals(3 + snippets.size(), ntpa.getItemCount());
|
| + mSnippetsSource.setStatusForCategory(KnownCategories.ARTICLES,
|
| + CategoryStatus.INITIALIZING);
|
| + mSnippetsSource.setSnippetsForCategory(KnownCategories.ARTICLES, snippets);
|
| + assertEquals(3 + snippets.size(), mNtpAdapter.getItemCount());
|
|
|
| // The adapter should now be waiting for new snippets.
|
| - mSnippetsObserver.onCategoryStatusChanged(CategoryStatus.AVAILABLE);
|
| - mSnippetsObserver.onSnippetsReceived(snippets);
|
| - assertEquals(3 + snippets.size(), ntpa.getItemCount());
|
| + mSnippetsSource.setStatusForCategory(KnownCategories.ARTICLES,
|
| + CategoryStatus.AVAILABLE);
|
| + mSnippetsSource.setSnippetsForCategory(KnownCategories.ARTICLES, snippets);
|
| + assertEquals(3 + snippets.size(), mNtpAdapter.getItemCount());
|
| }
|
|
|
| /**
|
| @@ -200,30 +230,35 @@ public class NewTabPageAdapterTest {
|
| @Test
|
| @Feature({"Ntp"})
|
| public void testProgressIndicatorDisplay() {
|
| - NewTabPageAdapter ntpa =
|
| - new NewTabPageAdapter(mNewTabPageManager, null, mSnippetsBridge, null);
|
| - int progressPos = ntpa.getBottomSpacerPosition() - 1;
|
| - ProgressListItem progress = (ProgressListItem) ntpa.getItemsForTesting().get(progressPos);
|
| + int progressPos = mNtpAdapter.getBottomSpacerPosition() - 1;
|
| + ProgressListItem progress = (ProgressListItem) mNtpAdapter.getItems().get(progressPos);
|
|
|
| - mSnippetsObserver.onCategoryStatusChanged(CategoryStatus.INITIALIZING);
|
| + mSnippetsSource.setStatusForCategory(KnownCategories.ARTICLES,
|
| + CategoryStatus.INITIALIZING);
|
| assertTrue(progress.isVisible());
|
|
|
| - mSnippetsObserver.onCategoryStatusChanged(CategoryStatus.AVAILABLE);
|
| + mSnippetsSource.setStatusForCategory(KnownCategories.ARTICLES,
|
| + CategoryStatus.AVAILABLE);
|
| assertFalse(progress.isVisible());
|
|
|
| - mSnippetsObserver.onCategoryStatusChanged(CategoryStatus.AVAILABLE_LOADING);
|
| + mSnippetsSource.setStatusForCategory(KnownCategories.ARTICLES,
|
| + CategoryStatus.AVAILABLE_LOADING);
|
| assertTrue(progress.isVisible());
|
|
|
| - mSnippetsObserver.onCategoryStatusChanged(CategoryStatus.NOT_PROVIDED);
|
| + mSnippetsSource.setStatusForCategory(KnownCategories.ARTICLES,
|
| + CategoryStatus.NOT_PROVIDED);
|
| assertFalse(progress.isVisible());
|
|
|
| - mSnippetsObserver.onCategoryStatusChanged(CategoryStatus.CATEGORY_EXPLICITLY_DISABLED);
|
| + mSnippetsSource.setStatusForCategory(KnownCategories.ARTICLES,
|
| + CategoryStatus.CATEGORY_EXPLICITLY_DISABLED);
|
| assertFalse(progress.isVisible());
|
|
|
| - mSnippetsObserver.onCategoryStatusChanged(CategoryStatus.SIGNED_OUT);
|
| + mSnippetsSource.setStatusForCategory(KnownCategories.ARTICLES,
|
| + CategoryStatus.SIGNED_OUT);
|
| assertFalse(progress.isVisible());
|
|
|
| - mSnippetsObserver.onCategoryStatusChanged(CategoryStatus.LOADING_ERROR);
|
| + mSnippetsSource.setStatusForCategory(KnownCategories.ARTICLES,
|
| + CategoryStatus.LOADING_ERROR);
|
| assertFalse(progress.isVisible());
|
| }
|
|
|
|
|