22
33import android .content .Context ;
44import android .content .res .AssetManager ;
5+ import android .util .Log ;
56
67import androidx .test .ext .junit .runners .AndroidJUnit4 ;
78import androidx .test .filters .LargeTest ;
1415import org .junit .runner .RunWith ;
1516
1617import java .io .File ;
18+ import java .io .FileInputStream ;
1719import java .io .FileOutputStream ;
1820import java .io .IOException ;
1921import java .io .InputStream ;
2628public 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}
0 commit comments