Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Unified Diff: content/test/navigation_simulator_unittest.cc

Issue 2834543003: [subresource_filter] SB throttle can send multiple speculative requests. (Closed)
Patch Set: rebase on #468982 Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/test/BUILD.gn ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/test/navigation_simulator_unittest.cc
diff --git a/content/test/navigation_simulator_unittest.cc b/content/test/navigation_simulator_unittest.cc
index 55a20c3b81acb1ed8f328a88861578b68a04ea88..56b602cf401009609b7bcc7142892adb5f6e963a 100644
--- a/content/test/navigation_simulator_unittest.cc
+++ b/content/test/navigation_simulator_unittest.cc
@@ -15,6 +15,7 @@
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/navigation_throttle.h"
#include "content/public/browser/web_contents_observer.h"
+#include "content/public/test/cancelling_navigation_throttle.h"
#include "content/test/test_render_frame_host.h"
#include "content/test/test_render_view_host.h"
#include "content/test/test_web_contents.h"
@@ -23,97 +24,12 @@
namespace content {
-enum CancelTime {
- WILL_SEND_REQUEST,
- WILL_REDIRECT_REQUEST,
- WILL_PROCESS_RESPONSE,
- NEVER,
-};
-
-enum ResultSynchrony {
- SYNCHRONOUS,
- ASYNCHRONOUS,
-};
-
-std::string CancelTimeToString(CancelTime cancel_time) {
- switch (cancel_time) {
- case WILL_SEND_REQUEST:
- return "WILL_SEND_REQUEST";
- case WILL_REDIRECT_REQUEST:
- return "WILL_REDIRECT_REQUEST";
- case WILL_PROCESS_RESPONSE:
- return "WILL_PROCESS_RESPONSE";
- case NEVER:
- return "NEVER";
- }
- NOTREACHED();
- return "";
-}
-
-std::string ResultSynchronyToString(ResultSynchrony sync) {
- return sync == SYNCHRONOUS ? "SYNCHRONOUS" : "ASYNCHRONOUS";
-}
-
-// TODO(csharrison): Expose this class in the content public API.
-class CancellingNavigationThrottle : public NavigationThrottle {
- public:
- CancellingNavigationThrottle(NavigationHandle* handle,
- CancelTime cancel_time,
- ResultSynchrony sync)
- : NavigationThrottle(handle),
- cancel_time_(cancel_time),
- sync_(sync),
- weak_ptr_factory_(this) {}
-
- ~CancellingNavigationThrottle() override {}
-
- NavigationThrottle::ThrottleCheckResult WillStartRequest() override {
- return ProcessState(cancel_time_ == WILL_SEND_REQUEST);
- }
-
- NavigationThrottle::ThrottleCheckResult WillRedirectRequest() override {
- return ProcessState(cancel_time_ == WILL_REDIRECT_REQUEST);
- }
-
- NavigationThrottle::ThrottleCheckResult WillProcessResponse() override {
- return ProcessState(cancel_time_ == WILL_PROCESS_RESPONSE);
- }
-
- const char* GetNameForLogging() override {
- return "CancellingNavigationThrottle";
- }
-
- NavigationThrottle::ThrottleCheckResult ProcessState(bool should_cancel) {
- if (sync_ == ASYNCHRONOUS) {
- BrowserThread::PostTask(
- BrowserThread::UI, FROM_HERE,
- base::Bind(&CancellingNavigationThrottle::MaybeCancel,
- weak_ptr_factory_.GetWeakPtr(), should_cancel));
- return NavigationThrottle::DEFER;
- }
- return should_cancel ? NavigationThrottle::CANCEL
- : NavigationThrottle::PROCEED;
- }
-
- void MaybeCancel(bool cancel) {
- if (cancel)
- navigation_handle()->CancelDeferredNavigation(NavigationThrottle::CANCEL);
- else
- navigation_handle()->Resume();
- }
-
- private:
- const CancelTime cancel_time_;
- const ResultSynchrony sync_;
- base::WeakPtrFactory<CancellingNavigationThrottle> weak_ptr_factory_;
-
- DISALLOW_COPY_AND_ASSIGN(CancellingNavigationThrottle);
-};
-
-class NavigationSimulatorTest : public RenderViewHostImplTestHarness,
- public WebContentsObserver,
- public testing::WithParamInterface<
- std::tuple<CancelTime, ResultSynchrony>> {
+class NavigationSimulatorTest
+ : public RenderViewHostImplTestHarness,
+ public WebContentsObserver,
+ public testing::WithParamInterface<
+ std::tuple<CancellingNavigationThrottle::CancelTime,
+ CancellingNavigationThrottle::ResultSynchrony>> {
public:
NavigationSimulatorTest() {}
~NavigationSimulatorTest() override {}
@@ -142,8 +58,8 @@ class NavigationSimulatorTest : public RenderViewHostImplTestHarness,
did_finish_navigation_ = true;
}
- CancelTime cancel_time_;
- ResultSynchrony sync_;
+ CancellingNavigationThrottle::CancelTime cancel_time_;
+ CancellingNavigationThrottle::ResultSynchrony sync_;
std::unique_ptr<NavigationSimulator> simulator_;
bool did_finish_navigation_ = false;
@@ -155,11 +71,10 @@ class NavigationSimulatorTest : public RenderViewHostImplTestHarness,
// the navigation at various points in the flow, both synchronously and
// asynchronously.
TEST_P(NavigationSimulatorTest, Cancel) {
- SCOPED_TRACE(::testing::Message()
- << "CancelTime: " << CancelTimeToString(cancel_time_)
- << " ResultSynchrony: " << ResultSynchronyToString(sync_));
+ SCOPED_TRACE(::testing::Message() << "CancelTime: " << cancel_time_
+ << " ResultSynchrony: " << sync_);
simulator_->Start();
- if (cancel_time_ == WILL_SEND_REQUEST) {
+ if (cancel_time_ == CancellingNavigationThrottle::WILL_START_REQUEST) {
EXPECT_EQ(NavigationThrottle::CANCEL,
simulator_->GetLastThrottleCheckResult());
return;
@@ -167,7 +82,7 @@ TEST_P(NavigationSimulatorTest, Cancel) {
EXPECT_EQ(NavigationThrottle::PROCEED,
simulator_->GetLastThrottleCheckResult());
simulator_->Redirect(GURL("https://example.redirect"));
- if (cancel_time_ == WILL_REDIRECT_REQUEST) {
+ if (cancel_time_ == CancellingNavigationThrottle::WILL_REDIRECT_REQUEST) {
EXPECT_EQ(NavigationThrottle::CANCEL,
simulator_->GetLastThrottleCheckResult());
return;
@@ -175,7 +90,7 @@ TEST_P(NavigationSimulatorTest, Cancel) {
EXPECT_EQ(NavigationThrottle::PROCEED,
simulator_->GetLastThrottleCheckResult());
simulator_->Commit();
- if (cancel_time_ == WILL_PROCESS_RESPONSE) {
+ if (cancel_time_ == CancellingNavigationThrottle::WILL_PROCESS_RESPONSE) {
EXPECT_EQ(NavigationThrottle::CANCEL,
simulator_->GetLastThrottleCheckResult());
return;
@@ -187,13 +102,12 @@ TEST_P(NavigationSimulatorTest, Cancel) {
INSTANTIATE_TEST_CASE_P(
CancelMethod,
NavigationSimulatorTest,
- ::testing::Values(std::make_tuple(WILL_SEND_REQUEST, SYNCHRONOUS),
- std::make_tuple(WILL_SEND_REQUEST, ASYNCHRONOUS),
- std::make_tuple(WILL_REDIRECT_REQUEST, SYNCHRONOUS),
- std::make_tuple(WILL_REDIRECT_REQUEST, ASYNCHRONOUS),
- std::make_tuple(WILL_PROCESS_RESPONSE, SYNCHRONOUS),
- std::make_tuple(WILL_PROCESS_RESPONSE, ASYNCHRONOUS),
- std::make_tuple(NEVER, SYNCHRONOUS),
- std::make_tuple(NEVER, ASYNCHRONOUS)));
+ ::testing::Combine(
+ ::testing::Values(CancellingNavigationThrottle::WILL_START_REQUEST,
+ CancellingNavigationThrottle::WILL_REDIRECT_REQUEST,
+ CancellingNavigationThrottle::WILL_PROCESS_RESPONSE,
+ CancellingNavigationThrottle::NEVER),
+ ::testing::Values(CancellingNavigationThrottle::SYNCHRONOUS,
+ CancellingNavigationThrottle::ASYNCHRONOUS)));
} // namespace content
« no previous file with comments | « content/test/BUILD.gn ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698