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

Side by Side Diff: chrome/test/android/javatests/src/org/chromium/chrome/test/util/browser/suggestions/ContentSuggestionsTestUtils.java

Issue 2860463002: [Suggestions] Remove TreeNode.getSuggestionAt() in favor of a visitor. (Closed)
Patch Set: rebase Created 3 years, 7 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 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.test.util.browser.suggestions; 5 package org.chromium.chrome.test.util.browser.suggestions;
6 6
7 import org.chromium.chrome.browser.ntp.cards.ItemViewType; 7 import org.chromium.chrome.browser.ntp.cards.NodeVisitor;
8 import org.chromium.chrome.browser.ntp.cards.SuggestionsCategoryInfo; 8 import org.chromium.chrome.browser.ntp.cards.SuggestionsCategoryInfo;
9 import org.chromium.chrome.browser.ntp.cards.TreeNode; 9 import org.chromium.chrome.browser.ntp.cards.TreeNode;
10 import org.chromium.chrome.browser.ntp.snippets.CategoryInt; 10 import org.chromium.chrome.browser.ntp.snippets.CategoryInt;
11 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus; 11 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus;
12 import org.chromium.chrome.browser.ntp.snippets.ContentSuggestionsCardLayout; 12 import org.chromium.chrome.browser.ntp.snippets.ContentSuggestionsCardLayout;
13 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; 13 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle;
14 import org.chromium.chrome.browser.suggestions.ContentSuggestionsAdditionalActio n; 14 import org.chromium.chrome.browser.suggestions.ContentSuggestionsAdditionalActio n;
15 15
16 import java.util.ArrayList; 16 import java.util.ArrayList;
17 import java.util.List; 17 import java.util.List;
18 import java.util.Locale;
19 18
20 /** Utilities to make testing content suggestions code easier. */ 19 /** Utilities to make testing content suggestions code easier. */
21 public final class ContentSuggestionsTestUtils { 20 public final class ContentSuggestionsTestUtils {
22 private ContentSuggestionsTestUtils() {} 21 private ContentSuggestionsTestUtils() {}
23 22
24 public static List<SnippetArticle> createDummySuggestions( 23 public static List<SnippetArticle> createDummySuggestions(
25 int count, @CategoryInt int category, String prefix) { 24 int count, @CategoryInt int category, String prefix) {
26 List<SnippetArticle> suggestions = new ArrayList<>(); 25 List<SnippetArticle> suggestions = new ArrayList<>();
27 for (int index = 0; index < count; index++) { 26 for (int index = 0; index < count; index++) {
28 suggestions.add(new SnippetArticle(category, "https://site.com/url" + prefix + index, 27 suggestions.add(new SnippetArticle(category, "https://site.com/url" + prefix + index,
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 suggestionsSource.setStatusForCategory( 62 suggestionsSource.setStatusForCategory(
64 categoryInfo.getCategory(), CategoryStatus.AVAILABLE); 63 categoryInfo.getCategory(), CategoryStatus.AVAILABLE);
65 suggestionsSource.setInfoForCategory(categoryInfo.getCategory(), categor yInfo); 64 suggestionsSource.setInfoForCategory(categoryInfo.getCategory(), categor yInfo);
66 65
67 List<SnippetArticle> suggestions = 66 List<SnippetArticle> suggestions =
68 createDummySuggestions(suggestionCount, categoryInfo.getCategory ()); 67 createDummySuggestions(suggestionCount, categoryInfo.getCategory ());
69 suggestionsSource.setSuggestionsForCategory(categoryInfo.getCategory(), suggestions); 68 suggestionsSource.setSuggestionsForCategory(categoryInfo.getCategory(), suggestions);
70 return suggestions; 69 return suggestions;
71 } 70 }
72 71
73 public static String viewTypeToString(@ItemViewType int viewType) {
74 switch (viewType) {
75 case ItemViewType.ABOVE_THE_FOLD:
76 return "ABOVE_THE_FOLD";
77 case ItemViewType.HEADER:
78 return "HEADER";
79 case ItemViewType.SNIPPET:
80 return "SNIPPET";
81 case ItemViewType.SPACING:
82 return "SPACING";
83 case ItemViewType.STATUS:
84 return "STATUS";
85 case ItemViewType.PROGRESS:
86 return "PROGRESS";
87 case ItemViewType.ACTION:
88 return "ACTION";
89 case ItemViewType.FOOTER:
90 return "FOOTER";
91 case ItemViewType.PROMO:
92 return "PROMO";
93 case ItemViewType.ALL_DISMISSED:
94 return "ALL_DISMISSED";
95 }
96 throw new AssertionError();
97 }
98
99 /** 72 /**
100 * Uses the builder pattern to simplify constructing category info objects f or tests. 73 * Uses the builder pattern to simplify constructing category info objects f or tests.
101 */ 74 */
102 public static class CategoryInfoBuilder { 75 public static class CategoryInfoBuilder {
103 @CategoryInt 76 @CategoryInt
104 private final int mCategory; 77 private final int mCategory;
105 private int mAdditionalAction; 78 private int mAdditionalAction;
106 private boolean mShowIfEmpty; 79 private boolean mShowIfEmpty;
107 private String mTitle = ""; 80 private String mTitle = "";
108 private String mNoSuggestionsMessage = ""; 81 private String mNoSuggestionsMessage = "";
(...skipping 29 matching lines...) Expand all
138 } 111 }
139 112
140 public SuggestionsCategoryInfo build() { 113 public SuggestionsCategoryInfo build() {
141 return new SuggestionsCategoryInfo(mCategory, mTitle, mCardLayout, m AdditionalAction, 114 return new SuggestionsCategoryInfo(mCategory, mTitle, mCardLayout, m AdditionalAction,
142 mShowIfEmpty, mNoSuggestionsMessage); 115 mShowIfEmpty, mNoSuggestionsMessage);
143 } 116 }
144 } 117 }
145 118
146 /** Helper method to print the current state of a node. */ 119 /** Helper method to print the current state of a node. */
147 public static String stringify(TreeNode root) { 120 public static String stringify(TreeNode root) {
148 return explainFailedExpectation(root, -1, ItemViewType.ALL_DISMISSED); 121 final StringBuilder stringBuilder = new StringBuilder();
149 }
150 122
151 /** 123 root.visitItems(new NodeVisitor() {
152 * Helper method to print the current state of a node, highlighting somethin g that went wrong. 124 private int mPosition;
153 * @param root node to print information about.
154 * @param errorIndex index where an unexpected item was found.
155 * @param expectedType item type that was expected at {@code errorIndex}.
156 */
157 public static String explainFailedExpectation(
158 TreeNode root, int errorIndex, @ItemViewType int expectedType) {
159 StringBuilder stringBuilder = new StringBuilder();
160 125
161 stringBuilder.append("explainFailedExpectation -- START -- \n"); 126 @Override
162 for (int i = 0; i < root.getItemCount(); ++i) { 127 public void visitAboveTheFoldItem() {
163 if (errorIndex == i) { 128 describeItem("ABOVE_THE_FOLD");
164 addLine(stringBuilder, "%d - %s <= expected: %s", i,
165 viewTypeToString(root.getItemViewType(i)), viewTypeToStr ing(expectedType));
166 } else {
167 addLine(stringBuilder, "%d - %s", i, viewTypeToString(root.getIt emViewType(i)));
168 } 129 }
169 } 130
170 if (errorIndex >= root.getItemCount()) { 131 @Override
171 addLine(stringBuilder, "<end of list>"); 132 public void visitActionItem(@ContentSuggestionsAdditionalAction int currentAction) {
172 addLine(stringBuilder, "%d - <NONE> <= expected: %s", errorIndex, 133 describeItem("ACTION(%d)", currentAction);
173 viewTypeToString(expectedType)); 134 }
174 } 135
175 addLine(stringBuilder, "explainFailedExpectation -- END --"); 136 @Override
137 public void visitAllDismissedItem() {
138 describeItem("ALL_DISMISSED");
139 }
140
141 @Override
142 public void visitFooter() {
143 describeItem("FOOTER");
144 }
145
146 @Override
147 public void visitProgressItem() {
148 describeItem("PROGRESS");
149 }
150
151 @Override
152 public void visitSignInPromo() {
153 describeItem("SIGN_IN_PROMO");
154 }
155
156 @Override
157 public void visitSpacingItem() {
158 describeItem("SPACING");
159 }
160
161 @Override
162 public void visitNoSuggestionsItem() {
163 describeItem("NO_SUGGESTIONS");
164 }
165
166 @Override
167 public void visitSuggestion(SnippetArticle suggestion) {
168 describeItem("SUGGESTION(%1.42s)", suggestion.mTitle);
169 }
170
171 @Override
172 public void visitHeader() {
173 describeItem("HEADER");
174 }
175
176 @Override
177 public void visitTileGrid() {
178 describeItem("TILE_GRID");
179 }
180
181 private void describeItem(String description) {
182 stringBuilder.append(String.format("%s - %s%n", mPosition++, des cription));
dgn 2017/05/22 13:47:03 Didn't know about %n, really cool :)
183 }
184
185 private void describeItem(String template, Object... args) {
186 describeItem(String.format(template, args));
dgn 2017/05/22 13:47:03 Nit: I think using String.format raises a lint war
Bernhard Bauer 2017/05/23 16:40:27 I'm using Locale.US now, because the format string
dgn 2017/05/24 11:30:30 Acknowledged.
187 }
188 });
176 return stringBuilder.toString(); 189 return stringBuilder.toString();
177 } 190 }
178
179 private static void addLine(StringBuilder stringBuilder, String template, Ob ject... args) {
180 stringBuilder.append(String.format(Locale.US, template + "\n", args));
181 }
182 } 191 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698