diff --git a/components/browser/tabstray/src/main/java/mozilla/components/browser/tabstray/thumbnail/TabThumbnailView.kt b/components/browser/tabstray/src/main/java/mozilla/components/browser/tabstray/thumbnail/TabThumbnailView.kt index 5d3c6b91f3c..bbb2db2d227 100644 --- a/components/browser/tabstray/src/main/java/mozilla/components/browser/tabstray/thumbnail/TabThumbnailView.kt +++ b/components/browser/tabstray/src/main/java/mozilla/components/browser/tabstray/thumbnail/TabThumbnailView.kt @@ -17,13 +17,17 @@ class TabThumbnailView(context: Context, attrs: AttributeSet) : AppCompatImageVi @VisibleForTesting public override fun setFrame(l: Int, t: Int, r: Int, b: Int): Boolean { - val changed = super.setFrame(l, t, r, b) - if (changed) { - val matrix = imageMatrix - val scaleFactor = width / drawable.intrinsicWidth.toFloat() - matrix.setScale(scaleFactor, scaleFactor, 0f, 0f) - imageMatrix = matrix + val result = super.setFrame(l, t, r, b) + + val matrix = imageMatrix + val scaleFactor = if (drawable != null) { + width / drawable.intrinsicWidth.toFloat() + } else { + 1F } - return changed + matrix.setScale(scaleFactor, scaleFactor, 0f, 0f) + imageMatrix = matrix + + return result } } diff --git a/components/browser/tabstray/src/test/java/mozilla/components/browser/tabstray/thumbnail/TabThumbnailViewTest.kt b/components/browser/tabstray/src/test/java/mozilla/components/browser/tabstray/thumbnail/TabThumbnailViewTest.kt index 58f23d2ddf4..07265ae3e32 100644 --- a/components/browser/tabstray/src/test/java/mozilla/components/browser/tabstray/thumbnail/TabThumbnailViewTest.kt +++ b/components/browser/tabstray/src/test/java/mozilla/components/browser/tabstray/thumbnail/TabThumbnailViewTest.kt @@ -4,6 +4,7 @@ package mozilla.components.browser.tabstray.thumbnail +import android.graphics.Matrix import android.graphics.drawable.Drawable import android.widget.ImageView import androidx.test.ext.junit.runners.AndroidJUnit4 @@ -14,6 +15,8 @@ import org.junit.Assert.assertNotEquals import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mockito.`when` +import org.mockito.Mockito.spy +import org.mockito.Mockito.verify import org.robolectric.Robolectric.buildAttributeSet @RunWith(AndroidJUnit4::class) @@ -61,6 +64,18 @@ class TabThumbnailViewTest { assertEquals(matrix, matrix2) } + + @Test + fun `view scaleFactor does not change if there is no drawable`() { + val view = spy(TabThumbnailView(testContext, emptyAttributeSet())) + val matrix: Matrix = spy(Matrix()) + + `when`(view.imageMatrix).thenReturn(matrix) + + view.setFrame(5, 5, 5, 5) + + verify(matrix).setScale(1f, 1f, 0f, 0f) + } } private fun emptyAttributeSet() = buildAttributeSet().build()