Commit ca4f3ba8 authored by megjablon's avatar megjablon Committed by Commit bot

Re-enable DataReductionPromoInfoBar tests and fix flakiness

StrictMode violation was from Calendar.getInstance() reading
the /etc/timezone file. Whitelist the StrictMode violation
to be fixed.

Tests were flaky because the UI thread would not always launch
the InfoBar before calling addInfoBarAnimationFinished(). Run
the UI thread blocking so that the InfoBar has always been
launched.

BUG=625038, 577185, 627038

Committed: https://crrev.com/8fcd3f03a3a531f12bb291dc8665834d85511c23
Review-Url: https://codereview.chromium.org/2130083002
Cr-Original-Commit-Position: refs/heads/master@{#404506}
Cr-Commit-Position: refs/heads/master@{#405545}
parent 2dbbe5b0
......@@ -9,6 +9,7 @@ import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.StrictMode;
import org.chromium.base.CommandLine;
import org.chromium.base.ThreadUtils;
......@@ -41,6 +42,7 @@ public class DataReductionPromoInfoBar extends ConfirmInfoBar {
private static String sText;
private static String sPrimaryButtonText;
private static String sSecondaryButtonText;
/**
* Launch the data reduction infobar promo, if it needs to be displayed.
*
......@@ -80,37 +82,45 @@ public class DataReductionPromoInfoBar extends ConfirmInfoBar {
String freOrSecondRunVersion =
DataReductionPromoUtils.getDisplayedFreOrSecondRunPromoVersion();
Calendar releaseDateOfM48Stable = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
releaseDateOfM48Stable.setTime(Date.valueOf(M48_STABLE_RELEASE_DATE));
long packageInstallTime = getPackageInstallTime(context);
// The boolean pref that stores whether user opted out on the first run experience was
// added in M51. If the last promo was shown before M51, then |freOrSecondRunVersion| will
// be empty. If Chrome was installed after the FRE promo was added in M48 and before M51,
// assume the user opted out from the FRE and don't show the infobar.
if (freOrSecondRunVersion.isEmpty()
&& packageInstallTime > releaseDateOfM48Stable.getTimeInMillis()) {
return false;
}
// Only show the promo if the current version is at least two milestones after the last
// promo was displayed or the command line switch is on. If the last promo was shown before
// M51 then |freOrSecondRunVersion| will be empty and it is safe to show the infobar promo.
if (!CommandLine.getInstance().hasSwitch(ENABLE_INFOBAR_SWITCH)
&& !freOrSecondRunVersion.isEmpty()
&& currentMilestone < VersionNumberGetter
.getMilestoneFromVersionNumber(freOrSecondRunVersion) + 2) {
return false;
// Temporarily allowing disk access. TODO: Fix. See http://crbug.com/577185
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
try {
Calendar releaseDateOfM48Stable = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
releaseDateOfM48Stable.setTime(Date.valueOf(M48_STABLE_RELEASE_DATE));
long packageInstallTime = getPackageInstallTime(context);
// The boolean pref that stores whether user opted out on the first run experience was
// added in M51. If the last promo was shown before M51, then |freOrSecondRunVersion|
// will be empty. If Chrome was installed after the FRE promo was added in M48 and
// beforeM51,assume the user opted out from the FRE and don't show the infobar.
if (freOrSecondRunVersion.isEmpty()
&& packageInstallTime > releaseDateOfM48Stable.getTimeInMillis()) {
return false;
}
// Only show the promo if the current version is at least two milestones after the last
// promo was displayed or the command line switch is on. If the last promo was shown
// before M51 then |freOrSecondRunVersion| will be empty and it is safe to show the
// infobar promo.
if (!CommandLine.getInstance().hasSwitch(ENABLE_INFOBAR_SWITCH)
&& !freOrSecondRunVersion.isEmpty()
&& currentMilestone < VersionNumberGetter
.getMilestoneFromVersionNumber(freOrSecondRunVersion) + 2) {
return false;
}
DataReductionPromoInfoBar.launch(webContents,
BitmapFactory.decodeResource(context.getResources(), R.mipmap.app_icon),
context.getString(R.string.data_reduction_promo_infobar_title),
context.getString(R.string.data_reduction_promo_infobar_text),
context.getString(R.string.data_reduction_promo_infobar_button),
context.getString(R.string.no_thanks));
return true;
} finally {
StrictMode.setThreadPolicy(oldPolicy);
}
DataReductionPromoInfoBar.launch(webContents,
BitmapFactory.decodeResource(context.getResources(), R.mipmap.app_icon),
context.getString(R.string.data_reduction_promo_infobar_title),
context.getString(R.string.data_reduction_promo_infobar_text),
context.getString(R.string.data_reduction_promo_infobar_button),
context.getString(R.string.no_thanks));
return true;
}
/**
......
......@@ -6,6 +6,7 @@ package org.chromium.chrome.browser.infobar;
import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
import android.content.Context;
import android.os.Environment;
import android.test.UiThreadTest;
import android.test.suitebuilder.annotation.MediumTest;
......@@ -14,6 +15,8 @@ import android.test.suitebuilder.annotation.Smoke;
import org.chromium.base.CommandLine;
import org.chromium.base.ContextUtils;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.AdvancedMockContext;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.UrlUtils;
......@@ -54,6 +57,26 @@ public class InfoBarTest extends ChromeActivityTestCaseBase<ChromeActivity> {
private EmbeddedTestServer mTestServer;
private InfoBarTestAnimationListener mListener;
private void waitUntilNoInfoBarsExist() throws InterruptedException {
CriteriaHelper.pollUiThread(new Criteria() {
@Override
public boolean isSatisfied() {
return getInfoBars().isEmpty();
}
});
}
private void waitUntilDataReductionPromoInfoBarAppears() throws InterruptedException {
CriteriaHelper.pollUiThread(new Criteria() {
@Override
public boolean isSatisfied() {
List<InfoBar> infobars = getInfoBars();
if (infobars.size() != 1) return false;
return infobars.get(0) instanceof DataReductionPromoInfoBar;
}
});
}
public InfoBarTest() {
super(ChromeActivity.class);
}
......@@ -82,6 +105,11 @@ public class InfoBarTest extends ChromeActivityTestCaseBase<ChromeActivity> {
mTestServer = EmbeddedTestServer.createAndStartFileServer(
getInstrumentation().getContext(), Environment.getExternalStorageDirectory());
// Using an AdvancedMockContext allows us to use a fresh in-memory SharedPreference.
Context context = new AdvancedMockContext(
getInstrumentation().getTargetContext().getApplicationContext());
ContextUtils.initApplicationContextForTests(context);
}
@Override
......@@ -174,10 +202,10 @@ public class InfoBarTest extends ChromeActivityTestCaseBase<ChromeActivity> {
* it.
*/
@MediumTest
@CommandLineFlags.Add("force-fieldtrials=DataCompressionProxyPromoVisibility/Enabled")
@Feature({"Browser", "Main"})
@DisabledTest // crbug.com/625038
public void testDataReductionPromoInfoBar() throws InterruptedException {
ThreadUtils.runOnUiThread(new Runnable() {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
assertFalse("Data Reduction Proxy enabled",
......@@ -189,21 +217,20 @@ public class InfoBarTest extends ChromeActivityTestCaseBase<ChromeActivity> {
.putString(SHARED_PREF_DISPLAYED_FRE_OR_SECOND_PROMO_VERSION, M51_VERSION)
.apply();
// Add an infobar.
DataReductionPromoInfoBar.maybeLaunchPromoInfoBar(
assertTrue(DataReductionPromoInfoBar.maybeLaunchPromoInfoBar(
getActivity(), getActivity().getActivityTab().getWebContents(),
"http://google.com", false, false, HttpURLConnection.HTTP_OK);
"http://google.com", false, false, HttpURLConnection.HTTP_OK));
}
});
assertTrue("InfoBar not added", mListener.addInfoBarAnimationFinished());
waitUntilDataReductionPromoInfoBarAppears();
final List<InfoBar> infoBars = getInfoBars();
assertEquals("Wrong infobar count", 1, infoBars.size());
assertTrue("InfoBar does not have primary button",
InfoBarUtil.hasPrimaryButton(infoBars.get(0)));
assertTrue("InfoBar does not have secondary button",
InfoBarUtil.hasSecondaryButton(infoBars.get(0)));
ThreadUtils.runOnUiThread(new Runnable() {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
InfoBarUtil.clickPrimaryButton(infoBars.get(0));
......@@ -211,10 +238,9 @@ public class InfoBarTest extends ChromeActivityTestCaseBase<ChromeActivity> {
});
// The renderer should have been killed and the infobar removed.
assertTrue("InfoBar not removed.", mListener.removeInfoBarAnimationFinished());
assertTrue("Wrong infobar count", getInfoBars().isEmpty());
waitUntilNoInfoBarsExist();
ThreadUtils.runOnUiThread(new Runnable() {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
assertTrue("Data Reduction Proxy not enabled",
......@@ -236,10 +262,10 @@ public class InfoBarTest extends ChromeActivityTestCaseBase<ChromeActivity> {
* it.
*/
@MediumTest
@CommandLineFlags.Add("force-fieldtrials=DataCompressionProxyPromoVisibility/Enabled")
@Feature({"Browser", "Main"})
@DisabledTest // crbug.com/625038
public void testDataReductionPromoInfoBarDismissed() throws InterruptedException {
ThreadUtils.runOnUiThread(new Runnable() {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
assertFalse("Data Reduction Proxy enabled",
......@@ -251,21 +277,20 @@ public class InfoBarTest extends ChromeActivityTestCaseBase<ChromeActivity> {
.putString(SHARED_PREF_DISPLAYED_FRE_OR_SECOND_PROMO_VERSION, M51_VERSION)
.apply();
// Add an infobar.
DataReductionPromoInfoBar.maybeLaunchPromoInfoBar(
assertTrue(DataReductionPromoInfoBar.maybeLaunchPromoInfoBar(
getActivity(), getActivity().getActivityTab().getWebContents(),
"http://google.com", false, false, HttpURLConnection.HTTP_OK);
"http://google.com", false, false, HttpURLConnection.HTTP_OK));
}
});
assertTrue("InfoBar not added", mListener.addInfoBarAnimationFinished());
waitUntilDataReductionPromoInfoBarAppears();
final List<InfoBar> infoBars = getInfoBars();
assertEquals("Wrong infobar count", 1, infoBars.size());
assertTrue("InfoBar does not have primary button",
InfoBarUtil.hasPrimaryButton(infoBars.get(0)));
assertTrue("InfoBar does not have secondary button",
InfoBarUtil.hasSecondaryButton(infoBars.get(0)));
ThreadUtils.runOnUiThread(new Runnable() {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
InfoBarUtil.clickSecondaryButton(infoBars.get(0));
......@@ -273,10 +298,9 @@ public class InfoBarTest extends ChromeActivityTestCaseBase<ChromeActivity> {
});
// The renderer should have been killed and the infobar removed.
assertTrue("InfoBar not removed.", mListener.removeInfoBarAnimationFinished());
assertTrue("Wrong infobar count", getInfoBars().isEmpty());
waitUntilNoInfoBarsExist();
ThreadUtils.runOnUiThread(new Runnable() {
ThreadUtils.runOnUiThreadBlocking(new Runnable() {
@Override
public void run() {
assertFalse("Data Reduction Proxy enabled",
......@@ -296,8 +320,8 @@ public class InfoBarTest extends ChromeActivityTestCaseBase<ChromeActivity> {
*/
@UiThreadTest
@MediumTest
@CommandLineFlags.Add("force-fieldtrials=DataCompressionProxyPromoVisibility/Enabled")
@Feature({"Browser", "Main"})
@DisabledTest // crbug.com/625038
public void testDataReductionPromoInfoBarPostM48Install() {
assertFalse("Data Reduction Proxy enabled",
DataReductionProxySettings.getInstance().isDataReductionProxyEnabled());
......@@ -321,8 +345,8 @@ public class InfoBarTest extends ChromeActivityTestCaseBase<ChromeActivity> {
*/
@UiThreadTest
@MediumTest
@CommandLineFlags.Add("force-fieldtrials=DataCompressionProxyPromoVisibility/Enabled")
@Feature({"Browser", "Main"})
@DisabledTest // crbug.com/625038
public void testDataReductionPromoInfoBarFreOptOut() {
// Try to add an infobar. Infobar should not be added since the first run experience or
// second run promo hasn't been shown.
......
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