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.
Issue mozilla-mobile#6827: Move Thumbnails into its own browser compo…
…nent
- Loading branch information
1 parent
31b8f8e
commit b7225a7
Showing
9 changed files
with
260 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# [Android Components](../../../README.md) > Browser > Thumbnails | ||
|
||
A component for loading and storing website thumbnails (screenshot of the website). | ||
|
||
## Usage | ||
|
||
### Setting up the dependency | ||
|
||
Use Gradle to download the library from [maven.mozilla.org](https://maven.mozilla.org/) ([Setup repository](../../../README.md#maven-repository)): | ||
|
||
```Groovy | ||
implementation "org.mozilla.components:browser-thumbnails:{latest-version}" | ||
``` | ||
|
||
## License | ||
|
||
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/ |
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,47 @@ | ||
/* 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/. */ | ||
|
||
apply plugin: 'com.android.library' | ||
apply plugin: 'kotlin-android' | ||
|
||
android { | ||
compileSdkVersion config.compileSdkVersion | ||
|
||
defaultConfig { | ||
minSdkVersion config.minSdkVersion | ||
targetSdkVersion config.targetSdkVersion | ||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
|
||
packagingOptions { | ||
exclude 'META-INF/proguard/androidx-annotations.pro' | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation project(':support-ktx') | ||
implementation project(':concept-engine') | ||
implementation project(':browser-session') | ||
|
||
implementation Dependencies.androidx_annotation | ||
|
||
testImplementation project(':support-test') | ||
|
||
testImplementation Dependencies.androidx_test_junit | ||
testImplementation Dependencies.testing_mockito | ||
|
||
androidTestImplementation Dependencies.androidx_test_core | ||
androidTestImplementation Dependencies.androidx_test_runner | ||
androidTestImplementation Dependencies.androidx_test_rules | ||
} | ||
|
||
apply from: '../../../publish.gradle' | ||
ext.configurePublish(config.componentsGroupId, archivesBaseName, project.ext.description) |
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,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
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,5 @@ | ||
<!-- 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/. --> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="mozilla.components.browser.thumbnails" /> |
72 changes: 72 additions & 0 deletions
72
...owser/thumbnails/src/main/java/mozilla/components/browser/thumbnails/BrowserThumbnails.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,72 @@ | ||
/* 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 | ||
|
||
import android.content.Context | ||
import androidx.annotation.VisibleForTesting | ||
import mozilla.components.browser.session.SelectionAwareSessionObserver | ||
import mozilla.components.browser.session.Session | ||
import mozilla.components.browser.session.SessionManager | ||
import mozilla.components.concept.engine.EngineView | ||
import mozilla.components.support.base.feature.LifecycleAwareFeature | ||
import mozilla.components.support.ktx.android.content.isOSOnLowMemory | ||
|
||
/** | ||
* Feature implementation for automatically taking thumbnails of sites. | ||
* The feature will take a screenshot when the page finishes loading, | ||
* and will add it to the [Session.thumbnail] property. | ||
* | ||
* If the OS is under low memory conditions, the screenshot will be not taken. | ||
* Ideally, this should be used in conjunction with [SessionManager.onLowMemory] to allow | ||
* free up some [Session.thumbnail] from memory. | ||
*/ | ||
class BrowserThumbnails( | ||
private val context: Context, | ||
private val engineView: EngineView, | ||
sessionManager: SessionManager | ||
) : LifecycleAwareFeature { | ||
|
||
private val observer = ThumbnailsRequestObserver(sessionManager) | ||
|
||
/** | ||
* Starts observing the selected session to listen for when a session finish loading. | ||
*/ | ||
override fun start() { | ||
observer.observeSelected() | ||
} | ||
|
||
/** | ||
* Stops observing the selected session. | ||
*/ | ||
override fun stop() { | ||
observer.stop() | ||
} | ||
|
||
internal inner class ThumbnailsRequestObserver( | ||
sessionManager: SessionManager | ||
) : SelectionAwareSessionObserver(sessionManager) { | ||
|
||
override fun onLoadingStateChanged(session: Session, loading: Boolean) { | ||
if (!loading) { | ||
requestScreenshot(session) | ||
} | ||
} | ||
} | ||
|
||
private fun requestScreenshot(session: Session) { | ||
if (!isLowOnMemory()) { | ||
engineView.captureThumbnail { | ||
session.thumbnail = it | ||
} | ||
} else { | ||
session.thumbnail = null | ||
} | ||
} | ||
|
||
@VisibleForTesting | ||
internal var testLowMemory = false | ||
|
||
private fun isLowOnMemory() = testLowMemory || context.isOSOnLowMemory() | ||
} |
88 changes: 88 additions & 0 deletions
88
...r/thumbnails/src/test/java/mozilla/components/browser/thumbnails/BrowserThumbnailsTest.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,88 @@ | ||
/* 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 | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import mozilla.components.browser.session.Session | ||
import mozilla.components.browser.session.SessionManager | ||
import mozilla.components.concept.engine.Engine | ||
import mozilla.components.concept.engine.EngineView | ||
import mozilla.components.support.test.any | ||
import mozilla.components.support.test.mock | ||
import mozilla.components.support.test.robolectric.testContext | ||
import org.junit.Assert.assertNull | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mockito.never | ||
import org.mockito.Mockito.spy | ||
import org.mockito.Mockito.verify | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class BrowserThumbnailsTest { | ||
|
||
private lateinit var mockSessionManager: SessionManager | ||
private lateinit var mockEngineView: EngineView | ||
private lateinit var thumbnails: BrowserThumbnails | ||
|
||
@Before | ||
fun setup() { | ||
val engine = mock<Engine>() | ||
mockSessionManager = spy(SessionManager(engine)) | ||
mockEngineView = mock() | ||
thumbnails = BrowserThumbnails(testContext, mockEngineView, mockSessionManager) | ||
} | ||
|
||
@Test | ||
fun `when feature is stop must not capture thumbnail when a site finish loading`() { | ||
thumbnails.start() | ||
thumbnails.stop() | ||
|
||
val session = getSelectedSession() | ||
|
||
session.notifyObservers { | ||
onLoadingStateChanged(session, false) | ||
} | ||
|
||
verify(mockEngineView, never()).captureThumbnail(any()) | ||
} | ||
|
||
@Test | ||
fun `feature must capture thumbnail when a site finish loading`() { | ||
thumbnails.start() | ||
|
||
val session = getSelectedSession() | ||
|
||
session.notifyObservers { | ||
onLoadingStateChanged(session, false) | ||
} | ||
|
||
verify(mockEngineView).captureThumbnail(any()) | ||
} | ||
|
||
@Test | ||
fun `when a page is loaded and the os is in low memory condition none thumbnail should be captured`() { | ||
thumbnails.start() | ||
|
||
val session = getSelectedSession() | ||
session.thumbnail = mock() | ||
|
||
thumbnails.testLowMemory = true | ||
|
||
session.notifyObservers { | ||
onLoadingStateChanged(session, false) | ||
} | ||
|
||
verify(mockEngineView, never()).captureThumbnail(any()) | ||
assertNull(session.thumbnail) | ||
} | ||
|
||
private fun getSelectedSession(): Session { | ||
val session = Session("https://www.mozilla.org") | ||
mockSessionManager.add(session) | ||
mockSessionManager.select(session) | ||
return session | ||
} | ||
} |
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