Commit b5d03e09 authored by Ted Choc's avatar Ted Choc Committed by Commit Bot

Replace OmniboxTest zero suggest tests w/ unit tests.

AutocompleteEditText#testDelete_CommitTextWithSpannableModel ensures
deletion updates are processed (and it checks that shouldAutocomplete()
is false after deletions).  This was an existing test.

LocationBarTest#testDeleteButton does isolated testing of the delete
button.

AutocompleteMediatorUnitTest#onUrlFocusChange_triggersZeroSuggest_*
adds more complete zero suggest unit tests to complement
onTextChanged_emptyTextTriggersZeroSuggest.

BUG=1027549

Change-Id: I71635c615afb2359326aaa82d50223466b836a2c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2125428
Commit-Queue: Ted Choc <tedchoc@chromium.org>
Reviewed-by: default avatarBrandon Wylie <wylieb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#754543}
parent 2f29efd6
......@@ -29,6 +29,7 @@ import org.chromium.base.ContextUtils;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.Feature;
import org.chromium.base.test.util.Matchers;
import org.chromium.base.test.util.Restriction;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.ChromeActivity;
......@@ -41,6 +42,9 @@ import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.util.browser.Features.EnableFeatures;
import org.chromium.components.embedder_support.util.UrlConstants;
import org.chromium.components.security_state.ConnectionSecurityLevel;
import org.chromium.content_public.browser.test.util.ClickUtils;
import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.ui.test.util.UiRestriction;
......@@ -202,6 +206,17 @@ public class LocationBarLayoutTest {
onView(withId(R.id.delete_button)).check(matches(not(isDisplayed())));
}
@Test
@SmallTest
public void testDeleteButton() throws ExecutionException {
setUrlBarTextAndFocus("testing");
Assert.assertEquals(getDeleteButton().getVisibility(), VISIBLE);
ClickUtils.clickButton(getDeleteButton());
CriteriaHelper.pollUiThread(
Criteria.checkThat(() -> getDeleteButton().getVisibility(), Matchers.not(VISIBLE)));
Assert.assertEquals("", getUrlText(getUrlBar()));
}
@Test
@SmallTest
@EnableFeatures(ChromeFeatureList.QUERY_IN_OMNIBOX)
......
......@@ -34,7 +34,6 @@ import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.omnibox.status.StatusViewCoordinator;
import org.chromium.chrome.browser.omnibox.suggestions.AutocompleteController.OnSuggestionsReceivedListener;
import org.chromium.chrome.browser.omnibox.suggestions.AutocompleteCoordinatorTestUtils;
import org.chromium.chrome.browser.omnibox.suggestions.OmniboxSuggestion;
import org.chromium.chrome.browser.tab.EmptyTabObserver;
import org.chromium.chrome.browser.tab.Tab;
......@@ -44,20 +43,15 @@ import org.chromium.chrome.test.ChromeActivityTestRule;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.util.ChromeTabUtils;
import org.chromium.chrome.test.util.OmniboxTestUtils;
import org.chromium.chrome.test.util.OmniboxTestUtils.SuggestionsResult;
import org.chromium.chrome.test.util.OmniboxTestUtils.TestAutocompleteController;
import org.chromium.chrome.test.util.browser.Features.DisableFeatures;
import org.chromium.components.embedder_support.util.UrlConstants;
import org.chromium.content_public.browser.test.util.Criteria;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.KeyUtils;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.content_public.browser.test.util.TouchCommon;
import org.chromium.content_public.browser.test.util.UiUtils;
import org.chromium.net.test.EmbeddedTestServer;
import org.chromium.net.test.ServerCertificate;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Callable;
......@@ -159,127 +153,6 @@ public class OmniboxTest {
}));
}
/**
* Tests that focusing a url bar starts a zero suggest request.
*/
@Test
@MediumTest
@Feature({"Omnibox"})
@RetryOnFailure
public void testRequestZeroSuggestOnFocus() {
final LocationBarLayout locationBar =
(LocationBarLayout) mActivityTestRule.getActivity().findViewById(R.id.location_bar);
final UrlBar urlBar = (UrlBar) mActivityTestRule.getActivity().findViewById(R.id.url_bar);
TestThreadUtils.runOnUiThreadBlocking(() -> { urlBar.setText("http://www.example.com/"); });
final TestAutocompleteController controller = new TestAutocompleteController(locationBar,
sEmptySuggestionListener, new HashMap<String, List<SuggestionsResult>>());
TestThreadUtils.runOnUiThreadBlocking(() -> {
AutocompleteCoordinatorTestUtils.setAutocompleteController(
locationBar.getAutocompleteCoordinator(), controller);
});
Assert.assertEquals("Should not have any zero suggest requests yet", 0,
controller.numZeroSuggestRequests());
OmniboxTestUtils.toggleUrlBarFocus(urlBar, true);
CriteriaHelper.pollInstrumentationThread(Criteria.equals(1, new Callable<Integer>() {
@Override
public Integer call() {
return controller.numZeroSuggestRequests();
}
}));
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
Assert.assertFalse(controller.isStartAutocompleteCalled());
}
/**
* Tests that focusing a url bar starts a zero suggest request.
*/
@Test
@DisableIf.
Build(sdk_is_greater_than = Build.VERSION_CODES.KITKAT, message = "crbug.com/1027549")
@MediumTest
@Feature({"Omnibox"})
@RetryOnFailure
public void testRequestZeroSuggestAfterDelete() throws InterruptedException {
final LocationBarLayout locationBar =
(LocationBarLayout) mActivityTestRule.getActivity().findViewById(R.id.location_bar);
final UrlBar urlBar = (UrlBar) mActivityTestRule.getActivity().findViewById(R.id.url_bar);
final ImageButton deleteButton =
(ImageButton) mActivityTestRule.getActivity().findViewById(R.id.delete_button);
final TestAutocompleteController controller = new TestAutocompleteController(locationBar,
sEmptySuggestionListener, new HashMap<String, List<SuggestionsResult>>());
OmniboxTestUtils.toggleUrlBarFocus(urlBar, true);
TestThreadUtils.runOnUiThreadBlocking(() -> {
AutocompleteCoordinatorTestUtils.setAutocompleteController(
locationBar.getAutocompleteCoordinator(), controller);
urlBar.setText("g");
});
CriteriaHelper.pollInstrumentationThread(
new Criteria("Should have drawn the delete button") {
@Override
public boolean isSatisfied() {
return deleteButton.getWidth() > 0;
}
});
// The click view below ends up clicking on the menu button underneath the delete button
// for some time after the delete button appears. Wait for UI to settle down before
// clicking.
UiUtils.settleDownUI(InstrumentationRegistry.getInstrumentation());
TouchCommon.singleClickView(deleteButton);
CriteriaHelper.pollInstrumentationThread(Criteria.equals(1, new Callable<Integer>() {
@Override
public Integer call() {
return controller.numZeroSuggestRequests();
}
}));
}
@Test
@DisableIf.
Build(sdk_is_greater_than = Build.VERSION_CODES.KITKAT, message = "crbug.com/1027549")
@MediumTest
@Feature({"Omnibox"})
@RetryOnFailure
public void testRequestZeroSuggestTypeAndBackspace() {
final LocationBarLayout locationBar =
(LocationBarLayout) mActivityTestRule.getActivity().findViewById(R.id.location_bar);
final UrlBar urlBar = (UrlBar) mActivityTestRule.getActivity().findViewById(R.id.url_bar);
final TestAutocompleteController controller = new TestAutocompleteController(locationBar,
sEmptySuggestionListener, new HashMap<String, List<SuggestionsResult>>());
OmniboxTestUtils.toggleUrlBarFocus(urlBar, true);
TestThreadUtils.runOnUiThreadBlocking(() -> {
AutocompleteCoordinatorTestUtils.setAutocompleteController(
locationBar.getAutocompleteCoordinator(), controller);
urlBar.setText("g");
urlBar.setSelection(1);
});
Assert.assertEquals("No calls to zero suggest yet", 0, controller.numZeroSuggestRequests());
KeyUtils.singleKeyEventView(
InstrumentationRegistry.getInstrumentation(), urlBar, KeyEvent.KEYCODE_DEL);
CriteriaHelper.pollInstrumentationThread(Criteria.equals(1, new Callable<Integer>() {
@Override
public Integer call() {
return controller.numZeroSuggestRequests();
}
}));
}
// Sanity check that no text is displayed in the omnibox when on the NTP page and that the hint
// text is correct.
@Test
......
......@@ -476,4 +476,58 @@ public class AutocompleteMediatorUnitTest {
SuggestionCommonProperties.LAYOUT_DIRECTION));
}
}
@Test
@Features.DisableFeatures({ChromeFeatureList.OMNIBOX_ADAPTIVE_SUGGESTIONS_COUNT,
ChromeFeatureList.OMNIBOX_DEFERRED_KEYBOARD_POPUP})
public void onUrlFocusChange_triggersZeroSuggest_nativeInitialized() {
when(mAutocompleteDelegate.isUrlBarFocused()).thenReturn(true);
when(mAutocompleteDelegate.didFocusUrlFromFakebox()).thenReturn(false);
Profile profile = Mockito.mock(Profile.class);
String url = "http://www.example.com";
String title = "Title";
int pageClassification = 2;
when(mToolbarDataProvider.getProfile()).thenReturn(profile);
when(mToolbarDataProvider.getCurrentUrl()).thenReturn(url);
when(mToolbarDataProvider.getTitle()).thenReturn(title);
when(mToolbarDataProvider.hasTab()).thenReturn(true);
when(mToolbarDataProvider.getPageClassification(false)).thenReturn(pageClassification);
when(mTextStateProvider.getTextWithAutocomplete()).thenReturn(url);
mMediator.onNativeInitialized();
mMediator.onUrlFocusChange(true);
verify(mAutocompleteController)
.startZeroSuggest(profile, url, url, pageClassification, title);
}
@Test
@Features.DisableFeatures({ChromeFeatureList.OMNIBOX_ADAPTIVE_SUGGESTIONS_COUNT,
ChromeFeatureList.OMNIBOX_DEFERRED_KEYBOARD_POPUP})
public void onUrlFocusChange_triggersZeroSuggest_nativeNotInitialized() {
when(mAutocompleteDelegate.isUrlBarFocused()).thenReturn(true);
when(mAutocompleteDelegate.didFocusUrlFromFakebox()).thenReturn(false);
Profile profile = Mockito.mock(Profile.class);
String url = "http://www.example.com";
String title = "Title";
int pageClassification = 2;
when(mToolbarDataProvider.getProfile()).thenReturn(profile);
when(mToolbarDataProvider.getCurrentUrl()).thenReturn(url);
when(mToolbarDataProvider.getTitle()).thenReturn(title);
when(mToolbarDataProvider.hasTab()).thenReturn(true);
when(mToolbarDataProvider.getPageClassification(false)).thenReturn(pageClassification);
when(mTextStateProvider.getTextWithAutocomplete()).thenReturn("");
// Signal focus prior to initializing native.
mMediator.onUrlFocusChange(true);
// Initialize native and ensure zero suggest is triggered.
mMediator.onNativeInitialized();
ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
verify(mAutocompleteController)
.startZeroSuggest(profile, "", url, pageClassification, title);
}
}
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