Skip to content

Commit

Permalink
Refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
max-kammerer committed Oct 15, 2023
1 parent 23ab3f7 commit c44d856
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@ package universe.constellation.orion.viewer.test

import android.graphics.Bitmap
import android.graphics.Point
import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import universe.constellation.orion.viewer.*
import universe.constellation.orion.viewer.document.DocumentWithCaching
import universe.constellation.orion.viewer.layout.LayoutPosition
import universe.constellation.orion.viewer.layout.LayoutStrategy
import universe.constellation.orion.viewer.layout.SimpleLayoutStrategy
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
import java.util.concurrent.atomic.AtomicReference

@RunWith(Parameterized::class)
class RenderingAndNavigationTest(path: String) : InstrumentationTestCase() {
class RenderingAndNavigationTest(book: BookDescription) : InstrumentationTestCase(book.toOpenIntent()) {

companion object {
@JvmStatic
@Parameterized.Parameters(name = "Test simple navigation in {0}")
fun testData(): Iterable<Array<Any>> {
return BookDescription.values().map { arrayOf<Any>(it.path) }
fun testData(): Iterable<Array<BookDescription>> {
return BookDescription.values().map { arrayOf(it) }
}
}

Expand All @@ -45,23 +45,17 @@ class RenderingAndNavigationTest(path: String) : InstrumentationTestCase() {
}
}

private val document by lazy {
openTestBook(path)
}

private val deviceSize = Point(300, 350) //to split page on two screen - page size is 663x886

private lateinit var view: MyView

@Before
fun setUp() {
mActivityRule.launchActivity(null)
view = MyView(activity.view)
}

@After
fun destroy() {
document.destroy()
val ref = AtomicReference<OrionImageListener>()
activityScenarioRule.scenario.onActivity {
ref.set(it.view)
}
view = MyView(ref.get())
}

@Test
Expand Down Expand Up @@ -113,18 +107,22 @@ class RenderingAndNavigationTest(path: String) : InstrumentationTestCase() {
}

private fun prepareEngine(): Controller {

val layoutStrategy: LayoutStrategy = SimpleLayoutStrategy.create(document)
val renderer = SingleThreadRenderer(activity, view, layoutStrategy, document)
val controller = Controller(activity, document, layoutStrategy, renderer)
lateinit var controller: Controller
activityScenarioRule.scenario.onActivity { activity ->
val document = activity.controller!!.document as DocumentWithCaching
val layoutStrategy: LayoutStrategy = SimpleLayoutStrategy.create(document)
val renderer = SingleThreadRenderer(activity, view, layoutStrategy, document)
controller = Controller(activity, document, layoutStrategy, renderer)


val lastPageInfo = LastPageInfo.loadBookParameters(activity, "123", initalizer(activity.globalOptions))
controller.changeOrinatation(lastPageInfo.screenOrientation)
controller.init(lastPageInfo, deviceSize)
val lastPageInfo =
LastPageInfo.loadBookParameters(activity, "123", initalizer(activity.globalOptions))
controller.changeOrinatation(lastPageInfo.screenOrientation)
controller.init(lastPageInfo, deviceSize)

//getSubscriptionManager()?.sendDocOpenedNotification(controller)
activity.view.setDimensionAware(controller)
//getSubscriptionManager()?.sendDocOpenedNotification(controller)
activity.view.setDimensionAware(controller)
}
return controller
}
}
Original file line number Diff line number Diff line change
@@ -1,45 +1,56 @@
package universe.constellation.orion.viewer.test

import android.content.pm.ActivityInfo
import androidx.test.filters.SdkSuppress
import org.junit.Assert
import org.junit.Ignore
import org.junit.Test
import universe.constellation.orion.viewer.OrionScene
import universe.constellation.orion.viewer.test.framework.BookDescription
import universe.constellation.orion.viewer.test.framework.InstrumentationTestCase

class RotationTest : InstrumentationTestCase() {
class RotationTest : InstrumentationTestCase(BookDescription.SICP.toOpenIntent()) {

@Test
@Ignore
@SdkSuppress(minSdkVersion = 21)
fun testRotation() {
startActivityWithBook(BookDescription.SICP.toOpenIntent())
lateinit var view: OrionScene
activityScenarioRule.scenario.onActivity {
view = it.view
}

val view = activity.view
val width = view.sceneWidth
val height = view.sceneHeight

Assert.assertTrue(width != 0)
Assert.assertTrue(height != 0)
val orientation = activity.resources!!.configuration.orientation

mActivityRule.runOnUiThread {
getController().changeOrinatation(if (orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) "PORTRAIT" else "LANDSCAPE")
var orientation: Int = Int.MIN_VALUE
activityScenarioRule.scenario.onActivity {
orientation = it.resources!!.configuration.orientation
it.controller!!.changeOrinatation(if (orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) "PORTRAIT" else "LANDSCAPE")
}

Thread.sleep(1000)

try {
Assert.assertTrue("Orientation not changed: $orientation", orientation != activity.resources!!.configuration.orientation)
var newOrientation: Int = Int.MAX_VALUE
activityScenarioRule.scenario.onActivity {
newOrientation = it.resources!!.configuration.orientation
}

Assert.assertNotEquals("Orientation not changed: $orientation", orientation, newOrientation)
val width2 = view.sceneWidth
val height2 = view.sceneHeight
Assert.assertTrue("w1: $width, w2: $width2, original orientation: $orientation", width != width2)
Assert.assertTrue("h1: $height, h2: $$height2, original orientation: $orientation", height != height2)
} finally {
mActivityRule.runOnUiThread {
getController().changeOrinatation("PORTRAIT")
activityScenarioRule.scenario.onActivity {
it.controller!!.changeOrinatation("PORTRAIT")
}
Thread.sleep(1000)
mActivityRule.finishActivity()
activityScenarioRule.scenario.onActivity {
it.finishActivity(0)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package universe.constellation.orion.viewer.test.espresso

import android.content.Intent
import android.graphics.Rect
import android.view.View
import androidx.test.espresso.Espresso.*
Expand All @@ -9,61 +8,42 @@ 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 androidx.test.filters.SdkSuppress
import org.hamcrest.Matchers.*
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
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
import java.util.concurrent.atomic.AtomicReference


abstract class CheckTapZoneStartScreen(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 NoBookNoStartTapScreen: CheckTapZoneStartScreen(true, BookDescription.SICP.toOpenIntent().apply { data = null;}) {
@SdkSuppress(minSdkVersion = 21)
class NoBookNoStartTapScreen: InstrumentationTestCase(BookDescription.SICP.toOpenIntent().apply { data = null;}, true) {
@Test
fun testStartScreenAbsent() {
val options =
(InstrumentationRegistry.getInstrumentation().targetContext.applicationContext as OrionApplication).options
onView(withId(R.id.tap_help_close)).check(doesNotExist())
assertTrue(options.isShowTapHelp)
assertTrue(globalOptions.isShowTapHelp)
}
}

@RunWith(AndroidJUnit4::class)
class BookWithStartTapScreen: CheckTapZoneStartScreen(true, BookDescription.SICP.toOpenIntent()) {
@SdkSuppress(minSdkVersion = 21)
class BookWithStartTapScreen: InstrumentationTestCase(BookDescription.SICP.toOpenIntent(), true) {
@Test
fun testStartScreen() {
val options =
(InstrumentationRegistry.getInstrumentation().targetContext.applicationContext as OrionApplication).options
assertTrue(options.isShowTapHelp)
assertTrue(globalOptions.isShowTapHelp)
onView(withId(R.id.tap_help_close)).check(matches(isDisplayed()))
checkSizeAndPosition()

onView(withId(R.id.tap_help_close)).perform(click())
//onView(withId(R.id.tap_help_close)).check(doesNotExist())//TODO
assertFalse(options.isShowTapHelp)
assertFalse(globalOptions.isShowTapHelp)
}

private fun checkSizeAndPosition() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package universe.constellation.orion.viewer.test.framework

import android.content.Context
import android.content.Intent
import androidx.test.rule.ActivityTestRule
import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.GrantPermissionRule
import org.junit.Rule
import universe.constellation.orion.viewer.Controller
import universe.constellation.orion.viewer.OrionViewerActivity
import universe.constellation.orion.viewer.prefs.GlobalOptions
import universe.constellation.orion.viewer.prefs.OrionApplication


abstract class InstrumentationTestCase : TestUtil {
abstract class InstrumentationTestCase(intent: Intent, private val showTapHelp: Boolean = false) : TestUtil {

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

@get:Rule
val mActivityRule: ActivityTestRule<OrionViewerActivity> = ActivityTestRule(OrionViewerActivity::class.java, true, false)

val activity: OrionViewerActivity
get() = mActivityRule.activity

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

fun startActivityWithBook(intent: Intent) {
mActivityRule.launchActivity(intent)
activity.orionContext.isTesting = true
var activityScenarioRule = activityScenarioRule<OrionViewerActivity>(intent.apply {
if (showTapHelp) {
putExtra(GlobalOptions.SHOW_TAP_HELP, true)
} else {
putExtra(GlobalOptions.SHOW_TAP_HELP, false)
}
})

val globalOptions by lazy {
(InstrumentationRegistry.getInstrumentation().targetContext.applicationContext as OrionApplication).options
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fun extractFileFromTestData(fileName: String): File {
throw RuntimeException("Couldn't create new file " + outFile.absolutePath, e)
}

val input = ClassLoader.getSystemClassLoader().getResourceAsStream(getFileUnderTestData(fileName))
val input = TestUtil::class.java.classLoader.getResourceAsStream(getFileUnderTestData(fileName))
val bufferedOutputStream = FileOutputStream(outFile).buffered()
input.buffered().copyTo(bufferedOutputStream)
bufferedOutputStream.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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 @@ -1074,7 +1074,7 @@ class OrionViewerActivity : OrionBaseActivity(viewerType = Device.VIEWER_ACTIVIT
fun showTapDialogIfNeeded() {
println("tap")
if (++tapHelpCounter < 2) return
if (globalOptions.isShowTapHelp && !orionContext.isTesting) {
if (globalOptions.isShowTapHelp) {
TapHelpDialog(this).showDialog()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ class OrionApplication : Application() {

val device = createDevice()

var isTesting = false

private var langCode: String? = null

private val isLightTheme: Boolean
Expand Down

0 comments on commit c44d856

Please sign in to comment.