Skip to content

Commit

Permalink
Merge pull request #596 from adobe/dev-v2.6.1-core
Browse files Browse the repository at this point in the history
  • Loading branch information
praveek authored Dec 13, 2023
2 parents 65b082a + 511bb67 commit cc16023
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ package com.adobe.marketing.mobile.internal

internal object CoreConstants {
const val LOG_TAG = "MobileCore"
const val VERSION = "2.6.0"
const val VERSION = "2.6.1"

object EventDataKeys {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class AEPMessage implements FullscreenMessage {
private static final String FRAGMENT_TAG = "AEPMessageFragment";
private static final String UNEXPECTED_NULL_VALUE = "Unexpected Null Value";
private static final int ANIMATION_DURATION = 300;

private static final int WEBVIEW_CREATION_MAX_TRIES = 5;
private static final String UTF_8 = "UTF-8";

// package private vars
Expand Down Expand Up @@ -201,7 +203,20 @@ public void show(final boolean withMessagingDelegateControl) {

// create the webview if needed
if (webView == null) {
webView = createWebView();
for (int i = 0; i < WEBVIEW_CREATION_MAX_TRIES; i++) {
webView = createWebView();

if (webView != null) {
// exit loop if webview creation was successful
break;
}
}

if (webView == null) {
// unable to create the webview, need to call failure logic and bail.
listener.onShowFailure();
return;
}
}

final Activity currentActivity = getCurrentActivity();
Expand Down Expand Up @@ -487,37 +502,45 @@ private WebView createWebView() {
@SuppressLint("SetJavaScriptEnabled")
final Runnable createWebViewRunnable =
() -> {
final WebView newWebView = new WebView(getApplicationContext());
// assign a random resource id to identify this webview
newWebView.setId(Math.abs(new Random().nextInt()));
newWebView.setVerticalScrollBarEnabled(true);
newWebView.setHorizontalScrollBarEnabled(true);
newWebView.setScrollbarFadingEnabled(true);
newWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
newWebView.setBackgroundColor(Color.TRANSPARENT);

webViewClient = new MessageWebViewClient(AEPMessage.this);
webViewClient.setLocalAssetsMap(assetMap);
newWebView.setWebViewClient(webViewClient);

final WebSettings webviewSettings = newWebView.getSettings();
webviewSettings.setJavaScriptEnabled(true);
webviewSettings.setAllowFileAccess(false);
webviewSettings.setDomStorageEnabled(true);
webviewSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
webviewSettings.setDefaultTextEncodingName(UTF_8);

// Disallow need for a user gesture to play media.
webviewSettings.setMediaPlaybackRequiresUserGesture(false);

if (ServiceProvider.getInstance()
.getDeviceInfoService()
.getApplicationCacheDir()
!= null) {
webviewSettings.setDatabaseEnabled(true);
try {
final WebView newWebView = new WebView(getApplicationContext());
// assign a random resource id to identify this webview
newWebView.setId(Math.abs(new Random().nextInt()));
newWebView.setVerticalScrollBarEnabled(true);
newWebView.setHorizontalScrollBarEnabled(true);
newWebView.setScrollbarFadingEnabled(true);
newWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
newWebView.setBackgroundColor(Color.TRANSPARENT);

webViewClient = new MessageWebViewClient(AEPMessage.this);
webViewClient.setLocalAssetsMap(assetMap);
newWebView.setWebViewClient(webViewClient);

final WebSettings webviewSettings = newWebView.getSettings();
webviewSettings.setJavaScriptEnabled(true);
webviewSettings.setAllowFileAccess(false);
webviewSettings.setDomStorageEnabled(true);
webviewSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
webviewSettings.setDefaultTextEncodingName(UTF_8);

// Disallow need for a user gesture to play media.
webviewSettings.setMediaPlaybackRequiresUserGesture(false);

if (ServiceProvider.getInstance()
.getDeviceInfoService()
.getApplicationCacheDir()
!= null) {
webviewSettings.setDatabaseEnabled(true);
}

webViewAtomicReference.set(newWebView);
} catch (Exception ex) {
Log.warning(
ServiceConstants.LOG_TAG,
TAG,
"Exception thrown inside of createWebViewRunnable: %s",
ex.getLocalizedMessage());
}

webViewAtomicReference.set(newWebView);
};

final RunnableFuture<Void> createWebviewTask =
Expand All @@ -529,12 +552,11 @@ private WebView createWebView() {
createWebviewTask.get(1, TimeUnit.SECONDS);
return webViewAtomicReference.get();
} catch (final InterruptedException | ExecutionException | TimeoutException exception) {
Log.debug(
Log.warning(
ServiceConstants.LOG_TAG,
TAG,
"Exception occurred when creating the webview: %s",
exception.getLocalizedMessage());
listener.onShowFailure();
exception.getMessage());
createWebviewTask.cancel(true);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import kotlin.test.assertTrue
@RunWith(MockitoJUnitRunner.Silent::class)
class MobileCoreTests {

private var EXTENSION_VERSION = "2.6.0"
private var EXTENSION_VERSION = "2.6.1"

@Mock
private lateinit var mockedEventHub: EventHub
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import kotlin.test.assertTrue
@RunWith(MockitoJUnitRunner.Silent::class)
class ConfigurationExtensionTests {

private var EXTENSION_VERSION = "2.6.0"
private var EXTENSION_VERSION = "2.6.1"

@Mock
private lateinit var mockServiceProvider: ServiceProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockConstruction;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.when;

Expand All @@ -30,6 +32,7 @@
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.webkit.WebSettings;
import android.webkit.WebView;
import androidx.cardview.widget.CardView;
import com.adobe.marketing.mobile.services.AppContextService;
Expand All @@ -47,6 +50,7 @@
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

Expand Down Expand Up @@ -176,79 +180,95 @@ public void testCreateAEPMessage_nullMessageDelegate() throws MessageCreationExc
// AEPMessage show tests
@Test
public void aepMessageIsShown_When_NoOtherMessagesAreDisplayed() {
// setup
Mockito.when(mockMessageMonitor.show(any(FullscreenMessage.class), anyBoolean()))
.thenCallRealMethod()
.thenReturn(true);
Mockito.when(mockMessageMonitor.isDisplayed()).thenReturn(false);
Mockito.when(
mockMessagingDelegate.shouldShowMessage(
ArgumentMatchers.any(AEPMessage.class)))
.thenReturn(true);

try {
message =
new AEPMessage(
"html",
mockFullscreenMessageDelegate,
false,
mockMessageMonitor,
mockAEPMessageSettings,
mockExecutor);
} catch (MessageCreationException ex) {
Assert.fail(ex.getMessage());
try (MockedConstruction<WebView> ignored =
mockConstruction(
WebView.class,
(mock, context) -> {
WebSettings mockWebSettings = Mockito.mock(WebSettings.class);
when(mock.getSettings()).thenReturn(mockWebSettings);
})) {
// setup
Mockito.when(mockMessageMonitor.show(any(FullscreenMessage.class), anyBoolean()))
.thenCallRealMethod()
.thenReturn(true);
Mockito.when(mockMessageMonitor.isDisplayed()).thenReturn(false);
Mockito.when(
mockMessagingDelegate.shouldShowMessage(
ArgumentMatchers.any(AEPMessage.class)))
.thenReturn(true);

try {
message =
new AEPMessage(
"html",
mockFullscreenMessageDelegate,
false,
mockMessageMonitor,
mockAEPMessageSettings,
mockExecutor);
} catch (MessageCreationException ex) {
Assert.fail(ex.getMessage());
}
Mockito.when(mockViewGroup.getMeasuredWidth()).thenReturn(1000);
Mockito.when(mockViewGroup.getMeasuredHeight()).thenReturn(1000);
setupFragmentTransactionMocks();

// test
message.show();
// verify
Mockito.verify(mockMessageMonitor, Mockito.times(1))
.show(any(FullscreenMessage.class), eq(true));
Mockito.verify(mockMessageMonitor, Mockito.times(1)).displayed();

// Verify that the message delegates are notified
Mockito.verify(mockFullscreenMessageDelegate).onShow(message);
Mockito.verify(mockMessagingDelegate).onShow(message);
}
Mockito.when(mockViewGroup.getMeasuredWidth()).thenReturn(1000);
Mockito.when(mockViewGroup.getMeasuredHeight()).thenReturn(1000);
setupFragmentTransactionMocks();

// test
message.show();
// verify
Mockito.verify(mockMessageMonitor, Mockito.times(1))
.show(any(FullscreenMessage.class), eq(true));
Mockito.verify(mockMessageMonitor, Mockito.times(1)).displayed();

// Verify that the message delegates are notified
Mockito.verify(mockFullscreenMessageDelegate).onShow(message);
Mockito.verify(mockMessagingDelegate).onShow(message);
}

@Test
public void aepMessageIsNotShown_When_AnotherMessageIsDisplayed() {
// setup
Mockito.when(mockMessageMonitor.isDisplayed()).thenReturn(true);
Mockito.when(
mockMessagingDelegate.shouldShowMessage(
ArgumentMatchers.any(AEPMessage.class)))
.thenReturn(true);

try {
message =
new AEPMessage(
"html",
mockFullscreenMessageDelegate,
false,
mockMessageMonitor,
mockAEPMessageSettings,
mockExecutor);
} catch (MessageCreationException ex) {
Assert.fail(ex.getMessage());
try (MockedConstruction<WebView> ignored =
mockConstruction(
WebView.class,
(mock, context) -> {
WebSettings mockWebSettings = Mockito.mock(WebSettings.class);
when(mock.getSettings()).thenReturn(mockWebSettings);
})) {
// setup
Mockito.when(mockMessageMonitor.isDisplayed()).thenReturn(true);
Mockito.when(
mockMessagingDelegate.shouldShowMessage(
ArgumentMatchers.any(AEPMessage.class)))
.thenReturn(true);

try {
message =
new AEPMessage(
"html",
mockFullscreenMessageDelegate,
false,
mockMessageMonitor,
mockAEPMessageSettings,
mockExecutor);
} catch (MessageCreationException ex) {
Assert.fail(ex.getMessage());
}
Mockito.when(mockViewGroup.getMeasuredWidth()).thenReturn(1000);
Mockito.when(mockViewGroup.getMeasuredHeight()).thenReturn(1000);
setupFragmentTransactionMocks();

// test
message.show();
// verify
Mockito.verify(mockMessageMonitor, Mockito.times(1))
.show(any(FullscreenMessage.class), eq(true));
Mockito.verify(mockMessageMonitor, Mockito.times(0)).displayed();

// Verify that the message delegates are never notified about showing
Mockito.verify(mockFullscreenMessageDelegate, never()).onShow(message);
Mockito.verify(mockMessagingDelegate, never()).onShow(message);
}
Mockito.when(mockViewGroup.getMeasuredWidth()).thenReturn(1000);
Mockito.when(mockViewGroup.getMeasuredHeight()).thenReturn(1000);
setupFragmentTransactionMocks();

// test
message.show();
// verify
Mockito.verify(mockMessageMonitor, Mockito.times(1))
.show(any(FullscreenMessage.class), eq(true));
Mockito.verify(mockMessageMonitor, Mockito.times(0)).displayed();

// Verify that the message delegates are never notified about showing
Mockito.verify(mockFullscreenMessageDelegate, never()).onShow(message);
Mockito.verify(mockMessagingDelegate, never()).onShow(message);
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion code/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ android.useAndroidX=true
#
#Maven artifacts
#Core extension
coreExtensionVersion=2.6.0
coreExtensionVersion=2.6.1
coreExtensionName=core
coreExtensionAARName=core-phone-release.aar
coreMavenRepoName=AdobeMobileCoreSdk
Expand Down
Loading

0 comments on commit cc16023

Please sign in to comment.