Commit 233b263f authored by Peter Kotwicz's avatar Peter Kotwicz Committed by Commit Bot

[Android TWAs] Fix status bar colour

In order to use the theme-color specified by the web page for TWAs,
CustomTabStatusBarColorProvider#setUseTabThemeColor() needs to be
invoked because CustomTabStatusBarColorProvider#mUseTabThemeColor defaults to
false.
setUseTabThemeColor() is currently not invoked because
TrustedWebActivityCoordinator#mInTwaMode defaults to true

This CL fixes the bug by adding a speculative call to
setUseTabThemeColor() in onPostInflationStartup() similar to what is done for
the toolbar hide state

This CL also enables CustomTabActivityTest.testToolbarColor() on Lollipop.
This matches similar tests like
WebappSplashScreenThemeColorTest#testThemeColorNotUsedIfPagesHasOne()

BUG=997578
TEST=TrustedWebActivityTest.testStatusBarColorPrecedence

Change-Id: I6015acbae310650051b49f75cacbddc16a5e66e8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1771316
Commit-Queue: Peter Kotwicz <pkotwicz@chromium.org>
Reviewed-by: default avatarPeter Conn <peconn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#694940}
parent 08ccf9cf
......@@ -69,9 +69,9 @@ public class TrustedWebActivityCoordinator implements InflationObserver {
@Override
public void onPostInflationStartup() {
// Before the verification completes, we optimistically expect it to be successful and apply
// the trusted web activity mode to UI. So hide the toolbar as soon as possible.
// the trusted web activity mode to UI.
if (mVerifier.getState() == null) {
mToolbarCoordinator.setToolbarHidden(true);
updateUi(true);
}
}
......@@ -93,20 +93,16 @@ public class TrustedWebActivityCoordinator implements InflationObserver {
boolean inTwaMode = state == null || state.status != VerificationStatus.FAILURE;
if (inTwaMode == mInTwaMode) return;
mInTwaMode = inTwaMode;
updateToolbar();
updateStatusBar();
updateUi(mInTwaMode);
}
private void updateToolbar() {
mToolbarCoordinator.setToolbarHidden(mInTwaMode);
private void updateUi(boolean inTwaMode) {
mToolbarCoordinator.setToolbarHidden(inTwaMode);
mStatusBarColorProvider.setUseTabThemeColor(inTwaMode);
if (!mInTwaMode) {
if (!inTwaMode) {
// Force showing the controls for a bit when leaving Trusted Web Activity mode.
mToolbarCoordinator.showToolbarTemporarily();
}
}
private void updateStatusBar() {
mStatusBarColorProvider.setUseTabThemeColor(mInTwaMode);
}
}
......@@ -4,6 +4,7 @@
package org.chromium.chrome.browser.browserservices;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
......@@ -13,8 +14,13 @@ import static org.chromium.chrome.browser.browserservices.TrustedWebActivityTest
import static org.chromium.chrome.browser.browserservices.TrustedWebActivityTestUtil.spoofVerification;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.test.filters.MediumTest;
import androidx.browser.customtabs.CustomTabsIntent;
import androidx.browser.customtabs.TrustedWebUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
......@@ -25,15 +31,19 @@ import org.chromium.base.library_loader.LibraryLoader;
import org.chromium.base.library_loader.LibraryProcessType;
import org.chromium.base.library_loader.ProcessInitException;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.MinAndroidSdkLevel;
import org.chromium.chrome.browser.ChromeSwitches;
import org.chromium.chrome.browser.customtabs.CustomTabActivity;
import org.chromium.chrome.browser.customtabs.CustomTabActivityTestRule;
import org.chromium.chrome.browser.util.ColorUtils;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.net.test.EmbeddedTestServerRule;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeoutException;
import androidx.browser.customtabs.TrustedWebUtils;
/**
* Instrumentation tests for launching
* {@link org.chromium.chrome.browser.customtabs.CustomTabActivity} in Trusted Web Activity Mode.
......@@ -98,4 +108,43 @@ public class TrustedWebActivityTest {
assertFalse(isTrustedWebActivity(mCustomTabActivityTestRule.getActivity()));
}
/**
* Test that for trusted activities if the page provides a theme-color and the toolbar color was
* specified in the intent that the page theme-color is used for the status bar.
*/
@Test
@MediumTest
@MinAndroidSdkLevel(Build.VERSION_CODES.LOLLIPOP_MR1)
public void testStatusBarColorPrecedence() throws TimeoutException, InterruptedException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return;
final int intentToolbarColor = Color.GREEN;
final String pageWithThemeColor = mEmbeddedTestServerRule.getServer().getURL(
"/chrome/test/data/android/theme_color_test.html");
final int pageThemeColor = Color.RED;
Intent intent = createTrustedWebActivityIntent(pageWithThemeColor);
spoofVerification(PACKAGE_NAME, mTestPage);
intent.putExtra(CustomTabsIntent.EXTRA_TOOLBAR_COLOR, intentToolbarColor);
createSession(intent, PACKAGE_NAME);
mCustomTabActivityTestRule.startCustomTabActivityWithIntent(intent);
// Waits for theme-color to change so the test doesn't rely on system timing.
CustomTabActivity activity = mCustomTabActivityTestRule.getActivity();
CriteriaHelper.pollInstrumentationThread(
Criteria.equals(pageThemeColor, new Callable<Integer>() {
@Override
public Integer call() {
return activity.getActivityTab().getWebContents().getThemeColor();
}
}));
int expectedColor = pageThemeColor;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
expectedColor = ColorUtils.getDarkenedColorForStatusBar(expectedColor);
}
assertEquals(expectedColor, activity.getWindow().getStatusBarColor());
}
}
......@@ -899,7 +899,7 @@ public class CustomTabActivityTest {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
assertEquals(expectedColor,
mCustomTabActivityTestRule.getActivity().getWindow().getStatusBarColor());
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
assertEquals(ColorUtils.getDarkenedColorForStatusBar(expectedColor),
mCustomTabActivityTestRule.getActivity().getWindow().getStatusBarColor());
}
......
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