From 774d31d9a69e4f11cef07e2f0532eb6e3cc4953d Mon Sep 17 00:00:00 2001 From: Ryan Morales Date: Mon, 15 Jul 2024 15:10:08 -0700 Subject: [PATCH 1/4] Use InAppMessage dimensions relative to host activity - implementation of fix in https://github.com/adobe/aepsdk-core-android/pull/618 --- .../ui/message/views/MessageContent.kt | 20 ++-------- .../services/ui/message/views/MessageFrame.kt | 40 ++++++++++++++----- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageContent.kt b/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageContent.kt index 47379a4b4..7c681d5a0 100644 --- a/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageContent.kt +++ b/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageContent.kt @@ -13,43 +13,31 @@ package com.adobe.marketing.mobile.services.ui.message.views import android.view.ViewGroup import android.webkit.WebView -import androidx.compose.foundation.layout.height -import androidx.compose.foundation.layout.width import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip -import androidx.compose.ui.platform.LocalConfiguration import androidx.compose.ui.platform.testTag -import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp import androidx.compose.ui.viewinterop.AndroidView import com.adobe.marketing.mobile.services.Log import com.adobe.marketing.mobile.services.ServiceConstants -import com.adobe.marketing.mobile.services.ui.message.GestureTracker import com.adobe.marketing.mobile.services.ui.message.InAppMessagePresentable import com.adobe.marketing.mobile.services.ui.message.InAppMessageSettings import java.nio.charset.StandardCharsets /** * Represents the content of the InAppMessage. Holds a WebView that loads the HTML content of the message + * @param modifier [Modifier] to apply to the content (AndroidView holding the WebView) * @param inAppMessageSettings [InAppMessageSettings] settings for the InAppMessage * @param onCreated callback to be invoked when the WenView that this composable holds is created - * @param gestureTracker [GestureTracker] to track the swipe/drag gestures on the webview */ @Composable internal fun MessageContent( + modifier: Modifier, inAppMessageSettings: InAppMessageSettings, onCreated: (WebView) -> Unit ) { - // Size variables. Ideally, these values can be remembered because generally a configuration - // change will refresh the size. However, in-case the configuration changes (e.g. device rotation) - // are restricted by the Activity hosting this composable, these values will not be recomputed. - // So, we are not remembering these values to ensure that the size is recalculated on every - // composition. - val currentConfiguration = LocalConfiguration.current - val heightDp: Dp = ((currentConfiguration.screenHeightDp * inAppMessageSettings.height) / 100).dp - val widthDp: Dp = ((currentConfiguration.screenWidthDp * inAppMessageSettings.width) / 100).dp AndroidView( factory = { @@ -80,9 +68,7 @@ internal fun MessageContent( ) } }, - modifier = Modifier - .height(heightDp) - .width(widthDp) + modifier = modifier .clip(RoundedCornerShape(inAppMessageSettings.cornerRadius.dp)) .testTag(MessageTestTags.MESSAGE_CONTENT) ) diff --git a/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageFrame.kt b/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageFrame.kt index 8e59b03e1..47eddff68 100644 --- a/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageFrame.kt +++ b/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageFrame.kt @@ -11,7 +11,9 @@ package com.adobe.marketing.mobile.services.ui.message.views +import android.app.Activity import android.os.Build +import android.view.View import android.webkit.WebView import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.core.MutableTransitionState @@ -21,7 +23,9 @@ import androidx.compose.foundation.gestures.draggable import androidx.compose.foundation.gestures.rememberDraggableState import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.offset +import androidx.compose.foundation.layout.width import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.Card import androidx.compose.runtime.Composable @@ -32,9 +36,12 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color -import androidx.compose.ui.platform.LocalConfiguration +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.testTag import androidx.compose.ui.unit.dp +import com.adobe.marketing.mobile.services.Log +import com.adobe.marketing.mobile.services.ServiceConstants import com.adobe.marketing.mobile.services.ui.message.GestureTracker import com.adobe.marketing.mobile.services.ui.message.InAppMessageSettings import com.adobe.marketing.mobile.services.ui.message.mapping.MessageAlignmentMapper @@ -58,22 +65,35 @@ internal fun MessageFrame( onCreated: (WebView) -> Unit, onDisposed: () -> Unit ) { - val currentConfiguration = LocalConfiguration.current + // The current context is the activity that is hosting the message. We can safely cast it to + // an Activity because this composable is always used within the context of the activity in + // the current implementation of UIService + val currentActivity = LocalContext.current as? Activity ?: run { + onDisposed() + Log.debug(ServiceConstants.LOG_TAG, "MessageFrame", "Unable to get the current activity. Dismissing the message.") + return + } + + // We want to calculate the height and width of the message based on the host activity's + // content view. This is because the message is displayed on top of the content view and + // we want to ensure that the message is displayed relative to the activity's size. + // This allows the message to be displayed correctly in split screen mode and other + // multi-window modes. + val contentView = currentActivity.findViewById(android.R.id.content) + val contentHeightDp = with(LocalDensity.current) { contentView.height.toDp() } + val contentWidthDp = with(LocalDensity.current) { contentView.width.toDp() } + val heightDp = remember { ((contentHeightDp * inAppMessageSettings.height) / 100) } + val widthDp = remember { ((contentWidthDp * inAppMessageSettings.width) / 100) } - // Ideally, these values can be remembered because generally a configuration - // change will refresh the size. However, in-case the configuration changes (e.g. device rotation) - // are restricted by the Activity hosting this composable, these values will not be recomputed. - // So, we are not remembering these values to ensure that the size is recalculated on every - // composition. val horizontalOffset = MessageOffsetMapper.getHorizontalOffset( inAppMessageSettings.horizontalAlignment, inAppMessageSettings.horizontalInset, - currentConfiguration.screenWidthDp.dp + widthDp ) val verticalOffset = MessageOffsetMapper.getVerticalOffset( inAppMessageSettings.verticalAlignment, inAppMessageSettings.verticalInset, - currentConfiguration.screenHeightDp.dp + heightDp ) val allowGestures = remember { inAppMessageSettings.gestureMap.isNotEmpty() } @@ -147,7 +167,7 @@ internal fun MessageFrame( } ) ) { - MessageContent(inAppMessageSettings, onCreated) + MessageContent(Modifier.height(heightDp).width(widthDp), inAppMessageSettings, onCreated) } // This is a one-time effect that will be called when this composable is completely removed from the composition From 2f5c7520d595d26f448d4c1072a0d904a8972786 Mon Sep 17 00:00:00 2001 From: Ryan Morales Date: Mon, 15 Jul 2024 19:24:53 -0700 Subject: [PATCH 2/4] add ContextExt.findActivity() --- .../mobile/internal/util/ContextExt.kt | 28 +++++++++++++++++++ .../services/ui/message/views/MessageFrame.kt | 4 +-- 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 code/core/src/phone/java/com/adobe/marketing/mobile/internal/util/ContextExt.kt diff --git a/code/core/src/phone/java/com/adobe/marketing/mobile/internal/util/ContextExt.kt b/code/core/src/phone/java/com/adobe/marketing/mobile/internal/util/ContextExt.kt new file mode 100644 index 000000000..2dec58283 --- /dev/null +++ b/code/core/src/phone/java/com/adobe/marketing/mobile/internal/util/ContextExt.kt @@ -0,0 +1,28 @@ +/* + Copyright 2024 Adobe. All rights reserved. + This file is licensed to you under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. You may obtain a copy + of the License at http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software distributed under + the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS + OF ANY KIND, either express or implied. See the License for the specific language + governing permissions and limitations under the License. +*/ + +package com.adobe.marketing.mobile.internal.util + +import android.app.Activity +import android.content.Context +import android.content.ContextWrapper + +/** + * An extension for finding the activity from a context. + */ +internal fun Context.findActivity(): Activity? { + var context = this + while (context is ContextWrapper) { + if (context is Activity) return context + context = context.baseContext + } + return null +} diff --git a/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageFrame.kt b/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageFrame.kt index 47eddff68..e9a58a7fe 100644 --- a/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageFrame.kt +++ b/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageFrame.kt @@ -11,7 +11,6 @@ package com.adobe.marketing.mobile.services.ui.message.views -import android.app.Activity import android.os.Build import android.view.View import android.webkit.WebView @@ -40,6 +39,7 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.testTag import androidx.compose.ui.unit.dp +import com.adobe.marketing.mobile.internal.util.findActivity import com.adobe.marketing.mobile.services.Log import com.adobe.marketing.mobile.services.ServiceConstants import com.adobe.marketing.mobile.services.ui.message.GestureTracker @@ -68,7 +68,7 @@ internal fun MessageFrame( // The current context is the activity that is hosting the message. We can safely cast it to // an Activity because this composable is always used within the context of the activity in // the current implementation of UIService - val currentActivity = LocalContext.current as? Activity ?: run { + val currentActivity = LocalContext.current.findActivity() ?: run { onDisposed() Log.debug(ServiceConstants.LOG_TAG, "MessageFrame", "Unable to get the current activity. Dismissing the message.") return From 2558e69a7c1c666261f0aa2ca73afb477fbd94c2 Mon Sep 17 00:00:00 2001 From: Ryan Morales Date: Tue, 16 Jul 2024 08:34:07 -0700 Subject: [PATCH 3/4] move findActivity within MessageFrame.kt --- .../mobile/internal/util/ContextExt.kt | 28 ------------------- .../services/ui/message/views/MessageFrame.kt | 16 ++++++++++- 2 files changed, 15 insertions(+), 29 deletions(-) delete mode 100644 code/core/src/phone/java/com/adobe/marketing/mobile/internal/util/ContextExt.kt diff --git a/code/core/src/phone/java/com/adobe/marketing/mobile/internal/util/ContextExt.kt b/code/core/src/phone/java/com/adobe/marketing/mobile/internal/util/ContextExt.kt deleted file mode 100644 index 2dec58283..000000000 --- a/code/core/src/phone/java/com/adobe/marketing/mobile/internal/util/ContextExt.kt +++ /dev/null @@ -1,28 +0,0 @@ -/* - Copyright 2024 Adobe. All rights reserved. - This file is licensed to you under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. You may obtain a copy - of the License at http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable law or agreed to in writing, software distributed under - the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS - OF ANY KIND, either express or implied. See the License for the specific language - governing permissions and limitations under the License. -*/ - -package com.adobe.marketing.mobile.internal.util - -import android.app.Activity -import android.content.Context -import android.content.ContextWrapper - -/** - * An extension for finding the activity from a context. - */ -internal fun Context.findActivity(): Activity? { - var context = this - while (context is ContextWrapper) { - if (context is Activity) return context - context = context.baseContext - } - return null -} diff --git a/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageFrame.kt b/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageFrame.kt index e9a58a7fe..9ada3eb43 100644 --- a/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageFrame.kt +++ b/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageFrame.kt @@ -11,6 +11,9 @@ package com.adobe.marketing.mobile.services.ui.message.views +import android.app.Activity +import android.content.Context +import android.content.ContextWrapper import android.os.Build import android.view.View import android.webkit.WebView @@ -39,7 +42,6 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.testTag import androidx.compose.ui.unit.dp -import com.adobe.marketing.mobile.internal.util.findActivity import com.adobe.marketing.mobile.services.Log import com.adobe.marketing.mobile.services.ServiceConstants import com.adobe.marketing.mobile.services.ui.message.GestureTracker @@ -180,3 +182,15 @@ internal fun MessageFrame( } } } + +/** + * An extension for finding the activity from a context. + */ +private fun Context.findActivity(): Activity? { + var context = this + while (context is ContextWrapper) { + if (context is Activity) return context + context = context.baseContext + } + return null +} From 3da292397340fdc764c994782871b9ef5ad3e2af Mon Sep 17 00:00:00 2001 From: Ryan Morales Date: Wed, 17 Jul 2024 13:42:58 -0700 Subject: [PATCH 4/4] fix functional tests broken by fullscreen iam fix --- .../views/MessageScreenOrientationTests.kt | 52 +- .../ui/message/views/MessageScreenTests.kt | 480 ++++++++---------- .../services/ui/message/views/MessageFrame.kt | 4 +- 3 files changed, 244 insertions(+), 292 deletions(-) diff --git a/code/core/src/androidTest/java/com/adobe/marketing/mobile/services/ui/message/views/MessageScreenOrientationTests.kt b/code/core/src/androidTest/java/com/adobe/marketing/mobile/services/ui/message/views/MessageScreenOrientationTests.kt index f17862128..25b950af2 100644 --- a/code/core/src/androidTest/java/com/adobe/marketing/mobile/services/ui/message/views/MessageScreenOrientationTests.kt +++ b/code/core/src/androidTest/java/com/adobe/marketing/mobile/services/ui/message/views/MessageScreenOrientationTests.kt @@ -11,15 +11,19 @@ package com.adobe.marketing.mobile.services.ui.message.views +import android.app.Activity import android.view.View import androidx.compose.runtime.mutableStateOf import androidx.compose.ui.platform.LocalConfiguration +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.test.getUnclippedBoundsInRoot import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.height +import androidx.compose.ui.unit.width import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry import androidx.test.uiautomator.UiDevice @@ -82,20 +86,20 @@ class MessageScreenOrientationTests { .content(HTML_TEXT_SAMPLE) .build() - val screenHeightDp = mutableStateOf(0.dp) - val screenWidthDp = mutableStateOf(0.dp) - val activityHeightDp = mutableStateOf(0.dp) - val activityWidthDp = mutableStateOf(0.dp) + val contentViewHeightDp = mutableStateOf(0.dp) + val contentViewWidthDp = mutableStateOf(0.dp) + val contentHeightDp = mutableStateOf(0.dp) + val contentWidthDp = mutableStateOf(0.dp) composeTestRule.setContent { // setting our composable as content for test // Get the screen dimensions - val currentConfiguration = LocalConfiguration.current - screenHeightDp.value = currentConfiguration.screenHeightDp.dp - screenWidthDp.value = currentConfiguration.screenWidthDp.dp - - val activityRoot = - composeTestRule.activity.window.decorView.findViewById(android.R.id.content) - activityHeightDp.value = with(LocalDensity.current) { activityRoot.height.toDp() } - activityWidthDp.value = with(LocalDensity.current) { activityRoot.width.toDp() } + LocalConfiguration.current.run { + val activity = LocalContext.current as Activity + val contentView = activity.findViewById(android.R.id.content) + contentViewHeightDp.value = with(LocalDensity.current) { contentView.height.toDp() } + contentViewWidthDp.value = with(LocalDensity.current) { contentView.width.toDp() } + contentHeightDp.value = ((contentViewHeightDp.value * settings.height) / 100) + contentWidthDp.value = ((contentViewWidthDp.value * settings.width) / 100) + } MessageScreen( presentationStateManager = presentationStateManager, @@ -123,9 +127,9 @@ class MessageScreenOrientationTests { val contentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT) .getUnclippedBoundsInRoot() val (expectedInitialHeight, expectedInitialWidth) = calculateDimensions( - screenHeightDp.value, - screenWidthDp.value, - activityHeightDp.value, + contentViewHeightDp.value, + contentViewWidthDp.value, + contentHeightDp.value, heightPercentage, widthPercentage ) @@ -158,9 +162,9 @@ class MessageScreenOrientationTests { val landscapeContentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT) .getUnclippedBoundsInRoot() val (expectedLandscapeHeight, expectedLandscapeWidth) = calculateDimensions( - screenHeightDp.value, - screenWidthDp.value, - activityHeightDp.value, + contentViewHeightDp.value, + contentViewWidthDp.value, + contentHeightDp.value, heightPercentage, widthPercentage ) @@ -190,18 +194,18 @@ class MessageScreenOrientationTests { clipped = false ) + val naturalContentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT) + .getUnclippedBoundsInRoot() + // Verify that the message content is restored to its original size val (expectedNaturalHeight, expectedNaturalWidth) = calculateDimensions( - screenHeightDp.value, - screenWidthDp.value, - activityHeightDp.value, + contentViewHeightDp.value, + contentViewWidthDp.value, + contentHeightDp.value, heightPercentage, widthPercentage ) - val naturalContentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT) - .getUnclippedBoundsInRoot() - MessageScreenTestHelper.validateViewSize( naturalContentBounds, expectedNaturalHeight, diff --git a/code/core/src/androidTest/java/com/adobe/marketing/mobile/services/ui/message/views/MessageScreenTests.kt b/code/core/src/androidTest/java/com/adobe/marketing/mobile/services/ui/message/views/MessageScreenTests.kt index a05286691..e1d786612 100644 --- a/code/core/src/androidTest/java/com/adobe/marketing/mobile/services/ui/message/views/MessageScreenTests.kt +++ b/code/core/src/androidTest/java/com/adobe/marketing/mobile/services/ui/message/views/MessageScreenTests.kt @@ -11,10 +11,11 @@ package com.adobe.marketing.mobile.services.ui.message.views +import android.app.Activity import android.view.View import androidx.activity.ComponentActivity import androidx.compose.ui.geometry.Offset -import androidx.compose.ui.platform.LocalConfiguration +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.click @@ -421,19 +422,17 @@ class MessageScreenTests { .content(HTML_TEXT_SAMPLE) .build() - var screenHeightDp = 0.dp - var screenWidthDp = 0.dp - var activityHeightDp = 0.dp - var activityWidthDp = 0.dp + var contentViewHeightDp = 0.dp + var contentViewWidthDp = 0.dp + var messageContentHeightDp = 0.dp + var messageContentWidthDp = 0.dp composeTestRule.setContent { // setting our composable as content for test - val currentConfiguration = LocalConfiguration.current - screenHeightDp = currentConfiguration.screenHeightDp.dp - screenWidthDp = currentConfiguration.screenWidthDp.dp - - val activityRoot = - composeTestRule.activity.window.decorView.findViewById(android.R.id.content) - activityHeightDp = with(LocalDensity.current) { activityRoot.height.toDp() } - activityWidthDp = with(LocalDensity.current) { activityRoot.width.toDp() } + val activity = LocalContext.current as Activity + val contentView = activity.findViewById(android.R.id.content) + contentViewHeightDp = with(LocalDensity.current) { contentView.height.toDp() } + contentViewWidthDp = with(LocalDensity.current) { contentView.width.toDp() } + messageContentHeightDp = ((contentViewHeightDp * settings.height) / 100) + messageContentWidthDp = ((contentViewWidthDp * settings.width) / 100) MessageScreen( presentationStateManager = presentationStateManager, @@ -460,12 +459,12 @@ class MessageScreenTests { // Message Frame all the available height and width allowed by the parent (in this case ComponentActivity) val frameBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_FRAME).getUnclippedBoundsInRoot() - validateViewSize(frameBounds, activityHeightDp, screenWidthDp) + validateViewSize(frameBounds, contentViewHeightDp, contentViewWidthDp) // Message Content(WebView) is 100% of height and width, as allowed by the activity val contentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT) .getUnclippedBoundsInRoot() - validateViewSize(contentBounds, activityHeightDp, screenWidthDp) + validateViewSize(contentBounds, messageContentHeightDp, messageContentWidthDp) } @Test @@ -478,19 +477,16 @@ class MessageScreenTests { .content(HTML_TEXT_SAMPLE) .build() - var screenHeightDp = 0.dp - var screenWidthDp = 0.dp - var activityHeightDp = 0.dp - var activityWidthDp = 0.dp + var contentViewHeightDp = 0.dp + var contentViewWidthDp = 0.dp + var messageContentHeightDp = 0.dp composeTestRule.setContent { // setting our composable as content for test - val currentConfiguration = LocalConfiguration.current - screenHeightDp = currentConfiguration.screenHeightDp.dp - screenWidthDp = currentConfiguration.screenWidthDp.dp - - val activityRoot = - composeTestRule.activity.window.decorView.findViewById(android.R.id.content) - activityHeightDp = with(LocalDensity.current) { activityRoot.height.toDp() } - activityWidthDp = with(LocalDensity.current) { activityRoot.width.toDp() } + val activity = LocalContext.current as Activity + val contentView = activity.findViewById(android.R.id.content) + contentViewHeightDp = with(LocalDensity.current) { contentView.height.toDp() } + contentViewWidthDp = with(LocalDensity.current) { contentView.width.toDp() } + messageContentHeightDp = ((contentViewHeightDp * settings.height) / 100) + MessageScreen( presentationStateManager = presentationStateManager, inAppMessageSettings = settings, @@ -516,23 +512,23 @@ class MessageScreenTests { // Message Frame all the available height and width allowed by the parent (in this case ComponentActivity) val frameBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_FRAME).getUnclippedBoundsInRoot() - validateViewSize(frameBounds, activityHeightDp, screenWidthDp) + validateViewSize(frameBounds, contentViewHeightDp, contentViewWidthDp) // Message Content(WebView) is 95% of the screen height and heightPercent% of the screen width. // If the height exceeds what is allowed by the activity (due to actionbar), it takes up the full height of the activity val contentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT) .getUnclippedBoundsInRoot() - if ((screenHeightDp * (heightPercentage / 100f)) > activityHeightDp) { + if ((contentViewHeightDp * (heightPercentage / 100f)) > messageContentHeightDp) { validateViewSize( contentBounds, - activityHeightDp, - screenWidthDp * (widthPercentage / 100f) + messageContentHeightDp, + contentViewWidthDp * (widthPercentage / 100f) ) } else { validateViewSize( contentBounds, - screenHeightDp * (heightPercentage / 100f), - screenWidthDp * (widthPercentage / 100f) + contentViewHeightDp * (heightPercentage / 100f), + contentViewWidthDp * (widthPercentage / 100f) ) } } @@ -551,19 +547,16 @@ class MessageScreenTests { .verticalAlignment(InAppMessageSettings.MessageAlignment.TOP) .build() - var screenHeightDp = 0.dp - var screenWidthDp = 0.dp - var activityHeightDp = 0.dp - var activityWidthDp = 0.dp + var contentViewHeightDp = 0.dp + var contentViewWidthDp = 0.dp + var messageContentHeightDp = 0.dp composeTestRule.setContent { // setting our composable as content for test - val currentConfiguration = LocalConfiguration.current - screenHeightDp = currentConfiguration.screenHeightDp.dp - screenWidthDp = currentConfiguration.screenWidthDp.dp - - val activityRoot = - composeTestRule.activity.window.decorView.findViewById(android.R.id.content) - activityHeightDp = with(LocalDensity.current) { activityRoot.height.toDp() } - activityWidthDp = with(LocalDensity.current) { activityRoot.width.toDp() } + val activity = LocalContext.current as Activity + val contentView = activity.findViewById(android.R.id.content) + contentViewHeightDp = with(LocalDensity.current) { contentView.height.toDp() } + contentViewWidthDp = with(LocalDensity.current) { contentView.width.toDp() } + messageContentHeightDp = ((contentViewHeightDp * settings.height) / 100) + MessageScreen( presentationStateManager = presentationStateManager, inAppMessageSettings = settings, @@ -591,8 +584,7 @@ class MessageScreenTests { composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_FRAME).getBoundsInRoot() val contentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT).getBoundsInRoot() - val contentHeightDp = screenHeightDp * heightPercent.toFloat() / 100f - val horizontalContentPaddingDp = (screenWidthDp * (100 - widthPercent).toFloat() / 100f) / 2 + val horizontalContentPaddingDp = (contentViewWidthDp * (100 - widthPercent).toFloat() / 100f) / 2 // Frame is same size as its parent so bounds should match the root bounds validateBounds( @@ -607,7 +599,7 @@ class MessageScreenTests { validateBounds( contentBounds, rootBounds.top, // top bound is same as the top bound of root - rootBounds.top + contentHeightDp, // bottom bound is top bound plus content height + rootBounds.top + messageContentHeightDp, // bottom bound is top bound plus content height rootBounds.left + horizontalContentPaddingDp, // left bound is left bound of root plus content padding rootBounds.right - horizontalContentPaddingDp // right bound is right bound of root minus content padding ) @@ -624,19 +616,16 @@ class MessageScreenTests { .verticalAlignment(InAppMessageSettings.MessageAlignment.BOTTOM) .build() - var screenHeightDp = 0.dp - var screenWidthDp = 0.dp - var activityHeightDp = 0.dp - var activityWidthDp = 0.dp + var contentViewHeightDp = 0.dp + var contentViewWidthDp = 0.dp + var messageContentHeightDp = 0.dp composeTestRule.setContent { // setting our composable as content for test - val currentConfiguration = LocalConfiguration.current - screenHeightDp = currentConfiguration.screenHeightDp.dp - screenWidthDp = currentConfiguration.screenWidthDp.dp - - val activityRoot = - composeTestRule.activity.window.decorView.findViewById(android.R.id.content) - activityHeightDp = with(LocalDensity.current) { activityRoot.height.toDp() } - activityWidthDp = with(LocalDensity.current) { activityRoot.width.toDp() } + val activity = LocalContext.current as Activity + val contentView = activity.findViewById(android.R.id.content) + contentViewHeightDp = with(LocalDensity.current) { contentView.height.toDp() } + contentViewWidthDp = with(LocalDensity.current) { contentView.width.toDp() } + messageContentHeightDp = ((contentViewHeightDp * settings.height) / 100) + MessageScreen( presentationStateManager = presentationStateManager, inAppMessageSettings = settings, @@ -664,8 +653,7 @@ class MessageScreenTests { composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_FRAME).getBoundsInRoot() val contentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT).getBoundsInRoot() - val contentHeightDp = screenHeightDp * heightPercent.toFloat() / 100f - val horizontalContentPaddingDp = (screenWidthDp * (100 - widthPercent).toFloat() / 100f) / 2 + val horizontalContentPaddingDp = (contentViewWidthDp * (100 - widthPercent).toFloat() / 100f) / 2 // Frame is same size as its parent so bounds should match the root bounds validateBounds( @@ -679,7 +667,7 @@ class MessageScreenTests { // Content is bottom aligned vertically and centered horizontally and takes 80% of screen width validateBounds( contentBounds, - rootBounds.bottom - contentHeightDp, // top bound is bottom bound minus content height + rootBounds.bottom - messageContentHeightDp, // top bound is bottom bound minus content height rootBounds.bottom, // bottom bound is same as the bottom bound of root rootBounds.left + horizontalContentPaddingDp, // left bound is left bound of root plus content padding rootBounds.right - horizontalContentPaddingDp // right bound is right bound of root minus content padding @@ -697,19 +685,18 @@ class MessageScreenTests { .horizontalAlignment(InAppMessageSettings.MessageAlignment.LEFT) .build() - var screenHeightDp = 0.dp - var screenWidthDp = 0.dp - var activityHeightDp = 0.dp - var activityWidthDp = 0.dp + var contentViewHeightDp = 0.dp + var contentViewWidthDp = 0.dp + var messageContentHeightDp = 0.dp + var messageContentWidthDp = 0.dp composeTestRule.setContent { // setting our composable as content for test - val currentConfiguration = LocalConfiguration.current - screenHeightDp = currentConfiguration.screenHeightDp.dp - screenWidthDp = currentConfiguration.screenWidthDp.dp - - val activityRoot = - composeTestRule.activity.window.decorView.findViewById(android.R.id.content) - activityHeightDp = with(LocalDensity.current) { activityRoot.height.toDp() } - activityWidthDp = with(LocalDensity.current) { activityRoot.width.toDp() } + val activity = LocalContext.current as Activity + val contentView = activity.findViewById(android.R.id.content) + contentViewHeightDp = with(LocalDensity.current) { contentView.height.toDp() } + contentViewWidthDp = with(LocalDensity.current) { contentView.width.toDp() } + messageContentHeightDp = ((contentViewHeightDp * settings.height) / 100) + messageContentWidthDp = ((contentViewWidthDp * settings.width) / 100) + MessageScreen( presentationStateManager = presentationStateManager, inAppMessageSettings = settings, @@ -737,8 +724,6 @@ class MessageScreenTests { composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_FRAME).getBoundsInRoot() val contentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT).getBoundsInRoot() - val contentHeightDp = screenHeightDp * heightPercent.toFloat() / 100f - val contentWidth = screenWidthDp * widthPercent.toFloat() / 100f // Frame is same size as its parent so bounds should match the root bounds validateBounds( @@ -752,10 +737,10 @@ class MessageScreenTests { // Content is center aligned vertically and left aligned horizontally and takes heightPercent% of screen height validateBounds( contentBounds, - rootBounds.top + ((activityHeightDp - contentHeightDp) / 2), // top bound is half of the difference between the activity height and the content height - rootBounds.bottom - ((activityHeightDp - contentHeightDp) / 2), // top bound is the remaining half of the difference between the activity height and the content height + rootBounds.top + ((contentViewHeightDp - messageContentHeightDp) / 2), // top bound is half of the difference between the activity height and the content height + rootBounds.bottom - ((contentViewHeightDp - messageContentHeightDp) / 2), // top bound is the remaining half of the difference between the activity height and the content height rootBounds.left, // left bound is same as left bound of the root - rootBounds.left + contentWidth // right bound is left bound plus width of content + rootBounds.left + messageContentWidthDp // right bound is left bound plus width of content ) } @@ -770,19 +755,18 @@ class MessageScreenTests { .horizontalAlignment(InAppMessageSettings.MessageAlignment.RIGHT) .build() - var screenHeightDp = 0.dp - var screenWidthDp = 0.dp - var activityHeightDp = 0.dp - var activityWidthDp = 0.dp + var contentViewHeightDp = 0.dp + var contentViewWidthDp = 0.dp + var messageContentHeightDp = 0.dp + var messageContentWidthDp = 0.dp composeTestRule.setContent { // setting our composable as content for test - val currentConfiguration = LocalConfiguration.current - screenHeightDp = currentConfiguration.screenHeightDp.dp - screenWidthDp = currentConfiguration.screenWidthDp.dp - - val activityRoot = - composeTestRule.activity.window.decorView.findViewById(android.R.id.content) - activityHeightDp = with(LocalDensity.current) { activityRoot.height.toDp() } - activityWidthDp = with(LocalDensity.current) { activityRoot.width.toDp() } + val activity = LocalContext.current as Activity + val contentView = activity.findViewById(android.R.id.content) + contentViewHeightDp = with(LocalDensity.current) { contentView.height.toDp() } + contentViewWidthDp = with(LocalDensity.current) { contentView.width.toDp() } + messageContentHeightDp = ((contentViewHeightDp * settings.height) / 100) + messageContentWidthDp = ((contentViewWidthDp * settings.width) / 100) + MessageScreen( presentationStateManager = presentationStateManager, inAppMessageSettings = settings, @@ -810,8 +794,6 @@ class MessageScreenTests { composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_FRAME).getBoundsInRoot() val contentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT).getBoundsInRoot() - val contentHeightDp = screenHeightDp * heightPercent.toFloat() / 100f - val contentWidth = screenWidthDp * widthPercent.toFloat() / 100f // Frame is same size as its parent so bounds should match the root bounds validateBounds( @@ -825,9 +807,9 @@ class MessageScreenTests { // Content is center aligned vertically and right aligned horizontally and takes heightPercent% of screen height validateBounds( contentBounds, - rootBounds.top + ((activityHeightDp - contentHeightDp) / 2), // top bound is top bound of root plus half of the difference between the activity height and the content height - rootBounds.bottom - ((activityHeightDp - contentHeightDp) / 2), // bottom bound is bottom bound of root plys the remaining half of the difference between the activity height and the content height - rootBounds.right - contentWidth, // left bound is right bound minus width of content + rootBounds.top + ((contentViewHeightDp - messageContentHeightDp) / 2), // top bound is top bound of root plus half of the difference between the activity height and the content height + rootBounds.bottom - ((contentViewHeightDp - messageContentHeightDp) / 2), // bottom bound is bottom bound of root plys the remaining half of the difference between the activity height and the content height + rootBounds.right - messageContentWidthDp, // left bound is right bound minus width of content rootBounds.right // right bound is same as right bound of the root ) } @@ -844,19 +826,18 @@ class MessageScreenTests { .horizontalAlignment(InAppMessageSettings.MessageAlignment.RIGHT) .build() - var screenHeightDp = 0.dp - var screenWidthDp = 0.dp - var activityHeightDp = 0.dp - var activityWidthDp = 0.dp + var contentViewHeightDp = 0.dp + var contentViewWidthDp = 0.dp + var messageContentHeightDp = 0.dp + var messageContentWidthDp = 0.dp composeTestRule.setContent { // setting our composable as content for test - val currentConfiguration = LocalConfiguration.current - screenHeightDp = currentConfiguration.screenHeightDp.dp - screenWidthDp = currentConfiguration.screenWidthDp.dp - - val activityRoot = - composeTestRule.activity.window.decorView.findViewById(android.R.id.content) - activityHeightDp = with(LocalDensity.current) { activityRoot.height.toDp() } - activityWidthDp = with(LocalDensity.current) { activityRoot.width.toDp() } + val activity = LocalContext.current as Activity + val contentView = activity.findViewById(android.R.id.content) + contentViewHeightDp = with(LocalDensity.current) { contentView.height.toDp() } + contentViewWidthDp = with(LocalDensity.current) { contentView.width.toDp() } + messageContentHeightDp = ((contentViewHeightDp * settings.height) / 100) + messageContentWidthDp = ((contentViewWidthDp * settings.width) / 100) + MessageScreen( presentationStateManager = presentationStateManager, inAppMessageSettings = settings, @@ -884,8 +865,6 @@ class MessageScreenTests { composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_FRAME).getBoundsInRoot() val contentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT).getBoundsInRoot() - val contentHeightDp = screenHeightDp * heightPercent.toFloat() / 100f - val contentWidth = screenWidthDp * widthPercent.toFloat() / 100f // Frame is same size as its parent so bounds should match the root bounds validateBounds( @@ -899,9 +878,9 @@ class MessageScreenTests { // Content is center aligned vertically and right aligned horizontally and takes heightPercent% of screen height validateBounds( contentBounds, - rootBounds.bottom - contentHeightDp, // top bound is bottom bound minus content height + rootBounds.bottom - messageContentHeightDp, // top bound is bottom bound minus content height rootBounds.bottom, // bottom bound is same as the bottom bound of root - rootBounds.right - contentWidth, // left bound is right bound minus width of content + rootBounds.right - messageContentWidthDp, // left bound is right bound minus width of content rootBounds.right // right bound is same as right bound of the root ) } @@ -922,19 +901,16 @@ class MessageScreenTests { .verticalInset(offsetPercent) .build() - var screenHeightDp = 0.dp - var screenWidthDp = 0.dp - var activityHeightDp = 0.dp - var activityWidthDp = 0.dp + var contentViewHeightDp = 0.dp + var contentViewWidthDp = 0.dp + var messageContentHeightDp = 0.dp composeTestRule.setContent { // setting our composable as content for test - val currentConfiguration = LocalConfiguration.current - screenHeightDp = currentConfiguration.screenHeightDp.dp - screenWidthDp = currentConfiguration.screenWidthDp.dp - - val activityRoot = - composeTestRule.activity.window.decorView.findViewById(android.R.id.content) - activityHeightDp = with(LocalDensity.current) { activityRoot.height.toDp() } - activityWidthDp = with(LocalDensity.current) { activityRoot.width.toDp() } + val activity = LocalContext.current as Activity + val contentView = activity.findViewById(android.R.id.content) + contentViewHeightDp = with(LocalDensity.current) { contentView.height.toDp() } + contentViewWidthDp = with(LocalDensity.current) { contentView.width.toDp() } + messageContentHeightDp = ((contentViewHeightDp * settings.height) / 100) + MessageScreen( presentationStateManager = presentationStateManager, inAppMessageSettings = settings, @@ -962,9 +938,8 @@ class MessageScreenTests { composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_FRAME).getUnclippedBoundsInRoot() val contentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT) .getUnclippedBoundsInRoot() - val contentHeightDp = screenHeightDp * heightPercent.toFloat() / 100f - val horizontalContentPaddingDp = (screenWidthDp * (100 - widthPercent).toFloat() / 100f) / 2 - val offsetDp = screenHeightDp * offsetPercent.toFloat() / 100f + val horizontalContentPaddingDp = (contentViewWidthDp * (100 - widthPercent).toFloat() / 100f) / 2 + val offsetDp = messageContentHeightDp * offsetPercent.toFloat() / 100f // Frame top and bottom bounds are offset downwards by the inset value from the top validateBounds( @@ -979,7 +954,7 @@ class MessageScreenTests { validateBounds( contentBounds, rootBounds.top + offsetDp, // top bound is offset downwards by the inset value from the top bound of the root - rootBounds.top + contentHeightDp + offsetDp, // bottom bound is the top bound of the root plus content height offset downwards by the inset value + rootBounds.top + messageContentHeightDp + offsetDp, // bottom bound is the top bound of the root plus content height offset downwards by the inset value rootBounds.left + horizontalContentPaddingDp, // left bound is left bound of root plus content padding rootBounds.right - horizontalContentPaddingDp // right bound is right bound of root minus content padding ) @@ -998,19 +973,16 @@ class MessageScreenTests { .verticalInset(-offsetPercent) .build() - var screenHeightDp = 0.dp - var screenWidthDp = 0.dp - var activityHeightDp = 0.dp - var activityWidthDp = 0.dp + var contentViewHeightDp = 0.dp + var contentViewWidthDp = 0.dp + var messageContentHeightDp = 0.dp composeTestRule.setContent { // setting our composable as content for test - val currentConfiguration = LocalConfiguration.current - screenHeightDp = currentConfiguration.screenHeightDp.dp - screenWidthDp = currentConfiguration.screenWidthDp.dp - - val activityRoot = - composeTestRule.activity.window.decorView.findViewById(android.R.id.content) - activityHeightDp = with(LocalDensity.current) { activityRoot.height.toDp() } - activityWidthDp = with(LocalDensity.current) { activityRoot.width.toDp() } + val activity = LocalContext.current as Activity + val contentView = activity.findViewById(android.R.id.content) + contentViewHeightDp = with(LocalDensity.current) { contentView.height.toDp() } + contentViewWidthDp = with(LocalDensity.current) { contentView.width.toDp() } + messageContentHeightDp = ((contentViewHeightDp * settings.height) / 100) + MessageScreen( presentationStateManager = presentationStateManager, inAppMessageSettings = settings, @@ -1038,9 +1010,8 @@ class MessageScreenTests { composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_FRAME).getUnclippedBoundsInRoot() val contentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT) .getUnclippedBoundsInRoot() - val contentHeightDp = screenHeightDp * heightPercent.toFloat() / 100f - val horizontalContentPaddingDp = (screenWidthDp * (100 - widthPercent).toFloat() / 100f) / 2 - val offsetDp = screenHeightDp * offsetPercent.toFloat() / 100f + val horizontalContentPaddingDp = (contentViewWidthDp * (100 - widthPercent).toFloat() / 100f) / 2 + val offsetDp = messageContentHeightDp * offsetPercent.toFloat() / 100f // Frame top and bottom bounds are offset upwards by the inset value from the top validateBounds( @@ -1055,7 +1026,7 @@ class MessageScreenTests { validateBounds( contentBounds, rootBounds.top - offsetDp, // top bound is offset upwards by the inset value from the top bound of the root - rootBounds.top + contentHeightDp - offsetDp, // bottom bound is the top bound of the root plus content height offset upwards by the inset value + rootBounds.top + messageContentHeightDp - offsetDp, // bottom bound is the top bound of the root plus content height offset upwards by the inset value rootBounds.left + horizontalContentPaddingDp, // left bound is left bound of root plus content padding rootBounds.right - horizontalContentPaddingDp // right bound is right bound of root minus content padding ) @@ -1074,19 +1045,16 @@ class MessageScreenTests { .verticalInset(offsetPercent) .build() - var screenHeightDp = 0.dp - var screenWidthDp = 0.dp - var activityHeightDp = 0.dp - var activityWidthDp = 0.dp + var contentViewHeightDp = 0.dp + var contentViewWidthDp = 0.dp + var messageContentHeightDp = 0.dp composeTestRule.setContent { // setting our composable as content for test - val currentConfiguration = LocalConfiguration.current - screenHeightDp = currentConfiguration.screenHeightDp.dp - screenWidthDp = currentConfiguration.screenWidthDp.dp - - val activityRoot = - composeTestRule.activity.window.decorView.findViewById(android.R.id.content) - activityHeightDp = with(LocalDensity.current) { activityRoot.height.toDp() } - activityWidthDp = with(LocalDensity.current) { activityRoot.width.toDp() } + val activity = LocalContext.current as Activity + val contentView = activity.findViewById(android.R.id.content) + contentViewHeightDp = with(LocalDensity.current) { contentView.height.toDp() } + contentViewWidthDp = with(LocalDensity.current) { contentView.width.toDp() } + messageContentHeightDp = ((contentViewHeightDp * settings.height) / 100) + MessageScreen( presentationStateManager = presentationStateManager, inAppMessageSettings = settings, @@ -1114,9 +1082,8 @@ class MessageScreenTests { composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_FRAME).getUnclippedBoundsInRoot() val contentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT) .getUnclippedBoundsInRoot() - val contentHeightDp = screenHeightDp * heightPercent.toFloat() / 100f - val horizontalContentPaddingDp = (screenWidthDp * (100 - widthPercent).toFloat() / 100f) / 2 - val offsetDp = screenHeightDp * offsetPercent.toFloat() / 100f + val horizontalContentPaddingDp = (contentViewWidthDp * (100 - widthPercent).toFloat() / 100f) / 2 + val offsetDp = messageContentHeightDp * offsetPercent.toFloat() / 100f // Frame top and bottom bounds are offset upwards by the inset value from the bottom validateBounds( @@ -1130,7 +1097,7 @@ class MessageScreenTests { // Content is bottom aligned vertically and offset upwards and centered horizontally and takes 80% of screen width validateBounds( contentBounds, - rootBounds.bottom - contentHeightDp - offsetDp, // top bound is the bottom bound of the root minus content height offset upwards by the inset value + rootBounds.bottom - messageContentHeightDp - offsetDp, // top bound is the bottom bound of the root minus content height offset upwards by the inset value rootBounds.bottom - offsetDp, // bottom bound is the bottom bound of the root offset upwards by the inset value rootBounds.left + horizontalContentPaddingDp, // left bound is left bound of root plus content padding rootBounds.right - horizontalContentPaddingDp // right bound is right bound of root minus content padding @@ -1150,19 +1117,16 @@ class MessageScreenTests { .verticalInset(-offsetPercent) .build() - var screenHeightDp = 0.dp - var screenWidthDp = 0.dp - var activityHeightDp = 0.dp - var activityWidthDp = 0.dp + var contentViewHeightDp = 0.dp + var contentViewWidthDp = 0.dp + var messageContentHeightDp = 0.dp composeTestRule.setContent { // setting our composable as content for test - val currentConfiguration = LocalConfiguration.current - screenHeightDp = currentConfiguration.screenHeightDp.dp - screenWidthDp = currentConfiguration.screenWidthDp.dp - - val activityRoot = - composeTestRule.activity.window.decorView.findViewById(android.R.id.content) - activityHeightDp = with(LocalDensity.current) { activityRoot.height.toDp() } - activityWidthDp = with(LocalDensity.current) { activityRoot.width.toDp() } + val activity = LocalContext.current as Activity + val contentView = activity.findViewById(android.R.id.content) + contentViewHeightDp = with(LocalDensity.current) { contentView.height.toDp() } + contentViewWidthDp = with(LocalDensity.current) { contentView.width.toDp() } + messageContentHeightDp = ((contentViewHeightDp * settings.height) / 100) + MessageScreen( presentationStateManager = presentationStateManager, inAppMessageSettings = settings, @@ -1190,9 +1154,8 @@ class MessageScreenTests { composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_FRAME).getUnclippedBoundsInRoot() val contentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT) .getUnclippedBoundsInRoot() - val contentHeightDp = screenHeightDp * heightPercent.toFloat() / 100f - val horizontalContentPaddingDp = (screenWidthDp * (100 - widthPercent).toFloat() / 100f) / 2 - val offsetDp = screenHeightDp * offsetPercent.toFloat() / 100f + val horizontalContentPaddingDp = (contentViewWidthDp * (100 - widthPercent).toFloat() / 100f) / 2 + val offsetDp = messageContentHeightDp * offsetPercent.toFloat() / 100f // Frame top and bottom bounds are offset downwards by the inset value from the bottom validateBounds( @@ -1206,7 +1169,7 @@ class MessageScreenTests { // Content is bottom aligned vertically and offset upwards and centered horizontally and takes 80% of screen width validateBounds( contentBounds, - rootBounds.bottom - contentHeightDp + offsetDp, // top bound is the bottom bound of the root minus content height offset downwards by the inset value + rootBounds.bottom - messageContentHeightDp + offsetDp, // top bound is the bottom bound of the root minus content height offset downwards by the inset value rootBounds.bottom + offsetDp, // bottom bound is the bottom bound of the root offset downwards by the inset value rootBounds.left + horizontalContentPaddingDp, // left bound is left bound of root plus content padding rootBounds.right - horizontalContentPaddingDp // right bound is right bound of root minus content padding @@ -1226,19 +1189,18 @@ class MessageScreenTests { .horizontalInset(offsetPercent) .build() - var screenHeightDp = 0.dp - var screenWidthDp = 0.dp - var activityHeightDp = 0.dp - var activityWidthDp = 0.dp + var contentViewHeightDp = 0.dp + var contentViewWidthDp = 0.dp + var messageContentHeightDp = 0.dp + var messageContentWidthDp = 0.dp composeTestRule.setContent { // setting our composable as content for test - val currentConfiguration = LocalConfiguration.current - screenHeightDp = currentConfiguration.screenHeightDp.dp - screenWidthDp = currentConfiguration.screenWidthDp.dp - - val activityRoot = - composeTestRule.activity.window.decorView.findViewById(android.R.id.content) - activityHeightDp = with(LocalDensity.current) { activityRoot.height.toDp() } - activityWidthDp = with(LocalDensity.current) { activityRoot.width.toDp() } + val activity = LocalContext.current as Activity + val contentView = activity.findViewById(android.R.id.content) + contentViewHeightDp = with(LocalDensity.current) { contentView.height.toDp() } + contentViewWidthDp = with(LocalDensity.current) { contentView.width.toDp() } + messageContentHeightDp = ((contentViewHeightDp * settings.height) / 100) + messageContentWidthDp = ((contentViewWidthDp * settings.width) / 100) + MessageScreen( presentationStateManager = presentationStateManager, inAppMessageSettings = settings, @@ -1266,10 +1228,8 @@ class MessageScreenTests { composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_FRAME).getUnclippedBoundsInRoot() val contentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT) .getUnclippedBoundsInRoot() - val contentHeightDp = screenHeightDp * heightPercent.toFloat() / 100f - val contentWidthDp = screenWidthDp * widthPercent.toFloat() / 100f - val verticalContentPaddingDp = (activityHeightDp - contentHeightDp) / 2 - val offsetDp = screenWidthDp * offsetPercent.toFloat() / 100f + val verticalContentPaddingDp = (contentViewHeightDp - messageContentHeightDp) / 2 + val offsetDp = messageContentWidthDp * offsetPercent.toFloat() / 100f // Frame left and right bounds are offset leftwards by the inset value from the left validateBounds( @@ -1286,7 +1246,7 @@ class MessageScreenTests { rootBounds.top + verticalContentPaddingDp, // top bound is the top round of the root plus content padding rootBounds.bottom - verticalContentPaddingDp, // bottom bound is the bottom bound of the root minus content padding rootBounds.left + offsetDp, // left bound is the left bound of the root offset leftwards by the inset value - rootBounds.left + contentWidthDp + offsetDp // right bound is left bound of root plus content width offset leftwards by the inset value + rootBounds.left + messageContentWidthDp + offsetDp // right bound is left bound of root plus content width offset leftwards by the inset value ) } @@ -1303,19 +1263,18 @@ class MessageScreenTests { .horizontalInset(-offsetPercent) .build() - var screenHeightDp = 0.dp - var screenWidthDp = 0.dp - var activityHeightDp = 0.dp - var activityWidthDp = 0.dp + var contentViewHeightDp = 0.dp + var contentViewWidthDp = 0.dp + var messageContentHeightDp = 0.dp + var messageContentWidthDp = 0.dp composeTestRule.setContent { // setting our composable as content for test - val currentConfiguration = LocalConfiguration.current - screenHeightDp = currentConfiguration.screenHeightDp.dp - screenWidthDp = currentConfiguration.screenWidthDp.dp - - val activityRoot = - composeTestRule.activity.window.decorView.findViewById(android.R.id.content) - activityHeightDp = with(LocalDensity.current) { activityRoot.height.toDp() } - activityWidthDp = with(LocalDensity.current) { activityRoot.width.toDp() } + val activity = LocalContext.current as Activity + val contentView = activity.findViewById(android.R.id.content) + contentViewHeightDp = with(LocalDensity.current) { contentView.height.toDp() } + contentViewWidthDp = with(LocalDensity.current) { contentView.width.toDp() } + messageContentHeightDp = ((contentViewHeightDp * settings.height) / 100) + messageContentWidthDp = ((contentViewWidthDp * settings.width) / 100) + MessageScreen( presentationStateManager = presentationStateManager, inAppMessageSettings = settings, @@ -1343,10 +1302,8 @@ class MessageScreenTests { composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_FRAME).getUnclippedBoundsInRoot() val contentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT) .getUnclippedBoundsInRoot() - val contentHeightDp = screenHeightDp * heightPercent.toFloat() / 100f - val contentWidthDp = screenWidthDp * widthPercent.toFloat() / 100f - val verticalContentPaddingDp = (activityHeightDp - contentHeightDp) / 2 - val offsetDp = screenWidthDp * offsetPercent.toFloat() / 100f + val verticalContentPaddingDp = (contentViewHeightDp - messageContentHeightDp) / 2 + val offsetDp = messageContentWidthDp * offsetPercent.toFloat() / 100f // Frame left and right bounds are offset rightwards by the inset value from the left validateBounds( @@ -1363,7 +1320,7 @@ class MessageScreenTests { rootBounds.top + verticalContentPaddingDp, // top bound is the top round of the root plus content padding rootBounds.bottom - verticalContentPaddingDp, // bottom bound is the bottom bound of the root minus content padding rootBounds.left - offsetDp, // left bound is the left bound of the root offset rightwards by the inset value - rootBounds.left + contentWidthDp - offsetDp // right bound is left bound of root plus content width offset rightwards by the inset value + rootBounds.left + messageContentWidthDp - offsetDp // right bound is left bound of root plus content width offset rightwards by the inset value ) } @@ -1380,19 +1337,18 @@ class MessageScreenTests { .horizontalInset(offsetPercent) .build() - var screenHeightDp = 0.dp - var screenWidthDp = 0.dp - var activityHeightDp = 0.dp - var activityWidthDp = 0.dp + var contentViewHeightDp = 0.dp + var contentViewWidthDp = 0.dp + var messageContentHeightDp = 0.dp + var messageContentWidthDp = 0.dp composeTestRule.setContent { // setting our composable as content for test - val currentConfiguration = LocalConfiguration.current - screenHeightDp = currentConfiguration.screenHeightDp.dp - screenWidthDp = currentConfiguration.screenWidthDp.dp - - val activityRoot = - composeTestRule.activity.window.decorView.findViewById(android.R.id.content) - activityHeightDp = with(LocalDensity.current) { activityRoot.height.toDp() } - activityWidthDp = with(LocalDensity.current) { activityRoot.width.toDp() } + val activity = LocalContext.current as Activity + val contentView = activity.findViewById(android.R.id.content) + contentViewHeightDp = with(LocalDensity.current) { contentView.height.toDp() } + contentViewWidthDp = with(LocalDensity.current) { contentView.width.toDp() } + messageContentHeightDp = ((contentViewHeightDp * settings.height) / 100) + messageContentWidthDp = ((contentViewWidthDp * settings.width) / 100) + MessageScreen( presentationStateManager = presentationStateManager, inAppMessageSettings = settings, @@ -1420,10 +1376,8 @@ class MessageScreenTests { composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_FRAME).getUnclippedBoundsInRoot() val contentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT) .getUnclippedBoundsInRoot() - val contentHeightDp = screenHeightDp * heightPercent.toFloat() / 100f - val contentWidthDp = screenWidthDp * widthPercent.toFloat() / 100f - val verticalContentPaddingDp = (activityHeightDp - contentHeightDp) / 2 - val offsetDp = screenWidthDp * offsetPercent.toFloat() / 100f + val verticalContentPaddingDp = (contentViewHeightDp - messageContentHeightDp) / 2 + val offsetDp = messageContentWidthDp * offsetPercent.toFloat() / 100f // Frame left and right bounds are offset leftwards by the inset value from the right validateBounds( @@ -1439,7 +1393,7 @@ class MessageScreenTests { contentBounds, rootBounds.top + verticalContentPaddingDp, // top bound is the top round of the root plus content padding rootBounds.bottom - verticalContentPaddingDp, // bottom bound is the bottom bound of the root minus content padding - rootBounds.right - contentWidthDp - offsetDp, // left bound is the right bound of the root minus content width offset leftwards by the inset value + rootBounds.right - messageContentWidthDp - offsetDp, // left bound is the right bound of the root minus content width offset leftwards by the inset value rootBounds.right - offsetDp // right bound is right bound of root offset leftwards by the inset value ) } @@ -1457,19 +1411,18 @@ class MessageScreenTests { .horizontalInset(-offsetPercent) .build() - var screenHeightDp = 0.dp - var screenWidthDp = 0.dp - var activityHeightDp = 0.dp - var activityWidthDp = 0.dp + var contentViewHeightDp = 0.dp + var contentViewWidthDp = 0.dp + var messageContentHeightDp = 0.dp + var messageContentWidthDp = 0.dp composeTestRule.setContent { // setting our composable as content for test - val currentConfiguration = LocalConfiguration.current - screenHeightDp = currentConfiguration.screenHeightDp.dp - screenWidthDp = currentConfiguration.screenWidthDp.dp - - val activityRoot = - composeTestRule.activity.window.decorView.findViewById(android.R.id.content) - activityHeightDp = with(LocalDensity.current) { activityRoot.height.toDp() } - activityWidthDp = with(LocalDensity.current) { activityRoot.width.toDp() } + val activity = LocalContext.current as Activity + val contentView = activity.findViewById(android.R.id.content) + contentViewHeightDp = with(LocalDensity.current) { contentView.height.toDp() } + contentViewWidthDp = with(LocalDensity.current) { contentView.width.toDp() } + messageContentHeightDp = ((contentViewHeightDp * settings.height) / 100) + messageContentWidthDp = ((contentViewWidthDp * settings.width) / 100) + MessageScreen( presentationStateManager = presentationStateManager, inAppMessageSettings = settings, @@ -1497,10 +1450,8 @@ class MessageScreenTests { composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_FRAME).getUnclippedBoundsInRoot() val contentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT) .getUnclippedBoundsInRoot() - val contentHeightDp = screenHeightDp * heightPercent.toFloat() / 100f - val contentWidthDp = screenWidthDp * widthPercent.toFloat() / 100f - val verticalContentPaddingDp = (activityHeightDp - contentHeightDp) / 2 - val offsetDp = screenWidthDp * offsetPercent.toFloat() / 100f + val verticalContentPaddingDp = (contentViewHeightDp - messageContentHeightDp) / 2 + val offsetDp = messageContentWidthDp * offsetPercent.toFloat() / 100f // Frame left and right bounds are offset rightwards by the inset value from the right validateBounds( @@ -1516,7 +1467,7 @@ class MessageScreenTests { contentBounds, rootBounds.top + verticalContentPaddingDp, // top bound is the top round of the root plus content padding rootBounds.bottom - verticalContentPaddingDp, // bottom bound is the bottom bound of the root minus content padding - rootBounds.right - contentWidthDp + offsetDp, // left bound is the right bound of the root minus content width offset rightwards by the inset value + rootBounds.right - messageContentWidthDp + offsetDp, // left bound is the right bound of the root minus content width offset rightwards by the inset value rootBounds.right + offsetDp // right bound is right bound of root offset rightwards by the inset value ) } @@ -1536,19 +1487,18 @@ class MessageScreenTests { .horizontalInset(-offsetPercent) .build() - var screenHeightDp = 0.dp - var screenWidthDp = 0.dp - var activityHeightDp = 0.dp - var activityWidthDp = 0.dp + var contentViewHeightDp = 0.dp + var contentViewWidthDp = 0.dp + var messageContentHeightDp = 0.dp + var messageContentWidthDp = 0.dp composeTestRule.setContent { // setting our composable as content for test - val currentConfiguration = LocalConfiguration.current - screenHeightDp = currentConfiguration.screenHeightDp.dp - screenWidthDp = currentConfiguration.screenWidthDp.dp - - val activityRoot = - composeTestRule.activity.window.decorView.findViewById(android.R.id.content) - activityHeightDp = with(LocalDensity.current) { activityRoot.height.toDp() } - activityWidthDp = with(LocalDensity.current) { activityRoot.width.toDp() } + val activity = LocalContext.current as Activity + val contentView = activity.findViewById(android.R.id.content) + contentViewHeightDp = with(LocalDensity.current) { contentView.height.toDp() } + contentViewWidthDp = with(LocalDensity.current) { contentView.width.toDp() } + messageContentHeightDp = ((contentViewHeightDp * settings.height) / 100) + messageContentWidthDp = ((contentViewWidthDp * settings.width) / 100) + MessageScreen( presentationStateManager = presentationStateManager, inAppMessageSettings = settings, @@ -1576,17 +1526,15 @@ class MessageScreenTests { composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_FRAME).getUnclippedBoundsInRoot() val contentBounds = composeTestRule.onNodeWithTag(MessageTestTags.MESSAGE_CONTENT) .getUnclippedBoundsInRoot() - val contentHeightDp = screenHeightDp * heightPercent.toFloat() / 100f - val contentWidthDp = screenWidthDp * widthPercent.toFloat() / 100f - val heightOffset = screenHeightDp * offsetPercent.toFloat() / 100f - val widthOffsetDp = screenWidthDp * offsetPercent.toFloat() / 100f + val heightOffsetDp = messageContentHeightDp * offsetPercent.toFloat() / 100f + val widthOffsetDp = messageContentWidthDp * offsetPercent.toFloat() / 100f // Frame top and bottom bounds are offset upwards by the inset from the bottom // left and right bounds are offset rightwards by the inset value from the right validateBounds( frameBounds, - rootBounds.top - heightOffset, - rootBounds.bottom - heightOffset, + rootBounds.top - heightOffsetDp, + rootBounds.bottom - heightOffsetDp, rootBounds.left + widthOffsetDp, rootBounds.right + widthOffsetDp ) @@ -1594,9 +1542,9 @@ class MessageScreenTests { // Content is bottom aligned vertically and offset upwards and centered horizontally and takes 80% of screen width validateBounds( contentBounds, - rootBounds.bottom - contentHeightDp - heightOffset, // top bound is the bottom bound of the root minus content height offset upwards by the inset value - rootBounds.bottom - heightOffset, // bottom bound is the bottom bound of the root offset upwards by the inset value - rootBounds.right - contentWidthDp + widthOffsetDp, // left bound is the right bound of the root minus content width offset rightwards by the inset value + rootBounds.bottom - messageContentHeightDp - heightOffsetDp, // top bound is the bottom bound of the root minus content height offset upwards by the inset value + rootBounds.bottom - heightOffsetDp, // bottom bound is the bottom bound of the root offset upwards by the inset value + rootBounds.right - messageContentWidthDp + widthOffsetDp, // left bound is the right bound of the root minus content width offset rightwards by the inset value rootBounds.right + widthOffsetDp // right bound is right bound of root offset rightwards by the inset value ) } diff --git a/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageFrame.kt b/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageFrame.kt index 9ada3eb43..bee6e4cdd 100644 --- a/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageFrame.kt +++ b/code/core/src/phone/java/com/adobe/marketing/mobile/services/ui/message/views/MessageFrame.kt @@ -84,8 +84,8 @@ internal fun MessageFrame( val contentView = currentActivity.findViewById(android.R.id.content) val contentHeightDp = with(LocalDensity.current) { contentView.height.toDp() } val contentWidthDp = with(LocalDensity.current) { contentView.width.toDp() } - val heightDp = remember { ((contentHeightDp * inAppMessageSettings.height) / 100) } - val widthDp = remember { ((contentWidthDp * inAppMessageSettings.width) / 100) } + val heightDp = ((contentHeightDp * inAppMessageSettings.height) / 100) + val widthDp = ((contentWidthDp * inAppMessageSettings.width) / 100) val horizontalOffset = MessageOffsetMapper.getHorizontalOffset( inAppMessageSettings.horizontalAlignment,