1/* 2 * Copyright (C) 2008 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 17package com.android.unit_tests; 18 19import android.database.AbstractCursor; 20import android.test.suitebuilder.annotation.SmallTest; 21import com.android.internal.database.ArrayListCursor; 22import android.database.CursorWindow; 23import android.test.PerformanceTestCase; 24 25import com.google.android.collect.Lists; 26 27import java.util.ArrayList; 28import java.util.Arrays; 29import java.util.Random; 30 31import junit.framework.TestCase; 32 33public class CursorWindowTest extends TestCase implements PerformanceTestCase { 34 public boolean isPerformanceOnly() { 35 return false; 36 } 37 38 // These test can only be run once. 39 public int startPerformance(Intermediates intermediates) { 40 return 1; 41 } 42 43 @SmallTest 44 public void testWriteCursorToWindow() throws Exception { 45 // create cursor 46 String[] colNames = new String[]{"name", "number", "profit"}; 47 int colsize = colNames.length; 48 ArrayList<ArrayList> list = createTestList(10, colsize); 49 AbstractCursor cursor = new ArrayListCursor(colNames, (ArrayList<ArrayList>) list); 50 51 // fill window 52 CursorWindow window = new CursorWindow(false); 53 cursor.fillWindow(0, window); 54 55 // read from cursor window 56 for (int i = 0; i < list.size(); i++) { 57 ArrayList<Integer> col = list.get(i); 58 for (int j = 0; j < colsize; j++) { 59 String s = window.getString(i, j); 60 int r2 = col.get(j); 61 int r1 = Integer.parseInt(s); 62 assertEquals(r2, r1); 63 } 64 } 65 66 // test cursor window handle startpos != 0 67 window.clear(); 68 cursor.fillWindow(1, window); 69 // read from cursor from window 70 for (int i = 1; i < list.size(); i++) { 71 ArrayList<Integer> col = list.get(i); 72 for (int j = 0; j < colsize; j++) { 73 String s = window.getString(i, j); 74 int r2 = col.get(j); 75 int r1 = Integer.parseInt(s); 76 assertEquals(r2, r1); 77 } 78 } 79 80 // Clear the window and make sure it's empty 81 window.clear(); 82 assertEquals(0, window.getNumRows()); 83 } 84 85 @SmallTest 86 public void testValuesLocalWindow() { 87 doTestValues(new CursorWindow(true)); 88 } 89 90 @SmallTest 91 public void testValuesRemoteWindow() { 92 doTestValues(new CursorWindow(false)); 93 } 94 95 private void doTestValues(CursorWindow window) { 96 assertTrue(window.setNumColumns(7)); 97 assertTrue(window.allocRow()); 98 double db1 = 1.26; 99 assertTrue(window.putDouble(db1, 0, 0)); 100 double db2 = window.getDouble(0, 0); 101 assertEquals(db1, db2); 102 103 long int1 = Long.MAX_VALUE; 104 assertTrue(window.putLong(int1, 0, 1)); 105 long int2 = window.getLong(0, 1); 106 assertEquals(int1, int2); 107 108 assertTrue(window.putString("1198032740000", 0, 3)); 109 assertEquals("1198032740000", window.getString(0, 3)); 110 assertEquals(1198032740000L, window.getLong(0, 3)); 111 112 assertTrue(window.putString(Long.toString(1198032740000L), 0, 3)); 113 assertEquals(Long.toString(1198032740000L), window.getString(0, 3)); 114 assertEquals(1198032740000L, window.getLong(0, 3)); 115 116 assertTrue(window.putString(Double.toString(42.0), 0, 4)); 117 assertEquals(Double.toString(42.0), window.getString(0, 4)); 118 assertEquals(42.0, window.getDouble(0, 4)); 119 120 // put blob 121 byte[] blob = new byte[1000]; 122 byte value = 99; 123 Arrays.fill(blob, value); 124 assertTrue(window.putBlob(blob, 0, 6)); 125 assertTrue(Arrays.equals(blob, window.getBlob(0, 6))); 126 } 127 128 @SmallTest 129 public void testNull() { 130 CursorWindow window = getOneByOneWindow(); 131 132 // Put in a null value and read it back as various types 133 assertTrue(window.putNull(0, 0)); 134 assertNull(window.getString(0, 0)); 135 assertEquals(0, window.getLong(0, 0)); 136 assertEquals(0.0, window.getDouble(0, 0)); 137 assertNull(window.getBlob(0, 0)); 138 } 139 140 @SmallTest 141 public void testEmptyString() { 142 CursorWindow window = getOneByOneWindow(); 143 144 // put size 0 string and read it back as various types 145 assertTrue(window.putString("", 0, 0)); 146 assertEquals("", window.getString(0, 0)); 147 assertEquals(0, window.getLong(0, 0)); 148 assertEquals(0.0, window.getDouble(0, 0)); 149 } 150 151 private CursorWindow getOneByOneWindow() { 152 CursorWindow window = new CursorWindow(false); 153 assertTrue(window.setNumColumns(1)); 154 assertTrue(window.allocRow()); 155 return window; 156 } 157 158 private static ArrayList<ArrayList> createTestList(int rows, int cols) { 159 ArrayList<ArrayList> list = Lists.newArrayList(); 160 Random generator = new Random(); 161 162 for (int i = 0; i < rows; i++) { 163 ArrayList<Integer> col = Lists.newArrayList(); 164 list.add(col); 165 for (int j = 0; j < cols; j++) { 166 // generate random number 167 Integer r = generator.nextInt(); 168 col.add(r); 169 } 170 } 171 return list; 172 } 173} 174