|
18 | 18 | package org.kiwix.kiwixmobile.search |
19 | 19 |
|
20 | 20 | import androidx.core.content.edit |
| 21 | +import androidx.core.net.toUri |
21 | 22 | import androidx.preference.PreferenceManager |
22 | 23 | import androidx.test.core.app.ActivityScenario |
23 | 24 | import androidx.test.internal.runner.junit4.statement.UiThreadStatement |
24 | 25 | import androidx.test.platform.app.InstrumentationRegistry |
25 | 26 | import androidx.test.uiautomator.UiDevice |
26 | | -import com.adevinta.android.barista.interaction.BaristaSleepInteractions |
27 | 27 | import leakcanary.LeakAssertions |
28 | 28 | import org.junit.After |
29 | | -import org.junit.Assert |
30 | 29 | import org.junit.Before |
31 | 30 | import org.junit.Rule |
32 | 31 | import org.junit.Test |
33 | 32 | import org.kiwix.kiwixmobile.BaseActivityTest |
34 | 33 | import org.kiwix.kiwixmobile.R |
35 | 34 | import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil |
36 | | -import org.kiwix.kiwixmobile.download.downloadRobot |
37 | 35 | import org.kiwix.kiwixmobile.main.KiwixMainActivity |
| 36 | +import org.kiwix.kiwixmobile.nav.destination.library.LocalLibraryFragmentDirections.actionNavigationLibraryToNavigationReader |
38 | 37 | import org.kiwix.kiwixmobile.testutils.RetryRule |
39 | | -import org.kiwix.kiwixmobile.testutils.TestUtils |
40 | 38 | import org.kiwix.kiwixmobile.testutils.TestUtils.closeSystemDialogs |
41 | 39 | import org.kiwix.kiwixmobile.testutils.TestUtils.isSystemUINotRespondingDialogVisible |
| 40 | +import java.io.File |
| 41 | +import java.io.FileOutputStream |
| 42 | +import java.io.OutputStream |
42 | 43 |
|
43 | 44 | class SearchFragmentTest : BaseActivityTest() { |
44 | 45 |
|
@@ -67,29 +68,40 @@ class SearchFragmentTest : BaseActivityTest() { |
67 | 68 | fun searchFragmentSimple() { |
68 | 69 | ActivityScenario.launch(KiwixMainActivity::class.java).onActivity { |
69 | 70 | kiwixMainActivity = it |
| 71 | + kiwixMainActivity.navigate(R.id.libraryFragment) |
70 | 72 | } |
71 | | - BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong()) |
72 | | - try { |
73 | | - downloadRobot { |
74 | | - clickLibraryOnBottomNav() |
75 | | - deleteZimIfExists(false) |
76 | | - clickDownloadOnBottomNav() |
77 | | - waitForDataToLoad() |
78 | | - downloadZimFile() |
79 | | - assertDownloadStart() |
80 | | - waitUntilDownloadComplete() |
81 | | - clickLibraryOnBottomNav() |
82 | | - checkIfZimFileDownloaded() |
83 | | - downloadZimFile() |
| 73 | + val loadFileStream = |
| 74 | + SearchFragmentTest::class.java.classLoader.getResourceAsStream("testzim.zim") |
| 75 | + val zimFile = File(context.cacheDir, "testzim.zim") |
| 76 | + if (zimFile.exists()) zimFile.delete() |
| 77 | + zimFile.createNewFile() |
| 78 | + loadFileStream.use { inputStream -> |
| 79 | + val outputStream: OutputStream = FileOutputStream(zimFile) |
| 80 | + outputStream.use { it -> |
| 81 | + val buffer = ByteArray(inputStream.available()) |
| 82 | + var length: Int |
| 83 | + while (inputStream.read(buffer).also { length = it } > 0) { |
| 84 | + it.write(buffer, 0, length) |
| 85 | + } |
84 | 86 | } |
85 | | - } catch (e: Exception) { |
86 | | - Assert.fail( |
87 | | - "Couldn't find downloaded file ' Off the Grid ' Original Exception: ${e.message}" |
| 87 | + } |
| 88 | + UiThreadStatement.runOnUiThread { |
| 89 | + kiwixMainActivity.navigate( |
| 90 | + actionNavigationLibraryToNavigationReader() |
| 91 | + .apply { zimFileUri = zimFile.toUri().toString() } |
88 | 92 | ) |
89 | 93 | } |
90 | 94 | search { checkZimFileSearchSuccessful(R.id.readerFragment) } |
91 | 95 | UiThreadStatement.runOnUiThread { |
92 | | - kiwixMainActivity.openSearch(searchString = "100R") |
| 96 | + if (zimFile.canRead()) { |
| 97 | + kiwixMainActivity.openSearch(searchString = "Android") |
| 98 | + } else { |
| 99 | + throw RuntimeException( |
| 100 | + "File $zimFile is not readable." + |
| 101 | + " Original File $zimFile is readable = ${zimFile.canRead()}" + |
| 102 | + " Size ${zimFile.length()}" |
| 103 | + ) |
| 104 | + } |
93 | 105 | } |
94 | 106 | search { |
95 | 107 | clickOnSearchItemInSearchList() |
|
0 commit comments