1717package androidx.pdf
1818
1919import android.content.Context
20+ import android.net.Uri
2021import android.os.Build
2122import androidx.pdf.service.connect.FakePdfServiceConnection
2223import androidx.pdf.utils.TestUtils
@@ -35,7 +36,7 @@ import org.junit.runner.RunWith
3536@RunWith(AndroidJUnit4 ::class )
3637class SandboxedPdfLoaderTest {
3738 @Test
38- fun openDocument_notConnected_connectsAndLoadsDocument () = runTest {
39+ fun openDocumentUri_notConnected_connectsAndLoadsDocument () = runTest {
3940 var isServiceConnected = false
4041 val context = ApplicationProvider .getApplicationContext<Context >()
4142 val loader = SandboxedPdfLoader (context, Dispatchers .Main )
@@ -53,8 +54,27 @@ class SandboxedPdfLoaderTest {
5354 document.close()
5455 }
5556
57+ @Test
58+ fun openDocumentFd_notConnected_connectsAndLoadsDocument () = runTest {
59+ var isServiceConnected = false
60+ val context = ApplicationProvider .getApplicationContext<Context >()
61+ val loader = SandboxedPdfLoader (context, Dispatchers .Main )
62+ loader.testingConnection =
63+ FakePdfServiceConnection (context, isConnected = false ) { isServiceConnected = true }
64+ val pfd = TestUtils .openFileDescriptor(context, PDF_DOCUMENT )
65+
66+ val document = loader.openDocument(FAKE_URI_1 , pfd)
67+
68+ val expectedPageCount = 3
69+ assertThat(isServiceConnected).isTrue()
70+ assertThat(document.uri == FAKE_URI_1 ).isTrue()
71+ assertThat(document.pageCount == expectedPageCount).isTrue()
72+ assertThat(! document.isLinearized).isTrue()
73+ document.close()
74+ }
75+
5676 @Test(expected = IllegalStateException ::class )
57- fun openDocument_connectedAndNullBinder_throwsIllegalStateException () {
77+ fun openDocumentUri_connectedAndNullBinder_throwsIllegalStateException () {
5878 val context = ApplicationProvider .getApplicationContext<Context >()
5979 val loader = SandboxedPdfLoader (context, Dispatchers .Main )
6080 loader.testingConnection = FakePdfServiceConnection (context, isConnected = true )
@@ -63,18 +83,47 @@ class SandboxedPdfLoaderTest {
6383 runTest { loader.openDocument(uri) }
6484 }
6585
86+ @Test(expected = IllegalStateException ::class )
87+ fun openDocumentFd_connectedAndNullBinder_throwsIllegalStateException () {
88+ val context = ApplicationProvider .getApplicationContext<Context >()
89+ val loader = SandboxedPdfLoader (context, Dispatchers .Main )
90+ loader.testingConnection = FakePdfServiceConnection (context, isConnected = true )
91+
92+ val pfd = TestUtils .openFileDescriptor(context, PDF_DOCUMENT )
93+ runTest { loader.openDocument(FAKE_URI_1 , pfd) }
94+ }
95+
6696 @Test(expected = PdfPasswordException ::class )
67- fun openDocument_passwordProtected_throwsPdfPasswordException () {
97+ fun openDocumentUri_passwordProtected_throwsPdfPasswordException () {
6898 val context = ApplicationProvider .getApplicationContext<Context >()
6999 val loader = SandboxedPdfLoader (context, Dispatchers .Main )
70100
71101 val uri = TestUtils .openFile(context, PASSWORD_PROTECTED_DOCUMENT )
72-
73102 runTest { loader.openDocument(uri) }
74103 }
75104
105+ @Test(expected = PdfPasswordException ::class )
106+ fun openDocumentFd_passwordProtected_throwsPdfPasswordException () {
107+ val context = ApplicationProvider .getApplicationContext<Context >()
108+ val loader = SandboxedPdfLoader (context, Dispatchers .Main )
109+
110+ val pfd = TestUtils .openFileDescriptor(context, PASSWORD_PROTECTED_DOCUMENT )
111+
112+ runTest { loader.openDocument(FAKE_URI_1 , pfd) }
113+ }
114+
115+ @Test(expected = IllegalStateException ::class )
116+ fun openDocumentUri_corruptedDocument_throwsIllegalStateException () {
117+ val context = ApplicationProvider .getApplicationContext<Context >()
118+ val loader = SandboxedPdfLoader (context, Dispatchers .Main )
119+
120+ val pfd = TestUtils .openFileDescriptor(context, CORRUPTED_DOCUMENT )
121+
122+ runTest { loader.openDocument(FAKE_URI_1 , pfd) }
123+ }
124+
76125 @Test(expected = IllegalStateException ::class )
77- fun openDocument_corruptedDocument_throwsIllegalStateException () {
126+ fun openDocumentFd_corruptedDocument_throwsIllegalStateException () {
78127 val context = ApplicationProvider .getApplicationContext<Context >()
79128 val loader = SandboxedPdfLoader (context, Dispatchers .Main )
80129
@@ -88,7 +137,7 @@ class SandboxedPdfLoaderTest {
88137 * document's internal state. See b/380140417
89138 */
90139 @Test
91- fun openTwoDocuments_sharedLoader () = runTest {
140+ fun openTwoDocumentsUri_sharedLoader () = runTest {
92141 val context = ApplicationProvider .getApplicationContext<Context >()
93142 val uri1 = TestUtils .openFile(context, " sample.pdf" )
94143 val uri2 = TestUtils .openFile(context, " alt_text.pdf" )
@@ -111,9 +160,38 @@ class SandboxedPdfLoaderTest {
111160 assertThat(document1.getPageInfo(2 ).width).isEqualTo(doc1Page3Info.width)
112161 }
113162
163+ @Test
164+ fun openTwoDocumentsFd_sharedLoader () = runTest {
165+ val context = ApplicationProvider .getApplicationContext<Context >()
166+ val pfd1 = TestUtils .openFileDescriptor(context, " sample.pdf" )
167+ val pfd2 = TestUtils .openFileDescriptor(context, " alt_text.pdf" )
168+ val sharedLoader = SandboxedPdfLoader (context, Dispatchers .Main )
169+
170+ // Grab some data from document1
171+ val document1 = sharedLoader.openDocument(FAKE_URI_1 , pfd1)
172+ assertThat(document1.pageCount).isEqualTo(3 )
173+ val doc1Page3Info = document1.getPageInfo(2 )
174+ val doc1Page1Text = document1.getPageContent(0 )?.textContents?.get(0 )?.text
175+
176+ // Load document2, make a basic assertion to verify it is indeed a different PDF document
177+ val document2 = sharedLoader.openDocument(FAKE_URI_2 , pfd2)
178+ assertThat(document2.pageCount).isEqualTo(1 )
179+
180+ // Make sure we receive the same data from document1 as before, i.e. that loading document2
181+ // did not in any way corrupt document1
182+ assertThat(document1.getPageContent(0 )?.textContents?.get(0 )?.text).isEqualTo(doc1Page1Text)
183+ assertThat(document1.getPageInfo(2 ).height).isEqualTo(doc1Page3Info.height)
184+ assertThat(document1.getPageInfo(2 ).width).isEqualTo(doc1Page3Info.width)
185+ }
186+
114187 companion object {
115188 private const val PDF_DOCUMENT = " sample.pdf"
116189 private const val PASSWORD_PROTECTED_DOCUMENT = " sample-protected.pdf"
117190 private const val CORRUPTED_DOCUMENT = " corrupted.pdf"
191+
192+ // We deliberately use fake URIs in the file descriptor versions of these tests to validate
193+ // the file descriptor API's behavior of using the URI only as a unique identifier.
194+ private val FAKE_URI_1 = Uri .parse(" content://who.cares/not_a.pdf" )
195+ private val FAKE_URI_2 = Uri .parse(" http://this_is.not/even_a_scheme_we_support.html" )
118196 }
119197}
0 commit comments