Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
max-kammerer committed Oct 15, 2023
1 parent ac65b1a commit 4353828
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 70 deletions.
5 changes: 5 additions & 0 deletions orion-viewer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ project.ext {
dependencies {

implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.preference:preference-ktx:1.2.1'
//tab layout
implementation 'com.google.android.material:material:1.9.0'
implementation project(':tree-view-list-android')
Expand All @@ -28,7 +29,11 @@ dependencies {
implementation project(":nativeLibs:djvuModule")
implementation project(":nativeLibs:mupdfModule")

androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation 'androidx.test:core:1.5.0'
androidTestImplementation 'androidx.test:core-ktx:1.5.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.5'
androidTestImplementation 'androidx.test:runner:1.5.2'
androidTestImplementation 'androidx.test:rules:1.5.0'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import universe.constellation.orion.viewer.prefs.initalizer
import universe.constellation.orion.viewer.test.framework.BookDescription
import universe.constellation.orion.viewer.test.framework.InstrumentationTestCase
import universe.constellation.orion.viewer.test.framework.SingleThreadRenderer
import universe.constellation.orion.viewer.test.framework.openTestBook
import universe.constellation.orion.viewer.view.Scene
import java.util.concurrent.CountDownLatch

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package universe.constellation.orion.viewer.test

import android.content.Intent
import android.content.pm.ActivityInfo
import android.net.Uri
import org.junit.Assert
import org.junit.Ignore
import org.junit.Test
import universe.constellation.orion.viewer.test.framework.BookDescription
import universe.constellation.orion.viewer.test.framework.InstrumentationTestCase
import universe.constellation.orion.viewer.test.framework.TestUtil
import java.util.concurrent.CountDownLatch

class RotationTest : InstrumentationTestCase() {

@Test
@Ignore
fun testRotation() {
val file = extractFileFromTestData(TestUtil.SICP)
val intent = Intent()
intent.data = Uri.fromFile(file)
startActivityWithBook(intent)
startActivityWithBook(BookDescription.SICP.toOpenIntent())

val view = activity.view
val width = view.sceneWidth
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package universe.constellation.orion.viewer.test.espresso

import android.content.Intent
import androidx.test.espresso.Espresso.*
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.assertion.ViewAssertions.doesNotExist
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.hamcrest.Matchers.*
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import universe.constellation.orion.viewer.OrionViewerActivity
import universe.constellation.orion.viewer.R
import universe.constellation.orion.viewer.prefs.GlobalOptions
import universe.constellation.orion.viewer.prefs.OrionApplication
import universe.constellation.orion.viewer.test.framework.BookDescription
import universe.constellation.orion.viewer.test.framework.InstrumentationTestCase

abstract class CheckTapZoneWelcomeScreen(private val showTapHelp: Boolean, intent: Intent) : InstrumentationTestCase() {

@get:Rule
var activityScenarioRule = activityScenarioRule<OrionViewerActivity>(intent.apply {
if (showTapHelp) {
putExtra(GlobalOptions.SHOW_TAP_HELP, true)
} else {
putExtra(GlobalOptions.SHOW_TAP_HELP, false)
}
})

}
@RunWith(AndroidJUnit4::class)
class NoBookNoWelcome: CheckTapZoneWelcomeScreen(true, BookDescription.SICP.toOpenIntent().apply { data = null;}) {
@Test
fun testWelcome() {
val options =
(InstrumentationRegistry.getInstrumentation().targetContext.applicationContext as OrionApplication).options
onView(withId(R.id.tap_help_close)).check(doesNotExist())
assertTrue(options.isShowTapHelp)
}
}

@RunWith(AndroidJUnit4::class)
class BookWithWelcome: CheckTapZoneWelcomeScreen(true, BookDescription.SICP.toOpenIntent()) {
@Test
fun testWelcome() {
val options =
(InstrumentationRegistry.getInstrumentation().targetContext.applicationContext as OrionApplication).options
assertTrue(options.isShowTapHelp)
onView(withId(R.id.tap_help_close)).check(matches(isDisplayed()))
onView(withId(R.id.tap_help_close)).perform(ViewActions.click())
onView(withId(R.id.tap_help_close)).check(matches(isDisplayed()))
onView(withId(R.id.tap_help_close)).check(doesNotExist())
//
// assertFalse(options.isShowTapHelp)

}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
package universe.constellation.orion.viewer.test.framework

import android.content.Context
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.GrantPermissionRule
import org.junit.Rule

/**
* User: mike
* Date: 19.10.13
* Time: 13:45
*/
abstract class BaseTest : TestUtil {

@Rule
@JvmField
val mRuntimePermissionRule: GrantPermissionRule = GrantPermissionRule.grant(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE)

override fun getOrionTestContext(): Context {
return InstrumentationRegistry.getInstrumentation().targetContext
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package universe.constellation.orion.viewer.test.framework

import android.content.Intent
import android.graphics.Point
import android.net.Uri
import android.os.Bundle
import org.junit.After
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import universe.constellation.orion.viewer.BuildConfig
import universe.constellation.orion.viewer.OrionViewerActivity

@RunWith(Parameterized::class)
abstract class BookTest(path: String) : BaseTest() {

protected val document by lazy { openTestBook(path) }

@After
fun close() {
document.destroy()
Expand All @@ -26,5 +30,17 @@ enum class BookDescription(
) {
SICP(TestUtil.SICP, 762, "", 15, 139, Point(662, 885)),
ALICE(TestUtil.ALICE, 77, null, 0, pageSize = Point(2481, 3508)),
DJVU_SPEC(TestUtil.DJVU_SPEC, 71, null, 1, 100, Point(2539, 3295))
DJVU_SPEC(TestUtil.DJVU_SPEC, 71, null, 1, 100, Point(2539, 3295));

fun toOpenIntent(): Intent {
val path = extractFileFromTestData(path)
return Intent(Intent.ACTION_VIEW).apply {
setClassName(
BuildConfig.APPLICATION_ID,
OrionViewerActivity::class.qualifiedName!!
)
data = Uri.fromFile(path)
addCategory(Intent.CATEGORY_DEFAULT)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ abstract class InstrumentationTestCase : TestUtil {
val activity: OrionViewerActivity
get() = mActivityRule.activity

override fun getOrionTestContext(): Context = mActivityRule.activity.orionContext

fun getController() : Controller = mActivityRule.activity.controller!!

fun startActivityWithBook(intent: Intent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,34 @@ import java.io.File
import java.io.FileOutputStream
import java.io.IOException

/**
* User: mike
* Date: 20.10.13
* Time: 8:32
*/

interface TestUtil {

fun openTestBook(relativePath: String) : DocumentWithCaching {
val fileOnSdcard = extractFileFromTestData(relativePath)
return FileUtil.openFile(fileOnSdcard)
}

fun extractFileFromTestData(fileName: String): File {
val outFile = File(testFolder, fileName)
if (outFile.exists()) {
return outFile
}
try {
outFile.parentFile!!.mkdirs()
outFile.createNewFile()
} catch (e: IOException) {
throw RuntimeException("Couldn't create new file " + outFile.absolutePath, e)
}

val input = this.javaClass.classLoader!!.getResourceAsStream(getFileUnderTestData(fileName))
val bufferedOutputStream = FileOutputStream(outFile).buffered()
input.buffered().copyTo(bufferedOutputStream)
bufferedOutputStream.close()
fun openTestBook(relativePath: String) : DocumentWithCaching {
val fileOnSdcard = extractFileFromTestData(relativePath)
return FileUtil.openFile(fileOnSdcard)
}

fun extractFileFromTestData(fileName: String): File {
val outFile = File(TestUtil.testFolder, fileName)
if (outFile.exists()) {
return outFile
}
try {
outFile.parentFile!!.mkdirs()
outFile.createNewFile()
} catch (e: IOException) {
throw RuntimeException("Couldn't create new file " + outFile.absolutePath, e)
}

fun getFileUnderTestData(relativePath: String): String = "testData/$relativePath"
val input = ClassLoader.getSystemClassLoader().getResourceAsStream(getFileUnderTestData(fileName))
val bufferedOutputStream = FileOutputStream(outFile).buffered()
input.buffered().copyTo(bufferedOutputStream)
bufferedOutputStream.close()
return outFile
}

fun getOrionTestContext(): Context
fun getFileUnderTestData(relativePath: String): String = "testData/$relativePath"


interface TestUtil {
companion object {
val testFolder: File = File(Environment.getExternalStorageDirectory(), "orion")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package universe.constellation.orion.viewer

import android.annotation.SuppressLint
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.ActivityInfo
import android.content.pm.PackageManager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class OrionViewerActivity : OrionBaseActivity(viewerType = Device.VIEWER_ACTIVIT
return
}

processAdditionalOptionsInIntent(intent)
openFileWithGrantedPermissions(intent)
}

Expand Down Expand Up @@ -1071,9 +1072,9 @@ class OrionViewerActivity : OrionBaseActivity(viewerType = Device.VIEWER_ACTIVIT
}

fun showTapDialogIfNeeded() {
println("tap")
if (++tapHelpCounter < 2) return
if (globalOptions.isShowTapHelp && !orionContext.isTesting) {
globalOptions.saveBooleanProperty(GlobalOptions.SHOW_TAP_HELP, false)
TapHelpDialog(this).showDialog()
}
}
Expand All @@ -1093,6 +1094,13 @@ class OrionViewerActivity : OrionBaseActivity(viewerType = Device.VIEWER_ACTIVIT
}
}

private fun processAdditionalOptionsInIntent(intent: Intent) {
if (intent.hasExtra(GlobalOptions.SHOW_TAP_HELP)) {
val showTapHelp = intent.getBooleanExtra(GlobalOptions.SHOW_TAP_HELP, false)
globalOptions.saveBooleanProperty(GlobalOptions.SHOW_TAP_HELP, showTapHelp)
}
}

companion object {

val BOOK_MENU_ITEMS = setOf(R.id.search_menu_item,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import universe.constellation.orion.viewer.Action;
import universe.constellation.orion.viewer.OrionViewerActivity;
import universe.constellation.orion.viewer.R;
import universe.constellation.orion.viewer.prefs.GlobalOptions;
import universe.constellation.orion.viewer.prefs.OrionTapActivity;

/**
Expand Down Expand Up @@ -58,7 +59,10 @@ public TapHelpDialog(OrionViewerActivity activity) {
ImageView view = dialog.findViewById(R.id.tap_help_close);
view.setVisibility(View.VISIBLE);
view.setClickable(true);
view.setOnClickListener(v -> dialog.dismiss());
view.setOnClickListener(v -> {
dialog.dismiss();
activity.getGlobalOptions().saveBooleanProperty(GlobalOptions.SHOW_TAP_HELP, false);
});
}

public void showDialog() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,20 +325,6 @@ public boolean isEinkOptimization() {
return getBooleanProperty(EINK_OPTIMIZATION, false);
}

// public Integer getInteger(String key) {
// if (!prefValues.containsKey(key)) {
// String value = prefs.getString(key, null);
// Integer newIntValue = null;
// if (value == null || "".equals(value)) {
// return null;
// } else {
// newIntValue = Integer.valueOf(value);
// }
// prefValues.put(key, newIntValue);
// }
// return (Integer) prefValues.get(key);
// }

public int getIntFromStringProperty(String key, int defaultValue) {
if (!prefValues.containsKey(key)) {
String value = prefs.getString(key, null);
Expand Down

0 comments on commit 4353828

Please sign in to comment.