Skip to content
Draft
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
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ dependencies {
androidTestImplementation 'androidx.test:runner:1.6.2'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.6.1'
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.3.0'
// espresso-idling-resource is used in main sourceSet as well. cannot be just androidTestImplementation
implementation 'androidx.test.espresso:espresso-idling-resource:3.6.1'
implementation 'androidx.annotation:annotation:1.9.1'
Expand Down
Binary file added app/src/androidTest/assets/spreadsheet-test.ods
Binary file not shown.
Binary file added app/src/androidTest/assets/style-various-1.docx
Binary file not shown.
113 changes: 107 additions & 6 deletions app/src/androidTest/java/at/tomtasche/reader/test/CoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import androidx.test.platform.app.InstrumentationRegistry;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

Expand All @@ -24,13 +26,50 @@
@LargeTest
@RunWith(AndroidJUnit4.class)
public class CoreTest {
private static Thread serverThread;
private File m_testFile;
private File m_passwordTestFile;
private File m_spreadsheetTestFile;
private File m_docxTestFile;

@Before
public void initializeCore() {
@BeforeClass
public static void startServer() throws InterruptedException {
Context appCtx = InstrumentationRegistry.getInstrumentation().getTargetContext();
CoreWrapper.initialize(appCtx);

// Create server cache directory
File serverCacheDir = new File(appCtx.getCacheDir(), "core/server");
if (!serverCacheDir.isDirectory()) {
serverCacheDir.mkdirs();
}
CoreWrapper.createServer(serverCacheDir.getAbsolutePath());

// Start server in background thread
serverThread = new Thread(() -> {
try {
CoreWrapper.listenServer(29665);
} catch (Exception e) {
e.printStackTrace();
}
});
serverThread.setDaemon(true);
serverThread.start();

// Give server time to start
Thread.sleep(1000);
}

@AfterClass
public static void stopServer() {
CoreWrapper.stopServer();
if (serverThread != null) {
serverThread.interrupt();
}
}

@Before
public void initializeCore() {
// Server is already initialized in @BeforeClass
}

@Before
Expand All @@ -47,6 +86,14 @@ public void extractTestFile() throws IOException {
try (InputStream inputStream = assetManager.open("password-test.odt")) {
copy(inputStream, m_passwordTestFile);
}
m_spreadsheetTestFile = new File(appCtx.getCacheDir(), "spreadsheet-test.ods");
try (InputStream inputStream = assetManager.open("spreadsheet-test.ods")) {
copy(inputStream, m_spreadsheetTestFile);
}
m_docxTestFile = new File(appCtx.getCacheDir(), "style-various-1.docx");
try (InputStream inputStream = assetManager.open("style-various-1.docx")) {
copy(inputStream, m_docxTestFile);
}
}

@After
Expand All @@ -57,6 +104,12 @@ public void cleanupTestFile() {
if (null != m_passwordTestFile) {
m_passwordTestFile.delete();
}
if (null != m_spreadsheetTestFile) {
m_spreadsheetTestFile.delete();
}
if (null != m_docxTestFile) {
m_docxTestFile.delete();
}
}

private static void copy(InputStream src, File dst) throws IOException {
Expand All @@ -81,7 +134,7 @@ public void test() {
coreOptions.editable = true;
coreOptions.cachePath = cachePath.getPath();

CoreWrapper.CoreResult coreResult = CoreWrapper.parse(coreOptions);
CoreWrapper.CoreResult coreResult = CoreWrapper.hostFile("test", coreOptions);
Assert.assertEquals(0, coreResult.errorCode);

File resultFile = new File(cacheDir, "result");
Expand All @@ -93,6 +146,30 @@ public void test() {
Assert.assertEquals(0, result.errorCode);
}

@Test
public void testDocxEdit() {
File cacheDir = InstrumentationRegistry.getInstrumentation().getTargetContext().getCacheDir();
File outputPath = new File(cacheDir, "core_output_docx");
File cachePath = new File(cacheDir, "core_cache");

CoreWrapper.CoreOptions coreOptions = new CoreWrapper.CoreOptions();
coreOptions.inputPath = m_docxTestFile.getAbsolutePath();
coreOptions.outputPath = outputPath.getPath();
coreOptions.editable = true;
coreOptions.cachePath = cachePath.getPath();

CoreWrapper.CoreResult coreResult = CoreWrapper.hostFile("docx-edit", coreOptions);
Assert.assertEquals(0, coreResult.errorCode);

File resultFile = new File(cacheDir, "result_docx");
coreOptions.outputPath = resultFile.getPath();

String htmlDiff = "{\"modifiedText\":{\"/child:16/child:0/child:0\":\"Outasdfsdafdline\",\"/child:24/child:0/child:0\":\"Colorasdfasdfasdfed Line\",\"/child:6/child:0/child:0\":\"Text hello world!\"}}";

CoreWrapper.CoreResult result = CoreWrapper.backtranslate(coreOptions, htmlDiff);
Assert.assertEquals(0, result.errorCode);
}

@Test
public void testPasswordProtectedDocumentWithoutPassword() {
File cacheDir = InstrumentationRegistry.getInstrumentation().getTargetContext().getCacheDir();
Expand All @@ -105,7 +182,7 @@ public void testPasswordProtectedDocumentWithoutPassword() {
coreOptions.editable = false;
coreOptions.cachePath = cachePath.getPath();

CoreWrapper.CoreResult coreResult = CoreWrapper.parse(coreOptions);
CoreWrapper.CoreResult coreResult = CoreWrapper.hostFile("password-test-no-pw", coreOptions);
Assert.assertEquals(-2, coreResult.errorCode);
}

Expand All @@ -122,7 +199,7 @@ public void testPasswordProtectedDocumentWithWrongPassword() {
coreOptions.editable = false;
coreOptions.cachePath = cachePath.getPath();

CoreWrapper.CoreResult coreResult = CoreWrapper.parse(coreOptions);
CoreWrapper.CoreResult coreResult = CoreWrapper.hostFile("password-test-wrong-pw", coreOptions);
Assert.assertEquals(-2, coreResult.errorCode);
}

Expand All @@ -139,7 +216,31 @@ public void testPasswordProtectedDocumentWithCorrectPassword() {
coreOptions.editable = false;
coreOptions.cachePath = cachePath.getPath();

CoreWrapper.CoreResult coreResult = CoreWrapper.parse(coreOptions);
CoreWrapper.CoreResult coreResult = CoreWrapper.hostFile("password-test-correct-pw", coreOptions);
Assert.assertEquals(0, coreResult.errorCode);
}

@Test
public void testSpreadsheetSheetNames() {
File cacheDir = InstrumentationRegistry.getInstrumentation().getTargetContext().getCacheDir();
File outputPath = new File(cacheDir, "spreadsheet_output");
File cachePath = new File(cacheDir, "spreadsheet_cache");

CoreWrapper.CoreOptions coreOptions = new CoreWrapper.CoreOptions();
coreOptions.inputPath = m_spreadsheetTestFile.getAbsolutePath();
coreOptions.outputPath = outputPath.getPath();
coreOptions.editable = false;
coreOptions.cachePath = cachePath.getPath();

CoreWrapper.CoreResult coreResult = CoreWrapper.hostFile("spreadsheet-test", coreOptions);
Assert.assertEquals("CoreWrapper should successfully parse the ODS file", 0, coreResult.errorCode);

// Verify we have exactly 3 sheets
Assert.assertEquals("ODS file should contain 3 sheets", 3, coreResult.pageNames.size());

// Verify sheet names match the actual sheet names from the ODS file
Assert.assertEquals("First sheet should be named 'hey'", "hey", coreResult.pageNames.get(0));
Assert.assertEquals("Second sheet should be named 'ho'", "ho", coreResult.pageNames.get(1));
Assert.assertEquals("Third sheet should be named 'Sheet3'", "Sheet3", coreResult.pageNames.get(2));
}
}
Loading
Loading