Skip to content

Commit b8da917

Browse files
Add instrumented test which checks if conversion does not crash.
Sadly, converting twice does crash.
1 parent 8b703c6 commit b8da917

3 files changed

Lines changed: 117 additions & 0 deletions

File tree

pdf2htmlEX/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ android {
4141
archivesBaseName = rootProject.name
4242

4343
ndk.abiFilters = project.hasProperty('abi') ? [ project.property('abi') ] : null
44+
45+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
4446
}
4547
buildTypes.release.externalNativeBuild {
4648
cmake.arguments '-DCMAKE_BUILD_TYPE=MinSizeRel'
@@ -267,6 +269,9 @@ dependencies {
267269
compileOnly project(":dependency-builder")
268270
implementation 'com.viliussutkus89:tmpfile-android:1.0.2'
269271
implementation 'androidx.annotation:annotation:1.1.0'
272+
273+
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
274+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
270275
}
271276

272277
task extractLibtmpfileSoForLinkingInCMake {
134 KB
Binary file not shown.
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* InstrumentedTests.java
3+
*
4+
* Copyright (C) 2019 Vilius Sutkus'89
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
package com.viliussutkus89.android.pdf2htmlex;
21+
22+
import android.app.Instrumentation;
23+
import android.content.Context;
24+
25+
import androidx.test.ext.junit.runners.AndroidJUnit4;
26+
import androidx.test.platform.app.InstrumentationRegistry;
27+
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
31+
import java.io.BufferedInputStream;
32+
import java.io.BufferedOutputStream;
33+
import java.io.File;
34+
import java.io.FileOutputStream;
35+
import java.io.IOException;
36+
import java.io.InputStream;
37+
import java.io.OutputStream;
38+
39+
import static org.junit.Assert.assertTrue;
40+
import static org.junit.Assert.fail;
41+
42+
@RunWith(AndroidJUnit4.class)
43+
public class InstrumentedTests {
44+
45+
private File extractAssetPDF(String filename) throws IOException {
46+
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
47+
Context appContext = instrumentation.getTargetContext();
48+
File mInputDir = new File(appContext.getCacheDir(), "PDFs-extracted-from-assets");
49+
if (!mInputDir.mkdir()) {
50+
assertTrue("Failed to create folder for input PDFs", mInputDir.exists());
51+
}
52+
53+
Context testContext = instrumentation.getContext();
54+
InputStream is = testContext.getAssets().open(filename);
55+
File fileInCache = new File(mInputDir, filename);
56+
copyFile(is, new FileOutputStream(fileInCache));
57+
assertTrue("Failed to copy input PDF", fileInCache.exists());
58+
return fileInCache;
59+
}
60+
61+
private void copyFile(InputStream input, OutputStream output) throws IOException {
62+
final int buffer_size = 1024;
63+
byte[] buffer = new byte[buffer_size];
64+
65+
BufferedInputStream in = new BufferedInputStream(input, buffer_size);
66+
BufferedOutputStream out = new BufferedOutputStream(output, buffer_size);
67+
68+
int haveRead;
69+
while (-1 != (haveRead = in.read(buffer))) {
70+
out.write(buffer, 0, haveRead);
71+
}
72+
out.flush();
73+
out.close();
74+
output.close();
75+
in.close();
76+
input.close();
77+
}
78+
79+
@Test
80+
public synchronized void conversionTest() {
81+
File pdfFile, htmlFile;
82+
try {
83+
pdfFile = extractAssetPDF("fontfile3_opentype.pdf");
84+
} catch (IOException e) {
85+
e.printStackTrace();
86+
fail("Failed to extract PDF from assets");
87+
return;
88+
}
89+
90+
pdf2htmlEX converter = new pdf2htmlEX(InstrumentationRegistry.getInstrumentation().getTargetContext());
91+
try {
92+
htmlFile = converter.convert(pdfFile);
93+
} catch (IOException e) {
94+
e.printStackTrace();
95+
fail("Failed to convert PDF to HTML");
96+
return;
97+
} catch (pdf2htmlEX.ConversionFailedException e) {
98+
e.printStackTrace();
99+
fail("Failed to convert PDF to HTML");
100+
return;
101+
}
102+
assertTrue("Converted HTML file not found!", htmlFile.exists());
103+
assertTrue("Converted HTML file empty!", htmlFile.length() > 0);
104+
}
105+
106+
@Test
107+
public void conversionTwiceTest() {
108+
conversionTest();
109+
conversionTest();
110+
}
111+
112+
}

0 commit comments

Comments
 (0)