Commit 177ca193 authored by Karandeep Bhatia's avatar Karandeep Bhatia Committed by Commit Bot

DNR: Remove references to IO thread in browser tests.

Remove reference to IO thread in URLRequestMonitor and
RulesetCountWaiter. RulesetManager now lives on the UI thread, hence a
lock shouldn't be necessary. Use a SequenceChecker to assert the same.
Also delete ScopedRulesetManagerTestObserver.

BUG=754526

Change-Id: I540207ee0eeeab73dc6b9b9e98e42e050fe920e2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2128927Reviewed-by: default avatarKelvin Jiang <kelvinjiang@chromium.org>
Commit-Queue: Karan Bhatia <karandeepb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#755216}
parent 9380988c
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "base/path_service.h" #include "base/path_service.h"
#include "base/rand_util.h" #include "base/rand_util.h"
#include "base/run_loop.h" #include "base/run_loop.h"
#include "base/sequence_checker.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
...@@ -132,11 +133,14 @@ bool WasFrameWithScriptLoaded(content::RenderFrameHost* rfh) { ...@@ -132,11 +133,14 @@ bool WasFrameWithScriptLoaded(content::RenderFrameHost* rfh) {
// Used to monitor requests that reach the RulesetManager. // Used to monitor requests that reach the RulesetManager.
class URLRequestMonitor : public RulesetManager::TestObserver { class URLRequestMonitor : public RulesetManager::TestObserver {
public: public:
explicit URLRequestMonitor(GURL url) : url_(std::move(url)) {} explicit URLRequestMonitor(RulesetManager* manager, GURL url)
: manager_(manager), url_(std::move(url)) {
manager_->SetObserverForTest(this);
}
~URLRequestMonitor() override { manager_->SetObserverForTest(nullptr); }
// This is called from both the UI and IO thread. Clients should ensure
// there's no race.
bool GetAndResetRequestSeen(bool new_val) { bool GetAndResetRequestSeen(bool new_val) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
bool return_val = request_seen_; bool return_val = request_seen_;
request_seen_ = new_val; request_seen_ = new_val;
return return_val; return return_val;
...@@ -146,12 +150,15 @@ class URLRequestMonitor : public RulesetManager::TestObserver { ...@@ -146,12 +150,15 @@ class URLRequestMonitor : public RulesetManager::TestObserver {
// RulesetManager::TestObserver implementation. // RulesetManager::TestObserver implementation.
void OnEvaluateRequest(const WebRequestInfo& request, void OnEvaluateRequest(const WebRequestInfo& request,
bool is_incognito_context) override { bool is_incognito_context) override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (request.url == url_) if (request.url == url_)
GetAndResetRequestSeen(true); GetAndResetRequestSeen(true);
} }
RulesetManager* const manager_;
GURL url_; GURL url_;
bool request_seen_ = false; bool request_seen_ = false;
SEQUENCE_CHECKER(sequence_checker_);
DISALLOW_COPY_AND_ASSIGN(URLRequestMonitor); DISALLOW_COPY_AND_ASSIGN(URLRequestMonitor);
}; };
...@@ -160,76 +167,46 @@ class URLRequestMonitor : public RulesetManager::TestObserver { ...@@ -160,76 +167,46 @@ class URLRequestMonitor : public RulesetManager::TestObserver {
// a certain count. // a certain count.
class RulesetCountWaiter : public RulesetManager::TestObserver { class RulesetCountWaiter : public RulesetManager::TestObserver {
public: public:
RulesetCountWaiter() = default; explicit RulesetCountWaiter(RulesetManager* manager)
: manager_(manager), current_count_(manager_->GetMatcherCountForTest()) {
manager_->SetObserverForTest(this);
}
~RulesetCountWaiter() override { manager_->SetObserverForTest(nullptr); }
void WaitForRulesetCount(size_t count) { void WaitForRulesetCount(size_t count) {
{ DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::AutoLock lock(lock_);
ASSERT_FALSE(expected_count_); ASSERT_FALSE(expected_count_);
if (current_count_ == count) if (current_count_ == count)
return; return;
expected_count_ = count; expected_count_ = count;
run_loop_ = std::make_unique<base::RunLoop>(); run_loop_ = std::make_unique<base::RunLoop>();
}
run_loop_->Run(); run_loop_->Run();
} }
private: private:
// RulesetManager::TestObserver implementation. // RulesetManager::TestObserver implementation.
void OnRulesetCountChanged(size_t count) override { void OnRulesetCountChanged(size_t count) override {
base::AutoLock lock(lock_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
current_count_ = count; current_count_ = count;
if (expected_count_ != count) if (expected_count_ != count)
return; return;
ASSERT_TRUE(run_loop_.get()); ASSERT_TRUE(run_loop_.get());
// The run-loop has either started or a task on the UI thread to start it is
// underway. RunLoop::Quit is thread-safe and should post a task to the UI
// thread to quit the run-loop.
run_loop_->Quit(); run_loop_->Quit();
expected_count_.reset(); expected_count_.reset();
} }
// Accessed on both the UI and IO threads. Access is synchronized using RulesetManager* const manager_;
// |lock_|.
size_t current_count_ = 0; size_t current_count_ = 0;
base::Optional<size_t> expected_count_; base::Optional<size_t> expected_count_;
std::unique_ptr<base::RunLoop> run_loop_; std::unique_ptr<base::RunLoop> run_loop_;
SEQUENCE_CHECKER(sequence_checker_);
base::Lock lock_;
DISALLOW_COPY_AND_ASSIGN(RulesetCountWaiter); DISALLOW_COPY_AND_ASSIGN(RulesetCountWaiter);
}; };
// Helper to set (and reset on destruction) the given
// RulesetManager::TestObserver on the IO thread. Lifetime of |observer| should
// be managed by clients.
class ScopedRulesetManagerTestObserver {
public:
ScopedRulesetManagerTestObserver(RulesetManager::TestObserver* observer,
content::BrowserContext* browser_context)
: browser_context_(browser_context) {
SetRulesetManagerTestObserver(observer);
}
~ScopedRulesetManagerTestObserver() {
SetRulesetManagerTestObserver(nullptr);
}
private:
void SetRulesetManagerTestObserver(RulesetManager::TestObserver* observer) {
declarative_net_request::RulesMonitorService::Get(browser_context_)
->ruleset_manager()
->SetObserverForTest(observer);
}
content::BrowserContext* browser_context_;
DISALLOW_COPY_AND_ASSIGN(ScopedRulesetManagerTestObserver);
};
// Helper to wait for warnings thrown for a given extension. // Helper to wait for warnings thrown for a given extension.
class WarningServiceObserver : public WarningService::Observer { class WarningServiceObserver : public WarningService::Observer {
public: public:
...@@ -331,6 +308,10 @@ class DeclarativeNetRequestBrowserTest ...@@ -331,6 +308,10 @@ class DeclarativeNetRequestBrowserTest
->GetPageType(); ->GetPageType();
} }
RulesetManager* ruleset_manager() {
return RulesMonitorService::Get(profile())->ruleset_manager();
}
content::PageType GetPageType() const { return GetPageType(browser()); } content::PageType GetPageType() const { return GetPageType(browser()); }
std::string GetPageBody() const { std::string GetPageBody() const {
...@@ -1621,8 +1602,8 @@ IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest, RendererCacheCleared) { ...@@ -1621,8 +1602,8 @@ IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest, RendererCacheCleared) {
// Set-up an observer for RulesetMatcher to monitor requests to // Set-up an observer for RulesetMatcher to monitor requests to
// script.js. // script.js.
URLRequestMonitor script_monitor( URLRequestMonitor script_monitor(
ruleset_manager(),
embedded_test_server()->GetURL("example.com", "/cached/script.js")); embedded_test_server()->GetURL("example.com", "/cached/script.js"));
ScopedRulesetManagerTestObserver scoped_observer(&script_monitor, profile());
GURL url = embedded_test_server()->GetURL( GURL url = embedded_test_server()->GetURL(
"example.com", "/cached/page_with_cacheable_script.html"); "example.com", "/cached/page_with_cacheable_script.html");
...@@ -1849,9 +1830,7 @@ IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest_Packed, ...@@ -1849,9 +1830,7 @@ IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest_Packed,
CorruptedIndexedRuleset) { CorruptedIndexedRuleset) {
// Set-up an observer for RulesetMatcher to monitor the number of extension // Set-up an observer for RulesetMatcher to monitor the number of extension
// rulesets. // rulesets.
RulesetCountWaiter ruleset_count_waiter; RulesetCountWaiter ruleset_count_waiter(ruleset_manager());
ScopedRulesetManagerTestObserver scoped_observer(&ruleset_count_waiter,
profile());
set_config_flags(ConfigFlag::kConfig_HasBackgroundScript); set_config_flags(ConfigFlag::kConfig_HasBackgroundScript);
...@@ -2056,9 +2035,7 @@ IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest_Packed, ...@@ -2056,9 +2035,7 @@ IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest_Packed,
// Set up an observer for RulesetMatcher to monitor the number of extension // Set up an observer for RulesetMatcher to monitor the number of extension
// rulesets. // rulesets.
RulesetCountWaiter ruleset_count_waiter; RulesetCountWaiter ruleset_count_waiter(ruleset_manager());
ScopedRulesetManagerTestObserver scoped_observer(&ruleset_count_waiter,
profile());
TestRule rule = CreateGenericRule(); TestRule rule = CreateGenericRule();
rule.condition->url_filter = std::string("*"); rule.condition->url_filter = std::string("*");
...@@ -2387,9 +2364,7 @@ IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest, ...@@ -2387,9 +2364,7 @@ IN_PROC_BROWSER_TEST_P(DeclarativeNetRequestBrowserTest,
// Set up an observer for RulesetMatcher to monitor the number of extension // Set up an observer for RulesetMatcher to monitor the number of extension
// rulesets. // rulesets.
RulesetCountWaiter ruleset_count_waiter; RulesetCountWaiter ruleset_count_waiter(ruleset_manager());
ScopedRulesetManagerTestObserver scoped_observer(&ruleset_count_waiter,
profile());
EXPECT_FALSE( EXPECT_FALSE(
ExtensionWebRequestEventRouter::GetInstance()->HasAnyExtraHeadersListener( ExtensionWebRequestEventRouter::GetInstance()->HasAnyExtraHeadersListener(
......
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