From 3a8d3dcd906f184d36310acb05adc094aca1623a Mon Sep 17 00:00:00 2001 From: Michael Bogdanov Date: Sun, 15 Oct 2023 16:37:57 +0200 Subject: [PATCH] Refactor tests --- .../viewer/test/RenderingAndNavigationTest.kt | 48 +++++++++---------- .../orion/viewer/test/RotationTest.kt | 32 +++++++++---- .../test/espresso/CheckTapZoneStartScreen.kt | 33 ++----------- .../test/framework/InstrumentationTestCase.kt | 29 +++++------ .../orion/viewer/test/framework/TestUtil.kt | 2 +- .../orion/viewer/OrionViewerActivity.kt | 2 +- .../orion/viewer/prefs/OrionApplication.kt | 2 - 7 files changed, 67 insertions(+), 81 deletions(-) diff --git a/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/RenderingAndNavigationTest.kt b/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/RenderingAndNavigationTest.kt index 35af9d3fd..49fb50112 100644 --- a/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/RenderingAndNavigationTest.kt +++ b/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/RenderingAndNavigationTest.kt @@ -2,13 +2,13 @@ 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 @@ -16,18 +16,18 @@ 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> { - return BookDescription.values().map { arrayOf(it.path) } + fun testData(): Iterable> { + return BookDescription.values().map { arrayOf(it) } } } @@ -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() + activityScenarioRule.scenario.onActivity { + ref.set(it.view) + } + view = MyView(ref.get()) } @Test @@ -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 } } \ No newline at end of file diff --git a/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/RotationTest.kt b/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/RotationTest.kt index cb1d83eac..c46c42e64 100644 --- a/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/RotationTest.kt +++ b/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/RotationTest.kt @@ -4,42 +4,54 @@ import android.content.pm.ActivityInfo 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 +import universe.constellation.orion.viewer.view.OrionDrawScene -class RotationTest : InstrumentationTestCase() { +class RotationTest : InstrumentationTestCase(BookDescription.SICP.toOpenIntent()) { @Test @Ignore 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) + } } } } \ No newline at end of file diff --git a/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/espresso/CheckTapZoneStartScreen.kt b/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/espresso/CheckTapZoneStartScreen.kt index b82e14b51..4acba435e 100644 --- a/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/espresso/CheckTapZoneStartScreen.kt +++ b/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/espresso/CheckTapZoneStartScreen.kt @@ -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.* @@ -9,61 +8,39 @@ 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.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(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;}) { +class NoBookNoStartTapScreen: InstrumentationTestCase(BookDescription.SICP.toOpenIntent().apply { data = null;}, false) { @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()) { +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() { diff --git a/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/framework/InstrumentationTestCase.kt b/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/framework/InstrumentationTestCase.kt index f69b2338f..bb337ee58 100644 --- a/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/framework/InstrumentationTestCase.kt +++ b/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/framework/InstrumentationTestCase.kt @@ -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 = 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(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 } } \ No newline at end of file diff --git a/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/framework/TestUtil.kt b/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/framework/TestUtil.kt index 110098f0b..b8a590355 100644 --- a/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/framework/TestUtil.kt +++ b/orion-viewer/src/androidTest/kotlin/universe/constellation/orion/viewer/test/framework/TestUtil.kt @@ -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() diff --git a/orion-viewer/src/main/java/universe/constellation/orion/viewer/OrionViewerActivity.kt b/orion-viewer/src/main/java/universe/constellation/orion/viewer/OrionViewerActivity.kt index 86cd8ea1b..5cb22bff5 100644 --- a/orion-viewer/src/main/java/universe/constellation/orion/viewer/OrionViewerActivity.kt +++ b/orion-viewer/src/main/java/universe/constellation/orion/viewer/OrionViewerActivity.kt @@ -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() } } diff --git a/orion-viewer/src/main/java/universe/constellation/orion/viewer/prefs/OrionApplication.kt b/orion-viewer/src/main/java/universe/constellation/orion/viewer/prefs/OrionApplication.kt index 88d400ef5..861ba5015 100644 --- a/orion-viewer/src/main/java/universe/constellation/orion/viewer/prefs/OrionApplication.kt +++ b/orion-viewer/src/main/java/universe/constellation/orion/viewer/prefs/OrionApplication.kt @@ -63,8 +63,6 @@ class OrionApplication : Application() { val device = createDevice() - var isTesting = false - private var langCode: String? = null private val isLightTheme: Boolean