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

Unified 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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/SharedPrefsStorageTest.java
diff --git a/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/SharedPrefsStorageTest.java b/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/SharedPrefsStorageTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..229acd911fac912e565f92d804a03231663f60f3
--- /dev/null
+++ b/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/SharedPrefsStorageTest.java
@@ -0,0 +1,380 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2017 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package org.adblockplus.libadblockplus.tests;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.test.AndroidTestCase;
+
+import org.adblockplus.libadblockplus.android.Subscription;
+import org.adblockplus.libadblockplus.android.settings.AdblockSettings;
+import org.adblockplus.libadblockplus.android.settings.ConnectionType;
+import org.adblockplus.libadblockplus.android.settings.SharedPrefsStorage;
+import org.junit.Test;
+
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+
+public class SharedPrefsStorageTest extends AndroidTestCase
+{
+ protected static final String NAME = "prefs";
+
+ protected SharedPreferences prefs;
+ protected SharedPrefsStorage storage;
+
+ @Override
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+
+ prefs = getContext().getSharedPreferences(NAME, Context.MODE_PRIVATE);
+ prefs.edit().clear().commit();
+
+ storage = new SharedPrefsStorage(prefs);
+ storage.setCommit(true);
+ }
+
+ @Test
+ public void testNeverSaved()
+ {
+ final AdblockSettings settings = storage.load();
+ assertNull(settings);
+ }
+
+ protected void _testAdblockEnabled()
+ {
+ final AdblockSettings savedSettings = new AdblockSettings();
+ savedSettings.setAdblockEnabled(true);
+ storage.save(savedSettings);
+
+ AdblockSettings loadedSettings = storage.load();
+ assertEquals(savedSettings.isAdblockEnabled(), loadedSettings.isAdblockEnabled());
+
+ savedSettings.setAdblockEnabled(false);
+ storage.save(savedSettings);
+
+ loadedSettings = storage.load();
+ assertEquals(savedSettings.isAdblockEnabled(), loadedSettings.isAdblockEnabled());
+ }
+
+ @Test
+ public void testAdblockEnabled()
+ {
+ storage.setCommit(true);
+ _testAdblockEnabled();
+ }
+
+ @Test
+ public void testAdblockEnabled_Apply()
+ {
+ storage.setCommit(false); // commit is sync (for `true`), apply is async (for `false`)
+ _testAdblockEnabled();
+ }
+
+ @Test
+ public void testAcceptableAdsEnabled()
+ {
+ final AdblockSettings savedSettings = new AdblockSettings();
+ savedSettings.setAcceptableAdsEnabled(true);
+ storage.save(savedSettings);
+
+ AdblockSettings loadedSettings = storage.load();
+ assertEquals(savedSettings.isAcceptableAdsEnabled(), loadedSettings.isAcceptableAdsEnabled());
+
+ savedSettings.setAcceptableAdsEnabled(false);
+ storage.save(savedSettings);
+
+ loadedSettings = storage.load();
+ assertEquals(savedSettings.isAcceptableAdsEnabled(), loadedSettings.isAcceptableAdsEnabled());
+ }
+
+ @Test
+ public void testSubscriptions_Null()
+ {
+ final AdblockSettings savedSettings = new AdblockSettings();
+ savedSettings.setAdblockEnabled(true);
+ assertNull(savedSettings.getSubscriptions());
+ storage.save(savedSettings);
+
+ final AdblockSettings loadedSettings = storage.load();
+ assertNull(loadedSettings.getSubscriptions());
+ }
+
+ @Test
+ public void testSubscriptions_Empty()
+ {
+ final AdblockSettings savedSettings = new AdblockSettings();
+ savedSettings.setAdblockEnabled(true);
+ savedSettings.setSubscriptions(Collections.<Subscription>emptyList());
+ storage.save(savedSettings);
+
+ final AdblockSettings loadedSettings = storage.load();
+ assertNotNull(loadedSettings.getSubscriptions());
+ assertEquals(0, loadedSettings.getSubscriptions().size());
+ }
+
+ @Test
+ public void testSubscriptions_OneValue()
+ {
+ final AdblockSettings savedSettings = new AdblockSettings();
+ savedSettings.setAdblockEnabled(true);
+
+ final List<Subscription> subscriptions = new LinkedList<Subscription>();
+ final Subscription savedSubscription = new Subscription();
+ savedSubscription.title = "Title";
+ savedSubscription.url = "URL";
+ subscriptions.add(savedSubscription);
+
+ savedSettings.setSubscriptions(subscriptions);
+ storage.save(savedSettings);
+
+ final AdblockSettings loadedSettings = storage.load();
+ assertNotNull(loadedSettings.getSubscriptions());
+ assertEquals(1, loadedSettings.getSubscriptions().size());
+
+ final Subscription loadedSubscription = loadedSettings.getSubscriptions().get(0);
+ assertEquals(savedSubscription.title, loadedSubscription.title);
+ assertEquals(savedSubscription.url, loadedSubscription.url);
+
+ // '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
+ }
+
+ @Test
+ public void testSubscriptions_OneValueNonEnglishTitle()
+ {
+ final AdblockSettings savedSettings = new AdblockSettings();
+ savedSettings.setAdblockEnabled(true);
+
+ final List<Subscription> subscriptions = new LinkedList<Subscription>();
+ final Subscription savedSubscription = new Subscription();
+ savedSubscription.title = "Заголовок"; // non-English characters
+ savedSubscription.url = "URL";
+ subscriptions.add(savedSubscription);
+
+ savedSettings.setSubscriptions(subscriptions);
+ storage.save(savedSettings);
+
+ final AdblockSettings loadedSettings = storage.load();
+ assertNotNull(loadedSettings.getSubscriptions());
+ assertEquals(1, loadedSettings.getSubscriptions().size());
+
+ final Subscription loadedSubscription = loadedSettings.getSubscriptions().get(0);
+ assertEquals(savedSubscription.title, loadedSubscription.title);
+ assertEquals(savedSubscription.url, loadedSubscription.url);
+
+ // 'author', 'homepage', 'specialization', 'prefixes' settings are not saved by SharedPrefsStorage
+ }
+
+ @Test
+ public void testSubscriptions_OneValueNonEnglishUrl()
+ {
+ final AdblockSettings savedSettings = new AdblockSettings();
+ savedSettings.setAdblockEnabled(true);
+
+ final List<Subscription> subscriptions = new LinkedList<Subscription>();
+ final Subscription savedSubscription = new Subscription();
+ savedSubscription.title = "Title";
+ savedSubscription.url = "http://почта.рф"; // non-English characters
+ subscriptions.add(savedSubscription);
+
+ savedSettings.setSubscriptions(subscriptions);
+ storage.save(savedSettings);
+
+ final AdblockSettings loadedSettings = storage.load();
+ assertNotNull(loadedSettings.getSubscriptions());
+ assertEquals(1, loadedSettings.getSubscriptions().size());
+
+ final Subscription loadedSubscription = loadedSettings.getSubscriptions().get(0);
+ assertEquals(savedSubscription.title, loadedSubscription.title);
+ assertEquals(savedSubscription.url, loadedSubscription.url);
+
+ // 'author', 'homepage', 'specialization', 'prefixes' settings are not saved by SharedPrefsStorage
+ }
+
+ @Test
+ public void testSubscriptions_MultipleValues()
+ {
+ AdblockSettings savedSettings = new AdblockSettings();
+ savedSettings.setAdblockEnabled(true);
+
+ final List<Subscription> subscriptions = new LinkedList<Subscription>();
+ final Subscription savedSubscription1 = new Subscription();
+ savedSubscription1.title = "Title1";
+ savedSubscription1.url = "URL1";
+ subscriptions.add(savedSubscription1);
+
+ final Subscription savedSubscription2 = new Subscription();
+ savedSubscription2.title = "Title2";
+ savedSubscription2.url = "URL2";
+ subscriptions.add(savedSubscription2);
+
+ savedSettings.setSubscriptions(subscriptions);
+ storage.save(savedSettings);
+
+ final AdblockSettings loadedSettings = storage.load();
+ assertNotNull(loadedSettings.getSubscriptions());
+ assertEquals(2, loadedSettings.getSubscriptions().size());
+
+ final Subscription loadedSubscription1 = loadedSettings.getSubscriptions().get(0);
+ assertEquals(savedSubscription1.title, loadedSubscription1.title);
+ assertEquals(savedSubscription1.url, loadedSubscription1.url);
+
+ final Subscription loadedSubscription2 = loadedSettings.getSubscriptions().get(1);
+ assertEquals(savedSubscription2.title, loadedSubscription2.title);
+ assertEquals(savedSubscription2.url, loadedSubscription2.url);
+ }
+
+ @Test
+ public void testWhitelistedDomains_Null()
+ {
+ final AdblockSettings savedSettings = new AdblockSettings();
+ savedSettings.setAdblockEnabled(true);
+ assertNull(savedSettings.getWhitelistedDomains());
+ storage.save(savedSettings);
+
+ final AdblockSettings loadedSettings = storage.load();
+ assertNull(loadedSettings.getWhitelistedDomains());
+ }
+
+ @Test
+ public void testWhitelistedDomains_Empty()
+ {
+ final AdblockSettings savedSettings = new AdblockSettings();
+ savedSettings.setAdblockEnabled(true);
+ savedSettings.setWhitelistedDomains(Collections.<String>emptyList());
+ storage.save(savedSettings);
+
+ final AdblockSettings loadedSettings = storage.load();
+ assertNotNull(loadedSettings.getWhitelistedDomains());
+ assertEquals(0, loadedSettings.getWhitelistedDomains().size());
+ }
+
+ @Test
+ public void testWhitelistedDomains_SingleValue()
+ {
+ final AdblockSettings savedSettings = new AdblockSettings();
+ savedSettings.setAdblockEnabled(true);
+
+ final List<String> whitelistedDomains = new LinkedList<String>();
+ whitelistedDomains.add("http://www.domain1.com");
+
+ savedSettings.setWhitelistedDomains(whitelistedDomains);
+ storage.save(savedSettings);
+
+ final AdblockSettings loadedSettings = storage.load();
+ assertNotNull(loadedSettings.getWhitelistedDomains());
+ assertEquals(1, loadedSettings.getWhitelistedDomains().size());
+
+ assertEquals(
+ savedSettings.getWhitelistedDomains().get(0),
+ loadedSettings.getWhitelistedDomains().get(0));
+ }
+
+ @Test
+ public void testWhitelistedDomains_SingleValueNonEnglish()
+ {
+ final AdblockSettings savedSettings = new AdblockSettings();
+ savedSettings.setAdblockEnabled(true);
+
+ final List<String> whitelistedDomains = new LinkedList<String>();
+ whitelistedDomains.add("http://почта.рф");
+
+ savedSettings.setWhitelistedDomains(whitelistedDomains);
+ storage.save(savedSettings);
+
+ final AdblockSettings loadedSettings = storage.load();
+ assertNotNull(loadedSettings.getWhitelistedDomains());
+ assertEquals(1, loadedSettings.getWhitelistedDomains().size());
+
+ assertEquals(
+ savedSettings.getWhitelistedDomains().get(0),
+ loadedSettings.getWhitelistedDomains().get(0));
+ }
+
+ @Test
+ public void testWhitelistedDomains_MultipleValues()
+ {
+ final AdblockSettings savedSettings = new AdblockSettings();
+ savedSettings.setAdblockEnabled(true);
+
+ final List<String> whitelistedDomains = new LinkedList<String>();
+ whitelistedDomains.add("http://www.domain1.com");
+ whitelistedDomains.add("http://www.domain2.com");
+
+ savedSettings.setWhitelistedDomains(whitelistedDomains);
+ storage.save(savedSettings);
+
+ final AdblockSettings loadedSettings = storage.load();
+ assertNotNull(loadedSettings.getWhitelistedDomains());
+ assertEquals(2, loadedSettings.getWhitelistedDomains().size());
+
+ assertEquals(
+ savedSettings.getWhitelistedDomains().get(0),
+ loadedSettings.getWhitelistedDomains().get(0));
+
+ assertEquals(
+ savedSettings.getWhitelistedDomains().get(1),
+ loadedSettings.getWhitelistedDomains().get(1));
+ }
+
+ @Test
+ public void testAllowedConnectionType_Null()
+ {
+ final AdblockSettings savedSettings = new AdblockSettings();
+ savedSettings.setAllowedConnectionType(null);
+ assertNull(savedSettings.getAllowedConnectionType());
+ storage.save(savedSettings);
+
+ final AdblockSettings loadedSettings = storage.load();
+ assertNull(loadedSettings.getAllowedConnectionType());
+ }
+
+ @Test
+ public void testAllowedConnectionType()
+ {
+ final AdblockSettings savedSettings = new AdblockSettings();
+
+ for (ConnectionType eachConnectionType : ConnectionType.values())
+ {
+ savedSettings.setAllowedConnectionType(eachConnectionType);
+ storage.save(savedSettings);
+
+ final AdblockSettings loadedSettings = storage.load();
+ assertEquals(savedSettings.getAllowedConnectionType(), loadedSettings.getAllowedConnectionType());
+ }
+ }
+
+ @Test
+ public void testFullModel()
+ {
+ final AdblockSettings savedSettings = AdblockSettingsTest.buildModel(2, 1);
+ storage.save(savedSettings);
+
+ final AdblockSettings loadedSettings = storage.load();
+
+ AdblockSettingsTest.assertSettingsEquals(savedSettings, loadedSettings);
+ }
+
+ @Override
+ protected void tearDown() throws Exception
+ {
+ prefs.edit().clear().commit();
+ super.tearDown();
+ }
+}

Powered by Google App Engine
This is Rietveld