Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/SharedPrefsStorageTest.java

Issue 29412555: Issue 5140 - Add tests for settings storage (Closed)
Patch Set: Created April 14, 2017, 8:02 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 Eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package org.adblockplus.libadblockplus.tests;
19
20 import android.content.Context;
21 import android.content.SharedPreferences;
22 import android.test.AndroidTestCase;
23
24 import org.adblockplus.libadblockplus.android.Subscription;
25 import org.adblockplus.libadblockplus.android.settings.AdblockSettings;
26 import org.adblockplus.libadblockplus.android.settings.ConnectionType;
27 import org.adblockplus.libadblockplus.android.settings.SharedPrefsStorage;
28 import org.junit.Test;
29
30 import java.util.Collections;
31 import java.util.LinkedList;
32 import java.util.List;
33
34 public class SharedPrefsStorageTest extends AndroidTestCase
35 {
36 protected static final String NAME = "prefs";
37
38 protected SharedPreferences prefs;
39 protected SharedPrefsStorage storage;
40
41 @Override
42 protected void setUp() throws Exception
43 {
44 super.setUp();
45
46 prefs = getContext().getSharedPreferences(NAME, Context.MODE_PRIVATE);
47 prefs.edit().clear().commit();
48
49 storage = new SharedPrefsStorage(prefs);
50 storage.setCommit(true);
51 }
52
53 @Test
54 public void testNeverSaved()
55 {
56 final AdblockSettings settings = storage.load();
57 assertNull(settings);
58 }
59
60 protected void _testAdblockEnabled()
61 {
62 final AdblockSettings savedSettings = new AdblockSettings();
63 savedSettings.setAdblockEnabled(true);
64 storage.save(savedSettings);
65
66 AdblockSettings loadedSettings = storage.load();
67 assertEquals(savedSettings.isAdblockEnabled(), loadedSettings.isAdblockEnabl ed());
68
69 savedSettings.setAdblockEnabled(false);
70 storage.save(savedSettings);
71
72 loadedSettings = storage.load();
73 assertEquals(savedSettings.isAdblockEnabled(), loadedSettings.isAdblockEnabl ed());
74 }
75
76 @Test
77 public void testAdblockEnabled()
78 {
79 storage.setCommit(true);
80 _testAdblockEnabled();
81 }
82
83 @Test
84 public void testAdblockEnabled_Apply()
85 {
86 storage.setCommit(false); // commit is sync (for `true`), apply is async (fo r `false`)
87 _testAdblockEnabled();
88 }
89
90 @Test
91 public void testAcceptableAdsEnabled()
92 {
93 final AdblockSettings savedSettings = new AdblockSettings();
94 savedSettings.setAcceptableAdsEnabled(true);
95 storage.save(savedSettings);
96
97 AdblockSettings loadedSettings = storage.load();
98 assertEquals(savedSettings.isAcceptableAdsEnabled(), loadedSettings.isAccept ableAdsEnabled());
99
100 savedSettings.setAcceptableAdsEnabled(false);
101 storage.save(savedSettings);
102
103 loadedSettings = storage.load();
104 assertEquals(savedSettings.isAcceptableAdsEnabled(), loadedSettings.isAccept ableAdsEnabled());
105 }
106
107 @Test
108 public void testSubscriptions_Null()
109 {
110 final AdblockSettings savedSettings = new AdblockSettings();
111 savedSettings.setAdblockEnabled(true);
112 assertNull(savedSettings.getSubscriptions());
113 storage.save(savedSettings);
114
115 final AdblockSettings loadedSettings = storage.load();
116 assertNull(loadedSettings.getSubscriptions());
117 }
118
119 @Test
120 public void testSubscriptions_Empty()
121 {
122 final AdblockSettings savedSettings = new AdblockSettings();
123 savedSettings.setAdblockEnabled(true);
124 savedSettings.setSubscriptions(Collections.<Subscription>emptyList());
125 storage.save(savedSettings);
126
127 final AdblockSettings loadedSettings = storage.load();
128 assertNotNull(loadedSettings.getSubscriptions());
129 assertEquals(0, loadedSettings.getSubscriptions().size());
130 }
131
132 @Test
133 public void testSubscriptions_OneValue()
134 {
135 final AdblockSettings savedSettings = new AdblockSettings();
136 savedSettings.setAdblockEnabled(true);
137
138 final List<Subscription> subscriptions = new LinkedList<Subscription>();
139 final Subscription savedSubscription = new Subscription();
140 savedSubscription.title = "Title";
141 savedSubscription.url = "URL";
142 subscriptions.add(savedSubscription);
143
144 savedSettings.setSubscriptions(subscriptions);
145 storage.save(savedSettings);
146
147 final AdblockSettings loadedSettings = storage.load();
148 assertNotNull(loadedSettings.getSubscriptions());
149 assertEquals(1, loadedSettings.getSubscriptions().size());
150
151 final Subscription loadedSubscription = loadedSettings.getSubscriptions().ge t(0);
152 assertEquals(savedSubscription.title, loadedSubscription.title);
153 assertEquals(savedSubscription.url, loadedSubscription.url);
154
155 // 'author', 'homepage', 'specialization', 'prefixes' settings are not saved by SharedPrefsStorage
anton 2017/04/14 08:14:01 All that fields are NOT saved because they were no
156 }
157
158 @Test
159 public void testSubscriptions_OneValueNonEnglishTitle()
160 {
161 final AdblockSettings savedSettings = new AdblockSettings();
162 savedSettings.setAdblockEnabled(true);
163
164 final List<Subscription> subscriptions = new LinkedList<Subscription>();
165 final Subscription savedSubscription = new Subscription();
166 savedSubscription.title = "Заголовок"; // non-English characters
167 savedSubscription.url = "URL";
168 subscriptions.add(savedSubscription);
169
170 savedSettings.setSubscriptions(subscriptions);
171 storage.save(savedSettings);
172
173 final AdblockSettings loadedSettings = storage.load();
174 assertNotNull(loadedSettings.getSubscriptions());
175 assertEquals(1, loadedSettings.getSubscriptions().size());
176
177 final Subscription loadedSubscription = loadedSettings.getSubscriptions().ge t(0);
178 assertEquals(savedSubscription.title, loadedSubscription.title);
179 assertEquals(savedSubscription.url, loadedSubscription.url);
180
181 // 'author', 'homepage', 'specialization', 'prefixes' settings are not saved by SharedPrefsStorage
182 }
183
184 @Test
185 public void testSubscriptions_OneValueNonEnglishUrl()
186 {
187 final AdblockSettings savedSettings = new AdblockSettings();
188 savedSettings.setAdblockEnabled(true);
189
190 final List<Subscription> subscriptions = new LinkedList<Subscription>();
191 final Subscription savedSubscription = new Subscription();
192 savedSubscription.title = "Title";
193 savedSubscription.url = "http://почта.рф"; // non-English characters
194 subscriptions.add(savedSubscription);
195
196 savedSettings.setSubscriptions(subscriptions);
197 storage.save(savedSettings);
198
199 final AdblockSettings loadedSettings = storage.load();
200 assertNotNull(loadedSettings.getSubscriptions());
201 assertEquals(1, loadedSettings.getSubscriptions().size());
202
203 final Subscription loadedSubscription = loadedSettings.getSubscriptions().ge t(0);
204 assertEquals(savedSubscription.title, loadedSubscription.title);
205 assertEquals(savedSubscription.url, loadedSubscription.url);
206
207 // 'author', 'homepage', 'specialization', 'prefixes' settings are not saved by SharedPrefsStorage
208 }
209
210 @Test
211 public void testSubscriptions_MultipleValues()
212 {
213 AdblockSettings savedSettings = new AdblockSettings();
214 savedSettings.setAdblockEnabled(true);
215
216 final List<Subscription> subscriptions = new LinkedList<Subscription>();
217 final Subscription savedSubscription1 = new Subscription();
218 savedSubscription1.title = "Title1";
219 savedSubscription1.url = "URL1";
220 subscriptions.add(savedSubscription1);
221
222 final Subscription savedSubscription2 = new Subscription();
223 savedSubscription2.title = "Title2";
224 savedSubscription2.url = "URL2";
225 subscriptions.add(savedSubscription2);
226
227 savedSettings.setSubscriptions(subscriptions);
228 storage.save(savedSettings);
229
230 final AdblockSettings loadedSettings = storage.load();
231 assertNotNull(loadedSettings.getSubscriptions());
232 assertEquals(2, loadedSettings.getSubscriptions().size());
233
234 final Subscription loadedSubscription1 = loadedSettings.getSubscriptions().g et(0);
235 assertEquals(savedSubscription1.title, loadedSubscription1.title);
236 assertEquals(savedSubscription1.url, loadedSubscription1.url);
237
238 final Subscription loadedSubscription2 = loadedSettings.getSubscriptions().g et(1);
239 assertEquals(savedSubscription2.title, loadedSubscription2.title);
240 assertEquals(savedSubscription2.url, loadedSubscription2.url);
241 }
242
243 @Test
244 public void testWhitelistedDomains_Null()
245 {
246 final AdblockSettings savedSettings = new AdblockSettings();
247 savedSettings.setAdblockEnabled(true);
248 assertNull(savedSettings.getWhitelistedDomains());
249 storage.save(savedSettings);
250
251 final AdblockSettings loadedSettings = storage.load();
252 assertNull(loadedSettings.getWhitelistedDomains());
253 }
254
255 @Test
256 public void testWhitelistedDomains_Empty()
257 {
258 final AdblockSettings savedSettings = new AdblockSettings();
259 savedSettings.setAdblockEnabled(true);
260 savedSettings.setWhitelistedDomains(Collections.<String>emptyList());
261 storage.save(savedSettings);
262
263 final AdblockSettings loadedSettings = storage.load();
264 assertNotNull(loadedSettings.getWhitelistedDomains());
265 assertEquals(0, loadedSettings.getWhitelistedDomains().size());
266 }
267
268 @Test
269 public void testWhitelistedDomains_SingleValue()
270 {
271 final AdblockSettings savedSettings = new AdblockSettings();
272 savedSettings.setAdblockEnabled(true);
273
274 final List<String> whitelistedDomains = new LinkedList<String>();
275 whitelistedDomains.add("http://www.domain1.com");
276
277 savedSettings.setWhitelistedDomains(whitelistedDomains);
278 storage.save(savedSettings);
279
280 final AdblockSettings loadedSettings = storage.load();
281 assertNotNull(loadedSettings.getWhitelistedDomains());
282 assertEquals(1, loadedSettings.getWhitelistedDomains().size());
283
284 assertEquals(
285 savedSettings.getWhitelistedDomains().get(0),
286 loadedSettings.getWhitelistedDomains().get(0));
287 }
288
289 @Test
290 public void testWhitelistedDomains_SingleValueNonEnglish()
291 {
292 final AdblockSettings savedSettings = new AdblockSettings();
293 savedSettings.setAdblockEnabled(true);
294
295 final List<String> whitelistedDomains = new LinkedList<String>();
296 whitelistedDomains.add("http://почта.рф");
297
298 savedSettings.setWhitelistedDomains(whitelistedDomains);
299 storage.save(savedSettings);
300
301 final AdblockSettings loadedSettings = storage.load();
302 assertNotNull(loadedSettings.getWhitelistedDomains());
303 assertEquals(1, loadedSettings.getWhitelistedDomains().size());
304
305 assertEquals(
306 savedSettings.getWhitelistedDomains().get(0),
307 loadedSettings.getWhitelistedDomains().get(0));
308 }
309
310 @Test
311 public void testWhitelistedDomains_MultipleValues()
312 {
313 final AdblockSettings savedSettings = new AdblockSettings();
314 savedSettings.setAdblockEnabled(true);
315
316 final List<String> whitelistedDomains = new LinkedList<String>();
317 whitelistedDomains.add("http://www.domain1.com");
318 whitelistedDomains.add("http://www.domain2.com");
319
320 savedSettings.setWhitelistedDomains(whitelistedDomains);
321 storage.save(savedSettings);
322
323 final AdblockSettings loadedSettings = storage.load();
324 assertNotNull(loadedSettings.getWhitelistedDomains());
325 assertEquals(2, loadedSettings.getWhitelistedDomains().size());
326
327 assertEquals(
328 savedSettings.getWhitelistedDomains().get(0),
329 loadedSettings.getWhitelistedDomains().get(0));
330
331 assertEquals(
332 savedSettings.getWhitelistedDomains().get(1),
333 loadedSettings.getWhitelistedDomains().get(1));
334 }
335
336 @Test
337 public void testAllowedConnectionType_Null()
338 {
339 final AdblockSettings savedSettings = new AdblockSettings();
340 savedSettings.setAllowedConnectionType(null);
341 assertNull(savedSettings.getAllowedConnectionType());
342 storage.save(savedSettings);
343
344 final AdblockSettings loadedSettings = storage.load();
345 assertNull(loadedSettings.getAllowedConnectionType());
346 }
347
348 @Test
349 public void testAllowedConnectionType()
350 {
351 final AdblockSettings savedSettings = new AdblockSettings();
352
353 for (ConnectionType eachConnectionType : ConnectionType.values())
354 {
355 savedSettings.setAllowedConnectionType(eachConnectionType);
356 storage.save(savedSettings);
357
358 final AdblockSettings loadedSettings = storage.load();
359 assertEquals(savedSettings.getAllowedConnectionType(), loadedSettings.getA llowedConnectionType());
360 }
361 }
362
363 @Test
364 public void testFullModel()
365 {
366 final AdblockSettings savedSettings = AdblockSettingsTest.buildModel(2, 1);
367 storage.save(savedSettings);
368
369 final AdblockSettings loadedSettings = storage.load();
370
371 AdblockSettingsTest.assertSettingsEquals(savedSettings, loadedSettings);
372 }
373
374 @Override
375 protected void tearDown() throws Exception
376 {
377 prefs.edit().clear().commit();
378 super.tearDown();
379 }
380 }
OLDNEW

Powered by Google App Engine
This is Rietveld