-
Notifications
You must be signed in to change notification settings - Fork 928
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This class will take the url that a user has entered and if: 1. It is blank will set it as a default to duckduckgo.com 2. If it has no scheme will add http (in case the website does not currently support https) 3. Other wise return what was passed in
- Loading branch information
1 parent
b40783d
commit 8949392
Showing
6 changed files
with
205 additions
and
2 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...ava/com/duckduckgo/app/generalsettings/showonapplaunch/ShowOnAppLaunchUrlConverterImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2024 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.app.generalsettings.showonapplaunch | ||
|
||
import android.net.Uri | ||
import com.duckduckgo.app.generalsettings.showonapplaunch.store.ShowOnAppLaunchOptionDataStore | ||
|
||
class ShowOnAppLaunchUrlConverterImpl : UrlConverter { | ||
|
||
override fun convertUrl(url: String?): String { | ||
if (url.isNullOrBlank()) return ShowOnAppLaunchOptionDataStore.DEFAULT_SPECIFIC_PAGE_URL | ||
|
||
val uri = Uri.parse(url.trim()) | ||
|
||
val convertedUri = if (uri.scheme == null) { | ||
Uri.Builder().scheme("http").authority(uri.path?.lowercase()) | ||
} else { | ||
uri.buildUpon() | ||
.scheme(uri.scheme?.lowercase()) | ||
.authority(uri.authority?.lowercase()) | ||
} | ||
.apply { | ||
query(uri.query) | ||
fragment(uri.fragment) | ||
} | ||
.build() | ||
.toString() | ||
|
||
return Uri.decode(convertedUri) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
app/src/main/java/com/duckduckgo/app/generalsettings/showonapplaunch/UrlConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright (c) 2024 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.app.generalsettings.showonapplaunch | ||
|
||
interface UrlConverter { | ||
|
||
fun convertUrl(url: String?): String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
...com/duckduckgo/app/generalsettings/showonapplaunch/ShowOnAppLaunchUrlConverterImplTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Copyright (c) 2024 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.app.generalsettings.showonapplaunch | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.duckduckgo.app.generalsettings.showonapplaunch.store.ShowOnAppLaunchOptionDataStore.Companion.DEFAULT_SPECIFIC_PAGE_URL | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class ShowOnAppLaunchUrlConverterImplTest { | ||
|
||
private val urlConverter = ShowOnAppLaunchUrlConverterImpl() | ||
|
||
@Test | ||
fun whenUrlIsNullThenShouldReturnDefaultUrl() { | ||
val result = urlConverter.convertUrl(null) | ||
assertEquals(DEFAULT_SPECIFIC_PAGE_URL, result) | ||
} | ||
|
||
@Test | ||
fun whenUrlIsEmptyThenShouldReturnDefaultUrl() { | ||
val result = urlConverter.convertUrl("") | ||
assertEquals(DEFAULT_SPECIFIC_PAGE_URL, result) | ||
} | ||
|
||
@Test | ||
fun whenUrlIsBlankThenShouldReturnDefaultUrl() { | ||
val result = urlConverter.convertUrl(" ") | ||
assertEquals(DEFAULT_SPECIFIC_PAGE_URL, result) | ||
} | ||
|
||
@Test | ||
fun whenUrlHasNoSchemeThenHttpSchemeIsAdded() { | ||
val result = urlConverter.convertUrl("www.example.com") | ||
assertEquals("http://www.example.com", result) | ||
} | ||
|
||
@Test | ||
fun whenUrlHasNoSchemeAndSubdomainThenHttpSchemeIsAdded() { | ||
val result = urlConverter.convertUrl("example.com") | ||
assertEquals("http://example.com", result) | ||
} | ||
|
||
@Test | ||
fun whenUrlHasASchemeThenShouldReturnTheSameUrl() { | ||
val result = urlConverter.convertUrl("https://www.example.com") | ||
assertEquals("https://www.example.com", result) | ||
} | ||
|
||
@Test | ||
fun whenUrlHasDifferentSchemeThenShouldReturnTheSameUrl() { | ||
val result = urlConverter.convertUrl("ftp://www.example.com") | ||
assertEquals("ftp://www.example.com", result) | ||
} | ||
|
||
@Test | ||
fun whenUrlHasSpecialCharactersThenShouldReturnTheSameUrl() { | ||
val result = urlConverter.convertUrl("https://www.example.com/path?query=param&another=param") | ||
assertEquals("https://www.example.com/path?query=param&another=param", result) | ||
} | ||
|
||
@Test | ||
fun whenUrlHasPortThenShouldReturnTheSameUrl() { | ||
val result = urlConverter.convertUrl("https://www.example.com:8080") | ||
assertEquals("https://www.example.com:8080", result) | ||
} | ||
|
||
@Test | ||
fun whenUrlHasPathAndQueryParametersThenShouldReturnTheSameUrl() { | ||
val result = urlConverter.convertUrl("https://www.example.com/path/to/resource?query=param") | ||
assertEquals("https://www.example.com/path/to/resource?query=param", result) | ||
} | ||
|
||
@Test | ||
fun whenUrlHasUppercaseProtocolThenShouldLowercaseProtocol() { | ||
val result = urlConverter.convertUrl("HTTPS://www.example.com") | ||
assertEquals("https://www.example.com", result) | ||
} | ||
|
||
@Test | ||
fun whenUrlHasUppercaseSubdomainThenShouldLowercaseSubdomain() { | ||
val result = urlConverter.convertUrl("https://WWW.example.com") | ||
assertEquals("https://www.example.com", result) | ||
} | ||
|
||
@Test | ||
fun whenUrlHasUppercaseDomainThenShouldLowercaseDomain() { | ||
val result = urlConverter.convertUrl("https://www.EXAMPLE.com") | ||
assertEquals("https://www.example.com", result) | ||
} | ||
|
||
@Test | ||
fun whenUrlHasUppercaseTopLevelDomainThenShouldLowercaseTopLevelDomain() { | ||
val result = urlConverter.convertUrl("https://www.example.COM") | ||
assertEquals("https://www.example.com", result) | ||
} | ||
|
||
@Test | ||
fun whenUrlHasMixedCaseThenOnlyProtocolSubdomainDomainAndTldAreLowercased() { | ||
val result = urlConverter.convertUrl("HTTPS://WWW.EXAMPLE.COM/Path?Query=Param#Fragment") | ||
assertEquals("https://www.example.com/Path?Query=Param#Fragment", result) | ||
} | ||
|
||
@Test | ||
fun whenUrlIsNotAValidUrlReturnsInvalidUrlWithHttpScheme() { | ||
val result = urlConverter.convertUrl("example") | ||
assertEquals("http://example", result) | ||
} | ||
} |