Index: components/subresource_filter/content/browser/subresource_filter_safe_browsing_activation_throttle_unittest.cc |
diff --git a/components/subresource_filter/content/browser/subresource_filter_safe_browsing_activation_throttle_unittest.cc b/components/subresource_filter/content/browser/subresource_filter_safe_browsing_activation_throttle_unittest.cc |
index 3479cee5c95e8e55be56e03388fc59b8d5513b37..84fe67fb9f6eca2d1752d7a4e2f97c364872d3a8 100644 |
--- a/components/subresource_filter/content/browser/subresource_filter_safe_browsing_activation_throttle_unittest.cc |
+++ b/components/subresource_filter/content/browser/subresource_filter_safe_browsing_activation_throttle_unittest.cc |
@@ -9,12 +9,17 @@ |
#include "base/memory/ptr_util.h" |
#include "base/metrics/field_trial.h" |
#include "base/test/histogram_tester.h" |
+#include "base/threading/thread_task_runner_handle.h" |
#include "components/safe_browsing_db/test_database_manager.h" |
#include "components/subresource_filter/content/browser/content_subresource_filter_driver_factory.h" |
+#include "components/subresource_filter/content/browser/content_subresource_filter_throttle_manager.h" |
#include "components/subresource_filter/content/browser/fake_safe_browsing_database_manager.h" |
#include "components/subresource_filter/content/browser/subresource_filter_client.h" |
+#include "components/subresource_filter/content/browser/verified_ruleset_dealer.h" |
#include "components/subresource_filter/core/browser/subresource_filter_features.h" |
#include "components/subresource_filter/core/browser/subresource_filter_features_test_support.h" |
+#include "components/subresource_filter/core/common/activation_decision.h" |
+#include "components/subresource_filter/core/common/proto/rules.pb.h" |
#include "components/subresource_filter/core/common/test_ruleset_creator.h" |
#include "content/public/browser/browser_thread.h" |
#include "content/public/browser/navigation_handle.h" |
@@ -58,7 +63,8 @@ enum RedirectChainMatchPattern { |
class MockSubresourceFilterClient |
: public subresource_filter::SubresourceFilterClient { |
public: |
- MockSubresourceFilterClient() {} |
+ MockSubresourceFilterClient(VerifiedRulesetDealer::Handle* dealer_handle) |
+ : dealer_handle_(dealer_handle) {} |
~MockSubresourceFilterClient() override = default; |
@@ -66,35 +72,15 @@ class MockSubresourceFilterClient |
MOCK_METHOD1(ShouldSuppressActivation, bool(content::NavigationHandle*)); |
MOCK_METHOD1(WhitelistByContentSettings, void(const GURL&)); |
MOCK_METHOD1(WhitelistInCurrentWebContents, void(const GURL&)); |
- MOCK_METHOD0(GetRulesetDealer, VerifiedRulesetDealer::Handle*()); |
- private: |
- DISALLOW_COPY_AND_ASSIGN(MockSubresourceFilterClient); |
-}; |
- |
-// Throttle to call WillProcessResponse on the factory, which is otherwise |
-// called by the ThrottleManager. |
-class TestForwardingNavigationThrottle : public content::NavigationThrottle { |
- public: |
- TestForwardingNavigationThrottle(content::NavigationHandle* handle) |
- : content::NavigationThrottle(handle) {} |
- ~TestForwardingNavigationThrottle() override {} |
- |
- // content::NavigationThrottle: |
- content::NavigationThrottle::ThrottleCheckResult WillProcessResponse() |
- override { |
- content::WebContents* web_contents = navigation_handle()->GetWebContents(); |
- ContentSubresourceFilterDriverFactory* factory = |
- ContentSubresourceFilterDriverFactory::FromWebContents(web_contents); |
- factory->WillProcessResponse(navigation_handle()); |
- return content::NavigationThrottle::PROCEED; |
- } |
- const char* GetNameForLogging() override { |
- return "TestForwardingNavigationThrottle"; |
+ VerifiedRulesetDealer::Handle* GetRulesetDealer() override { |
+ return dealer_handle_; |
} |
private: |
- DISALLOW_COPY_AND_ASSIGN(TestForwardingNavigationThrottle); |
+ // Owned by the test harness. |
+ VerifiedRulesetDealer::Handle* dealer_handle_; |
+ DISALLOW_COPY_AND_ASSIGN(MockSubresourceFilterClient); |
}; |
} // namespace |
@@ -113,9 +99,31 @@ class SubresourceFilterSafeBrowsingActivationThrottleTest |
new testing::ScopedSubresourceFilterFeatureToggle( |
base::FeatureList::OVERRIDE_ENABLE_FEATURE, kActivationLevelEnabled, |
kActivationScopeActivationList, kActivationListSubresourceFilter)); |
+ |
+ // Initialize the ruleset dealer. This is needed because the throttle |
+ // manager requires a valid ruleset dealer to create throttles, which are |
+ // necessary for checking that the activation decision was plumbed through. |
+ testing::TestRulesetCreator test_ruleset_creator; |
+ testing::TestRulesetPair ruleset_pair; |
+ std::vector<proto::UrlRule> rules; |
+ ASSERT_NO_FATAL_FAILURE( |
+ test_ruleset_creator.CreateRulesetWithRules(rules, &ruleset_pair)); |
+ dealer_handle_ = base::MakeUnique<VerifiedRulesetDealer::Handle>( |
+ base::ThreadTaskRunnerHandle::Get()); |
+ dealer_handle_->SetRulesetFile( |
+ testing::TestRuleset::Open(ruleset_pair.indexed)); |
+ |
+ // Make the blocking task runner run on the current task runner for the |
+ // tests, to ensure that the NavigationSimulator properly runs all necessary |
+ // tasks while waiting for throttle checks to finish. |
+ dealer_handle_ = base::MakeUnique<VerifiedRulesetDealer::Handle>( |
+ base::ThreadTaskRunnerHandle::Get()); |
+ dealer_handle_->SetRulesetFile( |
+ testing::TestRuleset::Open(ruleset_pair.indexed)); |
// Note: Using NiceMock to allow uninteresting calls and suppress warnings. |
auto client = |
- base::MakeUnique<::testing::NiceMock<MockSubresourceFilterClient>>(); |
+ base::MakeUnique<::testing::NiceMock<MockSubresourceFilterClient>>( |
+ dealer_handle_.get()); |
ContentSubresourceFilterDriverFactory::CreateForWebContents( |
RenderViewHostTestHarness::web_contents(), std::move(client)); |
fake_safe_browsing_database_ = new FakeSafeBrowsingDatabaseManager(); |
@@ -123,9 +131,10 @@ class SubresourceFilterSafeBrowsingActivationThrottleTest |
Observe(RenderViewHostTestHarness::web_contents()); |
} |
- ContentSubresourceFilterDriverFactory* factory() { |
+ ContentSubresourceFilterThrottleManager* throttle_manager() { |
return ContentSubresourceFilterDriverFactory::FromWebContents( |
- RenderViewHostTestHarness::web_contents()); |
+ RenderViewHostTestHarness::web_contents()) |
+ ->throttle_manager(); |
} |
// content::WebContentsObserver: |
@@ -136,8 +145,12 @@ class SubresourceFilterSafeBrowsingActivationThrottleTest |
navigation_handle->RegisterThrottleForTesting( |
base::MakeUnique<SubresourceFilterSafeBrowsingActivationThrottle>( |
navigation_handle, fake_safe_browsing_database_)); |
- navigation_handle->RegisterThrottleForTesting( |
- base::MakeUnique<TestForwardingNavigationThrottle>(navigation_handle)); |
+ std::vector<std::unique_ptr<content::NavigationThrottle>> throttles; |
+ throttle_manager()->MaybeAppendNavigationThrottles(navigation_handle, |
+ &throttles); |
+ for (auto& it : throttles) { |
+ navigation_handle->RegisterThrottleForTesting(std::move(it)); |
+ } |
} |
void SimulateStartAndExpectProceed() { |
@@ -177,6 +190,7 @@ class SubresourceFilterSafeBrowsingActivationThrottleTest |
base::FieldTrialList field_trial_list_; |
std::unique_ptr<testing::ScopedSubresourceFilterFeatureToggle> |
scoped_feature_toggle_; |
+ std::unique_ptr<VerifiedRulesetDealer::Handle> dealer_handle_; |
std::unique_ptr<content::NavigationSimulator> navigation_simulator_; |
scoped_refptr<FakeSafeBrowsingDatabaseManager> fake_safe_browsing_database_; |
base::HistogramTester tester_; |
@@ -191,9 +205,9 @@ TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, |
CreateTestNavigationForMainFrame(url); |
SimulateStartAndExpectProceed(); |
SimulateCommitAndExpectProceed(); |
- EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: |
- ACTIVATION_LIST_NOT_MATCHED, |
- factory()->GetActivationDecisionForLastCommittedPageLoad()); |
+ EXPECT_EQ( |
+ ActivationDecision::ACTIVATION_LIST_NOT_MATCHED, |
+ throttle_manager()->GetActivationDecisionForLastCommittedPageLoad()); |
tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, |
0); |
tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0); |
@@ -207,8 +221,8 @@ TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, |
SimulateStartAndExpectProceed(); |
SimulateCommitAndExpectProceed(); |
EXPECT_EQ( |
- ContentSubresourceFilterDriverFactory::ActivationDecision::ACTIVATED, |
- factory()->GetActivationDecisionForLastCommittedPageLoad()); |
+ ActivationDecision::ACTIVATED, |
+ throttle_manager()->GetActivationDecisionForLastCommittedPageLoad()); |
tester().ExpectUniqueSample( |
kMatchesPatternHistogramNameSubresourceFilterSuffix, NO_REDIRECTS_HIT, 1); |
tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 1, |
@@ -222,9 +236,9 @@ TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, |
SimulateStartAndExpectProceed(); |
SimulateRedirectAndExpectProceed(GURL(kRedirectURL)); |
SimulateCommitAndExpectProceed(); |
- EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: |
- ACTIVATION_LIST_NOT_MATCHED, |
- factory()->GetActivationDecisionForLastCommittedPageLoad()); |
+ EXPECT_EQ( |
+ ActivationDecision::ACTIVATION_LIST_NOT_MATCHED, |
+ throttle_manager()->GetActivationDecisionForLastCommittedPageLoad()); |
tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, |
0); |
tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0); |
@@ -239,8 +253,8 @@ TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, |
SimulateRedirectAndExpectProceed(GURL(kRedirectURL)); |
SimulateCommitAndExpectProceed(); |
EXPECT_EQ( |
- ContentSubresourceFilterDriverFactory::ActivationDecision::ACTIVATED, |
- factory()->GetActivationDecisionForLastCommittedPageLoad()); |
+ ActivationDecision::ACTIVATED, |
+ throttle_manager()->GetActivationDecisionForLastCommittedPageLoad()); |
tester().ExpectUniqueSample( |
kMatchesPatternHistogramNameSubresourceFilterSuffix, F0M0L1, 1); |
tester().ExpectUniqueSample(kNavigationChainSizeSubresourceFilterSuffix, 2, |
@@ -254,9 +268,9 @@ TEST_F(SubresourceFilterSafeBrowsingActivationThrottleTest, |
CreateTestNavigationForMainFrame(url); |
SimulateStartAndExpectProceed(); |
SimulateCommitAndExpectProceed(); |
- EXPECT_EQ(ContentSubresourceFilterDriverFactory::ActivationDecision:: |
- ACTIVATION_LIST_NOT_MATCHED, |
- factory()->GetActivationDecisionForLastCommittedPageLoad()); |
+ EXPECT_EQ( |
+ ActivationDecision::ACTIVATION_LIST_NOT_MATCHED, |
+ throttle_manager()->GetActivationDecisionForLastCommittedPageLoad()); |
tester().ExpectTotalCount(kMatchesPatternHistogramNameSubresourceFilterSuffix, |
0); |
tester().ExpectTotalCount(kNavigationChainSizeSubresourceFilterSuffix, 0); |