Repository navigation
Use the shared ArchiveBox search editor on Android #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ef8b902
Use the server search editor on Android
pirate 8f28f2e
Restore result navigation and exercise Android WebView search end to end
pirate d98e8e0
Refresh Pixel search screenshots and run search coverage in device CI
pirate 89a67f2
Search within the active Android library view
pirate 2f4cb5e
Preserve scoped search during progressive loads and activity recreation
pirate File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
53 changes: 53 additions & 0 deletions
53
app/src/androidTest/java/io/archivebox/app/ServerSearch.kt
This file contains hidden or 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,53 @@ | ||
| package io.archivebox.app | ||
|
|
||
| import android.content.ClipData | ||
| import android.content.ClipboardManager | ||
| import android.view.KeyEvent | ||
| import android.webkit.WebView | ||
| import androidx.test.espresso.Espresso.onView | ||
| import androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom | ||
| import androidx.test.espresso.web.assertion.WebViewAssertions.webMatches | ||
| import androidx.test.espresso.web.model.Atoms | ||
| import androidx.test.espresso.web.sugar.Web.onWebView | ||
| import androidx.test.espresso.web.webdriver.DriverAtoms.findElement | ||
| import androidx.test.espresso.web.webdriver.DriverAtoms.webClick | ||
| import androidx.test.espresso.web.webdriver.Locator | ||
| import androidx.test.platform.app.InstrumentationRegistry | ||
| import androidx.test.uiautomator.UiDevice | ||
| import kotlin.math.roundToInt | ||
| import org.hamcrest.Matchers.equalTo | ||
| import org.json.JSONArray | ||
|
|
||
| /** Use the real WebView input and keyboard so non-ASCII and URL punctuation survive submission. */ | ||
| internal fun enterServerSearch(query: String) { | ||
| // Older supported servers fall back to their ordinary Snapshot search form. | ||
| val editor = "form[role=search] [role=combobox], form[role=search] input[name=q]:not(.abx-search-field input)" | ||
| val instrumentation = InstrumentationRegistry.getInstrumentation() | ||
| val device = UiDevice.getInstance(instrumentation) | ||
| // Espresso's webClick synthesizes a DOM click, so Android's actual IME may still | ||
| // target a native view. Tap the rendered field, accounting for WebView density. | ||
| val center = Atoms.script( | ||
| """function(e) { | ||
| const r = e.getBoundingClientRect(); | ||
| return JSON.stringify([(r.left + r.width/2) * devicePixelRatio, (r.top + r.height/2) * devicePixelRatio]); | ||
| }""", Atoms.castOrDie(String::class.java)) | ||
| val point = JSONArray(onWebView().withElement(findElement(Locator.CSS_SELECTOR, editor)).perform(center).get()) | ||
| val offset = IntArray(2) | ||
| onView(isAssignableFrom(WebView::class.java)).check { view, _ -> view.getLocationOnScreen(offset) } | ||
| device.click(offset[0] + point.getDouble(0).roundToInt(), offset[1] + point.getDouble(1).roundToInt()) | ||
| device.waitForIdle() | ||
| instrumentation.runOnMainSync { | ||
| instrumentation.targetContext.getSystemService(ClipboardManager::class.java) | ||
| .setPrimaryClip(ClipData.newPlainText("Search", query)) | ||
| } | ||
| device.pressKeyCode(KeyEvent.KEYCODE_V, KeyEvent.META_CTRL_ON) | ||
| device.waitForIdle() | ||
| val value = Atoms.script("function(element) { return element.value; }", Atoms.castOrDie(String::class.java)) | ||
| onWebView().withElement(findElement(Locator.CSS_SELECTOR, "form[role=search] input[name=q]")) | ||
| .check(webMatches(value, equalTo(query))) | ||
| } | ||
|
|
||
| internal fun submitServerSearch() { | ||
| onWebView().withElement(findElement(Locator.CSS_SELECTOR, "form[role=search] :is(button,input)[type=submit]")) | ||
| .perform(webClick()) | ||
| } | ||
116 changes: 116 additions & 0 deletions
116
app/src/androidTest/java/io/archivebox/app/ServerSearchIntegrationTest.kt
This file contains hidden or 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,116 @@ | ||
| package io.archivebox.app | ||
|
|
||
| import android.content.Intent | ||
| import android.net.Uri | ||
| import androidx.compose.ui.test.* | ||
| import androidx.compose.ui.test.junit4.v2.createEmptyComposeRule | ||
| import androidx.test.espresso.web.assertion.WebViewAssertions.webMatches | ||
| import androidx.test.espresso.web.model.Atoms | ||
| import androidx.test.espresso.web.sugar.Web.onWebView | ||
| import androidx.test.espresso.web.webdriver.DriverAtoms.findElement | ||
| import androidx.test.espresso.web.webdriver.DriverAtoms.webClick | ||
| import androidx.test.espresso.web.webdriver.Locator | ||
| import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
| import androidx.test.platform.app.InstrumentationRegistry | ||
| import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry | ||
| import androidx.test.runner.lifecycle.Stage | ||
| import org.hamcrest.Matchers.equalTo | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertNotNull | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
|
|
||
| /** Exercises search against a connected server independently of LAN discovery or gallery fixtures. */ | ||
| @RunWith(AndroidJUnit4::class) | ||
| class ServerSearchIntegrationTest { | ||
|
pirate marked this conversation as resolved.
Resolved
|
||
| @get:Rule val compose = createEmptyComposeRule() | ||
|
|
||
| private fun await(tag: String) = compose.waitUntil(30_000) { | ||
| compose.onAllNodesWithTag(tag).fetchSemanticsNodes().isNotEmpty() | ||
| } | ||
|
|
||
| private fun connect() { | ||
| val args = InstrumentationRegistry.getArguments() | ||
| val context = InstrumentationRegistry.getInstrumentation().targetContext | ||
| val uri = Uri.parse("archivebox://connect").buildUpon() | ||
| .appendQueryParameter("server", requireNotNull(args.getString("serverUrl"))) | ||
| .appendQueryParameter("api_key", requireNotNull(args.getString("apiToken"))) | ||
| .build() | ||
| context.startActivity(Intent(Intent.ACTION_VIEW, uri, context, MainActivity::class.java) | ||
| .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) | ||
| await("connection.save") | ||
| compose.onNodeWithTag("connection.save").performScrollTo().performClick() | ||
| await("connection.status") | ||
| } | ||
|
|
||
| private fun recreateActivity() { | ||
| val instrumentation = InstrumentationRegistry.getInstrumentation() | ||
| instrumentation.runOnMainSync { | ||
| ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED).single().recreate() | ||
| } | ||
| await("browser.ready") | ||
| } | ||
|
|
||
| private val currentUrl = Atoms.script("return location.href;", Atoms.castOrDie(String::class.java)) | ||
|
|
||
| private fun waitForSubmittedQuery(query: String): Uri { | ||
| // The admin updates history when streamed results arrive; the document | ||
| // remains ready throughout, so wait for the actual submitted URL. | ||
| compose.waitUntil(30_000) { | ||
| Uri.parse(onWebView().perform(currentUrl).get()).getQueryParameter("q") == query | ||
| } | ||
| return Uri.parse(onWebView().perform(currentUrl).get()) | ||
| } | ||
|
|
||
| @Test fun submitAndReopenQuotedQuery() { | ||
| connect() | ||
| compose.onNodeWithTag("tab.Search").performClick() | ||
| await("browser.ready") | ||
| val query = "title:\"a & b/日本語\"" | ||
| enterServerSearch(query) | ||
| submitServerSearch() | ||
| await("browser.ready") | ||
| val submitted = waitForSubmittedQuery(query) | ||
| // A recreated Activity must restore results, including the encoded expression. | ||
| recreateActivity() | ||
| onWebView().check(webMatches(currentUrl, equalTo(submitted.toString()))) | ||
| compose.onNodeWithTag("tab.Archive").performClick() | ||
| compose.onNodeWithTag("sidebar.searchField").performClick() | ||
| await("browser.ready") | ||
| val input = Atoms.script("return document.querySelector('form[role=search] input[name=q]').value;", Atoms.castOrDie(String::class.java)) | ||
| onWebView().check(webMatches(input, equalTo(query))) | ||
| } | ||
|
|
||
| @Test fun nativeSearchStaysInSelectedTag() { | ||
| connect() | ||
| compose.onNodeWithTag("tab.Archive").performClick() | ||
| compose.onNodeWithTag("home").performScrollToNode(hasTestTag("route.Tags")) | ||
| compose.onNodeWithTag("route.Tags").performClick() | ||
| await("browser.ready") | ||
| onWebView().withElement(findElement(Locator.CSS_SELECTOR, "a[href*='tags__id__exact=']")) | ||
| .perform(webClick()) | ||
| await("browser.ready") | ||
| val filtered = Uri.parse(onWebView().perform(currentUrl).get()) | ||
| val tag = filtered.getQueryParameter("tags__id__exact") | ||
| assertNotNull("The tag count link must open a tag-filtered Snapshot view", tag) | ||
|
|
||
| compose.onNodeWithTag("tab.Search").performClick() | ||
|
pirate marked this conversation as resolved.
Resolved
|
||
| // The native Search tab must focus the filter's existing form. Opening a | ||
| // standalone editor here would appear functional while silently losing the tag. | ||
| val fieldFocused = Atoms.script( | ||
| "return document.activeElement.matches('[role=search] [role=combobox], [role=search] input[name=q]');", | ||
| Atoms.castOrDie(Boolean::class.javaObjectType)) | ||
| compose.waitUntil(30_000) { onWebView().perform(fieldFocused).get() } | ||
| onWebView().check(webMatches(currentUrl, equalTo(filtered.toString()))) | ||
| enterServerSearch("https") | ||
| submitServerSearch() | ||
| await("browser.ready") | ||
| val submitted = waitForSubmittedQuery("https") | ||
| assertEquals(filtered.path, submitted.path) | ||
| assertEquals(tag, submitted.getQueryParameter("tags__id__exact")) | ||
| assertEquals("https", submitted.getQueryParameter("q")) | ||
| recreateActivity() | ||
| onWebView().check(webMatches(currentUrl, equalTo(submitted.toString()))) | ||
| } | ||
| } | ||
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.