Skip to content

Commit

Permalink
Update test
Browse files Browse the repository at this point in the history
  • Loading branch information
max-kammerer committed Jan 18, 2024
1 parent 5f5bb9a commit 08c88a7
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,32 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import universe.constellation.orion.viewer.*
import universe.constellation.orion.viewer.layout.LayoutStrategy
import universe.constellation.orion.viewer.layout.SimpleLayoutStrategy
import universe.constellation.orion.viewer.prefs.GlobalOptions.TEST_SCREEN_HEIGHT
import universe.constellation.orion.viewer.prefs.GlobalOptions.TEST_SCREEN_WIDTH
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.openTestBook
import universe.constellation.orion.viewer.view.OrionDrawScene
import java.util.concurrent.CountDownLatch
import java.util.concurrent.atomic.AtomicReference

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

@RunWith(Parameterized::class)
class RenderingAndNavigationTest(private val book: BookDescription) : InstrumentationTestCase(book.toOpenIntent()) {
class RenderingAndNavigationTest(private val book: BookDescription) : InstrumentationTestCase(book.toOpenIntent(), additionalParams = {
intent ->
intent.putExtra(TEST_SCREEN_WIDTH, deviceSize.x)
intent.putExtra(TEST_SCREEN_HEIGHT, deviceSize.y)
}) {

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

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

@Test
fun testProperPages() {
Expand All @@ -38,48 +43,65 @@ class RenderingAndNavigationTest(private val book: BookDescription) : Instrument
val controller = prepareEngine()
val screens = 21

assertEquals(0, controller.currentPage)
assertEquals(book.pageCount, controller.pageCount)
assertEquals(0, controller.currentPage)

val nexts = arrayListOf<IntArray>()
val nextPageList = arrayListOf<IntArray>()
//TODO
repeat(screens) {
processBitmap(nexts) { controller.drawNext() }
val latch = CountDownLatch(1)
activityScenarioRule.scenario.onActivity { activity ->
controller.drawNext()

controller.pageLayoutManager.pageListener = object : PageInitListener {
override fun onPageInited(pageView: PageView) {
controller.drawPage {
processBitmap(nextPageList, it as Bitmap)
controller.pageLayoutManager.pageListener = null
latch.countDown()
}
}
}
}
latch.await()
}

controller.drawNext()

val prevs = arrayListOf<IntArray>()
val prevPageList = arrayListOf<IntArray>()
repeat(screens) {
processBitmap(prevs) { controller.drawPrev() }
val latch = CountDownLatch(1)
activityScenarioRule.scenario.onActivity { activity ->
controller.drawPrev()
controller.pageLayoutManager.pageListener = object : PageInitListener {
override fun onPageInited(pageView: PageView) {
controller.drawPage {
processBitmap(prevPageList, it as Bitmap)
latch.countDown()
controller.pageLayoutManager.pageListener = null
}
}
}
}
latch.await()
}

assertEquals(prevs.size, screens)
assertEquals(nexts.size, screens)

nexts.zipWithNext().forEachIndexed { index, (left, right) ->
assertEquals(nextPageList.size, screens)
assertEquals(prevPageList.size, screens)

nextPageList.zipWithNext().forEachIndexed { index, (left, right) ->
assertFalse("Screens $index and ${index+1} are equals: ${left.joinToString()}", left.contentEquals(right))
}

prevs.zipWithNext().forEachIndexed { index, (left, right) ->
prevPageList.zipWithNext().forEachIndexed { index, (left, right) ->
assertFalse("Screens $index and ${index+1} are equals: ${left.joinToString()}", left.contentEquals(right))
}

nexts.zip(prevs.reversed()).forEachIndexed() { index, (next, prev) ->
nextPageList.zip(prevPageList.reversed()).forEachIndexed() { index, (next, prev) ->
assertArrayEquals("fail on $index", next, prev)
}
}

private fun processBitmap(list: MutableList<IntArray>, drawer: () -> Unit) {
drawer()
//TODO: rework
val bitmapRef = AtomicReference<Bitmap>()
while (bitmapRef.get() == null) {
activityScenarioRule.scenario.onActivity {
bitmapRef.set((it.fullScene.drawView as OrionDrawScene).pageView?.bitmap)
}
Thread.sleep(50)
}
val bitmap = bitmapRef.get()
private fun processBitmap(list: MutableList<IntArray>, bitmap: Bitmap) {
assertNotNull(bitmap)

val pixels = IntArray(bitmap.width * bitmap.height)
Expand All @@ -91,17 +113,18 @@ class RenderingAndNavigationTest(private val book: BookDescription) : Instrument
private fun prepareEngine(): Controller {
val ref = AtomicReference<Controller>()
activityScenarioRule.scenario.onActivity { activity ->
val document = openTestBook(book)
val layoutStrategy: LayoutStrategy = SimpleLayoutStrategy.create(document)
val controller = Controller(activity, document, layoutStrategy)

// val document = openTestBook(book)
// val layoutStrategy: LayoutStrategy = SimpleLayoutStrategy.create(document)
// val controller = Controller(activity, document, layoutStrategy)
val lastPageInfo =
LastPageInfo.loadBookParameters(activity, "123", initalizer(activity.globalOptions))
controller.changeOrinatation(lastPageInfo.screenOrientation)
controller.init(lastPageInfo, deviceSize)
// controller.changeOrinatation(lastPageInfo.screenOrientation)

//getSubscriptionManager()?.sendDocOpenedNotification(controller)
activity.view.setDimensionAware(controller)
val controller = activity.controller!!
controller.init(lastPageInfo, deviceSize)
controller.pageLayoutManager.isSinglePageMode = true
controller.forcePageRecreation()
ref.set(controller)
}
return ref.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import universe.constellation.orion.viewer.prefs.GlobalOptions
import universe.constellation.orion.viewer.prefs.OrionApplication


abstract class InstrumentationTestCase(intent: Intent, private val showTapHelp: Boolean = false) : BaseTest() {
abstract class InstrumentationTestCase(intent: Intent, private val showTapHelp: Boolean = false, additionalParams: (Intent) -> Unit = {}) : BaseTest() {

@get:Rule
var activityScenarioRule = activityScenarioRule<OrionViewerActivity>(intent.apply {
Expand All @@ -18,6 +18,7 @@ abstract class InstrumentationTestCase(intent: Intent, private val showTapHelp:
} else {
putExtra(GlobalOptions.SHOW_TAP_HELP, false)
}
additionalParams(this)
})

val globalOptions by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ class Controller(
}

@JvmOverloads
fun drawPage(info: LayoutPosition = layoutInfo) {
log("Draw page " + document.title + " "+ info.pageNumber)
fun drawPage(info: LayoutPosition = layoutInfo, uiCallaback: Function1<Any, Unit>? = null) {
log("Controller drawPage: " + document.title + " "+ info)
layoutInfo = info
pageLayoutManager.renderPageWithOffset(info.pageNumber, info.x.offset, info.y.offset)
pageLayoutManager.renderPageWithOffset(info.pageNumber, info.x.offset, info.y.offset, uiCallaback)
}

fun processPendingEvents() {
Expand Down Expand Up @@ -139,14 +139,16 @@ class Controller(
}
}

fun drawNext() {
@JvmOverloads
fun drawNext(uiCallaback: Function1<Any, Unit>? = null) {
layoutStrategy.calcPageLayout(layoutInfo, true, pageCount)
drawPage(layoutInfo)
drawPage(layoutInfo, uiCallaback)
}

fun drawPrev() {
@JvmOverloads
fun drawPrev(uiCallaback: Function1<Any, Unit>? = null) {
layoutStrategy.calcPageLayout(layoutInfo, false, pageCount)
drawPage(layoutInfo)
drawPage(layoutInfo, uiCallaback)
}

fun translateAndZoom(changeZoom: Boolean, zoomScaling: Float, deltaX: Float, deltaY: Float) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,14 @@ class OrionViewerActivity : OrionBaseActivity(viewerType = Device.VIEWER_ACTIVIT
val showTapHelp = intent.getBooleanExtra(GlobalOptions.SHOW_TAP_HELP, false)
globalOptions.saveBooleanProperty(GlobalOptions.SHOW_TAP_HELP, showTapHelp)
}

if (intent.hasExtra(GlobalOptions.TEST_SCREEN_WIDTH) && intent.hasExtra(GlobalOptions.TEST_SCREEN_HEIGHT)) {
val newWidth = intent.getIntExtra(GlobalOptions.TEST_SCREEN_WIDTH, view.layoutParams.width)
val newHeigth = intent.getIntExtra(GlobalOptions.TEST_SCREEN_HEIGHT, view.layoutParams.height)
view.layoutParams.width = newWidth
view.layoutParams.height = newHeigth
view.requestLayout()
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.graphics.Point
import android.graphics.PointF
import android.graphics.Rect
import android.graphics.Region
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
Expand All @@ -31,6 +30,11 @@ enum class State(val interactWithUUI: Boolean) {
DESTROYED(false)
}

interface PageInitListener {

fun onPageInited(pageView: PageView)
}

class LayoutData {
var order: Int = 0
val position: PointF = PointF(0f, 0f)
Expand Down Expand Up @@ -221,18 +225,18 @@ class PageView(
return controller.bitmapCache.createBitmap(width, height)
}

internal fun renderVisible() {
internal fun renderVisible(uiCallaback: Function1<Any, Unit>? = null) {
val tmpRect = layoutData.tmpRect
tmpRect.set(wholePageRect)
tmpRect.offset(layoutData.position.x.toInt(), layoutData.position.y.toInt())

if (pageLayoutManager.isVisible(this)) {
tmpRect.offset((-layoutData.position.x).toInt(), (-layoutData.position.y).toInt())
render(tmpRect)
render(tmpRect, uiCallaback)
}
}

internal fun render(rect: Rect) {
internal fun render(rect: Rect, uiCallaback: Function1<Any, Unit>? = null) {
val layoutStrategy = controller.layoutStrategy
if (!(layoutStrategy.viewWidth > 0 && layoutStrategy.viewHeight > 0)) return

Expand All @@ -258,12 +262,18 @@ class PageView(
log("invalidate: $pageNum $layoutData")
println("inv: $pageNum ")
scene?.invalidate()
if (uiCallaback != null) {
uiCallaback(bitmap!!)
}
}
}
}
}
} else {
scene?.invalidate()
if (uiCallaback != null) {
uiCallaback(bitmap!!)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public class GlobalOptions implements Serializable, PageOptions {

public final static String SHOW_TAP_HELP = "SHOW_TAP_HELP";

public final static String TEST_SCREEN_WIDTH = "TEST_SCREEN_WIDTH";

public final static String TEST_SCREEN_HEIGHT = "TEST_SCREEN_HEIGHT";

public final static String SCREEN_BACKLIGHT_TIMEOUT = "SCREEN_BACKLIGHT_TIMEOUT";

public final static String ENABLE_TOUCH_MOVE = "ENABLE_TOUCH_MOVE";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import android.graphics.PointF
import android.graphics.Rect
import androidx.core.math.MathUtils
import universe.constellation.orion.viewer.Controller
import universe.constellation.orion.viewer.PageInitListener
import universe.constellation.orion.viewer.PageView
import universe.constellation.orion.viewer.State
import universe.constellation.orion.viewer.log
import universe.constellation.orion.viewer.selection.PageAndSelection
import kotlin.math.abs
Expand Down Expand Up @@ -53,6 +55,17 @@ class PageLayoutManager(val controller: Controller, val scene: OrionDrawScene):
visiblePages.clear()
}


var pageListener: PageInitListener? = null
set(value) {
field = value
visiblePages.forEach {
if (it.state != State.CREATED) {
value?.onPageInited(it)
}
}
}

fun doScroll(xPos: Float, yPos: Float, distanceX: Float, distanceY: Float) {
println("onScroll: $distanceX, $distanceY")
isSinglePageMode = false
Expand Down Expand Up @@ -157,7 +170,7 @@ class PageLayoutManager(val controller: Controller, val scene: OrionDrawScene):
}
}

fun renderPageWithOffset(pageNum: Int, x: Int, y: Int) {
fun renderPageWithOffset(pageNum: Int, x: Int, y: Int, uiCallaback: Function1<Any, Unit>? = null) {
isSinglePageMode = true
val iterator = visiblePages.iterator()
for (page in iterator) {
Expand All @@ -176,7 +189,7 @@ class PageLayoutManager(val controller: Controller, val scene: OrionDrawScene):

//TODO keep proper position
page.layoutData.position.set(x.toFloat(), y.toFloat())
page.renderVisible()
page.renderVisible(uiCallaback)
}

fun dump() {
Expand Down Expand Up @@ -218,6 +231,8 @@ class PageLayoutManager(val controller: Controller, val scene: OrionDrawScene):
}

fun onSizeCalculated(updatedView: PageView, oldArea: Rect) {
pageListener?.onPageInited(updatedView)

log("onSizeCalculated ${updatedView.pageNum}: ${updatedView.layoutData} $oldArea")
var found = false
var shiftY = 0
Expand Down

0 comments on commit 08c88a7

Please sign in to comment.