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 package com.android.quicksearchbox; 17 18 import android.app.SearchManager; 19 import android.database.Cursor; 20 import android.test.AndroidTestCase; 21 22 import java.util.Arrays; 23 import java.util.HashSet; 24 25 /** 26 * Tests to verify that custom columns from a suggestion cursor get propagated through QSB properly. 27 */ 28 public class SuggestionCursorWithExtrasTest extends AndroidTestCase { 29 30 public void testSuggestionCursorExtraRowString() { 31 checkSuggestionCursorExtraColumnValue("extra_text", "Extra text"); 32 } 33 34 public void testSuggestionCursorExtraRowInteger() { 35 checkSuggestionCursorExtraColumnValue("extra_int", 42); 36 } 37 38 public void testSuggestionCursorExtraRowFloat() { 39 checkSuggestionCursorExtraColumnValue("extra_float", new Float(Math.E)); 40 } 41 42 public void testSuggestionCursorExtraRowNull() { 43 checkSuggestionCursorExtraColumnValue("extra_null", null); 44 } 45 46 public void testCursorExtraRowString() { 47 checkExtraRowString("stringy-string"); 48 checkExtraRowString(""); 49 checkExtraRowString(null); 50 } 51 52 private void checkExtraRowString(String value) { 53 String column = "extra_string"; 54 Cursor c = createCursorWithExtras(column, value); 55 assertEquals("Extra column value", value, c.getString(c.getColumnIndex(column))); 56 } 57 58 public void testCursorExtraRowInt() { 59 checkCursorExtraInt(42); 60 checkCursorExtraInt(0); 61 checkCursorExtraInt(-42); 62 checkCursorExtraInt(Integer.MAX_VALUE); 63 checkCursorExtraInt(Integer.MIN_VALUE); 64 } 65 66 public void checkCursorExtraInt(int value) { 67 String column = "extra_int"; 68 Cursor c = createCursorWithExtras(column, value); 69 assertEquals("Extra column value", value, c.getInt(c.getColumnIndex(column))); 70 } 71 72 public void testCursorExtraRowDouble() { 73 checkCursorExtraRowDouble(Math.PI); 74 checkCursorExtraRowDouble(-Math.PI); 75 checkCursorExtraRowDouble(0); 76 checkCursorExtraRowDouble(Double.MAX_VALUE); 77 checkCursorExtraRowDouble(Double.MIN_VALUE); 78 } 79 80 public void checkCursorExtraRowDouble(double value) { 81 String column = "extra_double"; 82 Cursor c = createCursorWithExtras(column, value); 83 assertEquals("Extra column value", value, c.getDouble(c.getColumnIndex(column))); 84 } 85 86 public void testCursorExtraRowFloat() { 87 checkCursorExtraRowFloat((float) Math.E); 88 checkCursorExtraRowFloat((float) -Math.E); 89 checkCursorExtraRowFloat(0f); 90 checkCursorExtraRowFloat(Float.MAX_VALUE); 91 checkCursorExtraRowFloat(Float.MIN_VALUE); 92 } 93 94 public void checkCursorExtraRowFloat(float value) { 95 String column = "extra_float"; 96 Cursor c = createCursorWithExtras(column, value); 97 assertEquals("Extra column value", value, c.getFloat(c.getColumnIndex(column))); 98 } 99 100 public void testCursorExtraRowLong() { 101 checkExtraRowLong(0xfeed0beefl); 102 checkExtraRowLong(-0xfeed0beefl); 103 checkExtraRowLong(0); 104 checkExtraRowLong(Long.MIN_VALUE); 105 checkExtraRowLong(Long.MAX_VALUE); 106 } 107 108 private void checkExtraRowLong(long value) { 109 String column = "extra_long"; 110 Cursor c = createCursorWithExtras(column, value); 111 assertEquals("Extra column value", value, c.getLong(c.getColumnIndex(column))); 112 } 113 114 public void testCursorExtraRowShort() { 115 checkCursorExtraRowShort((short) 0xabc); 116 checkCursorExtraRowShort((short) -0xabc); 117 checkCursorExtraRowShort((short) 0); 118 checkCursorExtraRowShort(Short.MAX_VALUE); 119 checkCursorExtraRowShort(Short.MIN_VALUE); 120 } 121 122 private void checkCursorExtraRowShort(short value) { 123 String column = "extra_short"; 124 Cursor c = createCursorWithExtras(column, value); 125 assertEquals("Extra column value", value, c.getShort(c.getColumnIndex(column))); 126 } 127 128 private Cursor createCursorWithExtras(String columnName, Object columnValue) { 129 MockSuggestionProviderCursor expectedCursor = new MockSuggestionProviderCursor( 130 new String[]{"_id", SearchManager.SUGGEST_COLUMN_TEXT_1, columnName }); 131 expectedCursor.addRow( 0, "Text 1", columnValue); 132 133 // this roughly approcimates what happens to suggestions 134 CursorBackedSourceResult suggestions = new CursorBackedSourceResult( 135 MockSource.SOURCE_1, "", expectedCursor); 136 assertEquals("Number of suggestions", 1, suggestions.getCount()); 137 suggestions.moveTo(0); 138 SuggestionCursorBackedCursor observedCursor = new SuggestionCursorBackedCursor(suggestions); 139 140 assertEquals("Cursor rows", 1, observedCursor.getCount()); 141 HashSet<String> rows = new HashSet<String>(); 142 rows.addAll(Arrays.asList(observedCursor.getColumnNames())); 143 assertTrue("Extra column present in cursor", rows.contains(columnName)); 144 observedCursor.moveToFirst(); 145 return observedCursor; 146 } 147 148 private void checkSuggestionCursorExtraColumnValue(String columnName, Object columnValue) { 149 MockSuggestionProviderCursor cursor = new MockSuggestionProviderCursor( 150 new String[]{"_id", SearchManager.SUGGEST_COLUMN_TEXT_1, columnName }); 151 cursor.addRow( 0, "Text 1", columnValue); 152 153 CursorBackedSourceResult suggestions = new CursorBackedSourceResult( 154 MockSource.SOURCE_1, "", cursor); 155 156 assertEquals("Suggestions count matches cursor count", 157 cursor.getCount(), suggestions.getCount()); 158 159 cursor.moveToFirst(); 160 suggestions.moveTo(0); 161 162 SuggestionExtras extras = suggestions.getExtras(); 163 assertNotNull("Suggestions extras", extras); 164 assertTrue("Extra column missing", extras.getExtraColumnNames().contains(columnName)); 165 assertTrue("Spurious columns", extras.getExtraColumnNames().size() == 1); 166 Object extraValue = extras.getExtra(columnName); 167 if (columnValue == null) { 168 assertEquals("Wrong extra value", columnValue, extraValue); 169 } else { 170 assertNotNull("Extra value null", extraValue); 171 if (columnValue == null) { 172 assertEquals("Extra value wrong", columnValue, extraValue); 173 } else { 174 assertEquals("Extra value wrong", columnValue.toString(), extraValue); 175 } 176 } 177 } 178 179 } 180