@@ -3,7 +3,15 @@ package com.rajat.pdfviewer
33import android.content.Context
44import androidx.test.core.app.ActivityScenario
55import androidx.test.core.app.ApplicationProvider
6+ import androidx.test.platform.app.InstrumentationRegistry
7+ import com.rajat.pdfviewer.util.CacheManager
8+ import com.rajat.pdfviewer.util.CacheStrategy
69import com.rajat.pdfviewer.util.saveTo
10+ import okhttp3.mockwebserver.Dispatcher
11+ import okhttp3.mockwebserver.MockResponse
12+ import okhttp3.mockwebserver.MockWebServer
13+ import okhttp3.mockwebserver.RecordedRequest
14+ import okio.Buffer
715import java.io.File
816
917abstract class BasePdfViewerTest {
@@ -23,32 +31,84 @@ abstract class BasePdfViewerTest {
2331 protected fun launchPdfFromUrl (
2432 url : String = sampleUrl,
2533 title : String = "Remote PDF ",
26- enableDownload : Boolean = true
34+ enableDownload : Boolean = true,
35+ cacheStrategy : CacheStrategy = CacheStrategy .MAXIMIZE_PERFORMANCE
2736 ): ActivityScenario <PdfViewerActivity > {
2837 val intent = PdfViewerActivity .launchPdfFromUrl(
2938 context = context,
3039 pdfUrl = url,
3140 pdfTitle = title,
3241 saveTo = saveTo.DOWNLOADS ,
33- enableDownload = enableDownload
42+ enableDownload = enableDownload,
43+ cacheStrategy = cacheStrategy
3444 )
3545 return ActivityScenario .launch(intent)
3646 }
3747
3848 protected fun launchPdfFromAssets (
3949 assetName : String = samplePdf,
4050 title : String = "PDF From Asset ",
41- enableZoom : Boolean = true
51+ enableZoom : Boolean = true,
52+ cacheStrategy : CacheStrategy = CacheStrategy .MAXIMIZE_PERFORMANCE
4253 ): ActivityScenario <PdfViewerActivity > {
4354 val file = copyAssetPdfToCache(assetName)
55+ return launchPdfFromFile(
56+ file = file,
57+ title = title,
58+ enableZoom = enableZoom,
59+ cacheStrategy = cacheStrategy
60+ )
61+ }
62+
63+ protected fun launchPdfFromFile (
64+ file : File ,
65+ title : String = "PDF From File ",
66+ enableZoom : Boolean = true,
67+ cacheStrategy : CacheStrategy = CacheStrategy .MAXIMIZE_PERFORMANCE
68+ ): ActivityScenario <PdfViewerActivity > {
4469 val intent = PdfViewerActivity .launchPdfFromPath(
4570 context = context,
4671 path = file.absolutePath,
4772 pdfTitle = title,
4873 saveTo = saveTo.DOWNLOADS ,
4974 fromAssets = false ,
50- enableZoom = enableZoom
75+ enableZoom = enableZoom,
76+ cacheStrategy = cacheStrategy
5177 )
5278 return ActivityScenario .launch(intent)
5379 }
80+
81+ protected fun clearPdfCacheRoot () {
82+ File (context.cacheDir, CacheManager .CACHE_PATH ).deleteRecursively()
83+ }
84+
85+ // Hermetic remote-cache tests use a local HTTP server so they don't depend on public hosts.
86+ protected fun startPdfServer (assetName : String = samplePdf): MockWebServer {
87+ val pdfBytes = context.assets.open(assetName).use { it.readBytes() }
88+ return MockWebServer ().apply {
89+ dispatcher = object : Dispatcher () {
90+ override fun dispatch (request : RecordedRequest ): MockResponse {
91+ return MockResponse ()
92+ .setResponseCode(200 )
93+ .setHeader(" Content-Type" , " application/pdf" )
94+ .setBody(Buffer ().write(pdfBytes))
95+ }
96+ }
97+ start()
98+ }
99+ }
100+
101+ protected fun waitForTestCondition (
102+ timeoutMs : Long = 10_000,
103+ intervalMs : Long = 100,
104+ condition : () -> Boolean
105+ ) {
106+ val deadline = System .currentTimeMillis() + timeoutMs
107+ while (System .currentTimeMillis() < deadline) {
108+ InstrumentationRegistry .getInstrumentation().waitForIdleSync()
109+ if (condition()) return
110+ Thread .sleep(intervalMs)
111+ }
112+ check(condition()) { " Condition not met within ${timeoutMs} ms" }
113+ }
54114}
0 commit comments