Skip to content

Add instrumented tests and js tests#627

Open
ggtlvkma356 wants to merge 4 commits intoGrapheneOS:mainfrom
ggtlvkma356:Espresso-Web
Open

Add instrumented tests and js tests#627
ggtlvkma356 wants to merge 4 commits intoGrapheneOS:mainfrom
ggtlvkma356:Espresso-Web

Conversation

@ggtlvkma356
Copy link
Copy Markdown
Contributor

Adds instrumented tests that covers most of the user interactions.

The JS tests only cover outline, because pdf.js also has their own tests, which covers most cases already.

The changes to production code is minimal:

  • Changed visibility of two variables in main activity from private to package private
  • Moved one JS function to a separate file

@ggtlvkma356 ggtlvkma356 force-pushed the Espresso-Web branch 3 times, most recently from 6dd6ed5 to 7a61ba4 Compare April 24, 2026 19:36
Copy link
Copy Markdown
Member

@inthewaves inthewaves left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage looks good, just some gaps we could add

Comment on lines +156 to +196
@Test
fun openSecondDocument_resetsStateAndLoadsNewDocument() {
PdfViewerLauncher.launchWithTestAsset("test-multipage.pdf").use { scenario ->
PdfViewerTestUtils.waitForDocumentFullyLoaded(scenario)
PdfViewerTestUtils.waitForCanvasRendered(scenario)

scenario.onActivity {
it.onJumpToPageInDocument(3)
}
scenario.onActivity {
assertEquals(3, it.currentPage)
assertEquals(4, it.totalPages)
}

Intents.init()
try {
val secondUri = PdfViewerLauncher.testAssetUri("test-simple.pdf")
intending(hasAction(Intent.ACTION_OPEN_DOCUMENT))
.respondWith(
Instrumentation.ActivityResult(
Activity.RESULT_OK,
Intent().apply { data = secondUri }
)
)

robot.click(PdfViewerRobot.AppMenuItem.Open)
} finally {
Intents.release()
}

PdfViewerTestUtils.waitForDocumentChanged(scenario, expectedPages = 1)
PdfViewerTestUtils.waitForCanvasRendered(scenario)

scenario.onActivity {
assertEquals("Page should reset to 1", 1, it.currentPage)
assertEquals("New document should have 1 page", 1, it.totalPages)
}

PdfViewerTestUtils.assertTextLayerContent(scenario, "Test Text")
}
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test format is great: loading PDF A, doing some stuff, then load PDF B and verify all state is reset

Currently, this only tests that the page is reset, but there's other state could assert for as well (e.g., zoom state, rotation)

Also we could add some other test cases too, like

  • loading test-multipage.pdf which has an outline, checking that outline is visible in menu, then load some other PDF that doesn't have an outline, and ensure outline option is no longer shown in menu (and vice versa)
  • opening non-encrypted PDF first, then open encrypted PDF and ensure it still asks for password

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added these tests. However, for rotation, current it is not reset after loading a second pdf, so the assertion is commented out. I think this is a bug. The rest works correctly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can open an issue for this then

Comment on lines +31 to +57
@Override
public ParcelFileDescriptor openFile(Uri uri, @NonNull String mode) throws FileNotFoundException {
String filename = uri.getLastPathSegment();
if (filename == null) {
throw new FileNotFoundException("URI must include a filename");
}

File file = new File(getContext().getCacheDir(), filename);
if (!file.exists()) {
try {
InputStream is = getContext().getAssets().open(filename);
FileOutputStream os = new FileOutputStream(file);
byte[] buffer = new byte[8192];
int read;
while ((read = is.read(buffer)) != -1) {
os.write(buffer, 0, read);
}
os.close();
is.close();
} catch (IOException e) {
throw new FileNotFoundException(
"Failed to copy asset '" + filename + "': " + e.getMessage());
}
}

return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth adding a test for content provider open failure in middle of reading file (e.g. opening document from cloud provider or USB gets unplugged). Could do this like a query parameter on the Uri that would make it throw an exception partway

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the test, but for the current implementation it doesn't do much, because the UI didn't show error if I'm not mistaken. Adding some feedback will be a separate PR.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can open an issue for this error reporting then

Copy link
Copy Markdown
Member

@inthewaves inthewaves left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I'm sure this seems a bit trivial (I think so as well), but it could be good for reproducibility to include the .tex files used to generate the PDFs somewhere (or put them all in one markdown in separate code blocks)

@ggtlvkma356
Copy link
Copy Markdown
Contributor Author

Also, I'm sure this seems a bit trivial (I think so as well), but it could be good for reproducibility to include the .tex files used to generate the PDFs somewhere (or put them all in one markdown in separate code blocks)

I have added a Markdown file in the assets folder to include all three latex files.

@inthewaves inthewaves self-assigned this Apr 28, 2026
@ggtlvkma356
Copy link
Copy Markdown
Contributor Author

I opened #635, #636 to track the two issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants