| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (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.Locale | |
| 34 | |
| 35 @RunWith(RobolectricTestRunner::class) | |
| 36 class UtilsTest | |
| 37 { | |
| 38 | |
|
anton
2017/11/16 10:46:36
the line seems to be not required
jens
2017/11/17 09:16:26
Acknowledged.
| |
| 39 private var mockedDefaultSubs: DefaultSubscriptions? = null | |
|
anton
2017/11/16 10:46:36
shouldn't it be 'lateinit var .. ' too? it's init
jens
2017/11/17 09:16:26
Yeah, you are right. That is the proper way to do
| |
| 40 private lateinit var context: Context | |
| 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 -> m ockedDefaultSubs = | |
| 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 } | |
| OLD | NEW |