Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - code example for Testing with the ProviderTestCase2

Tags:

android

Where can I find a simple, working and easy code example for testing on Android with the ProviderTestCase2?

Best would be an OpenSource project actually using it - on the other hand, in the complexity of a real project it can become hard to realize the actual functionality, so any good example is welcome. The Google Android docs are not very specific and contain no proper example.

like image 499
Henning Avatar asked Mar 13 '11 01:03

Henning


2 Answers

In this example I'm testing a ContentProvider named 'DataLayer' which exists in the app being tested; below, I've begun testing the ContentProvider interface to a table called 'ActiveUser'.

If anyone knows how to use newResolverWithContentProviderFromSql please enlighten me!

package com.example.android.app.test;

import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.test.ProviderTestCase2;
import android.test.mock.MockContentResolver;
import android.util.Log;

import com.example.android.app.Constants;
import com.example.android.app.DataLayer;
import com.example.android.app.provider.contract.ActiveUserContract;

public class DataLayerTests extends ProviderTestCase2<DataLayer> {

    private static final String TAG = DataLayerTests.class.getSimpleName();

    MockContentResolver mMockResolver;

    public DataLayerTests() {
        super(DataLayer.class, Constants.DATA_LAYER_AUTHORITY);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        Log.d(TAG, "setUp: ");
        mMockResolver = getMockContentResolver();
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        Log.d(TAG, "tearDown:");
    }

    public void testActiveUserInsert__inserts_a_valid_record() {
        Uri uri = mMockResolver.insert(ActiveUserContract.CONTENT_URI, getFullActiveUserContentValues());
        assertEquals(1L, ContentUris.parseId(uri));
    }

    public void testActiveUserInsert__cursor_contains_valid_data() {
        mMockResolver.insert(ActiveUserContract.CONTENT_URI, getFullActiveUserContentValues());
        Cursor cursor = mMockResolver.query(ActiveUserContract.CONTENT_URI, null, null, new String[] {}, null);
        assertNotNull(cursor);
        assertEquals(1, cursor.getCount());
        assertTrue(cursor.moveToFirst());
        assertEquals(VALID_USERNAME,     cursor.getString(cursor.getColumnIndex(ActiveUserContract.Columns.USERNAME)));
        assertEquals(VALID_COMPANY_CODE,    cursor.getString(cursor.getColumnIndex(ActiveUserContract.Columns.COMPANY_CODE)));
        assertEquals(VALID_COMPANY_LETTER,  cursor.getString(cursor.getColumnIndex(ActiveUserContract.Columns.COMPANY_LETTER)));
        assertEquals(VALID_DRIVER_CODE,     cursor.getString(cursor.getColumnIndex(ActiveUserContract.Columns.DRIVER_CODE)));
        assertEquals(VALID_SITE_NUMBER,     cursor.getString(cursor.getColumnIndex(ActiveUserContract.Columns.SITE_NUMBER)));
        assertEquals(VALID_FIRST_NAME,      cursor.getString(cursor.getColumnIndex(ActiveUserContract.Columns.FIRST_NAME)));
        assertEquals(VALID_SURNAME,         cursor.getString(cursor.getColumnIndex(ActiveUserContract.Columns.SURNAME)));
    }

    public void testActiveUserInsert__throws_SQLiteConstraintException_when_NOT_NULL_constraint_not_met() {
        try {
            mMockResolver.insert(ActiveUserContract.CONTENT_URI, getActiveUserContentValuesWithNullCompanyCode());
            fail("SQLiteConstraintException should have been thrown!");
        } catch (SQLiteConstraintException e) {
            assertEquals("active_user.comp_code may not be NULL (code 19)", e.getMessage());
        }
    }

    /** @return a ContentValues object with a value set for each ActiveUser column */
    public static ContentValues getFullActiveUserContentValues() {
        ContentValues v = new ContentValues(7);
        v.put(ActiveUserContract.Columns.USERNAME,       VALID_USERNAME);
        v.put(ActiveUserContract.Columns.COMPANY_CODE,   VALID_COMPANY_CODE);
        v.put(ActiveUserContract.Columns.COMPANY_LETTER, VALID_COMPANY_LETTER);
        v.put(ActiveUserContract.Columns.DRIVER_CODE,    VALID_DRIVER_CODE);
        v.put(ActiveUserContract.Columns.SITE_NUMBER,    VALID_SITE_NUMBER);
        v.put(ActiveUserContract.Columns.FIRST_NAME,     VALID_FIRST_NAME);
        v.put(ActiveUserContract.Columns.SURNAME,        VALID_SURNAME);
        return v;
    }

public static ContentValues getActiveUserContentValuesWithNullCompanyCode() {
    ContentValues v = new ContentValues(7);
    v.put(ActiveUserContract.Columns.USERNAME,       VALID_USERNAME);
    v.putNull(ActiveUserContract.Columns.COMPANY_CODE);
    v.put(ActiveUserContract.Columns.COMPANY_LETTER, VALID_COMPANY_LETTER);
    v.put(ActiveUserContract.Columns.DRIVER_CODE,    VALID_DRIVER_CODE);
    v.put(ActiveUserContract.Columns.SITE_NUMBER,    VALID_SITE_NUMBER);
    v.put(ActiveUserContract.Columns.FIRST_NAME,     VALID_FIRST_NAME);
    v.put(ActiveUserContract.Columns.SURNAME,        VALID_SURNAME);
    return v;
}

    private static final String VALID_SURNAME        = "Brin";
    private static final String VALID_FIRST_NAME     = "Sergey";
    private static final String VALID_SITE_NUMBER    = "9";
    private static final String VALID_DRIVER_CODE    = "SergB201";
    private static final String VALID_COMPANY_LETTER = "G";
    private static final String VALID_COMPANY_CODE   = "GOOGLE!";
    private static final String VALID_USERNAME       = "123456";
}
like image 77
fr1550n Avatar answered Sep 19 '22 01:09

fr1550n


Android uses it internally to test its providers. For examples see here and here. Google Code Search is great for this stuff :)

like image 32
Nic Strong Avatar answered Sep 18 '22 01:09

Nic Strong