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

Side by Side Diff: chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/SectionListTest.java

Issue 2623993007: 🏠 Extract the ContentSuggestionManager interface from NTP (Closed)
Patch Set: aaaand rebase again Created 3 years, 11 months 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 unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.ntp.cards; 5 package org.chromium.chrome.browser.ntp.cards;
6 6
7 import static org.hamcrest.CoreMatchers.equalTo; 7 import static org.hamcrest.CoreMatchers.equalTo;
8 import static org.junit.Assert.assertThat; 8 import static org.junit.Assert.assertThat;
9 import static org.mockito.Mockito.mock; 9 import static org.mockito.Mockito.mock;
10 import static org.mockito.Mockito.when; 10 import static org.mockito.Mockito.when;
11 11
12 import static org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils. createDummySuggestions; 12 import static org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils. createDummySuggestions;
13 import static org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils. registerCategory; 13 import static org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils. registerCategory;
14 14
15 import org.junit.Before; 15 import org.junit.Before;
16 import org.junit.Test; 16 import org.junit.Test;
17 import org.junit.runner.RunWith; 17 import org.junit.runner.RunWith;
18 import org.mockito.Mock; 18 import org.mockito.Mock;
19 import org.mockito.MockitoAnnotations; 19 import org.mockito.MockitoAnnotations;
20 import org.robolectric.annotation.Config; 20 import org.robolectric.annotation.Config;
21 21
22 import org.chromium.base.Callback; 22 import org.chromium.base.Callback;
23 import org.chromium.base.test.util.Feature; 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; 24 import org.chromium.chrome.browser.ntp.cards.ContentSuggestionsTestUtils.Categor yInfoBuilder;
26 import org.chromium.chrome.browser.ntp.snippets.CategoryInt; 25 import org.chromium.chrome.browser.ntp.snippets.CategoryInt;
27 import org.chromium.chrome.browser.ntp.snippets.FakeSuggestionsSource; 26 import org.chromium.chrome.browser.ntp.snippets.FakeSuggestionsSource;
28 import org.chromium.chrome.browser.ntp.snippets.KnownCategories; 27 import org.chromium.chrome.browser.ntp.snippets.KnownCategories;
29 import org.chromium.chrome.browser.ntp.snippets.SectionHeaderViewHolder; 28 import org.chromium.chrome.browser.ntp.snippets.SectionHeaderViewHolder;
30 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; 29 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle;
31 import org.chromium.chrome.browser.ntp.snippets.SnippetArticleViewHolder; 30 import org.chromium.chrome.browser.ntp.snippets.SnippetArticleViewHolder;
32 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; 31 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge;
33 import org.chromium.chrome.browser.suggestions.SuggestionsMetricsReporter; 32 import org.chromium.chrome.browser.suggestions.SuggestionsMetricsReporter;
33 import org.chromium.chrome.browser.suggestions.SuggestionsUiDelegate;
34 import org.chromium.testing.local.LocalRobolectricTestRunner; 34 import org.chromium.testing.local.LocalRobolectricTestRunner;
35 35
36 import java.util.Collections; 36 import java.util.Collections;
37 import java.util.List; 37 import java.util.List;
38 38
39 /** 39 /**
40 * Unit tests for {@link SuggestionsSection}. 40 * Unit tests for {@link SuggestionsSection}.
41 */ 41 */
42 @RunWith(LocalRobolectricTestRunner.class) 42 @RunWith(LocalRobolectricTestRunner.class)
43 @Config(manifest = Config.NONE) 43 @Config(manifest = Config.NONE)
44 public class SectionListTest { 44 public class SectionListTest {
45 @Mock 45 @Mock
46 private NewTabPageManager mNtpManager; 46 private SuggestionsUiDelegate mUiDelegate;
47 @Mock 47 @Mock
48 private OfflinePageBridge mOfflinePageBridge; 48 private OfflinePageBridge mOfflinePageBridge;
49 @Mock 49 @Mock
50 private SuggestionsMetricsReporter mMetricsReporter; 50 private SuggestionsMetricsReporter mMetricsReporter;
51 private FakeSuggestionsSource mSuggestionSource; 51 private FakeSuggestionsSource mSuggestionSource;
52 52
53 @Before 53 @Before
54 public void setUp() { 54 public void setUp() {
55 MockitoAnnotations.initMocks(this); 55 MockitoAnnotations.initMocks(this);
56 mSuggestionSource = new FakeSuggestionsSource(); 56 mSuggestionSource = new FakeSuggestionsSource();
57 57
58 when(mNtpManager.getSuggestionsSource()).thenReturn(mSuggestionSource); 58 when(mUiDelegate.getSuggestionsSource()).thenReturn(mSuggestionSource);
59 when(mNtpManager.getSuggestionsMetricsReporter()).thenReturn(mMetricsRep orter); 59 when(mUiDelegate.getMetricsReporter()).thenReturn(mMetricsReporter);
60 } 60 }
61 61
62 @Test 62 @Test
63 @Feature({"Ntp"}) 63 @Feature({"Ntp"})
64 public void testGetSuggestionRank() { 64 public void testGetSuggestionRank() {
65 // Setup the section list the following way: 65 // Setup the section list the following way:
66 // 66 //
67 // Item type | local rank | global rank 67 // Item type | local rank | global rank
68 // -----------+------------+------------- 68 // -----------+------------+-------------
69 // HEADER | | 69 // HEADER | |
70 // ARTICLE | 0 | 0 70 // ARTICLE | 0 | 0
71 // ARTICLE | 1 | 1 71 // ARTICLE | 1 | 1
72 // ARTICLE | 2 | 2 72 // ARTICLE | 2 | 2
73 // HEADER | | 73 // HEADER | |
74 // STATUS | | 74 // STATUS | |
75 // ACTION | 0 | 75 // ACTION | 0 |
76 // BOOKMARK | 0 | 3 76 // BOOKMARK | 0 | 3
77 // BOOKMARK | 1 | 4 77 // BOOKMARK | 1 | 4
78 // BOOKMARK | 2 | 5 78 // BOOKMARK | 2 | 5
79 // BOOKMARK | 3 | 6 79 // BOOKMARK | 3 | 6
80 List<SnippetArticle> articles = 80 List<SnippetArticle> articles =
81 registerCategory(mSuggestionSource, KnownCategories.ARTICLES, 3) ; 81 registerCategory(mSuggestionSource, KnownCategories.ARTICLES, 3) ;
82 registerCategory(mSuggestionSource, KnownCategories.DOWNLOADS, 0); 82 registerCategory(mSuggestionSource, KnownCategories.DOWNLOADS, 0);
83 List<SnippetArticle> bookmarks = 83 List<SnippetArticle> bookmarks =
84 registerCategory(mSuggestionSource, KnownCategories.BOOKMARKS, 4 ); 84 registerCategory(mSuggestionSource, KnownCategories.BOOKMARKS, 4 );
85 85
86 SectionList sectionList = new SectionList(mNtpManager, mOfflinePageBridg e); 86 SectionList sectionList = new SectionList(mUiDelegate, mOfflinePageBridg e);
87 87
88 bindViewHolders(sectionList); 88 bindViewHolders(sectionList);
89 89
90 assertThat(articles.get(0).getGlobalRank(), equalTo(0)); 90 assertThat(articles.get(0).getGlobalRank(), equalTo(0));
91 assertThat(articles.get(0).getPerSectionRank(), equalTo(0)); 91 assertThat(articles.get(0).getPerSectionRank(), equalTo(0));
92 assertThat(articles.get(2).getGlobalRank(), equalTo(2)); 92 assertThat(articles.get(2).getGlobalRank(), equalTo(2));
93 assertThat(articles.get(2).getPerSectionRank(), equalTo(2)); 93 assertThat(articles.get(2).getPerSectionRank(), equalTo(2));
94 assertThat(bookmarks.get(1).getGlobalRank(), equalTo(4)); 94 assertThat(bookmarks.get(1).getGlobalRank(), equalTo(4));
95 assertThat(bookmarks.get(1).getPerSectionRank(), equalTo(1)); 95 assertThat(bookmarks.get(1).getPerSectionRank(), equalTo(1));
96 } 96 }
(...skipping 15 matching lines...) Expand all
112 // BOOKMARK | 0 | 3 112 // BOOKMARK | 0 | 3
113 // BOOKMARK | 1 | 4 113 // BOOKMARK | 1 | 4
114 // BOOKMARK | 2 | 5 114 // BOOKMARK | 2 | 5
115 // BOOKMARK | 3 | 6 115 // BOOKMARK | 3 | 6
116 List<SnippetArticle> articles = 116 List<SnippetArticle> articles =
117 registerCategory(mSuggestionSource, KnownCategories.ARTICLES, 3) ; 117 registerCategory(mSuggestionSource, KnownCategories.ARTICLES, 3) ;
118 registerCategory(mSuggestionSource, KnownCategories.DOWNLOADS, 0); 118 registerCategory(mSuggestionSource, KnownCategories.DOWNLOADS, 0);
119 List<SnippetArticle> bookmarks = 119 List<SnippetArticle> bookmarks =
120 registerCategory(mSuggestionSource, KnownCategories.BOOKMARKS, 4 ); 120 registerCategory(mSuggestionSource, KnownCategories.BOOKMARKS, 4 );
121 121
122 SectionList sectionList = new SectionList(mNtpManager, mOfflinePageBridg e); 122 SectionList sectionList = new SectionList(mUiDelegate, mOfflinePageBridg e);
123 123
124 bindViewHolders(sectionList, 0, 5); // Bind until after the third articl e. 124 bindViewHolders(sectionList, 0, 5); // Bind until after the third articl e.
125 125
126 assertThat(articles.get(0).getGlobalRank(), equalTo(0)); 126 assertThat(articles.get(0).getGlobalRank(), equalTo(0));
127 assertThat(articles.get(0).getPerSectionRank(), equalTo(0)); 127 assertThat(articles.get(0).getPerSectionRank(), equalTo(0));
128 assertThat(articles.get(2).getGlobalRank(), equalTo(2)); 128 assertThat(articles.get(2).getGlobalRank(), equalTo(2));
129 assertThat(articles.get(2).getPerSectionRank(), equalTo(2)); 129 assertThat(articles.get(2).getPerSectionRank(), equalTo(2));
130 assertThat(bookmarks.get(1).getGlobalRank(), equalTo(-1)); // Not bound nor ranked yet. 130 assertThat(bookmarks.get(1).getGlobalRank(), equalTo(-1)); // Not bound nor ranked yet.
131 assertThat(bookmarks.get(1).getPerSectionRank(), equalTo(-1)); 131 assertThat(bookmarks.get(1).getPerSectionRank(), equalTo(-1));
132 132
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 assertThat(newArticles.get(1).getPerSectionRank(), equalTo(4)); 212 assertThat(newArticles.get(1).getPerSectionRank(), equalTo(4));
213 } 213 }
214 214
215 @Test 215 @Test
216 @Feature({"Ntp"}) 216 @Feature({"Ntp"})
217 public void testGetActionItemRank() { 217 public void testGetActionItemRank() {
218 registerCategory(mSuggestionSource, KnownCategories.ARTICLES, 0); 218 registerCategory(mSuggestionSource, KnownCategories.ARTICLES, 0);
219 registerCategory(mSuggestionSource, 219 registerCategory(mSuggestionSource,
220 new CategoryInfoBuilder(KnownCategories.DOWNLOADS).withViewAllAc tion().build(), 3); 220 new CategoryInfoBuilder(KnownCategories.DOWNLOADS).withViewAllAc tion().build(), 3);
221 221
222 SectionList sectionList = new SectionList(mNtpManager, mOfflinePageBridg e); 222 SectionList sectionList = new SectionList(mUiDelegate, mOfflinePageBridg e);
223 bindViewHolders(sectionList); 223 bindViewHolders(sectionList);
224 224
225 assertThat(sectionList.getSectionForTesting(KnownCategories.ARTICLES) 225 assertThat(sectionList.getSectionForTesting(KnownCategories.ARTICLES)
226 .getActionItem() 226 .getActionItem()
227 .getPerSectionRank(), 227 .getPerSectionRank(),
228 equalTo(0)); 228 equalTo(0));
229 assertThat(sectionList.getSectionForTesting(KnownCategories.DOWNLOADS) 229 assertThat(sectionList.getSectionForTesting(KnownCategories.DOWNLOADS)
230 .getActionItem() 230 .getActionItem()
231 .getPerSectionRank(), 231 .getPerSectionRank(),
232 equalTo(3)); 232 equalTo(3));
(...skipping 20 matching lines...) Expand all
253 return mock(StatusCardViewHolder.class); 253 return mock(StatusCardViewHolder.class);
254 case ItemViewType.ACTION: 254 case ItemViewType.ACTION:
255 return mock(ActionItem.ViewHolder.class); 255 return mock(ActionItem.ViewHolder.class);
256 case ItemViewType.PROGRESS: 256 case ItemViewType.PROGRESS:
257 return mock(ProgressViewHolder.class); 257 return mock(ProgressViewHolder.class);
258 default: 258 default:
259 return mock(NewTabPageViewHolder.class); 259 return mock(NewTabPageViewHolder.class);
260 } 260 }
261 } 261 }
262 } 262 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698