Skip to content

Commit

Permalink
For mozilla-mobile#1824: Prevent extremely long URLs from locking up …
Browse files Browse the repository at this point in the history
…HomeFragment
  • Loading branch information
csadilek committed May 21, 2020
1 parent 8627327 commit 4ebe209
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.view.ViewOutlineProvider
import androidx.appcompat.content.res.AppCompatResources
import kotlinx.android.synthetic.main.tab_list_row.*
import mozilla.components.browser.state.state.MediaState
import mozilla.components.browser.toolbar.MAX_URI_LENGTH
import mozilla.components.support.ktx.android.util.dpToFloat
import org.mozilla.fenix.R
import org.mozilla.fenix.components.metrics.Event
Expand Down Expand Up @@ -81,7 +82,12 @@ class TabViewHolder(
internal fun bindSession(tab: Tab) {
updateTab(tab)
updateTitle(tab.title)
updateHostname(tab.hostname)
// Truncate to MAX_URI_LENGTH to prevent the UI from locking up for
// extremely large URLs such as data URIs or bookmarklets. The same
// is done in the toolbar and awesomebar:
// https://github.com/mozilla-mobile/fenix/issues/1824
// https://github.com/mozilla-mobile/android-components/issues/6985
updateHostname(tab.hostname.take(MAX_URI_LENGTH))
updateFavIcon(tab.url, tab.icon)
updateSelected(tab.selected ?: false)
updatePlayPauseButton(tab.mediaState)
Expand Down
43 changes: 43 additions & 0 deletions app/src/test/java/org/mozilla/fenix/home/TabViewHolderTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* 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
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.fenix.home

import android.view.LayoutInflater
import io.mockk.mockk
import kotlinx.android.synthetic.main.tab_list_row.view.*
import mozilla.components.browser.state.state.MediaState
import mozilla.components.browser.toolbar.MAX_URI_LENGTH
import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.R
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.home.sessioncontrol.TabSessionInteractor
import org.mozilla.fenix.home.sessioncontrol.viewholders.TabViewHolder

@RunWith(FenixRobolectricTestRunner::class)
class TabViewHolderTest {

@Test
fun `extremely long URLs are truncated to prevent slowing down the UI`() {
val view = LayoutInflater.from(testContext).inflate(
R.layout.tab_list_row, null, false)

val interactor: TabSessionInteractor = mockk()
val tabViewHolder = TabViewHolder(view, interactor)

val extremelyLongUrl = "m".repeat(MAX_URI_LENGTH + 1)
val tab = Tab(
sessionId = "123",
url = extremelyLongUrl,
hostname = extremelyLongUrl,
title = "test",
mediaState = MediaState.State.NONE)
tabViewHolder.bindSession(tab)

assertEquals("m".repeat(MAX_URI_LENGTH), view.hostname.text)
}
}

0 comments on commit 4ebe209

Please sign in to comment.