forked from mozilla-mobile/android-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Close mozilla-mobile#7313: Use storage/loader to get thumbnails
We were seeing odd bugs and performance issues from trying to map the disk cache into the TabsTrayPresenter. A better solution, would be to load the thumbnails straight from the cache, and incremental updates via the store.
- Loading branch information
1 parent
fc6a582
commit d078536
Showing
16 changed files
with
285 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
.../thumbnails/src/main/java/mozilla/components/browser/thumbnails/loader/ThumbnailLoader.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* 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 mozilla.components.browser.thumbnails.loader | ||
|
||
import android.graphics.drawable.Drawable | ||
import android.widget.ImageView | ||
import androidx.annotation.MainThread | ||
import kotlinx.coroutines.CancellationException | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.launch | ||
import mozilla.components.browser.thumbnails.R | ||
import mozilla.components.browser.thumbnails.storage.ThumbnailStorage | ||
import mozilla.components.support.images.CancelOnDetach | ||
import mozilla.components.support.images.loader.ImageLoader | ||
import java.lang.ref.WeakReference | ||
|
||
/** | ||
* An implementation of [ImageLoader] for loading thumbnails into a [ImageView]. | ||
*/ | ||
class ThumbnailLoader(private val storage: ThumbnailStorage) : ImageLoader { | ||
|
||
override fun loadIntoView( | ||
view: ImageView, | ||
id: String, | ||
placeholder: Drawable?, | ||
error: Drawable? | ||
) { | ||
CoroutineScope(Dispatchers.Main).launch { | ||
loadIntoViewInternal(WeakReference(view), id, placeholder, error) | ||
} | ||
} | ||
|
||
@MainThread | ||
private suspend fun loadIntoViewInternal( | ||
view: WeakReference<ImageView>, | ||
id: String, | ||
placeholder: Drawable?, | ||
error: Drawable? | ||
) { | ||
// If we previously started loading into the view, cancel the job. | ||
val existingJob = view.get()?.getTag(R.id.mozac_browser_thumbnails_tag_job) as? Job | ||
existingJob?.cancel() | ||
|
||
view.get()?.setImageDrawable(placeholder) | ||
|
||
// Create a loading job | ||
val deferredThumbnail = storage.loadThumbnail(id) | ||
|
||
view.get()?.setTag(R.id.mozac_browser_thumbnails_tag_job, deferredThumbnail) | ||
val onAttachStateChangeListener = CancelOnDetach(deferredThumbnail).also { | ||
view.get()?.addOnAttachStateChangeListener(it) | ||
} | ||
|
||
try { | ||
val icon = deferredThumbnail.await() | ||
view.get()?.setImageBitmap(icon) | ||
} catch (e: CancellationException) { | ||
view.get()?.setImageDrawable(error) | ||
} finally { | ||
view.get()?.removeOnAttachStateChangeListener(onAttachStateChangeListener) | ||
view.get()?.setTag(R.id.mozac_browser_thumbnails_tag_job, null) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- 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/. --> | ||
|
||
<resources> | ||
<item name="mozac_browser_thumbnails_tag_job" type="id" /> | ||
</resources> |
114 changes: 114 additions & 0 deletions
114
...mbnails/src/test/java/mozilla/components/browser/thumbnails/loader/ThumbnailLoaderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* 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 mozilla.components.browser.thumbnails.loader | ||
|
||
import android.graphics.Bitmap | ||
import android.graphics.drawable.Drawable | ||
import android.widget.ImageView | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import kotlinx.coroutines.CompletableDeferred | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.test.TestCoroutineDispatcher | ||
import kotlinx.coroutines.test.resetMain | ||
import kotlinx.coroutines.test.setMain | ||
import mozilla.components.browser.thumbnails.R | ||
import mozilla.components.browser.thumbnails.storage.ThumbnailStorage | ||
import mozilla.components.support.test.any | ||
import mozilla.components.support.test.eq | ||
import mozilla.components.support.test.mock | ||
import mozilla.components.support.test.rule.MainCoroutineRule | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mockito.doReturn | ||
import org.mockito.Mockito.never | ||
import org.mockito.Mockito.spy | ||
import org.mockito.Mockito.verify | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class ThumbnailLoaderTest { | ||
private val testDispatcher = TestCoroutineDispatcher() | ||
|
||
@get:Rule | ||
val coroutinesTestRule = MainCoroutineRule(testDispatcher) | ||
|
||
@Before | ||
fun setup() { | ||
Dispatchers.setMain(testDispatcher) | ||
} | ||
|
||
@After | ||
fun teardown() { | ||
Dispatchers.resetMain() | ||
testDispatcher.cleanupTestCoroutines() | ||
} | ||
|
||
@Test | ||
fun `automatically load thumbnails into image view`() { | ||
val mockedBitmap: Bitmap = mock() | ||
val result = CompletableDeferred<Bitmap>() | ||
val view: ImageView = mock() | ||
val storage: ThumbnailStorage = mock() | ||
val loader = spy(ThumbnailLoader(storage)) | ||
|
||
doReturn(result).`when`(storage).loadThumbnail("123") | ||
|
||
loader.loadIntoView(view, "123") | ||
|
||
verify(view).setImageDrawable(null) | ||
verify(view).addOnAttachStateChangeListener(any()) | ||
verify(view).setTag(eq(R.id.mozac_browser_thumbnails_tag_job), any()) | ||
verify(view, never()).setImageBitmap(any()) | ||
|
||
result.complete(mockedBitmap) | ||
|
||
verify(view).setImageBitmap(mockedBitmap) | ||
verify(view).removeOnAttachStateChangeListener(any()) | ||
verify(view).setTag(R.id.mozac_browser_thumbnails_tag_job, null) | ||
} | ||
|
||
@Test | ||
fun `loadIntoView sets drawable to error if cancelled`() { | ||
val result = CompletableDeferred<Bitmap>() | ||
val view: ImageView = mock() | ||
val placeholder: Drawable = mock() | ||
val error: Drawable = mock() | ||
val storage: ThumbnailStorage = mock() | ||
val loader = spy(ThumbnailLoader(storage)) | ||
|
||
doReturn(result).`when`(storage).loadThumbnail("123") | ||
|
||
loader.loadIntoView(view, "123", placeholder = placeholder, error = error) | ||
|
||
verify(view).setImageDrawable(placeholder) | ||
|
||
result.cancel() | ||
|
||
verify(view).setImageDrawable(error) | ||
verify(view).removeOnAttachStateChangeListener(any()) | ||
verify(view).setTag(R.id.mozac_browser_thumbnails_tag_job, null) | ||
} | ||
|
||
@Test | ||
fun `loadIntoView cancels previous jobs`() { | ||
val result = CompletableDeferred<Bitmap>() | ||
val view: ImageView = mock() | ||
val previousJob: Job = mock() | ||
val storage: ThumbnailStorage = mock() | ||
val loader = spy(ThumbnailLoader(storage)) | ||
|
||
doReturn(previousJob).`when`(view).getTag(R.id.mozac_browser_thumbnails_tag_job) | ||
doReturn(result).`when`(storage).loadThumbnail("123") | ||
|
||
loader.loadIntoView(view, "123") | ||
|
||
verify(previousJob).cancel() | ||
|
||
result.cancel() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.