Revision control
Copy as Markdown
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
package org.mozilla.focus.ext
import android.net.Uri
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
@RunWith(RobolectricTestRunner::class)
class UriKtTest {
@Test
fun `WHEN truncatedHost is called on a null host THEN it returns null`() {
val uri = httpsUriBuilder()
.path("mozilla/en-US")
.authority(null)
.build()
assertNull(uri.host)
assertNull(uri.truncatedHost())
}
@Test
fun `WHEN truncatedHost is called on an empty host THEN it returns the host`() {
val hosts = arrayOf(
"",
" "
)
val uris = hosts.map {
httpsUriBuilder()
.path("mozilla/en-US")
.authority(it)
.build()
}
hosts.zip(uris).forEachIndexed { i, (host, uri) ->
val assertMsg = "index: $i" // hosts contain spaces so we must print index to disambiguate in loop
assertEquals(assertMsg, host, uri.host) // assert init code is correct.
assertEquals(assertMsg, host, uri.truncatedHost()) // assert method is correct.
}
}
@Test
fun `WHEN truncatedHost is called on a common valid Uri THEN the truncated host is returned`() {
arrayOf(
Pair("https://de.search.yahoo.com/search?p=mozilla&fr=yfp-t&fp=1&toggle=1&cop=mss&ei=UTF-8", "yahoo.com"),
).map { (uriStr, expectedStr) ->
Pair(uriStr.toUri()!!, expectedStr)
}.forEach { (input, expected) ->
assertEquals(expected, input.truncatedHost())
}
}
@Test
fun `WHEN isYouTubeDesktop called with a null path THEN uri is not YouTube desktop`() {
val uri = httpsUriBuilder()
.authority("mozilla.org")
.path(null)
.build()
assertNull(uri.path)
assertFalse(uri.isYouTubeDesktop)
}
@Test
fun `WHEN isYouTubeDesktop called with a null host THEN uri is not YouTube desktop`() {
val uri = httpsUriBuilder()
.authority(null)
.path("mozilla/en-US")
.build()
assertNull(uri.host)
assertFalse(uri.isYouTubeDesktop)
}
@Test
fun `WHEN isYouTubeDesktop called with non-YT desktop uri THEN uri is not YouTube desktop`() {
arrayOf(
).map { it.toUri()!! }.forEach {
assertFalse(it.toString(), it.isYouTubeDesktop)
}
}
@Test
fun `WHEN isYouTubeDesktop called with YT desktop uri THEN uri is YouTube desktop`() {
arrayOf(
).map { it.toUri()!! }.forEach {
assertTrue(it.toString(), it.isYouTubeDesktop)
}
}
@Test
fun `isYouTubeDesktop is false for youtube in uri path`() {
}
@Test
fun testTruncatedPathWithEmptySegments() {
}
@Test
fun testTrunactedPathWithOneSegment() {
}
@Test
fun testTruncatedPathWithTwoSegments() {
assertTruncatedPath("/mozilla-mobile/focus-android", "https://github.com/mozilla-mobile/focus-android")
}
@Test
fun testTruncatedPathWithMultipleSegments() {
assertTruncatedPath("/2017/…/nasa-hi-seas-mars-analogue-mission-hawaii-mauna-loa",
}
@Test
fun testTruncatedPathWithMultipleSegmentsAndFragment() {
assertTruncatedPath(
"/@bfrancis/the-story-of-firefox-os-cb5bf796e8fb",
}
private fun assertTruncatedPath(expectedTruncatedPath: String, url: String) {
assertEquals("truncatedPath($url)",
expectedTruncatedPath,
Uri.parse(url).truncatedPath())
}
}
private fun httpsUriBuilder(): Uri.Builder = Uri.Builder().scheme("https")