OLD | NEW |
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; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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(mNtpManager, mOfflinePageBridg
e); |
87 | 87 |
| 88 bindViewHolders(sectionList); |
| 89 |
88 assertThat(articles.get(0).getGlobalRank(), equalTo(0)); | 90 assertThat(articles.get(0).getGlobalRank(), equalTo(0)); |
89 assertThat(articles.get(0).getPerSectionRank(), equalTo(0)); | 91 assertThat(articles.get(0).getPerSectionRank(), equalTo(0)); |
90 assertThat(articles.get(2).getGlobalRank(), equalTo(2)); | 92 assertThat(articles.get(2).getGlobalRank(), equalTo(2)); |
91 assertThat(articles.get(2).getPerSectionRank(), equalTo(2)); | 93 assertThat(articles.get(2).getPerSectionRank(), equalTo(2)); |
92 assertThat(bookmarks.get(1).getGlobalRank(), equalTo(4)); | 94 assertThat(bookmarks.get(1).getGlobalRank(), equalTo(4)); |
93 assertThat(bookmarks.get(1).getPerSectionRank(), equalTo(1)); | 95 assertThat(bookmarks.get(1).getPerSectionRank(), equalTo(1)); |
| 96 } |
| 97 |
| 98 @Test |
| 99 @Feature({"Ntp"}) |
| 100 public void testGetSuggestionRankWithChanges() { |
| 101 // Setup the section list the following way: |
| 102 // |
| 103 // Item type | local rank | global rank |
| 104 // -----------+------------+------------- |
| 105 // HEADER | | |
| 106 // ARTICLE | 0 | 0 |
| 107 // ARTICLE | 1 | 1 |
| 108 // ARTICLE | 2 | 2 |
| 109 // HEADER | | |
| 110 // STATUS | | |
| 111 // ACTION | 0 | |
| 112 // BOOKMARK | 0 | 3 |
| 113 // BOOKMARK | 1 | 4 |
| 114 // BOOKMARK | 2 | 5 |
| 115 // BOOKMARK | 3 | 6 |
| 116 List<SnippetArticle> articles = |
| 117 registerCategory(mSuggestionSource, KnownCategories.ARTICLES, 3)
; |
| 118 registerCategory(mSuggestionSource, KnownCategories.DOWNLOADS, 0); |
| 119 List<SnippetArticle> bookmarks = |
| 120 registerCategory(mSuggestionSource, KnownCategories.BOOKMARKS, 4
); |
| 121 |
| 122 SectionList sectionList = new SectionList(mNtpManager, mOfflinePageBridg
e); |
| 123 |
| 124 bindViewHolders(sectionList, 0, 5); // Bind until after the third articl
e. |
| 125 |
| 126 assertThat(articles.get(0).getGlobalRank(), equalTo(0)); |
| 127 assertThat(articles.get(0).getPerSectionRank(), equalTo(0)); |
| 128 assertThat(articles.get(2).getGlobalRank(), equalTo(2)); |
| 129 assertThat(articles.get(2).getPerSectionRank(), equalTo(2)); |
| 130 assertThat(bookmarks.get(1).getGlobalRank(), equalTo(-1)); // Not bound
nor ranked yet. |
| 131 assertThat(bookmarks.get(1).getPerSectionRank(), equalTo(-1)); |
| 132 |
94 | 133 |
95 // Test ranks after changes: remove then add some items. | 134 // Test ranks after changes: remove then add some items. |
96 @SuppressWarnings("unchecked") | 135 @SuppressWarnings("unchecked") |
97 Callback<String> cb = mock(Callback.class); | 136 Callback<String> cb = mock(Callback.class); |
98 sectionList.dismissItem(2, cb); | 137 sectionList.dismissItem(2, cb); |
99 | 138 |
100 // Using sublists to skip the first items and avoid using existing ones | 139 // Using sublists to skip the first items and avoid using existing ones |
101 List<SnippetArticle> newArticles = | 140 List<SnippetArticle> newArticles = |
102 createDummySuggestions(5, KnownCategories.ARTICLES).subList(3, 5
); | 141 createDummySuggestions(5, KnownCategories.ARTICLES).subList(3, 5
); |
103 List<SnippetArticle> newBookmarks = | 142 List<SnippetArticle> newBookmarks = |
104 createDummySuggestions(6, KnownCategories.BOOKMARKS).subList(4,
6); | 143 createDummySuggestions(6, KnownCategories.BOOKMARKS).subList(4,
6); |
105 | 144 |
106 sectionList.onMoreSuggestions(KnownCategories.ARTICLES, newArticles); | 145 sectionList.onMoreSuggestions(KnownCategories.ARTICLES, newArticles.subL
ist(0, 1)); |
107 sectionList.onMoreSuggestions(KnownCategories.BOOKMARKS, newBookmarks); | 146 sectionList.onMoreSuggestions(KnownCategories.BOOKMARKS, newBookmarks); |
108 | 147 |
| 148 bindViewHolders(sectionList, 3, sectionList.getItemCount()); |
| 149 |
109 // After the changes we should have: | 150 // After the changes we should have: |
110 // Item type | local rank | global rank | 151 // Item type | local rank | global rank |
111 // -----------+------------+------------- | 152 // -----------+------------+------------- |
112 // HEADER | | | 153 // HEADER | | |
113 // ARTICLE | 0 | 0 | 154 // ARTICLE | 0 | 0 |
114 // ARTICLE | 1 | 1 | 155 // ARTICLE | 1 | 1 |
115 // | - | - (deleted) | 156 // | - | - (deleted) |
116 // ARTICLE | 3 | 7 (new) | 157 // ARTICLE | 3 | 3 (new) |
117 // ARTICLE | 4 | 8 (new) | |
118 // HEADER | | | 158 // HEADER | | |
119 // STATUS | | | 159 // STATUS | | |
120 // ACTION | 0 | | 160 // ACTION | 0 | |
121 // BOOKMARK | 0 | 3 | 161 // BOOKMARK | 0 | 4 (old but not seen until now) |
122 // BOOKMARK | 1 | 4 | 162 // BOOKMARK | 1 | 5 (old but not seen until now) |
123 // BOOKMARK | 2 | 5 | 163 // BOOKMARK | 2 | 6 (old but not seen until now) |
124 // BOOKMARK | 3 | 6 | 164 // BOOKMARK | 3 | 7 (old but not seen until now) |
125 // BOOKMARK | 4 | 9 (new) | 165 // BOOKMARK | 4 | 8 (new) |
126 // BOOKMARK | 5 | 10 (new) | 166 // BOOKMARK | 5 | 9 (new) |
127 | 167 |
128 // The old ranks should not change. | 168 |
| 169 // The new article is seen first and gets the next global rank |
| 170 assertThat(newArticles.get(0).getGlobalRank(), equalTo(3)); |
| 171 assertThat(newArticles.get(0).getPerSectionRank(), equalTo(3)); |
| 172 |
| 173 // Bookmarks old and new are seen after the new article and have higher
global ranks |
| 174 assertThat(bookmarks.get(1).getGlobalRank(), equalTo(5)); |
| 175 assertThat(bookmarks.get(1).getPerSectionRank(), equalTo(1)); |
| 176 assertThat(newBookmarks.get(1).getGlobalRank(), equalTo(9)); |
| 177 assertThat(newBookmarks.get(1).getPerSectionRank(), equalTo(5)); |
| 178 |
| 179 // Add one more article |
| 180 sectionList.onMoreSuggestions(KnownCategories.ARTICLES, newArticles.subL
ist(1, 2)); |
| 181 bindViewHolders(sectionList); |
| 182 |
| 183 // After the changes we should have: |
| 184 // Item type | local rank | global rank |
| 185 // -----------+------------+------------- |
| 186 // HEADER | | |
| 187 // ARTICLE | 0 | 0 |
| 188 // ARTICLE | 1 | 1 |
| 189 // | - | - (deleted) |
| 190 // ARTICLE | 3 | 3 |
| 191 // ARTICLE | 4 | 10 (new) |
| 192 // HEADER | | |
| 193 // STATUS | | |
| 194 // ACTION | 0 | |
| 195 // BOOKMARK | 0 | 4 |
| 196 // BOOKMARK | 1 | 5 |
| 197 // BOOKMARK | 2 | 6 |
| 198 // BOOKMARK | 3 | 7 |
| 199 // BOOKMARK | 4 | 8 |
| 200 // BOOKMARK | 5 | 9 |
| 201 |
| 202 // Old suggestions' ranks should not change. |
129 assertThat(articles.get(0).getGlobalRank(), equalTo(0)); | 203 assertThat(articles.get(0).getGlobalRank(), equalTo(0)); |
130 assertThat(articles.get(0).getPerSectionRank(), equalTo(0)); | 204 assertThat(articles.get(0).getPerSectionRank(), equalTo(0)); |
131 assertThat(articles.get(2).getGlobalRank(), equalTo(2)); | 205 assertThat(articles.get(2).getGlobalRank(), equalTo(2)); |
132 assertThat(articles.get(2).getPerSectionRank(), equalTo(2)); | 206 assertThat(articles.get(2).getPerSectionRank(), equalTo(2)); |
133 assertThat(bookmarks.get(1).getGlobalRank(), equalTo(4)); | 207 assertThat(bookmarks.get(1).getGlobalRank(), equalTo(5)); |
134 assertThat(bookmarks.get(1).getPerSectionRank(), equalTo(1)); | 208 assertThat(bookmarks.get(1).getPerSectionRank(), equalTo(1)); |
135 | 209 |
136 // New ranks take into account the previously existing items. | 210 // The new article should get the last global rank |
137 assertThat(newArticles.get(1).getGlobalRank(), equalTo(8)); | 211 assertThat(newArticles.get(1).getGlobalRank(), equalTo(10)); |
138 assertThat(newArticles.get(1).getPerSectionRank(), equalTo(4)); | 212 assertThat(newArticles.get(1).getPerSectionRank(), equalTo(4)); |
139 assertThat(newBookmarks.get(1).getGlobalRank(), equalTo(10)); | |
140 assertThat(newBookmarks.get(1).getPerSectionRank(), equalTo(5)); | |
141 } | 213 } |
142 | 214 |
143 @Test | 215 @Test |
144 @Feature({"Ntp"}) | 216 @Feature({"Ntp"}) |
145 public void testGetActionItemRank() { | 217 public void testGetActionItemRank() { |
146 registerCategory(mSuggestionSource, KnownCategories.ARTICLES, 0); | 218 registerCategory(mSuggestionSource, KnownCategories.ARTICLES, 0); |
147 registerCategory(mSuggestionSource, | 219 registerCategory(mSuggestionSource, |
148 new CategoryInfoBuilder(KnownCategories.DOWNLOADS).withViewAllAc
tion().build(), 3); | 220 new CategoryInfoBuilder(KnownCategories.DOWNLOADS).withViewAllAc
tion().build(), 3); |
149 | 221 |
150 SectionList sectionList = new SectionList(mNtpManager, mOfflinePageBridg
e); | 222 SectionList sectionList = new SectionList(mNtpManager, mOfflinePageBridg
e); |
(...skipping 30 matching lines...) Expand all Loading... |
181 return mock(StatusCardViewHolder.class); | 253 return mock(StatusCardViewHolder.class); |
182 case ItemViewType.ACTION: | 254 case ItemViewType.ACTION: |
183 return mock(ActionItem.ViewHolder.class); | 255 return mock(ActionItem.ViewHolder.class); |
184 case ItemViewType.PROGRESS: | 256 case ItemViewType.PROGRESS: |
185 return mock(ProgressViewHolder.class); | 257 return mock(ProgressViewHolder.class); |
186 default: | 258 default: |
187 return mock(NewTabPageViewHolder.class); | 259 return mock(NewTabPageViewHolder.class); |
188 } | 260 } |
189 } | 261 } |
190 } | 262 } |
OLD | NEW |