Skip to content

Commit ea72905

Browse files
author
irgendeinich
committed
Fix issue where events too close to each other would get coalesced
Add tests for form field getters and setters
1 parent 71cdfc6 commit ea72905

File tree

5 files changed

+117
-2
lines changed

5 files changed

+117
-2
lines changed

android/src/main/java/com/pspdfkit/react/TestingModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static String getValue(@NonNull String key) throws InterruptedException {
4040
if (!values.containsKey(key)) {
4141
values.wait(60000);
4242
if (!values.containsKey(key)) {
43-
throw new IllegalArgumentException("Key " + key + " was not found.");
43+
throw new IllegalArgumentException("Key " + key + " was not found. Got: " + values.toString());
4444
}
4545
}
4646
return values.get(key);

android/src/main/java/com/pspdfkit/react/events/PdfViewDataReturnedEvent.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ public class PdfViewDataReturnedEvent extends Event<PdfViewDataReturnedEvent> {
2222

2323
public static final String EVENT_NAME = "pdfViewDataReturned";
2424

25+
private final int requestId;
2526
private final WritableMap payload;
2627

2728
public PdfViewDataReturnedEvent(@IdRes int viewId, int requestId, @NonNull List<Annotation> annotationsToSerialize) {
2829
super(viewId);
30+
this.requestId = requestId;
2931
Map<String, Object> map = new HashMap<>();
3032
map.put("requestId", requestId);
3133
try {
@@ -47,6 +49,7 @@ public PdfViewDataReturnedEvent(@IdRes int viewId, int requestId, @NonNull List<
4749

4850
public PdfViewDataReturnedEvent(@IdRes int viewId, int requestId, @NonNull JSONObject jsonObject) {
4951
super(viewId);
52+
this.requestId = requestId;
5053
Map<String, Object> map = new HashMap<>();
5154
map.put("requestId", requestId);
5255

@@ -61,7 +64,7 @@ public PdfViewDataReturnedEvent(@IdRes int viewId, int requestId, @NonNull JSONO
6164

6265
public PdfViewDataReturnedEvent(@IdRes int viewId, int requestId, @NonNull Throwable throwable) {
6366
super(viewId);
64-
67+
this.requestId = requestId;
6568
payload = Arguments.createMap();
6669
payload.putInt("requestId", requestId);
6770
payload.putString("error", throwable.getMessage());
@@ -76,4 +79,9 @@ public String getEventName() {
7679
public void dispatch(RCTEventEmitter rctEventEmitter) {
7780
rctEventEmitter.receiveEvent(getViewTag(), getEventName(), payload);
7881
}
82+
83+
@Override
84+
public short getCoalescingKey() {
85+
return (short) requestId;
86+
}
7987
}

samples/Catalog/android/app/src/androidTest/java/com/pspdfkit/react/PdfViewTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.support.test.runner.AndroidJUnit4;
77

88
import com.pspdfkit.annotations.FreeTextAnnotation;
9+
import com.pspdfkit.forms.TextFormField;
910
import com.pspdfkit.preferences.PSPDFKitPreferences;
1011
import com.pspdfkit.react.helper.JsonUtilities;
1112
import com.pspdfkit.react.test.TestActivity;
@@ -27,6 +28,7 @@
2728
import static com.pspdfkit.react.utils.ViewActions.waitForViewNotDisplayed;
2829
import static junit.framework.Assert.assertEquals;
2930
import static junit.framework.Assert.assertFalse;
31+
import static junit.framework.Assert.assertNull;
3032

3133
@RunWith(AndroidJUnit4.class)
3234
public class PdfViewTest {
@@ -110,6 +112,50 @@ public void testGettingAnnotationsInComponentDidMount() throws InterruptedExcept
110112
assertEquals("{\"annotations\":[]}", annotations);
111113
}
112114

115+
@Test
116+
public void testSettingsFormValues() throws InterruptedException {
117+
// FormsScreen.js
118+
119+
openExample("Forms");
120+
121+
// Check with the form provider that the fields are empty.
122+
PdfFragment fragment = (PdfFragment) activityRule.getActivity().getSupportFragmentManager().findFragmentByTag("PDF1");
123+
TextFormField lastname = (TextFormField) fragment.getDocument().getFormProvider().getFormFieldWithFullyQualifiedName("Name_Last");
124+
TextFormField firstname = (TextFormField)fragment.getDocument().getFormProvider().getFormFieldWithFullyQualifiedName("Name_First");
125+
assertNull(lastname.getFormElement().getText());
126+
assertNull(firstname.getFormElement().getText());
127+
128+
// Perform setting the form values.
129+
onView(withText("SET")).perform(click());
130+
131+
assertEquals("Appleseed", lastname.getFormElement().getText());
132+
assertEquals("John", firstname.getFormElement().getText());
133+
}
134+
135+
@Test
136+
public void testGettingFormValues() throws InterruptedException {
137+
// FormsScreen.js
138+
139+
openExample("Forms");
140+
141+
// Check with the form provider that the fields are empty.
142+
PdfFragment fragment = (PdfFragment) activityRule.getActivity().getSupportFragmentManager().findFragmentByTag("PDF1");
143+
TextFormField lastname = (TextFormField) fragment.getDocument().getFormProvider().getFormFieldWithFullyQualifiedName("Name_Last");
144+
TextFormField firstname = (TextFormField)fragment.getDocument().getFormProvider().getFormFieldWithFullyQualifiedName("Name_First");
145+
assertNull(lastname.getFormElement().getText());
146+
assertNull(firstname.getFormElement().getText());
147+
148+
// Set the form fields.
149+
lastname.getFormElement().setText("Appleseed");
150+
firstname.getFormElement().setText("John");
151+
152+
// Get the form values in our react code.
153+
onView(withText("GET")).perform(click());
154+
155+
assertEquals("{\"value\":\"Appleseed\"}", TestingModule.getValue("lastName"));
156+
assertEquals("{\"value\":\"John\"}", TestingModule.getValue("firstName"));
157+
}
158+
113159
private void openExample(String exampleName) throws InterruptedException {
114160
// Wait until react is loaded.
115161
onView(isRoot()).perform(waitForView(withText("Test Cases")));
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import React, { Component } from "react";
2+
3+
import {
4+
View,
5+
Button,
6+
NativeModules
7+
} from "react-native"
8+
9+
import PSPDFKitView from "react-native-pspdfkit";
10+
11+
export default class FormsScreen extends Component<{}> {
12+
static navigationOptions = ({ navigation }) => {
13+
return {
14+
title: "PDF"
15+
};
16+
};
17+
18+
componentDidMount() {
19+
NativeModules.TestingModule.setValue("did_load", "true");
20+
}
21+
22+
render() {
23+
return (
24+
<View style={{ flex: 1 }}>
25+
<PSPDFKitView
26+
ref="pdfView"
27+
document="file:///android_asset/Form_example.pdf"
28+
configuration={{
29+
}}
30+
fragmentTag="PDF1"
31+
annotationAuthorName="Author"
32+
style={{ flex: 1 }}
33+
/>
34+
<View style={{ flexDirection: 'row', height: 40, alignItems: 'center', padding: 10 }}>
35+
<Button onPress={() => {
36+
this.refs.pdfView.getFormFieldValue("Name_Last").then(value => {
37+
NativeModules.TestingModule.setValue("lastName", JSON.stringify(value));
38+
})
39+
this.refs.pdfView.getFormFieldValue("Name_First").then(value => {
40+
NativeModules.TestingModule.setValue("firstName", JSON.stringify(value));
41+
})
42+
}} title="Get" />
43+
<Button onPress={() => {
44+
this.refs.pdfView.setFormFieldValue("Name_Last", "Appleseed");
45+
this.refs.pdfView.setFormFieldValue("Name_First", "John");
46+
}} title="Set" />
47+
</View>
48+
</View>
49+
);
50+
}
51+
}

samples/Catalog/testing/Testing.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import styles from './styles'
1919
import AuthorNameScreen from './AuthorNameScreen'
2020
import AnnotationToolbarScreen from './AnnotationToolbarScreen'
2121
import GetAnnotationsScreen from './GetAnnotationsScreen'
22+
import FormsScreen from './FormsScreen'
2223

2324
import PSPDFKitView from "react-native-pspdfkit";
2425

@@ -38,6 +39,12 @@ var examples = [
3839
action: component => {
3940
component.props.navigation.navigate("GetAnnotations");
4041
}
42+
},
43+
{
44+
name: "Forms",
45+
action: component => {
46+
component.props.navigation.navigate("Forms");
47+
}
4148
}
4249
]
4350

@@ -105,6 +112,9 @@ export default StackNavigator(
105112
},
106113
GetAnnotations: {
107114
screen: GetAnnotationsScreen
115+
},
116+
Forms: {
117+
screen: FormsScreen
108118
}
109119
},
110120
{

0 commit comments

Comments
 (0)