|
| 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