| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 org.chromium.chrome.browser.ntp.snippets.CategoryInt; | 7 import org.chromium.chrome.browser.ntp.snippets.CategoryInt; |
| 8 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus; | 8 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus; |
| 9 import org.chromium.chrome.browser.ntp.snippets.ContentSuggestionsCardLayout; | 9 import org.chromium.chrome.browser.ntp.snippets.ContentSuggestionsCardLayout; |
| 10 import org.chromium.chrome.browser.ntp.snippets.ContentSuggestionsCardLayout.Con
tentSuggestionsCardLayoutEnum; | 10 import org.chromium.chrome.browser.ntp.snippets.ContentSuggestionsCardLayout.Con
tentSuggestionsCardLayoutEnum; |
| 11 import org.chromium.chrome.browser.ntp.snippets.FakeSuggestionsSource; | 11 import org.chromium.chrome.browser.ntp.snippets.FakeSuggestionsSource; |
| 12 import org.chromium.chrome.browser.ntp.snippets.KnownCategories; | 12 import org.chromium.chrome.browser.ntp.snippets.KnownCategories; |
| 13 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; | 13 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; |
| 14 | 14 |
| 15 import java.util.ArrayList; | 15 import java.util.ArrayList; |
| 16 import java.util.List; | 16 import java.util.List; |
| 17 | 17 |
| 18 /** Utilities to make testing content suggestions code easier. */ | 18 /** Utilities to make testing content suggestions code easier. */ |
| 19 public final class ContentSuggestionsTestUtils { | 19 public final class ContentSuggestionsTestUtils { |
| 20 private ContentSuggestionsTestUtils() {} | 20 private ContentSuggestionsTestUtils() {} |
| 21 | 21 |
| 22 public static List<SnippetArticle> createDummySuggestions(int count) { | 22 public static List<SnippetArticle> createDummySuggestions( |
| 23 int count, @CategoryInt int category) { |
| 23 List<SnippetArticle> suggestions = new ArrayList<>(); | 24 List<SnippetArticle> suggestions = new ArrayList<>(); |
| 24 for (int index = 0; index < count; index++) { | 25 for (int index = 0; index < count; index++) { |
| 25 suggestions.add(new SnippetArticle(KnownCategories.BOOKMARKS, | 26 suggestions.add( |
| 26 "https://site.com/url" + index, "title" + index, "pub" + ind
ex, "txt" + index, | 27 new SnippetArticle(category, "https://site.com/url" + index,
"title" + index, |
| 27 "https://site.com/url" + index, 0, 0, 0)); | 28 "pub" + index, "txt" + index, "https://site.com/url"
+ index, 0, 0)); |
| 28 } | 29 } |
| 29 return suggestions; | 30 return suggestions; |
| 30 } | 31 } |
| 31 | 32 |
| 32 /** Registers a category that has a reload action and is shown if empty. */ | 33 /** |
| 33 public static void registerCategory(FakeSuggestionsSource suggestionsSource, | 34 * @deprecated The hardcoded category is a common source of bugs. Prefer |
| 35 * {@link #createDummySuggestions(int, int)} |
| 36 */ |
| 37 @Deprecated |
| 38 public static List<SnippetArticle> createDummySuggestions(int count) { |
| 39 return createDummySuggestions(count, KnownCategories.BOOKMARKS); |
| 40 } |
| 41 |
| 42 /** |
| 43 * Registers a category according to the provided category info. |
| 44 * @return the suggestions added to the newly registered category. |
| 45 */ |
| 46 public static List<SnippetArticle> registerCategory(FakeSuggestionsSource su
ggestionsSource, |
| 34 @CategoryInt int category, int suggestionCount) { | 47 @CategoryInt int category, int suggestionCount) { |
| 48 // Important: showIfEmpty flag to true. |
| 49 SuggestionsCategoryInfo categoryInfo = |
| 50 new CategoryInfoBuilder(category).withReloadAction().showIfEmpty
().build(); |
| 51 return registerCategory(suggestionsSource, categoryInfo, suggestionCount
); |
| 52 } |
| 53 |
| 54 /** |
| 55 * Registers a category that has a reload action and is shown if empty. |
| 56 * @return the suggestions added to the newly registered category. |
| 57 */ |
| 58 public static List<SnippetArticle> registerCategory(FakeSuggestionsSource su
ggestionsSource, |
| 59 SuggestionsCategoryInfo categoryInfo, int suggestionCount) { |
| 35 // FakeSuggestionSource does not provide suggestions if the category's s
tatus is not | 60 // FakeSuggestionSource does not provide suggestions if the category's s
tatus is not |
| 36 // AVAILABLE. | 61 // AVAILABLE. |
| 37 suggestionsSource.setStatusForCategory(category, CategoryStatus.AVAILABL
E); | 62 suggestionsSource.setStatusForCategory( |
| 38 // Important: showIfEmpty flag to true. | 63 categoryInfo.getCategory(), CategoryStatus.AVAILABLE); |
| 39 suggestionsSource.setInfoForCategory(category, | 64 suggestionsSource.setInfoForCategory(categoryInfo.getCategory(), categor
yInfo); |
| 40 new CategoryInfoBuilder(category).withReloadAction().showIfEmpty
().build()); | 65 |
| 41 suggestionsSource.setSuggestionsForCategory( | 66 List<SnippetArticle> suggestions = |
| 42 category, createDummySuggestions(suggestionCount)); | 67 createDummySuggestions(suggestionCount, categoryInfo.getCategory
()); |
| 68 suggestionsSource.setSuggestionsForCategory(categoryInfo.getCategory(),
suggestions); |
| 69 return suggestions; |
| 43 } | 70 } |
| 44 | 71 |
| 45 public static String viewTypeToString(@ItemViewType int viewType) { | 72 public static String viewTypeToString(@ItemViewType int viewType) { |
| 46 switch (viewType) { | 73 switch (viewType) { |
| 47 case ItemViewType.ABOVE_THE_FOLD: | 74 case ItemViewType.ABOVE_THE_FOLD: |
| 48 return "ABOVE_THE_FOLD"; | 75 return "ABOVE_THE_FOLD"; |
| 49 case ItemViewType.HEADER: | 76 case ItemViewType.HEADER: |
| 50 return "HEADER"; | 77 return "HEADER"; |
| 51 case ItemViewType.SNIPPET: | 78 case ItemViewType.SNIPPET: |
| 52 return "SNIPPET"; | 79 return "SNIPPET"; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 mCardLayout = cardLayout; | 149 mCardLayout = cardLayout; |
| 123 return this; | 150 return this; |
| 124 } | 151 } |
| 125 | 152 |
| 126 public SuggestionsCategoryInfo build() { | 153 public SuggestionsCategoryInfo build() { |
| 127 return new SuggestionsCategoryInfo(mCategory, mTitle, mCardLayout, m
HasMoreAction, | 154 return new SuggestionsCategoryInfo(mCategory, mTitle, mCardLayout, m
HasMoreAction, |
| 128 mHasReloadAction, mHasViewAllAction, mShowIfEmpty, mNoSugges
tionsMessage); | 155 mHasReloadAction, mHasViewAllAction, mShowIfEmpty, mNoSugges
tionsMessage); |
| 129 } | 156 } |
| 130 } | 157 } |
| 131 } | 158 } |
| OLD | NEW |