From fad93bf3d2ad920053b64af8402c8156d1b520e9 Mon Sep 17 00:00:00 2001 From: Darnell Andries Date: Wed, 3 Jul 2024 14:45:04 -0700 Subject: [PATCH] Fix Web Discovery Android test --- browser/sources.gni | 1 - components/web_discovery/browser/BUILD.gn | 6 +-- .../browser/content_scraper_browsertest.cc | 40 +++++++++++++++---- test/BUILD.gn | 1 - 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/browser/sources.gni b/browser/sources.gni index 29f317d87e78..92dc904384de 100644 --- a/browser/sources.gni +++ b/browser/sources.gni @@ -205,7 +205,6 @@ brave_chrome_browser_deps = [ "//brave/components/speedreader/common/buildflags", "//brave/components/tor/buildflags", "//brave/components/version_info", - "//brave/components/web_discovery/browser", "//brave/components/web_discovery/common", "//brave/components/web_discovery/common/buildflags", "//brave/components/webcompat:core", diff --git a/components/web_discovery/browser/BUILD.gn b/components/web_discovery/browser/BUILD.gn index 50d24344e7eb..ab2c6b354eb5 100644 --- a/components/web_discovery/browser/BUILD.gn +++ b/components/web_discovery/browser/BUILD.gn @@ -103,15 +103,11 @@ source_set("browser_tests") { "//brave/components/constants", "//brave/components/web_discovery/common", "//brave/components/web_discovery/common:mojom", + "//chrome/test:test_support", "//content/test:test_support", "//net:test_support", "//services/service_manager/public/cpp", "//testing/gtest", ] - if (!is_android) { - deps += [ "//chrome/test:test_support_ui" ] - } else { - deps += [ "//chrome/test:test_support_ui_android" ] - } defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ] } diff --git a/components/web_discovery/browser/content_scraper_browsertest.cc b/components/web_discovery/browser/content_scraper_browsertest.cc index 5d76e7a9ee77..2597918e8f9b 100644 --- a/components/web_discovery/browser/content_scraper_browsertest.cc +++ b/components/web_discovery/browser/content_scraper_browsertest.cc @@ -12,13 +12,18 @@ #include "base/files/file_util.h" #include "base/path_service.h" #include "base/test/bind.h" +#include "base/test/scoped_feature_list.h" #include "brave/components/constants/brave_paths.h" #include "brave/components/web_discovery/browser/patterns.h" #include "brave/components/web_discovery/browser/server_config_loader.h" #include "brave/components/web_discovery/common/features.h" #include "brave/components/web_discovery/common/web_discovery.mojom.h" -#include "chrome/test/base/ui_test_utils.h" +#include "chrome/test/base/chrome_test_utils.h" +#include "content/public/browser/web_contents.h" #include "content/public/test/browser_test.h" +#include "content/public/test/browser_test_base.h" +#include "content/public/test/browser_test_utils.h" +#include "content/public/test/content_mock_cert_verifier.h" #include "net/dns/mock_host_resolver.h" #include "services/service_manager/public/cpp/interface_provider.h" #include "testing/gtest/include/gtest/gtest.h" @@ -36,16 +41,18 @@ class WebDiscoveryContentScraperTest : public PlatformBrowserTest { public: WebDiscoveryContentScraperTest() : scoped_features_(features::kWebDiscoveryNative) {} + + // PlatformBrowserTest: void SetUpOnMainThread() override { - InProcessBrowserTest::SetUpOnMainThread(); + PlatformBrowserTest::SetUpOnMainThread(); base::FilePath data_path = base::PathService::CheckedGet(brave::DIR_TEST_DATA); data_path = data_path.AppendASCII("web_discovery"); host_resolver()->AddRule("*", "127.0.0.1"); - auto& test_server = embedded_https_test_server(); - test_server.ServeFilesFromDirectory(data_path); - ASSERT_TRUE(test_server.Start()); + test_server_.ServeFilesFromDirectory(data_path); + mock_cert_verifier_.mock_cert_verifier()->set_default_result(net::OK); + ASSERT_TRUE(test_server_.Start()); InitScraper(); run_loop_ = std::make_unique(); @@ -54,12 +61,29 @@ class WebDiscoveryContentScraperTest : public PlatformBrowserTest { &page_content_)); } + void SetUpCommandLine(base::CommandLine* command_line) override { + PlatformBrowserTest::SetUpCommandLine(command_line); + mock_cert_verifier_.SetUpCommandLine(command_line); + } + + void SetUpInProcessBrowserTestFixture() override { + PlatformBrowserTest::SetUpInProcessBrowserTestFixture(); + mock_cert_verifier_.SetUpInProcessBrowserTestFixture(); + } + + void TearDownInProcessBrowserTestFixture() override { + mock_cert_verifier_.TearDownInProcessBrowserTestFixture(); + PlatformBrowserTest::TearDownInProcessBrowserTestFixture(); + } + protected: mojo::Remote LoadTestPageAndGetExtractor() { mojo::Remote remote; - auto url = embedded_https_test_server().GetURL("example.com", "/page.html"); - auto* render_frame_host = ui_test_utils::NavigateToURL(browser(), url); + auto url = test_server_.GetURL("example.com", "/page.html"); + auto* contents = chrome_test_utils::GetActiveWebContents(this); + EXPECT_TRUE(content::NavigateToURL(contents, url)); + auto* render_frame_host = contents->GetPrimaryMainFrame(); if (render_frame_host) { render_frame_host->GetRemoteInterfaces()->GetInterface( @@ -148,6 +172,8 @@ class WebDiscoveryContentScraperTest : public PlatformBrowserTest { ®ex_util_); } + content::ContentMockCertVerifier mock_cert_verifier_; + net::EmbeddedTestServer test_server_{net::EmbeddedTestServer::TYPE_HTTPS}; base::test::ScopedFeatureList scoped_features_; RegexUtil regex_util_; std::unique_ptr server_config_loader_; diff --git a/test/BUILD.gn b/test/BUILD.gn index f095441d66f5..2290534c2db1 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -928,7 +928,6 @@ test("brave_browser_tests") { "//brave/components/speedreader/common/buildflags", "//brave/components/tor/buildflags", "//brave/components/url_sanitizer/browser", - "//brave/components/web_discovery/browser:browser_tests", "//brave/components/webcompat:core", "//brave/renderer/skus:browser_tests", "//brave/renderer/test:browser_tests",