|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 DuckDuckGo |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.duckduckgo.app.browser.localstorage |
| 18 | + |
| 19 | +import com.duckduckgo.common.test.CoroutineTestRule |
| 20 | +import com.duckduckgo.common.test.FileUtilities |
| 21 | +import kotlinx.coroutines.test.runTest |
| 22 | +import org.junit.Assert.* |
| 23 | +import org.junit.Rule |
| 24 | +import org.junit.Test |
| 25 | + |
| 26 | +class LocalStorageSettingsJsonParserImplTest { |
| 27 | + |
| 28 | + @get:Rule |
| 29 | + val coroutineTestRule: CoroutineTestRule = CoroutineTestRule() |
| 30 | + |
| 31 | + private val testee = LocalStorageSettingsJsonParserImpl(coroutineTestRule.testDispatcherProvider) |
| 32 | + |
| 33 | + @Test |
| 34 | + fun whenGibberishInputThenReturnEmptyDomainsAndRegex() = runTest { |
| 35 | + val result = testee.parseJson("invalid json") |
| 36 | + assertTrue(result.domains.list.isEmpty()) |
| 37 | + assertTrue(result.matchingRegex.list.isEmpty()) |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + fun whenDomainsAndRegexMissingThenReturnEmptyDomainsAndRegex() = runTest { |
| 42 | + val result = testee.parseJson("{}") |
| 43 | + assertTrue(result.domains.list.isEmpty()) |
| 44 | + assertTrue(result.matchingRegex.list.isEmpty()) |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + fun whenListsEmptyThenReturnEmptyDomainsAndRegex() = runTest { |
| 49 | + val result = testee.parseJson("local_storage_empty".loadJsonFile()) |
| 50 | + assertTrue(result.domains.list.isEmpty()) |
| 51 | + assertTrue(result.matchingRegex.list.isEmpty()) |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun whenListsHaveSingleEntryThenReturnSingleDomainAndRegex() = runTest { |
| 56 | + val result = testee.parseJson("local_storage_single_entry".loadJsonFile()) |
| 57 | + assertEquals(1, result.domains.list.size) |
| 58 | + assertEquals(1, result.matchingRegex.list.size) |
| 59 | + assertEquals("example.com", result.domains.list[0]) |
| 60 | + assertEquals("^_https://([a-zA-Z0-9.-]+\\.)?{domain}\u0000\u0001.+$", result.matchingRegex.list[0]) |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + fun whenListsHaveMultipleEntriesThenReturnMultipleDomainsAndRegex() = runTest { |
| 65 | + val result = testee.parseJson("local_storage_multiple_entries".loadJsonFile()) |
| 66 | + assertEquals(3, result.domains.list.size) |
| 67 | + assertEquals(3, result.matchingRegex.list.size) |
| 68 | + |
| 69 | + assertEquals("example.com", result.domains.list[0]) |
| 70 | + assertEquals("foo.com", result.domains.list[1]) |
| 71 | + assertEquals("bar.com", result.domains.list[2]) |
| 72 | + |
| 73 | + assertEquals("^_https://([a-zA-Z0-9.-]+\\.)?{domain}\u0000\u0001.+$", result.matchingRegex.list[0]) |
| 74 | + assertEquals("^META:https://([a-zA-Z0-9.-]+\\.)?{domain}$", result.matchingRegex.list[1]) |
| 75 | + assertEquals("^METAACCESS:https://([a-zA-Z0-9.-]+\\.)?{domain}$", result.matchingRegex.list[2]) |
| 76 | + } |
| 77 | + |
| 78 | + private fun String.loadJsonFile(): String { |
| 79 | + return FileUtilities.loadText( |
| 80 | + LocalStorageSettingsJsonParserImplTest::class.java.classLoader!!, |
| 81 | + "json/$this.json", |
| 82 | + ) |
| 83 | + } |
| 84 | +} |
0 commit comments