Commit 6c273799 authored by Jinsuk Kim's avatar Jinsuk Kim Committed by Commit Bot

Toolbar: Remove dependency on NewTabPage in ToolbarColors

ToolbarColors is a collection of public static methods that returns
the right color of various toolbar ui components which gets customized
when the current tab is showing NTP. This CL refactors the customized
logic by moving it behind NativePage interface, so that the class
doesn't have direct dependency on NewTabPage.

Bug: 1127732
Change-Id: Ib1b139467a0f1cc216792d112b13b40168c44c3b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2486526
Commit-Queue: Jinsuk Kim <jinsukkim@chromium.org>
Reviewed-by: default avatarMatthew Jones <mdjones@chromium.org>
Reviewed-by: default avatarFilip Gorski <fgorski@chromium.org>
Cr-Commit-Position: refs/heads/master@{#823491}
parent a26fa9a7
......@@ -296,6 +296,7 @@ chrome_test_java_sources = [
"javatests/src/org/chromium/chrome/browser/notifications/settings/NotificationSettingsTest.java",
"javatests/src/org/chromium/chrome/browser/ntp/IncognitoDescriptionViewRenderTest.java",
"javatests/src/org/chromium/chrome/browser/ntp/IncognitoNewTabPageTest.java",
"javatests/src/org/chromium/chrome/browser/ntp/NewTabPageColorTest.java",
"javatests/src/org/chromium/chrome/browser/ntp/NewTabPageLoadTest.java",
"javatests/src/org/chromium/chrome/browser/ntp/NewTabPageNavigationTest.java",
"javatests/src/org/chromium/chrome/browser/ntp/NewTabPageTest.java",
......
......@@ -6,6 +6,7 @@ package org.chromium.chrome.browser.ntp;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.Rect;
......@@ -15,6 +16,7 @@ import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.ColorInt;
import androidx.annotation.VisibleForTesting;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.RecyclerView;
......@@ -67,6 +69,7 @@ import org.chromium.chrome.browser.ui.native_page.NativePage;
import org.chromium.chrome.browser.ui.native_page.NativePageHost;
import org.chromium.chrome.browser.vr.VrModuleProvider;
import org.chromium.components.browser_ui.bottomsheet.BottomSheetController;
import org.chromium.components.browser_ui.styles.ChromeColors;
import org.chromium.components.embedder_support.util.UrlConstants;
import org.chromium.components.embedder_support.util.UrlUtilities;
import org.chromium.components.feature_engagement.EventConstants;
......@@ -96,6 +99,7 @@ public class NewTabPage implements NativePage, InvalidationAwareThumbnailProvide
private final ActivityLifecycleDispatcher mActivityLifecycleDispatcher;
private final String mTitle;
private Resources mResources;
private final int mBackgroundColor;
protected final NewTabPageManagerImpl mNewTabPageManager;
protected final TileGroup.Delegate mTileGroupDelegate;
......@@ -293,6 +297,7 @@ public class NewTabPage implements NativePage, InvalidationAwareThumbnailProvide
mTileGroupDelegate = new NewTabPageTileGroupDelegate(
activity, profile, navigationDelegate, snackbarManager);
mResources = activity.getResources();
mTitle = activity.getResources().getString(R.string.button_new_tab);
mBackgroundColor =
ApiCompatibilityUtils.getColor(activity.getResources(), R.color.default_bg_color);
......@@ -751,6 +756,27 @@ public class NewTabPage implements NativePage, InvalidationAwareThumbnailProvide
return mBackgroundColor;
}
@Override
public @ColorInt int getToolbarTextBoxBackgroundColor(@ColorInt int defaultColor) {
if (isLocationBarShownInNTP()) {
return isLocationBarScrolledToTopInNtp()
? ApiCompatibilityUtils.getColor(
mResources, R.color.toolbar_text_box_background)
: ChromeColors.getPrimaryBackgroundColor(mResources, false);
}
return defaultColor;
}
@Override
public @ColorInt int getToolbarSceneLayerBackground(@ColorInt int defaultColor) {
return isLocationBarShownInNTP() ? getBackgroundColor() : defaultColor;
}
@Override
public float getToolbarTextBoxAlpha(float defaultAlpha) {
return isLocationBarShownInNTP() ? 0.f : defaultAlpha;
}
@Override
public boolean needsToolbarShadow() {
return !mSearchProviderHasLogo;
......
......@@ -18,10 +18,10 @@ import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.device.DeviceClassManager;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.ntp.NewTabPage;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tab.TabThemeColorHelper;
import org.chromium.chrome.browser.tasks.tab_management.TabUiFeatureUtilities;
import org.chromium.chrome.browser.ui.native_page.NativePage;
import org.chromium.chrome.features.start_surface.StartSurfaceConfiguration;
import org.chromium.components.browser_ui.styles.ChromeColors;
import org.chromium.ui.util.ColorUtils;
......@@ -36,23 +36,18 @@ public class ToolbarColors {
* Determine the text box background color given the current tab.
* @param res {@link Resources} used to retrieve colors.
* @param tab The current {@link Tab}
* @param color The color of the toolbar background.
* @param backgroundColor The color of the toolbar background.
* @return The base color for the textbox given a toolbar background color.
*/
public static int getTextBoxColorForToolbarBackground(
Resources res, @Nullable Tab tab, int color) {
public static @ColorInt int getTextBoxColorForToolbarBackground(
Resources res, @Nullable Tab tab, @ColorInt int backgroundColor) {
boolean isIncognito = tab != null && tab.isIncognito();
if (tab != null && tab.getNativePage() instanceof NewTabPage) {
NewTabPage page = (NewTabPage) tab.getNativePage();
if (page.isLocationBarShownInNTP()) {
return page.isLocationBarScrolledToTopInNtp()
? ApiCompatibilityUtils.getColor(res, R.color.toolbar_text_box_background)
: ChromeColors.getPrimaryBackgroundColor(res, false);
}
}
return ToolbarColors.getTextBoxColorForToolbarBackgroundInNonNativePage(
res, color, isIncognito);
@ColorInt
int defaultColor = getTextBoxColorForToolbarBackgroundInNonNativePage(
res, backgroundColor, isIncognito);
NativePage nativePage = tab != null ? tab.getNativePage() : null;
return nativePage != null ? nativePage.getToolbarTextBoxBackgroundColor(defaultColor)
: defaultColor;
}
/**
......@@ -62,8 +57,8 @@ public class ToolbarColors {
* @param isIncognito Whether or not the color is used for incognito mode.
* @return The base color for the textbox given a toolbar background color.
*/
public static int getTextBoxColorForToolbarBackgroundInNonNativePage(
Resources res, int color, boolean isIncognito) {
public static @ColorInt int getTextBoxColorForToolbarBackgroundInNonNativePage(
Resources res, @ColorInt int color, boolean isIncognito) {
// Text box color on default toolbar background in incognito mode is a pre-defined
// color. We calculate the equivalent opaque color from the pre-defined translucent color.
if (isIncognito) {
......@@ -95,26 +90,22 @@ public class ToolbarColors {
public static @ColorInt int getToolbarSceneLayerBackground(Tab tab) {
// On NTP, the toolbar background is tinted as the NTP background color, so return NTP
// background color here to make animation smoother.
if (tab.getNativePage() instanceof NewTabPage) {
if (((NewTabPage) tab.getNativePage()).isLocationBarShownInNTP()) {
return tab.getNativePage().getBackgroundColor();
}
}
return TabThemeColorHelper.getColor(tab);
@ColorInt
int defaultColor = TabThemeColorHelper.getColor(tab);
NativePage nativePage = tab.getNativePage();
return nativePage != null ? nativePage.getToolbarSceneLayerBackground(defaultColor)
: defaultColor;
}
/**
* @return Alpha for the textbox given a Tab.
*/
public static float getTextBoxAlphaForToolbarBackground(Tab tab) {
if (tab.getNativePage() instanceof NewTabPage) {
if (((NewTabPage) tab.getNativePage()).isLocationBarShownInNTP()) return 0f;
}
int color = TabThemeColorHelper.getColor(tab);
return ColorUtils.shouldUseOpaqueTextboxBackground(color)
? 1f
float alpha = ColorUtils.shouldUseOpaqueTextboxBackground(TabThemeColorHelper.getColor(tab))
? 1.f
: LOCATION_BAR_TRANSPARENT_BACKGROUND_ALPHA;
NativePage nativePage = tab.getNativePage();
return nativePage != null ? nativePage.getToolbarTextBoxAlpha(alpha) : alpha;
}
/**
......
// Copyright 2020 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.ntp;
import android.content.res.Resources;
import android.graphics.Color;
import androidx.recyclerview.widget.RecyclerView;
import androidx.test.filters.MediumTest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.Feature;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.feed.v1.DataFilePath;
import org.chromium.chrome.browser.feed.v1.FeedDataInjectRule;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
import org.chromium.chrome.test.util.NewTabPageTestUtils;
import org.chromium.chrome.test.util.browser.Features;
import org.chromium.chrome.test.util.browser.suggestions.SuggestionsDependenciesRule;
import org.chromium.components.browser_ui.styles.ChromeColors;
import org.chromium.components.browser_ui.widget.RecyclerViewTestUtils;
import org.chromium.components.embedder_support.util.UrlConstants;
/**
* Tests for colors used in UI components in the native android New Tab Page.
*/
// clang-format off
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE,
"disable-features=IPH_FeedHeaderMenu"})
@Features.DisableFeatures({ChromeFeatureList.EXPLORE_SITES,
ChromeFeatureList.REPORT_FEED_USER_ACTIONS,
ChromeFeatureList.QUERY_TILES, ChromeFeatureList.VIDEO_TUTORIALS})
// clang-format on
public class NewTabPageColorTest {
@Rule
public ChromeTabbedActivityTestRule mActivityTestRule = new ChromeTabbedActivityTestRule();
@Rule
public SuggestionsDependenciesRule mSuggestionsDeps = new SuggestionsDependenciesRule();
@Rule
public FeedDataInjectRule mFeedDataInjector = new FeedDataInjectRule(true);
private static final String TEST_FEED_DATA_BASE_PATH = "/chrome/test/data/android/feed/";
private Tab mTab;
private NewTabPage mNtp;
@Before
public void setUp() throws Exception {
mActivityTestRule.startMainActivityWithURL("about:blank");
mActivityTestRule.loadUrl(UrlConstants.NTP_URL);
mTab = mActivityTestRule.getActivity().getActivityTab();
NewTabPageTestUtils.waitForNtpLoaded(mTab);
Assert.assertTrue(mTab.getNativePage() instanceof NewTabPage);
mNtp = (NewTabPage) mTab.getNativePage();
}
// clang-format off
@Test
@MediumTest
@Feature({"NewTabPage", "FeedNewTabPage"})
@Features.EnableFeatures({ChromeFeatureList.INTEREST_FEED_CONTENT_SUGGESTIONS,
ChromeFeatureList.OMNIBOX_SEARCH_ENGINE_LOGO})
@Features.DisableFeatures({ChromeFeatureList.ENHANCED_PROTECTION_PROMO_CARD,
ChromeFeatureList.INTEREST_FEED_V2})
@DataFilePath(TEST_FEED_DATA_BASE_PATH + "feed_world.gcl.bin")
public void testTextBoxBackgroundColor() throws Exception {
// clang-format on
RecyclerView recycleView =
(RecyclerView) mNtp.getCoordinatorForTesting().getStreamForTesting().getView();
Resources resources = mActivityTestRule.getActivity().getResources();
Assert.assertEquals(ChromeColors.getPrimaryBackgroundColor(resources, false),
mNtp.getToolbarTextBoxBackgroundColor(Color.BLACK));
// Trigger a refresh to get feed cards so that page becomes long enough
// for the location bar to get placed to the top of the page after scrolling.
mFeedDataInjector.triggerFeedRefreshOnUiThreadBlocking(
mNtp.getCoordinatorForTesting().getStreamForTesting());
// Scroll to the bottom.
RecyclerViewTestUtils.scrollToBottom(recycleView);
RecyclerViewTestUtils.waitForStableRecyclerView(recycleView);
Assert.assertTrue(mNtp.isLocationBarScrolledToTopInNtp());
Assert.assertEquals(
ApiCompatibilityUtils.getColor(resources, R.color.toolbar_text_box_background),
mNtp.getToolbarTextBoxBackgroundColor(Color.BLACK));
}
}
......@@ -4,8 +4,11 @@
package org.chromium.chrome.browser.ui.native_page;
import android.graphics.Color;
import android.view.View;
import androidx.annotation.ColorInt;
/**
* A empty stand-in for a native page. An inactive NativePage may be replaced with a
* FrozenNativePage to free up resources.
......@@ -14,6 +17,8 @@ import android.view.View;
* return null.
*/
public class FrozenNativePage implements NativePage {
@ColorInt
private static final int INVALID_COLOR = Color.TRANSPARENT;
private final String mUrl;
private final String mHost;
private final String mTitle;
......@@ -61,6 +66,21 @@ public class FrozenNativePage implements NativePage {
return mBackgroundColor;
}
@Override
public @ColorInt int getToolbarTextBoxBackgroundColor(@ColorInt int defaultColor) {
return INVALID_COLOR;
}
@Override
public @ColorInt int getToolbarSceneLayerBackground(@ColorInt int defaultColor) {
return INVALID_COLOR;
}
@Override
public float getToolbarTextBoxAlpha(float defaultAlpha) {
return 1.f;
}
@Override
public boolean needsToolbarShadow() {
return true;
......
......@@ -7,6 +7,7 @@ package org.chromium.chrome.browser.ui.native_page;
import android.net.Uri;
import android.view.View;
import androidx.annotation.ColorInt;
import androidx.annotation.IntDef;
import org.chromium.components.embedder_support.util.UrlConstants;
......@@ -43,6 +44,31 @@ public interface NativePage {
*/
int getBackgroundColor();
/**
* @param defaultColor Default color if not customized.
* @return The color of the toolbar textbox background.
*/
default @ColorInt int getToolbarTextBoxBackgroundColor(@ColorInt int defaultColor) {
return defaultColor;
}
/**
* @param defaultColor Default color if not customized.
* @return The toolbar (or browser controls) color used in the compositor scene layer.
* @see {@link Toolbar#getToolbarSceneLayerBackground()}
*/
default @ColorInt int getToolbarSceneLayerBackground(@ColorInt int defaultColor) {
return defaultColor;
}
/**
* @param defaultAlpha Default alpha if not customized.
* @return Alpha for the toolbar textbox.
*/
default float getToolbarTextBoxAlpha(float defaultColor) {
return defaultColor;
}
/**
* @return True if the native page needs the toolbar shadow to be drawn.
*/
......
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