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 android.database;
18
19import android.test.suitebuilder.annotation.SmallTest;
20import android.database.CursorWindow;
21import android.test.PerformanceTestCase;
22
23import java.util.Arrays;
24
25import junit.framework.TestCase;
26
27public class CursorWindowTest extends TestCase implements PerformanceTestCase {
28    public boolean isPerformanceOnly() {
29        return false;
30    }
31
32    // These test can only be run once.
33    public int startPerformance(Intermediates intermediates) {
34        return 1;
35    }
36
37    @SmallTest
38    public void testConstructor_WithName() {
39        CursorWindow window = new CursorWindow("MyWindow");
40        assertEquals("MyWindow", window.getName());
41        assertEquals(0, window.getStartPosition());
42        window.close();
43    }
44
45    @SmallTest
46    public void testConstructorWithEmptyName() {
47        CursorWindow window = new CursorWindow("");
48        assertEquals("<unnamed>", window.getName());
49        assertEquals(0, window.getStartPosition());
50        window.close();
51    }
52
53    @SmallTest
54    public void testConstructorWithNullName() {
55        CursorWindow window = new CursorWindow(null);
56        assertEquals("<unnamed>", window.getName());
57        assertEquals(0, window.getStartPosition());
58        window.close();
59    }
60
61    @SmallTest
62    public void testDeprecatedConstructor() {
63        @SuppressWarnings("deprecation")
64        CursorWindow window = new CursorWindow(true /*this argument is ignored*/);
65        assertEquals("<unnamed>", window.getName());
66        assertEquals(0, window.getStartPosition());
67        window.close();
68    }
69
70    @SmallTest
71    public void testValues() {
72        CursorWindow window = new CursorWindow("MyWindow");
73        doTestValues(window);
74        window.close();
75    }
76
77    private void doTestValues(CursorWindow window) {
78        assertTrue(window.setNumColumns(7));
79        assertTrue(window.allocRow());
80        double db1 = 1.26;
81        assertTrue(window.putDouble(db1, 0, 0));
82        double db2 = window.getDouble(0, 0);
83        assertEquals(db1, db2);
84
85        long int1 = Long.MAX_VALUE;
86        assertTrue(window.putLong(int1, 0, 1));
87        long int2 = window.getLong(0, 1);
88        assertEquals(int1, int2);
89
90        assertTrue(window.putString("1198032740000", 0, 3));
91        assertEquals("1198032740000", window.getString(0, 3));
92        assertEquals(1198032740000L, window.getLong(0, 3));
93
94        assertTrue(window.putString(Long.toString(1198032740000L), 0, 3));
95        assertEquals(Long.toString(1198032740000L), window.getString(0, 3));
96        assertEquals(1198032740000L, window.getLong(0, 3));
97
98        assertTrue(window.putString(Double.toString(42.0), 0, 4));
99        assertEquals(Double.toString(42.0), window.getString(0, 4));
100        assertEquals(42.0, window.getDouble(0, 4));
101
102        // put blob
103        byte[] blob = new byte[1000];
104        byte value = 99;
105        Arrays.fill(blob, value);
106        assertTrue(window.putBlob(blob, 0, 6));
107        assertTrue(Arrays.equals(blob, window.getBlob(0, 6)));
108    }
109}
110