[query-filter] Add query filter test helper (#36318)

This changes adds a new test helper to allow populating the query filter rules when the query filter component is enabled. This helps outside test clients that relied on hardcoded rules previously to test query stripping functionality. The hardcoded rules are going away once the `QueryFilterComponent` flag is enabled. 

This change updates the two existing test suites to support the change when we migrate to the new query filter component. Please see the larger [change](https://github.com/brave/brave-core/pull/36222) to see the full integration. 

Note that for now the test suites would *continue* to test the hardcoded rules despite this change as the actual query filtering is not yet supported. However, the plan is that once we do, these test suites would not need to be modified and they will automatically tests the new behaviour.

Resolves https://github.com/brave/brave-browser/issues/55418
This commit is contained in:
r0hit0303
2026-05-11 16:35:42 +01:00
committed by GitHub
parent e0f6e9015b
commit de712e73ee
6 changed files with 210 additions and 5 deletions
+3
View File
@@ -35,6 +35,8 @@ source_set("browser_tests") {
"//brave/components/brave_user_agent/common",
"//brave/components/geolocation",
"//brave/components/global_privacy_control",
"//brave/components/query_filter/browser/test_support",
"//brave/components/query_filter/common",
"//brave/components/static_redirect_helper",
"//brave/components/tor/buildflags",
"//chrome/browser",
@@ -94,6 +96,7 @@ source_set("unit_tests") {
"//brave/components/brave_user_agent/common",
"//brave/components/geolocation",
"//brave/components/l10n/common:test_support",
"//brave/components/query_filter/browser/test_support",
"//brave/components/query_filter/common",
"//brave/components/static_redirect_helper",
"//chrome/browser/prefs",
@@ -3,6 +3,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */
#include <optional>
#include <string_view>
#include "base/base64url.h"
@@ -11,6 +12,8 @@
#include "base/types/zip.h"
#include "brave/components/brave_shields/core/browser/brave_shields_utils.h"
#include "brave/components/constants/brave_paths.h"
#include "brave/components/query_filter/browser/test_support/query_filter_test_helper.h"
#include "brave/components/query_filter/common/features.h"
#include "brave/components/tor/buildflags/buildflags.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/profiles/profile.h"
@@ -36,7 +39,10 @@
class BraveSiteHacksNetworkDelegateBrowserTest : public InProcessBrowserTest {
public:
BraveSiteHacksNetworkDelegateBrowserTest()
: https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {}
: https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {
scoped_feature_list_.InitAndEnableFeature(
query_filter::features::kQueryFilterComponent);
}
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
@@ -51,9 +57,10 @@ class BraveSiteHacksNetworkDelegateBrowserTest : public InProcessBrowserTest {
https_server_.RegisterRequestMonitor(base::BindRepeating(
&BraveSiteHacksNetworkDelegateBrowserTest::HandleRequest,
base::Unretained(this)));
ASSERT_TRUE(https_server_.Start());
testing_filter_rules_.emplace();
simple_landing_url_ = https_server_.GetURL("a.com", "/simple.html");
redirect_to_cross_site_landing_url_ =
https_server_.GetURL("redir.b.com", "/cross-site/a.com/simple.html");
@@ -123,6 +130,7 @@ class BraveSiteHacksNetworkDelegateBrowserTest : public InProcessBrowserTest {
void TearDownInProcessBrowserTestFixture() override {
mock_cert_verifier_.TearDownInProcessBrowserTestFixture();
InProcessBrowserTest::TearDownInProcessBrowserTestFixture();
testing_filter_rules_.reset();
}
GURL url(const GURL& destination_url, const GURL& navigation_url) {
@@ -243,6 +251,9 @@ class BraveSiteHacksNetworkDelegateBrowserTest : public InProcessBrowserTest {
base::FilePath test_data_dir_;
content::ContentMockCertVerifier mock_cert_verifier_;
net::test_server::EmbeddedTestServer https_server_;
base::test::ScopedFeatureList scoped_feature_list_;
std::optional<query_filter::test::ScopedTestingQueryFilterRules>
testing_filter_rules_;
};
IN_PROC_BROWSER_TEST_F(BraveSiteHacksNetworkDelegateBrowserTest,
@@ -5,7 +5,7 @@
#include "brave/browser/net/brave_site_hacks_network_delegate_helper.h"
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
@@ -14,6 +14,8 @@
#include "brave/browser/net/features.h"
#include "brave/browser/net/url_context.h"
#include "brave/components/constants/network_constants.h"
#include "brave/components/query_filter/browser/test_support/query_filter_test_helper.h"
#include "brave/components/query_filter/common/features.h"
#include "brave/components/query_filter/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
@@ -54,10 +56,14 @@ class BraveSiteHacksNetworkDelegateHelperTest : public testing::Test {
bool enable_flag = std::is_same_v<
typename PtrStrategy::template Ptr<brave::BraveRequestInfo>,
base::WeakPtr<brave::BraveRequestInfo>>;
scoped_feature_list_.InitWithFeatureState(
features::kBraveRequestInfoUniquePtr, enable_flag);
scoped_feature_list_.InitWithFeatureStates(
{{features::kBraveRequestInfoUniquePtr, enable_flag},
{query_filter::features::kQueryFilterComponent, true}});
testing_filter_rules_.emplace();
}
void TearDown() override { testing_filter_rules_.reset(); }
sync_preferences::TestingPrefServiceSyncable* GetPrefs() {
return profile_->GetTestingPrefService();
}
@@ -82,6 +88,8 @@ class BraveSiteHacksNetworkDelegateHelperTest : public testing::Test {
std::unique_ptr<TestingProfile> profile_;
std::unique_ptr<brave::BraveRequestInfo> owned_request_;
base::test::ScopedFeatureList scoped_feature_list_;
std::optional<query_filter::test::ScopedTestingQueryFilterRules>
testing_filter_rules_;
};
using PtrStrategies = testing::Types<SharedPtrStrategy, WeakPtrStrategy>;
@@ -0,0 +1,14 @@
# Copyright (c) 2026 The Brave Authors. All rights reserved.
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at https://mozilla.org/MPL/2.0/.
source_set("test_support") {
testonly = true
sources = [
"query_filter_test_helper.cc",
"query_filter_test_helper.h",
]
deps = [ "//brave/components/query_filter/browser" ]
}
@@ -0,0 +1,139 @@
// Copyright (c) 2026 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
#include "brave/components/query_filter/browser/test_support/query_filter_test_helper.h"
#include "brave/components/query_filter/browser/query_filter_data.h"
namespace {
// Sample query filter JSON which would be written to a file during setup.
constexpr char kDefaultQueryFilterRules[] = R"json(
[
{
"include": [
"*://*/*"
],
"exclude": [],
"params": [
"__hsfp",
"__hssc",
"__hstc",
"__s",
"_bhlid",
"_branch_match_id",
"_branch_referrer",
"_gl",
"_hsenc",
"_openstat",
"at_recipient_id",
"at_recipient_list",
"bbeml",
"bsft_clkid",
"bsft_uid",
"dclid",
"et_rid",
"fb_action_ids",
"fb_comment_id",
"fbclid",
"gclid",
"guce_referrer",
"guce_referrer_sig",
"hsCtaTracking",
"irclickid",
"mc_eid",
"ml_subscriber",
"ml_subscriber_hash",
"msclkid",
"mtm_cid",
"oft_c",
"oft_ck",
"oft_d",
"oft_id",
"oft_ids",
"oft_k",
"oft_lk",
"oft_sk",
"oly_anon_id",
"oly_enc_id",
"pk_cid",
"rb_clickid",
"s_cid",
"sc_customer",
"sc_eh",
"sc_uid",
"sfmc_activityid",
"sfmc_id",
"sms_click",
"sms_source",
"sms_uph",
"srsltid",
"ss_email_id",
"syclid",
"ttclid",
"twclid",
"unicorn_click_id",
"vero_conv",
"vero_id",
"vgo_ee",
"wbraid",
"wickedid",
"yclid",
"ymclid",
"ysclid"
]
},
{
"include": [
"*://*.instagram.com/*",
"*://instagram.com/*"
],
"exclude": [],
"params": [
"igsh",
"igshid"
]
},
{
"include": [
"*://*.twitter.com/*",
"*://twitter.com/*",
"*://*.x.com/*",
"*://x.com/*"
],
"exclude": [],
"params": [
"ref_src",
"ref_url"
]
},
{
"include": [
"*://*.youtube.com/*",
"*://youtube.com/*",
"*://youtu.be/*"
],
"exclude": [],
"params": [
"si"
]
}
])json";
} // namespace
namespace query_filter {
namespace test {
ScopedTestingQueryFilterRules::ScopedTestingQueryFilterRules() {
QueryFilterData::GetInstance()->PopulateDataFromComponent(
kDefaultQueryFilterRules);
}
ScopedTestingQueryFilterRules::~ScopedTestingQueryFilterRules() {
QueryFilterData::GetInstance()->ResetRulesForTesting();
}
} // namespace test
} // namespace query_filter
@@ -0,0 +1,30 @@
// Copyright (c) 2026 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
#ifndef BRAVE_COMPONENTS_QUERY_FILTER_BROWSER_TEST_SUPPORT_QUERY_FILTER_TEST_HELPER_H_
#define BRAVE_COMPONENTS_QUERY_FILTER_BROWSER_TEST_SUPPORT_QUERY_FILTER_TEST_HELPER_H_
namespace query_filter {
namespace test {
// Populates the query filter component with default set of rules
// and clears all rules from the query filter component upon
// destruction. Must only be called with kQueryFilterComponent enabled;
// otherwise will crash the test.
class ScopedTestingQueryFilterRules {
public:
ScopedTestingQueryFilterRules();
~ScopedTestingQueryFilterRules();
ScopedTestingQueryFilterRules(const ScopedTestingQueryFilterRules&) = delete;
ScopedTestingQueryFilterRules& operator=(
const ScopedTestingQueryFilterRules&) = delete;
};
} // namespace test
} // namespace query_filter
#endif // BRAVE_COMPONENTS_QUERY_FILTER_BROWSER_TEST_SUPPORT_QUERY_FILTER_TEST_HELPER_H_