mobile/android/base/tests/testHomeListsProvider.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 package org.mozilla.gecko.tests;
michael@0 2
michael@0 3 import android.content.ContentUris;
michael@0 4 import android.content.ContentValues;
michael@0 5 import android.database.Cursor;
michael@0 6 import android.net.Uri;
michael@0 7
michael@0 8 public class testHomeListsProvider extends ContentProviderTest {
michael@0 9 // This test does not run, so it just needs to compile. The test was
michael@0 10 // disabled at the time the real Contract was removed; to leave a skeleton
michael@0 11 // for a future re-implementor, we include this dummy Contract class.
michael@0 12 private static class Contract {
michael@0 13 public static final Uri CONTENT_URI = null;
michael@0 14 public static final Uri CONTENT_FAKE_URI = null;
michael@0 15
michael@0 16 public static final String _ID = null;
michael@0 17 public static final String PROVIDER_ID = null;
michael@0 18 public static final String TITLE = null;
michael@0 19 public static final String URL = null;
michael@0 20 }
michael@0 21
michael@0 22 @SuppressWarnings("unused")
michael@0 23 private void ensureEmptyDatabase() throws Exception {
michael@0 24 // Delete all the list entries.
michael@0 25 mProvider.delete(Contract.CONTENT_URI, null, null);
michael@0 26
michael@0 27 final Cursor c = mProvider.query(Contract.CONTENT_URI, null, null, null, null);
michael@0 28 mAsserter.is(c.getCount(), 0, "All list entries were deleted");
michael@0 29 c.close();
michael@0 30 }
michael@0 31
michael@0 32 @Override
michael@0 33 public void setUp() throws Exception {
michael@0 34 // This test is disabled, so this just needs to compile.
michael@0 35 super.setUp(null, null, "homelists.db");
michael@0 36
michael@0 37 mTests.add(new TestFakeItems());
michael@0 38
michael@0 39 // Disabled until database support lands
michael@0 40 //mTests.add(new TestInsertItem());
michael@0 41 }
michael@0 42
michael@0 43 public void testListsProvider() throws Exception {
michael@0 44 for (int i = 0; i < mTests.size(); i++) {
michael@0 45 Runnable test = mTests.get(i);
michael@0 46
michael@0 47 setTestName(test.getClass().getSimpleName());
michael@0 48 // Disabled until database support lands
michael@0 49 //ensureEmptyDatabase();
michael@0 50 test.run();
michael@0 51 }
michael@0 52 }
michael@0 53
michael@0 54 abstract class Test implements Runnable {
michael@0 55 @Override
michael@0 56 public void run() {
michael@0 57 try {
michael@0 58 test();
michael@0 59 } catch (Exception e) {
michael@0 60 mAsserter.is(true, false, "Test " + this.getClass().getName() +
michael@0 61 " threw exception: " + e);
michael@0 62 }
michael@0 63 }
michael@0 64
michael@0 65 public abstract void test() throws Exception;
michael@0 66 }
michael@0 67
michael@0 68 class TestFakeItems extends Test {
michael@0 69 @Override
michael@0 70 public void test() throws Exception {
michael@0 71 final long id = 1;
michael@0 72 final String providerId = "fake-provider";
michael@0 73 final String title = "Example";
michael@0 74 final String url = "http://example.com";
michael@0 75
michael@0 76 final Cursor c = mProvider.query(Contract.CONTENT_FAKE_URI, null, null, null, null);
michael@0 77 mAsserter.is(c.moveToFirst(), true, "Fake list item found");
michael@0 78
michael@0 79 mAsserter.is(c.getLong(c.getColumnIndex(Contract._ID)), id, "Fake list item has correct ID");
michael@0 80 mAsserter.is(c.getString(c.getColumnIndex(Contract.PROVIDER_ID)), providerId, "Fake list item has correct provider ID");
michael@0 81 mAsserter.is(c.getString(c.getColumnIndex(Contract.TITLE)), title, "Fake list item has correct title");
michael@0 82 mAsserter.is(c.getString(c.getColumnIndex(Contract.URL)), url, "Fake list item has correct URL");
michael@0 83
michael@0 84 c.close();
michael@0 85 }
michael@0 86 }
michael@0 87
michael@0 88 class TestInsertItem extends Test {
michael@0 89 @Override
michael@0 90 public void test() throws Exception {
michael@0 91 final String providerId = "{c77da387-4c80-0c45-9f22-70276c29b3ed}";
michael@0 92 final String title = "Mozilla";
michael@0 93 final String url = "https://mozilla.org";
michael@0 94
michael@0 95 // Insert a new list item with test values.
michael@0 96 final ContentValues cv = new ContentValues();
michael@0 97 cv.put(Contract.PROVIDER_ID, providerId);
michael@0 98 cv.put(Contract.TITLE, title);
michael@0 99 cv.put(Contract.URL, url);
michael@0 100
michael@0 101 final long id = ContentUris.parseId(mProvider.insert(Contract.CONTENT_URI, cv));
michael@0 102
michael@0 103 // Check that the item was inserted correctly.
michael@0 104 final Cursor c = mProvider.query(Contract.CONTENT_URI, null, Contract._ID + " = ?", new String[] { String.valueOf(id) }, null);
michael@0 105 mAsserter.is(c.moveToFirst(), true, "Inserted list item found");
michael@0 106
michael@0 107 mAsserter.is(c.getString(c.getColumnIndex(Contract.PROVIDER_ID)), providerId, "Inserted list item has correct provider ID");
michael@0 108 mAsserter.is(c.getString(c.getColumnIndex(Contract.TITLE)), title, "Inserted list item has correct title");
michael@0 109 mAsserter.is(c.getString(c.getColumnIndex(Contract.URL)), url, "Inserted list item has correct URL");
michael@0 110
michael@0 111 c.close();
michael@0 112 }
michael@0 113 }
michael@0 114 }

mercurial