1package com.xtremelabs.robolectric.tester.android.database;
2
3import android.content.ContentResolver;
4import android.net.Uri;
5import com.xtremelabs.robolectric.Robolectric;
6import com.xtremelabs.robolectric.WithTestDefaultsRunner;
7import com.xtremelabs.robolectric.shadows.ShadowContentResolver;
8import org.junit.Before;
9import org.junit.Test;
10import org.junit.runner.RunWith;
11
12import java.util.ArrayList;
13
14import static com.xtremelabs.robolectric.Robolectric.shadowOf;
15import static org.hamcrest.CoreMatchers.equalTo;
16import static org.junit.Assert.assertThat;
17
18@RunWith(WithTestDefaultsRunner.class)
19public class SimpleTestCursorTest {
20    private Uri uri;
21    private SimpleTestCursor cursor;
22    private ContentResolver contentResolver;
23
24    @Before
25    public void setup() throws Exception {
26        contentResolver = Robolectric.application.getContentResolver();
27        ShadowContentResolver shadowContentResolver = shadowOf(contentResolver);
28        uri = Uri.parse("http://foo");
29        cursor = new SimpleTestCursor();
30        shadowContentResolver.setCursor(uri, cursor);
31        ArrayList<String> columnNames = new ArrayList<String>();
32        columnNames.add("stringColumn");
33        columnNames.add("longColumn");
34        cursor.setColumnNames(columnNames);
35    }
36
37    @Test
38    public void doingQueryShouldMakeQueryParamsAvailable() throws Exception {
39        contentResolver.query(uri, new String[]{"projection"}, "selection", new String[]{"selection"}, "sortOrder");
40        assertThat(cursor.uri, equalTo(uri));
41        assertThat(cursor.projection[0], equalTo("projection"));
42        assertThat(cursor.selection, equalTo("selection"));
43        assertThat(cursor.selectionArgs[0], equalTo("selection"));
44        assertThat(cursor.sortOrder, equalTo("sortOrder"));
45    }
46
47    @Test
48    public void canGetStringsAndLongs() throws Exception {
49        cursor.setResults(new Object[][]{new Object[]{"aString", 1234L}});
50        assertThat(cursor.moveToNext(), equalTo(true));
51        assertThat(cursor.getString(cursor.getColumnIndex("stringColumn")), equalTo("aString"));
52        assertThat(cursor.getLong(cursor.getColumnIndex("longColumn")), equalTo(1234L));
53    }
54
55    @Test
56    public void moveToNextAdvancesToNextRow() throws Exception {
57        cursor.setResults(new Object[][] { new Object[] { "aString", 1234L }, new Object[] { "anotherString", 5678L }});
58        assertThat(cursor.moveToNext(), equalTo(true));
59        assertThat(cursor.moveToNext(), equalTo(true));
60        assertThat(cursor.getString(cursor.getColumnIndex("stringColumn")), equalTo("anotherString"));
61        assertThat(cursor.getLong(cursor.getColumnIndex("longColumn")), equalTo(5678L));
62    }
63
64    @Test
65    public void closeIsRemembered() throws Exception {
66        cursor.close();
67        assertThat(cursor.getCloseWasCalled(), equalTo(true));
68    }
69}
70