Commit fe77ecf0 authored by Ehimare Okoyomon's avatar Ehimare Okoyomon Committed by Commit Bot

Add managed state to Incognito NTP Cookie Controls Toggle

Add support and UI to disable incognito ntp toggle if that setting is
managed.
EnforcedByPolicy: https://screenshot.googleplex.com/BoEVngjYMsn.png
EnforcedBySettings: https://screenshot.googleplex.com/21UPKQSmc31.png

Bug: 1040091
Change-Id: Ie1f269af20a82025d4ec3d6f92e9c5e8b061f3c3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2062445
Commit-Queue: Ehimare Okoyomon <eokoyomon@chromium.org>
Reviewed-by: default avatarEvan Stade <estade@chromium.org>
Reviewed-by: default avatarLijin Shen <lazzzis@chromium.org>
Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Reviewed-by: default avatarChristian Dullweber <dullweber@chromium.org>
Reviewed-by: default avatarGanggui Tang <gogerald@chromium.org>
Cr-Commit-Position: refs/heads/master@{#746581}
parent 6eaf42ce
......@@ -428,6 +428,7 @@ android_library("chrome_java") {
"//chrome/browser:sharing_send_message_result_generated_enum",
"//chrome/browser/notifications/scheduler/public:jni_enums",
"//chrome/browser/supervised_user/supervised_user_error_page:enums_srcjar",
"//chrome/browser/ui:cookie_controls_enforcement_javagen",
"//chrome/browser/ui:tab_model_enums_java",
"//chrome/browser/updates/announcement_notification:jni_enums",
"//components/autofill_assistant/browser:autofill_assistant_enums_java",
......
......@@ -10,6 +10,7 @@ import static org.chromium.chrome.browser.tasks.TasksSurfaceProperties.INCOGNITO
import static org.chromium.chrome.browser.tasks.TasksSurfaceProperties.INCOGNITO_COOKIE_CONTROLS_MANAGER;
import static org.chromium.chrome.browser.tasks.TasksSurfaceProperties.INCOGNITO_COOKIE_CONTROLS_TOGGLE_CHECKED;
import static org.chromium.chrome.browser.tasks.TasksSurfaceProperties.INCOGNITO_COOKIE_CONTROLS_TOGGLE_CHECKED_LISTENER;
import static org.chromium.chrome.browser.tasks.TasksSurfaceProperties.INCOGNITO_COOKIE_CONTROLS_TOGGLE_ENFORCEMENT;
import static org.chromium.chrome.browser.tasks.TasksSurfaceProperties.INCOGNITO_LEARN_MORE_CLICK_LISTENER;
import static org.chromium.chrome.browser.tasks.TasksSurfaceProperties.IS_FAKE_SEARCH_BOX_VISIBLE;
import static org.chromium.chrome.browser.tasks.TasksSurfaceProperties.IS_TAB_CAROUSEL_VISIBLE;
......@@ -27,6 +28,7 @@ import org.chromium.chrome.browser.ntp.FakeboxDelegate;
import org.chromium.chrome.browser.ntp.IncognitoCookieControlsManager;
import org.chromium.chrome.browser.omnibox.LocationBar;
import org.chromium.chrome.browser.omnibox.voice.VoiceRecognitionHandler;
import org.chromium.components.content_settings.CookieControlsEnforcement;
import org.chromium.ui.modelutil.PropertyModel;
/**
......@@ -88,10 +90,8 @@ class TasksSurfaceMediator {
mModel.set(INCOGNITO_COOKIE_CONTROLS_MANAGER, mIncognitoCookieControlsManager);
mIncognitoCookieControlsObserver = new IncognitoCookieControlsManager.Observer() {
@Override
public void onUpdate(boolean checked, boolean enforced) {
// TODO(crbug.com/1040091): use enforced to support the case where this toggle is
// managed by organization or the normal 3PC blocking setting and update the UI
// accordingly.
public void onUpdate(boolean checked, @CookieControlsEnforcement int enforcement) {
mModel.set(INCOGNITO_COOKIE_CONTROLS_TOGGLE_ENFORCEMENT, enforcement);
mModel.set(INCOGNITO_COOKIE_CONTROLS_TOGGLE_CHECKED, checked);
}
};
......
......@@ -41,6 +41,9 @@ public class TasksSurfaceProperties {
public static final PropertyModel.WritableObjectPropertyKey<OnCheckedChangeListener>
INCOGNITO_COOKIE_CONTROLS_TOGGLE_CHECKED_LISTENER =
new PropertyModel.WritableObjectPropertyKey<OnCheckedChangeListener>();
public static final PropertyModel
.WritableIntPropertyKey INCOGNITO_COOKIE_CONTROLS_TOGGLE_ENFORCEMENT =
new PropertyModel.WritableIntPropertyKey();
public static final PropertyModel.WritableObjectPropertyKey<IncognitoCookieControlsManager>
INCOGNITO_COOKIE_CONTROLS_MANAGER =
new PropertyModel.WritableObjectPropertyKey<IncognitoCookieControlsManager>();
......@@ -64,7 +67,8 @@ public class TasksSurfaceProperties {
IS_INCOGNITO, IS_INCOGNITO_DESCRIPTION_INITIALIZED, IS_INCOGNITO_DESCRIPTION_VISIBLE,
IS_TAB_CAROUSEL_VISIBLE, IS_VOICE_RECOGNITION_BUTTON_VISIBLE,
INCOGNITO_COOKIE_CONTROLS_CARD_VISIBILITY, INCOGNITO_COOKIE_CONTROLS_TOGGLE_CHECKED,
INCOGNITO_COOKIE_CONTROLS_TOGGLE_CHECKED_LISTENER, INCOGNITO_COOKIE_CONTROLS_MANAGER,
INCOGNITO_COOKIE_CONTROLS_TOGGLE_CHECKED_LISTENER,
INCOGNITO_COOKIE_CONTROLS_TOGGLE_ENFORCEMENT, INCOGNITO_COOKIE_CONTROLS_MANAGER,
INCOGNITO_LEARN_MORE_CLICK_LISTENER, FAKE_SEARCH_BOX_CLICK_LISTENER,
FAKE_SEARCH_BOX_TEXT_WATCHER, MORE_TABS_CLICK_LISTENER, MV_TILES_VISIBLE,
VOICE_SEARCH_BUTTON_CLICK_LISTENER};
......
......@@ -26,6 +26,7 @@ import org.chromium.chrome.browser.ntp.IncognitoDescriptionView;
import org.chromium.chrome.browser.ntp.NewTabPageLayout.SearchBoxContainerView;
import org.chromium.chrome.tab_ui.R;
import org.chromium.components.browser_ui.styles.ChromeColors;
import org.chromium.components.content_settings.CookieControlsEnforcement;
// The view of the tasks surface.
class TasksView extends CoordinatorLayoutForPointer {
......@@ -40,6 +41,8 @@ class TasksView extends CoordinatorLayoutForPointer {
private boolean mIncognitoCookieControlsCardIsVisible;
private boolean mIncognitoCookieControlsToggleIsChecked;
private OnCheckedChangeListener mIncognitoCookieControlsToggleCheckedListener;
private @CookieControlsEnforcement int mIncognitoCookieControlsToggleEnforcement =
CookieControlsEnforcement.NO_ENFORCEMENT;
/** Default constructor needed to inflate via XML. */
public TasksView(Context context, AttributeSet attrs) {
......@@ -175,6 +178,7 @@ class TasksView extends CoordinatorLayoutForPointer {
mIncognitoCookieControlsToggleCheckedListener);
mIncognitoCookieControlsToggleCheckedListener = null;
}
setIncognitoCookieControlsToggleEnforcement(mIncognitoCookieControlsToggleEnforcement);
}
/**
......@@ -229,4 +233,15 @@ class TasksView extends CoordinatorLayoutForPointer {
mIncognitoDescriptionView.setCookieControlsToggleOnCheckedChangeListener(listener);
}
}
/**
* Set the enforcement rule for the incognito cookie controls toggle.
* @param enforcement The enforcement enum to set.
*/
void setIncognitoCookieControlsToggleEnforcement(@CookieControlsEnforcement int enforcement) {
mIncognitoCookieControlsToggleEnforcement = enforcement;
if (mIncognitoDescriptionView != null) {
mIncognitoDescriptionView.setCookieControlsEnforcement(enforcement);
}
}
}
......@@ -10,6 +10,7 @@ import static org.chromium.chrome.browser.tasks.TasksSurfaceProperties.INCOGNITO
import static org.chromium.chrome.browser.tasks.TasksSurfaceProperties.INCOGNITO_COOKIE_CONTROLS_MANAGER;
import static org.chromium.chrome.browser.tasks.TasksSurfaceProperties.INCOGNITO_COOKIE_CONTROLS_TOGGLE_CHECKED;
import static org.chromium.chrome.browser.tasks.TasksSurfaceProperties.INCOGNITO_COOKIE_CONTROLS_TOGGLE_CHECKED_LISTENER;
import static org.chromium.chrome.browser.tasks.TasksSurfaceProperties.INCOGNITO_COOKIE_CONTROLS_TOGGLE_ENFORCEMENT;
import static org.chromium.chrome.browser.tasks.TasksSurfaceProperties.INCOGNITO_LEARN_MORE_CLICK_LISTENER;
import static org.chromium.chrome.browser.tasks.TasksSurfaceProperties.IS_FAKE_SEARCH_BOX_VISIBLE;
import static org.chromium.chrome.browser.tasks.TasksSurfaceProperties.IS_INCOGNITO;
......@@ -42,6 +43,9 @@ class TasksViewBinder {
} else if (propertyKey == INCOGNITO_COOKIE_CONTROLS_TOGGLE_CHECKED_LISTENER) {
view.setIncognitoCookieControlsToggleCheckedListener(
model.get(INCOGNITO_COOKIE_CONTROLS_TOGGLE_CHECKED_LISTENER));
} else if (propertyKey == INCOGNITO_COOKIE_CONTROLS_TOGGLE_ENFORCEMENT) {
view.setIncognitoCookieControlsToggleEnforcement(
model.get(INCOGNITO_COOKIE_CONTROLS_TOGGLE_ENFORCEMENT));
} else if (propertyKey == INCOGNITO_LEARN_MORE_CLICK_LISTENER) {
view.setIncognitoDescriptionLearnMoreClickListener(
model.get(INCOGNITO_LEARN_MORE_CLICK_LISTENER));
......
......@@ -77,6 +77,16 @@
android:layout_marginTop="24dp"
android:padding="16dp">
<ImageView
android:id="@+id/cookie_controls_card_managed_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="ContentDescription"
android:layout_marginEnd="10dp"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
app:tint="@color/default_icon_color_light" />
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/cookie_controls_card_toggle"
android:layout_width="wrap_content"
......@@ -90,16 +100,18 @@
android:id="@+id/cookie_controls_card_title"
android:text="@string/new_tab_otr_third_party_cookie"
android:textAppearance="@style/TextAppearance.TextLarge.Primary.Light"
android:layout_toStartOf="@id/cookie_controls_card_toggle"
android:layout_toEndOf="@id/cookie_controls_card_managed_icon"
android:layout_marginBottom="@dimen/md_incognito_ntp_line_spacing"
android:layout_alignParentStart="true"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
<org.chromium.ui.widget.TextViewWithLeading
android:id="@+id/cookie_controls_card_subtitle"
android:text="@string/new_tab_otr_third_party_cookie_sublabel"
android:textAppearance="@style/TextAppearance.TextMedium.Secondary.Light"
android:layout_alignParentStart="true"
android:layout_toStartOf="@id/cookie_controls_card_toggle"
android:layout_toEndOf="@id/cookie_controls_card_managed_icon"
android:layout_below="@id/cookie_controls_card_title"
app:leading="@dimen/text_size_medium_leading"
android:layout_height="wrap_content"
......
......@@ -12,6 +12,7 @@ import org.chromium.chrome.R;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.settings.website.CookieControlsServiceBridge;
import org.chromium.chrome.browser.settings.website.CookieControlsServiceBridge.CookieControlsServiceObserver;
import org.chromium.components.content_settings.CookieControlsEnforcement;
/**
* A manager for cookie controls related behaviour on the incognito description view.
......@@ -30,16 +31,17 @@ public class IncognitoCookieControlsManager
* Notifies that this manager has received an update.
* @param checked A boolean indicating whether the toggle indicating third-party cookies are
* currently being blocked should be checked or not.
* @param enforced A boolean indicating if third-party cookies being blocked is currently
* enforced by policy/cookie settings or not.
* @param enforcement A CookieControlsEnforcement enum type indicating the enforcement rule
* for these cookie controls.
*/
void onUpdate(boolean checked, boolean enforced);
void onUpdate(boolean checked, @CookieControlsEnforcement int enforcement);
}
private CookieControlsServiceBridge mServiceBridge;
private final ObserverList<Observer> mObservers = new ObserverList<>();
private boolean mIsInitialized;
private boolean mShowCard;
private @CookieControlsEnforcement int mEnforcement = CookieControlsEnforcement.NO_ENFORCEMENT;
/**
* Initializes the IncognitoCookieControlsManager explicitly.
......@@ -84,15 +86,18 @@ public class IncognitoCookieControlsManager
}
@Override
public void sendCookieControlsUIChanges(boolean checked, boolean enforced) {
public void sendCookieControlsUIChanges(
boolean checked, @CookieControlsEnforcement int enforcement) {
mEnforcement = enforcement;
for (Observer obs : mObservers) {
obs.onUpdate(checked, enforced);
obs.onUpdate(checked, enforcement);
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mShowCard && (buttonView.getId() == R.id.cookie_controls_card_toggle)) {
if (mShowCard && mEnforcement == CookieControlsEnforcement.NO_ENFORCEMENT
&& (buttonView.getId() == R.id.cookie_controls_card_toggle)) {
mServiceBridge.handleCookieControlsToggleChanged(isChecked);
}
}
......
......@@ -8,6 +8,7 @@ import static org.chromium.ui.base.ViewUtils.dpToPx;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.support.v7.widget.SwitchCompat;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
......@@ -26,10 +27,13 @@ import androidx.annotation.StringRes;
import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.chrome.R;
import org.chromium.components.content_settings.CookieControlsEnforcement;
import org.chromium.ui.text.NoUnderlineClickableSpan;
import org.chromium.ui.text.SpanApplier;
import org.chromium.ui.widget.ChromeBulletSpan;
import java.util.Locale;
/**
* The view to describle incognito mode.
*/
......@@ -45,6 +49,9 @@ public class IncognitoDescriptionView extends LinearLayout {
private TextView[] mParagraphs;
private RelativeLayout mCookieControlsCard;
private SwitchCompat mCookieControlsToggle;
private ImageView mCookieControlsManagedIcon;
private TextView mCookieControlsTitle;
private TextView mCookieControlsSubtitle;
private static final int BULLETPOINTS_HORIZONTAL_SPACING_DP = 40;
private static final int CONTENT_WIDTH_DP = 600;
......@@ -98,6 +105,9 @@ public class IncognitoDescriptionView extends LinearLayout {
mBulletpointsContainer = findViewById(R.id.new_tab_incognito_bulletpoints_container);
mCookieControlsCard = findViewById(R.id.cookie_controls_card);
mCookieControlsToggle = findViewById(R.id.cookie_controls_card_toggle);
mCookieControlsManagedIcon = findViewById(R.id.cookie_controls_card_managed_icon);
mCookieControlsTitle = findViewById(R.id.cookie_controls_card_title);
mCookieControlsSubtitle = findViewById(R.id.cookie_controls_card_subtitle);
adjustView();
}
......@@ -296,4 +306,55 @@ public class IncognitoDescriptionView extends LinearLayout {
public void showCookieControlsCard(boolean showCard) {
mCookieControlsCard.setVisibility(showCard ? View.VISIBLE : View.GONE);
}
/**
* Sets the cookie controls enforced state.
* @param enforcement A CookieControlsEnforcement enum type indicating the type of
* enforcement policy being applied to Cookie Controls.
*/
public void setCookieControlsEnforcement(@CookieControlsEnforcement int enforcement) {
boolean enforced = enforcement != CookieControlsEnforcement.NO_ENFORCEMENT;
mCookieControlsToggle.setEnabled(!enforced);
mCookieControlsManagedIcon.setVisibility(enforced ? View.VISIBLE : View.GONE);
mCookieControlsTitle.setEnabled(!enforced);
mCookieControlsSubtitle.setEnabled(!enforced);
Resources resources = getContext().getResources();
StringBuilder subtitleText = new StringBuilder();
subtitleText.append(resources.getString(R.string.new_tab_otr_third_party_cookie_sublabel));
if (!enforced) {
mCookieControlsSubtitle.setText(subtitleText.toString());
return;
}
int iconRes;
String addition;
switch (enforcement) {
case CookieControlsEnforcement.ENFORCED_BY_POLICY:
iconRes = R.drawable.controlled_setting_mandatory;
addition = resources.getString(R.string.managed_by_your_organization);
break;
case CookieControlsEnforcement.ENFORCED_BY_COOKIE_SETTING:
iconRes = R.drawable.settings_cog;
int placeholderTextId =
R.string.new_tab_otr_cookie_controls_controlled_tooltip_text;
// Need to construct the string by replacing the placeholder text with the correct
// strings. However, since this text is shared with desktop, the placeholder
// characters for arguments are different, so we first need to swap in the correct
// placeholder characters.
// TODO(crbug.com/1040091): Look into changing this string on Android and Desktop
// to not use placeholders at all.
addition = String.format(Locale.getDefault(),
resources.getString(placeholderTextId).replaceAll("\\$(\\d+)", "%$1\\$s"),
resources.getString(R.string.block_third_party_cookies_title),
resources.getString(R.string.cookies_title));
break;
default:
return;
}
mCookieControlsManagedIcon.setImageResource(iconRes);
subtitleText.append("\n");
subtitleText.append(addition);
mCookieControlsSubtitle.setText(subtitleText.toString());
}
}
......@@ -22,6 +22,7 @@ import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.ui.native_page.BasicNativePage;
import org.chromium.chrome.browser.ui.native_page.NativePageHost;
import org.chromium.chrome.browser.vr.VrModuleProvider;
import org.chromium.components.content_settings.CookieControlsEnforcement;
import org.chromium.components.embedder_support.util.UrlConstants;
/**
......@@ -82,10 +83,10 @@ public class IncognitoNewTabPage
mCookieControlsManager.shouldShowCookieControlsCard());
mCookieControlsObserver = new IncognitoCookieControlsManager.Observer() {
@Override
public void onUpdate(boolean checked, boolean enforced) {
// TODO(crbug.com/1040091): use enforced to support the case where this
// toggle is managed by organization or the normal 3PC blocking setting and
// update the UI accordingly.
public void onUpdate(
boolean checked, @CookieControlsEnforcement int enforcement) {
mIncognitoNewTabPageView.setIncognitoCookieControlsToggleEnforcement(
enforcement);
mIncognitoNewTabPageView.setIncognitoCookieControlsToggleChecked(checked);
}
};
......
......@@ -13,6 +13,7 @@ import android.widget.FrameLayout;
import org.chromium.base.ApiCompatibilityUtils;
import org.chromium.chrome.R;
import org.chromium.components.content_settings.CookieControlsEnforcement;
import org.chromium.ui.base.ViewUtils;
/**
......@@ -151,4 +152,12 @@ public class IncognitoNewTabPageView extends FrameLayout {
void setIncognitoCookieControlsToggleCheckedListener(OnCheckedChangeListener listener) {
mDescriptionView.setCookieControlsToggleOnCheckedChangeListener(listener);
}
/**
* Set the enforcement rule for the incognito cookie controls toggle.
* @param enforcement The enforcement enum to set.
*/
void setIncognitoCookieControlsToggleEnforcement(@CookieControlsEnforcement int enforcement) {
mDescriptionView.setCookieControlsEnforcement(enforcement);
}
}
......@@ -6,6 +6,7 @@ package org.chromium.chrome.browser.settings.website;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.components.content_settings.CookieControlsEnforcement;
/**
* Communicates between CookieControlsService (C++ backend) and observers in the Incognito NTP Java
......@@ -21,10 +22,11 @@ public class CookieControlsServiceBridge {
* Called when there is an update in the cookie controls that should be reflected in the UI.
* @param checked A boolean indicating whether the toggle indicating third-party cookies are
* currently being blocked should be checked or not.
* @param enforced A boolean indicating if third-party cookies being blocked is currently
* enforced by policy/cookie settings or not.
* @param enforcement A CookieControlsEnforcement enum type indicating the enforcement rule
* for these cookie controls.
*/
public void sendCookieControlsUIChanges(boolean checked, boolean enforced);
public void sendCookieControlsUIChanges(
boolean checked, @CookieControlsEnforcement int enforcement);
}
private long mNativeCookieControlsServiceBridge;
......@@ -70,8 +72,9 @@ public class CookieControlsServiceBridge {
}
@CalledByNative
private void sendCookieControlsUIChanges(boolean checked, boolean enforced) {
mObserver.sendCookieControlsUIChanges(checked, enforced);
private void sendCookieControlsUIChanges(
boolean checked, @CookieControlsEnforcement int enforcement) {
mObserver.sendCookieControlsUIChanges(checked, enforcement);
}
@NativeMethods
......
......@@ -4,9 +4,21 @@
package org.chromium.chrome.browser.ntp;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.scrollTo;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isChecked;
import static android.support.test.espresso.matcher.ViewMatchers.isEnabled;
import static android.support.test.espresso.matcher.ViewMatchers.isNotChecked;
import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.not;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.filters.SmallTest;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import org.junit.Assert;
import org.junit.Before;
......@@ -44,6 +56,25 @@ public class IncognitoNewTabPageTest {
mActivityTestRule.startMainActivityOnBlankPage();
}
private void setThirdPartyCookieBlocking(boolean enabled) {
TestThreadUtils.runOnUiThreadBlocking(() -> {
PrefServiceBridge.getInstance().setBoolean(Pref.BLOCK_THIRD_PARTY_COOKIES, enabled);
});
}
private void setCookieControlsMode(@CookieControlsMode int mode) {
TestThreadUtils.runOnUiThreadBlocking(() -> {
PrefServiceBridge.getInstance().setInteger(Pref.COOKIE_CONTROLS_MODE, mode);
});
}
private void assertCookieControlsMode(@CookieControlsMode int mode) {
TestThreadUtils.runOnUiThreadBlocking(() -> {
Assert.assertEquals(
PrefServiceBridge.getInstance().getInteger(Pref.COOKIE_CONTROLS_MODE), mode);
});
}
/**
* Test cookie controls card is GONE when cookie controls flag disabled.
*/
......@@ -52,15 +83,8 @@ public class IncognitoNewTabPageTest {
@DisableFeatures(ChromeFeatureList.IMPROVED_COOKIE_CONTROLS)
public void testCookieControlsCardGONE() throws Exception {
mActivityTestRule.newIncognitoTabFromMenu();
final IncognitoNewTabPage ntp = (IncognitoNewTabPage) mActivityTestRule.getActivity()
.getActivityTab()
.getNativePage();
TestThreadUtils.runOnUiThreadBlocking(() -> {
Assert.assertEquals(
ntp.getView().findViewById(R.id.cookie_controls_card).getVisibility(),
View.GONE);
});
onView(withId(R.id.cookie_controls_card))
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.GONE)));
}
/**
......@@ -69,29 +93,15 @@ public class IncognitoNewTabPageTest {
@Test
@SmallTest
public void testCookieControlsToggleStartsOn() throws Exception {
TestThreadUtils.runOnUiThreadBlocking(() -> {
// Set normal third-party cookie blocking to off.
PrefServiceBridge.getInstance().setBoolean(Pref.BLOCK_THIRD_PARTY_COOKIES, false);
// Set CookieControlsMode Pref to On
PrefServiceBridge.getInstance().setInteger(
Pref.COOKIE_CONTROLS_MODE, CookieControlsMode.INCOGNITO_ONLY);
});
setThirdPartyCookieBlocking(false);
setCookieControlsMode(CookieControlsMode.INCOGNITO_ONLY);
mActivityTestRule.newIncognitoTabFromMenu();
final IncognitoNewTabPage ntp = (IncognitoNewTabPage) mActivityTestRule.getActivity()
.getActivityTab()
.getNativePage();
TestThreadUtils.runOnUiThreadBlocking(() -> {
// Make sure cookie controls card is visible
Assert.assertEquals(
ntp.getView().findViewById(R.id.cookie_controls_card).getVisibility(),
View.VISIBLE);
// Assert the cookie controls toggle is checked
Assert.assertTrue(
((SwitchCompat) ntp.getView().findViewById(R.id.cookie_controls_card_toggle))
.isChecked());
});
// Make sure cookie controls card is visible
onView(withId(R.id.cookie_controls_card))
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
// Assert the cookie controls toggle is checked
onView(withId(R.id.cookie_controls_card_toggle)).check(matches(isChecked()));
}
/**
......@@ -100,38 +110,53 @@ public class IncognitoNewTabPageTest {
@Test
@SmallTest
public void testCookieControlsToggleChanges() throws Exception {
TestThreadUtils.runOnUiThreadBlocking(() -> {
// Set normal third-party cookie blocking to off.
PrefServiceBridge.getInstance().setBoolean(Pref.BLOCK_THIRD_PARTY_COOKIES, false);
// Set CookieControlsMode Pref to Off
PrefServiceBridge.getInstance().setInteger(
Pref.COOKIE_CONTROLS_MODE, CookieControlsMode.OFF);
});
setThirdPartyCookieBlocking(false);
setCookieControlsMode(CookieControlsMode.OFF);
mActivityTestRule.newIncognitoTabFromMenu();
onView(withId(R.id.cookie_controls_card))
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
int toggle_id = R.id.cookie_controls_card_toggle;
// Toggle should start unchecked
onView(withId(toggle_id)).check(matches(isNotChecked()));
// Toggle should be checked after click
onView(withId(toggle_id)).perform(scrollTo(), click()).check(matches(isChecked()));
// CookieControlsMode should be incognito_only
assertCookieControlsMode(CookieControlsMode.INCOGNITO_ONLY);
// Toggle should be unchecked again after click
onView(withId(toggle_id)).perform(scrollTo(), click()).check(matches(isNotChecked()));
// CookieControlsMode should be off
assertCookieControlsMode(CookieControlsMode.OFF);
}
/**
* Test cookie controls disabled if managed by settings.
*/
@Test
@SmallTest
public void testCookieControlsToggleManaged() throws Exception {
setThirdPartyCookieBlocking(false);
setCookieControlsMode(CookieControlsMode.INCOGNITO_ONLY);
mActivityTestRule.newIncognitoTabFromMenu();
final IncognitoNewTabPage ntp = (IncognitoNewTabPage) mActivityTestRule.getActivity()
.getActivityTab()
.getNativePage();
onView(withId(R.id.cookie_controls_card))
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
TestThreadUtils.runOnUiThreadBlocking(() -> {
// Make sure cookie controls card is visible
Assert.assertEquals(
ntp.getView().findViewById(R.id.cookie_controls_card).getVisibility(),
View.VISIBLE);
SwitchCompat toggle =
(SwitchCompat) ntp.getView().findViewById(R.id.cookie_controls_card_toggle);
Assert.assertFalse("Toggle should be unchecked", toggle.isChecked());
toggle.performClick();
Assert.assertTrue("Toggle should be checked", toggle.isChecked());
Assert.assertEquals("CookieControlsMode should be incognito_only",
PrefServiceBridge.getInstance().getInteger(Pref.COOKIE_CONTROLS_MODE),
CookieControlsMode.INCOGNITO_ONLY);
toggle.performClick();
Assert.assertFalse("Toggle should be unchecked again", toggle.isChecked());
Assert.assertEquals("CookieControlsMode should be off",
PrefServiceBridge.getInstance().getInteger(Pref.COOKIE_CONTROLS_MODE),
CookieControlsMode.OFF);
});
int toggle_id = R.id.cookie_controls_card_toggle;
// Toggle should start checked and enabled
onView(withId(toggle_id)).check(matches(allOf(isChecked(), isEnabled())));
// Toggle should be disabled if managed by setting
setThirdPartyCookieBlocking(true);
onView(withId(toggle_id)).check(matches(not(isEnabled())));
// Toggle should be enabled and remain checked
setThirdPartyCookieBlocking(false);
onView(withId(toggle_id)).check(matches(allOf(isChecked(), isEnabled())));
// Repeat of above but toggle should remain unchecked
onView(withId(toggle_id)).perform(scrollTo(), click());
onView(withId(toggle_id)).check(matches(allOf(isNotChecked(), isEnabled())));
setThirdPartyCookieBlocking(true);
onView(withId(toggle_id)).check(matches(not(isEnabled())));
setThirdPartyCookieBlocking(false);
onView(withId(toggle_id)).check(matches(allOf(isNotChecked(), isEnabled())));
}
}
......@@ -25,6 +25,7 @@ import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.util.browser.Features.EnableFeatures;
import org.chromium.components.content_settings.CookieControlsEnforcement;
import org.chromium.components.content_settings.CookieControlsMode;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.net.test.EmbeddedTestServer;
......@@ -45,9 +46,10 @@ public class CookieControlsServiceBridgeTest {
}
@Override
public void sendCookieControlsUIChanges(boolean checked, boolean enforced) {
public void sendCookieControlsUIChanges(
boolean checked, @CookieControlsEnforcement int enforcement) {
mChecked = checked;
mEnforced = enforced;
mEnforcement = enforcement;
mHelper.notifyCalled();
}
}
......@@ -60,7 +62,7 @@ public class CookieControlsServiceBridgeTest {
private TestCallbackHandler mCallbackHandler;
private CookieControlsServiceBridge mCookieControlsServiceBridge;
private boolean mChecked;
private boolean mEnforced;
private @CookieControlsEnforcement int mEnforcement;
@Before
public void setUp() throws Exception {
......@@ -126,13 +128,13 @@ public class CookieControlsServiceBridgeTest {
// Test that the toggle switches back on and enforced (by settings)
expectedChecked = true;
mChecked = false;
boolean expectedEnforced = true;
mEnforced = false;
int expectedEnforcement = CookieControlsEnforcement.ENFORCED_BY_COOKIE_SETTING;
mEnforcement = CookieControlsEnforcement.NO_ENFORCEMENT;
currentCallCount = mCallbackHelper.getCallCount();
setThirdPartyCookieBlocking(true);
mCallbackHelper.waitForCallback(currentCallCount, 1);
Assert.assertEquals(expectedChecked, mChecked);
Assert.assertEquals(expectedEnforced, mEnforced);
Assert.assertEquals(expectedEnforcement, mEnforcement);
}
/**
......
......@@ -43,10 +43,11 @@ void CookieControlsServiceBridge::UpdateServiceIfNecessary(JNIEnv* env) {
void CookieControlsServiceBridge::SendCookieControlsUIChanges() {
bool checked = service_->GetToggleCheckedValue();
bool enforced = service_->ShouldEnforceCookieControls();
CookieControlsEnforcement enforcement =
service_->GetCookieControlsEnforcement();
JNIEnv* env = base::android::AttachCurrentThread();
Java_CookieControlsServiceBridge_sendCookieControlsUIChanges(
env, jobject_, checked, enforced);
env, jobject_, checked, static_cast<int>(enforcement));
}
void CookieControlsServiceBridge::OnThirdPartyCookieBlockingPrefChanged() {
......
......@@ -4381,6 +4381,10 @@ if (is_chromeos) {
}
if (is_android) {
java_cpp_enum("cookie_controls_enforcement_javagen") {
sources = [ "cookie_controls/cookie_controls_service.h" ]
}
java_cpp_enum("tab_model_enums_java") {
sources = [ "android/tab_model/tab_model.h" ]
}
......
......@@ -24,10 +24,12 @@ namespace policy {
class PolicyChangeRegistrar;
}
// GENERATED_JAVA_ENUM_PACKAGE: org.chromium.components.content_settings
enum class CookieControlsEnforcement {
kNoEnforcement,
kEnforcedByPolicy,
kEnforcedByCookieSetting
kNoEnforcement = 0,
kEnforcedByPolicy = 1,
kEnforcedByCookieSetting = 2,
kMaxValue = kEnforcedByCookieSetting,
};
// // Handles the global state for cookie settings changes and observation.
......
......@@ -166,7 +166,8 @@
<ph name="END_LIST">&lt;/ul&gt;</ph>
</message>
<message name="IDS_NEW_TAB_OTR_COOKIE_CONTROLS_CONTROLLED_TOOLTIP_TEXT"
desc="Text displayed in the settings tooltip when third-party cookie blocking is managed or turned on by the user.">
desc="Text displayed in the settings tooltip when third-party cookie blocking is managed or turned on by the user."
formatter_data="android_java">
This setting is enforced by &quot;<ph name="ENFORCING_SETTING">$1<ex>Block third-party cookies</ex></ph>&quot; in &quot;<ph name="SETTINGS_PAGE">$2<ex>Cookies and site data</ex></ph>&quot;
</message>
<message name="IDS_NEW_TAB_OTR_THIRD_PARTY_COOKIE" desc="Label for the Block 3rd-party cookie checkbox on the Cookies category." formatter_data="android_java">
......
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="@dimen/default_disabled_alpha"
android:color="@color/default_text_color_light" android:state_enabled="false"/>
<item android:color="@color/default_text_color_light" />
</selector>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<!-- 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.
-->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="@dimen/default_disabled_alpha"
android:color="@color/default_text_color_secondary_light" android:state_enabled="false"/>
<item android:color="@color/default_text_color_secondary_light" />
</selector>
\ No newline at end of file
......@@ -205,26 +205,26 @@
<!-- Non Adaptive Text Styles -->
<!-- Light version -->
<style name="TextAppearance.Headline.Primary.Light" tools:ignore="UnusedResources">
<item name="android:textColor">@color/default_text_color_light</item>
<item name="android:textColor">@color/default_text_color_light_list</item>
</style>
<style name="TextAppearance.TextLarge.Primary.Light" tools:ignore="UnusedResources">
<item name="android:textColor">@color/default_text_color_light</item>
<item name="android:textColor">@color/default_text_color_light_list</item>
</style>
<style name="TextAppearance.TextMediumThick.Primary.Light" tools:ignore="UnusedResources">
<item name="android:textColor">@color/default_text_color_light</item>
<item name="android:textColor">@color/default_text_color_light_list</item>
</style>
<style name="TextAppearance.TextMedium.Primary.Light" tools:ignore="UnusedResources">
<item name="android:textColor">@color/default_text_color_light</item>
<item name="android:textColor">@color/default_text_color_light_list</item>
</style>
<style name="TextAppearance.TextSmall.Primary.Light" tools:ignore="UnusedResources">
<item name="android:textColor">@color/default_text_color_light</item>
<item name="android:textColor">@color/default_text_color_light_list</item>
</style>
<style name="TextAppearance.TextLarge.Secondary.Light" tools:ignore="UnusedResources">
<item name="android:textColor">@color/default_text_color_secondary_light</item>
<item name="android:textColor">@color/default_text_color_secondary_light_list</item>
</style>
<style name="TextAppearance.TextMedium.Secondary.Light" tools:ignore="UnusedResources">
<item name="android:textColor">@color/default_text_color_secondary_light</item>
<item name="android:textColor">@color/default_text_color_secondary_light_list</item>
</style>
<style name="TextAppearance.TextLarge.Tertiary.Light" tools:ignore="UnusedResources">
......
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