Commit e0ce764d authored by Brandon Wylie's avatar Brandon Wylie Committed by Commit Bot

Reset the SelectionPopupController when the window attachment is null

Bug: 1091135
Change-Id: I6568f6fe1a98c3a91bd3887c452d5eb2de0ecbb2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2240353Reviewed-by: default avatarTheresa  <twellington@chromium.org>
Reviewed-by: default avatarShimi Zhang <ctzsm@chromium.org>
Reviewed-by: default avatarDonn Denman <donnd@chromium.org>
Commit-Queue: Brandon Wylie <wylieb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#778604}
parent da91a231
......@@ -293,6 +293,23 @@ public class SelectionPopupControllerImpl extends ActionModeCallbackHelper
getPopupController().registerPopup(this);
}
private void reset() {
dropFocus();
mContext = null;
mWindowAndroid = null;
}
private void dropFocus() {
// Hide popups and clear selection.
destroyActionModeAndUnselect();
dismissTextHandles();
PopupController.hideAll(mWebContents);
// Clear the selection. The selection is cleared on destroying IME
// and also here since we may receive destroy first, for example
// when focus is lost in webview.
clearSelection();
}
public static String sanitizeQuery(String query, int maxLength) {
if (TextUtils.isEmpty(query) || query.length() < maxLength) return query;
Log.w(TAG, "Truncating oversized query (" + query.length() + ").");
......@@ -610,6 +627,11 @@ public class SelectionPopupControllerImpl extends ActionModeCallbackHelper
@Override
public void onWindowAndroidChanged(WindowAndroid newWindowAndroid) {
if (newWindowAndroid == null) {
reset();
return;
}
mWindowAndroid = newWindowAndroid;
mContext = mWebContents.getContext();
initHandleObserver();
......@@ -638,14 +660,7 @@ public class SelectionPopupControllerImpl extends ActionModeCallbackHelper
setPreserveSelectionOnNextLossOfFocus(false);
hidePopupsAndPreserveSelection();
} else {
// Hide popups and clear selection.
destroyActionModeAndUnselect();
dismissTextHandles();
PopupController.hideAll(mWebContents);
// Clear the selection. The selection is cleared on destroying IME
// and also here since we may receive destroy first, for example
// when focus is lost in webview.
clearSelection();
dropFocus();
}
}
}
......
......@@ -607,6 +607,43 @@ public class SelectionPopupControllerTest {
Mockito.verify(spyController, times(3)).finishActionMode();
}
@Test
@Feature({"TextInput"})
public void testSelectionWhenWindowIsNull() {
SelectionPopupControllerImpl spyController = Mockito.spy(mController);
when(mView.startActionMode(any(FloatingActionModeCallback.class), anyInt()))
.thenReturn(mActionMode);
// Long press triggered showSelectionMenu() call.
spyController.showSelectionMenu(0, 0, 0, 0, 0, /* isEditable = */ true,
/* isPasswordType = */ false, AMPHITHEATRE_FULL, /* selectionOffset = */ 0,
/* canSelectAll = */ true,
/* canRichlyEdit = */ true, /* shouldSuggest = */ true,
MenuSourceType.MENU_SOURCE_LONG_PRESS);
Mockito.verify(mView).startActionMode(
isA(FloatingActionModeCallback.class), eq(ActionMode.TYPE_FLOATING));
// showSelectionMenu() will invoke the first call to finishActionMode() in the
// showActionModeOrClearOnFailure().
Mockito.verify(spyController, times(1)).finishActionMode();
assertTrue(spyController.isSelectActionBarShowing());
// Setting the window to null should clear selections and reset the state.
spyController.onWindowAndroidChanged(null);
// Changed the focused node attribute to non-editable and non-password.
spyController.updateSelectionState(false, false);
// finishActionMode will be called twice for unselect.
Mockito.verify(spyController, times(2)).finishActionMode();
// SELECTION_HANDLES_CLEARED happens.
spyController.onSelectionEvent(SelectionEventType.SELECTION_HANDLES_CLEARED, 0, 0, 1, 1);
assertFalse(spyController.isSelectActionBarShowing());
Mockito.verify(spyController, times(3)).finishActionMode();
}
// Result generated by long press "Amphitheatre" in "1600 Amphitheatre Parkway".
private SelectionClient.Result resultForAmphitheatre() {
SelectionClient.Result result = new SelectionClient.Result();
......
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