Skip to content

Commit

Permalink
Close mozilla-mobile#9145: Catch IOException where clearing thumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalmeida authored and mergify[bot] committed Dec 7, 2020
1 parent 8f9c61e commit 35c99f4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package mozilla.components.browser.thumbnails.utils

import android.content.Context
import android.graphics.Bitmap
import androidx.annotation.VisibleForTesting
import com.jakewharton.disklrucache.DiskLruCache
import mozilla.components.support.base.log.logger.Logger
import mozilla.components.concept.base.images.ImageLoadRequest
Expand All @@ -22,12 +23,17 @@ private const val WEBP_QUALITY = 90
*/
class ThumbnailDiskCache {
private val logger = Logger("ThumbnailDiskCache")
private var thumbnailCache: DiskLruCache? = null
@VisibleForTesting
internal var thumbnailCache: DiskLruCache? = null
private val thumbnailCacheWriteLock = Any()

internal fun clear(context: Context) {
synchronized(thumbnailCacheWriteLock) {
getThumbnailCache(context).delete()
try {
getThumbnailCache(context).delete()
} catch (e: IOException) {
logger.warn("Thumbnail cache could not be cleared. Perhaps there are none?")
}
thumbnailCache = null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package mozilla.components.browser.thumbnails.utils

import android.graphics.Bitmap
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.jakewharton.disklrucache.DiskLruCache
import mozilla.components.concept.base.images.ImageLoadRequest
import mozilla.components.support.test.any
import mozilla.components.support.test.mock
Expand All @@ -16,7 +17,8 @@ import org.junit.Assert.assertNull
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.Mockito
import org.mockito.Mockito.`when`
import java.io.IOException
import java.io.OutputStream

@RunWith(AndroidJUnit4::class)
Expand All @@ -28,7 +30,7 @@ class ThumbnailDiskCacheTest {
val request = ImageLoadRequest("123", 100)

val bitmap: Bitmap = mock()
Mockito.`when`(bitmap.compress(any(), ArgumentMatchers.anyInt(), any())).thenAnswer {
`when`(bitmap.compress(any(), ArgumentMatchers.anyInt(), any())).thenAnswer {
Assert.assertEquals(
Bitmap.CompressFormat.WEBP,
it.arguments[0] as Bitmap.CompressFormat
Expand Down Expand Up @@ -76,4 +78,17 @@ class ThumbnailDiskCacheTest {
data = cache.getThumbnailData(testContext, request)
assertNull(data)
}

@Test
fun `Clearing bitmap from disk catch IOException`() {
val cache = ThumbnailDiskCache()
val lruCache: DiskLruCache = mock()
cache.thumbnailCache = lruCache

`when`(lruCache.delete()).thenThrow(IOException("test"))

cache.clear(testContext)

assertNull(cache.thumbnailCache)
}
}
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ permalink: /changelog/
* ⚠️ **This is a breaking change**: The `SitePermissionsRules` constructor, now requires a new parameter `mediaKeySystemAccess`.
* 🌟 Added support for EME permission prompts, see [#7121](https://github.com/mozilla-mobile/android-components/issues/7121).

* **browser-thumbnail**
* Catch `IOException` that may be thrown when deleting thumbnails.

# 68.0.0

* [Commits](https://github.com/mozilla-mobile/android-components/compare/v67.0.0...v68.0.0)
Expand Down

0 comments on commit 35c99f4

Please sign in to comment.