Skip to content

Commit dd5f1fb

Browse files
author
irgendeinich
committed
Add some simple tests
1 parent 8aa1114 commit dd5f1fb

File tree

11 files changed

+785
-4
lines changed

11 files changed

+785
-4
lines changed

samples/Catalog/android/app/build.gradle

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ import com.android.build.OutputFile
6666
*/
6767

6868
project.ext.react = [
69-
entryFile: "index.js"
69+
entryFile: "index.js",
70+
bundleInDebug: true
7071
]
7172

7273
apply from: "../../node_modules/react-native/react.gradle"
@@ -100,6 +101,7 @@ android {
100101
ndk {
101102
abiFilters "armeabi-v7a", "x86"
102103
}
104+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
103105
}
104106
splits {
105107
abi {
@@ -128,7 +130,7 @@ android {
128130
}
129131
}
130132
}
131-
sourceSets { main { assets.srcDirs = ['src/main/assets', '../../../PDFs' , '../../../Images'] } }
133+
sourceSets { main { assets.srcDirs = ['src/main/assets', '../../../PDFs', '../../../Images'] } }
132134
}
133135

134136
dependencies {
@@ -137,11 +139,27 @@ dependencies {
137139
// From node_modules
138140
compile project(':react-native-pspdfkit')
139141
compile project(':react-native-fs')
142+
143+
androidTestCompile('com.android.support.test:runner:1.0.2') {
144+
exclude group: 'com.android.support'
145+
}
146+
androidTestCompile('com.android.support.test:rules:1.0.2') {
147+
exclude group: 'com.android.support'
148+
}
149+
// Optional -- Hamcrest library
150+
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
151+
// Optional -- UI testing with Espresso
152+
androidTestCompile('com.android.support.test.espresso:espresso-core:3.0.2') {
153+
exclude group: 'com.android.support'
154+
exclude group: 'com.google.code.findbugs'
155+
}
156+
// Optional -- UI testing with UI Automator
157+
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
140158
}
141159

142160
// Run this once to be able to run the application with BUCK
143161
// puts all compile dependencies into folder libs for BUCK to use
144162
task copyDownloadableDepsToLibs(type: Copy) {
145-
from configurations.compile
146-
into 'libs'
163+
from configurations.compile
164+
into 'libs'
147165
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.pspdfkit.react;
2+
3+
import android.graphics.RectF;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.rule.ActivityTestRule;
6+
import android.support.test.runner.AndroidJUnit4;
7+
8+
import com.pspdfkit.annotations.FreeTextAnnotation;
9+
import com.pspdfkit.preferences.PSPDFKitPreferences;
10+
import com.pspdfkit.react.helper.JsonUtilities;
11+
import com.pspdfkit.react.test.TestActivity;
12+
import com.pspdfkit.ui.PdfFragment;
13+
14+
import org.json.JSONArray;
15+
import org.json.JSONException;
16+
import org.json.JSONObject;
17+
import org.junit.Before;
18+
import org.junit.Rule;
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
import static android.support.test.espresso.Espresso.onView;
23+
import static android.support.test.espresso.action.ViewActions.click;
24+
import static android.support.test.espresso.assertion.ViewAssertions.matches;
25+
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
26+
import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
27+
import static android.support.test.espresso.matcher.ViewMatchers.withId;
28+
import static android.support.test.espresso.matcher.ViewMatchers.withText;
29+
import static com.pspdfkit.react.utils.ViewActions.waitForView;
30+
import static com.pspdfkit.react.utils.ViewActions.waitForViewNotDisplayed;
31+
import static junit.framework.Assert.assertEquals;
32+
import static junit.framework.Assert.assertFalse;
33+
34+
@RunWith(AndroidJUnit4.class)
35+
public class PdfViewTest {
36+
@Rule
37+
public ActivityTestRule<TestActivity> activityRule = new ActivityTestRule<>(TestActivity.class);
38+
39+
@Before
40+
public void clearAnnotationCreator() {
41+
PSPDFKitPreferences.get(InstrumentationRegistry.getTargetContext()).resetAnnotationCreator();
42+
TestingModule.resetValues();
43+
}
44+
45+
@Test
46+
public void testAuthorNameProp() {
47+
// AuthorNameScreen.js
48+
49+
// Pre Condition: No annotation creator is set.
50+
assertFalse(PSPDFKitPreferences.get(activityRule.getActivity()).isAnnotationCreatorSet());
51+
52+
// Wait until react is loaded.
53+
onView(isRoot()).perform(waitForView(withText("Test Cases")));
54+
55+
// Open the screen containing the logic we want to test.
56+
onView(withText("AuthorName")).perform(click());
57+
58+
// Check that annotation creator is set.
59+
assertEquals("Author", PSPDFKitPreferences.get(activityRule.getActivity()).getAnnotationCreator(null));
60+
}
61+
62+
@Test
63+
public void testEnterAndExitAnnotationCreation() {
64+
// AnnotationToolbarScreen.js
65+
66+
// Wait until react is loaded.
67+
onView(isRoot()).perform(waitForView(withText("Test Cases")));
68+
69+
// Open the screen containing the logic we want to test.
70+
onView(withText("AnnotationToolbar")).perform(click());
71+
72+
// Open toolbar and check that it is displayed,
73+
onView(withText("OPEN")).perform(click());
74+
onView(withId(R.id.pspdf__annotation_creation_toolbar)).check(matches(isDisplayed()));
75+
76+
// Now close it again.
77+
onView(withText("CLOSE")).perform(click());
78+
onView(isRoot()).perform(waitForViewNotDisplayed(withId(R.id.pspdf__annotation_creation_toolbar)));
79+
}
80+
81+
@Test
82+
public void testGetEmptyAnnotations() throws InterruptedException {
83+
// GetAnnotationsScreen.js
84+
85+
// Wait until react is loaded.
86+
onView(isRoot()).perform(waitForView(withText("Test Cases")));
87+
88+
// Open the screen containing the logic we want to test.
89+
onView(withText("GetAnnotations")).perform(click());
90+
91+
// Get annotations for first page should return nothing.
92+
onView(withText("GET")).perform(click());
93+
String annotations = TestingModule.getValue("annotations");
94+
95+
assertEquals("[]", annotations);
96+
}
97+
98+
@Test
99+
public void testGetAnnotation() throws InterruptedException, JSONException {
100+
// GetAnnotationsScreen.js
101+
102+
// Wait until react is loaded.
103+
onView(isRoot()).perform(waitForView(withText("Test Cases")));
104+
105+
// Open the screen containing the logic we want to test.
106+
onView(withText("GetAnnotations")).perform(click());
107+
108+
PdfFragment fragment = (PdfFragment) activityRule.getActivity().getSupportFragmentManager().findFragmentByTag("PDF1");
109+
FreeTextAnnotation annotation = new FreeTextAnnotation(0, new RectF(0, 0, 100, 100), "Test");
110+
fragment.getDocument().getAnnotationProvider().addAnnotationToPage(annotation);
111+
112+
// Get annotations for first page should return nothing.
113+
onView(withText("GET")).perform(click());
114+
JSONArray annotations = new JSONArray(TestingModule.getValue("annotations"));
115+
116+
117+
JSONObject originalInstantJson = new JSONObject(annotation.toInstantJson());
118+
assertEquals(JsonUtilities.jsonObjectToMap(originalInstantJson), JsonUtilities.jsonObjectToMap(annotations.getJSONObject(0)));
119+
}
120+
121+
}

0 commit comments

Comments
 (0)