Skip to content

Commit

Permalink
fix(iam-dialog): fixed inAppMessageContainer blocking the app
Browse files Browse the repository at this point in the history
SDK-186

Co-authored-by: LasOri <[email protected]>
Co-authored-by: Andras Sarro <[email protected]>
Co-authored-by: matusekma <[email protected]>
  • Loading branch information
4 people committed Jan 31, 2025
1 parent 8f2864c commit fdea9b5
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.emarsys.mobileengage.iam.webview.MessageLoadedListener
import com.emarsys.testUtil.ExtensionTestUtils.runOnMain
import com.emarsys.testUtil.InstrumentationRegistry.Companion.getTargetContext
import com.emarsys.testUtil.ReflectionTestUtils
import io.kotest.matchers.ints.shouldBeGreaterThan
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.mockk.every
Expand All @@ -43,8 +44,8 @@ import java.util.concurrent.CountDownLatch
class IamDialogTest {
private companion object {
const val CAMPAIGN_ID = "id_value"
private const val SID = "testSid"
private const val URL = "https://www.emarsys.com"
const val SID = "testSid"
const val URL = "https://www.emarsys.com"
const val ON_SCREEN_TIME_KEY = "on_screen_time"
const val CAMPAIGN_ID_KEY = "id"
const val REQUEST_ID_KEY = "request_id"
Expand Down Expand Up @@ -132,10 +133,9 @@ class IamDialogTest {

@Test
fun testCreate_shouldReturnIamDialogInstance() {
val fragmentScenario =
launchFragment {
iamDialog
}
val fragmentScenario = launchFragment(initialState = Lifecycle.State.CREATED) {
iamDialog
}
fragmentScenario.onFragment { fragment ->
(fragment is IamDialog) shouldBe true
fragment shouldNotBe null
Expand All @@ -150,7 +150,7 @@ class IamDialogTest {
bundle.putString(IamDialog.SID, null)
bundle.putString(IamDialog.URL, null)
bundle.putString(IamDialog.REQUEST_ID, null)
val fragmentScenario = launchFragment(bundle) {
val fragmentScenario = launchFragment(bundle, initialState = Lifecycle.State.CREATED) {
iamDialog
}
fragmentScenario.onFragment { fragment ->
Expand All @@ -170,7 +170,7 @@ class IamDialogTest {
bundle.putString(IamDialog.SID, null)
bundle.putString(IamDialog.URL, null)
bundle.putString(IamDialog.REQUEST_ID, requestId)
val fragmentScenario = launchFragment(bundle) {
val fragmentScenario = launchFragment(bundle, initialState = Lifecycle.State.CREATED) {
iamDialog
}
fragmentScenario.onFragment { fragment ->
Expand All @@ -191,7 +191,7 @@ class IamDialogTest {
bundle.putString(IamDialog.URL, null)
bundle.putString(IamDialog.REQUEST_ID, requestId)

val fragmentScenario = launchFragment(bundle) {
val fragmentScenario = launchFragment(bundle, initialState = Lifecycle.State.CREATED) {
iamDialog
}
fragmentScenario.onFragment { fragment ->
Expand All @@ -212,7 +212,7 @@ class IamDialogTest {
bundle.putString(IamDialog.URL, URL)
bundle.putString(IamDialog.REQUEST_ID, requestId)

val fragmentScenario = launchFragment(bundle) {
val fragmentScenario = launchFragment(bundle, initialState = Lifecycle.State.CREATED) {
iamDialog
}
fragmentScenario.onFragment { fragment ->
Expand All @@ -223,6 +223,35 @@ class IamDialogTest {
}
}

@Test
fun testOnCreate_shouldCreateWebView_withFragmentActivity_whenActivityReferenceIsNull() {
val fragmentScenario = launchFragment(initialState = Lifecycle.State.CREATED) {
iamDialog
}
fragmentScenario.onFragment { fragment ->
verify { mockWebViewFactory.create(fragment.activity!!) }
}
}

@Test
fun testOnCreate_should_notOverrideHtml_whenItIsStillAvailable() {
val mockIamWebView: IamWebView = mockk(relaxed = true)
every { mockWebViewFactory.create(any()) } returns mockIamWebView

val testHtml = "<html>123</html>"
val testInAppMetaData = InAppMetaData("123", null, null)
ReflectionTestUtils.setInstanceField(iamDialog, "html", testHtml)
ReflectionTestUtils.setInstanceField(iamDialog, "inAppMetaData", testInAppMetaData)

val fragmentScenario = launchFragment(initialState = Lifecycle.State.CREATED) {
iamDialog
}

fragmentScenario.onFragment { fragment ->
verify { mockIamWebView.load(testHtml, testInAppMetaData, any()) }
}
}

@Test
fun testCreate_shouldInitializeDialog_withOutRequestId() {
val campaignId = "campaignId"
Expand All @@ -231,7 +260,7 @@ class IamDialogTest {
bundle.putString(IamDialog.SID, SID)
bundle.putString(IamDialog.URL, URL)
bundle.putString(IamDialog.REQUEST_ID, null)
val fragmentScenario = launchFragment(bundle) {
val fragmentScenario = launchFragment(bundle, initialState = Lifecycle.State.CREATED) {
iamDialog
}
fragmentScenario.onFragment { fragment ->
Expand All @@ -244,12 +273,16 @@ class IamDialogTest {

@Test
fun testInitialization_setsDimAmountToZero() {
launchActivityIfNeeded()

every { mockWebViewFactory.create(any()) } returns iamWebView

val bundle = Bundle()
bundle.putString(IamDialog.CAMPAIGN_ID, CAMPAIGN_ID)
bundle.putString(IamDialog.SID, SID)
bundle.putString(IamDialog.URL, URL)
bundle.putString(IamDialog.REQUEST_ID, null)
val fragmentScenario = launchFragment(bundle) {
val fragmentScenario = launchFragment(bundle, initialState = Lifecycle.State.CREATED) {
iamDialog
}
displayDialog(fragmentScenario)
Expand All @@ -262,122 +295,73 @@ class IamDialogTest {

@Test
fun testInitialization_setsDialogToFullscreen() {
val bundle = Bundle()
bundle.putString(IamDialog.CAMPAIGN_ID, CAMPAIGN_ID)
bundle.putString(IamDialog.SID, SID)
bundle.putString(IamDialog.URL, URL)
bundle.putString(IamDialog.REQUEST_ID, null)

val fragmentScenario = launchFragment(bundle) {
iamDialog
}

displayDialog(fragmentScenario)
fragmentScenario.onFragment {
val windowWidth = it.activity!!.window.attributes.width
val windowHeight = it.activity!!.window.attributes.height

val dialogWidth = it.dialog!!.window!!.attributes.width
val dialogHeight = it.dialog!!.window!!.attributes.height
launchActivityIfNeeded()

dialogWidth shouldBe windowWidth
dialogHeight shouldBe windowHeight
}
}
every { mockWebViewFactory.create(any()) } returns iamWebView

@Test
fun testDialog_stillVisible_afterOrientationChange() {
val bundle = Bundle()
bundle.putString(IamDialog.CAMPAIGN_ID, CAMPAIGN_ID)
bundle.putString(IamDialog.SID, SID)
bundle.putString(IamDialog.URL, URL)
bundle.putString(IamDialog.REQUEST_ID, REQUEST_ID_KEY)
bundle.putSerializable("loading_time", InAppLoadingTime(0, 0))
bundle.putString(IamDialog.REQUEST_ID, null)

val fragmentScenario = launchFragment(bundle) {
val fragmentScenario = launchFragment(bundle, initialState = Lifecycle.State.CREATED) {
iamDialog
}

displayDialog(fragmentScenario)

fragmentScenario.onFragment {
it.activity?.runOnUiThread {
it.activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}
fragmentScenario.moveToState(Lifecycle.State.STARTED)
fragmentScenario.moveToState(Lifecycle.State.RESUMED)

it.activity?.runOnUiThread {
it.activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
val windowWidth = it.activity!!.window.decorView.measuredWidth
val windowHeight = it.activity!!.window.decorView.measuredHeight

val dialogWidth = it.activity?.window?.attributes?.width ?: 0
val dialogHeight = it.activity?.window?.attributes?.height ?: 0
val windowWidth = it.dialog!!.window!!.attributes.width
val windowHeight = it.dialog!!.window!!.attributes.height
val dialogWidth = it.view!!.layoutParams.width
val dialogHeight = it.view!!.layoutParams.height

dialogWidth shouldBe windowWidth
dialogHeight shouldBe windowHeight
windowHeight shouldBeGreaterThan 0
windowWidth shouldBeGreaterThan 0
dialogHeight shouldBe -1
dialogWidth shouldBe -1
}
}

@Test
fun testDialog_cancel_turnsRetainInstanceOff() {
val bundle = Bundle()
bundle.putString(IamDialog.CAMPAIGN_ID, CAMPAIGN_ID)
bundle.putString(IamDialog.SID, SID)
bundle.putString(IamDialog.URL, URL)
bundle.putString(IamDialog.REQUEST_ID, REQUEST_ID_KEY)

val fragmentScenario =
launchFragment(bundle) {
iamDialog
}
val fragmentLatch = CountDownLatch(1)

displayDialog(fragmentScenario)
fragmentScenario.onFragment {
it.setInAppLoadingTime(InAppLoadingTime(1, 1))
it.activity?.runOnUiThread {
it.activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE

it.onCancel(it.dialog!!)

it.activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
fun testDialog_stillVisible_afterOrientationChange() {
launchActivityIfNeeded()

it.retainInstance shouldBe false
fragmentLatch.countDown()
}
}
fragmentLatch.await()
}
every { mockWebViewFactory.create(any()) } returns iamWebView

@Test
fun testDialog_dismiss_turnsRetainInstanceOff() {
val bundle = Bundle()
bundle.putString(IamDialog.CAMPAIGN_ID, CAMPAIGN_ID)
bundle.putString(IamDialog.SID, SID)
bundle.putString(IamDialog.URL, URL)
bundle.putString(IamDialog.REQUEST_ID, REQUEST_ID_KEY)
bundle.putSerializable("loading_time", InAppLoadingTime(0, 0))

val fragmentScenario = launchFragment(bundle) {
val fragmentScenario = launchFragment(bundle, initialState = Lifecycle.State.CREATED) {
iamDialog
}
val fragmentLatch = CountDownLatch(1)

displayDialog(fragmentScenario)
fragmentScenario.onFragment {
it.setInAppLoadingTime(InAppLoadingTime(1, 1))
it.activity?.runOnUiThread {
it.activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
fragmentScenario.moveToState(Lifecycle.State.STARTED)
fragmentScenario.moveToState(Lifecycle.State.RESUMED)

it.dismiss()
it.activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE

it.activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
it.activity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

it.retainInstance shouldBe false
fragmentLatch.countDown()
}
val windowWidth = it.activity!!.window.decorView.measuredWidth
val windowHeight = it.activity!!.window.decorView.measuredHeight

val dialogWidth = it.view!!.layoutParams.width
val dialogHeight = it.view!!.layoutParams.height

windowHeight shouldBeGreaterThan 0
windowWidth shouldBeGreaterThan 0
dialogHeight shouldBe -1
dialogWidth shouldBe -1
}
fragmentLatch.await()
}

@Test
Expand Down Expand Up @@ -493,9 +477,11 @@ class IamDialogTest {
fun testOnStart_shouldNotThrowTheSpecifiedChildAlreadyHasAParent_exception() {
launchActivityIfNeeded()

every { mockWebViewFactory.create(any()) } returns iamWebView

var result: Exception? = null
try {
val fragmentScenario = launchFragment {
val fragmentScenario = launchFragment(initialState = Lifecycle.State.CREATED) {
iamDialog
}
displayDialog(fragmentScenario)
Expand All @@ -513,6 +499,8 @@ class IamDialogTest {
fun testOnStart_shouldNotThrowTheSpecifiedWebViewAlreadyHasAParent_exception() {
launchActivityIfNeeded()

every { mockWebViewFactory.create(any()) } returns iamWebView

var result: Exception? = null
try {

Expand All @@ -525,7 +513,7 @@ class IamDialogTest {

every { mockWebViewFactory.create(any()) } returns iamWebView

val fragmentScenario = launchFragment {
val fragmentScenario = launchFragment(initialState = Lifecycle.State.CREATED) {
iamDialog
}
displayDialog(fragmentScenario)
Expand Down Expand Up @@ -573,9 +561,7 @@ class IamDialogTest {

private fun displayDialog(fragmentScenario: FragmentScenario<IamDialog>) {
fragmentScenario.onFragment { dialog ->
dialog.activity?.runOnUiThread {
dialog.activity?.supportFragmentManager?.executePendingTransactions()
}
dialog.activity?.supportFragmentManager?.executePendingTransactions()
}
fragmentScenario.moveToState(Lifecycle.State.RESUMED)
}
Expand Down
Loading

0 comments on commit fdea9b5

Please sign in to comment.