1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 package mozilla.components.feature.addons
7 import androidx.test.ext.junit.runners.AndroidJUnit4
8 import mozilla.components.concept.engine.webextension.DisabledFlags
9 import mozilla.components.concept.engine.webextension.Metadata
10 import mozilla.components.concept.engine.webextension.WebExtension
11 import mozilla.components.support.test.mock
12 import mozilla.components.support.test.robolectric.testContext
13 import mozilla.components.support.test.whenever
14 import org.junit.Assert.assertEquals
15 import org.junit.Assert.assertFalse
16 import org.junit.Assert.assertTrue
18 import org.junit.runner.RunWith
20 @RunWith(AndroidJUnit4::class)
24 fun `translatePermissions - must return the expected string ids per permission category`() {
27 authors = emptyList(),
28 categories = emptyList(),
29 downloadUrl = "downloadUrl",
37 "declarativeNetRequest",
38 "declarativeNetRequestFeedback",
62 val translatedPermissions = addon.translatePermissions(testContext)
63 val expectedPermissions = listOf(
64 R.string.mozac_feature_addons_permissions_bookmarks_description,
65 R.string.mozac_feature_addons_permissions_browser_setting_description,
66 R.string.mozac_feature_addons_permissions_browser_data_description,
67 R.string.mozac_feature_addons_permissions_clipboard_read_description,
68 R.string.mozac_feature_addons_permissions_clipboard_write_description,
69 R.string.mozac_feature_addons_permissions_declarative_net_request_description,
70 R.string.mozac_feature_addons_permissions_declarative_net_request_feedback_description,
71 R.string.mozac_feature_addons_permissions_downloads_description,
72 R.string.mozac_feature_addons_permissions_downloads_open_description,
73 R.string.mozac_feature_addons_permissions_find_description,
74 R.string.mozac_feature_addons_permissions_geolocation_description,
75 R.string.mozac_feature_addons_permissions_history_description,
76 R.string.mozac_feature_addons_permissions_management_description,
77 R.string.mozac_feature_addons_permissions_native_messaging_description,
78 R.string.mozac_feature_addons_permissions_notifications_description,
79 R.string.mozac_feature_addons_permissions_pkcs11_description,
80 R.string.mozac_feature_addons_permissions_privacy_description,
81 R.string.mozac_feature_addons_permissions_proxy_description,
82 R.string.mozac_feature_addons_permissions_sessions_description,
83 R.string.mozac_feature_addons_permissions_tab_hide_description,
84 R.string.mozac_feature_addons_permissions_tabs_description,
85 R.string.mozac_feature_addons_permissions_top_sites_description,
86 R.string.mozac_feature_addons_permissions_unlimited_storage_description,
87 R.string.mozac_feature_addons_permissions_web_navigation_description,
88 R.string.mozac_feature_addons_permissions_devtools_description,
89 ).map { testContext.getString(it) }
90 assertEquals(expectedPermissions, translatedPermissions)
94 fun `isInstalled - true if installed state present and otherwise false`() {
97 authors = emptyList(),
98 categories = emptyList(),
99 downloadUrl = "downloadUrl",
101 permissions = emptyList(),
105 assertFalse(addon.isInstalled())
107 val installedAddon = addon.copy(installedState = Addon.InstalledState("id", "1.0", ""))
108 assertTrue(installedAddon.isInstalled())
112 fun `isEnabled - true if installed state enabled and otherwise false`() {
115 authors = emptyList(),
116 categories = emptyList(),
117 downloadUrl = "downloadUrl",
119 permissions = emptyList(),
123 assertFalse(addon.isEnabled())
125 val installedAddon = addon.copy(installedState = Addon.InstalledState("id", "1.0", ""))
126 assertFalse(installedAddon.isEnabled())
128 val enabledAddon = addon.copy(installedState = Addon.InstalledState("id", "1.0", "", enabled = true))
129 assertTrue(enabledAddon.isEnabled())
133 fun `filterTranslations - only keeps specified translations`() {
136 authors = emptyList(),
137 categories = emptyList(),
138 downloadUrl = "downloadUrl",
140 permissions = emptyList(),
143 translatableName = mapOf(Addon.DEFAULT_LOCALE to "name", "de" to "Name", "es" to "nombre"),
144 translatableDescription = mapOf(Addon.DEFAULT_LOCALE to "description", "de" to "Beschreibung", "es" to "descripciĆ³n"),
145 translatableSummary = mapOf(Addon.DEFAULT_LOCALE to "summary", "de" to "Kurzfassung", "es" to "resumen"),
148 val addonEn = addon.filterTranslations(listOf())
149 assertEquals(1, addonEn.translatableName.size)
150 assertTrue(addonEn.translatableName.contains(addonEn.defaultLocale))
152 val addonEs = addon.filterTranslations(listOf("es"))
153 assertEquals(2, addonEs.translatableName.size)
154 assertTrue(addonEs.translatableName.contains(addonEn.defaultLocale))
155 assertTrue(addonEs.translatableName.contains("es"))
159 fun `localizedURLAccessPermissions - must translate all_urls access permission`() {
160 val expectedString = testContext.getString(R.string.mozac_feature_addons_permissions_all_urls_description)
161 val permissions = listOf(
163 "webRequestBlocking",
167 val result = Addon.localizedURLAccessPermissions(testContext, permissions).first()
169 assertEquals(expectedString, result)
173 fun `localizedURLAccessPermissions - must translate all urls access permission`() {
174 val expectedString = testContext.getString(R.string.mozac_feature_addons_permissions_all_urls_description)
175 val permissions = listOf(
177 "webRequestBlocking",
181 val result = Addon.localizedURLAccessPermissions(testContext, permissions).first()
183 assertEquals(expectedString, result)
187 fun `localizedURLAccessPermissions - must translate domain access permissions`() {
188 val expectedString = listOf("tweetdeck.twitter.com", "twitter.com").map {
189 testContext.getString(R.string.mozac_feature_addons_permissions_one_site_description, it)
191 testContext.getString(R.string.mozac_feature_addons_permissions_all_urls_description)
192 val permissions = listOf(
194 "webRequestBlocking",
195 "*://tweetdeck.twitter.com/*",
199 val result = Addon.localizedURLAccessPermissions(testContext, permissions)
201 assertEquals(expectedString, result)
205 fun `localizedURLAccessPermissions - must translate one site access permissions`() {
206 val expectedString = listOf("youtube.com", "youtube-nocookie.com", "vimeo.com").map {
207 testContext.getString(R.string.mozac_feature_addons_permissions_sites_in_domain_description, it)
209 testContext.getString(R.string.mozac_feature_addons_permissions_all_urls_description)
210 val permissions = listOf(
212 "*://*.youtube.com/*",
213 "*://*.youtube-nocookie.com/*",
217 val result = Addon.localizedURLAccessPermissions(testContext, permissions)
219 assertEquals(expectedString, result)
223 fun `localizedURLAccessPermissions - must collapse url access permissions`() {
224 var expectedString = listOf("youtube.com", "youtube-nocookie.com", "vimeo.com", "google.co.ao").map {
225 testContext.getString(R.string.mozac_feature_addons_permissions_sites_in_domain_description, it)
226 } + testContext.getString(R.string.mozac_feature_addons_permissions_one_extra_domain_description)
228 var permissions = listOf(
230 "*://*.youtube.com/*",
231 "*://*.youtube-nocookie.com/*",
233 "*://*.google.co.ao/*",
234 "*://*.google.com.do/*",
237 var result = Addon.localizedURLAccessPermissions(testContext, permissions)
239 // 1 domain permissions must be collapsed
240 assertEquals(expectedString, result)
242 expectedString = listOf("youtube.com", "youtube-nocookie.com", "vimeo.com", "google.co.ao").map {
243 testContext.getString(R.string.mozac_feature_addons_permissions_sites_in_domain_description, it)
244 } + testContext.getString(R.string.mozac_feature_addons_permissions_extra_domains_description_plural, 2)
246 permissions = listOf(
248 "*://*.youtube.com/*",
249 "*://*.youtube-nocookie.com/*",
251 "*://*.google.co.ao/*",
252 "*://*.google.com.do/*",
253 "*://*.google.co.ar/*",
256 result = Addon.localizedURLAccessPermissions(testContext, permissions)
258 // 2 domain permissions must be collapsed
259 assertEquals(expectedString, result)
261 permissions = listOf(
263 "*://www.youtube.com/*",
264 "*://www.youtube-nocookie.com/*",
265 "*://www.vimeo.com/*",
266 "https://mozilla.org/a/b/c/",
267 "*://www.google.com.do/*",
270 expectedString = listOf("www.youtube.com", "www.youtube-nocookie.com", "www.vimeo.com", "mozilla.org").map {
271 testContext.getString(R.string.mozac_feature_addons_permissions_one_site_description, it)
272 } + testContext.getString(R.string.mozac_feature_addons_permissions_one_extra_site_description)
274 result = Addon.localizedURLAccessPermissions(testContext, permissions)
276 // 1 site permissions must be Collapsed
277 assertEquals(expectedString, result)
279 permissions = listOf(
281 "*://www.youtube.com/*",
282 "*://www.youtube-nocookie.com/*",
283 "*://www.vimeo.com/*",
284 "https://mozilla.org/a/b/c/",
285 "*://www.google.com.do/*",
286 "*://www.google.co.ar/*",
289 expectedString = listOf("www.youtube.com", "www.youtube-nocookie.com", "www.vimeo.com", "mozilla.org").map {
290 testContext.getString(R.string.mozac_feature_addons_permissions_one_site_description, it)
291 } + testContext.getString(R.string.mozac_feature_addons_permissions_extra_sites_description, 2)
293 result = Addon.localizedURLAccessPermissions(testContext, permissions)
295 // 2 site permissions must be Collapsed
296 assertEquals(expectedString, result)
298 permissions = listOf(
300 "*://www.youtube.com/*",
301 "*://www.youtube-nocookie.com/*",
302 "*://www.vimeo.com/*",
303 "https://mozilla.org/a/b/c/",
306 expectedString = listOf("www.youtube.com", "www.youtube-nocookie.com", "www.vimeo.com", "mozilla.org").map {
307 testContext.getString(R.string.mozac_feature_addons_permissions_one_site_description, it)
310 result = Addon.localizedURLAccessPermissions(testContext, permissions)
312 // None permissions must be collapsed
313 assertEquals(expectedString, result)
317 fun `localizeURLAccessPermission - must provide the correct localized string`() {
318 val siteAccess = listOf(
320 "*://tweetdeck.twitter.com/*",
321 "https://mozilla.org/a/b/c/",
322 "https://www.google.com.ag/*",
323 "https://www.google.co.ck/*",
327 val stringId = Addon.localizeURLAccessPermission(it)
328 assertEquals(R.string.mozac_feature_addons_permissions_one_site_description, stringId)
331 val domainAccess = listOf(
332 "*://*.youtube.com/*",
333 "*://*.youtube.com/*",
334 "*://*.youtube-nocookie.com/*",
336 "*://*.facebookcorewwwi.onion/*",
339 domainAccess.forEach {
340 val stringId = Addon.localizeURLAccessPermission(it)
341 assertEquals(R.string.mozac_feature_addons_permissions_sites_in_domain_description, stringId)
344 val allUrlsAccess = listOf(
352 allUrlsAccess.forEach {
353 val stringId = Addon.localizeURLAccessPermission(it)
354 assertEquals(R.string.mozac_feature_addons_permissions_all_urls_description, stringId)
359 fun `newFromWebExtension - must return an Addon instance`() {
360 val version = "1.2.3"
361 val permissions = listOf("scripting", "activeTab")
362 val hostPermissions = listOf("https://example.org/")
363 val name = "some name"
364 val description = "some description"
365 val extension: WebExtension = mock()
366 val metadata: Metadata = mock()
367 whenever(extension.id).thenReturn("some-id")
368 whenever(extension.url).thenReturn("some-url")
369 whenever(extension.getMetadata()).thenReturn(metadata)
370 whenever(metadata.version).thenReturn(version)
371 whenever(metadata.permissions).thenReturn(permissions)
372 whenever(metadata.hostPermissions).thenReturn(hostPermissions)
373 whenever(metadata.name).thenReturn(name)
374 whenever(metadata.description).thenReturn(description)
375 whenever(metadata.disabledFlags).thenReturn(DisabledFlags.select(0))
376 whenever(metadata.baseUrl).thenReturn("some-base-url")
378 val addon = Addon.newFromWebExtension(extension)
380 assertEquals("some-id", addon.id)
381 assertEquals("some-url", addon.siteUrl)
382 assertEquals("some-url", addon.downloadUrl)
383 assertEquals(permissions + hostPermissions, addon.permissions)
384 assertEquals("1970-01-01T00:00:00Z", addon.updatedAt)
385 assertEquals("some name", addon.translatableName[Addon.DEFAULT_LOCALE])
386 assertEquals("some description", addon.translatableDescription[Addon.DEFAULT_LOCALE])
387 assertEquals("some description", addon.translatableSummary[Addon.DEFAULT_LOCALE])