Commit 011b4cb3 authored by Shakti Sahu's avatar Shakti Sahu Committed by Commit Bot

Query Tiles: Submit query on last level tile click

This CL adds :
1 - Submit query and show SRP on the last level tile click
2 - Shorten most visited tile section to 1 row for smaller screens

Bug: 1068308
Change-Id: Icae29ad8bcc563ca6cfa3bfd35d18fa90ff65b7f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2147041
Commit-Queue: Shakti Sahu <shaktisahu@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Cr-Commit-Position: refs/heads/master@{#758724}
parent 93132287
...@@ -26,6 +26,14 @@ public interface FakeboxDelegate { ...@@ -26,6 +26,14 @@ public interface FakeboxDelegate {
void setUrlBarFocus( void setUrlBarFocus(
boolean shouldBeFocused, @Nullable String pastedText, @OmniboxFocusReason int reason); boolean shouldBeFocused, @Nullable String pastedText, @OmniboxFocusReason int reason);
/**
* Performs a search query on the current {@link Tab}. This calls {@link
* TemplateUrlService#getUrlForSearchQuery(String)} to get a url based on {@code query} and
* loads that url in the current {@link Tab}.
* @param query The {@link String} that represents the text query that should be searched for.
*/
void performSearchQuery(String query);
/** /**
* @return Whether the URL bar is currently focused. * @return Whether the URL bar is currently focused.
*/ */
......
...@@ -218,6 +218,12 @@ public class NewTabPage implements NativePage, InvalidationAwareThumbnailProvide ...@@ -218,6 +218,12 @@ public class NewTabPage implements NativePage, InvalidationAwareThumbnailProvide
} }
} }
@Override
public void performSearchQuery(String query) {
if (mFakeboxDelegate == null) return;
mFakeboxDelegate.performSearchQuery(query);
}
@Override @Override
public boolean isCurrentPage() { public boolean isCurrentPage() {
if (mIsDestroyed) return false; if (mIsDestroyed) return false;
......
...@@ -257,10 +257,11 @@ public class NewTabPageLayout extends LinearLayout implements TileGroup.Observer ...@@ -257,10 +257,11 @@ public class NewTabPageLayout extends LinearLayout implements TileGroup.Observer
setSearchProviderInfo(searchProviderHasLogo, searchProviderIsGoogle); setSearchProviderInfo(searchProviderHasLogo, searchProviderIsGoogle);
mSearchProviderLogoView.showSearchProviderInitialView(); mSearchProviderLogoView.showSearchProviderInitialView();
mQueryTileSection = new QueryTileSection( mQueryTileSection = new QueryTileSection(findViewById(R.id.query_tiles),
findViewById(R.id.query_tiles), mSearchBoxCoordinator, profile); mSearchBoxCoordinator, profile, mManager::performSearchQuery);
mTileGroup.startObserving(getMaxTileRows() * getMaxTileColumns()); mTileGroup.startObserving(
getMaxRowsForMostVisitedTiles() * getMaxColumnsForMostVisitedTiles());
VrModuleProvider.registerVrModeObserver(this); VrModuleProvider.registerVrModeObserver(this);
if (VrModuleProvider.getDelegate().isInVr()) onEnterVr(); if (VrModuleProvider.getDelegate().isInVr()) onEnterVr();
...@@ -775,15 +776,15 @@ public class NewTabPageLayout extends LinearLayout implements TileGroup.Observer ...@@ -775,15 +776,15 @@ public class NewTabPageLayout extends LinearLayout implements TileGroup.Observer
} }
} }
private static int getMaxTileRows() { private int getMaxRowsForMostVisitedTiles() {
return 2; return mQueryTileSection != null && mQueryTileSection.shouldConsiderAsSmallScreen() ? 1 : 2;
} }
/** /**
* Determines The maximum number of tiles to try and fit in a row. On smaller screens, there * Determines The maximum number of tiles to try and fit in a row. On smaller screens, there
* may not be enough space to fit all of them. * may not be enough space to fit all of them.
*/ */
private int getMaxTileColumns() { private int getMaxColumnsForMostVisitedTiles() {
return 4; return 4;
} }
......
...@@ -77,6 +77,12 @@ public class NewTabPageView extends FrameLayout { ...@@ -77,6 +77,12 @@ public class NewTabPageView extends FrameLayout {
*/ */
void focusSearchBox(boolean beginVoiceSearch, String pastedText); void focusSearchBox(boolean beginVoiceSearch, String pastedText);
/**
* Performs a search query on the current {@link Tab}.
* @param query The {@link String} representing the query text.
*/
void performSearchQuery(String query);
/** /**
* @return whether the {@link NewTabPage} associated with this manager is the current page * @return whether the {@link NewTabPage} associated with this manager is the current page
* displayed to the user. * displayed to the user.
......
...@@ -66,7 +66,6 @@ import org.chromium.chrome.browser.ui.native_page.NativePage; ...@@ -66,7 +66,6 @@ import org.chromium.chrome.browser.ui.native_page.NativePage;
import org.chromium.chrome.browser.util.AccessibilityUtil; import org.chromium.chrome.browser.util.AccessibilityUtil;
import org.chromium.components.browser_ui.styles.ChromeColors; import org.chromium.components.browser_ui.styles.ChromeColors;
import org.chromium.components.browser_ui.widget.CompositeTouchDelegate; import org.chromium.components.browser_ui.widget.CompositeTouchDelegate;
import org.chromium.components.search_engines.TemplateUrlService;
import org.chromium.content_public.browser.LoadUrlParams; import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.content_public.common.ResourceRequestBody; import org.chromium.content_public.common.ResourceRequestBody;
import org.chromium.ui.base.DeviceFormFactor; import org.chromium.ui.base.DeviceFormFactor;
...@@ -730,14 +729,8 @@ public class LocationBarLayout extends FrameLayout ...@@ -730,14 +729,8 @@ public class LocationBarLayout extends FrameLayout
// When we restore tabs, we focus the selected tab so the URL of the page shows. // When we restore tabs, we focus the selected tab so the URL of the page shows.
} }
/** @Override
* Performs a search query on the current {@link Tab}. This calls public void performSearchQuery(String query) {
* {@link TemplateUrlService#getUrlForSearchQuery(String)} to get a url based on {@code query}
* and loads that url in the current {@link Tab}.
* @param query The {@link String} that represents the text query that should be searched for.
*/
@VisibleForTesting
public void performSearchQueryForTest(String query) {
if (TextUtils.isEmpty(query)) return; if (TextUtils.isEmpty(query)) return;
String queryUrl = TemplateUrlServiceFactory.get().getUrlForSearchQuery(query); String queryUrl = TemplateUrlServiceFactory.get().getUrlForSearchQuery(query);
......
...@@ -13,6 +13,8 @@ import org.chromium.base.Callback; ...@@ -13,6 +13,8 @@ import org.chromium.base.Callback;
import org.chromium.chrome.browser.flags.ChromeFeatureList; import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.ntp.search.SearchBoxCoordinator; import org.chromium.chrome.browser.ntp.search.SearchBoxCoordinator;
import org.chromium.chrome.browser.profiles.Profile; import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.ui.display.DisplayAndroid;
import org.chromium.ui.display.DisplayUtil;
import java.util.List; import java.util.List;
...@@ -22,17 +24,24 @@ import java.util.List; ...@@ -22,17 +24,24 @@ import java.util.List;
* section. * section.
*/ */
public class QueryTileSection { public class QueryTileSection {
private static final String QUERY_TILES_SHORTEN_MOST_VISITED_TILES_FOR_SMALL_SCREEN =
"shorten_most_visited_tiles_for_small_screen";
private static final int SMALL_SCREEN_HEIGHT_THRESHOLD_DP = 600;
private final ViewGroup mQueryTileSectionView; private final ViewGroup mQueryTileSectionView;
private final SearchBoxCoordinator mSearchBoxCoordinator; private final SearchBoxCoordinator mSearchBoxCoordinator;
private final Callback<String> mSubmitQueryCallback;
private QueryTileCoordinator mQueryTileCoordinator; private QueryTileCoordinator mQueryTileCoordinator;
private TileProvider mTileProvider; private TileProvider mTileProvider;
/** Constructor. */ /** Constructor. */
public QueryTileSection(ViewGroup queryTileSectionView, public QueryTileSection(ViewGroup queryTileSectionView,
SearchBoxCoordinator searchBoxCoordinator, Profile profile) { SearchBoxCoordinator searchBoxCoordinator, Profile profile,
Callback<String> performSearchQueryCallback) {
mQueryTileSectionView = queryTileSectionView; mQueryTileSectionView = queryTileSectionView;
mSearchBoxCoordinator = searchBoxCoordinator; mSearchBoxCoordinator = searchBoxCoordinator;
if (!ChromeFeatureList.isEnabled(ChromeFeatureList.QUERY_TILES)) return; mSubmitQueryCallback = performSearchQueryCallback;
if (!isFeatureEnabled()) return;
mTileProvider = TileProviderFactory.getForProfile(profile); mTileProvider = TileProviderFactory.getForProfile(profile);
mQueryTileCoordinator = QueryTileCoordinatorFactory.create( mQueryTileCoordinator = QueryTileCoordinatorFactory.create(
...@@ -43,15 +52,46 @@ public class QueryTileSection { ...@@ -43,15 +52,46 @@ public class QueryTileSection {
} }
private void onTileClicked(Tile tile) { private void onTileClicked(Tile tile) {
mTileProvider.getQueryTiles(tiles -> { if (tile == null) {
mQueryTileCoordinator.setTiles(tiles); mTileProvider.getQueryTiles(this::setTiles);
mQueryTileSectionView.setVisibility(tiles.isEmpty() ? View.GONE : View.VISIBLE); } else {
}); boolean isLastLevelTile = tile.children.isEmpty();
setTiles(tile.children);
if (isLastLevelTile) {
mSubmitQueryCallback.onResult(tile.queryText);
} else {
// TODO(shaktisahu): Show chip on fakebox;
}
}
}
if (tile != null) mSearchBoxCoordinator.setSearchText(tile.queryText); private void setTiles(List<Tile> tiles) {
mQueryTileCoordinator.setTiles(tiles);
mQueryTileSectionView.setVisibility(tiles.isEmpty() ? View.GONE : View.VISIBLE);
} }
private void getVisuals(Tile tile, Callback<List<Bitmap>> callback) { private void getVisuals(Tile tile, Callback<List<Bitmap>> callback) {
mTileProvider.getVisuals(tile.id, callback); mTileProvider.getVisuals(tile.id, callback);
} }
}
\ No newline at end of file /**
* @return Whether the screen height is small. Used for shortening the most visited tiles
* section on NTP so that feed is still visible above the fold.
*/
public boolean shouldConsiderAsSmallScreen() {
if (!isFeatureEnabled()) return false;
boolean shortenMostVisitedTiles = ChromeFeatureList.getFieldTrialParamByFeatureAsBoolean(
ChromeFeatureList.QUERY_TILES,
QUERY_TILES_SHORTEN_MOST_VISITED_TILES_FOR_SMALL_SCREEN, false);
if (!shortenMostVisitedTiles) return false;
DisplayAndroid display =
DisplayAndroid.getNonMultiDisplay(mQueryTileSectionView.getContext());
int screenHeightDp = DisplayUtil.pxToDp(display, display.getDisplayHeight());
return screenHeightDp < SMALL_SCREEN_HEIGHT_THRESHOLD_DP;
}
private static boolean isFeatureEnabled() {
return ChromeFeatureList.isEnabled(ChromeFeatureList.QUERY_TILES);
}
}
...@@ -847,6 +847,9 @@ public class CustomTabToolbar extends ToolbarLayout implements View.OnLongClickL ...@@ -847,6 +847,9 @@ public class CustomTabToolbar extends ToolbarLayout implements View.OnLongClickL
return false; return false;
} }
@Override
public void performSearchQuery(String query) {}
@Override @Override
public void setUrlBarFocus(boolean shouldBeFocused, @Nullable String pastedText, public void setUrlBarFocus(boolean shouldBeFocused, @Nullable String pastedText,
@LocationBar.OmniboxFocusReason int reason) {} @LocationBar.OmniboxFocusReason int reason) {}
......
...@@ -8,8 +8,11 @@ import static android.support.test.espresso.Espresso.onView; ...@@ -8,8 +8,11 @@ import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.test.espresso.action.ViewActions;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
import org.junit.After; import org.junit.After;
...@@ -19,6 +22,7 @@ import org.junit.Test; ...@@ -19,6 +22,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.chromium.base.Callback; import org.chromium.base.Callback;
import org.chromium.base.ContextUtils;
import org.chromium.base.test.util.CommandLineFlags; import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.chrome.R; import org.chromium.chrome.R;
import org.chromium.chrome.browser.flags.ChromeFeatureList; import org.chromium.chrome.browser.flags.ChromeFeatureList;
...@@ -28,6 +32,8 @@ import org.chromium.chrome.test.ChromeJUnit4ClassRunner; ...@@ -28,6 +32,8 @@ import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.ChromeTabbedActivityTestRule; import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
import org.chromium.chrome.test.util.NewTabPageTestUtils; import org.chromium.chrome.test.util.NewTabPageTestUtils;
import org.chromium.chrome.test.util.browser.Features; 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;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -38,6 +44,8 @@ import java.util.List; ...@@ -38,6 +44,8 @@ import java.util.List;
@RunWith(ChromeJUnit4ClassRunner.class) @RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE}) @CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
public class QueryTileSectionTest { public class QueryTileSectionTest {
private static final String SEARCH_URL_PATTERN = "https://www.google.com/search?q=";
@Rule @Rule
public ChromeTabbedActivityTestRule mActivityTestRule = new ChromeTabbedActivityTestRule(); public ChromeTabbedActivityTestRule mActivityTestRule = new ChromeTabbedActivityTestRule();
...@@ -58,13 +66,35 @@ public class QueryTileSectionTest { ...@@ -58,13 +66,35 @@ public class QueryTileSectionTest {
@Features.EnableFeatures(ChromeFeatureList.QUERY_TILES) @Features.EnableFeatures(ChromeFeatureList.QUERY_TILES)
public void testSimpleTest() throws Exception { public void testSimpleTest() throws Exception {
onView(withId(R.id.query_tiles)).check(matches(isDisplayed())); onView(withId(R.id.query_tiles)).check(matches(isDisplayed()));
onView(withText("Tile 1")).check(matches(isDisplayed()));
}
@Test
@SmallTest
@Features.EnableFeatures(ChromeFeatureList.QUERY_TILES)
public void testLeafLevelTileLoadsSearchResultsPage() throws Exception {
onView(withId(R.id.query_tiles)).check(matches(isDisplayed()));
onView(withText("Tile 1")).perform(ViewActions.click());
onView(withText("Tile 1_1")).check(matches(isDisplayed())).perform(ViewActions.click());
waitForSearchResultsPage(mActivityTestRule.getActivity().getActivityTab());
}
private static void waitForSearchResultsPage(final Tab tab) {
CriteriaHelper.pollUiThread(new Criteria("The SRP was never loaded.") {
@Override
public boolean isSatisfied() {
return tab.getUrl().getValidSpecOrEmpty().contains(SEARCH_URL_PATTERN);
}
});
} }
private static class TestTileProvider implements TileProvider { private static class TestTileProvider implements TileProvider {
private List<Tile> mTiles = new ArrayList<>(); private List<Tile> mTiles = new ArrayList<>();
private TestTileProvider() { private TestTileProvider() {
Tile tile = new Tile("1", "Tile 1", "Tile 1", "Tile 1", null); List<Tile> children = new ArrayList<>();
children.add(new Tile("tile1_1", "Tile 1_1", "Tile 1_1", "Tile 1_1 Query", null));
Tile tile = new Tile("1", "Tile 1", "Tile 1", "Tile 1 Query", children);
mTiles.add(tile); mTiles.add(tile);
} }
...@@ -75,7 +105,12 @@ public class QueryTileSectionTest { ...@@ -75,7 +105,12 @@ public class QueryTileSectionTest {
@Override @Override
public void getVisuals(String id, Callback<List<Bitmap>> callback) { public void getVisuals(String id, Callback<List<Bitmap>> callback) {
callback.onResult(null); List<Bitmap> bitmapList = new ArrayList<>();
Bitmap bitmap = BitmapFactory.decodeResource(
ContextUtils.getApplicationContext().getResources(),
R.drawable.chrome_sync_logo);
bitmapList.add(bitmap);
callback.onResult(bitmapList);
} }
} }
} }
\ No newline at end of file
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