Commit 73240199 authored by Christian Dullweber's avatar Christian Dullweber Committed by Commit Bot

Remove state variables from FourStateCookieSettingsPreference

The preference only needs to know which radio button is selected
to determine its state. Removing the variables avoids inconsistencies.
The values used for initialization are moved into a class to avoid a
large number of boolean parameters.

Change-Id: I1cde64d6c14c396b2997aafdcbaf26a49453b25d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2139727
Commit-Queue: Christian Dullweber <dullweber@chromium.org>
Reviewed-by: default avatarFinnur Thorarinsson <finnur@chromium.org>
Reviewed-by: default avatarEhimare Okoyomon <eokoyomon@chromium.org>
Cr-Commit-Position: refs/heads/master@{#758881}
parent e7ea3e9e
......@@ -21,8 +21,6 @@ import org.chromium.components.browser_ui.widget.RadioButtonWithDescription;
import org.chromium.components.browser_ui.widget.text.TextViewWithCompoundDrawables;
import org.chromium.components.content_settings.CookieControlsMode;
import java.util.Arrays;
/**
* A 4-state radio group Preference used for the Cookies subpage of SiteSettings.
*/
......@@ -36,14 +34,25 @@ public class FourStateCookieSettingsPreference
BLOCK
}
// Signals used to determine the view and button states.
private boolean mCookiesContentSettingEnforced;
private boolean mThirdPartyBlockingEnforced;
private boolean mAllowCookies;
private boolean mBlockThirdPartyCookies;
/**
* Signals used to determine the view and button states.
*/
public static class Params {
// Whether the cookies content setting is enabled.
public boolean allowCookies;
// Whether third-party blocking is enabled.
public boolean blockThirdPartyCookies;
// An enum indicating when to block third-party cookies.
private @CookieControlsMode int mCookieControlsMode;
private CookieSettingsState mState = CookieSettingsState.UNINITIALIZED;
public @CookieControlsMode int cookieControlsMode;
// Whether the cookies content setting is enforced.
public boolean cookiesContentSettingEnforced;
// Whether third-party blocking is enforced.
public boolean thirdPartyBlockingEnforced;
}
// Keeps the params that are applied to the UI if the params are set before the UI is ready.
private Params mInitializationParams;
// UI Elements.
private RadioButtonWithDescription mAllowButton;
......@@ -66,23 +75,12 @@ public class FourStateCookieSettingsPreference
/**
* Sets the cookie settings state and updates the radio buttons.
* @param cookiesContentSettingEnforced Whether the cookies content setting is enforced.
* @param thirdPartyBlockingEnforced Whether third-party blocking is enforced.
* @param allowCookies Whether the cookies content setting is enabled.
* @param blockThirdPartyCookies Whether third-party blocking is enabled.
* @param cookieControlsMode The CookieControlsMode enum for the current cookie settings state.
*/
public void setState(boolean cookiesContentSettingEnforced, boolean thirdPartyBlockingEnforced,
boolean allowCookies, boolean blockThirdPartyCookies,
@CookieControlsMode int cookieControlsMode) {
mCookiesContentSettingEnforced = cookiesContentSettingEnforced;
mThirdPartyBlockingEnforced = thirdPartyBlockingEnforced;
mAllowCookies = allowCookies;
mBlockThirdPartyCookies = blockThirdPartyCookies;
mCookieControlsMode = cookieControlsMode;
public void setState(Params state) {
if (mRadioGroup != null) {
setRadioButtons();
configureRadioButtons(state);
} else {
mInitializationParams = state;
}
}
......@@ -90,22 +88,30 @@ public class FourStateCookieSettingsPreference
* @return The state that is currently selected.
*/
public CookieSettingsState getState() {
return mState;
if (mRadioGroup == null && mInitializationParams == null) {
return CookieSettingsState.UNINITIALIZED;
}
// Calculate the state from mInitializationParams if the UI is not initialized yet.
if (mInitializationParams != null) {
return getActiveState(mInitializationParams);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (mAllowButton.isChecked()) {
mState = CookieSettingsState.ALLOW;
return CookieSettingsState.ALLOW;
} else if (mBlockThirdPartyIncognitoButton.isChecked()) {
mState = CookieSettingsState.BLOCK_THIRD_PARTY_INCOGNITO;
return CookieSettingsState.BLOCK_THIRD_PARTY_INCOGNITO;
} else if (mBlockThirdPartyButton.isChecked()) {
mState = CookieSettingsState.BLOCK_THIRD_PARTY;
} else if (mBlockButton.isChecked()) {
mState = CookieSettingsState.BLOCK;
return CookieSettingsState.BLOCK_THIRD_PARTY;
} else {
assert mBlockButton.isChecked();
return CookieSettingsState.BLOCK;
}
}
callChangeListener(mState);
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
callChangeListener(getState());
}
@Override
......@@ -128,48 +134,51 @@ public class FourStateCookieSettingsPreference
mManagedView.setCompoundDrawablesRelativeWithIntrinsicBounds(
managedIcon, drawables[1], drawables[2], drawables[3]);
setRadioButtons();
if (mInitializationParams != null) {
configureRadioButtons(mInitializationParams);
}
}
private RadioButtonWithDescription getActiveRadioButton() {
private CookieSettingsState getActiveState(Params params) {
// These conditions only check the preference combinations that deterministically decide
// your cookie settings state. In the future we would refactor the backend preferences to
// reflect the only possible states you can be in
// (Allow/BlockThirdPartyIncognito/BlockThirdParty/Block), instead of using this
// combination of multiple signals.
if (!mAllowCookies) {
mState = CookieSettingsState.BLOCK;
return mBlockButton;
} else if (mBlockThirdPartyCookies || mCookieControlsMode == CookieControlsMode.ON) {
if (!params.allowCookies) {
return CookieSettingsState.BLOCK;
} else if (params.blockThirdPartyCookies
|| params.cookieControlsMode == CookieControlsMode.ON) {
// Having CookieControlsMode.ON is equivalent to having the BLOCK_THIRD_PARTY_COOKIES
// pref set to enabled, because it means third party cookie blocking is always on.
mState = CookieSettingsState.BLOCK_THIRD_PARTY;
return mBlockThirdPartyButton;
} else if (mCookieControlsMode == CookieControlsMode.INCOGNITO_ONLY) {
mState = CookieSettingsState.BLOCK_THIRD_PARTY_INCOGNITO;
return mBlockThirdPartyIncognitoButton;
return CookieSettingsState.BLOCK_THIRD_PARTY;
} else if (params.cookieControlsMode == CookieControlsMode.INCOGNITO_ONLY) {
return CookieSettingsState.BLOCK_THIRD_PARTY_INCOGNITO;
} else {
mState = CookieSettingsState.ALLOW;
return mAllowButton;
return CookieSettingsState.ALLOW;
}
}
private void setRadioButtons() {
private void configureRadioButtons(Params params) {
assert (mRadioGroup != null);
mAllowButton.setEnabled(true);
mBlockThirdPartyIncognitoButton.setEnabled(true);
mBlockThirdPartyButton.setEnabled(true);
mBlockButton.setEnabled(true);
for (RadioButtonWithDescription button : getEnforcedButtons()) {
for (RadioButtonWithDescription button : getEnforcedButtons(params)) {
button.setEnabled(false);
}
mManagedView.setVisibility((mCookiesContentSettingEnforced || mThirdPartyBlockingEnforced)
mManagedView.setVisibility(
(params.cookiesContentSettingEnforced || params.thirdPartyBlockingEnforced)
? View.VISIBLE
: View.GONE);
RadioButtonWithDescription button = getActiveRadioButton();
RadioButtonWithDescription button = getButton(getActiveState(params));
// Always want to enable the selected option.
button.setEnabled(true);
button.setChecked(true);
mInitializationParams = null;
}
/**
......@@ -179,27 +188,45 @@ public class FourStateCookieSettingsPreference
return args;
}
private RadioButtonWithDescription getButton(CookieSettingsState state) {
switch (state) {
case ALLOW:
return mAllowButton;
case BLOCK_THIRD_PARTY_INCOGNITO:
return mBlockThirdPartyIncognitoButton;
case BLOCK_THIRD_PARTY:
return mBlockThirdPartyButton;
case BLOCK:
return mBlockButton;
case UNINITIALIZED:
assert false;
return null;
}
assert false;
return null;
}
/**
* @return An array of radio buttons that have to be disabled as they can't be selected due to
* policy restrictions.
*/
private RadioButtonWithDescription[] getEnforcedButtons() {
if (!mCookiesContentSettingEnforced && !mThirdPartyBlockingEnforced) {
private RadioButtonWithDescription[] getEnforcedButtons(Params params) {
if (!params.cookiesContentSettingEnforced && !params.thirdPartyBlockingEnforced) {
return buttons();
}
if (mCookiesContentSettingEnforced && mThirdPartyBlockingEnforced) {
if (params.cookiesContentSettingEnforced && params.thirdPartyBlockingEnforced) {
return buttons(mAllowButton, mBlockThirdPartyIncognitoButton, mBlockThirdPartyButton,
mBlockButton);
}
if (mCookiesContentSettingEnforced) {
if (mAllowCookies) {
if (params.cookiesContentSettingEnforced) {
if (params.allowCookies) {
return buttons(mBlockButton);
} else {
return buttons(mAllowButton, mBlockThirdPartyIncognitoButton,
mBlockThirdPartyButton, mBlockButton);
}
}
if (mBlockThirdPartyCookies) {
if (params.blockThirdPartyCookies) {
return buttons(mAllowButton, mBlockThirdPartyIncognitoButton);
} else {
return buttons(mBlockThirdPartyIncognitoButton, mBlockThirdPartyButton);
......@@ -207,24 +234,8 @@ public class FourStateCookieSettingsPreference
}
@VisibleForTesting
public boolean isStateEnforced(CookieSettingsState state) {
RadioButtonWithDescription button;
switch (state) {
case ALLOW:
button = mAllowButton;
break;
case BLOCK_THIRD_PARTY_INCOGNITO:
button = mBlockThirdPartyIncognitoButton;
break;
case BLOCK_THIRD_PARTY:
button = mBlockThirdPartyButton;
break;
case BLOCK:
button = mBlockButton;
break;
default:
return false;
}
return Arrays.asList(getEnforcedButtons()).contains(button);
boolean isButtonEnabledForTesting(CookieSettingsState state) {
assert getButton(state) != null;
return getButton(state).isEnabled();
}
}
......@@ -1034,11 +1034,18 @@ public class SingleCategorySettings extends SiteSettingsPreferenceFragment
private void configureFourStateCookieToggle(
FourStateCookieSettingsPreference fourStateCookieToggle) {
fourStateCookieToggle.setOnPreferenceChangeListener(this);
fourStateCookieToggle.setState(mCategory.isManaged(),
PrefServiceBridge.getInstance().isManagedPreference(Pref.BLOCK_THIRD_PARTY_COOKIES),
WebsitePreferenceBridge.isCategoryEnabled(ContentSettingsType.COOKIES),
PrefServiceBridge.getInstance().getBoolean(Pref.BLOCK_THIRD_PARTY_COOKIES),
PrefServiceBridge.getInstance().getInteger(Pref.COOKIE_CONTROLS_MODE));
FourStateCookieSettingsPreference.Params params =
new FourStateCookieSettingsPreference.Params();
params.allowCookies =
WebsitePreferenceBridge.isCategoryEnabled(ContentSettingsType.COOKIES);
params.blockThirdPartyCookies =
PrefServiceBridge.getInstance().getBoolean(Pref.BLOCK_THIRD_PARTY_COOKIES);
params.cookieControlsMode =
PrefServiceBridge.getInstance().getInteger(Pref.COOKIE_CONTROLS_MODE);
params.cookiesContentSettingEnforced = mCategory.isManaged();
params.thirdPartyBlockingEnforced =
PrefServiceBridge.getInstance().isManagedPreference(Pref.BLOCK_THIRD_PARTY_COOKIES);
fourStateCookieToggle.setState(params);
}
private void configureTriStateToggle(
......
......@@ -261,7 +261,7 @@ public class SiteSettingsTest {
/**
* Checks if the button representing the given state matches the managed expectation.
*/
private void checkFourStateCookieToggleButtonUnmanaged(final SettingsActivity settingsActivity,
private void checkFourStateCookieToggleButtonEnabled(final SettingsActivity settingsActivity,
final CookieSettingsState state, final boolean expected) {
TestThreadUtils.runOnUiThreadBlocking(() -> {
SingleCategorySettings preferences =
......@@ -269,8 +269,8 @@ public class SiteSettingsTest {
FourStateCookieSettingsPreference fourStateCookieToggle =
(FourStateCookieSettingsPreference) preferences.findPreference(
SingleCategorySettings.FOUR_STATE_COOKIE_TOGGLE_KEY);
Assert.assertNotEquals("Button should be " + (expected ? "unmanaged" : "managed"),
fourStateCookieToggle.isStateEnforced(state), expected);
Assert.assertEquals(state + " button should be " + (expected ? "enabled" : "disabled"),
fourStateCookieToggle.isButtonEnabledForTesting(state), expected);
});
}
......@@ -544,58 +544,57 @@ public class SiteSettingsTest {
}
/**
* Set the cookie content setting to managed and ensure the correct radio buttons are enforced.
* Set the cookie content setting to allow through policy and ensure the correct radio buttons
* are enabled.
*/
@Test
@SmallTest
@Feature({"Preferences"})
@Policies.Add({ @Policies.Item(key = "DefaultCookiesSetting", string = "1") })
public void testDefaultCookiesSettingManagedTrue() throws Exception {
public void testDefaultCookiesSettingManagedAllow() throws Exception {
checkDefaultCookiesSettingManaged(true);
checkThirdPartyCookieBlockingManaged(false);
// The ContentSetting is managed (and set to true) while ThirdPartyCookieBlocking is not
// managed. This means that every button other than BLOCKED is unmanaged.
// The ContentSetting is managed (and set to ALLOW) while ThirdPartyCookieBlocking is not
// managed. This means that every button other than BLOCK is enabled.
SettingsActivity settingsActivity =
SiteSettingsTestUtils.startSiteSettingsCategory(SiteSettingsCategory.Type.COOKIES);
checkFourStateCookieToggleButtonUnmanaged(
settingsActivity, CookieSettingsState.ALLOW, true);
checkFourStateCookieToggleButtonUnmanaged(
checkFourStateCookieToggleButtonEnabled(settingsActivity, CookieSettingsState.ALLOW, true);
checkFourStateCookieToggleButtonEnabled(
settingsActivity, CookieSettingsState.BLOCK_THIRD_PARTY_INCOGNITO, true);
checkFourStateCookieToggleButtonUnmanaged(
checkFourStateCookieToggleButtonEnabled(
settingsActivity, CookieSettingsState.BLOCK_THIRD_PARTY, true);
checkFourStateCookieToggleButtonUnmanaged(
settingsActivity, CookieSettingsState.BLOCK, false);
checkFourStateCookieToggleButtonEnabled(settingsActivity, CookieSettingsState.BLOCK, false);
settingsActivity.finish();
}
/**
* Set the cookie content setting to managed and ensure the correct radio buttons are enforced.
* Set the cookie content setting to block through a policy and ensure the correct radio buttons
* are enabled.
*/
@Test
@SmallTest
@Feature({"Preferences"})
@Policies.Add({ @Policies.Item(key = "DefaultCookiesSetting", string = "2") })
public void testDefaultCookiesSettingManagedFalse() throws Exception {
public void testDefaultCookiesSettingManagedBlock() {
checkDefaultCookiesSettingManaged(true);
checkThirdPartyCookieBlockingManaged(false);
// The ContentSetting is managed (and set to false) while ThirdPartyCookieBlocking is not
// The ContentSetting is managed (and set to BLOCK) while ThirdPartyCookieBlocking is not
// managed. This means cookies should always be blocked, so the user cannot choose any other
// options and all buttons should be managed.
// options and all buttons except the active one should be disabled.
SettingsActivity settingsActivity =
SiteSettingsTestUtils.startSiteSettingsCategory(SiteSettingsCategory.Type.COOKIES);
checkFourStateCookieToggleButtonUnmanaged(
settingsActivity, CookieSettingsState.ALLOW, false);
checkFourStateCookieToggleButtonUnmanaged(
checkFourStateCookieToggleButtonEnabled(settingsActivity, CookieSettingsState.ALLOW, false);
checkFourStateCookieToggleButtonEnabled(
settingsActivity, CookieSettingsState.BLOCK_THIRD_PARTY_INCOGNITO, false);
checkFourStateCookieToggleButtonUnmanaged(
checkFourStateCookieToggleButtonEnabled(
settingsActivity, CookieSettingsState.BLOCK_THIRD_PARTY, false);
checkFourStateCookieToggleButtonUnmanaged(
settingsActivity, CookieSettingsState.BLOCK, false);
checkFourStateCookieToggleButtonEnabled(settingsActivity, CookieSettingsState.BLOCK, true);
settingsActivity.finish();
}
/**
* Set third-party cookie blocking to managed and ensure the correct radio buttons are enforced.
* Enable third-party cookie blocking through policy and ensure the correct radio buttons are
* enabled.
*/
@Test
@SmallTest
......@@ -607,22 +606,21 @@ public class SiteSettingsTest {
checkThirdPartyCookieBlockingManaged(true);
// ThirdPartyCookieBlocking is managed (and set to true) while the ContentSetting is not
// managed. This means a user can choose only between BLOCK_THIRD_PARTY and BLOCK, so only
// these should be unmanaged.
// these should be enabled.
SettingsActivity settingsActivity =
SiteSettingsTestUtils.startSiteSettingsCategory(SiteSettingsCategory.Type.COOKIES);
checkFourStateCookieToggleButtonUnmanaged(
settingsActivity, CookieSettingsState.ALLOW, false);
checkFourStateCookieToggleButtonUnmanaged(
checkFourStateCookieToggleButtonEnabled(settingsActivity, CookieSettingsState.ALLOW, false);
checkFourStateCookieToggleButtonEnabled(
settingsActivity, CookieSettingsState.BLOCK_THIRD_PARTY_INCOGNITO, false);
checkFourStateCookieToggleButtonUnmanaged(
checkFourStateCookieToggleButtonEnabled(
settingsActivity, CookieSettingsState.BLOCK_THIRD_PARTY, true);
checkFourStateCookieToggleButtonUnmanaged(
settingsActivity, CookieSettingsState.BLOCK, true);
checkFourStateCookieToggleButtonEnabled(settingsActivity, CookieSettingsState.BLOCK, true);
settingsActivity.finish();
}
/**
* Set third-party cookie blocking to managed and ensure the correct radio buttons are enforced.
* Disable third-party cookie blocking through policy and ensure the correct radio buttons are
* enabled.
*/
@Test
@SmallTest
......@@ -634,23 +632,21 @@ public class SiteSettingsTest {
checkThirdPartyCookieBlockingManaged(true);
// ThirdPartyCookieBlocking is managed (and set to false) while the ContentSetting is not
// managed. This means a user can only choose to ALLOW all cookies or BLOCK all cookies, so
// only these should be unmanaged.
// only these should be enabled.
SettingsActivity settingsActivity =
SiteSettingsTestUtils.startSiteSettingsCategory(SiteSettingsCategory.Type.COOKIES);
checkFourStateCookieToggleButtonUnmanaged(
settingsActivity, CookieSettingsState.ALLOW, true);
checkFourStateCookieToggleButtonUnmanaged(
checkFourStateCookieToggleButtonEnabled(settingsActivity, CookieSettingsState.ALLOW, true);
checkFourStateCookieToggleButtonEnabled(
settingsActivity, CookieSettingsState.BLOCK_THIRD_PARTY_INCOGNITO, false);
checkFourStateCookieToggleButtonUnmanaged(
checkFourStateCookieToggleButtonEnabled(
settingsActivity, CookieSettingsState.BLOCK_THIRD_PARTY, false);
checkFourStateCookieToggleButtonUnmanaged(
settingsActivity, CookieSettingsState.BLOCK, true);
checkFourStateCookieToggleButtonEnabled(settingsActivity, CookieSettingsState.BLOCK, true);
settingsActivity.finish();
}
/**
* Set both the cookie content setting and third-party cookie blocking to managed and ensure the
* correct radio buttons are enforced.
* Set both the cookie content setting and third-party cookie blocking through policy and ensure
* the correct radio buttons are enabled.
*/
@Test
@SmallTest
......@@ -664,18 +660,16 @@ public class SiteSettingsTest {
checkDefaultCookiesSettingManaged(true);
checkThirdPartyCookieBlockingManaged(true);
// The ContentSetting and ThirdPartyCookieBlocking are managed. This means a user has a
// fixed setting for cookies that they cannot change. Therefore, all buttons should be
// managed.
// fixed setting for cookies that they cannot change. Therefore, all buttons except the
// selected one should be disabled.
SettingsActivity settingsActivity =
SiteSettingsTestUtils.startSiteSettingsCategory(SiteSettingsCategory.Type.COOKIES);
checkFourStateCookieToggleButtonUnmanaged(
settingsActivity, CookieSettingsState.ALLOW, false);
checkFourStateCookieToggleButtonUnmanaged(
checkFourStateCookieToggleButtonEnabled(settingsActivity, CookieSettingsState.ALLOW, true);
checkFourStateCookieToggleButtonEnabled(
settingsActivity, CookieSettingsState.BLOCK_THIRD_PARTY_INCOGNITO, false);
checkFourStateCookieToggleButtonUnmanaged(
checkFourStateCookieToggleButtonEnabled(
settingsActivity, CookieSettingsState.BLOCK_THIRD_PARTY, false);
checkFourStateCookieToggleButtonUnmanaged(
settingsActivity, CookieSettingsState.BLOCK, false);
checkFourStateCookieToggleButtonEnabled(settingsActivity, CookieSettingsState.BLOCK, false);
settingsActivity.finish();
}
......@@ -689,17 +683,15 @@ public class SiteSettingsTest {
checkDefaultCookiesSettingManaged(false);
checkThirdPartyCookieBlockingManaged(false);
// The ContentSetting and ThirdPartyCookieBlocking are unmanaged. This means all buttons
// should be unmanaged.
// should be enabled.
SettingsActivity settingsActivity =
SiteSettingsTestUtils.startSiteSettingsCategory(SiteSettingsCategory.Type.COOKIES);
checkFourStateCookieToggleButtonUnmanaged(
settingsActivity, CookieSettingsState.ALLOW, true);
checkFourStateCookieToggleButtonUnmanaged(
checkFourStateCookieToggleButtonEnabled(settingsActivity, CookieSettingsState.ALLOW, true);
checkFourStateCookieToggleButtonEnabled(
settingsActivity, CookieSettingsState.BLOCK_THIRD_PARTY_INCOGNITO, true);
checkFourStateCookieToggleButtonUnmanaged(
checkFourStateCookieToggleButtonEnabled(
settingsActivity, CookieSettingsState.BLOCK_THIRD_PARTY, true);
checkFourStateCookieToggleButtonUnmanaged(
settingsActivity, CookieSettingsState.BLOCK, true);
checkFourStateCookieToggleButtonEnabled(settingsActivity, CookieSettingsState.BLOCK, true);
settingsActivity.finish();
}
......
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