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
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:
... | ... | @@ -75,7 +75,8 @@ class BrowserToolbar @JvmOverloads constructor( |
75 | 75 | defStyleAttr: Int = 0
|
76 | 76 | ) : ViewGroup(context, attrs, defStyleAttr), Toolbar {
|
77 | 77 | private var state: State = State.DISPLAY
|
78 | - private var searchTerms: String = ""
|
|
78 | + @VisibleForTesting
|
|
79 | + internal var searchTerms: String = ""
|
|
79 | 80 | private var urlCommitListener: ((String) -> Boolean)? = null
|
80 | 81 | |
81 | 82 | /**
|
... | ... | @@ -219,11 +220,11 @@ class BrowserToolbar @JvmOverloads constructor( |
219 | 220 | }
|
220 | 221 | |
221 | 222 | override fun setSearchTerms(searchTerms: String) {
|
223 | + this.searchTerms = searchTerms.take(MAX_URI_LENGTH)
|
|
224 | + |
|
222 | 225 | if (state == State.EDIT) {
|
223 | - edit.editSuggestion(searchTerms)
|
|
226 | + edit.editSuggestion(this.searchTerms)
|
|
224 | 227 | }
|
225 | - |
|
226 | - this.searchTerms = searchTerms
|
|
227 | 228 | }
|
228 | 229 | |
229 | 230 | override fun displayProgress(progress: Int) {
|
... | ... | @@ -152,6 +152,38 @@ class BrowserToolbarTest { |
152 | 152 | assertEquals("c".repeat(MAX_URI_LENGTH - 1), capturedValues[2])
|
153 | 153 | }
|
154 | 154 | |
155 | + @Test
|
|
156 | + fun `searchTerms is truncated in case it is greater than MAX_URI_LENGTH`() {
|
|
157 | + val toolbar = BrowserToolbar(testContext)
|
|
158 | + toolbar.edit = spy(toolbar.edit)
|
|
159 | + toolbar.editMode()
|
|
160 | + |
|
161 | + toolbar.setSearchTerms("a".repeat(MAX_URI_LENGTH + 1))
|
|
162 | + |
|
163 | + // Value was too long and should've been truncated
|
|
164 | + assertEquals(toolbar.searchTerms.length, MAX_URI_LENGTH)
|
|
165 | + verify(toolbar.edit).editSuggestion("a".repeat(MAX_URI_LENGTH))
|
|
166 | + }
|
|
167 | + |
|
168 | + @Test
|
|
169 | + fun `searchTerms is not truncated in case it is equal or less than MAX_URI_LENGTH`() {
|
|
170 | + val toolbar = BrowserToolbar(testContext)
|
|
171 | + toolbar.edit = spy(toolbar.edit)
|
|
172 | + toolbar.editMode()
|
|
173 | + |
|
174 | + toolbar.setSearchTerms("b".repeat(MAX_URI_LENGTH))
|
|
175 | + |
|
176 | + // Value should be the same as before
|
|
177 | + assertEquals(toolbar.searchTerms.length, MAX_URI_LENGTH)
|
|
178 | + verify(toolbar.edit).editSuggestion("b".repeat(MAX_URI_LENGTH))
|
|
179 | + |
|
180 | + toolbar.setSearchTerms("c".repeat(MAX_URI_LENGTH - 1))
|
|
181 | + |
|
182 | + // Value should be the same as before
|
|
183 | + assertEquals(toolbar.searchTerms.length, MAX_URI_LENGTH - 1)
|
|
184 | + verify(toolbar.edit).editSuggestion("c".repeat(MAX_URI_LENGTH - 1))
|
|
185 | + }
|
|
186 | + |
|
155 | 187 | @Test
|
156 | 188 | fun `last URL will be forwarded to edit toolbar when switching mode`() {
|
157 | 189 | val toolbar = BrowserToolbar(testContext)
|