| Index: chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/SectionListTest.java
|
| diff --git a/chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/SectionListTest.java b/chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/SectionListTest.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..c23b99c6cd796eb2e7ccbd0d2cd34fbdc92b95e1
|
| --- /dev/null
|
| +++ b/chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/SectionListTest.java
|
| @@ -0,0 +1,155 @@
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +package org.chromium.chrome.browser.ntp.cards;
|
| +
|
| +import static org.hamcrest.CoreMatchers.equalTo;
|
| +import static org.junit.Assert.assertThat;
|
| +import static org.mockito.Mockito.when;
|
| +
|
| +import static org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils.createDummySuggestions;
|
| +import static org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils.registerCategory;
|
| +
|
| +import android.util.Pair;
|
| +
|
| +import org.junit.Before;
|
| +import org.junit.Test;
|
| +import org.junit.runner.RunWith;
|
| +import org.mockito.Mock;
|
| +import org.mockito.MockitoAnnotations;
|
| +import org.robolectric.annotation.Config;
|
| +import org.robolectric.annotation.Implementation;
|
| +import org.robolectric.annotation.Implements;
|
| +import org.robolectric.annotation.RealObject;
|
| +
|
| +import org.chromium.base.test.util.Feature;
|
| +import org.chromium.chrome.browser.ntp.NewTabPageView.NewTabPageManager;
|
| +import org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils.CategoryInfoBuilder;
|
| +import org.chromium.chrome.browser.ntp.cards.SectionListTest.ShadowPair;
|
| +import org.chromium.chrome.browser.ntp.snippets.FakeSuggestionsSource;
|
| +import org.chromium.chrome.browser.ntp.snippets.KnownCategories;
|
| +import org.chromium.chrome.browser.ntp.snippets.SnippetArticle;
|
| +import org.chromium.chrome.browser.offlinepages.OfflinePageBridge;
|
| +import org.chromium.chrome.browser.suggestions.SuggestionsMetricsReporter;
|
| +import org.chromium.testing.local.LocalRobolectricTestRunner;
|
| +
|
| +import java.util.List;
|
| +import java.util.Locale;
|
| +
|
| +/**
|
| + * Unit tests for {@link SuggestionsSection}.
|
| + */
|
| +@RunWith(LocalRobolectricTestRunner.class)
|
| +@Config(manifest = Config.NONE, shadows = {ShadowPair.class})
|
| +public class SectionListTest {
|
| + @Mock
|
| + private NewTabPageManager mNtpManager;
|
| + @Mock
|
| + private OfflinePageBridge mOfflinePageBridge;
|
| + @Mock
|
| + private SuggestionsMetricsReporter mMetricsReporter;
|
| + private FakeSuggestionsSource mSuggestionSource;
|
| +
|
| + @Before
|
| + public void setUp() {
|
| + MockitoAnnotations.initMocks(this);
|
| + mSuggestionSource = new FakeSuggestionsSource();
|
| +
|
| + when(mNtpManager.getSuggestionsSource()).thenReturn(mSuggestionSource);
|
| + when(mNtpManager.getSuggestionsMetricsReporter()).thenReturn(mMetricsReporter);
|
| + }
|
| +
|
| + @Test
|
| + @Feature({"Ntp"})
|
| + public void testGetSuggestionRank() {
|
| + // Setup the section list the following way:
|
| + //
|
| + // Item type | local rank | global rank
|
| + // -----------+------------+-------------
|
| + // HEADER | |
|
| + // ARTICLE | 0 | 0
|
| + // ARTICLE | 1 | 1
|
| + // ARTICLE | 2 | 2
|
| + // HEADER | |
|
| + // STATUS | |
|
| + // ACTION | 0 |
|
| + // BOOKMARK | 0 | 3
|
| + // BOOKMARK | 1 | 4
|
| + // BOOKMARK | 2 | 5
|
| + // BOOKMARK | 3 | 6
|
| + List<SnippetArticle> articles =
|
| + registerCategory(mSuggestionSource, KnownCategories.ARTICLES, 3);
|
| + registerCategory(mSuggestionSource, KnownCategories.DOWNLOADS, 0);
|
| + List<SnippetArticle> bookmarks =
|
| + registerCategory(mSuggestionSource, KnownCategories.BOOKMARKS, 4);
|
| +
|
| + SectionList sectionList = new SectionList(mNtpManager, mOfflinePageBridge);
|
| +
|
| + assertThat(sectionList.getSuggestionRank(articles.get(0)), equalTo(new Pair<>(0, 0)));
|
| + assertThat(sectionList.getSuggestionRank(articles.get(2)), equalTo(new Pair<>(2, 2)));
|
| + assertThat(sectionList.getSuggestionRank(bookmarks.get(1)), equalTo(new Pair<>(1, 4)));
|
| + }
|
| +
|
| + @Test(expected = IndexOutOfBoundsException.class)
|
| + @Feature({"Ntp"})
|
| + public void testGetSuggestionRankWithUnregisteredCategory() {
|
| + registerCategory(mSuggestionSource, KnownCategories.ARTICLES, 3);
|
| + SnippetArticle unregisteredSuggestion =
|
| + createDummySuggestions(1, KnownCategories.DOWNLOADS).get(0);
|
| +
|
| + SectionList sectionList = new SectionList(mNtpManager, mOfflinePageBridge);
|
| +
|
| + sectionList.getSuggestionRank(unregisteredSuggestion);
|
| + }
|
| +
|
| + @Test(expected = AssertionError.class)
|
| + @Feature({"Ntp"})
|
| + public void testGetSuggestionRankWithUnregisteredSuggestion() {
|
| + registerCategory(mSuggestionSource, KnownCategories.ARTICLES, 3);
|
| + SnippetArticle unregisteredSuggestion =
|
| + createDummySuggestions(5, KnownCategories.ARTICLES).get(4);
|
| +
|
| + SectionList sectionList = new SectionList(mNtpManager, mOfflinePageBridge);
|
| +
|
| + sectionList.getSuggestionRank(unregisteredSuggestion);
|
| + }
|
| +
|
| + @Test
|
| + @Feature({"Ntp"})
|
| + public void testGetActionItemRank() {
|
| + registerCategory(mSuggestionSource, KnownCategories.ARTICLES, 0);
|
| + registerCategory(mSuggestionSource,
|
| + new CategoryInfoBuilder(KnownCategories.DOWNLOADS).withViewAllAction().build(), 3);
|
| +
|
| + SectionList sectionList = new SectionList(mNtpManager, mOfflinePageBridge);
|
| +
|
| + assertThat(sectionList.getActionItemRank(KnownCategories.ARTICLES), equalTo(0));
|
| + assertThat(sectionList.getActionItemRank(KnownCategories.DOWNLOADS), equalTo(3));
|
| + }
|
| +
|
| + @Test(expected = AssertionError.class)
|
| + @Feature({"Ntp"})
|
| + public void testGetActionItemRankWhenInvisible() {
|
| + registerCategory(mSuggestionSource, KnownCategories.ARTICLES, 3);
|
| +
|
| + SectionList sectionList = new SectionList(mNtpManager, mOfflinePageBridge);
|
| +
|
| + sectionList.getActionItemRank(KnownCategories.ARTICLES);
|
| + }
|
| +
|
| + /**
|
| + * Shadow for {@link Pair} that allows to overriding {@link Pair#toString()} to make it useful
|
| + * in test failure messages.
|
| + */
|
| + @Implements(Pair.class)
|
| + public static class ShadowPair {
|
| + @RealObject
|
| + private Pair mRealPair;
|
| +
|
| + @Implementation
|
| + public String toString() {
|
| + return String.format(Locale.US, "{%s, %s}", mRealPair.first, mRealPair.second);
|
| + }
|
| + }
|
| +}
|
|
|