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") { ...@@ -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/OfflineTestUtil.java",
"javatests/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchTestBridge.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/prerender/PrerenderTestHelper.java",
"javatests/src/org/chromium/chrome/browser/query_tiles/QueryTileFakeServer.java",
"javatests/src/org/chromium/chrome/browser/test/MockCertVerifierRuleAndroid.java", "javatests/src/org/chromium/chrome/browser/test/MockCertVerifierRuleAndroid.java",
] ]
} }
...@@ -1744,6 +1745,7 @@ android_library("browser_java_test_support") { ...@@ -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/OfflineTestUtil.java",
"javatests/src/org/chromium/chrome/browser/offlinepages/prefetch/PrefetchTestBridge.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/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/sync/FakeServerHelper.java",
"javatests/src/org/chromium/chrome/browser/test/MockCertVerifierRuleAndroid.java", "javatests/src/org/chromium/chrome/browser/test/MockCertVerifierRuleAndroid.java",
] ]
...@@ -1772,13 +1774,17 @@ static_library("browser_test_support") { ...@@ -1772,13 +1774,17 @@ static_library("browser_test_support") {
"../browser/offline_pages/android/offline_test_util_jni.cc", "../browser/offline_pages/android/offline_test_util_jni.cc",
"../browser/offline_pages/android/prefetch_test_bridge.cc", "../browser/offline_pages/android/prefetch_test_bridge.cc",
"../browser/prerender/prerender_test_helper.cc", "../browser/prerender/prerender_test_helper.cc",
"../browser/query_tiles/query_tile_fake_server.cc",
] ]
deps = [ deps = [
":test_support_jni_headers", ":test_support_jni_headers",
"//chrome/browser", "//chrome/browser",
"//chrome/browser/thumbnail:test_support", "//chrome/browser/thumbnail:test_support",
"//components/offline_pages/core/background:test_support", "//components/offline_pages/core/background:test_support",
"//components/query_tiles",
"//components/query_tiles/test:test_support",
"//content/test:test_support", "//content/test:test_support",
"//net:test_support",
] ]
} }
......
...@@ -430,6 +430,7 @@ chrome_test_java_sources = [ ...@@ -430,6 +430,7 @@ chrome_test_java_sources = [
"javatests/src/org/chromium/chrome/browser/provider/ProviderTestRule.java", "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/push_messaging/PushMessagingTest.java",
"javatests/src/org/chromium/chrome/browser/query_tiles/ListMatchers.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/QueryTileSectionTest.java",
"javatests/src/org/chromium/chrome/browser/query_tiles/QueryTileSectionToOmniboxTest.java", "javatests/src/org/chromium/chrome/browser/query_tiles/QueryTileSectionToOmniboxTest.java",
"javatests/src/org/chromium/chrome/browser/query_tiles/TestTileProvider.java", "javatests/src/org/chromium/chrome/browser/query_tiles/TestTileProvider.java",
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
<FrameLayout <FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/omnibox_query_tiles"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:padding="@dimen/card_padding"> 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 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 { ...@@ -28,6 +28,14 @@ class TestTileProvider implements TileProvider {
mTiles = buildTiles("Tile", levels, count); 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. * Finds a tile by traversing the tree.
* @param indices The indices for each child to select as the tree is traversed. * @param indices The indices for each child to select as the tree is traversed.
...@@ -75,4 +83,4 @@ class TestTileProvider implements TileProvider { ...@@ -75,4 +83,4 @@ class TestTileProvider implements TileProvider {
return children; return children;
} }
} }
\ No newline at end of file
...@@ -352,6 +352,7 @@ public abstract class ChromeFeatureList { ...@@ -352,6 +352,7 @@ public abstract class ChromeFeatureList {
public static final String PRIORITIZE_BOOTSTRAP_TASKS = "PrioritizeBootstrapTasks"; public static final String PRIORITIZE_BOOTSTRAP_TASKS = "PrioritizeBootstrapTasks";
public static final String PROBABILISTIC_CRYPTID_RENDERER = "ProbabilisticCryptidRenderer"; public static final String PROBABILISTIC_CRYPTID_RENDERER = "ProbabilisticCryptidRenderer";
public static final String QUERY_TILES = "QueryTiles"; 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_TILES_ENABLE_QUERY_EDITING = "QueryTilesEnableQueryEditing";
public static final String QUERY_IN_OMNIBOX = "QueryInOmnibox"; public static final String QUERY_IN_OMNIBOX = "QueryInOmnibox";
public static final String QUIET_NOTIFICATION_PROMPTS = "QuietNotificationPrompts"; 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 @@ ...@@ -6,6 +6,7 @@
#include <utility> #include <utility>
#include "base/lazy_instance.h"
#include "components/query_tiles/internal/stats.h" #include "components/query_tiles/internal/stats.h"
#include "net/base/url_util.h" #include "net/base/url_util.h"
#include "net/http/http_request_headers.h" #include "net/http/http_request_headers.h"
...@@ -18,6 +19,9 @@ ...@@ -18,6 +19,9 @@
namespace query_tiles { namespace query_tiles {
namespace { namespace {
// An override server URL for testing.
base::LazyInstance<GURL>::Leaky g_override_url_for_testing;
const char kRequestContentType[] = "application/x-protobuf"; const char kRequestContentType[] = "application/x-protobuf";
constexpr net::NetworkTrafficAnnotationTag kQueryTilesFetcherTrafficAnnotation = constexpr net::NetworkTrafficAnnotationTag kQueryTilesFetcherTrafficAnnotation =
...@@ -94,6 +98,10 @@ class TileFetcherImpl : public TileFetcher { ...@@ -94,6 +98,10 @@ class TileFetcherImpl : public TileFetcher {
request->headers.SetHeader(net::HttpRequestHeaders::kAcceptLanguage, request->headers.SetHeader(net::HttpRequestHeaders::kAcceptLanguage,
accept_languages_); accept_languages_);
} }
if (!g_override_url_for_testing.Get().is_empty())
request->url = g_override_url_for_testing.Get();
return request; return request;
} }
...@@ -175,6 +183,11 @@ std::unique_ptr<TileFetcher> TileFetcher::Create( ...@@ -175,6 +183,11 @@ std::unique_ptr<TileFetcher> TileFetcher::Create(
client_version, url_loader_factory); 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;
TileFetcher::~TileFetcher() = default; TileFetcher::~TileFetcher() = default;
......
...@@ -39,6 +39,9 @@ class TileFetcher { ...@@ -39,6 +39,9 @@ class TileFetcher {
const std::string& client_version, const std::string& client_version,
const scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory); const scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
// For testing only.
static void SetOverrideURLForTesting(const GURL& url);
// Start the fetch to download tiles. // Start the fetch to download tiles.
virtual void StartFetchForTiles(FinishedCallback callback) = 0; virtual void StartFetchForTiles(FinishedCallback callback) = 0;
......
...@@ -6,11 +6,17 @@ source_set("test_support") { ...@@ -6,11 +6,17 @@ source_set("test_support") {
testonly = true testonly = true
sources = [ sources = [
"fake_server_response.cc",
"fake_server_response.h",
"fake_tile_service.cc", "fake_tile_service.cc",
"fake_tile_service.h", "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") { 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