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

Side by Side Diff: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/AdblockSettingsTest.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 junit.framework.TestCase;
21
22 import org.adblockplus.libadblockplus.android.Subscription;
23 import org.adblockplus.libadblockplus.android.settings.AdblockSettings;
24 import org.adblockplus.libadblockplus.android.settings.ConnectionType;
25 import org.junit.Test;
26
27 import java.io.ByteArrayInputStream;
28 import java.io.ByteArrayOutputStream;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.ObjectInputStream;
32 import java.io.ObjectOutputStream;
33 import java.util.LinkedList;
34 import java.util.List;
35
36 public class AdblockSettingsTest extends TestCase
37 {
38 public static AdblockSettings buildModel(int subscriptionsCount, int whitelist edDomainsCount)
39 {
40 final AdblockSettings settings = new AdblockSettings();
41 settings.setAdblockEnabled(true);
42 settings.setAcceptableAdsEnabled(true);
43 settings.setAllowedConnectionType(ConnectionType.WIFI);
44
45 final List<Subscription> subscriptions = new LinkedList<Subscription>();
46 for (int i = 0; i < subscriptionsCount; i++)
47 {
48 final Subscription subscription = new Subscription();
49 subscription.title = "Title" + (i + 1);
50 subscription.url = "URL" + (i + 1);
51 subscriptions.add(subscription);
52 }
53 settings.setSubscriptions(subscriptions);
54
55 final List<String> domains = new LinkedList<String>();
56 for (int i = 0; i < whitelistedDomainsCount; i++)
57 {
58 domains.add("http://www.domain" + (i + 1) + ".com");
59 }
60 settings.setWhitelistedDomains(domains);
61
62 return settings;
63 }
64
65 public static void assertSettingsEquals(AdblockSettings expected, AdblockSetti ngs actual)
66 {
67 assertEquals(expected.isAdblockEnabled(), actual.isAdblockEnabled());
68 assertEquals(expected.isAcceptableAdsEnabled(), actual.isAcceptableAdsEnable d());
69 assertEquals(expected.getAllowedConnectionType(), actual.getAllowedConnectio nType());
70
71 assertNotNull(actual.getSubscriptions());
72 assertEquals(expected.getSubscriptions().size(), actual.getSubscriptions().s ize());
73 for (int i = 0; i < expected.getSubscriptions().size(); i++)
74 {
75 assertEquals(expected.getSubscriptions().get(i).title, actual.getSubscript ions().get(i).title);
76 assertEquals(expected.getSubscriptions().get(i).url, actual.getSubscriptio ns().get(i).url);
77 }
78
79 assertNotNull(actual.getWhitelistedDomains());
80 assertEquals(expected.getWhitelistedDomains().size(), actual.getWhitelistedD omains().size());
81
82 for (int i = 0; i < expected.getWhitelistedDomains().size(); i++)
83 {
84 assertEquals(expected.getWhitelistedDomains().get(i), actual.getWhiteliste dDomains().get(i));
85 }
86 }
87
88 @Test
89 public void testAdblockEnabled()
anton 2017/04/14 08:14:01 pretty dumb test following but the intention is to
90 {
91 final AdblockSettings settings = new AdblockSettings();
92 settings.setAdblockEnabled(true);
93 assertTrue(settings.isAdblockEnabled());
94
95 settings.setAdblockEnabled(false);
96 assertFalse(settings.isAdblockEnabled());
97 }
98
99 @Test
100 public void testAcceptableAds()
101 {
102 final AdblockSettings settings = new AdblockSettings();
103 settings.setAcceptableAdsEnabled(true);
104 assertTrue(settings.isAcceptableAdsEnabled());
105
106 settings.setAcceptableAdsEnabled(false);
107 assertFalse(settings.isAcceptableAdsEnabled());
108 }
109
110 @Test
111 public void testAllowedConnectionType()
112 {
113 final AdblockSettings settings = new AdblockSettings();
114 for (ConnectionType eachConnectionType : ConnectionType.values())
115 {
116 settings.setAllowedConnectionType(eachConnectionType);
117 assertEquals(eachConnectionType, settings.getAllowedConnectionType());
118 }
119 }
120
121 @Test
122 public void testSubscriptions()
123 {
124 for (int i = 0; i < 3; i++)
125 {
126 final AdblockSettings settings = buildModel(i, 1);
127 assertEquals(i, settings.getSubscriptions().size());
128 }
129 }
130
131 @Test
132 public void testwhitelistedDomains()
133 {
134 for (int i = 0; i < 3; i++)
135 {
136 final AdblockSettings settings = buildModel(1, i);
137 assertEquals(i, settings.getWhitelistedDomains().size());
138 }
139 }
140
141 @Test
142 public void testSerializable() throws IOException, ClassNotFoundException
143 {
144 final AdblockSettings savedSettings = buildModel(2, 3);
145
146 ByteArrayOutputStream baos = new ByteArrayOutputStream();
147 ObjectOutputStream oos = new ObjectOutputStream(baos);
148 oos.writeObject(savedSettings);
149
150 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
151 ObjectInputStream ois = new ObjectInputStream(bais);
152 AdblockSettings loadedSettings = (AdblockSettings) ois.readObject();
153
154 assertSettingsEquals(savedSettings, loadedSettings);
155 }
156 }
OLDNEW

Powered by Google App Engine
This is Rietveld