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

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: review 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
« no previous file with comments | « chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/SuggestionsSectionTest.java ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 suggestionsSource.setStatusForCategory( 63 suggestionsSource.setStatusForCategory(
64 categoryInfo.getCategory(), CategoryStatus.AVAILABLE); 64 categoryInfo.getCategory(), CategoryStatus.AVAILABLE);
65 suggestionsSource.setInfoForCategory(categoryInfo.getCategory(), categor yInfo); 65 suggestionsSource.setInfoForCategory(categoryInfo.getCategory(), categor yInfo);
66 66
67 List<SnippetArticle> suggestions = 67 List<SnippetArticle> suggestions =
68 createDummySuggestions(suggestionCount, categoryInfo.getCategory ()); 68 createDummySuggestions(suggestionCount, categoryInfo.getCategory ());
69 suggestionsSource.setSuggestionsForCategory(categoryInfo.getCategory(), suggestions); 69 suggestionsSource.setSuggestionsForCategory(categoryInfo.getCategory(), suggestions);
70 return suggestions; 70 return suggestions;
71 } 71 }
72 72
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 /** 73 /**
100 * Uses the builder pattern to simplify constructing category info objects f or tests. 74 * Uses the builder pattern to simplify constructing category info objects f or tests.
101 */ 75 */
102 public static class CategoryInfoBuilder { 76 public static class CategoryInfoBuilder {
103 @CategoryInt 77 @CategoryInt
104 private final int mCategory; 78 private final int mCategory;
105 private int mAdditionalAction; 79 private int mAdditionalAction;
106 private boolean mShowIfEmpty; 80 private boolean mShowIfEmpty;
107 private String mTitle = ""; 81 private String mTitle = "";
108 private String mNoSuggestionsMessage = ""; 82 private String mNoSuggestionsMessage = "";
(...skipping 29 matching lines...) Expand all
138 } 112 }
139 113
140 public SuggestionsCategoryInfo build() { 114 public SuggestionsCategoryInfo build() {
141 return new SuggestionsCategoryInfo(mCategory, mTitle, mCardLayout, m AdditionalAction, 115 return new SuggestionsCategoryInfo(mCategory, mTitle, mCardLayout, m AdditionalAction,
142 mShowIfEmpty, mNoSuggestionsMessage); 116 mShowIfEmpty, mNoSuggestionsMessage);
143 } 117 }
144 } 118 }
145 119
146 /** Helper method to print the current state of a node. */ 120 /** Helper method to print the current state of a node. */
147 public static String stringify(TreeNode root) { 121 public static String stringify(TreeNode root) {
148 return explainFailedExpectation(root, -1, ItemViewType.ALL_DISMISSED); 122 final StringBuilder stringBuilder = new StringBuilder();
149 }
150 123
151 /** 124 root.visitItems(new NodeVisitor() {
152 * Helper method to print the current state of a node, highlighting somethin g that went wrong. 125 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 126
161 stringBuilder.append("explainFailedExpectation -- START -- \n"); 127 @Override
162 for (int i = 0; i < root.getItemCount(); ++i) { 128 public void visitAboveTheFoldItem() {
163 if (errorIndex == i) { 129 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 } 130 }
169 } 131
170 if (errorIndex >= root.getItemCount()) { 132 @Override
171 addLine(stringBuilder, "<end of list>"); 133 public void visitActionItem(@ContentSuggestionsAdditionalAction int currentAction) {
172 addLine(stringBuilder, "%d - <NONE> <= expected: %s", errorIndex, 134 describeItem("ACTION(%d)", currentAction);
173 viewTypeToString(expectedType)); 135 }
174 } 136
175 addLine(stringBuilder, "explainFailedExpectation -- END --"); 137 @Override
138 public void visitAllDismissedItem() {
139 describeItem("ALL_DISMISSED");
140 }
141
142 @Override
143 public void visitFooter() {
144 describeItem("FOOTER");
145 }
146
147 @Override
148 public void visitProgressItem() {
149 describeItem("PROGRESS");
150 }
151
152 @Override
153 public void visitSignInPromo() {
154 describeItem("SIGN_IN_PROMO");
155 }
156
157 @Override
158 public void visitSpacingItem() {
159 describeItem("SPACING");
160 }
161
162 @Override
163 public void visitNoSuggestionsItem() {
164 describeItem("NO_SUGGESTIONS");
165 }
166
167 @Override
168 public void visitSuggestion(SnippetArticle suggestion) {
169 describeItem("SUGGESTION(%1.42s)", suggestion.mTitle);
170 }
171
172 @Override
173 public void visitHeader() {
174 describeItem("HEADER");
175 }
176
177 @Override
178 public void visitTileGrid() {
179 describeItem("TILE_GRID");
180 }
181
182 private void describeItem(String description) {
183 stringBuilder.append(
184 String.format(Locale.US, "%s - %s%n", mPosition++, descr iption));
185 }
186
187 private void describeItem(String template, Object... args) {
188 describeItem(String.format(Locale.US, template, args));
189 }
190 });
176 return stringBuilder.toString(); 191 return stringBuilder.toString();
177 } 192 }
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 } 193 }
OLDNEW
« no previous file with comments | « chrome/android/junit/src/org/chromium/chrome/browser/ntp/cards/SuggestionsSectionTest.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698