1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.globalsearch; 18 19 import android.content.ComponentName; 20 21 import java.util.List; 22 import java.util.Map; 23 import java.util.HashMap; 24 import java.util.Arrays; 25 import java.util.HashSet; 26 27 /** 28 * For testing, delivers a canned set of suggestions after fixed delay. 29 */ 30 class TestSuggestionSource extends AbstractSuggestionSource { 31 32 private final ComponentName mComponent; 33 private final long mDelay; 34 private final Map<String, List<SuggestionData>> mCannedResponses; 35 private final HashSet<String> mErrorQueries; 36 private final boolean mQueryAfterZeroResults; 37 private final String mLabel; 38 TestSuggestionSource( ComponentName component, long delay, Map<String, List<SuggestionData>> cannedResponses, HashSet<String> errorQueries, boolean queryAfterZeroResults, String label)39 private TestSuggestionSource( 40 ComponentName component, 41 long delay, 42 Map<String, List<SuggestionData>> cannedResponses, 43 HashSet<String> errorQueries, boolean queryAfterZeroResults, String label) { 44 mComponent = component; 45 mDelay = delay; 46 mCannedResponses = cannedResponses; 47 mErrorQueries = errorQueries; 48 mQueryAfterZeroResults = queryAfterZeroResults; 49 mLabel = label; 50 } 51 52 getComponentName()53 public ComponentName getComponentName() { 54 return mComponent; 55 } 56 getIcon()57 public String getIcon() { 58 return null; 59 } 60 getLabel()61 public String getLabel() { 62 return mLabel; 63 } 64 getSettingsDescription()65 public String getSettingsDescription() { 66 return "settings description"; 67 } 68 69 @Override getSuggestions(String query, int maxResults, int queryLimit)70 protected SuggestionResult getSuggestions(String query, int maxResults, int queryLimit) { 71 if (mDelay > 0) { 72 try { 73 Thread.sleep(mDelay); 74 } catch (InterruptedException e) { 75 Thread.currentThread().interrupt(); 76 return mEmptyResult; 77 } 78 } 79 80 if (mErrorQueries.contains(query)) { 81 return SuggestionResult.createErrorResult(this); 82 } 83 84 final List<SuggestionData> result = mCannedResponses.get(query); 85 return result != null ? 86 new SuggestionResult(this, result) : 87 mEmptyResult; 88 } 89 90 @Override validateShortcut(SuggestionData shortcut)91 protected SuggestionData validateShortcut(SuggestionData shortcut) { 92 String shortcutId = shortcut.getShortcutId(); 93 for (List<SuggestionData> suggestionDatas : mCannedResponses.values()) { 94 for (SuggestionData suggestionData : suggestionDatas) { 95 if (shortcutId.equals(suggestionData.getShortcutId())) { 96 return suggestionData; 97 } 98 } 99 } 100 return null; 101 } 102 queryAfterZeroResults()103 public boolean queryAfterZeroResults() { 104 return mQueryAfterZeroResults; 105 } 106 107 @Override toString()108 public String toString() { 109 return mComponent.toShortString(); 110 } 111 112 @Override equals(Object o)113 public boolean equals(Object o) { 114 if (this == o) return true; 115 if (o == null || getClass() != o.getClass()) return false; 116 117 TestSuggestionSource that = (TestSuggestionSource) o; 118 119 if (mDelay != that.mDelay) return false; 120 if (mQueryAfterZeroResults != that.mQueryAfterZeroResults) return false; 121 if (mCannedResponses != null ? 122 !mCannedResponses.equals(that.mCannedResponses) 123 : that.mCannedResponses != null) 124 return false; 125 if (!mComponent.equals(that.mComponent)) return false; 126 if (mLabel != null ? !mLabel.equals(that.mLabel) : that.mLabel != null) return false; 127 128 return true; 129 } 130 131 @Override hashCode()132 public int hashCode() { 133 int result = mComponent.hashCode(); 134 result = 31 * result + (int) (mDelay ^ (mDelay >>> 32)); 135 result = 31 * result + (mCannedResponses != null ? mCannedResponses.hashCode() : 0); 136 result = 31 * result + (mQueryAfterZeroResults ? 1 : 0); 137 result = 31 * result + (mLabel != null ? mLabel.hashCode() : 0); 138 return result; 139 } 140 141 /** 142 * Makes a test source that will returned 1 canned result matching the given query. 143 */ makeCanned(String query, String s, long delay)144 public static SuggestionSource makeCanned(String query, String s, long delay) { 145 final ComponentName name = new ComponentName("com.test." + s, "Class$" + s); 146 147 final SuggestionData suggestion = new SuggestionData.Builder(name) 148 .title(s) 149 .intentAction("view") 150 .intentData(s) 151 .build(); 152 153 return new TestSuggestionSource.Builder() 154 .setComponent(name) 155 .setLabel(s) 156 .setDelay(delay) 157 .addCannedResponse(query, suggestion) 158 .create(); 159 } 160 161 static class Builder { 162 private ComponentName mComponent = 163 new ComponentName( 164 "com.android.globalsearch", "com.android.globalsearch.GlobalSearch"); 165 private long mDelay = 0; 166 private Map<String, List<SuggestionData>> mCannedResponses = 167 new HashMap<String, List<SuggestionData>>(); 168 private HashSet<String> mErrorQueries = new HashSet<String>(); 169 private boolean mQueryAfterZeroResults = false; 170 private String mLabel = "TestSuggestionSource"; 171 setComponent(ComponentName component)172 public Builder setComponent(ComponentName component) { 173 mComponent = component; 174 return this; 175 } 176 setDelay(long delay)177 public Builder setDelay(long delay) { 178 mDelay = delay; 179 return this; 180 } 181 setQueryAfterZeroResults(boolean queryAfterNoResults)182 public Builder setQueryAfterZeroResults(boolean queryAfterNoResults) { 183 mQueryAfterZeroResults = queryAfterNoResults; 184 return this; 185 } 186 setLabel(String label)187 public Builder setLabel(String label) { 188 mLabel = label; 189 return this; 190 } 191 192 /** 193 * Adds a list of canned suggestions as a result for the query and all prefixes of it. 194 * 195 * @param query The query 196 * @param suggestions The suggetions to return for the query and its prefixes. 197 */ addCannedResponse(String query, SuggestionData... suggestions)198 public Builder addCannedResponse(String query, SuggestionData... suggestions) { 199 final List<SuggestionData> suggestionList = Arrays.asList(suggestions); 200 mCannedResponses.put(query, suggestionList); 201 202 final int queryLength = query.length(); 203 for (int i = 1; i < queryLength; i++) { 204 final String subQuery = query.substring(0, queryLength - i); 205 mCannedResponses.put(subQuery, suggestionList); 206 } 207 return this; 208 } 209 210 /** 211 * Makes it so the source will return a <code>null</code> result for this query. 212 */ addErrorResponse(String query)213 public Builder addErrorResponse(String query) { 214 mErrorQueries.add(query); 215 return this; 216 } 217 create()218 public TestSuggestionSource create() { 219 return new TestSuggestionSource( 220 mComponent, mDelay, mCannedResponses, mErrorQueries, 221 mQueryAfterZeroResults, mLabel); 222 } 223 } 224 } 225