| 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.test.util.browser.suggestions; | 5 package org.chromium.chrome.test.util.browser.suggestions; |
| 6 | 6 |
| 7 import android.graphics.Bitmap; | 7 import android.graphics.Bitmap; |
| 8 | 8 |
| 9 import org.chromium.base.Callback; | 9 import org.chromium.base.Callback; |
| 10 import org.chromium.base.ThreadUtils; |
| 10 import org.chromium.chrome.browser.ntp.cards.SuggestionsCategoryInfo; | 11 import org.chromium.chrome.browser.ntp.cards.SuggestionsCategoryInfo; |
| 11 import org.chromium.chrome.browser.ntp.snippets.CategoryInt; | 12 import org.chromium.chrome.browser.ntp.snippets.CategoryInt; |
| 12 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus; | 13 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus; |
| 13 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus.CategoryStatusEnu
m; | 14 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus.CategoryStatusEnu
m; |
| 14 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; | 15 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; |
| 15 import org.chromium.chrome.browser.ntp.snippets.SnippetsBridge; | 16 import org.chromium.chrome.browser.ntp.snippets.SnippetsBridge; |
| 16 import org.chromium.chrome.browser.ntp.snippets.SuggestionsSource; | 17 import org.chromium.chrome.browser.ntp.snippets.SuggestionsSource; |
| 17 | 18 |
| 18 import java.util.ArrayList; | 19 import java.util.ArrayList; |
| 19 import java.util.Collections; | 20 import java.util.Collections; |
| 20 import java.util.HashMap; | 21 import java.util.HashMap; |
| 21 import java.util.LinkedHashMap; | 22 import java.util.LinkedHashMap; |
| 22 import java.util.List; | 23 import java.util.List; |
| 23 import java.util.Map; | 24 import java.util.Map; |
| 24 | 25 |
| 25 /** | 26 /** |
| 26 * A fake Suggestions source for use in unit and instrumentation tests. | 27 * A fake Suggestions source for use in unit and instrumentation tests. |
| 27 */ | 28 */ |
| 28 public class FakeSuggestionsSource implements SuggestionsSource { | 29 public class FakeSuggestionsSource implements SuggestionsSource { |
| 29 private SuggestionsSource.Observer mObserver; | 30 private SuggestionsSource.Observer mObserver; |
| 30 private final List<Integer> mCategories = new ArrayList<>(); | 31 private final List<Integer> mCategories = new ArrayList<>(); |
| 31 private final Map<Integer, List<SnippetArticle>> mSuggestions = new HashMap<
>(); | 32 private final Map<Integer, List<SnippetArticle>> mSuggestions = new HashMap<
>(); |
| 32 private final Map<Integer, Integer> mCategoryStatus = new LinkedHashMap<>(); | 33 private final Map<Integer, Integer> mCategoryStatus = new LinkedHashMap<>(); |
| 33 private final Map<Integer, SuggestionsCategoryInfo> mCategoryInfo = new Hash
Map<>(); | 34 private final Map<Integer, SuggestionsCategoryInfo> mCategoryInfo = new Hash
Map<>(); |
| 35 |
| 36 // Maps within-category ids to their fake bitmaps. |
| 34 private final Map<String, Bitmap> mThumbnails = new HashMap<>(); | 37 private final Map<String, Bitmap> mThumbnails = new HashMap<>(); |
| 38 private final Map<String, Bitmap> mFavicons = new HashMap<>(); |
| 35 | 39 |
| 36 private final List<Integer> mDismissedCategories = new ArrayList<>(); | 40 private final List<Integer> mDismissedCategories = new ArrayList<>(); |
| 37 private final Map<Integer, List<SnippetArticle>> mDismissedCategorySuggestio
ns = | 41 private final Map<Integer, List<SnippetArticle>> mDismissedCategorySuggestio
ns = |
| 38 new HashMap<>(); | 42 new HashMap<>(); |
| 39 private final Map<Integer, Integer> mDismissedCategoryStatus = new LinkedHas
hMap<>(); | 43 private final Map<Integer, Integer> mDismissedCategoryStatus = new LinkedHas
hMap<>(); |
| 40 private final Map<Integer, SuggestionsCategoryInfo> mDismissedCategoryInfo =
new HashMap<>(); | 44 private final Map<Integer, SuggestionsCategoryInfo> mDismissedCategoryInfo =
new HashMap<>(); |
| 41 | 45 |
| 42 /** | 46 /** |
| 43 * Sets the status to be returned for a given category. | 47 * Sets the status to be returned for a given category. |
| 44 */ | 48 */ |
| (...skipping 18 matching lines...) Expand all Loading... |
| 63 } | 67 } |
| 64 | 68 |
| 65 /** | 69 /** |
| 66 * Sets the metadata to be returned for a given category. | 70 * Sets the metadata to be returned for a given category. |
| 67 */ | 71 */ |
| 68 public void setInfoForCategory(@CategoryInt int category, SuggestionsCategor
yInfo info) { | 72 public void setInfoForCategory(@CategoryInt int category, SuggestionsCategor
yInfo info) { |
| 69 mCategoryInfo.put(category, info); | 73 mCategoryInfo.put(category, info); |
| 70 } | 74 } |
| 71 | 75 |
| 72 /** | 76 /** |
| 73 * Sets the bitmap to be returned when the thumbnail is requested for a snip
pet with that id. | 77 * Sets the bitmap to be returned when the thumbnail is requested for a sugg
estion with that |
| 78 * (within-category) id. |
| 74 */ | 79 */ |
| 75 public void setThumbnailForId(String id, Bitmap bitmap) { | 80 public void setThumbnailForId(String id, Bitmap bitmap) { |
| 76 mThumbnails.put(id, bitmap); | 81 mThumbnails.put(id, bitmap); |
| 77 } | 82 } |
| 78 | 83 |
| 79 /** | 84 /** |
| 85 * Sets the bitmap to be returned when the favicon is requested for a sugges
tion with that |
| 86 * (within-category) id. |
| 87 */ |
| 88 public void setFaviconForId(String id, Bitmap bitmap) { |
| 89 mFavicons.put(id, bitmap); |
| 90 } |
| 91 |
| 92 /** |
| 80 * Removes the given suggestion from the source and notifies any observer th
at it has been | 93 * Removes the given suggestion from the source and notifies any observer th
at it has been |
| 81 * invalidated. | 94 * invalidated. |
| 82 */ | 95 */ |
| 83 public void fireSuggestionInvalidated(@CategoryInt int category, String idWi
thinCategory) { | 96 public void fireSuggestionInvalidated(@CategoryInt int category, String idWi
thinCategory) { |
| 84 for (SnippetArticle suggestion : mSuggestions.get(category)) { | 97 for (SnippetArticle suggestion : mSuggestions.get(category)) { |
| 85 if (suggestion.mIdWithinCategory.equals(idWithinCategory)) { | 98 if (suggestion.mIdWithinCategory.equals(idWithinCategory)) { |
| 86 mSuggestions.get(category).remove(suggestion); | 99 mSuggestions.get(category).remove(suggestion); |
| 87 break; | 100 break; |
| 88 } | 101 } |
| 89 } | 102 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 for (int category : mDismissedCategories) { | 144 for (int category : mDismissedCategories) { |
| 132 mSuggestions.put(category, mDismissedCategorySuggestions.remove(cate
gory)); | 145 mSuggestions.put(category, mDismissedCategorySuggestions.remove(cate
gory)); |
| 133 mCategoryStatus.put(category, mDismissedCategoryStatus.remove(catego
ry)); | 146 mCategoryStatus.put(category, mDismissedCategoryStatus.remove(catego
ry)); |
| 134 mCategoryInfo.put(category, mDismissedCategoryInfo.remove(category))
; | 147 mCategoryInfo.put(category, mDismissedCategoryInfo.remove(category))
; |
| 135 mCategories.add(category); | 148 mCategories.add(category); |
| 136 } | 149 } |
| 137 mDismissedCategories.clear(); | 150 mDismissedCategories.clear(); |
| 138 } | 151 } |
| 139 | 152 |
| 140 @Override | 153 @Override |
| 141 public void fetchSuggestionImage(SnippetArticle suggestion, Callback<Bitmap>
callback) { | 154 public void fetchSuggestionImage( |
| 155 final SnippetArticle suggestion, final Callback<Bitmap> callback) { |
| 142 if (mThumbnails.containsKey(suggestion.mIdWithinCategory)) { | 156 if (mThumbnails.containsKey(suggestion.mIdWithinCategory)) { |
| 143 callback.onResult(mThumbnails.get(suggestion.mIdWithinCategory)); | 157 ThreadUtils.postOnUiThread(new Runnable() { |
| 158 @Override |
| 159 public void run() { |
| 160 callback.onResult(mThumbnails.get(suggestion.mIdWithinCatego
ry)); |
| 161 } |
| 162 }); |
| 144 } | 163 } |
| 145 } | 164 } |
| 146 | 165 |
| 166 @Override |
| 167 public void fetchSuggestionFavicon(final SnippetArticle suggestion, int mini
mumSizePx, |
| 168 int desiredSizePx, final Callback<Bitmap> callback) { |
| 169 if (mFavicons.containsKey(suggestion.mIdWithinCategory)) { |
| 170 ThreadUtils.postOnUiThread(new Runnable() { |
| 171 @Override |
| 172 public void run() { |
| 173 callback.onResult(mFavicons.get(suggestion.mIdWithinCategory
)); |
| 174 } |
| 175 }); |
| 176 } |
| 177 } |
| 178 |
| 147 @Override | 179 @Override |
| 148 public void fetchSuggestions(@CategoryInt int category, String[] displayedSu
ggestionIds) { | 180 public void fetchSuggestions(@CategoryInt int category, String[] displayedSu
ggestionIds) { |
| 149 throw new UnsupportedOperationException(); | 181 throw new UnsupportedOperationException(); |
| 150 } | 182 } |
| 151 | 183 |
| 152 @Override | 184 @Override |
| 153 public void setObserver(Observer observer) { | 185 public void setObserver(Observer observer) { |
| 154 mObserver = observer; | 186 mObserver = observer; |
| 155 } | 187 } |
| 156 | 188 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 175 | 207 |
| 176 @Override | 208 @Override |
| 177 public List<SnippetArticle> getSuggestionsForCategory(int category) { | 209 public List<SnippetArticle> getSuggestionsForCategory(int category) { |
| 178 if (!SnippetsBridge.isCategoryStatusAvailable(mCategoryStatus.get(catego
ry))) { | 210 if (!SnippetsBridge.isCategoryStatusAvailable(mCategoryStatus.get(catego
ry))) { |
| 179 return Collections.emptyList(); | 211 return Collections.emptyList(); |
| 180 } | 212 } |
| 181 List<SnippetArticle> result = mSuggestions.get(category); | 213 List<SnippetArticle> result = mSuggestions.get(category); |
| 182 return result == null ? Collections.<SnippetArticle>emptyList() : new Ar
rayList<>(result); | 214 return result == null ? Collections.<SnippetArticle>emptyList() : new Ar
rayList<>(result); |
| 183 } | 215 } |
| 184 } | 216 } |
| OLD | NEW |