Commit c8b68515 authored by Peter E Conn's avatar Peter E Conn Committed by Commit Bot

🕵️ Focus URL bar for externally launched Incognito tab.

Bug: 929169
Change-Id: I06cf33cb79c42052adc460ce0ccce54e43a379a4
Reviewed-on: https://chromium-review.googlesource.com/c/1472719
Commit-Queue: Peter Conn <peconn@chromium.org>
Reviewed-by: default avatarPeter Conn <peconn@chromium.org>
Reviewed-by: default avatarTheresa <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#636753}
parent 02798ab0
...@@ -1295,6 +1295,7 @@ public class ChromeTabbedActivity ...@@ -1295,6 +1295,7 @@ public class ChromeTabbedActivity
boolean fromLauncherShortcut = IntentUtils.safeGetBooleanExtra( boolean fromLauncherShortcut = IntentUtils.safeGetBooleanExtra(
intent, IntentHandler.EXTRA_INVOKED_FROM_SHORTCUT, false); intent, IntentHandler.EXTRA_INVOKED_FROM_SHORTCUT, false);
boolean focus = false;
TabModel tabModel = getCurrentTabModel(); TabModel tabModel = getCurrentTabModel();
switch (tabOpenType) { switch (tabOpenType) {
...@@ -1419,8 +1420,17 @@ public class ChromeTabbedActivity ...@@ -1419,8 +1420,17 @@ public class ChromeTabbedActivity
recordLauncherShortcutAction(true); recordLauncherShortcutAction(true);
reportNewTabShortcutUsed(true); reportNewTabShortcutUsed(true);
} else if (IncognitoTabLauncher.didCreateIntent(intent)) { } else if (IncognitoTabLauncher.didCreateIntent(intent)) {
getTabCreator(true).launchUrl(UrlConstants.NTP_URL, Tab tab = getTabCreator(true).launchUrl(UrlConstants.NTP_URL,
TabLaunchType.FROM_LAUNCH_NEW_INCOGNITO_TAB); TabLaunchType.FROM_LAUNCH_NEW_INCOGNITO_TAB);
// Since the Tab is created in the foreground, its View will gain focus,
// and since the Tab and the URL bar are not yet in the same View
// hierarchy, setting the URL bar's focus here won't clear the Tab's
// focus.
// When the Tab is added to the hierarchy, we want the URL bar to retain
// focus, so we clear the Tab's focus here.
tab.getView().clearFocus();
focus = true;
IncognitoTabLauncher.recordUse(); IncognitoTabLauncher.recordUse();
} else { } else {
// Used by the Account management screen to open a new incognito tab. // Used by the Account management screen to open a new incognito tab.
...@@ -1440,7 +1450,7 @@ public class ChromeTabbedActivity ...@@ -1440,7 +1450,7 @@ public class ChromeTabbedActivity
assert false : "Unknown TabOpenType: " + tabOpenType; assert false : "Unknown TabOpenType: " + tabOpenType;
break; break;
} }
getToolbarManager().setUrlBarFocus(false); getToolbarManager().setUrlBarFocusOnceNativeInitialized(focus);
} }
@Override @Override
......
...@@ -29,7 +29,6 @@ import org.chromium.chrome.browser.util.IntentUtils; ...@@ -29,7 +29,6 @@ import org.chromium.chrome.browser.util.IntentUtils;
* *
* No URL or search term can be entered in, the Incognito tab is started with a blank (but focused) * No URL or search term can be entered in, the Incognito tab is started with a blank (but focused)
* omnibox. This component will be disabled if incognito mode is disabled. * omnibox. This component will be disabled if incognito mode is disabled.
* TODO(peconn): Focus the omnibox when the Incognito tab is opened.
*/ */
public class IncognitoTabLauncher extends Activity { public class IncognitoTabLauncher extends Activity {
/** The Intent action used to launch the IncognitoTabLauncher. */ /** The Intent action used to launch the IncognitoTabLauncher. */
......
...@@ -225,6 +225,7 @@ public class ToolbarManager ...@@ -225,6 +225,7 @@ public class ToolbarManager
private boolean mToolbarInflationComplete; private boolean mToolbarInflationComplete;
private boolean mInitializedWithNative; private boolean mInitializedWithNative;
private Runnable mOnInitializedRunnable;
private boolean mShouldUpdateToolbarPrimaryColor = true; private boolean mShouldUpdateToolbarPrimaryColor = true;
private int mCurrentThemeColor; private int mCurrentThemeColor;
...@@ -986,6 +987,11 @@ public class ToolbarManager ...@@ -986,6 +987,11 @@ public class ToolbarManager
onNativeLibraryReady(); onNativeLibraryReady();
mInitializedWithNative = true; mInitializedWithNative = true;
if (mOnInitializedRunnable != null) {
mOnInitializedRunnable.run();
mOnInitializedRunnable = null;
}
}); });
} }
...@@ -1558,6 +1564,28 @@ public class ToolbarManager ...@@ -1558,6 +1564,28 @@ public class ToolbarManager
} }
} }
/**
* See {@link #setUrlBarFocus}, but if native is not loaded it will queue the request instead
* of dropping it.
*/
public void setUrlBarFocusOnceNativeInitialized(boolean focused) {
if (isInitialized()) {
setUrlBarFocus(focused);
return;
}
if (focused) {
// Remember requests to focus the Url bar and replay them once native has been
// initialized. This is important for the Launch to Incognito Tab flow (see
// IncognitoTabLauncher.
mOnInitializedRunnable = () -> {
setUrlBarFocus(focused);
};
} else {
mOnInitializedRunnable = null;
}
}
/** /**
* Reverts any pending edits of the location bar and reset to the page state. This does not * Reverts any pending edits of the location bar and reset to the page state. This does not
* change the focus state of the location bar. * change the focus state of the location bar.
......
...@@ -23,6 +23,7 @@ import org.chromium.chrome.browser.ChromeTabbedActivity; ...@@ -23,6 +23,7 @@ import org.chromium.chrome.browser.ChromeTabbedActivity;
import org.chromium.chrome.test.ChromeActivityTestRule; import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner; import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.util.browser.Features; import org.chromium.chrome.test.util.browser.Features;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
/** /**
* Tests for {@link IncognitoTabLauncher}. * Tests for {@link IncognitoTabLauncher}.
...@@ -55,6 +56,21 @@ public class IncognitoTabLauncherTest { ...@@ -55,6 +56,21 @@ public class IncognitoTabLauncherTest {
@Feature("Incognito") @Feature("Incognito")
@MediumTest @MediumTest
public void testLaunchIncognitoNewTab() { public void testLaunchIncognitoNewTab() {
ChromeTabbedActivity activity = launchIncognitoTab();
Assert.assertTrue(activity.getTabModelSelector().isIncognitoSelected());
Assert.assertTrue(IncognitoTabLauncher.didCreateIntent(activity.getIntent()));
}
@Test
@Feature("Incognito")
@MediumTest
public void testLaunchIncognitoNewTab_omniboxFocused() {
ChromeTabbedActivity activity = launchIncognitoTab();
CriteriaHelper.pollUiThread(() -> activity.getToolbarManager().isUrlBarFocused());
}
private ChromeTabbedActivity launchIncognitoTab() {
Context context = InstrumentationRegistry.getTargetContext(); Context context = InstrumentationRegistry.getTargetContext();
IncognitoTabLauncher.setComponentEnabled(context, true); IncognitoTabLauncher.setComponentEnabled(context, true);
Intent intent = createLaunchIntent(context); Intent intent = createLaunchIntent(context);
...@@ -66,9 +82,7 @@ public class IncognitoTabLauncherTest { ...@@ -66,9 +82,7 @@ public class IncognitoTabLauncherTest {
ThreadUtils.runOnUiThreadBlocking(() -> context.startActivity(intent)); ThreadUtils.runOnUiThreadBlocking(() -> context.startActivity(intent));
ChromeTabbedActivity activity = ChromeActivityTestRule.waitFor(ChromeTabbedActivity.class); return ChromeActivityTestRule.waitFor(ChromeTabbedActivity.class);
Assert.assertTrue(activity.getTabModelSelector().isIncognitoSelected());
Assert.assertTrue(IncognitoTabLauncher.didCreateIntent(activity.getIntent()));
} }
private Intent createLaunchIntent(Context context) { private Intent createLaunchIntent(Context context) {
......
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