Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions example/e2e/android-legacy/search-unsupported.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Search on Android devices without the renderer's newer API (the API 34
# emulator image, SDK extension 7): every search reports isSupported: false
# ("n/a"), and the document stays usable. Devices with the newer API run
# viewer-search instead.
# maestro --device <id> test example/e2e/android-legacy/search-unsupported.yaml
appId: software.gmi.betterpdf.example
---
- runFlow: ../_start.yaml
- tapOn: "Viewer"
- tapOn: "Bundled sample.*"
- extendedWaitUntil: { visible: "1 / 3", timeout: 15000 }
- tapOn: "Find"
- tapOn: { id: "search-input" }
- inputText: "a"
- extendedWaitUntil: { visible: { id: "search-status", text: "n/a" }, timeout: 5000 }
- inputText: "b"
- extendedWaitUntil: { visible: { id: "search-status", text: "n/a" }, timeout: 5000 }
- tapOn: "Find"
- tapOn: "Last"
- extendedWaitUntil: { visible: "3 / 3", timeout: 5000 }
- tapOn: "‹ Back"
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ internal interface DocumentRenderer : AutoCloseable {
/** Whether a long press can select text: the same renderers. */
val supportsTextSelection: Boolean get() = supportsPasswords

/** Whether `search()` can find text: the same renderers. */
val supportsSearch: Boolean get() = supportsPasswords

/** Android 12–14 with SDK extension 13; API 35+ uses `PdfRenderer` itself. */
@get:ChecksSdkIntAtLeast(api = PRE_V_EXTENSION_VERSION, extension = Build.VERSION_CODES.S)
private val hasPreV: Boolean get() = usesPreV(Build.VERSION.SDK_INT, sExtensionVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ internal typealias SearchStart =
*/
internal class SearchController(
private val view: Host,
private val canSearch: Boolean = DocumentRenderer.supportsSearch,
private val emit: (PdfSearchChangeEvent) -> Unit,
) : PdfSearchTask.Listener {
interface Host {
Expand Down Expand Up @@ -67,8 +68,10 @@ internal class SearchController(
view.searchResults = results
startPage = view.currentPageIndex
searchedPages = 0
isSupported = true
isComplete = false
// Without the renderer's text API a task would only open the document
// and a page to find out.
isSupported = canSearch
isComplete = !canSearch
runTask()
emitNow()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SearchControllerTest {

private val host = Host(pageCount = 10, currentPageIndex = 4)
private val events = ArrayList<PdfSearchChangeEvent>()
private val controller = SearchController(host) { events += it }
private val controller = SearchController(host, canSearch = true) { events += it }
private val started = ArrayList<Started>()
private var canStart = true
private val startSearch: SearchStart = { query, pageCount, startPage, pages, listener ->
Expand All @@ -36,6 +36,8 @@ class SearchControllerTest {
}
}

private val startNothing: SearchStart = { _, _, _, _, _ -> throw AssertionError("A device that cannot search started a task") }

private fun match(page: Int) = listOf(listOf(PageRect(page.toFloat(), 0f, page + 1f, 1f)))

private fun search(index: Int, pages: Iterable<Int>, matchesOn: Set<Int> = emptySet()) {
Expand Down Expand Up @@ -161,4 +163,34 @@ class SearchControllerTest {
controller.onViewAttached()
assertEquals(1, started.size)
}

@Test
fun `a search on a device that cannot search ends at once without a task`() {
val controller = SearchController(host, canSearch = false) { events += it }
controller.onDocumentChanged(startNothing)
controller.onViewAttached()
controller.search("lorem")

val event = events.single()
assertEquals("lorem", event.query)
assertEquals(0.0, event.matchCount, 0.0)
assertTrue(event.isComplete)
assertFalse(event.isSupported)

controller.onViewDetached()
controller.onViewAttached()
assertEquals(1, events.size)
}

@Test
fun `a search on a device that cannot search, while the view is detached, ends at once`() {
val controller = SearchController(host, canSearch = false) { events += it }
controller.onDocumentChanged(startNothing)
controller.search("lorem")

assertTrue(events.single().isComplete)
assertFalse(events.single().isSupported)
controller.onViewAttached()
assertEquals(1, events.size)
}
}
Loading