Commit 005f5ddd authored by John Lin's avatar John Lin Committed by Commit Bot

Add a check to make sure the module is successfully loaded before header

customization on module-managed URLs.

Bug: 882404
Change-Id: I8b25e6d50e6ed93d426d387746e11b1d2970ca6e
Reviewed-on: https://chromium-review.googlesource.com/c/1350926
Commit-Queue: John Lin <chuanl@google.com>
Reviewed-by: default avatarMichael van Ouwerkerk <mvanouwerkerk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#611654}
parent 8e7c6ca6
...@@ -383,29 +383,31 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent ...@@ -383,29 +383,31 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent
if (entryPoint == null) { if (entryPoint == null) {
unregisterModuleNavigationEventObserver(); unregisterModuleNavigationEventObserver();
return; } else {
} mModuleEntryPoint = entryPoint;
mModuleEntryPoint = entryPoint;
long createActivityDelegateStartTime = ModuleMetrics.now(); long createActivityDelegateStartTime = ModuleMetrics.now();
mModuleActivityDelegate = entryPoint.createActivityDelegate( mModuleActivityDelegate = entryPoint.createActivityDelegate(
new ActivityHostImpl(CustomTabActivity.this)); new ActivityHostImpl(CustomTabActivity.this));
ModuleMetrics.recordCreateActivityDelegateTime(createActivityDelegateStartTime); ModuleMetrics.recordCreateActivityDelegateTime(createActivityDelegateStartTime);
mModuleActivityDelegate.onCreate(getSavedInstanceState()); mModuleActivityDelegate.onCreate(getSavedInstanceState());
if (mModuleOnStartPending) startModule(); if (mModuleOnStartPending) startModule();
if (mModuleOnResumePending) resumeModule(); if (mModuleOnResumePending) resumeModule();
if (mModuleEntryPoint.getModuleVersion() >= if (mModuleEntryPoint.getModuleVersion()
DynamicModuleConstants.ON_NAVIGATION_EVENT_MODULE_API_VERSION) { >= DynamicModuleConstants.ON_NAVIGATION_EVENT_MODULE_API_VERSION) {
mModuleNavigationEventObserver.setActivityDelegate(mModuleActivityDelegate); mModuleNavigationEventObserver.setActivityDelegate(mModuleActivityDelegate);
} else { } else {
unregisterModuleNavigationEventObserver(); unregisterModuleNavigationEventObserver();
} }
// Initialise the PostMessageHandler for the current web contents. // Initialise the PostMessageHandler for the current web contents.
maybeInitialiseDynamicModulePostMessageHandler( maybeInitialiseDynamicModulePostMessageHandler(
new ActivityDelegatePostMessageBackend()); new ActivityDelegatePostMessageBackend());
}
// Show CCT header (or top bar) if module fails (or succeeds) to load.
maybeUpdateCctHeaderVisibility(mIntentDataProvider.getUrlToLoad());
} }
} }
...@@ -1690,12 +1692,12 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent ...@@ -1690,12 +1692,12 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent
} }
private void maybeUpdateCctHeaderVisibility(String url) { private void maybeUpdateCctHeaderVisibility(String url) {
// TODO(crbug.com/882404) Show CCT top bar if module fails to load. if (!ChromeFeatureList.isEnabled(ChromeFeatureList.CCT_MODULE) || !isModuleLoaded()) {
return;
}
boolean isModuleManagedUrl = isModuleManagedUrl(url); boolean isModuleManagedUrl = isModuleManagedUrl(url);
mTopBarDelegate.showTopBarIfNecessary(isModuleManagedUrl); mTopBarDelegate.showTopBarIfNecessary(isModuleManagedUrl);
if (ChromeFeatureList.isEnabled(ChromeFeatureList.CCT_MODULE_CUSTOM_HEADER)
if (ChromeFeatureList.isEnabled(ChromeFeatureList.CCT_MODULE)
&& ChromeFeatureList.isEnabled(ChromeFeatureList.CCT_MODULE_CUSTOM_HEADER)
&& mIntentDataProvider.shouldHideCctHeaderOnModuleManagedUrls()) { && mIntentDataProvider.shouldHideCctHeaderOnModuleManagedUrls()) {
getToolbarManager().setToolbarVisibility( getToolbarManager().setToolbarVisibility(
isModuleManagedUrl ? View.GONE : mDefaultToolbarVisibility); isModuleManagedUrl ? View.GONE : mDefaultToolbarVisibility);
......
...@@ -1283,10 +1283,13 @@ public class CustomTabActivityTest { ...@@ -1283,10 +1283,13 @@ public class CustomTabActivityTest {
@Test @Test
@SmallTest @SmallTest
@RetryOnFailure @EnableFeatures(ChromeFeatureList.CCT_MODULE)
public void testSetTopBarContentView() throws Exception { public void testSetTopBarContentView() throws Exception {
Intent intent = createMinimalCustomTabIntent(); String moduleManagedUrl = mTestServer.getURL("/chrome/test/data/android/about.html");
Intent intent =
CustomTabsDynamicModuleTestUtils.makeDynamicModuleIntent(moduleManagedUrl, null);
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent); mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent);
waitForModuleLoading();
ThreadUtils.runOnUiThread(() -> { ThreadUtils.runOnUiThread(() -> {
CustomTabActivity cctActivity = mCustomTabActivityTestRule.getActivity(); CustomTabActivity cctActivity = mCustomTabActivityTestRule.getActivity();
...@@ -1301,10 +1304,13 @@ public class CustomTabActivityTest { ...@@ -1301,10 +1304,13 @@ public class CustomTabActivityTest {
@Test @Test
@SmallTest @SmallTest
@RetryOnFailure @EnableFeatures(ChromeFeatureList.CCT_MODULE)
public void testSetTopBarContentView_secondCallIsNoOp() throws Exception { public void testSetTopBarContentView_secondCallIsNoOp() throws Exception {
Intent intent = createMinimalCustomTabIntent(); String moduleManagedUrl = mTestServer.getURL("/chrome/test/data/android/about.html");
Intent intent =
CustomTabsDynamicModuleTestUtils.makeDynamicModuleIntent(moduleManagedUrl, null);
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent); mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent);
waitForModuleLoading();
ThreadUtils.runOnUiThread(() -> { ThreadUtils.runOnUiThread(() -> {
CustomTabActivity cctActivity = mCustomTabActivityTestRule.getActivity(); CustomTabActivity cctActivity = mCustomTabActivityTestRule.getActivity();
...@@ -1318,48 +1324,63 @@ public class CustomTabActivityTest { ...@@ -1318,48 +1324,63 @@ public class CustomTabActivityTest {
@Test @Test
@SmallTest @SmallTest
@EnableFeatures(ChromeFeatureList.CCT_MODULE) @EnableFeatures(ChromeFeatureList.CCT_MODULE)
public void testSetTopBarContentView_moduleNotProvided_topBarInvisible() throws Exception { public void testSetTopBarContentView_moduleNotProvided_noTopBar() throws Exception {
Intent intent = CustomTabsTestUtils.createMinimalCustomTabIntent( Intent intent = CustomTabsTestUtils.createMinimalCustomTabIntent(
InstrumentationRegistry.getTargetContext(), InstrumentationRegistry.getTargetContext(),
"https://www.google.com/search?q=london"); "https://www.google.com/search?q=london");
intent.putExtra(CustomTabIntentDataProvider.EXTRA_MODULE_MANAGED_URLS_REGEX, intent.putExtra(CustomTabIntentDataProvider.EXTRA_MODULE_MANAGED_URLS_REGEX,
"^https://www.google.com/search.*"); "^https://www.google.com/search.*");
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent); mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent);
waitForModuleLoading();
ThreadUtils.runOnUiThread(() -> { ThreadUtils.runOnUiThread(() -> {
CustomTabActivity cctActivity = mCustomTabActivityTestRule.getActivity(); CustomTabActivity cctActivity = mCustomTabActivityTestRule.getActivity();
View anyView = new View(cctActivity); View anyView = new View(cctActivity);
cctActivity.setTopBarContentView(anyView); cctActivity.setTopBarContentView(anyView);
ViewGroup topBar = cctActivity.findViewById(R.id.topbar); ViewGroup topBar = cctActivity.findViewById(R.id.topbar);
Assert.assertNotNull(topBar); Assert.assertNull(topBar);
Assert.assertThat(anyView.getParent(), equalTo(topBar));
Assert.assertEquals(View.GONE, anyView.getVisibility());
}); });
} }
@Test @Test
@SmallTest @SmallTest
@DisableFeatures(ChromeFeatureList.CCT_MODULE) @DisableFeatures(ChromeFeatureList.CCT_MODULE)
public void testSetTopBarContentView_featureDisabled_topBarInvisible() throws Exception { public void testSetTopBarContentView_featureDisabled_noTopBar() throws Exception {
String moduleManagedUrl = mTestServer.getURL("/chrome/test/data/android/about.html");
Intent intent = CustomTabsDynamicModuleTestUtils.makeDynamicModuleIntent(
moduleManagedUrl, "^(" + moduleManagedUrl + ")$");
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent);
waitForModuleLoading();
ThreadUtils.runOnUiThread(() -> {
CustomTabActivity cctActivity = mCustomTabActivityTestRule.getActivity();
View anyView = new View(cctActivity);
cctActivity.setTopBarContentView(anyView);
ViewGroup topBar = cctActivity.findViewById(R.id.topbar);
Assert.assertNull(topBar);
});
}
@Test
@SmallTest
@EnableFeatures(ChromeFeatureList.CCT_MODULE)
public void testSetTopBarContentView_moduleLoadingFailed_noTopBar() throws Exception {
// Make an intent with nonexistent class name so module loading fail
Intent intent = CustomTabsTestUtils.createMinimalCustomTabIntent( Intent intent = CustomTabsTestUtils.createMinimalCustomTabIntent(
InstrumentationRegistry.getTargetContext(), InstrumentationRegistry.getTargetContext(),
"https://www.google.com/search?q=london"); "https://www.google.com/search?q=london");
intent.putExtra(CustomTabIntentDataProvider.EXTRA_MODULE_PACKAGE_NAME, intent.putExtra(CustomTabIntentDataProvider.EXTRA_MODULE_PACKAGE_NAME,
"com.google.android.googlequicksearchbox"); CustomTabsDynamicModuleTestUtils.FAKE_MODULE_PACKAGE_NAME);
intent.putExtra(CustomTabIntentDataProvider.EXTRA_MODULE_CLASS_NAME, intent.putExtra(CustomTabIntentDataProvider.EXTRA_MODULE_CLASS_NAME, "ClassName");
"com.google.android.googlequicksearchbox.SearchActivity");
intent.putExtra(CustomTabIntentDataProvider.EXTRA_MODULE_MANAGED_URLS_REGEX,
"^https://www.google.com/search.*");
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent); mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent);
waitForModuleLoading();
ThreadUtils.runOnUiThread(() -> { ThreadUtils.runOnUiThread(() -> {
CustomTabActivity cctActivity = mCustomTabActivityTestRule.getActivity(); CustomTabActivity cctActivity = mCustomTabActivityTestRule.getActivity();
View anyView = new View(cctActivity); View anyView = new View(cctActivity);
cctActivity.setTopBarContentView(anyView); cctActivity.setTopBarContentView(anyView);
ViewGroup topBar = cctActivity.findViewById(R.id.topbar); ViewGroup topBar = cctActivity.findViewById(R.id.topbar);
Assert.assertNotNull(topBar); Assert.assertNull(topBar);
Assert.assertThat(anyView.getParent(), equalTo(topBar));
Assert.assertEquals(View.GONE, anyView.getVisibility());
}); });
} }
...@@ -1367,16 +1388,11 @@ public class CustomTabActivityTest { ...@@ -1367,16 +1388,11 @@ public class CustomTabActivityTest {
@SmallTest @SmallTest
@EnableFeatures(ChromeFeatureList.CCT_MODULE) @EnableFeatures(ChromeFeatureList.CCT_MODULE)
public void testSetTopBarContentView_withModuleAndManagedUrls_topBarVisible() throws Exception { public void testSetTopBarContentView_withModuleAndManagedUrls_topBarVisible() throws Exception {
Intent intent = CustomTabsTestUtils.createMinimalCustomTabIntent( String moduleManagedUrl = mTestServer.getURL("/chrome/test/data/android/about.html");
InstrumentationRegistry.getTargetContext(), Intent intent = CustomTabsDynamicModuleTestUtils.makeDynamicModuleIntent(
"https://www.google.com/search?q=london"); moduleManagedUrl, "^(" + moduleManagedUrl + ")$");
intent.putExtra(CustomTabIntentDataProvider.EXTRA_MODULE_PACKAGE_NAME,
"com.google.android.googlequicksearchbox");
intent.putExtra(CustomTabIntentDataProvider.EXTRA_MODULE_CLASS_NAME,
"com.google.android.googlequicksearchbox.SearchActivity");
intent.putExtra(CustomTabIntentDataProvider.EXTRA_MODULE_MANAGED_URLS_REGEX,
"^https://www.google.com/search.*");
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent); mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent);
waitForModuleLoading();
ThreadUtils.runOnUiThread(() -> { ThreadUtils.runOnUiThread(() -> {
CustomTabActivity cctActivity = mCustomTabActivityTestRule.getActivity(); CustomTabActivity cctActivity = mCustomTabActivityTestRule.getActivity();
...@@ -1401,6 +1417,7 @@ public class CustomTabActivityTest { ...@@ -1401,6 +1417,7 @@ public class CustomTabActivityTest {
intent.putExtra( intent.putExtra(
CustomTabIntentDataProvider.EXTRA_HIDE_CCT_HEADER_ON_MODULE_MANAGED_URLS, true); CustomTabIntentDataProvider.EXTRA_HIDE_CCT_HEADER_ON_MODULE_MANAGED_URLS, true);
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent); mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent);
waitForModuleLoading();
ThreadUtils.runOnUiThread(() -> { ThreadUtils.runOnUiThread(() -> {
CustomTabActivity cctActivity = mCustomTabActivityTestRule.getActivity(); CustomTabActivity cctActivity = mCustomTabActivityTestRule.getActivity();
...@@ -1417,18 +1434,13 @@ public class CustomTabActivityTest { ...@@ -1417,18 +1434,13 @@ public class CustomTabActivityTest {
@SmallTest @SmallTest
@DisableFeatures(ChromeFeatureList.CCT_MODULE_CUSTOM_HEADER) @DisableFeatures(ChromeFeatureList.CCT_MODULE_CUSTOM_HEADER)
public void testSetTopBarContentView_featureDisabled_cctHeaderVisible() throws Exception { public void testSetTopBarContentView_featureDisabled_cctHeaderVisible() throws Exception {
Intent intent = CustomTabsTestUtils.createMinimalCustomTabIntent( String moduleManagedUrl = mTestServer.getURL("/chrome/test/data/android/about.html");
InstrumentationRegistry.getTargetContext(), Intent intent = CustomTabsDynamicModuleTestUtils.makeDynamicModuleIntent(
"https://www.google.com/search?q=london"); moduleManagedUrl, "^(" + moduleManagedUrl + ")$");
intent.putExtra(CustomTabIntentDataProvider.EXTRA_MODULE_PACKAGE_NAME,
"com.google.android.googlequicksearchbox");
intent.putExtra(CustomTabIntentDataProvider.EXTRA_MODULE_CLASS_NAME,
"com.google.android.googlequicksearchbox.SearchActivity");
intent.putExtra(CustomTabIntentDataProvider.EXTRA_MODULE_MANAGED_URLS_REGEX,
"^https://www.google.com/search.*");
intent.putExtra( intent.putExtra(
CustomTabIntentDataProvider.EXTRA_HIDE_CCT_HEADER_ON_MODULE_MANAGED_URLS, true); CustomTabIntentDataProvider.EXTRA_HIDE_CCT_HEADER_ON_MODULE_MANAGED_URLS, true);
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent); mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent);
waitForModuleLoading();
ThreadUtils.runOnUiThread(() -> { ThreadUtils.runOnUiThread(() -> {
CustomTabActivity cctActivity = mCustomTabActivityTestRule.getActivity(); CustomTabActivity cctActivity = mCustomTabActivityTestRule.getActivity();
...@@ -1445,18 +1457,13 @@ public class CustomTabActivityTest { ...@@ -1445,18 +1457,13 @@ public class CustomTabActivityTest {
@SmallTest @SmallTest
@EnableFeatures({ChromeFeatureList.CCT_MODULE, ChromeFeatureList.CCT_MODULE_CUSTOM_HEADER}) @EnableFeatures({ChromeFeatureList.CCT_MODULE, ChromeFeatureList.CCT_MODULE_CUSTOM_HEADER})
public void testSetTopBarContentView_withModuleAndExtras_cctHeaderHidden() throws Exception { public void testSetTopBarContentView_withModuleAndExtras_cctHeaderHidden() throws Exception {
Intent intent = CustomTabsTestUtils.createMinimalCustomTabIntent( String moduleManagedUrl = mTestServer.getURL("/chrome/test/data/android/about.html");
InstrumentationRegistry.getTargetContext(), Intent intent = CustomTabsDynamicModuleTestUtils.makeDynamicModuleIntent(
"https://www.google.com/search?q=london"); moduleManagedUrl, "^(" + moduleManagedUrl + ")$");
intent.putExtra(CustomTabIntentDataProvider.EXTRA_MODULE_PACKAGE_NAME,
"com.google.android.googlequicksearchbox");
intent.putExtra(CustomTabIntentDataProvider.EXTRA_MODULE_CLASS_NAME,
"com.google.android.googlequicksearchbox.SearchActivity");
intent.putExtra(CustomTabIntentDataProvider.EXTRA_MODULE_MANAGED_URLS_REGEX,
"^https://www.google.com/search.*");
intent.putExtra( intent.putExtra(
CustomTabIntentDataProvider.EXTRA_HIDE_CCT_HEADER_ON_MODULE_MANAGED_URLS, true); CustomTabIntentDataProvider.EXTRA_HIDE_CCT_HEADER_ON_MODULE_MANAGED_URLS, true);
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent); mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent);
waitForModuleLoading();
ThreadUtils.runOnUiThread(() -> { ThreadUtils.runOnUiThread(() -> {
CustomTabActivity cctActivity = mCustomTabActivityTestRule.getActivity(); CustomTabActivity cctActivity = mCustomTabActivityTestRule.getActivity();
...@@ -3258,4 +3265,13 @@ public class CustomTabActivityTest { ...@@ -3258,4 +3265,13 @@ public class CustomTabActivityTest {
fail("Tab title didn't update in time"); fail("Tab title didn't update in time");
} }
} }
private void waitForModuleLoading() {
CriteriaHelper.pollUiThread(new Criteria() {
@Override
public boolean isSatisfied() {
return !mCustomTabActivityTestRule.getActivity().isModuleLoading();
}
});
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment