mobile/android/base/tests/testHomeListsProvider.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/tests/testHomeListsProvider.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,114 @@
     1.4 +package org.mozilla.gecko.tests;
     1.5 +
     1.6 +import android.content.ContentUris;
     1.7 +import android.content.ContentValues;
     1.8 +import android.database.Cursor;
     1.9 +import android.net.Uri;
    1.10 +
    1.11 +public class testHomeListsProvider extends ContentProviderTest {
    1.12 +    // This test does not run, so it just needs to compile. The test was
    1.13 +    // disabled at the time the real Contract was removed; to leave a skeleton
    1.14 +    // for a future re-implementor, we include this dummy Contract class.
    1.15 +    private static class Contract {
    1.16 +        public static final Uri CONTENT_URI = null;
    1.17 +        public static final Uri CONTENT_FAKE_URI = null;
    1.18 +
    1.19 +        public static final String _ID = null;
    1.20 +        public static final String PROVIDER_ID = null;
    1.21 +        public static final String TITLE = null;
    1.22 +        public static final String URL = null;
    1.23 +    }
    1.24 +
    1.25 +    @SuppressWarnings("unused")
    1.26 +  private void ensureEmptyDatabase() throws Exception {
    1.27 +        // Delete all the list entries.
    1.28 +        mProvider.delete(Contract.CONTENT_URI, null, null);
    1.29 +
    1.30 +        final Cursor c = mProvider.query(Contract.CONTENT_URI, null, null, null, null);
    1.31 +        mAsserter.is(c.getCount(), 0, "All list entries were deleted");
    1.32 +        c.close();
    1.33 +    }
    1.34 +
    1.35 +    @Override
    1.36 +    public void setUp() throws Exception {
    1.37 +        // This test is disabled, so this just needs to compile.
    1.38 +        super.setUp(null, null, "homelists.db");
    1.39 +
    1.40 +        mTests.add(new TestFakeItems());
    1.41 +
    1.42 +        // Disabled until database support lands
    1.43 +        //mTests.add(new TestInsertItem());
    1.44 +    }
    1.45 +
    1.46 +    public void testListsProvider() throws Exception {
    1.47 +        for (int i = 0; i < mTests.size(); i++) {
    1.48 +            Runnable test = mTests.get(i);
    1.49 +
    1.50 +            setTestName(test.getClass().getSimpleName());
    1.51 +            // Disabled until database support lands
    1.52 +            //ensureEmptyDatabase();
    1.53 +            test.run();
    1.54 +        }
    1.55 +    }
    1.56 +
    1.57 +    abstract class Test implements Runnable {
    1.58 +        @Override
    1.59 +        public void run() {
    1.60 +            try {
    1.61 +                test();
    1.62 +            } catch (Exception e) {
    1.63 +                mAsserter.is(true, false, "Test " + this.getClass().getName() +
    1.64 +                        " threw exception: " + e);
    1.65 +            }
    1.66 +        }
    1.67 +
    1.68 +        public abstract void test() throws Exception;
    1.69 +    }
    1.70 +
    1.71 +    class TestFakeItems extends Test {
    1.72 +        @Override
    1.73 +        public void test() throws Exception {
    1.74 +            final long id = 1;
    1.75 +            final String providerId = "fake-provider";
    1.76 +            final String title = "Example";
    1.77 +            final String url = "http://example.com";
    1.78 +
    1.79 +            final Cursor c = mProvider.query(Contract.CONTENT_FAKE_URI, null, null, null, null);
    1.80 +            mAsserter.is(c.moveToFirst(), true, "Fake list item found");
    1.81 +
    1.82 +            mAsserter.is(c.getLong(c.getColumnIndex(Contract._ID)), id, "Fake list item has correct ID");
    1.83 +            mAsserter.is(c.getString(c.getColumnIndex(Contract.PROVIDER_ID)), providerId, "Fake list item has correct provider ID");
    1.84 +            mAsserter.is(c.getString(c.getColumnIndex(Contract.TITLE)), title, "Fake list item has correct title");
    1.85 +            mAsserter.is(c.getString(c.getColumnIndex(Contract.URL)), url, "Fake list item has correct URL");
    1.86 +
    1.87 +            c.close();
    1.88 +        }
    1.89 +    }
    1.90 +
    1.91 +    class TestInsertItem extends Test {
    1.92 +        @Override
    1.93 +        public void test() throws Exception {
    1.94 +            final String providerId = "{c77da387-4c80-0c45-9f22-70276c29b3ed}";
    1.95 +            final String title = "Mozilla";
    1.96 +            final String url = "https://mozilla.org";
    1.97 +
    1.98 +            // Insert a new list item with test values.
    1.99 +            final ContentValues cv = new ContentValues();
   1.100 +            cv.put(Contract.PROVIDER_ID, providerId);
   1.101 +            cv.put(Contract.TITLE, title);
   1.102 +            cv.put(Contract.URL, url);
   1.103 +
   1.104 +            final long id = ContentUris.parseId(mProvider.insert(Contract.CONTENT_URI, cv));
   1.105 +
   1.106 +            // Check that the item was inserted correctly.
   1.107 +            final Cursor c = mProvider.query(Contract.CONTENT_URI, null, Contract._ID + " = ?", new String[] { String.valueOf(id) }, null);
   1.108 +            mAsserter.is(c.moveToFirst(), true, "Inserted list item found");
   1.109 +
   1.110 +            mAsserter.is(c.getString(c.getColumnIndex(Contract.PROVIDER_ID)), providerId, "Inserted list item has correct provider ID");
   1.111 +            mAsserter.is(c.getString(c.getColumnIndex(Contract.TITLE)), title, "Inserted list item has correct title");
   1.112 +            mAsserter.is(c.getString(c.getColumnIndex(Contract.URL)), url, "Inserted list item has correct URL");
   1.113 +
   1.114 +            c.close();
   1.115 +        }
   1.116 +    }
   1.117 +}

mercurial