OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.ntp.cards; |
| 6 |
| 7 import static org.hamcrest.CoreMatchers.equalTo; |
| 8 import static org.junit.Assert.assertThat; |
| 9 import static org.mockito.Mockito.mock; |
| 10 import static org.mockito.Mockito.when; |
| 11 |
| 12 import static org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils.
createDummySuggestions; |
| 13 import static org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils.
registerCategory; |
| 14 |
| 15 import org.junit.Before; |
| 16 import org.junit.Test; |
| 17 import org.junit.runner.RunWith; |
| 18 import org.mockito.Mock; |
| 19 import org.mockito.MockitoAnnotations; |
| 20 import org.robolectric.annotation.Config; |
| 21 |
| 22 import org.chromium.base.Callback; |
| 23 import org.chromium.base.test.util.Feature; |
| 24 import org.chromium.chrome.browser.ntp.NewTabPageView.NewTabPageManager; |
| 25 import org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils.Categor
yInfoBuilder; |
| 26 import org.chromium.chrome.browser.ntp.snippets.CategoryInt; |
| 27 import org.chromium.chrome.browser.ntp.snippets.FakeSuggestionsSource; |
| 28 import org.chromium.chrome.browser.ntp.snippets.KnownCategories; |
| 29 import org.chromium.chrome.browser.ntp.snippets.SectionHeaderViewHolder; |
| 30 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; |
| 31 import org.chromium.chrome.browser.ntp.snippets.SnippetArticleViewHolder; |
| 32 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; |
| 33 import org.chromium.chrome.browser.suggestions.SuggestionsMetricsReporter; |
| 34 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 35 |
| 36 import java.util.Collections; |
| 37 import java.util.List; |
| 38 |
| 39 /** |
| 40 * Unit tests for {@link SuggestionsSection}. |
| 41 */ |
| 42 @RunWith(LocalRobolectricTestRunner.class) |
| 43 @Config(manifest = Config.NONE) |
| 44 public class SectionListTest { |
| 45 @Mock |
| 46 private NewTabPageManager mNtpManager; |
| 47 @Mock |
| 48 private OfflinePageBridge mOfflinePageBridge; |
| 49 @Mock |
| 50 private SuggestionsMetricsReporter mMetricsReporter; |
| 51 private FakeSuggestionsSource mSuggestionSource; |
| 52 |
| 53 @Before |
| 54 public void setUp() { |
| 55 MockitoAnnotations.initMocks(this); |
| 56 mSuggestionSource = new FakeSuggestionsSource(); |
| 57 |
| 58 when(mNtpManager.getSuggestionsSource()).thenReturn(mSuggestionSource); |
| 59 when(mNtpManager.getSuggestionsMetricsReporter()).thenReturn(mMetricsRep
orter); |
| 60 } |
| 61 |
| 62 @Test |
| 63 @Feature({"Ntp"}) |
| 64 public void testGetSuggestionRank() { |
| 65 // Setup the section list the following way: |
| 66 // |
| 67 // Item type | local rank | global rank |
| 68 // -----------+------------+------------- |
| 69 // HEADER | | |
| 70 // ARTICLE | 0 | 0 |
| 71 // ARTICLE | 1 | 1 |
| 72 // ARTICLE | 2 | 2 |
| 73 // HEADER | | |
| 74 // STATUS | | |
| 75 // ACTION | 0 | |
| 76 // BOOKMARK | 0 | 3 |
| 77 // BOOKMARK | 1 | 4 |
| 78 // BOOKMARK | 2 | 5 |
| 79 // BOOKMARK | 3 | 6 |
| 80 List<SnippetArticle> articles = |
| 81 registerCategory(mSuggestionSource, KnownCategories.ARTICLES, 3)
; |
| 82 registerCategory(mSuggestionSource, KnownCategories.DOWNLOADS, 0); |
| 83 List<SnippetArticle> bookmarks = |
| 84 registerCategory(mSuggestionSource, KnownCategories.BOOKMARKS, 4
); |
| 85 |
| 86 SectionList sectionList = new SectionList(mNtpManager, mOfflinePageBridg
e); |
| 87 |
| 88 assertThat(articles.get(0).getGlobalRank(), equalTo(0)); |
| 89 assertThat(articles.get(0).getPerSectionRank(), equalTo(0)); |
| 90 assertThat(articles.get(2).getGlobalRank(), equalTo(2)); |
| 91 assertThat(articles.get(2).getPerSectionRank(), equalTo(2)); |
| 92 assertThat(bookmarks.get(1).getGlobalRank(), equalTo(4)); |
| 93 assertThat(bookmarks.get(1).getPerSectionRank(), equalTo(1)); |
| 94 |
| 95 // Test ranks after changes: remove then add some items. |
| 96 @SuppressWarnings("unchecked") |
| 97 Callback<String> cb = mock(Callback.class); |
| 98 sectionList.dismissItem(2, cb); |
| 99 |
| 100 // Using sublists to skip the first items and avoid using existing ones |
| 101 List<SnippetArticle> newArticles = |
| 102 createDummySuggestions(5, KnownCategories.ARTICLES).subList(3, 5
); |
| 103 List<SnippetArticle> newBookmarks = |
| 104 createDummySuggestions(6, KnownCategories.BOOKMARKS).subList(4,
6); |
| 105 |
| 106 sectionList.onMoreSuggestions(KnownCategories.ARTICLES, newArticles); |
| 107 sectionList.onMoreSuggestions(KnownCategories.BOOKMARKS, newBookmarks); |
| 108 |
| 109 // After the changes we should have: |
| 110 // Item type | local rank | global rank |
| 111 // -----------+------------+------------- |
| 112 // HEADER | | |
| 113 // ARTICLE | 0 | 0 |
| 114 // ARTICLE | 1 | 1 |
| 115 // | - | - (deleted) |
| 116 // ARTICLE | 3 | 7 (new) |
| 117 // ARTICLE | 4 | 8 (new) |
| 118 // HEADER | | |
| 119 // STATUS | | |
| 120 // ACTION | 0 | |
| 121 // BOOKMARK | 0 | 3 |
| 122 // BOOKMARK | 1 | 4 |
| 123 // BOOKMARK | 2 | 5 |
| 124 // BOOKMARK | 3 | 6 |
| 125 // BOOKMARK | 4 | 9 (new) |
| 126 // BOOKMARK | 5 | 10 (new) |
| 127 |
| 128 // The old ranks should not change. |
| 129 assertThat(articles.get(0).getGlobalRank(), equalTo(0)); |
| 130 assertThat(articles.get(0).getPerSectionRank(), equalTo(0)); |
| 131 assertThat(articles.get(2).getGlobalRank(), equalTo(2)); |
| 132 assertThat(articles.get(2).getPerSectionRank(), equalTo(2)); |
| 133 assertThat(bookmarks.get(1).getGlobalRank(), equalTo(4)); |
| 134 assertThat(bookmarks.get(1).getPerSectionRank(), equalTo(1)); |
| 135 |
| 136 // New ranks take into account the previously existing items. |
| 137 assertThat(newArticles.get(1).getGlobalRank(), equalTo(8)); |
| 138 assertThat(newArticles.get(1).getPerSectionRank(), equalTo(4)); |
| 139 assertThat(newBookmarks.get(1).getGlobalRank(), equalTo(10)); |
| 140 assertThat(newBookmarks.get(1).getPerSectionRank(), equalTo(5)); |
| 141 } |
| 142 |
| 143 @Test |
| 144 @Feature({"Ntp"}) |
| 145 public void testGetActionItemRank() { |
| 146 registerCategory(mSuggestionSource, KnownCategories.ARTICLES, 0); |
| 147 registerCategory(mSuggestionSource, |
| 148 new CategoryInfoBuilder(KnownCategories.DOWNLOADS).withViewAllAc
tion().build(), 3); |
| 149 |
| 150 SectionList sectionList = new SectionList(mNtpManager, mOfflinePageBridg
e); |
| 151 bindViewHolders(sectionList); |
| 152 |
| 153 assertThat(sectionList.getSectionForTesting(KnownCategories.ARTICLES) |
| 154 .getActionItem() |
| 155 .getPerSectionRank(), |
| 156 equalTo(0)); |
| 157 assertThat(sectionList.getSectionForTesting(KnownCategories.DOWNLOADS) |
| 158 .getActionItem() |
| 159 .getPerSectionRank(), |
| 160 equalTo(3)); |
| 161 } |
| 162 |
| 163 private static void bindViewHolders(InnerNode node) { |
| 164 bindViewHolders(node, 0, node.getItemCount()); |
| 165 } |
| 166 |
| 167 private static void bindViewHolders(InnerNode node, int startIndex, int endI
ndex) { |
| 168 for (int i = startIndex; i < endIndex; ++i) { |
| 169 node.onBindViewHolder( |
| 170 makeViewHolder(node.getItemViewType(i)), i, Collections.empt
yList()); |
| 171 } |
| 172 } |
| 173 |
| 174 private static NewTabPageViewHolder makeViewHolder(@CategoryInt int viewType
) { |
| 175 switch (viewType) { |
| 176 case ItemViewType.SNIPPET: |
| 177 return mock(SnippetArticleViewHolder.class); |
| 178 case ItemViewType.HEADER: |
| 179 return mock(SectionHeaderViewHolder.class); |
| 180 case ItemViewType.STATUS: |
| 181 return mock(StatusCardViewHolder.class); |
| 182 case ItemViewType.ACTION: |
| 183 return mock(ActionItem.ViewHolder.class); |
| 184 case ItemViewType.PROGRESS: |
| 185 return mock(ProgressViewHolder.class); |
| 186 default: |
| 187 return mock(NewTabPageViewHolder.class); |
| 188 } |
| 189 } |
| 190 } |
OLD | NEW |