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 java.util.ArrayList; |
michael@0 | 7 | |
michael@0 | 8 | import org.json.simple.JSONArray; |
michael@0 | 9 | import org.mozilla.gecko.sync.Utils; |
michael@0 | 10 | import org.mozilla.gecko.sync.repositories.NullCursorException; |
michael@0 | 11 | import org.mozilla.gecko.sync.repositories.android.ClientsDatabase; |
michael@0 | 12 | import org.mozilla.gecko.sync.repositories.android.RepoUtils; |
michael@0 | 13 | import org.mozilla.gecko.sync.repositories.domain.ClientRecord; |
michael@0 | 14 | import org.mozilla.gecko.sync.setup.Constants; |
michael@0 | 15 | |
michael@0 | 16 | import android.database.Cursor; |
michael@0 | 17 | import android.test.AndroidTestCase; |
michael@0 | 18 | |
michael@0 | 19 | public class TestClientsDatabase extends AndroidTestCase { |
michael@0 | 20 | |
michael@0 | 21 | protected ClientsDatabase db; |
michael@0 | 22 | |
michael@0 | 23 | public void setUp() { |
michael@0 | 24 | db = new ClientsDatabase(mContext); |
michael@0 | 25 | db.wipeDB(); |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | public void testStoreAndFetch() { |
michael@0 | 29 | ClientRecord record = new ClientRecord(); |
michael@0 | 30 | String profileConst = Constants.DEFAULT_PROFILE; |
michael@0 | 31 | db.store(profileConst, record); |
michael@0 | 32 | |
michael@0 | 33 | Cursor cur = null; |
michael@0 | 34 | try { |
michael@0 | 35 | // Test stored item gets fetched correctly. |
michael@0 | 36 | cur = db.fetchClientsCursor(record.guid, profileConst); |
michael@0 | 37 | assertTrue(cur.moveToFirst()); |
michael@0 | 38 | assertEquals(1, cur.getCount()); |
michael@0 | 39 | |
michael@0 | 40 | String guid = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_ACCOUNT_GUID); |
michael@0 | 41 | String profileId = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_PROFILE); |
michael@0 | 42 | String clientName = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_NAME); |
michael@0 | 43 | String clientType = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_TYPE); |
michael@0 | 44 | |
michael@0 | 45 | assertEquals(record.guid, guid); |
michael@0 | 46 | assertEquals(profileConst, profileId); |
michael@0 | 47 | assertEquals(record.name, clientName); |
michael@0 | 48 | assertEquals(record.type, clientType); |
michael@0 | 49 | } catch (NullCursorException e) { |
michael@0 | 50 | fail("Should not have NullCursorException"); |
michael@0 | 51 | } finally { |
michael@0 | 52 | if (cur != null) { |
michael@0 | 53 | cur.close(); |
michael@0 | 54 | } |
michael@0 | 55 | } |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | public void testStoreAndFetchSpecificCommands() { |
michael@0 | 59 | String accountGUID = Utils.generateGuid(); |
michael@0 | 60 | ArrayList<String> args = new ArrayList<String>(); |
michael@0 | 61 | args.add("URI of Page"); |
michael@0 | 62 | args.add("Sender GUID"); |
michael@0 | 63 | args.add("Title of Page"); |
michael@0 | 64 | String jsonArgs = JSONArray.toJSONString(args); |
michael@0 | 65 | |
michael@0 | 66 | Cursor cur = null; |
michael@0 | 67 | try { |
michael@0 | 68 | db.store(accountGUID, "displayURI", jsonArgs); |
michael@0 | 69 | |
michael@0 | 70 | // This row should not show up in the fetch. |
michael@0 | 71 | args.add("Another arg."); |
michael@0 | 72 | db.store(accountGUID, "displayURI", JSONArray.toJSONString(args)); |
michael@0 | 73 | |
michael@0 | 74 | // Test stored item gets fetched correctly. |
michael@0 | 75 | cur = db.fetchSpecificCommand(accountGUID, "displayURI", jsonArgs); |
michael@0 | 76 | assertTrue(cur.moveToFirst()); |
michael@0 | 77 | assertEquals(1, cur.getCount()); |
michael@0 | 78 | |
michael@0 | 79 | String guid = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_ACCOUNT_GUID); |
michael@0 | 80 | String commandType = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_COMMAND); |
michael@0 | 81 | String fetchedArgs = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_ARGS); |
michael@0 | 82 | |
michael@0 | 83 | assertEquals(accountGUID, guid); |
michael@0 | 84 | assertEquals("displayURI", commandType); |
michael@0 | 85 | assertEquals(jsonArgs, fetchedArgs); |
michael@0 | 86 | } catch (NullCursorException e) { |
michael@0 | 87 | fail("Should not have NullCursorException"); |
michael@0 | 88 | } finally { |
michael@0 | 89 | if (cur != null) { |
michael@0 | 90 | cur.close(); |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | public void testFetchCommandsForClient() { |
michael@0 | 96 | String accountGUID = Utils.generateGuid(); |
michael@0 | 97 | ArrayList<String> args = new ArrayList<String>(); |
michael@0 | 98 | args.add("URI of Page"); |
michael@0 | 99 | args.add("Sender GUID"); |
michael@0 | 100 | args.add("Title of Page"); |
michael@0 | 101 | String jsonArgs = JSONArray.toJSONString(args); |
michael@0 | 102 | |
michael@0 | 103 | Cursor cur = null; |
michael@0 | 104 | try { |
michael@0 | 105 | db.store(accountGUID, "displayURI", jsonArgs); |
michael@0 | 106 | |
michael@0 | 107 | // This row should ALSO show up in the fetch. |
michael@0 | 108 | args.add("Another arg."); |
michael@0 | 109 | db.store(accountGUID, "displayURI", JSONArray.toJSONString(args)); |
michael@0 | 110 | |
michael@0 | 111 | // Test both stored items with the same GUID but different command are fetched. |
michael@0 | 112 | cur = db.fetchCommandsForClient(accountGUID); |
michael@0 | 113 | assertTrue(cur.moveToFirst()); |
michael@0 | 114 | assertEquals(2, cur.getCount()); |
michael@0 | 115 | } catch (NullCursorException e) { |
michael@0 | 116 | fail("Should not have NullCursorException"); |
michael@0 | 117 | } finally { |
michael@0 | 118 | if (cur != null) { |
michael@0 | 119 | cur.close(); |
michael@0 | 120 | } |
michael@0 | 121 | } |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | @SuppressWarnings("resource") |
michael@0 | 125 | public void testDelete() { |
michael@0 | 126 | ClientRecord record1 = new ClientRecord(); |
michael@0 | 127 | ClientRecord record2 = new ClientRecord(); |
michael@0 | 128 | String profileConst = Constants.DEFAULT_PROFILE; |
michael@0 | 129 | |
michael@0 | 130 | db.store(profileConst, record1); |
michael@0 | 131 | db.store(profileConst, record2); |
michael@0 | 132 | |
michael@0 | 133 | Cursor cur = null; |
michael@0 | 134 | try { |
michael@0 | 135 | // Test record doesn't exist after delete. |
michael@0 | 136 | db.deleteClient(record1.guid, profileConst); |
michael@0 | 137 | cur = db.fetchClientsCursor(record1.guid, profileConst); |
michael@0 | 138 | assertFalse(cur.moveToFirst()); |
michael@0 | 139 | assertEquals(0, cur.getCount()); |
michael@0 | 140 | |
michael@0 | 141 | // Test record2 still there after deleting record1. |
michael@0 | 142 | cur = db.fetchClientsCursor(record2.guid, profileConst); |
michael@0 | 143 | assertTrue(cur.moveToFirst()); |
michael@0 | 144 | assertEquals(1, cur.getCount()); |
michael@0 | 145 | |
michael@0 | 146 | String guid = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_ACCOUNT_GUID); |
michael@0 | 147 | String profileId = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_PROFILE); |
michael@0 | 148 | String clientName = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_NAME); |
michael@0 | 149 | String clientType = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_TYPE); |
michael@0 | 150 | |
michael@0 | 151 | assertEquals(record2.guid, guid); |
michael@0 | 152 | assertEquals(profileConst, profileId); |
michael@0 | 153 | assertEquals(record2.name, clientName); |
michael@0 | 154 | assertEquals(record2.type, clientType); |
michael@0 | 155 | } catch (NullCursorException e) { |
michael@0 | 156 | fail("Should not have NullCursorException"); |
michael@0 | 157 | } finally { |
michael@0 | 158 | if (cur != null) { |
michael@0 | 159 | cur.close(); |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | @SuppressWarnings("resource") |
michael@0 | 165 | public void testWipe() { |
michael@0 | 166 | ClientRecord record1 = new ClientRecord(); |
michael@0 | 167 | ClientRecord record2 = new ClientRecord(); |
michael@0 | 168 | String profileConst = Constants.DEFAULT_PROFILE; |
michael@0 | 169 | |
michael@0 | 170 | db.store(profileConst, record1); |
michael@0 | 171 | db.store(profileConst, record2); |
michael@0 | 172 | |
michael@0 | 173 | |
michael@0 | 174 | Cursor cur = null; |
michael@0 | 175 | try { |
michael@0 | 176 | // Test before wipe the records are there. |
michael@0 | 177 | cur = db.fetchClientsCursor(record2.guid, profileConst); |
michael@0 | 178 | assertTrue(cur.moveToFirst()); |
michael@0 | 179 | assertEquals(1, cur.getCount()); |
michael@0 | 180 | cur = db.fetchClientsCursor(record2.guid, profileConst); |
michael@0 | 181 | assertTrue(cur.moveToFirst()); |
michael@0 | 182 | assertEquals(1, cur.getCount()); |
michael@0 | 183 | |
michael@0 | 184 | // Test after wipe neither record exists. |
michael@0 | 185 | db.wipeClientsTable(); |
michael@0 | 186 | cur = db.fetchClientsCursor(record2.guid, profileConst); |
michael@0 | 187 | assertFalse(cur.moveToFirst()); |
michael@0 | 188 | assertEquals(0, cur.getCount()); |
michael@0 | 189 | cur = db.fetchClientsCursor(record1.guid, profileConst); |
michael@0 | 190 | assertFalse(cur.moveToFirst()); |
michael@0 | 191 | assertEquals(0, cur.getCount()); |
michael@0 | 192 | } catch (NullCursorException e) { |
michael@0 | 193 | fail("Should not have NullCursorException"); |
michael@0 | 194 | } finally { |
michael@0 | 195 | if (cur != null) { |
michael@0 | 196 | cur.close(); |
michael@0 | 197 | } |
michael@0 | 198 | } |
michael@0 | 199 | } |
michael@0 | 200 | } |