1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/tests/background/junit3/src/db/TestClientsDatabase.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,200 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +package org.mozilla.gecko.background.db; 1.8 + 1.9 +import java.util.ArrayList; 1.10 + 1.11 +import org.json.simple.JSONArray; 1.12 +import org.mozilla.gecko.sync.Utils; 1.13 +import org.mozilla.gecko.sync.repositories.NullCursorException; 1.14 +import org.mozilla.gecko.sync.repositories.android.ClientsDatabase; 1.15 +import org.mozilla.gecko.sync.repositories.android.RepoUtils; 1.16 +import org.mozilla.gecko.sync.repositories.domain.ClientRecord; 1.17 +import org.mozilla.gecko.sync.setup.Constants; 1.18 + 1.19 +import android.database.Cursor; 1.20 +import android.test.AndroidTestCase; 1.21 + 1.22 +public class TestClientsDatabase extends AndroidTestCase { 1.23 + 1.24 + protected ClientsDatabase db; 1.25 + 1.26 + public void setUp() { 1.27 + db = new ClientsDatabase(mContext); 1.28 + db.wipeDB(); 1.29 + } 1.30 + 1.31 + public void testStoreAndFetch() { 1.32 + ClientRecord record = new ClientRecord(); 1.33 + String profileConst = Constants.DEFAULT_PROFILE; 1.34 + db.store(profileConst, record); 1.35 + 1.36 + Cursor cur = null; 1.37 + try { 1.38 + // Test stored item gets fetched correctly. 1.39 + cur = db.fetchClientsCursor(record.guid, profileConst); 1.40 + assertTrue(cur.moveToFirst()); 1.41 + assertEquals(1, cur.getCount()); 1.42 + 1.43 + String guid = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_ACCOUNT_GUID); 1.44 + String profileId = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_PROFILE); 1.45 + String clientName = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_NAME); 1.46 + String clientType = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_TYPE); 1.47 + 1.48 + assertEquals(record.guid, guid); 1.49 + assertEquals(profileConst, profileId); 1.50 + assertEquals(record.name, clientName); 1.51 + assertEquals(record.type, clientType); 1.52 + } catch (NullCursorException e) { 1.53 + fail("Should not have NullCursorException"); 1.54 + } finally { 1.55 + if (cur != null) { 1.56 + cur.close(); 1.57 + } 1.58 + } 1.59 + } 1.60 + 1.61 + public void testStoreAndFetchSpecificCommands() { 1.62 + String accountGUID = Utils.generateGuid(); 1.63 + ArrayList<String> args = new ArrayList<String>(); 1.64 + args.add("URI of Page"); 1.65 + args.add("Sender GUID"); 1.66 + args.add("Title of Page"); 1.67 + String jsonArgs = JSONArray.toJSONString(args); 1.68 + 1.69 + Cursor cur = null; 1.70 + try { 1.71 + db.store(accountGUID, "displayURI", jsonArgs); 1.72 + 1.73 + // This row should not show up in the fetch. 1.74 + args.add("Another arg."); 1.75 + db.store(accountGUID, "displayURI", JSONArray.toJSONString(args)); 1.76 + 1.77 + // Test stored item gets fetched correctly. 1.78 + cur = db.fetchSpecificCommand(accountGUID, "displayURI", jsonArgs); 1.79 + assertTrue(cur.moveToFirst()); 1.80 + assertEquals(1, cur.getCount()); 1.81 + 1.82 + String guid = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_ACCOUNT_GUID); 1.83 + String commandType = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_COMMAND); 1.84 + String fetchedArgs = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_ARGS); 1.85 + 1.86 + assertEquals(accountGUID, guid); 1.87 + assertEquals("displayURI", commandType); 1.88 + assertEquals(jsonArgs, fetchedArgs); 1.89 + } catch (NullCursorException e) { 1.90 + fail("Should not have NullCursorException"); 1.91 + } finally { 1.92 + if (cur != null) { 1.93 + cur.close(); 1.94 + } 1.95 + } 1.96 + } 1.97 + 1.98 + public void testFetchCommandsForClient() { 1.99 + String accountGUID = Utils.generateGuid(); 1.100 + ArrayList<String> args = new ArrayList<String>(); 1.101 + args.add("URI of Page"); 1.102 + args.add("Sender GUID"); 1.103 + args.add("Title of Page"); 1.104 + String jsonArgs = JSONArray.toJSONString(args); 1.105 + 1.106 + Cursor cur = null; 1.107 + try { 1.108 + db.store(accountGUID, "displayURI", jsonArgs); 1.109 + 1.110 + // This row should ALSO show up in the fetch. 1.111 + args.add("Another arg."); 1.112 + db.store(accountGUID, "displayURI", JSONArray.toJSONString(args)); 1.113 + 1.114 + // Test both stored items with the same GUID but different command are fetched. 1.115 + cur = db.fetchCommandsForClient(accountGUID); 1.116 + assertTrue(cur.moveToFirst()); 1.117 + assertEquals(2, cur.getCount()); 1.118 + } catch (NullCursorException e) { 1.119 + fail("Should not have NullCursorException"); 1.120 + } finally { 1.121 + if (cur != null) { 1.122 + cur.close(); 1.123 + } 1.124 + } 1.125 + } 1.126 + 1.127 + @SuppressWarnings("resource") 1.128 + public void testDelete() { 1.129 + ClientRecord record1 = new ClientRecord(); 1.130 + ClientRecord record2 = new ClientRecord(); 1.131 + String profileConst = Constants.DEFAULT_PROFILE; 1.132 + 1.133 + db.store(profileConst, record1); 1.134 + db.store(profileConst, record2); 1.135 + 1.136 + Cursor cur = null; 1.137 + try { 1.138 + // Test record doesn't exist after delete. 1.139 + db.deleteClient(record1.guid, profileConst); 1.140 + cur = db.fetchClientsCursor(record1.guid, profileConst); 1.141 + assertFalse(cur.moveToFirst()); 1.142 + assertEquals(0, cur.getCount()); 1.143 + 1.144 + // Test record2 still there after deleting record1. 1.145 + cur = db.fetchClientsCursor(record2.guid, profileConst); 1.146 + assertTrue(cur.moveToFirst()); 1.147 + assertEquals(1, cur.getCount()); 1.148 + 1.149 + String guid = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_ACCOUNT_GUID); 1.150 + String profileId = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_PROFILE); 1.151 + String clientName = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_NAME); 1.152 + String clientType = RepoUtils.getStringFromCursor(cur, ClientsDatabase.COL_TYPE); 1.153 + 1.154 + assertEquals(record2.guid, guid); 1.155 + assertEquals(profileConst, profileId); 1.156 + assertEquals(record2.name, clientName); 1.157 + assertEquals(record2.type, clientType); 1.158 + } catch (NullCursorException e) { 1.159 + fail("Should not have NullCursorException"); 1.160 + } finally { 1.161 + if (cur != null) { 1.162 + cur.close(); 1.163 + } 1.164 + } 1.165 + } 1.166 + 1.167 + @SuppressWarnings("resource") 1.168 + public void testWipe() { 1.169 + ClientRecord record1 = new ClientRecord(); 1.170 + ClientRecord record2 = new ClientRecord(); 1.171 + String profileConst = Constants.DEFAULT_PROFILE; 1.172 + 1.173 + db.store(profileConst, record1); 1.174 + db.store(profileConst, record2); 1.175 + 1.176 + 1.177 + Cursor cur = null; 1.178 + try { 1.179 + // Test before wipe the records are there. 1.180 + cur = db.fetchClientsCursor(record2.guid, profileConst); 1.181 + assertTrue(cur.moveToFirst()); 1.182 + assertEquals(1, cur.getCount()); 1.183 + cur = db.fetchClientsCursor(record2.guid, profileConst); 1.184 + assertTrue(cur.moveToFirst()); 1.185 + assertEquals(1, cur.getCount()); 1.186 + 1.187 + // Test after wipe neither record exists. 1.188 + db.wipeClientsTable(); 1.189 + cur = db.fetchClientsCursor(record2.guid, profileConst); 1.190 + assertFalse(cur.moveToFirst()); 1.191 + assertEquals(0, cur.getCount()); 1.192 + cur = db.fetchClientsCursor(record1.guid, profileConst); 1.193 + assertFalse(cur.moveToFirst()); 1.194 + assertEquals(0, cur.getCount()); 1.195 + } catch (NullCursorException e) { 1.196 + fail("Should not have NullCursorException"); 1.197 + } finally { 1.198 + if (cur != null) { 1.199 + cur.close(); 1.200 + } 1.201 + } 1.202 + } 1.203 +}