Commit bee2010f authored by Shakti Sahu's avatar Shakti Sahu Committed by Commit Bot

Reland "Query Tiles : Integration test"

This is a reland of d95d2248

Original change's description:
> Query Tiles : Integration test
> 
> This CL adds end-to-end Integration tests for query tiles. The network
> layer is substituted by a embedded test server that serves a fake
> server response. Added omnibox javatests that use this mechanism to
> test the full flow.
> 
> Bug: 1083807
> Change-Id: Ida65dd9e093ce2311def892158c6bbe4de1fe3c3
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2206153
> Commit-Queue: Shakti Sahu <shaktisahu@chromium.org>
> Reviewed-by: David Trainor <dtrainor@chromium.org>
> Reviewed-by: Shakti Sahu <shaktisahu@chromium.org>
> Reviewed-by: Hesen Zhang <hesen@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#770512}

Bug: 1083807
Change-Id: I7f65bfb79006201bdaa545c0aba8dd7cf2b4a243
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2210654Reviewed-by: default avatarXing Liu <xingliu@chromium.org>
Reviewed-by: default avatarDavid Trainor <dtrainor@chromium.org>
Reviewed-by: default avatarHesen Zhang <hesen@chromium.org>
Commit-Queue: Shakti Sahu <shaktisahu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#772139}
parent 1d695526
......@@ -1732,6 +1732,7 @@ generate_jni("test_support_jni_headers") {
"javatests/src/org/chromium/chrome/browser/offlinepages/OfflineTestUtil.java",
"javatests/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchTestBridge.java",
"javatests/src/org/chromium/chrome/browser/prerender/PrerenderTestHelper.java",
"javatests/src/org/chromium/chrome/browser/query_tiles/QueryTileFakeServer.java",
"javatests/src/org/chromium/chrome/browser/test/MockCertVerifierRuleAndroid.java",
]
}
......@@ -1744,6 +1745,7 @@ android_library("browser_java_test_support") {
"javatests/src/org/chromium/chrome/browser/offlinepages/OfflineTestUtil.java",
"javatests/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchTestBridge.java",
"javatests/src/org/chromium/chrome/browser/prerender/PrerenderTestHelper.java",
"javatests/src/org/chromium/chrome/browser/query_tiles/QueryTileFakeServer.java",
"javatests/src/org/chromium/chrome/browser/sync/FakeServerHelper.java",
"javatests/src/org/chromium/chrome/browser/test/MockCertVerifierRuleAndroid.java",
]
......@@ -1772,13 +1774,17 @@ static_library("browser_test_support") {
"../browser/offline_pages/android/offline_test_util_jni.cc",
"../browser/offline_pages/android/prefetch_test_bridge.cc",
"../browser/prerender/prerender_test_helper.cc",
"../browser/query_tiles/query_tile_fake_server.cc",
]
deps = [
":test_support_jni_headers",
"//chrome/browser",
"//chrome/browser/thumbnail:test_support",
"//components/offline_pages/core/background:test_support",
"//components/query_tiles",
"//components/query_tiles/test:test_support",
"//content/test:test_support",
"//net:test_support",
]
}
......
......@@ -430,6 +430,7 @@ chrome_test_java_sources = [
"javatests/src/org/chromium/chrome/browser/provider/ProviderTestRule.java",
"javatests/src/org/chromium/chrome/browser/push_messaging/PushMessagingTest.java",
"javatests/src/org/chromium/chrome/browser/query_tiles/ListMatchers.java",
"javatests/src/org/chromium/chrome/browser/query_tiles/OmniboxQueryTileSuggestionTest.java",
"javatests/src/org/chromium/chrome/browser/query_tiles/QueryTileSectionTest.java",
"javatests/src/org/chromium/chrome/browser/query_tiles/QueryTileSectionToOmniboxTest.java",
"javatests/src/org/chromium/chrome/browser/query_tiles/TestTileProvider.java",
......
......@@ -5,6 +5,7 @@
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/omnibox_query_tiles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/card_padding">
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.query_tiles;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.clearText;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
import static android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.chromium.chrome.browser.query_tiles.ListMatchers.matchList;
import static org.chromium.chrome.browser.query_tiles.ViewActions.scrollTo;
import android.support.test.filters.SmallTest;
import android.view.View;
import android.widget.TextView;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.task.PostTask;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.init.BrowserParts;
import org.chromium.chrome.browser.init.ChromeBrowserInitializer;
import org.chromium.chrome.browser.init.EmptyBrowserParts;
import org.chromium.chrome.browser.omnibox.LocationBarLayout;
import org.chromium.chrome.browser.omnibox.UrlBar;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
import org.chromium.chrome.test.util.NewTabPageTestUtils;
import org.chromium.chrome.test.util.OmniboxTestUtils;
import org.chromium.chrome.test.util.browser.Features;
import org.chromium.components.embedder_support.util.UrlConstants;
import org.chromium.components.query_tiles.QueryTile;
import org.chromium.components.query_tiles.TileProvider;
import org.chromium.content_public.browser.UiThreadTaskTraits;
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 java.util.concurrent.atomic.AtomicBoolean;
/**
* Tests for the query tiles section on the omnibox. The test uses a fake server in native to
* perform a full end-to-end test for omnibox query tiles.
*/
@RunWith(ChromeJUnit4ClassRunner.class)
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
@Features.EnableFeatures({ChromeFeatureList.QUERY_TILES, ChromeFeatureList.QUERY_TILES_IN_OMNIBOX})
public class OmniboxQueryTileSuggestionTest {
private static final String SEARCH_URL_PATTERN = "https://www.google.com/search?q=";
@Rule
public ChromeTabbedActivityTestRule mActivityTestRule = new ChromeTabbedActivityTestRule();
private Tab mTab;
private UrlBar mUrlBar;
private LocationBarLayout mLocationBar;
// A convenient wrapper around the real provider, used for matching purposes.
private TestTileProvider mTileProvider;
@Before
public void setUp() {
loadNative();
final AtomicBoolean tilesFetched = new AtomicBoolean();
TestThreadUtils.runOnUiThreadBlocking(() -> {
QueryTileFakeServer.setupFakeServer(2, 4, success -> tilesFetched.set(true));
});
mActivityTestRule.startMainActivityOnBlankPage();
mActivityTestRule.loadUrl(UrlConstants.NTP_URL);
mTab = mActivityTestRule.getActivity().getActivityTab();
NewTabPageTestUtils.waitForNtpLoaded(mTab);
CriteriaHelper.pollUiThread(tilesFetched::get, "Tile fetch was not completed.");
// Accessing profile needs UI thread.
TestThreadUtils.runOnUiThreadBlocking(() -> {
TileProvider realProvider =
TileProviderFactory.getForProfile(Profile.getLastUsedRegularProfile());
mTileProvider = new TestTileProvider(realProvider);
});
mUrlBar = (UrlBar) mActivityTestRule.getActivity().findViewById(R.id.url_bar);
mLocationBar =
(LocationBarLayout) mActivityTestRule.getActivity().findViewById(R.id.location_bar);
}
@After
public void tearDown() {}
/** Tests that tiles show up as expected when omnibox is opened. */
@Test
@SmallTest
public void testShowOmniboxQueryTileSuggestion() throws Exception {
clickNTPSearchBox();
OmniboxTestUtils.waitForFocusAndKeyboardActive(mUrlBar, true);
OmniboxTestUtils.waitForOmniboxSuggestions(mLocationBar);
Matcher<View> recyclerViewMatcher = withParent(withId(R.id.omnibox_query_tiles));
matchList(recyclerViewMatcher, mTileProvider.getChildrenOf());
}
/** Tests that clicking on a tile opens up the next level tiles in omnibox. */
@Test
@SmallTest
public void testClickTileOpensNextTierTiles() throws Exception {
clickNTPSearchBox();
OmniboxTestUtils.waitForFocusAndKeyboardActive(mUrlBar, true);
OmniboxTestUtils.waitForOmniboxSuggestions(mLocationBar);
Matcher<View> recyclerViewMatcher = withParent(withId(R.id.omnibox_query_tiles));
matchList(recyclerViewMatcher, mTileProvider.getChildrenOf());
// Click a tile. Suggestion with next level tiles should show up, and omnibox should show
// query text from the tile.
QueryTile tile = mTileProvider.getTileAt(0);
onView(recyclerViewMatcher).perform(scrollTo(0));
onView(tileSuggestionMatcher(tile)).perform(click());
matchList(recyclerViewMatcher, mTileProvider.getChildrenOf(0));
Assert.assertTrue(mUrlBar.getText().toString().contains(tile.queryText));
}
/** Tests that clicking on a tile hides the suggestion and shows the query on omnibox. */
@Test
@SmallTest
@Features.EnableFeatures(ChromeFeatureList.QUERY_TILES_ENABLE_QUERY_EDITING)
public void testClickTileOpensQueryEditMode() throws Exception {
clickNTPSearchBox();
OmniboxTestUtils.waitForFocusAndKeyboardActive(mUrlBar, true);
OmniboxTestUtils.waitForOmniboxSuggestions(mLocationBar);
Matcher<View> recyclerViewMatcher = withParent(withId(R.id.omnibox_query_tiles));
matchList(recyclerViewMatcher, mTileProvider.getChildrenOf());
// Click the first level tile.
QueryTile tile = mTileProvider.getTileAt(0);
QueryTile subtile = mTileProvider.getTileAt(0, 0);
onView(recyclerViewMatcher).perform(scrollTo(0));
onView(tileSuggestionMatcher(tile)).perform(click());
// Click the last level tile. The omnibox should show the query text. No suggestions should
// show up.
Assert.assertTrue(mUrlBar.getText().toString().contains(tile.queryText));
onView(recyclerViewMatcher).perform(scrollTo(0));
onView(tileSuggestionMatcher(subtile)).perform(click());
Assert.assertTrue(mUrlBar.getText().toString().contains(subtile.queryText));
onView(recyclerViewMatcher).check(doesNotExist());
}
/** Tests that clicking a last level tile loads the search result page. */
@Test
@SmallTest
public void testClickLastLevelTileOpensSearchResultsPage() throws Exception {
clickNTPSearchBox();
OmniboxTestUtils.waitForFocusAndKeyboardActive(mUrlBar, true);
OmniboxTestUtils.waitForOmniboxSuggestions(mLocationBar);
Matcher<View> recyclerViewMatcher = withParent(withId(R.id.omnibox_query_tiles));
matchList(recyclerViewMatcher, mTileProvider.getChildrenOf());
// Click twice to get to the search results page.
QueryTile tile = mTileProvider.getTileAt(0);
QueryTile subtile = mTileProvider.getTileAt(0, 0);
onView(recyclerViewMatcher).perform(scrollTo(0));
onView(tileSuggestionMatcher(tile)).perform(click());
matchList(recyclerViewMatcher, mTileProvider.getChildrenOf(0));
Assert.assertTrue(mUrlBar.getText().toString().contains(tile.queryText));
onView(recyclerViewMatcher).perform(scrollTo(0));
onView(tileSuggestionMatcher(subtile)).perform(click());
waitForSearchResultsPage();
Assert.assertTrue(getTabUrl().contains(subtile.queryText));
onView(recyclerViewMatcher).check(doesNotExist());
}
/** Tests that typing on omnibox will dismiss the query tiles suggestion. */
@Test
@SmallTest
public void testTypingInOmniboxDismissesQueryTileSuggestion() throws Exception {
clickNTPSearchBox();
OmniboxTestUtils.waitForFocusAndKeyboardActive(mUrlBar, true);
OmniboxTestUtils.waitForOmniboxSuggestions(mLocationBar);
Matcher<View> recyclerViewMatcher = withParent(withId(R.id.omnibox_query_tiles));
matchList(recyclerViewMatcher, mTileProvider.getChildrenOf());
QueryTile tile = mTileProvider.getTileAt(0);
onView(recyclerViewMatcher).perform(scrollTo(0));
onView(tileSuggestionMatcher(tile)).perform(click());
matchList(recyclerViewMatcher, mTileProvider.getChildrenOf(0));
Assert.assertTrue(mUrlBar.getText().toString().contains(tile.queryText));
// Start editing the text. The query tile suggestion should disappear.
onView(withId(R.id.url_bar)).perform(replaceText("xyz"));
waitForOmniboxQueryTileSuggestion(false);
// Backspace all the way to zero suggest. The query tile suggestion should reappear.
onView(withId(R.id.url_bar)).perform(clearText());
matchList(recyclerViewMatcher, mTileProvider.getChildrenOf());
}
private Matcher<View> tileSuggestionMatcher(QueryTile tile) {
Matcher<View> recyclerViewMatcher = withParent(withId(R.id.omnibox_query_tiles));
return Matchers.allOf(isDescendantOfA(recyclerViewMatcher), withText(tile.displayTitle));
}
private void clickNTPSearchBox() {
// Directly clicking search box using espresso doesn't work and seems to forward the click
// event to the chip cancel button instead.
TestThreadUtils.runOnUiThreadBlocking(() -> {
TextView searchBox =
(TextView) mActivityTestRule.getActivity().findViewById(R.id.search_box_text);
searchBox.performClick();
});
}
private String getTabUrl() {
return mActivityTestRule.getActivity().getActivityTab().getUrl().getValidSpecOrEmpty();
}
private void waitForOmniboxQueryTileSuggestion(boolean visible) {
CriteriaHelper.pollUiThread(new Criteria(
"The omnibox query tile suggestion didn't match the expected visibility: "
+ visible) {
@Override
public boolean isSatisfied() {
View view = mActivityTestRule.getActivity().findViewById(R.id.omnibox_query_tiles);
boolean isVisible = view != null && view.getVisibility() == View.VISIBLE;
return isVisible == visible;
}
});
}
private void waitForSearchResultsPage() {
CriteriaHelper.pollUiThread(
new Criteria("The SRP was never loaded. " + mTab.getUrl().getValidSpecOrEmpty()) {
@Override
public boolean isSatisfied() {
return mTab.getUrl().getValidSpecOrEmpty().contains(SEARCH_URL_PATTERN);
}
});
}
private void loadNative() {
final AtomicBoolean mNativeLoaded = new AtomicBoolean();
final BrowserParts parts = new EmptyBrowserParts() {
@Override
public void finishNativeInitialization() {
mNativeLoaded.set(true);
}
};
PostTask.postTask(UiThreadTaskTraits.DEFAULT, () -> {
ChromeBrowserInitializer.getInstance().handlePreNativeStartup(parts);
ChromeBrowserInitializer.getInstance().handlePostNativeStartup(true, parts);
});
CriteriaHelper.pollUiThread(
() -> mNativeLoaded.get(), "Failed while waiting for starting native.");
}
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.query_tiles;
import org.chromium.base.Callback;
/**
* Spins up a test server and starts serving query tiles in native.
*/
public class QueryTileFakeServer {
/**
* Spins up a test server and starts serving query tiles in native. Should be invoked after
* native initialization and before rest of the chrome and omnibox is loaded.
* @param levels The number of levels of query tiles.
* @param tilesPerLevel The number of tiles in each level.
* @param onFetchCompletedCallback The callback to be invoked after server fetch is complete.
*/
public static void setupFakeServer(
int levels, int tilesPerLevel, Callback<Boolean> onFetchCompletedCallback) {
nativeSetupFakeServer(onFetchCompletedCallback, levels, tilesPerLevel);
}
static native void nativeSetupFakeServer(
Callback<Boolean> onFetchCompletedCallback, int levels, int tilesPerLevel);
}
\ No newline at end of file
......@@ -28,6 +28,14 @@ class TestTileProvider implements TileProvider {
mTiles = buildTiles("Tile", levels, count);
}
/**
* Builds and populates a {@link TestTileProvider} around the real provider. Convenient for
* matching purposes.
*/
public TestTileProvider(TileProvider realProvider) {
realProvider.getQueryTiles(tiles -> { mTiles = tiles; });
}
/**
* Finds a tile by traversing the tree.
* @param indices The indices for each child to select as the tree is traversed.
......@@ -75,4 +83,4 @@ class TestTileProvider implements TileProvider {
return children;
}
}
\ No newline at end of file
}
......@@ -352,6 +352,7 @@ public abstract class ChromeFeatureList {
public static final String PRIORITIZE_BOOTSTRAP_TASKS = "PrioritizeBootstrapTasks";
public static final String PROBABILISTIC_CRYPTID_RENDERER = "ProbabilisticCryptidRenderer";
public static final String QUERY_TILES = "QueryTiles";
public static final String QUERY_TILES_IN_OMNIBOX = "QueryTilesInOmnibox";
public static final String QUERY_TILES_ENABLE_QUERY_EDITING = "QueryTilesEnableQueryEditing";
public static final String QUERY_IN_OMNIBOX = "QueryInOmnibox";
public static final String QUIET_NOTIFICATION_PROMPTS = "QuietNotificationPrompts";
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/android/callback_android.h"
#include "base/android/jni_android.h"
#include "base/android/scoped_java_ref.h"
#include "chrome/android/test_support_jni_headers/QueryTileFakeServer_jni.h"
#include "chrome/browser/android/profile_key_util.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/query_tiles/tile_service_factory.h"
#include "components/query_tiles/switches.h"
#include "components/query_tiles/test/fake_server_response.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
using base::android::JavaParamRef;
using base::android::ScopedJavaGlobalRef;
using net::test_server::EmbeddedTestServer;
using net::test_server::HttpRequest;
using net::test_server::HttpResponse;
namespace {
// An instance of test server responding with the required fake tiles.
static std::unique_ptr<EmbeddedTestServer> s_embedded_test_server;
std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
int levels,
int tiles_per_level,
const net::test_server::HttpRequest& request) {
auto response = std::make_unique<net::test_server::BasicHttpResponse>();
response->set_code(net::HTTP_OK);
std::string proto =
query_tiles::FakeServerResponse::CreateServerResponseProto(
levels, tiles_per_level);
response->set_content(proto);
return std::move(response);
}
void OnTilesFetched(const ScopedJavaGlobalRef<jobject>& j_callback,
bool success) {
base::android::RunBooleanCallbackAndroid(j_callback, success);
}
} // namespace
JNI_EXPORT void JNI_QueryTileFakeServer_SetupFakeServer(
JNIEnv* env,
const JavaParamRef<jobject>& j_callback,
jint levels,
jint tiles_per_level) {
s_embedded_test_server = std::make_unique<EmbeddedTestServer>();
s_embedded_test_server->RegisterRequestHandler(
base::BindRepeating(&HandleRequest, levels, tiles_per_level));
DCHECK(s_embedded_test_server->Start());
GURL url = s_embedded_test_server->GetURL("/fake_server_url");
query_tiles::FakeServerResponse::SetTileFetcherServerURL(url);
auto* profile_key = android::GetLastUsedRegularProfileKey();
query_tiles::TileService* tile_service =
query_tiles::TileServiceFactory::GetInstance()->GetForKey(profile_key);
tile_service->StartFetchForTiles(
false, base::BindOnce(&OnTilesFetched,
ScopedJavaGlobalRef<jobject>(j_callback)));
}
......@@ -6,6 +6,7 @@
#include <utility>
#include "base/lazy_instance.h"
#include "components/query_tiles/internal/stats.h"
#include "net/base/url_util.h"
#include "net/http/http_request_headers.h"
......@@ -18,6 +19,9 @@
namespace query_tiles {
namespace {
// An override server URL for testing.
base::LazyInstance<GURL>::Leaky g_override_url_for_testing;
const char kRequestContentType[] = "application/x-protobuf";
constexpr net::NetworkTrafficAnnotationTag kQueryTilesFetcherTrafficAnnotation =
......@@ -94,6 +98,10 @@ class TileFetcherImpl : public TileFetcher {
request->headers.SetHeader(net::HttpRequestHeaders::kAcceptLanguage,
accept_languages_);
}
if (!g_override_url_for_testing.Get().is_empty())
request->url = g_override_url_for_testing.Get();
return request;
}
......@@ -175,6 +183,11 @@ std::unique_ptr<TileFetcher> TileFetcher::Create(
client_version, url_loader_factory);
}
// static
void TileFetcher::SetOverrideURLForTesting(const GURL& url) {
g_override_url_for_testing.Get() = url;
}
TileFetcher::TileFetcher() = default;
TileFetcher::~TileFetcher() = default;
......
......@@ -39,6 +39,9 @@ class TileFetcher {
const std::string& client_version,
const scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
// For testing only.
static void SetOverrideURLForTesting(const GURL& url);
// Start the fetch to download tiles.
virtual void StartFetchForTiles(FinishedCallback callback) = 0;
......
......@@ -6,11 +6,17 @@ source_set("test_support") {
testonly = true
sources = [
"fake_server_response.cc",
"fake_server_response.h",
"fake_tile_service.cc",
"fake_tile_service.h",
]
deps = [ "//components/query_tiles:public" ]
deps = [
"//components/query_tiles:public",
"//components/query_tiles/internal",
"//components/query_tiles/proto",
]
}
source_set("test_lib") {
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/query_tiles/test/fake_server_response.h"
#include "base/strings/string_number_conversions.h"
#include "components/query_tiles/internal/tile_fetcher.h"
#include "components/query_tiles/proto/tile_response.pb.h"
namespace query_tiles {
namespace {
constexpr char kTestLocale[] = "en";
std::string BuildPrefix(const std::string& prefix, size_t pos) {
return prefix + "_" + base::NumberToString(pos);
}
void CreateTiles(proto::TileInfoGroup* info_group,
proto::TileInfo* parent,
const std::string& prefix,
int levels,
size_t tiles_per_level) {
if (levels <= 0)
return;
// Add sub-tiles.
for (size_t j = 0; j < tiles_per_level; j++) {
std::string subprefix = BuildPrefix(prefix, j);
parent->add_sub_tile_ids(subprefix + "_id");
auto* new_tile = info_group->add_tiles();
new_tile->set_tile_id(subprefix + "_id");
new_tile->set_display_text(subprefix + "_display_text");
new_tile->set_accessibility_text(subprefix + "_accessibility_text");
new_tile->set_query_string(subprefix + "_query_string");
new_tile->set_is_top_level(false);
// Add sub-tiles.
CreateTiles(info_group, new_tile, subprefix, levels - 1, tiles_per_level);
}
}
// Build a fake two level response proto.
void InitResponseProto(proto::ServerResponse* response,
int levels,
size_t tiles_per_level) {
proto::TileInfoGroup* info_group = response->mutable_tile_group();
info_group->set_locale(kTestLocale);
// Add top level tiles.
for (size_t i = 0; i < tiles_per_level; i++) {
auto* new_top_level_tile = info_group->add_tiles();
std::string prefix = BuildPrefix("Tile", i);
new_top_level_tile->set_tile_id(prefix + "_id");
new_top_level_tile->set_display_text(prefix + "_display_text");
new_top_level_tile->set_accessibility_text(prefix + "_accessibility_text");
new_top_level_tile->set_query_string(prefix + "_query_string");
new_top_level_tile->set_is_top_level(true);
// Add sub-tiles.
CreateTiles(info_group, new_top_level_tile, prefix, levels - 1,
tiles_per_level);
}
}
} // namespace
// static
void FakeServerResponse::SetTileFetcherServerURL(const GURL& url) {
TileFetcher::SetOverrideURLForTesting(url);
}
// static
std::string FakeServerResponse::CreateServerResponseProto(int levels,
int tiles_per_level) {
proto::ServerResponse server_response;
InitResponseProto(&server_response, levels, tiles_per_level);
std::string response_str;
DCHECK(server_response.SerializeToString(&response_str));
return response_str;
}
} // namespace query_tiles
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_QUERY_TILES_TEST_FAKE_SERVER_RESPONSE_H_
#define COMPONENTS_QUERY_TILES_TEST_FAKE_SERVER_RESPONSE_H_
#include <memory>
#include <string>
#include "url/gurl.h"
namespace query_tiles {
// This class provides the necessary utilities that can be used to fake the
// query tiles server interaction for using in tests.
class FakeServerResponse {
public:
// Sets the query tile server endpoint to the given |url|.
static void SetTileFetcherServerURL(const GURL& url);
// Creates a fake server response proto, which has |levels| tiers, and each
// tier has |tiles_per_level| tiles.
static std::string CreateServerResponseProto(int levels, int tiles_per_level);
};
} // namespace query_tiles
#endif // COMPONENTS_QUERY_TILES_TEST_FAKE_SERVER_RESPONSE_H_
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