Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | package org.mozilla.gecko.background.db; |
michael@0 | 5 | |
michael@0 | 6 | import org.json.simple.JSONArray; |
michael@0 | 7 | import org.mozilla.gecko.db.BrowserContract; |
michael@0 | 8 | import org.mozilla.gecko.sync.repositories.android.BrowserContractHelpers; |
michael@0 | 9 | |
michael@0 | 10 | import android.app.Activity; |
michael@0 | 11 | import android.content.ContentProviderClient; |
michael@0 | 12 | import android.content.ContentResolver; |
michael@0 | 13 | import android.content.ContentValues; |
michael@0 | 14 | import android.database.Cursor; |
michael@0 | 15 | import android.net.Uri; |
michael@0 | 16 | import android.os.RemoteException; |
michael@0 | 17 | import android.test.ActivityInstrumentationTestCase2; |
michael@0 | 18 | |
michael@0 | 19 | /** |
michael@0 | 20 | * Exercise Fennec's tabs provider. |
michael@0 | 21 | * |
michael@0 | 22 | * @author rnewman |
michael@0 | 23 | * |
michael@0 | 24 | */ |
michael@0 | 25 | public class TestFennecTabsStorage extends ActivityInstrumentationTestCase2<Activity> { |
michael@0 | 26 | public static final String TEST_CLIENT_GUID = "test guid"; // Real GUIDs never contain spaces. |
michael@0 | 27 | public static final String TEST_CLIENT_NAME = "test client name"; |
michael@0 | 28 | |
michael@0 | 29 | public static final String TABS_CLIENT_GUID_IS = BrowserContract.Tabs.CLIENT_GUID + " = ?"; |
michael@0 | 30 | |
michael@0 | 31 | protected Tab testTab1; |
michael@0 | 32 | protected Tab testTab2; |
michael@0 | 33 | protected Tab testTab3; |
michael@0 | 34 | |
michael@0 | 35 | public TestFennecTabsStorage() { |
michael@0 | 36 | super(Activity.class); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | protected ContentProviderClient getClientsClient() { |
michael@0 | 40 | final ContentResolver cr = getInstrumentation().getTargetContext().getApplicationContext().getContentResolver(); |
michael@0 | 41 | return cr.acquireContentProviderClient(BrowserContractHelpers.CLIENTS_CONTENT_URI); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | protected ContentProviderClient getTabsClient() { |
michael@0 | 45 | final ContentResolver cr = getInstrumentation().getTargetContext().getApplicationContext().getContentResolver(); |
michael@0 | 46 | return cr.acquireContentProviderClient(BrowserContractHelpers.TABS_CONTENT_URI); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | protected int deleteAllTestTabs(final ContentProviderClient tabsClient) throws RemoteException { |
michael@0 | 50 | if (tabsClient == null) { |
michael@0 | 51 | return -1; |
michael@0 | 52 | } |
michael@0 | 53 | return tabsClient.delete(BrowserContractHelpers.TABS_CONTENT_URI, TABS_CLIENT_GUID_IS, new String[] { TEST_CLIENT_GUID }); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | @Override |
michael@0 | 57 | protected void tearDown() throws Exception { |
michael@0 | 58 | deleteAllTestTabs(getTabsClient()); |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | @SuppressWarnings("unchecked") |
michael@0 | 62 | protected void insertSomeTestTabs(ContentProviderClient tabsClient) throws RemoteException { |
michael@0 | 63 | final JSONArray history1 = new JSONArray(); |
michael@0 | 64 | history1.add("http://test.com/test1.html"); |
michael@0 | 65 | testTab1 = new Tab("test title 1", "http://test.com/test1.png", history1, 1000); |
michael@0 | 66 | |
michael@0 | 67 | final JSONArray history2 = new JSONArray(); |
michael@0 | 68 | history2.add("http://test.com/test2.html#1"); |
michael@0 | 69 | history2.add("http://test.com/test2.html#2"); |
michael@0 | 70 | history2.add("http://test.com/test2.html#3"); |
michael@0 | 71 | testTab2 = new Tab("test title 2", "http://test.com/test2.png", history2, 2000); |
michael@0 | 72 | |
michael@0 | 73 | final JSONArray history3 = new JSONArray(); |
michael@0 | 74 | history3.add("http://test.com/test3.html#1"); |
michael@0 | 75 | history3.add("http://test.com/test3.html#2"); |
michael@0 | 76 | testTab3 = new Tab("test title 3", "http://test.com/test3.png", history3, 3000); |
michael@0 | 77 | |
michael@0 | 78 | tabsClient.insert(BrowserContractHelpers.TABS_CONTENT_URI, testTab1.toContentValues(TEST_CLIENT_GUID, 0)); |
michael@0 | 79 | tabsClient.insert(BrowserContractHelpers.TABS_CONTENT_URI, testTab2.toContentValues(TEST_CLIENT_GUID, 1)); |
michael@0 | 80 | tabsClient.insert(BrowserContractHelpers.TABS_CONTENT_URI, testTab3.toContentValues(TEST_CLIENT_GUID, 2)); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | // Sanity. |
michael@0 | 84 | public void testObtainCP() { |
michael@0 | 85 | final ContentProviderClient clientsClient = getClientsClient(); |
michael@0 | 86 | assertNotNull(clientsClient); |
michael@0 | 87 | clientsClient.release(); |
michael@0 | 88 | |
michael@0 | 89 | final ContentProviderClient tabsClient = getTabsClient(); |
michael@0 | 90 | assertNotNull(tabsClient); |
michael@0 | 91 | tabsClient.release(); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | public void testWipeClients() throws RemoteException { |
michael@0 | 95 | final Uri uri = BrowserContractHelpers.CLIENTS_CONTENT_URI; |
michael@0 | 96 | final ContentProviderClient clientsClient = getClientsClient(); |
michael@0 | 97 | |
michael@0 | 98 | // Have to ensure that it's empty… |
michael@0 | 99 | clientsClient.delete(uri, null, null); |
michael@0 | 100 | |
michael@0 | 101 | int deleted = clientsClient.delete(uri, null, null); |
michael@0 | 102 | assertEquals(0, deleted); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | public void testWipeTabs() throws RemoteException { |
michael@0 | 106 | final ContentProviderClient tabsClient = getTabsClient(); |
michael@0 | 107 | |
michael@0 | 108 | // Have to ensure that it's empty… |
michael@0 | 109 | deleteAllTestTabs(tabsClient); |
michael@0 | 110 | |
michael@0 | 111 | int deleted = deleteAllTestTabs(tabsClient); |
michael@0 | 112 | assertEquals(0, deleted); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | public void testStoreAndRetrieveClients() throws RemoteException { |
michael@0 | 116 | final Uri uri = BrowserContractHelpers.CLIENTS_CONTENT_URI; |
michael@0 | 117 | final ContentProviderClient clientsClient = getClientsClient(); |
michael@0 | 118 | |
michael@0 | 119 | // Have to ensure that it's empty… |
michael@0 | 120 | clientsClient.delete(uri, null, null); |
michael@0 | 121 | |
michael@0 | 122 | final long now = System.currentTimeMillis(); |
michael@0 | 123 | final ContentValues first = new ContentValues(); |
michael@0 | 124 | final ContentValues second = new ContentValues(); |
michael@0 | 125 | first.put(BrowserContract.Clients.GUID, "abcdefghijkl"); |
michael@0 | 126 | first.put(BrowserContract.Clients.NAME, "Frist Psot"); |
michael@0 | 127 | first.put(BrowserContract.Clients.LAST_MODIFIED, now + 1); |
michael@0 | 128 | second.put(BrowserContract.Clients.GUID, "mnopqrstuvwx"); |
michael@0 | 129 | second.put(BrowserContract.Clients.NAME, "Second!!1!"); |
michael@0 | 130 | second.put(BrowserContract.Clients.LAST_MODIFIED, now + 2); |
michael@0 | 131 | |
michael@0 | 132 | ContentValues[] values = new ContentValues[] { first, second }; |
michael@0 | 133 | final int inserted = clientsClient.bulkInsert(uri, values); |
michael@0 | 134 | assertEquals(2, inserted); |
michael@0 | 135 | |
michael@0 | 136 | final String since = BrowserContract.Clients.LAST_MODIFIED + " >= ?"; |
michael@0 | 137 | final String[] nowArg = new String[] { String.valueOf(now) }; |
michael@0 | 138 | final String guidAscending = BrowserContract.Clients.GUID + " ASC"; |
michael@0 | 139 | Cursor cursor = clientsClient.query(uri, null, since, nowArg, guidAscending); |
michael@0 | 140 | |
michael@0 | 141 | assertNotNull(cursor); |
michael@0 | 142 | try { |
michael@0 | 143 | assertTrue(cursor.moveToFirst()); |
michael@0 | 144 | assertEquals(2, cursor.getCount()); |
michael@0 | 145 | |
michael@0 | 146 | final String g1 = cursor.getString(cursor.getColumnIndexOrThrow(BrowserContract.Clients.GUID)); |
michael@0 | 147 | final String n1 = cursor.getString(cursor.getColumnIndexOrThrow(BrowserContract.Clients.NAME)); |
michael@0 | 148 | final long m1 = cursor.getLong(cursor.getColumnIndexOrThrow(BrowserContract.Clients.LAST_MODIFIED)); |
michael@0 | 149 | assertEquals(first.get(BrowserContract.Clients.GUID), g1); |
michael@0 | 150 | assertEquals(first.get(BrowserContract.Clients.NAME), n1); |
michael@0 | 151 | assertEquals(now + 1, m1); |
michael@0 | 152 | |
michael@0 | 153 | assertTrue(cursor.moveToNext()); |
michael@0 | 154 | final String g2 = cursor.getString(cursor.getColumnIndexOrThrow(BrowserContract.Clients.GUID)); |
michael@0 | 155 | final String n2 = cursor.getString(cursor.getColumnIndexOrThrow(BrowserContract.Clients.NAME)); |
michael@0 | 156 | final long m2 = cursor.getLong(cursor.getColumnIndexOrThrow(BrowserContract.Clients.LAST_MODIFIED)); |
michael@0 | 157 | assertEquals(second.get(BrowserContract.Clients.GUID), g2); |
michael@0 | 158 | assertEquals(second.get(BrowserContract.Clients.NAME), n2); |
michael@0 | 159 | assertEquals(now + 2, m2); |
michael@0 | 160 | |
michael@0 | 161 | assertFalse(cursor.moveToNext()); |
michael@0 | 162 | } finally { |
michael@0 | 163 | cursor.close(); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | int deleted = clientsClient.delete(uri, null, null); |
michael@0 | 167 | assertEquals(2, deleted); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | public void testTabFromCursor() throws Exception { |
michael@0 | 171 | final ContentProviderClient tabsClient = getTabsClient(); |
michael@0 | 172 | |
michael@0 | 173 | deleteAllTestTabs(tabsClient); |
michael@0 | 174 | insertSomeTestTabs(tabsClient); |
michael@0 | 175 | |
michael@0 | 176 | final String positionAscending = BrowserContract.Tabs.POSITION + " ASC"; |
michael@0 | 177 | Cursor cursor = null; |
michael@0 | 178 | try { |
michael@0 | 179 | cursor = tabsClient.query(BrowserContractHelpers.TABS_CONTENT_URI, null, TABS_CLIENT_GUID_IS, new String[] { TEST_CLIENT_GUID }, positionAscending); |
michael@0 | 180 | assertEquals(3, cursor.getCount()); |
michael@0 | 181 | |
michael@0 | 182 | cursor.moveToFirst(); |
michael@0 | 183 | final Tab parsed1 = Tab.fromCursor(cursor); |
michael@0 | 184 | assertEquals(testTab1, parsed1); |
michael@0 | 185 | |
michael@0 | 186 | cursor.moveToNext(); |
michael@0 | 187 | final Tab parsed2 = Tab.fromCursor(cursor); |
michael@0 | 188 | assertEquals(testTab2, parsed2); |
michael@0 | 189 | |
michael@0 | 190 | cursor.moveToPosition(2); |
michael@0 | 191 | final Tab parsed3 = Tab.fromCursor(cursor); |
michael@0 | 192 | assertEquals(testTab3, parsed3); |
michael@0 | 193 | } finally { |
michael@0 | 194 | cursor.close(); |
michael@0 | 195 | } |
michael@0 | 196 | } |
michael@0 | 197 | } |