Skip to content

Commit 3ed05da

Browse files
authored
Add tab favicon and renderer unit tests
1 parent 93ff4ce commit 3ed05da

File tree

3 files changed

+146
-9
lines changed

3 files changed

+146
-9
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright (c) 2018 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.tabs.ui
18+
19+
import android.support.test.InstrumentationRegistry
20+
import com.duckduckgo.app.tabs.model.TabEntity
21+
import org.junit.Assert.*
22+
import org.junit.Test
23+
24+
class TabRendererExtensionTest {
25+
26+
private val context = InstrumentationRegistry.getTargetContext()
27+
28+
@Test
29+
fun whenTabIsBlankThenDisplayTitleIsDuckDuckGo() {
30+
assertEquals("DuckDuckGo", TabEntity("").displayTitle(context))
31+
}
32+
33+
@Test
34+
fun whenTabHasTitleThenDisplayTitleIsSame() {
35+
assertEquals(TITLE, TabEntity("", URL, TITLE).displayTitle(context))
36+
}
37+
38+
@Test
39+
fun whenTabDoesNotHaveTitleThenDisplayTitleIsUrlHost() {
40+
assertEquals("example.com", TabEntity("", URL, null).displayTitle(context))
41+
}
42+
43+
@Test
44+
fun whenTabDoesNotHaveTitleAndUrlIsInvalidThenTitleIsBlank() {
45+
assertEquals("", TabEntity("", INVALID_URL, null).displayTitle(context))
46+
}
47+
48+
@Test
49+
fun whenTabIsBlankThenUrlIsDuckDuckGo() {
50+
assertEquals("https://duckduckgo.com", TabEntity("").displayUrl())
51+
}
52+
53+
@Test
54+
fun whenTabHasUrlThenDisplayUrlIsSame() {
55+
assertEquals(URL, TabEntity("", URL, TITLE).displayUrl())
56+
}
57+
58+
@Test
59+
fun whenTabDoesNotHaveAUrlThenDisplayUrlIsBlank() {
60+
assertEquals("", TabEntity("", null, TITLE).displayUrl())
61+
}
62+
63+
@Test
64+
fun whenTabHasUrlThenFaviconIsNotNull() {
65+
assertNotNull(TabEntity("", URL, TITLE).favicon())
66+
}
67+
68+
@Test
69+
fun whenTabDoesNotHaveAUrlThenFaviconIsNull() {
70+
assertNull(TabEntity("", null, TITLE).favicon())
71+
}
72+
73+
@Test
74+
fun whenTabHasInvalidUrlThenFaviconIsNull() {
75+
assertNull(TabEntity("", INVALID_URL, TITLE).favicon())
76+
}
77+
78+
companion object {
79+
private const val TITLE = "Title"
80+
private const val URL = "https://example.com"
81+
private const val INVALID_URL = "notaurl"
82+
83+
}
84+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2018 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.tabs.ui
18+
19+
import android.content.Context
20+
import android.net.Uri
21+
import com.duckduckgo.app.browser.R
22+
import com.duckduckgo.app.global.AppUrl
23+
import com.duckduckgo.app.global.faviconLocation
24+
import com.duckduckgo.app.tabs.model.TabEntity
25+
26+
val TabEntity.isBlank: Boolean
27+
get() = title == null && url == null
28+
29+
fun TabEntity.displayTitle(context: Context): String {
30+
if (isBlank) {
31+
return context.getString(R.string.homeTab)
32+
}
33+
34+
return title ?: Uri.parse(resolvedUrl()).host ?: ""
35+
}
36+
37+
private fun TabEntity.resolvedUrl(): String {
38+
return if (isBlank) AppUrl.Url.HOME else url ?: ""
39+
}
40+
41+
fun TabEntity.displayUrl(): String {
42+
return resolvedUrl()
43+
}
44+
45+
fun TabEntity.favicon(): Uri? {
46+
return Uri.parse(resolvedUrl())?.faviconLocation()
47+
}

app/src/main/java/com/duckduckgo/app/tabs/ui/TabSwitcherAdapter.kt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.duckduckgo.app.tabs.ui
1818

1919
import android.content.Context
20-
import android.net.Uri
2120
import android.support.v7.widget.RecyclerView
2221
import android.support.v7.widget.RecyclerView.Adapter
2322
import android.view.LayoutInflater
@@ -26,7 +25,7 @@ import android.view.ViewGroup
2625
import android.widget.ImageView
2726
import android.widget.TextView
2827
import com.duckduckgo.app.browser.R
29-
import com.duckduckgo.app.global.AppUrl
28+
import com.duckduckgo.app.global.image.GlideApp
3029
import com.duckduckgo.app.tabs.model.TabEntity
3130
import com.duckduckgo.app.tabs.ui.TabSwitcherAdapter.TabViewHolder
3231
import kotlinx.android.synthetic.main.item_tab.view.*
@@ -46,14 +45,21 @@ class TabSwitcherAdapter(private val context: Context, private val itemClickList
4645
}
4746

4847
override fun onBindViewHolder(holder: TabViewHolder, position: Int) {
48+
4949
val tab = data[position]
50-
if (tab.title == null && tab.url == null) {
51-
holder.title.text = context.getText(R.string.homeTab)
52-
holder.url.text = AppUrl.Url.HOME
53-
} else {
54-
holder.url.text = tab.url ?: ""
55-
holder.title.text = tab.title ?: Uri.parse(tab.url).host ?: ""
56-
}
50+
holder.title.text = tab.displayTitle(context)
51+
holder.url.text = tab.displayUrl()
52+
53+
GlideApp.with(holder.root)
54+
.load(tab.favicon())
55+
.placeholder(R.drawable.ic_globe_gray_16dp)
56+
.error(R.drawable.ic_globe_gray_16dp)
57+
.into(holder.favicon)
58+
59+
attachClickListeners(holder, tab)
60+
}
61+
62+
private fun attachClickListeners(holder: TabViewHolder, tab: TabEntity) {
5763
holder.root.setOnClickListener {
5864
itemClickListener.onTabSelected(tab)
5965
}

0 commit comments

Comments
 (0)