Skip to content

Commit 725ccb4

Browse files
committed
REVERTME: add test script
1 parent 9b6abe2 commit 725ccb4

3 files changed

Lines changed: 131 additions & 0 deletions

File tree

88.5 KB
Binary file not shown.

app/src/androidTest/java/at/tomtasche/reader/test/CoreTest.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Context;
44
import android.content.res.AssetManager;
5+
import android.util.Log;
56

67
import androidx.test.ext.junit.runners.AndroidJUnit4;
78
import androidx.test.filters.LargeTest;
@@ -14,6 +15,7 @@
1415
import org.junit.runner.RunWith;
1516

1617
import java.io.File;
18+
import java.io.FileInputStream;
1719
import java.io.FileOutputStream;
1820
import java.io.IOException;
1921
import java.io.InputStream;
@@ -26,6 +28,7 @@
2628
public class CoreTest {
2729
private File m_testFile;
2830
private File m_passwordTestFile;
31+
private File m_coreLibTestFile;
2932

3033
@Before
3134
public void initializeCore() {
@@ -38,6 +41,7 @@ public void extractTestFile() throws IOException {
3841
Context appCtx = InstrumentationRegistry.getInstrumentation().getTargetContext();
3942
m_testFile = new File(appCtx.getCacheDir(), "test.odt");
4043
m_passwordTestFile = new File(appCtx.getCacheDir(), "password-test.odt");
44+
m_coreLibTestFile = new File(appCtx.getCacheDir(), "style-various-1.odt");
4145

4246
Context testCtx = InstrumentationRegistry.getInstrumentation().getContext();
4347
AssetManager assetManager = testCtx.getAssets();
@@ -47,6 +51,9 @@ public void extractTestFile() throws IOException {
4751
try (InputStream inputStream = assetManager.open("password-test.odt")) {
4852
copy(inputStream, m_passwordTestFile);
4953
}
54+
try (InputStream inputStream = assetManager.open("style-various-1.odt")) {
55+
copy(inputStream, m_coreLibTestFile);
56+
}
5057
}
5158

5259
@After
@@ -57,6 +64,9 @@ public void cleanupTestFile() {
5764
if (null != m_passwordTestFile) {
5865
m_passwordTestFile.delete();
5966
}
67+
if (null != m_coreLibTestFile) {
68+
m_coreLibTestFile.delete();
69+
}
6070
}
6171

6272
private static void copy(InputStream src, File dst) throws IOException {
@@ -142,4 +152,72 @@ public void testPasswordProtectedDocumentWithCorrectPassword() {
142152
CoreWrapper.CoreResult coreResult = CoreWrapper.parse(coreOptions);
143153
Assert.assertEquals(0, coreResult.errorCode);
144154
}
155+
156+
@Test
157+
public void testCoreLibraryEditFormat() {
158+
// This test exactly mirrors the core library's edit_odt_diff test
159+
File cacheDir = InstrumentationRegistry.getInstrumentation().getTargetContext().getCacheDir();
160+
File outputPath = new File(cacheDir, "core_output_style");
161+
File cachePath = new File(cacheDir, "core_cache_style");
162+
163+
// Parse the document with editable=true
164+
CoreWrapper.CoreOptions coreOptions = new CoreWrapper.CoreOptions();
165+
coreOptions.inputPath = m_coreLibTestFile.getAbsolutePath();
166+
coreOptions.outputPath = outputPath.getPath();
167+
coreOptions.editable = true;
168+
coreOptions.cachePath = cachePath.getPath();
169+
170+
CoreWrapper.CoreResult parseResult = CoreWrapper.parse(coreOptions);
171+
Assert.assertEquals("Parse should succeed", 0, parseResult.errorCode);
172+
173+
// Use the exact same diff format as the core library test
174+
String htmlDiff = "{\"modifiedText\":{\"/child:16/child:0\":\"Outasdfsdafdline\",\"/child:24/child:0\":\"Colorasdfasdfasdfed Line\",\"/child:6/child:0\":\"Text hello world!\"}}";
175+
176+
// Set output path for the edited file
177+
File editedFile = new File(cacheDir, "style-various-1_edit_diff");
178+
coreOptions.outputPath = editedFile.getPath();
179+
180+
// Perform the edit
181+
CoreWrapper.CoreResult editResult = CoreWrapper.backtranslate(coreOptions, htmlDiff);
182+
Assert.assertEquals("Edit should succeed", 0, editResult.errorCode);
183+
184+
// Verify the file was created
185+
File outputFile = new File(editResult.outputPath);
186+
Log.e("CoreTest", "Edited file saved to: " + outputFile.getAbsolutePath());
187+
Log.e("CoreTest", "File size: " + outputFile.length() + " bytes");
188+
Assert.assertTrue("Edited file should exist", outputFile.exists());
189+
Assert.assertTrue("Edited file should have content", outputFile.length() > 0);
190+
191+
// Let's verify the edit actually worked by re-parsing the edited file
192+
CoreWrapper.CoreOptions verifyOptions = new CoreWrapper.CoreOptions();
193+
verifyOptions.inputPath = outputFile.getAbsolutePath();
194+
File verifyOutput = new File(cacheDir, "verify_output");
195+
verifyOptions.outputPath = verifyOutput.getPath();
196+
verifyOptions.editable = false;
197+
verifyOptions.cachePath = cachePath.getPath();
198+
199+
CoreWrapper.CoreResult verifyResult = CoreWrapper.parse(verifyOptions);
200+
Assert.assertEquals("Edited file should be parseable", 0, verifyResult.errorCode);
201+
Log.e("CoreTest", "Successfully verified edited file can be reopened");
202+
203+
// Try to copy to app's external files directory which should be accessible
204+
try {
205+
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
206+
File externalFilesDir = appContext.getExternalFilesDir(null);
207+
if (externalFilesDir != null) {
208+
File externalCopy = new File(externalFilesDir, "edited_test_output.odt");
209+
copy(new java.io.FileInputStream(outputFile), externalCopy);
210+
Log.e("CoreTest", "Copied to external: " + externalCopy.getAbsolutePath());
211+
212+
// Also try to make it world-readable
213+
externalCopy.setReadable(true, false);
214+
215+
// Keep file available for 10 seconds so we can pull it
216+
Log.e("CoreTest", "Waiting 10 seconds - pull the file now!");
217+
Thread.sleep(10000);
218+
}
219+
} catch (Exception e) {
220+
Log.e("CoreTest", "Failed to copy to external: " + e.getMessage());
221+
}
222+
}
145223
}

test-and-pull-edited-file.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
3+
# Script to run the ODT edit test and pull the edited file before it's cleaned up
4+
5+
echo "Starting ODT edit test and file retrieval..."
6+
7+
# First, build the test APK
8+
echo "Building test APK..."
9+
./gradlew assembleProDebugAndroidTest assembleProDebug
10+
11+
if [ $? -ne 0 ]; then
12+
echo "✗ Build failed!"
13+
exit 1
14+
fi
15+
16+
echo "Build complete. Installing and running test..."
17+
18+
# Start a background process to pull the file after a delay
19+
(
20+
echo "Waiting for test to create the file..."
21+
# Now we only need to wait for the actual test execution, not the build
22+
sleep 8
23+
24+
# Try to pull the file
25+
echo "Attempting to pull the edited file..."
26+
/home/tom/snap/android-sdk/platform-tools/adb pull \
27+
/storage/emulated/0/Android/data/at.tomtasche.reader.pro/files/edited_test_output.odt \
28+
./edited_test_output.odt 2>&1
29+
30+
if [ $? -eq 0 ]; then
31+
echo "✓ SUCCESS: File pulled to ./edited_test_output.odt"
32+
ls -lh ./edited_test_output.odt
33+
echo ""
34+
echo "You can now open edited_test_output.odt to verify the edits:"
35+
echo " - Text at /child:16/child:0 should be: 'Outasdfsdafdline'"
36+
echo " - Text at /child:24/child:0 should be: 'Colorasdfasdfasdfed Line'"
37+
echo " - Text at /child:6/child:0 should be: 'Text hello world!'"
38+
else
39+
echo "✗ FAILED: Could not pull the file. The test may have completed too quickly."
40+
echo "Try increasing the Thread.sleep() time in the test."
41+
fi
42+
) &
43+
44+
# Run the test (should be fast since everything is already built)
45+
echo "Running CoreTest.testCoreLibraryEditFormat..."
46+
./gradlew connectedProDebugAndroidTest \
47+
-Pandroid.testInstrumentationRunnerArguments.class=at.tomtasche.reader.test.CoreTest#testCoreLibraryEditFormat
48+
49+
# Wait for the background process to complete
50+
wait
51+
52+
echo ""
53+
echo "Test and file retrieval complete!"

0 commit comments

Comments
 (0)