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