Commit b37fcc78 authored by Josh Simmons's avatar Josh Simmons Committed by Commit Bot

Trigger translate immediately when the 'Translate...' button is pressed in the overflow menu.

This also sets mUserInteracted = true in a couple places where it was not being set previously. I believe the intention of the bool and the INFOBAR_DECLINE metric is to track the number of immediate closes of the infobar, but please let me know if this isn't the case.

Bug: 1123180
Change-Id: Ia8bd867575b7d242554cec8336a31aa5ead714e3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2388777
Commit-Queue: Josh Simmons <jds@google.com>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Reviewed-by: default avatarMegan Jablonski <megjablon@chromium.org>
Cr-Commit-Position: refs/heads/master@{#806410}
parent 29de8890
...@@ -229,7 +229,9 @@ public class TranslateCompactInfoBar extends InfoBar ...@@ -229,7 +229,9 @@ public class TranslateCompactInfoBar extends InfoBar
// Set translating status in the beginning for pages translated automatically. // Set translating status in the beginning for pages translated automatically.
mTabLayout.getTabAt(TARGET_TAB_INDEX).select(); mTabLayout.getTabAt(TARGET_TAB_INDEX).select();
mTabLayout.showProgressBarOnTab(TARGET_TAB_INDEX); mTabLayout.showProgressBarOnTab(TARGET_TAB_INDEX);
mUserInteracted = true; if (mOptions.triggeredFromMenu()) {
mUserInteracted = true;
}
} else if (mInitialStep == AFTER_TRANSLATING_INFOBAR) { } else if (mInitialStep == AFTER_TRANSLATING_INFOBAR) {
// Focus on target tab since we are after translation. // Focus on target tab since we are after translation.
mTabLayout.getTabAt(TARGET_TAB_INDEX).select(); mTabLayout.getTabAt(TARGET_TAB_INDEX).select();
...@@ -298,14 +300,34 @@ public class TranslateCompactInfoBar extends InfoBar ...@@ -298,14 +300,34 @@ public class TranslateCompactInfoBar extends InfoBar
} }
} }
private void startTranslating(int tabPosition) { /**
if (TARGET_TAB_INDEX == tabPosition) { * Selects the tab at tabIndex without triggering onTabSelected. This avoids treating the
// Already on the target tab. * selection as a user interaction and prevents a potential loop where a translate state change
* updates the UI, which then updates the translate state.
*/
private void silentlySelectTabAt(int tabIndex) {
if (mTabLayout == null) {
return;
}
mTabLayout.removeOnTabSelectedListener(this);
mTabLayout.getTabAt(tabIndex).select();
mTabLayout.addOnTabSelectedListener(this);
}
/**
* Begins the translation process and marks it as initiated by the user.
*/
private void startUserInitiatedTranslation() {
mUserInteracted = true;
onButtonClicked(ActionType.TRANSLATE);
}
@CalledByNative
private void onTranslating() {
if (mTabLayout != null) {
silentlySelectTabAt(TARGET_TAB_INDEX);
mTabLayout.showProgressBarOnTab(TARGET_TAB_INDEX); mTabLayout.showProgressBarOnTab(TARGET_TAB_INDEX);
onButtonClicked(ActionType.TRANSLATE);
mUserInteracted = true;
} else {
mTabLayout.getTabAt(TARGET_TAB_INDEX).select();
} }
} }
...@@ -319,11 +341,7 @@ public class TranslateCompactInfoBar extends InfoBar ...@@ -319,11 +341,7 @@ public class TranslateCompactInfoBar extends InfoBar
Toast.makeText(getContext(), R.string.translate_infobar_error, Toast.LENGTH_SHORT) Toast.makeText(getContext(), R.string.translate_infobar_error, Toast.LENGTH_SHORT)
.show(); .show();
errorUIShown = true; errorUIShown = true;
// Disable OnTabSelectedListener then revert selection. silentlySelectTabAt(SOURCE_TAB_INDEX);
mTabLayout.removeOnTabSelectedListener(this);
mTabLayout.getTabAt(SOURCE_TAB_INDEX).select();
// Add OnTabSelectedListener back.
mTabLayout.addOnTabSelectedListener(this);
} }
} }
return errorUIShown; return errorUIShown;
...@@ -382,13 +400,14 @@ public class TranslateCompactInfoBar extends InfoBar ...@@ -382,13 +400,14 @@ public class TranslateCompactInfoBar extends InfoBar
case SOURCE_TAB_INDEX: case SOURCE_TAB_INDEX:
incrementAndRecordTranslationsPerPageCount(); incrementAndRecordTranslationsPerPageCount();
recordInfobarAction(INFOBAR_REVERT); recordInfobarAction(INFOBAR_REVERT);
mUserInteracted = true;
onButtonClicked(ActionType.TRANSLATE_SHOW_ORIGINAL); onButtonClicked(ActionType.TRANSLATE_SHOW_ORIGINAL);
return; return;
case TARGET_TAB_INDEX: case TARGET_TAB_INDEX:
recordInfobarAction(INFOBAR_TARGET_TAB_TRANSLATE); recordInfobarAction(INFOBAR_TARGET_TAB_TRANSLATE);
recordInfobarLanguageData( recordInfobarLanguageData(
INFOBAR_HISTOGRAM_TRANSLATE_LANGUAGE, mOptions.targetLanguageCode()); INFOBAR_HISTOGRAM_TRANSLATE_LANGUAGE, mOptions.targetLanguageCode());
startTranslating(TARGET_TAB_INDEX); startUserInitiatedTranslation();
return; return;
default: default:
assert false : "Unexpected Tab Index"; assert false : "Unexpected Tab Index";
...@@ -460,7 +479,7 @@ public class TranslateCompactInfoBar extends InfoBar ...@@ -460,7 +479,7 @@ public class TranslateCompactInfoBar extends InfoBar
TranslateCompactInfoBar.this, TranslateOption.TARGET_CODE, code); TranslateCompactInfoBar.this, TranslateOption.TARGET_CODE, code);
// Adjust UI. // Adjust UI.
mTabLayout.replaceTabTitle(TARGET_TAB_INDEX, mOptions.getRepresentationFromCode(code)); mTabLayout.replaceTabTitle(TARGET_TAB_INDEX, mOptions.getRepresentationFromCode(code));
startTranslating(mTabLayout.getSelectedTabPosition()); startUserInitiatedTranslation();
} }
} }
...@@ -474,7 +493,7 @@ public class TranslateCompactInfoBar extends InfoBar ...@@ -474,7 +493,7 @@ public class TranslateCompactInfoBar extends InfoBar
TranslateCompactInfoBar.this, TranslateOption.SOURCE_CODE, code); TranslateCompactInfoBar.this, TranslateOption.SOURCE_CODE, code);
// Adjust UI. // Adjust UI.
mTabLayout.replaceTabTitle(SOURCE_TAB_INDEX, mOptions.getRepresentationFromCode(code)); mTabLayout.replaceTabTitle(SOURCE_TAB_INDEX, mOptions.getRepresentationFromCode(code));
startTranslating(mTabLayout.getSelectedTabPosition()); startUserInitiatedTranslation();
} }
} }
...@@ -585,20 +604,22 @@ public class TranslateCompactInfoBar extends InfoBar ...@@ -585,20 +604,22 @@ public class TranslateCompactInfoBar extends InfoBar
switch (actionId) { switch (actionId) {
case ACTION_OVERFLOW_ALWAYS_TRANSLATE: case ACTION_OVERFLOW_ALWAYS_TRANSLATE:
mUserInteracted = true;
toggleAlwaysTranslate(); toggleAlwaysTranslate();
// Start translating if always translate is selected and if page is not already // Start translating if always translate is selected and if page is not already
// translated to the target language. // translated to the target language.
if (mOptions.getTranslateState(TranslateOptions.Type.ALWAYS_LANGUAGE) if (mOptions.getTranslateState(TranslateOptions.Type.ALWAYS_LANGUAGE)
&& mTabLayout.getSelectedTabPosition() == SOURCE_TAB_INDEX) { && mTabLayout.getSelectedTabPosition() == SOURCE_TAB_INDEX) {
startTranslating(mTabLayout.getSelectedTabPosition()); startUserInitiatedTranslation();
} }
return; return;
case ACTION_AUTO_ALWAYS_TRANSLATE: case ACTION_AUTO_ALWAYS_TRANSLATE:
toggleAlwaysTranslate(); toggleAlwaysTranslate();
return; return;
case ACTION_OVERFLOW_NEVER_LANGUAGE: case ACTION_OVERFLOW_NEVER_LANGUAGE:
case ACTION_AUTO_NEVER_LANGUAGE:
mUserInteracted = true; mUserInteracted = true;
// Fallthrough intentional.
case ACTION_AUTO_NEVER_LANGUAGE:
// After applying this option, the infobar will dismiss. // After applying this option, the infobar will dismiss.
TranslateCompactInfoBarJni.get().applyBoolTranslateOption( TranslateCompactInfoBarJni.get().applyBoolTranslateOption(
mNativeTranslateInfoBarPtr, TranslateCompactInfoBar.this, mNativeTranslateInfoBarPtr, TranslateCompactInfoBar.this,
......
...@@ -48,6 +48,7 @@ public class TranslateCompactInfoBarTest { ...@@ -48,6 +48,7 @@ public class TranslateCompactInfoBarTest {
new ChromeActivityTestRule<>(ChromeActivity.class); new ChromeActivityTestRule<>(ChromeActivity.class);
private static final String TRANSLATE_PAGE = "/chrome/test/data/translate/fr_test.html"; private static final String TRANSLATE_PAGE = "/chrome/test/data/translate/fr_test.html";
private static final String NON_TRANSLATE_PAGE = "/chrome/test/data/android/simple.html";
private InfoBarContainer mInfoBarContainer; private InfoBarContainer mInfoBarContainer;
private InfoBarTestAnimationListener mListener; private InfoBarTestAnimationListener mListener;
...@@ -166,4 +167,64 @@ public class TranslateCompactInfoBarTest { ...@@ -166,4 +167,64 @@ public class TranslateCompactInfoBarTest {
Assert.assertFalse(infoBar.isSourceTabSelectedForTesting()); Assert.assertFalse(infoBar.isSourceTabSelectedForTesting());
Assert.assertTrue(infoBar.isTargetTabSelectedForTesting()); Assert.assertTrue(infoBar.isTargetTabSelectedForTesting());
} }
/**
* Test that translation starts automatically when "Translate..." is pressed in the menu.
*/
@Test
@MediumTest
@Feature({"Browser", "Main"})
@Restriction(ChromeRestriction.RESTRICTION_TYPE_GOOGLE_PLAY_SERVICES)
public void testStartTranslateOnManualInitiation() throws TimeoutException {
// Load a page that won't trigger the translate recommendation.
mActivityTestRule.loadUrl(mTestServer.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());
}
/**
* Test that pressing "Translate..." will start a translation even if the infobar is visible.
*/
@Test
@MediumTest
@Feature({"Browser", "Main"})
@Restriction(ChromeRestriction.RESTRICTION_TYPE_GOOGLE_PLAY_SERVICES)
public void testManualInitiationWithBarOpen() throws TimeoutException {
mActivityTestRule.loadUrl(mTestServer.getURL(TRANSLATE_PAGE));
mListener.addInfoBarAnimationFinished("InfoBar not opened.");
TranslateCompactInfoBar infoBar =
(TranslateCompactInfoBar) mInfoBarContainer.getInfoBarsForTesting().get(0);
// Only the source tab is selected.
Assert.assertTrue(infoBar.isSourceTabSelectedForTesting());
Assert.assertFalse(infoBar.isTargetTabSelectedForTesting());
MenuUtils.invokeCustomMenuActionSync(InstrumentationRegistry.getInstrumentation(),
mActivityTestRule.getActivity(), R.id.translate_id);
// Only the target tab is selected.
Assert.assertFalse(infoBar.isSourceTabSelectedForTesting());
Assert.assertTrue(infoBar.isTargetTabSelectedForTesting());
// 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());
}
} }
...@@ -245,7 +245,6 @@ bool ChromeTranslateClient::ShowTranslateUI( ...@@ -245,7 +245,6 @@ bool ChromeTranslateClient::ShowTranslateUI(
InfoBarService::FromWebContents(web_contents()), InfoBarService::FromWebContents(web_contents()),
web_contents()->GetBrowserContext()->IsOffTheRecord(), step, web_contents()->GetBrowserContext()->IsOffTheRecord(), step,
source_language, target_language, error_type, triggered_from_menu); source_language, target_language, error_type, triggered_from_menu);
return true;
#else #else
DCHECK(TranslateService::IsTranslateBubbleEnabled()); DCHECK(TranslateService::IsTranslateBubbleEnabled());
// Bubble UI. // Bubble UI.
...@@ -299,7 +298,7 @@ void ChromeTranslateClient::ManualTranslateWhenReady() { ...@@ -299,7 +298,7 @@ void ChromeTranslateClient::ManualTranslateWhenReady() {
manual_translate_on_ready_ = true; manual_translate_on_ready_ = true;
} else { } else {
translate::TranslateManager* manager = GetTranslateManager(); translate::TranslateManager* manager = GetTranslateManager();
manager->InitiateManualTranslation(); manager->InitiateManualTranslation(true);
} }
} }
#endif #endif
...@@ -356,7 +355,7 @@ void ChromeTranslateClient::OnLanguageDetermined( ...@@ -356,7 +355,7 @@ void ChromeTranslateClient::OnLanguageDetermined(
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
// See ChromeTranslateClient::ManualTranslateOnReady // See ChromeTranslateClient::ManualTranslateOnReady
if (manual_translate_on_ready_) { if (manual_translate_on_ready_) {
GetTranslateManager()->InitiateManualTranslation(); GetTranslateManager()->InitiateManualTranslation(true);
manual_translate_on_ready_ = false; manual_translate_on_ready_ = false;
} }
#endif #endif
......
...@@ -229,6 +229,9 @@ void TranslateCompactInfoBar::OnTranslateStepChanged( ...@@ -229,6 +229,9 @@ void TranslateCompactInfoBar::OnTranslateStepChanged(
if (error_ui_shown) { if (error_ui_shown) {
GetDelegate()->OnErrorShown(error_type); GetDelegate()->OnErrorShown(error_type);
} }
} else if (step == translate::TRANSLATE_STEP_TRANSLATING) {
JNIEnv* env = base::android::AttachCurrentThread();
Java_TranslateCompactInfoBar_onTranslating(env, GetJavaInfoBar());
} }
} }
......
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