Richard Pospesel pushed to branch android-components-102.0.14-12.0-1 at The Tor Project / Applications / android-components
Commits: 18a85e4a by sarah541 at 2022-08-30T20:29:32+00:00 Improve search term performance on the toolbar
- - - - -
2 changed files:
- components/browser/toolbar/src/main/java/mozilla/components/browser/toolbar/BrowserToolbar.kt - components/browser/toolbar/src/test/java/mozilla/components/browser/toolbar/BrowserToolbarTest.kt
Changes:
===================================== components/browser/toolbar/src/main/java/mozilla/components/browser/toolbar/BrowserToolbar.kt ===================================== @@ -75,7 +75,8 @@ class BrowserToolbar @JvmOverloads constructor( defStyleAttr: Int = 0 ) : ViewGroup(context, attrs, defStyleAttr), Toolbar { private var state: State = State.DISPLAY - private var searchTerms: String = "" + @VisibleForTesting + internal var searchTerms: String = "" private var urlCommitListener: ((String) -> Boolean)? = null
/** @@ -219,11 +220,11 @@ class BrowserToolbar @JvmOverloads constructor( }
override fun setSearchTerms(searchTerms: String) { + this.searchTerms = searchTerms.take(MAX_URI_LENGTH) + if (state == State.EDIT) { - edit.editSuggestion(searchTerms) + edit.editSuggestion(this.searchTerms) } - - this.searchTerms = searchTerms }
override fun displayProgress(progress: Int) {
===================================== components/browser/toolbar/src/test/java/mozilla/components/browser/toolbar/BrowserToolbarTest.kt ===================================== @@ -152,6 +152,38 @@ class BrowserToolbarTest { assertEquals("c".repeat(MAX_URI_LENGTH - 1), capturedValues[2]) }
+ @Test + fun `searchTerms is truncated in case it is greater than MAX_URI_LENGTH`() { + val toolbar = BrowserToolbar(testContext) + toolbar.edit = spy(toolbar.edit) + toolbar.editMode() + + toolbar.setSearchTerms("a".repeat(MAX_URI_LENGTH + 1)) + + // Value was too long and should've been truncated + assertEquals(toolbar.searchTerms.length, MAX_URI_LENGTH) + verify(toolbar.edit).editSuggestion("a".repeat(MAX_URI_LENGTH)) + } + + @Test + fun `searchTerms is not truncated in case it is equal or less than MAX_URI_LENGTH`() { + val toolbar = BrowserToolbar(testContext) + toolbar.edit = spy(toolbar.edit) + toolbar.editMode() + + toolbar.setSearchTerms("b".repeat(MAX_URI_LENGTH)) + + // Value should be the same as before + assertEquals(toolbar.searchTerms.length, MAX_URI_LENGTH) + verify(toolbar.edit).editSuggestion("b".repeat(MAX_URI_LENGTH)) + + toolbar.setSearchTerms("c".repeat(MAX_URI_LENGTH - 1)) + + // Value should be the same as before + assertEquals(toolbar.searchTerms.length, MAX_URI_LENGTH - 1) + verify(toolbar.edit).editSuggestion("c".repeat(MAX_URI_LENGTH - 1)) + } + @Test fun `last URL will be forwarded to edit toolbar when switching mode`() { val toolbar = BrowserToolbar(testContext)
View it on GitLab: https://gitlab.torproject.org/tpo/applications/android-components/-/commit/1...
tbb-commits@lists.torproject.org