Commit a2078164 authored by Sophie Chang's avatar Sophie Chang Committed by Commit Bot

Only show Data Saver main menu item if it's already allowed by the current...

Only show Data Saver main menu item if it's already allowed by the current conditions AND the user has saved 100KB.

Change-Id: I94363f7810d7529bf6975d9e50e1d5d142807a3e
Bug: 867197
Reviewed-on: https://chromium-review.googlesource.com/1149271Reviewed-by: default avatarTheresa <twellington@chromium.org>
Reviewed-by: default avatarDoug Arnett <dougarnett@chromium.org>
Commit-Queue: Sophie Chang <sophiechang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#578409}
parent be069140
...@@ -17,6 +17,7 @@ import org.chromium.chrome.browser.preferences.datareduction.DataReductionDataUs ...@@ -17,6 +17,7 @@ import org.chromium.chrome.browser.preferences.datareduction.DataReductionDataUs
import org.chromium.chrome.browser.preferences.datareduction.DataReductionPromoUtils; import org.chromium.chrome.browser.preferences.datareduction.DataReductionPromoUtils;
import org.chromium.chrome.browser.preferences.datareduction.DataReductionProxySavingsClearedReason; import org.chromium.chrome.browser.preferences.datareduction.DataReductionProxySavingsClearedReason;
import org.chromium.chrome.browser.preferences.datareduction.DataReductionStatsPreference; import org.chromium.chrome.browser.preferences.datareduction.DataReductionStatsPreference;
import org.chromium.chrome.browser.util.ConversionUtils;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -72,6 +73,8 @@ public class DataReductionProxySettings { ...@@ -72,6 +73,8 @@ public class DataReductionProxySettings {
private static final String PARAM_PERSISTENT_MENU_ITEM_ENABLED = "persistent_menu_item_enabled"; private static final String PARAM_PERSISTENT_MENU_ITEM_ENABLED = "persistent_menu_item_enabled";
private static final long DATA_REDUCTION_MAIN_MENU_ITEM_SAVED_KB_THRESHOLD = 100;
private Callback<List<DataReductionDataUseItem>> mQueryDataUsageCallback; private Callback<List<DataReductionDataUseItem>> mQueryDataUsageCallback;
/** /**
...@@ -197,6 +200,7 @@ public class DataReductionProxySettings { ...@@ -197,6 +200,7 @@ public class DataReductionProxySettings {
public boolean shouldUseDataReductionMainMenuItem() { public boolean shouldUseDataReductionMainMenuItem() {
if (!ChromeFeatureList.isEnabled(ChromeFeatureList.DATA_REDUCTION_MAIN_MENU)) return false; if (!ChromeFeatureList.isEnabled(ChromeFeatureList.DATA_REDUCTION_MAIN_MENU)) return false;
boolean data_reduction_main_menu_item_allowed = false;
if (ChromeFeatureList.getFieldTrialParamByFeatureAsBoolean( if (ChromeFeatureList.getFieldTrialParamByFeatureAsBoolean(
ChromeFeatureList.DATA_REDUCTION_MAIN_MENU, PARAM_PERSISTENT_MENU_ITEM_ENABLED, ChromeFeatureList.DATA_REDUCTION_MAIN_MENU, PARAM_PERSISTENT_MENU_ITEM_ENABLED,
false)) { false)) {
...@@ -208,11 +212,18 @@ public class DataReductionProxySettings { ...@@ -208,11 +212,18 @@ public class DataReductionProxySettings {
.putBoolean(DATA_REDUCTION_HAS_EVER_BEEN_ENABLED_PREF, true) .putBoolean(DATA_REDUCTION_HAS_EVER_BEEN_ENABLED_PREF, true)
.apply(); .apply();
} }
return ContextUtils.getAppSharedPreferences().getBoolean( data_reduction_main_menu_item_allowed =
DATA_REDUCTION_HAS_EVER_BEEN_ENABLED_PREF, false); ContextUtils.getAppSharedPreferences().getBoolean(
DATA_REDUCTION_HAS_EVER_BEEN_ENABLED_PREF, false);
} else { } else {
return isDataReductionProxyEnabled(); data_reduction_main_menu_item_allowed = isDataReductionProxyEnabled();
}
if (data_reduction_main_menu_item_allowed) {
return ConversionUtils.bytesToKilobytes(getContentLengthSavedInHistorySummary())
>= DATA_REDUCTION_MAIN_MENU_ITEM_SAVED_KB_THRESHOLD;
} }
return false;
} }
/** Returns true if the SPDY proxy is managed by an administrator's policy. */ /** Returns true if the SPDY proxy is managed by an administrator's policy. */
......
...@@ -37,6 +37,7 @@ public class DataSaverAppMenuTest { ...@@ -37,6 +37,7 @@ public class DataSaverAppMenuTest {
new ChromeActivityTestRule<>(ChromeActivity.class); new ChromeActivityTestRule<>(ChromeActivity.class);
private AppMenuHandlerForTest mAppMenuHandler; private AppMenuHandlerForTest mAppMenuHandler;
private TestDataReductionProxySettings mSettings;
/** /**
* AppMenuHandler that will be used to intercept the delegate for testing. * AppMenuHandler that will be used to intercept the delegate for testing.
...@@ -58,6 +59,23 @@ public class DataSaverAppMenuTest { ...@@ -58,6 +59,23 @@ public class DataSaverAppMenuTest {
} }
} }
private static class TestDataReductionProxySettings extends DataReductionProxySettings {
private long mContentLengthSavedInHistorySummary;
@Override
public long getContentLengthSavedInHistorySummary() {
return mContentLengthSavedInHistorySummary;
}
/**
* Sets the content length saved for the number of days shown in the history summary. This
* is only used for testing.
*/
public void setContentLengthSavedInHistorySummary(long contentLengthSavedInHistorySummary) {
mContentLengthSavedInHistorySummary = contentLengthSavedInHistorySummary;
}
}
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
ChromeTabbedActivity.setAppMenuHandlerFactoryForTesting( ChromeTabbedActivity.setAppMenuHandlerFactoryForTesting(
...@@ -68,6 +86,9 @@ public class DataSaverAppMenuTest { ...@@ -68,6 +86,9 @@ public class DataSaverAppMenuTest {
}); });
mActivityTestRule.startMainActivityOnBlankPage(); mActivityTestRule.startMainActivityOnBlankPage();
mSettings = new TestDataReductionProxySettings();
DataReductionProxySettings.setInstanceForTesting(mSettings);
} }
/** /**
...@@ -88,7 +109,8 @@ public class DataSaverAppMenuTest { ...@@ -88,7 +109,8 @@ public class DataSaverAppMenuTest {
} }
/** /**
* Verify the Data Saver footer shows with the flag when the proxy is on. * Verify the Data Saver footer shows with the flag when the proxy is on and the user has saved
* at least 100KB of data.
*/ */
@Test @Test
@SmallTest @SmallTest
...@@ -101,9 +123,19 @@ public class DataSaverAppMenuTest { ...@@ -101,9 +123,19 @@ public class DataSaverAppMenuTest {
// Data Saver hasn't been turned on, the footer shouldn't show. // Data Saver hasn't been turned on, the footer shouldn't show.
Assert.assertEquals(0, mAppMenuHandler.getDelegate().getFooterResourceId()); Assert.assertEquals(0, mAppMenuHandler.getDelegate().getFooterResourceId());
// Turn Data Saver on, the footer should show. // Turn Data Saver on, the footer should not show since the user hasn't saved any bytes
// yet.
DataReductionProxySettings.getInstance().setDataReductionProxyEnabled( DataReductionProxySettings.getInstance().setDataReductionProxyEnabled(
mActivityTestRule.getActivity().getApplicationContext(), true); mActivityTestRule.getActivity().getApplicationContext(), true);
Assert.assertEquals(0, mAppMenuHandler.getDelegate().getFooterResourceId());
// The user has only saved 50KB so far. Ensure footer is not shown since it is not above
// the threshold yet.
mSettings.setContentLengthSavedInHistorySummary(50 * 1024);
Assert.assertEquals(0, mAppMenuHandler.getDelegate().getFooterResourceId());
// The user has now saved 100KB. Ensure the footer is shown.
mSettings.setContentLengthSavedInHistorySummary(100 * 1024);
Assert.assertEquals(R.layout.data_reduction_main_menu_item, Assert.assertEquals(R.layout.data_reduction_main_menu_item,
mAppMenuHandler.getDelegate().getFooterResourceId()); mAppMenuHandler.getDelegate().getFooterResourceId());
...@@ -132,9 +164,19 @@ public class DataSaverAppMenuTest { ...@@ -132,9 +164,19 @@ public class DataSaverAppMenuTest {
// Data Saver hasn't been turned on, the footer shouldn't show. // Data Saver hasn't been turned on, the footer shouldn't show.
Assert.assertEquals(0, mAppMenuHandler.getDelegate().getFooterResourceId()); Assert.assertEquals(0, mAppMenuHandler.getDelegate().getFooterResourceId());
// Turn Data Saver on, the footer should show. // Turn Data Saver on, the footer should not be shown, as the user hasn't saved any
// bytes yet.
DataReductionProxySettings.getInstance().setDataReductionProxyEnabled( DataReductionProxySettings.getInstance().setDataReductionProxyEnabled(
mActivityTestRule.getActivity().getApplicationContext(), true); mActivityTestRule.getActivity().getApplicationContext(), true);
Assert.assertEquals(0, mAppMenuHandler.getDelegate().getFooterResourceId());
// The user has only saved 50KB so far. Ensure footer is not shown since it is not above
// the threshold yet.
mSettings.setContentLengthSavedInHistorySummary(50 * 1024);
Assert.assertEquals(0, mAppMenuHandler.getDelegate().getFooterResourceId());
// The user has now saved 100KB. Ensure the footer is shown.
mSettings.setContentLengthSavedInHistorySummary(100 * 1024);
Assert.assertEquals(R.layout.data_reduction_main_menu_item, Assert.assertEquals(R.layout.data_reduction_main_menu_item,
mAppMenuHandler.getDelegate().getFooterResourceId()); mAppMenuHandler.getDelegate().getFooterResourceId());
......
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