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

Side by Side Diff: adblockplussbrowser/src/test/java/org/adblockplus/sbrowser/contentblocker/engine/UtilsTest.kt

Issue 29603697: Issue 5931 - Create tests for util package (Closed)
Patch Set: Created Nov. 10, 2017, 2:23 p.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-present 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.sbrowser.contentblocker.engine
19
20 import android.content.Context
21 import android.content.res.Resources
22 import org.adblockplus.adblockplussbrowser.R
23 import org.adblockplus.sbrowser.contentblocker.util.SharedPrefsUtils
24 import org.adblockplus.sbrowser.contentblocker.util.SubscriptionUtils
25 import org.junit.Assert.assertEquals
26 import org.junit.Assert.assertNotEquals
27 import org.junit.Before
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 import org.mockito.Mockito.mock
31 import org.robolectric.RobolectricTestRunner
32 import org.robolectric.RuntimeEnvironment
33 import java.util.*
diegocarloslima 2017/11/13 21:10:13 I don't really like using wildcards on imports
jens 2017/11/14 10:57:58 Acknowledged.
34
35 @RunWith(RobolectricTestRunner::class)
36 class UtilsTest
37 {
38
39 private var mockedDefaultSubs: DefaultSubscriptions? = null
40 private var context: Context? = null
anton 2017/11/14 06:04:09 it would be better to have it marked with `lateini
jens 2017/11/14 10:57:59 Acknowledged.
jens 2017/11/14 11:03:54 But lateinit can only be used on var. So I will ch
41 private val EASYLIST_GERMANY_COMPLETE_URL =
42 "https://easylist-downloads.adblockplus.org/easylistgermany+easylist .txt"
43
44 @Before
45 fun setup()
46 {
47 context = RuntimeEnvironment.application
48 mockedDefaultSubs = mock(DefaultSubscriptions::class.java)
49 RuntimeEnvironment.application.resources
50 .openRawResource(R.raw.subscriptions).use({ subscriptionsXml -> mockedDefaultSubs =
diegocarloslima 2017/11/13 21:10:13 Is this parenthesis in .use( really necessary?
jens 2017/11/14 10:57:58 No it's not. Good catch.
51 DefaultSubscriptions.fromStream(subscriptionsXml) })
52 }
53
54 @Test
55 fun subscriptionUtilsChooseDefaultSubscriptionForEmptyList()
56 {
57 assertEquals(SubscriptionUtils.chooseDefaultSubscriptionUrl(
58 emptyList<DefaultSubscriptionInfo>()), Engine.EASYLIST_URL)
59 }
60
61 @Test
62 fun subscriptionUtilsDefaultSubscriptionForChinaIsNotEasylist()
63 {
64 Resources.getSystem().configuration.setLocale(Locale.CHINA)
65 assertNotEquals(SubscriptionUtils.chooseDefaultSubscriptionUrl(
66 mockedDefaultSubs?.adsSubscriptions), Engine.EASYLIST_URL)
67 }
68
69 @Test
70 fun subscriptionUtilsChooseDefaultSubscriptionUrlForGerman()
71 {
72 Resources.getSystem().configuration.setLocale(Locale.GERMANY)
73 assertEquals(SubscriptionUtils.chooseDefaultSubscriptionUrl(
74 mockedDefaultSubs?.adsSubscriptions), EASYLIST_GERMANY_COMPLETE_ URL)
75 }
76
77 @Test
78 fun subscriptionUtilsChooseDefaultSubscriptionForUnsupportedLanguage()
79 {
80 Resources.getSystem().configuration.setLocale(Locale.forLanguageTag("ab- xy"))
81 assertEquals(SubscriptionUtils.chooseDefaultSubscriptionUrl(
82 mockedDefaultSubs?.adsSubscriptions), Engine.EASYLIST_URL)
83 }
84
85 @Test
86 fun sharedPrefsUtilsPutAndGetSameBoolean()
87 {
88 val testBoolean = true
89 SharedPrefsUtils.putBoolean(context, R.string.key_aa_info_shown, testBoo lean)
90 val equalBoolean = SharedPrefsUtils.getBoolean(context, R.string.key_aa_ info_shown,
91 !testBoolean)
92 assertEquals(testBoolean, equalBoolean)
93 }
94
95 @Test
96 fun sharedPrefsUtilsPutAndGetDifferentBoolean()
97 {
98 val testBoolean = true
99 SharedPrefsUtils.putBoolean(context, R.string.key_aa_info_shown, testBoo lean)
100 val unequalBoolean = SharedPrefsUtils.getBoolean(context, R.string.key_a cceptable_ads,
101 !testBoolean)
102 assertNotEquals(testBoolean, unequalBoolean)
103 }
104
105 @Test
106 fun sharedPrefsUtilsPutAndGetSameInteger()
107 {
108 val testInteger = 5
109 SharedPrefsUtils.putInt(context, R.string.key_whitelisted_websites, test Integer)
110 val equalInteger = SharedPrefsUtils.getInt(context, R.string.key_whiteli sted_websites,
111 testInteger + 1)
112 assertEquals(testInteger, equalInteger)
113 }
114
115 @Test
116 fun sharedPrefsUtilsPutAndGetDifferentInteger()
117 {
118 val testInteger = 5
119 SharedPrefsUtils.putInt(context, R.string.key_whitelisted_websites, test Integer)
120 val unequalInteger = SharedPrefsUtils.getInt(context,
121 R.string.key_application_activated, testInteger + 1)
122 assertNotEquals(testInteger, unequalInteger)
123 }
124
125 @Test
126 fun sharedPrefsUtilsPutAndGetSameString()
127 {
128 val testString = "Hello World"
129 SharedPrefsUtils.putString(context, R.string.key_automatic_updates, test String)
130 val equalString = SharedPrefsUtils.getString(context, R.string.key_autom atic_updates,
131 testString + "!")
132 assertEquals(testString, equalString)
133 }
134
135 @Test
136 fun sharedPrefsUtilsPutAndGetDifferentString()
137 {
138 val testString = "Hello World"
139 SharedPrefsUtils.putString(context, R.string.key_automatic_updates, test String)
140 val unequalString = SharedPrefsUtils.getString(context,
141 R.string.key_cached_filter_path, testString + "!")
142 assertNotEquals(testString, unequalString)
143 }
144
145 @Test
146 fun sharedPrefsUtilsPutAndGetSameStringSet()
147 {
148 val testStringSet = setOf("Hello", "World")
149 SharedPrefsUtils.putStringSet(context, R.string.key_previous_version_cod e, testStringSet)
150 val equalStringSet = SharedPrefsUtils.getStringSet(context,
151 R.string.key_previous_version_code, setOf("Hello", "World", "!") )
152 assertEquals(testStringSet, equalStringSet)
153 }
154
155 @Test
156 fun sharedPrefsUtilsPutAndGetDifferentStringSet()
157 {
158 val testStringSet = setOf("Hello", "World")
159 SharedPrefsUtils.putStringSet(context, R.string.key_previous_version_cod e, testStringSet)
160 val unequalStringSet = SharedPrefsUtils.getStringSet(context,
161 R.string.key_aa_info_shown, setOf("Hello", "World", "!"))
162 assertNotEquals(testStringSet, unequalStringSet)
163 }
164 }
OLDNEW

Powered by Google App Engine
This is Rietveld