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 private lateinit var mockedDefaultSubs: DefaultSubscriptions |
| 39 private lateinit var context: Context |
| 40 private val EASYLIST_GERMANY_COMPLETE_URL = |
| 41 "https://easylist-downloads.adblockplus.org/easylistgermany+easylist
.txt" |
| 42 |
| 43 @Before |
| 44 fun setup() |
| 45 { |
| 46 context = RuntimeEnvironment.application |
| 47 mockedDefaultSubs = mock(DefaultSubscriptions::class.java) |
| 48 RuntimeEnvironment.application.resources |
| 49 .openRawResource(R.raw.subscriptions).use{ subscriptionsXml -> m
ockedDefaultSubs = |
| 50 DefaultSubscriptions.fromStream(subscriptionsXml) } |
| 51 } |
| 52 |
| 53 @Test |
| 54 fun subscriptionUtilsChooseDefaultSubscriptionForEmptyList() |
| 55 { |
| 56 assertEquals(SubscriptionUtils.chooseDefaultSubscriptionUrl( |
| 57 emptyList<DefaultSubscriptionInfo>()), Engine.EASYLIST_URL) |
| 58 } |
| 59 |
| 60 @Test |
| 61 fun subscriptionUtilsDefaultSubscriptionForChinaIsNotEasylist() |
| 62 { |
| 63 Resources.getSystem().configuration.setLocale(Locale.CHINA) |
| 64 assertNotEquals(SubscriptionUtils.chooseDefaultSubscriptionUrl( |
| 65 mockedDefaultSubs?.adsSubscriptions), Engine.EASYLIST_URL) |
| 66 } |
| 67 |
| 68 @Test |
| 69 fun subscriptionUtilsChooseDefaultSubscriptionUrlForGerman() |
| 70 { |
| 71 Resources.getSystem().configuration.setLocale(Locale.GERMANY) |
| 72 assertEquals(SubscriptionUtils.chooseDefaultSubscriptionUrl( |
| 73 mockedDefaultSubs?.adsSubscriptions), EASYLIST_GERMANY_COMPLETE_
URL) |
| 74 } |
| 75 |
| 76 @Test |
| 77 fun subscriptionUtilsChooseDefaultSubscriptionForUnsupportedLanguage() |
| 78 { |
| 79 Resources.getSystem().configuration.setLocale(Locale.forLanguageTag("ab-
xy")) |
| 80 assertEquals(SubscriptionUtils.chooseDefaultSubscriptionUrl( |
| 81 mockedDefaultSubs?.adsSubscriptions), Engine.EASYLIST_URL) |
| 82 } |
| 83 |
| 84 @Test |
| 85 fun sharedPrefsUtilsPutAndGetSameBoolean() |
| 86 { |
| 87 val testBoolean = true |
| 88 SharedPrefsUtils.putBoolean(context, R.string.key_aa_info_shown, testBoo
lean) |
| 89 val equalBoolean = SharedPrefsUtils.getBoolean(context, R.string.key_aa_
info_shown, |
| 90 !testBoolean) |
| 91 assertEquals(testBoolean, equalBoolean) |
| 92 } |
| 93 |
| 94 @Test |
| 95 fun sharedPrefsUtilsPutAndGetDifferentBoolean() |
| 96 { |
| 97 val testBoolean = true |
| 98 SharedPrefsUtils.putBoolean(context, R.string.key_aa_info_shown, testBoo
lean) |
| 99 val unequalBoolean = SharedPrefsUtils.getBoolean(context, R.string.key_a
cceptable_ads, |
| 100 !testBoolean) |
| 101 assertNotEquals(testBoolean, unequalBoolean) |
| 102 } |
| 103 |
| 104 @Test |
| 105 fun sharedPrefsUtilsPutAndGetSameInteger() |
| 106 { |
| 107 val testInteger = 5 |
| 108 SharedPrefsUtils.putInt(context, R.string.key_whitelisted_websites, test
Integer) |
| 109 val equalInteger = SharedPrefsUtils.getInt(context, R.string.key_whiteli
sted_websites, |
| 110 testInteger + 1) |
| 111 assertEquals(testInteger, equalInteger) |
| 112 } |
| 113 |
| 114 @Test |
| 115 fun sharedPrefsUtilsPutAndGetDifferentInteger() |
| 116 { |
| 117 val testInteger = 5 |
| 118 SharedPrefsUtils.putInt(context, R.string.key_whitelisted_websites, test
Integer) |
| 119 val unequalInteger = SharedPrefsUtils.getInt(context, |
| 120 R.string.key_application_activated, testInteger + 1) |
| 121 assertNotEquals(testInteger, unequalInteger) |
| 122 } |
| 123 |
| 124 @Test |
| 125 fun sharedPrefsUtilsPutAndGetSameString() |
| 126 { |
| 127 val testString = "Hello World" |
| 128 SharedPrefsUtils.putString(context, R.string.key_automatic_updates, test
String) |
| 129 val equalString = SharedPrefsUtils.getString(context, R.string.key_autom
atic_updates, |
| 130 testString + "!") |
| 131 assertEquals(testString, equalString) |
| 132 } |
| 133 |
| 134 @Test |
| 135 fun sharedPrefsUtilsPutAndGetDifferentString() |
| 136 { |
| 137 val testString = "Hello World" |
| 138 SharedPrefsUtils.putString(context, R.string.key_automatic_updates, test
String) |
| 139 val unequalString = SharedPrefsUtils.getString(context, |
| 140 R.string.key_cached_filter_path, testString + "!") |
| 141 assertNotEquals(testString, unequalString) |
| 142 } |
| 143 |
| 144 @Test |
| 145 fun sharedPrefsUtilsPutAndGetSameStringSet() |
| 146 { |
| 147 val testStringSet = setOf("Hello", "World") |
| 148 SharedPrefsUtils.putStringSet(context, R.string.key_previous_version_cod
e, testStringSet) |
| 149 val equalStringSet = SharedPrefsUtils.getStringSet(context, |
| 150 R.string.key_previous_version_code, setOf("Hello", "World", "!")
) |
| 151 assertEquals(testStringSet, equalStringSet) |
| 152 } |
| 153 |
| 154 @Test |
| 155 fun sharedPrefsUtilsPutAndGetDifferentStringSet() |
| 156 { |
| 157 val testStringSet = setOf("Hello", "World") |
| 158 SharedPrefsUtils.putStringSet(context, R.string.key_previous_version_cod
e, testStringSet) |
| 159 val unequalStringSet = SharedPrefsUtils.getStringSet(context, |
| 160 R.string.key_aa_info_shown, setOf("Hello", "World", "!")) |
| 161 assertNotEquals(testStringSet, unequalStringSet) |
| 162 } |
| 163 } |
OLD | NEW |