You have to set the permission on the application's manifest not on
the tests' manifest.
That's what you usually do by having a main and a tests project.
Furthermore, your test should look more like:

public class AndroidDummyContactsTests extends InstrumentationTestCase
{

        private static final String TESTUSER_NAME = "Test User";
        private static final String TESTUSER_NUMBER = "1234568909";
        private Instrumentation mInstrumentation;
        private ContentResolver mContentResolver;

        public AndroidDummyContactsTests(String name) {
                setName(name);
        }

        protected void setUp() throws Exception {
                super.setUp();
                mInstrumentation = getInstrumentation();
                mContentResolver =
mInstrumentation.getTargetContext().getContentResolver();
        }

        protected void tearDown() throws Exception {
                super.tearDown();
        }

        public final void testInsertContact() {
                final ContentValues values = new ContentValues();
                final Uri rawContact =
mContentResolver.insert(ContactsContract.RawContacts.CONTENT_URI,
values);
                assertNotNull(rawContact);
                final long rawContactId = ContentUris.parseId(rawContact);
                assertTrue(rawContactId > 0);

                values.clear();
                values.put(Data.RAW_CONTACT_ID, rawContactId);
                values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
                values.put(StructuredName.DISPLAY_NAME, TESTUSER_NAME);
                assertNotNull(mContentResolver.insert(Data.CONTENT_URI, 
values));

                values.clear();
                values.put(Data.RAW_CONTACT_ID, rawContactId);
                values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
                values.put(Phone.NUMBER, TESTUSER_NUMBER);
                assertNotNull(mContentResolver.insert(Data.CONTENT_URI, 
values));
        }
}

and it works (remember that you need an app too where you grant the
permission), but as you are not using mock objects you will be
populating your contacts database with test data which is probably not
a very good idea.


On Jan 8, 2:37 pm, javaxmlsoapdev <[email protected]> wrote:
> I am writing a test case to for reading phone contact list
>
> public class ContactTest extends AndroidTestCase {
>
>         static final String LOG_TAG = "ContactTest";
>     static final String TESTUSER_NAME = "Test User";
>     static final String TESTUSER_NUMBER = "1234568909";
>     ContentResolver contentResolver;
>     Uri newPerson;
>
>     public void setUp() {
>         contentResolver = getContext().getContentResolver();
>
>         ContentValues person = new ContentValues();
>         person.put(ContactsContract.Contacts.DISPLAY_NAME,
> TESTUSER_NAME );
>         person.put(ContactsContract.CommonDataKinds.Phone.NUMBER,
> TESTUSER_NUMBER );
>
>         newPerson =
> contentResolver.insert(ContactsContract.Contacts.CONTENT_URI,person);
>     }
>
> In Manifest file I have following permissions defined
> <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
>     <uses-permission android:name="android.permission.READ_CONTACTS"/>
>
> But while running the test it keeps throwing following permission
> exception
> java.lang.SecurityException: Permission Denial: writing
> com.android.providers.contacts.ContactsProvider2 uri
> content://com.android.contacts/contacts from pid=343, uid=10036
> requires android.permission.WRITE_CONTACTS
> at android.os.Parcel.readException(Parcel.java:1247)
> at
> android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:
> 160)
> at
> android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:
> 114)
> at
> android.content.ContentProviderProxy.insert(ContentProviderNative.java:
> 408)
> at android.content.ContentResolver.insert(ContentResolver.java:587)
> at com.aes.mobile.android.test.ContactTest.setUp(ContactTest.java:32)
> at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
> at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
> at
> android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.ja 
> va:
> 520)
> at android.app.Instrumentation
> $InstrumentationThread.run(Instrumentation.java:1447)
>
> Any idea?
>
> Thanks,


--
Have you read my blog ?
http://dtmilano.blogspot.com
android junit tests ui linux cult thin clients

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to