michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: package org.mozilla.gecko.background.db; michael@0: michael@0: import org.json.simple.JSONArray; michael@0: import org.mozilla.gecko.db.BrowserContract; michael@0: import org.mozilla.gecko.sync.repositories.android.BrowserContractHelpers; michael@0: michael@0: import android.app.Activity; michael@0: import android.content.ContentProviderClient; michael@0: import android.content.ContentResolver; michael@0: import android.content.ContentValues; michael@0: import android.database.Cursor; michael@0: import android.net.Uri; michael@0: import android.os.RemoteException; michael@0: import android.test.ActivityInstrumentationTestCase2; michael@0: michael@0: /** michael@0: * Exercise Fennec's tabs provider. michael@0: * michael@0: * @author rnewman michael@0: * michael@0: */ michael@0: public class TestFennecTabsStorage extends ActivityInstrumentationTestCase2 { michael@0: public static final String TEST_CLIENT_GUID = "test guid"; // Real GUIDs never contain spaces. michael@0: public static final String TEST_CLIENT_NAME = "test client name"; michael@0: michael@0: public static final String TABS_CLIENT_GUID_IS = BrowserContract.Tabs.CLIENT_GUID + " = ?"; michael@0: michael@0: protected Tab testTab1; michael@0: protected Tab testTab2; michael@0: protected Tab testTab3; michael@0: michael@0: public TestFennecTabsStorage() { michael@0: super(Activity.class); michael@0: } michael@0: michael@0: protected ContentProviderClient getClientsClient() { michael@0: final ContentResolver cr = getInstrumentation().getTargetContext().getApplicationContext().getContentResolver(); michael@0: return cr.acquireContentProviderClient(BrowserContractHelpers.CLIENTS_CONTENT_URI); michael@0: } michael@0: michael@0: protected ContentProviderClient getTabsClient() { michael@0: final ContentResolver cr = getInstrumentation().getTargetContext().getApplicationContext().getContentResolver(); michael@0: return cr.acquireContentProviderClient(BrowserContractHelpers.TABS_CONTENT_URI); michael@0: } michael@0: michael@0: protected int deleteAllTestTabs(final ContentProviderClient tabsClient) throws RemoteException { michael@0: if (tabsClient == null) { michael@0: return -1; michael@0: } michael@0: return tabsClient.delete(BrowserContractHelpers.TABS_CONTENT_URI, TABS_CLIENT_GUID_IS, new String[] { TEST_CLIENT_GUID }); michael@0: } michael@0: michael@0: @Override michael@0: protected void tearDown() throws Exception { michael@0: deleteAllTestTabs(getTabsClient()); michael@0: } michael@0: michael@0: @SuppressWarnings("unchecked") michael@0: protected void insertSomeTestTabs(ContentProviderClient tabsClient) throws RemoteException { michael@0: final JSONArray history1 = new JSONArray(); michael@0: history1.add("http://test.com/test1.html"); michael@0: testTab1 = new Tab("test title 1", "http://test.com/test1.png", history1, 1000); michael@0: michael@0: final JSONArray history2 = new JSONArray(); michael@0: history2.add("http://test.com/test2.html#1"); michael@0: history2.add("http://test.com/test2.html#2"); michael@0: history2.add("http://test.com/test2.html#3"); michael@0: testTab2 = new Tab("test title 2", "http://test.com/test2.png", history2, 2000); michael@0: michael@0: final JSONArray history3 = new JSONArray(); michael@0: history3.add("http://test.com/test3.html#1"); michael@0: history3.add("http://test.com/test3.html#2"); michael@0: testTab3 = new Tab("test title 3", "http://test.com/test3.png", history3, 3000); michael@0: michael@0: tabsClient.insert(BrowserContractHelpers.TABS_CONTENT_URI, testTab1.toContentValues(TEST_CLIENT_GUID, 0)); michael@0: tabsClient.insert(BrowserContractHelpers.TABS_CONTENT_URI, testTab2.toContentValues(TEST_CLIENT_GUID, 1)); michael@0: tabsClient.insert(BrowserContractHelpers.TABS_CONTENT_URI, testTab3.toContentValues(TEST_CLIENT_GUID, 2)); michael@0: } michael@0: michael@0: // Sanity. michael@0: public void testObtainCP() { michael@0: final ContentProviderClient clientsClient = getClientsClient(); michael@0: assertNotNull(clientsClient); michael@0: clientsClient.release(); michael@0: michael@0: final ContentProviderClient tabsClient = getTabsClient(); michael@0: assertNotNull(tabsClient); michael@0: tabsClient.release(); michael@0: } michael@0: michael@0: public void testWipeClients() throws RemoteException { michael@0: final Uri uri = BrowserContractHelpers.CLIENTS_CONTENT_URI; michael@0: final ContentProviderClient clientsClient = getClientsClient(); michael@0: michael@0: // Have to ensure that it's empty… michael@0: clientsClient.delete(uri, null, null); michael@0: michael@0: int deleted = clientsClient.delete(uri, null, null); michael@0: assertEquals(0, deleted); michael@0: } michael@0: michael@0: public void testWipeTabs() throws RemoteException { michael@0: final ContentProviderClient tabsClient = getTabsClient(); michael@0: michael@0: // Have to ensure that it's empty… michael@0: deleteAllTestTabs(tabsClient); michael@0: michael@0: int deleted = deleteAllTestTabs(tabsClient); michael@0: assertEquals(0, deleted); michael@0: } michael@0: michael@0: public void testStoreAndRetrieveClients() throws RemoteException { michael@0: final Uri uri = BrowserContractHelpers.CLIENTS_CONTENT_URI; michael@0: final ContentProviderClient clientsClient = getClientsClient(); michael@0: michael@0: // Have to ensure that it's empty… michael@0: clientsClient.delete(uri, null, null); michael@0: michael@0: final long now = System.currentTimeMillis(); michael@0: final ContentValues first = new ContentValues(); michael@0: final ContentValues second = new ContentValues(); michael@0: first.put(BrowserContract.Clients.GUID, "abcdefghijkl"); michael@0: first.put(BrowserContract.Clients.NAME, "Frist Psot"); michael@0: first.put(BrowserContract.Clients.LAST_MODIFIED, now + 1); michael@0: second.put(BrowserContract.Clients.GUID, "mnopqrstuvwx"); michael@0: second.put(BrowserContract.Clients.NAME, "Second!!1!"); michael@0: second.put(BrowserContract.Clients.LAST_MODIFIED, now + 2); michael@0: michael@0: ContentValues[] values = new ContentValues[] { first, second }; michael@0: final int inserted = clientsClient.bulkInsert(uri, values); michael@0: assertEquals(2, inserted); michael@0: michael@0: final String since = BrowserContract.Clients.LAST_MODIFIED + " >= ?"; michael@0: final String[] nowArg = new String[] { String.valueOf(now) }; michael@0: final String guidAscending = BrowserContract.Clients.GUID + " ASC"; michael@0: Cursor cursor = clientsClient.query(uri, null, since, nowArg, guidAscending); michael@0: michael@0: assertNotNull(cursor); michael@0: try { michael@0: assertTrue(cursor.moveToFirst()); michael@0: assertEquals(2, cursor.getCount()); michael@0: michael@0: final String g1 = cursor.getString(cursor.getColumnIndexOrThrow(BrowserContract.Clients.GUID)); michael@0: final String n1 = cursor.getString(cursor.getColumnIndexOrThrow(BrowserContract.Clients.NAME)); michael@0: final long m1 = cursor.getLong(cursor.getColumnIndexOrThrow(BrowserContract.Clients.LAST_MODIFIED)); michael@0: assertEquals(first.get(BrowserContract.Clients.GUID), g1); michael@0: assertEquals(first.get(BrowserContract.Clients.NAME), n1); michael@0: assertEquals(now + 1, m1); michael@0: michael@0: assertTrue(cursor.moveToNext()); michael@0: final String g2 = cursor.getString(cursor.getColumnIndexOrThrow(BrowserContract.Clients.GUID)); michael@0: final String n2 = cursor.getString(cursor.getColumnIndexOrThrow(BrowserContract.Clients.NAME)); michael@0: final long m2 = cursor.getLong(cursor.getColumnIndexOrThrow(BrowserContract.Clients.LAST_MODIFIED)); michael@0: assertEquals(second.get(BrowserContract.Clients.GUID), g2); michael@0: assertEquals(second.get(BrowserContract.Clients.NAME), n2); michael@0: assertEquals(now + 2, m2); michael@0: michael@0: assertFalse(cursor.moveToNext()); michael@0: } finally { michael@0: cursor.close(); michael@0: } michael@0: michael@0: int deleted = clientsClient.delete(uri, null, null); michael@0: assertEquals(2, deleted); michael@0: } michael@0: michael@0: public void testTabFromCursor() throws Exception { michael@0: final ContentProviderClient tabsClient = getTabsClient(); michael@0: michael@0: deleteAllTestTabs(tabsClient); michael@0: insertSomeTestTabs(tabsClient); michael@0: michael@0: final String positionAscending = BrowserContract.Tabs.POSITION + " ASC"; michael@0: Cursor cursor = null; michael@0: try { michael@0: cursor = tabsClient.query(BrowserContractHelpers.TABS_CONTENT_URI, null, TABS_CLIENT_GUID_IS, new String[] { TEST_CLIENT_GUID }, positionAscending); michael@0: assertEquals(3, cursor.getCount()); michael@0: michael@0: cursor.moveToFirst(); michael@0: final Tab parsed1 = Tab.fromCursor(cursor); michael@0: assertEquals(testTab1, parsed1); michael@0: michael@0: cursor.moveToNext(); michael@0: final Tab parsed2 = Tab.fromCursor(cursor); michael@0: assertEquals(testTab2, parsed2); michael@0: michael@0: cursor.moveToPosition(2); michael@0: final Tab parsed3 = Tab.fromCursor(cursor); michael@0: assertEquals(testTab3, parsed3); michael@0: } finally { michael@0: cursor.close(); michael@0: } michael@0: } michael@0: }