1 /* 2 * Copyright (C) 2010 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.quicksearchbox; 18 19 import static com.google.common.base.Objects.equal; 20 21 import com.google.common.collect.UnmodifiableIterator; 22 23 import java.util.Collection; 24 import java.util.Iterator; 25 import java.util.NoSuchElementException; 26 27 import junit.framework.Assert; 28 29 /** 30 * Test utilities for {@link ShortcutCursor}. 31 */ 32 public class SuggestionCursorUtil extends Assert { 33 assertNoSuggestions(SuggestionCursor suggestions)34 public static void assertNoSuggestions(SuggestionCursor suggestions) { 35 assertNoSuggestions("", suggestions); 36 } 37 assertNoSuggestions(String message, SuggestionCursor suggestions)38 public static void assertNoSuggestions(String message, SuggestionCursor suggestions) { 39 assertNotNull(suggestions); 40 assertEquals(message, 0, suggestions.getCount()); 41 } 42 assertSameSuggestion(String message, int position, SuggestionCursor expected, SuggestionCursor observed)43 public static void assertSameSuggestion(String message, int position, 44 SuggestionCursor expected, SuggestionCursor observed) { 45 assertSameSuggestion(message, expected, position, observed, position); 46 } 47 assertSameSuggestion(String message, SuggestionCursor expected, int positionExpected, SuggestionCursor observed, int positionObserved)48 public static void assertSameSuggestion(String message, 49 SuggestionCursor expected, int positionExpected, 50 SuggestionCursor observed, int positionObserved) { 51 message += " at positions " + positionExpected + "(expected) " 52 + positionObserved + " (observed)"; 53 expected.moveTo(positionExpected); 54 observed.moveTo(positionObserved); 55 assertSuggestionEquals(message, expected, observed); 56 } 57 assertSameSuggestions(SuggestionCursor expected, SuggestionCursor observed)58 public static void assertSameSuggestions(SuggestionCursor expected, SuggestionCursor observed) { 59 assertSameSuggestions("", expected, observed); 60 } 61 assertSameSuggestions( String message, SuggestionCursor expected, SuggestionCursor observed)62 public static void assertSameSuggestions( 63 String message, SuggestionCursor expected, SuggestionCursor observed) { 64 assertNotNull(message + ", expected == null", expected); 65 assertNotNull(message + ", observed == null", observed); 66 assertEquals(message + ", count", expected.getCount(), observed.getCount()); 67 assertEquals(message + ", userQuery", expected.getUserQuery(), observed.getUserQuery()); 68 int count = expected.getCount(); 69 for (int i = 0; i < count; i++) { 70 assertSameSuggestion(message, i, expected, observed); 71 } 72 } 73 assertSameSuggestionsNoOrder(SuggestionCursor expected, SuggestionCursor observed)74 public static void assertSameSuggestionsNoOrder(SuggestionCursor expected, 75 SuggestionCursor observed) { 76 assertSameSuggestionsNoOrder("", expected, observed); 77 } 78 assertSameSuggestionsNoOrder(String message, SuggestionCursor expected, SuggestionCursor observed)79 public static void assertSameSuggestionsNoOrder(String message, 80 SuggestionCursor expected, SuggestionCursor observed) { 81 for (Suggestion expectedSuggestion : iterable(expected)) { 82 assertContainsSuggestion(expectedSuggestion, observed); 83 } 84 for (Suggestion observedSuggestion : iterable(observed)) { 85 assertContainsSuggestion(observedSuggestion, expected); 86 } 87 } 88 assertContainsSuggestion(Suggestion expected, SuggestionCursor observed)89 public static void assertContainsSuggestion(Suggestion expected, SuggestionCursor observed) { 90 for (Suggestion observedSuggestion : iterable(observed)) { 91 if (checkSuggestionEquals(expected, observedSuggestion)) { 92 return; 93 } 94 } 95 fail(expected + " not found in " + observed); 96 } 97 iterable(final SuggestionCursor cursor)98 public static Iterable<Suggestion> iterable(final SuggestionCursor cursor) { 99 return new Iterable<Suggestion>() { 100 @Override 101 public Iterator<Suggestion> iterator() { 102 return SuggestionCursorUtil.iterator(cursor); 103 } 104 }; 105 } 106 107 public static UnmodifiableIterator<Suggestion> iterator(final SuggestionCursor cursor) { 108 return new UnmodifiableIterator<Suggestion>() { 109 private int mPos = 0; 110 111 @Override 112 public boolean hasNext() { 113 return cursor.getPosition() < cursor.getCount() - 1; 114 } 115 @Override 116 public Suggestion next() { 117 if (!hasNext()) { 118 throw new NoSuchElementException(); 119 } 120 mPos++; 121 return new SuggestionPosition(cursor, mPos); 122 } 123 }; 124 } 125 126 public static ListSuggestionCursor slice(SuggestionCursor cursor, int start) { 127 return slice(cursor, start, cursor.getCount() - start); 128 } 129 130 public static ListSuggestionCursor slice(SuggestionCursor cursor, int start, int length) { 131 ListSuggestionCursor out = new ListSuggestionCursor(cursor.getUserQuery()); 132 for (int i = start; i < start + length; i++) { 133 out.add(new SuggestionPosition(cursor, i)); 134 } 135 return out; 136 } 137 138 public static ListSuggestionCursor concat(SuggestionCursor... cursors) { 139 ListSuggestionCursor out = new ListSuggestionCursor(cursors[0].getUserQuery()); 140 for (SuggestionCursor cursor : cursors) { 141 for (int i = 0; i < cursor.getCount(); i++) { 142 out.add(new SuggestionPosition(cursor, i)); 143 } 144 } 145 return out; 146 } 147 148 public static void assertSuggestionEquals(Suggestion expected, Suggestion observed) { 149 assertSuggestionEquals(null, expected, observed); 150 } 151 152 public static void assertSuggestionEquals(String message, Suggestion expected, 153 Suggestion observed) { 154 assertFieldEquals(message, "source", expected.getSuggestionSource(), 155 observed.getSuggestionSource()); 156 assertFieldEquals(message, "shortcutId", expected.getShortcutId(), 157 observed.getShortcutId()); 158 assertFieldEquals(message, "spinnerWhileRefreshing", expected.isSpinnerWhileRefreshing(), 159 observed.isSpinnerWhileRefreshing()); 160 assertFieldEquals(message, "format", expected.getSuggestionFormat(), 161 observed.getSuggestionFormat()); 162 assertFieldEquals(message, "icon1", expected.getSuggestionIcon1(), 163 observed.getSuggestionIcon1()); 164 assertFieldEquals(message, "icon2", expected.getSuggestionIcon2(), 165 observed.getSuggestionIcon2()); 166 assertFieldEquals(message, "text1", expected.getSuggestionText1(), 167 observed.getSuggestionText1()); 168 assertFieldEquals(message, "text2", expected.getSuggestionText2(), 169 observed.getSuggestionText2()); 170 assertFieldEquals(message, "text2Url", expected.getSuggestionText2Url(), 171 observed.getSuggestionText2Url()); 172 assertFieldEquals(message, "action", expected.getSuggestionIntentAction(), 173 observed.getSuggestionIntentAction()); 174 assertFieldEquals(message, "data", expected.getSuggestionIntentDataString(), 175 observed.getSuggestionIntentDataString()); 176 assertFieldEquals(message, "extraData", expected.getSuggestionIntentExtraData(), 177 observed.getSuggestionIntentExtraData()); 178 assertFieldEquals(message, "query", expected.getSuggestionQuery(), 179 observed.getSuggestionQuery()); 180 assertFieldEquals(message, "logType", expected.getSuggestionLogType(), 181 observed.getSuggestionLogType()); 182 } 183 184 private static void assertFieldEquals(String message, String field, 185 Object expected, Object observed) { 186 String msg = (message == null) ? field : message + ", " + field; 187 assertEquals(msg, expected, observed); 188 } 189 190 public static void addAll(ListSuggestionCursor to, SuggestionCursor from) { 191 if (from == null) return; 192 int count = from.getCount(); 193 for (int i = 0; i < count; i++) { 194 to.add(new SuggestionPosition(from, i)); 195 } 196 } 197 198 public static boolean checkSuggestionEquals(Suggestion expected, Suggestion observed) { 199 return equal(expected.getSuggestionSource(), observed.getSuggestionSource()) 200 && equal(expected.getShortcutId(), observed.getShortcutId()) 201 && equal(expected.isSpinnerWhileRefreshing(), observed.isSpinnerWhileRefreshing()) 202 && equal(expected.getSuggestionFormat(), observed.getSuggestionFormat()) 203 && equal(expected.getSuggestionIcon1(), observed.getSuggestionIcon1()) 204 && equal(expected.getSuggestionIcon2(), observed.getSuggestionIcon2()) 205 && equal(expected.getSuggestionText1(), observed.getSuggestionText1()) 206 && equal(expected.getSuggestionText2(), observed.getSuggestionText2()) 207 && equal(expected.getSuggestionText2Url(), observed.getSuggestionText2Url()) 208 && equal(expected.getSuggestionIntentAction(), observed.getSuggestionIntentAction()) 209 && equal(expected.getSuggestionIntentDataString(), observed.getSuggestionIntentDataString()) 210 && equal(expected.getSuggestionIntentExtraData(), observed.getSuggestionIntentExtraData()) 211 && equal(expected.getSuggestionQuery(), observed.getSuggestionQuery()) 212 && equal(expected.getSuggestionLogType(), observed.getSuggestionLogType()); 213 } 214 215 public static void assertSuggestionExtras(String message, SuggestionCursor observed, 216 String extraColumn, Object expectedExtra) { 217 assertNotNull(message + ", observed == null", observed); 218 assertTrue(message + ", no suggestions", observed.getCount() > 0); 219 for (int i = 0; i < observed.getCount(); ++i) { 220 observed.moveTo(i); 221 SuggestionExtras extras = observed.getExtras(); 222 assertNotNull(message + ", no extras at position " + i, extras); 223 Collection<String> columns = extras.getExtraColumnNames(); 224 assertNotNull(message + ", extras columns is null at position " + i, columns); 225 assertTrue(message + ", column '" + extraColumn + 226 "' not reported by extras at position " + i, columns.contains(extraColumn)); 227 Object extra = extras.getExtra(extraColumn); 228 assertEquals(message + ", extra value", expectedExtra == null ? null : 229 expectedExtra.toString(), extra); 230 } 231 } 232 233 } 234