Commit 0d5f2bdb authored by Josh Simmons's avatar Josh Simmons Committed by Commit Bot

Fixing several issues in the Translate tests to make them less flakey.

 1) Tests were failing on pie-rel bots because swarmed builds do not
    include API keys. I've updated the tests to set a flag to ignore
    these missing keys.
 2) Tests were still flaking quite a bit due to batching and state not
    being cleared between runs. I've tried a couple things (enabling
    extensive clearing, opening new tabs and closing tabs after tests)
    but the only way I've been able to get them to run without flakes is
    by disabling batching. I'll need to dig into translate internals to
    determine what the right fix is here.
 3) Tests were failing the
    network_service_out_of_process_chrome_public_test_apk phase because,
    while the network monitor _says_ it has a connection to the internet
    during these tests, tests run under this configuration will actually
    refuse to resolve any DNS requests. I've added early returns for
    when the network service is out of process, but longer term I've
    opened crbug/1134812 to track the removal of the internet
    dependency. Similar to #2 this will take digging into Translate
    internals to make them more testable.

BUG=1141160

Change-Id: I4cac8deed71235c2e2da6a6a690edbbaed047b9a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2461711
Commit-Queue: Josh Simmons <jds@google.com>
Reviewed-by: default avatarMegan Jablonski <megjablon@chromium.org>
Reviewed-by: default avatarMichael Thiessen <mthiesse@chromium.org>
Cr-Commit-Position: refs/heads/master@{#819660}
parent 88757c64
......@@ -193,6 +193,10 @@ public class TranslateBridge {
TranslateBridgeJni.get().setExplicitLanguageAskPromptShown(shown);
}
public static void setIgnoreMissingKeyForTesting(boolean ignore) {
TranslateBridgeJni.get().setIgnoreMissingKeyForTesting(ignore); // IN-TEST
}
@NativeMethods
interface Natives {
void manualTranslateWhenReady(WebContents webContents);
......@@ -213,5 +217,6 @@ public class TranslateBridge {
void setLanguageBlockedState(String language, boolean blocked);
boolean getExplicitLanguageAskPromptShown();
void setExplicitLanguageAskPromptShown(boolean shown);
void setIgnoreMissingKeyForTesting(boolean ignore);
}
}
......@@ -17,22 +17,21 @@ import org.junit.Test;
import org.junit.runner.RunWith;
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.Restriction;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.infobar.InfoBarContainer;
import org.chromium.chrome.browser.infobar.TranslateCompactInfoBar;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
import org.chromium.chrome.test.util.ChromeRestriction;
import org.chromium.chrome.test.util.InfoBarTestAnimationListener;
import org.chromium.chrome.test.util.InfoBarUtil;
import org.chromium.chrome.test.util.MenuUtils;
import org.chromium.chrome.test.util.TranslateUtil;
import org.chromium.components.infobars.InfoBar;
import org.chromium.net.test.EmbeddedTestServer;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import java.util.concurrent.TimeoutException;
......@@ -51,20 +50,30 @@ public class TranslateCompactInfoBarTest {
private InfoBarContainer mInfoBarContainer;
private InfoBarTestAnimationListener mListener;
private EmbeddedTestServer mTestServer;
@Before
public void setUp() throws Exception {
mActivityTestRule.startMainActivityOnBlankPage();
TestThreadUtils.runOnUiThreadBlocking(
() -> TranslateBridge.setIgnoreMissingKeyForTesting(true));
mInfoBarContainer = mActivityTestRule.getInfoBarContainer();
mListener = new InfoBarTestAnimationListener();
mInfoBarContainer.addAnimationListener(mListener);
mTestServer = EmbeddedTestServer.createAndStartServer(InstrumentationRegistry.getContext());
}
@After
public void tearDown() {
mTestServer.stopAndDestroyServer();
mInfoBarContainer.removeAnimationListener(mListener);
}
/**
* Returns true if a test that requires internet access should be skipped due to an
* out-of-process NetworkService. When the NetworkService is run out-of-process, a fake DNS
* resolver is used that will fail to resolve any non-local names. crbug.com/1134812 is tracking
* the changes to make the translate service mockable and remove the internet requirement.
*/
private boolean shouldSkipDueToNetworkService() {
return !ChromeFeatureList.isEnabled("NetworkServiceInProcess");
}
/**
......@@ -73,10 +82,10 @@ public class TranslateCompactInfoBarTest {
@Test
@MediumTest
@Feature({"Browser", "Main"})
@Restriction(ChromeRestriction.RESTRICTION_TYPE_GOOGLE_PLAY_SERVICES)
@DisabledTest(message = "https://crbug.com/1130712")
@Restriction({Restriction.RESTRICTION_TYPE_INTERNET})
public void testTranslateCompactInfoBarAppears() throws TimeoutException {
mActivityTestRule.loadUrl(mTestServer.getURL(TRANSLATE_PAGE));
if (shouldSkipDueToNetworkService()) return;
mActivityTestRule.loadUrl(mActivityTestRule.getTestServer().getURL(TRANSLATE_PAGE));
mListener.addInfoBarAnimationFinished("InfoBar not opened.");
InfoBar infoBar = mInfoBarContainer.getInfoBarsForTesting().get(0);
TranslateUtil.assertCompactTranslateInfoBar(infoBar);
......@@ -89,10 +98,10 @@ public class TranslateCompactInfoBarTest {
@Test
@MediumTest
@Feature({"Browser", "Main"})
@Restriction(ChromeRestriction.RESTRICTION_TYPE_GOOGLE_PLAY_SERVICES)
@DisabledTest(message = "https://crbug.com/1130712")
@Restriction({Restriction.RESTRICTION_TYPE_INTERNET})
public void testTranslateCompactInfoBarOverflowMenus() throws TimeoutException {
mActivityTestRule.loadUrl(mTestServer.getURL(TRANSLATE_PAGE));
if (shouldSkipDueToNetworkService()) return;
mActivityTestRule.loadUrl(mActivityTestRule.getTestServer().getURL(TRANSLATE_PAGE));
mListener.addInfoBarAnimationFinished("InfoBar not opened.");
TranslateCompactInfoBar infoBar =
(TranslateCompactInfoBar) mInfoBarContainer.getInfoBarsForTesting().get(0);
......@@ -112,10 +121,10 @@ public class TranslateCompactInfoBarTest {
@Test
@MediumTest
@Feature({"Browser", "Main"})
@Restriction(ChromeRestriction.RESTRICTION_TYPE_GOOGLE_PLAY_SERVICES)
@DisabledTest(message = "https://crbug.com/1130712")
@Restriction({Restriction.RESTRICTION_TYPE_INTERNET})
public void testTabMenuDismissedOnOrientationChange() throws Exception {
mActivityTestRule.loadUrl(mTestServer.getURL(TRANSLATE_PAGE));
if (shouldSkipDueToNetworkService()) return;
mActivityTestRule.loadUrl(mActivityTestRule.getTestServer().getURL(TRANSLATE_PAGE));
mListener.addInfoBarAnimationFinished("InfoBar not opened.");
TranslateCompactInfoBar infoBar =
(TranslateCompactInfoBar) mInfoBarContainer.getInfoBarsForTesting().get(0);
......@@ -142,19 +151,15 @@ public class TranslateCompactInfoBarTest {
@Test
@MediumTest
@Feature({"Browser", "Main"})
@Restriction(ChromeRestriction.RESTRICTION_TYPE_GOOGLE_PLAY_SERVICES)
@DisabledTest(message = "https://crbug.com/1130712")
@Restriction({Restriction.RESTRICTION_TYPE_INTERNET})
public void testTranslateCompactInfoBarReopenOnTarget() throws TimeoutException {
mActivityTestRule.loadUrl(mTestServer.getURL(TRANSLATE_PAGE));
mListener.addInfoBarAnimationFinished("InfoBar not opened.");
if (shouldSkipDueToNetworkService()) return;
mActivityTestRule.loadUrl(mActivityTestRule.getTestServer().getURL(TRANSLATE_PAGE));
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/false);
TranslateCompactInfoBar infoBar =
(TranslateCompactInfoBar) mInfoBarContainer.getInfoBarsForTesting().get(0);
// Only the source tab is selected.
Assert.assertTrue(infoBar.isSourceTabSelectedForTesting());
Assert.assertFalse(infoBar.isTargetTabSelectedForTesting());
// Translate.
TranslateUtil.clickTargetMenuItem(infoBar, "en");
// Close bar.
......@@ -164,11 +169,7 @@ public class TranslateCompactInfoBarTest {
MenuUtils.invokeCustomMenuActionSync(InstrumentationRegistry.getInstrumentation(),
mActivityTestRule.getActivity(), R.id.translate_id);
infoBar = (TranslateCompactInfoBar) mInfoBarContainer.getInfoBarsForTesting().get(0);
// Only the target tab is selected.
Assert.assertFalse(infoBar.isSourceTabSelectedForTesting());
Assert.assertTrue(infoBar.isTargetTabSelectedForTesting());
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/true);
}
/**
......@@ -177,25 +178,19 @@ public class TranslateCompactInfoBarTest {
@Test
@MediumTest
@Feature({"Browser", "Main"})
@Restriction(ChromeRestriction.RESTRICTION_TYPE_GOOGLE_PLAY_SERVICES)
@DisabledTest(message = "https://crbug.com/1130712")
@Restriction({Restriction.RESTRICTION_TYPE_INTERNET})
public void testStartTranslateOnManualInitiation() throws TimeoutException {
if (shouldSkipDueToNetworkService()) return;
// Load a page that won't trigger the translate recommendation.
mActivityTestRule.loadUrl(mTestServer.getURL(NON_TRANSLATE_PAGE));
mActivityTestRule.loadUrl(mActivityTestRule.getTestServer().getURL(NON_TRANSLATE_PAGE));
Assert.assertTrue(mInfoBarContainer.getInfoBarsForTesting().isEmpty());
// Invoke bar by clicking the manual translate button.
MenuUtils.invokeCustomMenuActionSync(InstrumentationRegistry.getInstrumentation(),
mActivityTestRule.getActivity(), R.id.translate_id);
mListener.addInfoBarAnimationFinished("InfoBar not opened.");
TranslateCompactInfoBar infoBar =
(TranslateCompactInfoBar) mInfoBarContainer.getInfoBarsForTesting().get(0);
// Only the target tab is selected.
Assert.assertFalse(infoBar.isSourceTabSelectedForTesting());
Assert.assertTrue(infoBar.isTargetTabSelectedForTesting());
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/true);
}
/**
......@@ -204,32 +199,22 @@ public class TranslateCompactInfoBarTest {
@Test
@MediumTest
@Feature({"Browser", "Main"})
@Restriction(ChromeRestriction.RESTRICTION_TYPE_GOOGLE_PLAY_SERVICES)
@DisabledTest(message = "https://crbug.com/1130712")
@Restriction({Restriction.RESTRICTION_TYPE_INTERNET})
public void testManualInitiationWithBarOpen() throws TimeoutException {
mActivityTestRule.loadUrl(mTestServer.getURL(TRANSLATE_PAGE));
mListener.addInfoBarAnimationFinished("InfoBar not opened.");
TranslateCompactInfoBar infoBar =
(TranslateCompactInfoBar) mInfoBarContainer.getInfoBarsForTesting().get(0);
if (shouldSkipDueToNetworkService()) return;
mActivityTestRule.loadUrl(mActivityTestRule.getTestServer().getURL(TRANSLATE_PAGE));
// Only the source tab is selected.
Assert.assertTrue(infoBar.isSourceTabSelectedForTesting());
Assert.assertFalse(infoBar.isTargetTabSelectedForTesting());
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/false);
MenuUtils.invokeCustomMenuActionSync(InstrumentationRegistry.getInstrumentation(),
mActivityTestRule.getActivity(), R.id.translate_id);
// Only the target tab is selected.
Assert.assertFalse(infoBar.isSourceTabSelectedForTesting());
Assert.assertTrue(infoBar.isTargetTabSelectedForTesting());
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/true);
// Verify that hitting "Translate..." again doesn't revert the translation.
MenuUtils.invokeCustomMenuActionSync(InstrumentationRegistry.getInstrumentation(),
mActivityTestRule.getActivity(), R.id.translate_id);
// Only the target tab is selected.
Assert.assertFalse(infoBar.isSourceTabSelectedForTesting());
Assert.assertTrue(infoBar.isTargetTabSelectedForTesting());
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/true);
}
}
......@@ -16,26 +16,22 @@ import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.ContextUtils;
import org.chromium.base.test.util.Batch;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.Restriction;
import org.chromium.chrome.browser.IntentHandler;
import org.chromium.chrome.browser.document.ChromeLauncherActivity;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.infobar.InfoBarContainer;
import org.chromium.chrome.browser.infobar.TranslateCompactInfoBar;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
import org.chromium.chrome.test.batch.BlankCTATabInitialStateRule;
import org.chromium.chrome.test.util.InfoBarTestAnimationListener;
import org.chromium.chrome.test.util.TranslateUtil;
import org.chromium.chrome.test.util.browser.Features;
import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
......@@ -50,58 +46,36 @@ import java.util.concurrent.TimeoutException;
*/
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
@Batch(Batch.PER_CLASS)
public class TranslateIntentTest {
@ClassRule
public static final ChromeTabbedActivityTestRule sActivityTestRule =
new ChromeTabbedActivityTestRule();
@Rule
public final BlankCTATabInitialStateRule mInitialStateRule =
new BlankCTATabInitialStateRule(sActivityTestRule, false);
public ChromeTabbedActivityTestRule mActivityTestRule = new ChromeTabbedActivityTestRule();
private static final String TRANSLATE_PAGE = "/chrome/test/data/translate/fr_test.html";
private static final String NON_TRANSLATE_PAGE =
"/chrome/test/data/translate/english_page.html";
private InfoBarContainer mInfoBarContainer;
private InfoBarTestAnimationListener mListener;
/**
* Wait until the activity tab is translatable. This is useful in cases where we can't wait on
* the infobar.
*/
private void waitUntilTranslatable() {
CriteriaHelper.pollInstrumentationThread(() -> {
Tab tab = sActivityTestRule.getActivity().getActivityTab();
CriteriaHelper.pollUiThread(() -> {
Tab tab = mActivityTestRule.getActivity().getActivityTab();
Criteria.checkThat(tab, Matchers.notNullValue());
boolean canManuallyTranslate = false;
try {
canManuallyTranslate = TestThreadUtils.runOnUiThreadBlocking(
() -> TranslateBridge.canManuallyTranslate(tab));
} catch (ExecutionException e) {
e.printStackTrace();
canManuallyTranslate = false;
}
Criteria.checkThat(canManuallyTranslate, Matchers.is(true));
Criteria.checkThat(TranslateBridge.canManuallyTranslate(tab), Matchers.is(true));
});
}
/**
* Wait until the given translate infobar is in the given state.
* Returns true if a test that requires internet access should be skipped due to an
* out-of-process NetworkService. When the NetworkService is run out-of-process, a fake DNS
* resolver is used that will fail to resolve any non-local names. crbug.com/1134812 is tracking
* the changes to make the translate service mockable and remove the internet requirement.
*/
private void waitForTranslateInfoBarState(boolean expectTranslated) {
TranslateCompactInfoBar infoBar =
(TranslateCompactInfoBar) mInfoBarContainer.getInfoBarsForTesting().get(0);
CriteriaHelper.pollInstrumentationThread(() -> {
if (expectTranslated) {
return !infoBar.isSourceTabSelectedForTesting()
&& infoBar.isTargetTabSelectedForTesting();
} else {
return infoBar.isSourceTabSelectedForTesting()
&& !infoBar.isTargetTabSelectedForTesting();
}
});
private boolean shouldSkipDueToNetworkService() {
return !ChromeFeatureList.isEnabled("NetworkServiceInProcess");
}
/**
......@@ -118,86 +92,84 @@ public class TranslateIntentTest {
}
IntentHandler.addTrustedIntentExtras(intent);
TestThreadUtils.runOnUiThreadBlocking(
() -> sActivityTestRule.getActivity().onNewIntent(intent));
() -> mActivityTestRule.getActivity().onNewIntent(intent));
}
@Before
public void setUp() {
mInfoBarContainer = sActivityTestRule.getInfoBarContainer();
mListener = new InfoBarTestAnimationListener();
mInfoBarContainer.addAnimationListener(mListener);
mActivityTestRule.startMainActivityOnBlankPage();
TestThreadUtils.runOnUiThreadBlocking(
() -> TranslateBridge.setIgnoreMissingKeyForTesting(true));
mInfoBarContainer = mActivityTestRule.getInfoBarContainer();
}
@After
public void tearDown() {
mInfoBarContainer.removeAnimationListener(mListener);
}
@Test
@MediumTest
@DisabledTest(message = "https://crbug.com/1127786 and https://crbug.com/1135682")
@Restriction(Restriction.RESTRICTION_TYPE_INTERNET)
@Features.DisableFeatures({ChromeFeatureList.TRANSLATE_INTENT})
public void
testTranslateIntentDisabled() throws TimeoutException {
final String url = sActivityTestRule.getTestServer().getURL(TRANSLATE_PAGE);
public void testTranslateIntentDisabled() throws TimeoutException {
if (shouldSkipDueToNetworkService()) return;
final String url = mActivityTestRule.getTestServer().getURL(TRANSLATE_PAGE);
// Load a page that triggers the translate recommendation.
sActivityTestRule.loadUrl(url);
mListener.addInfoBarAnimationFinished("InfoBar not opened.");
waitForTranslateInfoBarState(false);
mActivityTestRule.loadUrl(url);
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/false);
sendTranslateIntent(url, null);
// No translation occurs.
waitForTranslateInfoBarState(false);
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/false);
}
@Test
@MediumTest
@DisabledTest(message = "https://crbug.com/1127786 and https://crbug.com/1135682")
@Restriction(Restriction.RESTRICTION_TYPE_INTERNET)
@Features.EnableFeatures({ChromeFeatureList.TRANSLATE_INTENT})
public void
testTranslateIntentOnTranslatePage() throws TimeoutException {
final String url = sActivityTestRule.getTestServer().getURL(TRANSLATE_PAGE);
public void testTranslateIntentOnTranslatePage() throws TimeoutException {
if (shouldSkipDueToNetworkService()) return;
final String url = mActivityTestRule.getTestServer().getURL(TRANSLATE_PAGE);
// Load a page that triggers the translate recommendation.
sActivityTestRule.loadUrl(url);
mListener.addInfoBarAnimationFinished("InfoBar not opened.");
waitForTranslateInfoBarState(false);
mActivityTestRule.loadUrl(url);
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/false);
sendTranslateIntent(url, null);
// Only the target tab is selected.
waitForTranslateInfoBarState(true);
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/true);
}
@Test
@MediumTest
@DisabledTest(message = "https://crbug.com/1127786 and https://crbug.com/1135682")
@Restriction(Restriction.RESTRICTION_TYPE_INTERNET)
@Features.EnableFeatures({ChromeFeatureList.TRANSLATE_INTENT})
public void
testTranslateIntentOnNonTranslatePage() throws TimeoutException {
final String url = sActivityTestRule.getTestServer().getURL(NON_TRANSLATE_PAGE);
public void testTranslateIntentOnNonTranslatePage() throws TimeoutException {
if (shouldSkipDueToNetworkService()) return;
final String url = mActivityTestRule.getTestServer().getURL(NON_TRANSLATE_PAGE);
// Load a page that doesn't trigger the translate recommendation.
sActivityTestRule.loadUrl(url);
mActivityTestRule.loadUrl(url);
waitUntilTranslatable();
Assert.assertTrue(mInfoBarContainer.getInfoBarsForTesting().isEmpty());
sendTranslateIntent(url, null);
mListener.addInfoBarAnimationFinished("InfoBar not opened.");
// Only the target tab is selected.
waitForTranslateInfoBarState(true);
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/true);
}
@Test
@MediumTest
@DisabledTest(message = "https://crbug.com/1127786 and https://crbug.com/1135682")
@Restriction(Restriction.RESTRICTION_TYPE_INTERNET)
@Features.EnableFeatures({ChromeFeatureList.TRANSLATE_INTENT})
public void
testTranslateIntentWithTargetLanguage() throws TimeoutException, ExecutionException {
final String url = sActivityTestRule.getTestServer().getURL(NON_TRANSLATE_PAGE);
public void testTranslateIntentWithTargetLanguage()
throws TimeoutException, ExecutionException {
if (shouldSkipDueToNetworkService()) return;
final String url = mActivityTestRule.getTestServer().getURL(NON_TRANSLATE_PAGE);
// Load a page that doesn't trigger the translate recommendation.
sActivityTestRule.loadUrl(url);
mActivityTestRule.loadUrl(url);
waitUntilTranslatable();
Assert.assertTrue(mInfoBarContainer.getInfoBarsForTesting().isEmpty());
......@@ -207,10 +179,9 @@ public class TranslateIntentTest {
Assert.assertFalse(acceptCodes.contains("de"));
sendTranslateIntent(url, "de");
mListener.addInfoBarAnimationFinished("InfoBar not opened.");
// Only the target tab is selected.
waitForTranslateInfoBarState(true);
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/true);
// Verify that German has been added to the user's accept languages.
acceptCodes =
......@@ -220,13 +191,13 @@ public class TranslateIntentTest {
@Test
@MediumTest
@DisabledTest(message = "https://crbug.com/1127786 and https://crbug.com/1135682")
@Restriction(Restriction.RESTRICTION_TYPE_INTERNET)
@Features.EnableFeatures({ChromeFeatureList.TRANSLATE_INTENT})
public void
testTranslateIntentWithUnsupportedTargetLanguage() throws TimeoutException {
final String url = sActivityTestRule.getTestServer().getURL(NON_TRANSLATE_PAGE);
public void testTranslateIntentWithUnsupportedTargetLanguage() throws TimeoutException {
if (shouldSkipDueToNetworkService()) return;
final String url = mActivityTestRule.getTestServer().getURL(NON_TRANSLATE_PAGE);
// Load a page that doesn't trigger the translate recommendation.
sActivityTestRule.loadUrl(url);
mActivityTestRule.loadUrl(url);
waitUntilTranslatable();
Assert.assertTrue(mInfoBarContainer.getInfoBarsForTesting().isEmpty());
......@@ -238,97 +209,96 @@ public class TranslateIntentTest {
@Test
@MediumTest
@DisabledTest(message = "https://crbug.com/1127786 and https://crbug.com/1135682")
@Restriction(Restriction.RESTRICTION_TYPE_INTERNET)
@Features.EnableFeatures({ChromeFeatureList.TRANSLATE_INTENT})
public void
testTranslateIntentOnIncognito() throws TimeoutException {
final String url = sActivityTestRule.getTestServer().getURL(TRANSLATE_PAGE);
public void testTranslateIntentOnIncognito() throws TimeoutException {
if (shouldSkipDueToNetworkService()) return;
final String url = mActivityTestRule.getTestServer().getURL(TRANSLATE_PAGE);
// Load a page that triggers the translate recommendation.
sActivityTestRule.loadUrlInNewTab(url, /*incognito=*/true);
mActivityTestRule.loadUrlInNewTab(url, /*incognito=*/true);
// A new tab is opened for this test so we need to get the new container.
mInfoBarContainer = sActivityTestRule.getInfoBarContainer();
InfoBarTestAnimationListener mListener = new InfoBarTestAnimationListener();
mInfoBarContainer.addAnimationListener(mListener);
mListener.addInfoBarAnimationFinished("InfoBar not opened.");
waitForTranslateInfoBarState(false);
mInfoBarContainer = mActivityTestRule.getInfoBarContainer();
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/false);
sendTranslateIntent(url, null);
// No translation should happen.
waitForTranslateInfoBarState(false);
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/false);
// We opened a new tab for this test. Ensure that it's closed.
TestThreadUtils.runOnUiThreadBlocking(
() -> { mActivityTestRule.getActivity().getTabModelSelector().closeAllTabs(); });
}
@Test
@MediumTest
@DisabledTest(message = "https://crbug.com/1127786 and https://crbug.com/1135682")
@Restriction(Restriction.RESTRICTION_TYPE_INTERNET)
@Features.EnableFeatures({ChromeFeatureList.TRANSLATE_INTENT})
public void
testTranslateIntentWithUrlMismatch() throws TimeoutException {
final String url = sActivityTestRule.getTestServer().getURL(TRANSLATE_PAGE);
public void testTranslateIntentWithUrlMismatch() throws TimeoutException {
if (shouldSkipDueToNetworkService()) return;
final String url = mActivityTestRule.getTestServer().getURL(TRANSLATE_PAGE);
// Load a page that triggers the translate recommendation.
sActivityTestRule.loadUrl(url);
mListener.addInfoBarAnimationFinished("InfoBar not opened.");
waitForTranslateInfoBarState(false);
mActivityTestRule.loadUrl(url);
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/false);
sendTranslateIntent("http://incorrect.com", null);
// No translation should happen.
waitForTranslateInfoBarState(false);
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/false);
}
@Test
@MediumTest
@DisabledTest(message = "https://crbug.com/1127786 and https://crbug.com/1135682")
@Restriction(Restriction.RESTRICTION_TYPE_INTERNET)
@Features.EnableFeatures({ChromeFeatureList.TRANSLATE_INTENT})
public void
testTranslateIntentWithoutExpectedUrl() throws TimeoutException {
final String url = sActivityTestRule.getTestServer().getURL(TRANSLATE_PAGE);
public void testTranslateIntentWithoutExpectedUrl() throws TimeoutException {
if (shouldSkipDueToNetworkService()) return;
final String url = mActivityTestRule.getTestServer().getURL(TRANSLATE_PAGE);
// Load a page that triggers the translate recommendation.
sActivityTestRule.loadUrl(url);
mListener.addInfoBarAnimationFinished("InfoBar not opened.");
waitForTranslateInfoBarState(false);
mActivityTestRule.loadUrl(url);
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/false);
sendTranslateIntent(null, null);
// No translation should happen.
waitForTranslateInfoBarState(false);
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/false);
}
@Test
@MediumTest
@DisabledTest(message = "https://crbug.com/1127786 and https://crbug.com/1135682")
@Restriction(Restriction.RESTRICTION_TYPE_INTERNET)
@Features.EnableFeatures({ChromeFeatureList.TRANSLATE_INTENT})
public void
testTranslateIntentVerifyComponent() throws TimeoutException {
final String url = sActivityTestRule.getTestServer().getURL(TRANSLATE_PAGE);
public void testTranslateIntentVerifyComponent() throws TimeoutException {
if (shouldSkipDueToNetworkService()) return;
final String url = mActivityTestRule.getTestServer().getURL(TRANSLATE_PAGE);
// Load a page that triggers the translate recommendation.
sActivityTestRule.loadUrl(url);
mListener.addInfoBarAnimationFinished("InfoBar not opened.");
waitForTranslateInfoBarState(false);
mActivityTestRule.loadUrl(url);
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/false);
Context context = ContextUtils.getApplicationContext();
final Intent intent = new Intent(TranslateIntentHandler.ACTION_TRANSLATE_TAB);
intent.setClassName(context, "com.google.android.apps.chrome.TranslateDispatcher");
intent.putExtra(TranslateIntentHandler.EXTRA_EXPECTED_URL, url);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Send this via startActivity so we don't bypass LaunchIntentHandler.
TestThreadUtils.runOnUiThreadBlocking(() -> context.startActivity(intent));
mActivityTestRule.startActivityCompletely(intent);
mActivityTestRule.waitForActivityNativeInitializationComplete();
// Only the target tab is selected.
waitForTranslateInfoBarState(true);
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/true);
}
@Test
@MediumTest
@DisabledTest(message = "https://crbug.com/1127786 and https://crbug.com/1135682")
@Restriction(Restriction.RESTRICTION_TYPE_INTERNET)
@Features.EnableFeatures({ChromeFeatureList.TRANSLATE_INTENT})
public void
testTranslateIntentIncorrectComponent() throws TimeoutException {
final String url = sActivityTestRule.getTestServer().getURL(TRANSLATE_PAGE);
public void testTranslateIntentIncorrectComponent() throws TimeoutException {
if (shouldSkipDueToNetworkService()) return;
final String url = mActivityTestRule.getTestServer().getURL(TRANSLATE_PAGE);
// Load a page that triggers the translate recommendation.
sActivityTestRule.loadUrl(url);
mListener.addInfoBarAnimationFinished("InfoBar not opened.");
waitForTranslateInfoBarState(false);
mActivityTestRule.loadUrl(url);
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/false);
Context context = ContextUtils.getApplicationContext();
......@@ -336,10 +306,11 @@ public class TranslateIntentTest {
// Bypassing the TranslateDispatcher activity alias should reject the intent.
intent.setClass(context, ChromeLauncherActivity.class);
intent.putExtra(TranslateIntentHandler.EXTRA_EXPECTED_URL, url);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
TestThreadUtils.runOnUiThreadBlocking(() -> context.startActivity(intent));
// No translation should happen.
waitForTranslateInfoBarState(false);
TranslateUtil.waitForTranslateInfoBarState(mInfoBarContainer, /*expectTranslated=*/false);
}
@Test
......
......@@ -412,3 +412,10 @@ static void JNI_TranslateBridge_SetExplicitLanguageAskPromptShown(
ChromeTranslateClient::CreateTranslatePrefs(GetPrefService());
translate_prefs->SetExplicitLanguageAskPromptShown(shown);
}
static void JNI_TranslateBridge_SetIgnoreMissingKeyForTesting( // IN-TEST
JNIEnv* env,
jboolean ignore) {
translate::TranslateManager::SetIgnoreMissingKeyForTesting( // IN-TEST
ignore);
}
......@@ -10,6 +10,7 @@ import android.view.View;
import org.junit.Assert;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.infobar.InfoBarContainer;
import org.chromium.chrome.browser.infobar.TranslateCompactInfoBar;
import org.chromium.components.infobars.InfoBar;
import org.chromium.components.infobars.InfoBarCompactLayout;
......@@ -18,6 +19,9 @@ import org.chromium.components.translate.TranslateTabLayout;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import java.util.ArrayList;
import java.util.concurrent.TimeoutException;
/**
* Utility functions for dealing with Translate InfoBars.
*/
......@@ -100,4 +104,27 @@ public class TranslateUtil {
final TranslateCompactInfoBar infoBar, final String code) {
TestThreadUtils.runOnUiThreadBlocking(() -> { infoBar.onTargetMenuItemClicked(code); });
}
/**
* Wait until the translate infobar is present in the given InfoBarContainer and is in the given
* state. Throws a TimeoutException if the given state is never reached.
*/
public static void waitForTranslateInfoBarState(
InfoBarContainer container, boolean expectTranslated) throws TimeoutException {
CriteriaHelper.pollUiThread(() -> {
ArrayList<InfoBar> infoBars = container.getInfoBarsForTesting();
if (infoBars.isEmpty()) {
return false;
}
assertCompactTranslateInfoBar(infoBars.get(0));
TranslateCompactInfoBar infoBar = (TranslateCompactInfoBar) infoBars.get(0);
if (expectTranslated) {
return !infoBar.isSourceTabSelectedForTesting()
&& infoBar.isTargetTabSelectedForTesting();
} else {
return infoBar.isSourceTabSelectedForTesting()
&& !infoBar.isTargetTabSelectedForTesting();
}
});
}
}
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