Commit f90f0256 authored by Peter E Conn's avatar Peter E Conn Committed by Commit Bot

🛃 Split out Custom Tabs status bar color code.

This code should have no behavioural change - it's just a small CL to
split the code out and add tests.

Bug: 964914
Change-Id: I0a6d3b356931ddaa7863f490cddc4009195a8495
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1621174Reviewed-by: default avatarPavel Shmakov <pshmakov@chromium.org>
Reviewed-by: default avatarMichael van Ouwerkerk <mvanouwerkerk@chromium.org>
Commit-Queue: Peter Conn <peconn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#661723}
parent de8b8209
......@@ -369,6 +369,7 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/customtabs/CustomTabBrowserControlsVisibilityDelegate.java",
"java/src/org/chromium/chrome/browser/customtabs/CustomTabDelegateFactory.java",
"java/src/org/chromium/chrome/browser/customtabs/CustomTabIntentDataProvider.java",
"java/src/org/chromium/chrome/browser/customtabs/CustomTabStatusBarColorProvider.java",
"java/src/org/chromium/chrome/browser/customtabs/CustomTabNavigationEventObserver.java",
"java/src/org/chromium/chrome/browser/customtabs/CustomTabNightModeStateController.java",
"java/src/org/chromium/chrome/browser/customtabs/CustomTabObserver.java",
......
......@@ -50,6 +50,7 @@ chrome_junit_test_java_sources = [
"junit/src/org/chromium/chrome/browser/crash/LogcatExtractionRunnableUnitTest.java",
"junit/src/org/chromium/chrome/browser/customtabs/CloseButtonNavigatorTest.java",
"junit/src/org/chromium/chrome/browser/customtabs/CustomTabIntentDataProviderTest.java",
"junit/src/org/chromium/chrome/browser/customtabs/CustomTabStatusBarColorProviderTest.java",
"junit/src/org/chromium/chrome/browser/customtabs/NavigationInfoCaptureTriggerTest.java",
"junit/src/org/chromium/chrome/browser/customtabs/content/CustomTabActivityContentTestEnvironment.java",
"junit/src/org/chromium/chrome/browser/customtabs/content/CustomTabActivityNavigationControllerTest.java",
......
......@@ -120,6 +120,7 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent
private CustomTabActivityTabProvider mTabProvider;
private CustomTabActivityTabFactory mTabFactory;
private CustomTabActivityNavigationController mNavigationController;
private CustomTabStatusBarColorProvider mCustomTabStatusBarColorProvider;
// This is to give the right package name while using the client's resources during an
// overridePendingTransition call.
......@@ -769,17 +770,14 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent
@Override
public int getBaseStatusBarColor() {
if (mIntentDataProvider.isOpenedByChrome()) return super.getBaseStatusBarColor();
if (getActivityTab() != null && getActivityTab().isPreview()) {
return ColorUtils.getDefaultThemeColor(getResources(), false);
}
return mIntentDataProvider.getToolbarColor();
return mCustomTabStatusBarColorProvider
.getBaseStatusBarColor(super.getBaseStatusBarColor());
}
@Override
public boolean isStatusBarDefaultThemeColor() {
if (mIntentDataProvider.isOpenedByChrome()) return super.isStatusBarDefaultThemeColor();
return false;
return mCustomTabStatusBarColorProvider
.isStatusBarDefaultThemeColor(super.isStatusBarDefaultThemeColor());
}
@Override
......@@ -895,6 +893,7 @@ public class CustomTabActivity extends ChromeActivity<CustomTabActivityComponent
ChromeApplication.getComponent().createCustomTabActivityComponent(
commonsModule, customTabsModule);
mCustomTabStatusBarColorProvider = component.resolveCustomTabStatusBarColorProvider();
mTabObserverRegistrar = component.resolveTabObserverRegistrar();
mTabController = component.resolveTabController();
mTabProvider = component.resolveTabProvider();
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.customtabs;
import android.content.res.Resources;
import org.chromium.chrome.browser.customtabs.content.CustomTabActivityTabProvider;
import org.chromium.chrome.browser.dependency_injection.ActivityScope;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.util.ColorUtils;
import javax.inject.Inject;
/**
* Manages the status bar color for a CustomTabActivity.
*/
@ActivityScope
public class CustomTabStatusBarColorProvider {
private final Resources mResources;
private final CustomTabIntentDataProvider mIntentDataProvider;
private final CustomTabActivityTabProvider mCustomTabActivityTabProvider;
@Inject
public CustomTabStatusBarColorProvider(Resources resources,
CustomTabIntentDataProvider intentDataProvider,
CustomTabActivityTabProvider customTabActivityTabProvider) {
mResources = resources;
mIntentDataProvider = intentDataProvider;
mCustomTabActivityTabProvider = customTabActivityTabProvider;
}
int getBaseStatusBarColor(int fallbackStatusBarColor) {
if (mIntentDataProvider.isOpenedByChrome()) return fallbackStatusBarColor;
Tab tab = mCustomTabActivityTabProvider.getTab();
if (tab!= null && tab.isPreview()) {
return ColorUtils.getDefaultThemeColor(mResources, false);
}
return mIntentDataProvider.getToolbarColor();
}
boolean isStatusBarDefaultThemeColor(boolean isFallbackColorDefault) {
if (mIntentDataProvider.isOpenedByChrome()) return isFallbackColorDefault;
return false;
}
}
......@@ -7,6 +7,7 @@ package org.chromium.chrome.browser.customtabs.dependency_injection;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.TrustedWebActivityCoordinator;
import org.chromium.chrome.browser.customtabs.CustomTabActivityLifecycleUmaTracker;
import org.chromium.chrome.browser.customtabs.CustomTabBottomBarDelegate;
import org.chromium.chrome.browser.customtabs.CustomTabStatusBarColorProvider;
import org.chromium.chrome.browser.customtabs.CustomTabTabPersistencePolicy;
import org.chromium.chrome.browser.customtabs.CustomTabTopBarDelegate;
import org.chromium.chrome.browser.customtabs.content.CustomTabActivityInitialPageLoader;
......@@ -43,6 +44,7 @@ public interface CustomTabActivityComponent extends ChromeActivityComponent {
CustomTabActivityInitialPageLoader resolveInitialPageLoader();
CustomTabActivityNavigationController resolveNavigationController();
CustomTabActivityTabProvider resolveTabProvider();
CustomTabStatusBarColorProvider resolveCustomTabStatusBarColorProvider();
CustomTabTabPersistencePolicy resolveTabPersistencePolicy(); // For testing
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.customtabs;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when;
import android.content.res.Resources;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.chrome.browser.customtabs.content.CustomTabActivityTabProvider;
import org.chromium.chrome.browser.tab.Tab;
/**
* Tests for {@link CustomTabStatusBarColorProvider}.
*/
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class CustomTabStatusBarColorProviderTest {
private static final int DEFAULT_COLOR = 0x11223344;
private static final int FALLBACK_COLOR = 0x55667788;
private static final int USER_PROVIDED_COLOR = 0x99aabbcc;
@Mock public Resources mResources;
@Mock public CustomTabIntentDataProvider mCustomTabIntentDataProvider;
@Mock public CustomTabActivityTabProvider mCustomTabActivityTabProvider;
private CustomTabStatusBarColorProvider mColorProvider;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mColorProvider = new CustomTabStatusBarColorProvider(mResources,
mCustomTabIntentDataProvider, mCustomTabActivityTabProvider);
// The color is accessed through ApiCompatibilityUtils which calls either
// Resources#getColor(int, Theme) or Resources#getColor(int) depending on the Android
// version. We mock out both calls so things don't break if we change the Android version
// the tests are run with.
when(mResources.getColor(anyInt(), any())).thenReturn(DEFAULT_COLOR);
when(mResources.getColor(anyInt())).thenReturn(DEFAULT_COLOR);
when(mCustomTabIntentDataProvider.getToolbarColor()).thenReturn(USER_PROVIDED_COLOR);
}
@Test
public void fallsBackWhenOpenedByChrome() {
when(mCustomTabIntentDataProvider.isOpenedByChrome()).thenReturn(true);
Assert.assertEquals(FALLBACK_COLOR, mColorProvider.getBaseStatusBarColor(FALLBACK_COLOR));
Assert.assertTrue(mColorProvider.isStatusBarDefaultThemeColor(true));
Assert.assertFalse(mColorProvider.isStatusBarDefaultThemeColor(false));
}
@Test
public void defaultThemeForPreviews() {
Tab tab = Mockito.mock(Tab.class);
when(tab.isPreview()).thenReturn(true);
when(mCustomTabActivityTabProvider.getTab()).thenReturn(tab);
Assert.assertEquals(DEFAULT_COLOR, mColorProvider.getBaseStatusBarColor(FALLBACK_COLOR));
Assert.assertFalse(mColorProvider.isStatusBarDefaultThemeColor(true));
}
@Test
public void userProvidedColor() {
Assert.assertEquals(USER_PROVIDED_COLOR,
mColorProvider.getBaseStatusBarColor(FALLBACK_COLOR));
Assert.assertFalse(mColorProvider.isStatusBarDefaultThemeColor(true));
}
}
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