Index: headless/lib/headless_browser_browsertest.cc |
diff --git a/headless/lib/headless_browser_browsertest.cc b/headless/lib/headless_browser_browsertest.cc |
index 231920fff4de0417bc7e4752fc745dd8270b0fcf..a6bbc8bf52db9030098ca541eaf92b5428ad485b 100644 |
--- a/headless/lib/headless_browser_browsertest.cc |
+++ b/headless/lib/headless_browser_browsertest.cc |
@@ -8,95 +8,17 @@ |
#include "base/strings/stringprintf.h" |
#include "content/public/test/browser_test.h" |
#include "headless/public/domains/page.h" |
-#include "headless/public/domains/runtime.h" |
-#include "headless/public/domains/types.h" |
#include "headless/public/headless_browser.h" |
-#include "headless/public/headless_browser_context.h" |
#include "headless/public/headless_devtools_client.h" |
#include "headless/public/headless_devtools_target.h" |
#include "headless/public/headless_web_contents.h" |
#include "headless/test/headless_browser_test.h" |
-#include "net/base/io_buffer.h" |
-#include "net/http/http_response_headers.h" |
+#include "headless/test/test_protocol_handler.h" |
#include "net/test/spawned_test_server/spawned_test_server.h" |
-#include "net/url_request/url_request_job.h" |
#include "testing/gtest/include/gtest/gtest.h" |
#include "ui/gfx/geometry/size.h" |
namespace headless { |
-namespace { |
- |
-class TestURLRequestJob : public net::URLRequestJob { |
- public: |
- TestURLRequestJob(net::URLRequest* request, |
- net::NetworkDelegate* network_delegate, |
- const std::string& body); |
- ~TestURLRequestJob() override {} |
- |
- // net::URLRequestJob implementation: |
- void Start() override; |
- void GetResponseInfo(net::HttpResponseInfo* info) override; |
- int ReadRawData(net::IOBuffer* buf, int buf_size) override; |
- |
- private: |
- scoped_refptr<net::StringIOBuffer> body_; |
- scoped_refptr<net::DrainableIOBuffer> src_buf_; |
- |
- DISALLOW_COPY_AND_ASSIGN(TestURLRequestJob); |
-}; |
- |
-TestURLRequestJob::TestURLRequestJob(net::URLRequest* request, |
- net::NetworkDelegate* network_delegate, |
- const std::string& body) |
- : net::URLRequestJob(request, network_delegate), |
- body_(new net::StringIOBuffer(body)), |
- src_buf_(new net::DrainableIOBuffer(body_.get(), body_->size())) {} |
- |
-void TestURLRequestJob::Start() { |
- NotifyHeadersComplete(); |
-} |
- |
-void TestURLRequestJob::GetResponseInfo(net::HttpResponseInfo* info) { |
- info->headers = |
- new net::HttpResponseHeaders("Content-Type: text/html\r\n\r\n"); |
-} |
- |
-int TestURLRequestJob::ReadRawData(net::IOBuffer* buf, int buf_size) { |
- scoped_refptr<net::DrainableIOBuffer> dest_buf( |
- new net::DrainableIOBuffer(buf, buf_size)); |
- while (src_buf_->BytesRemaining() > 0 && dest_buf->BytesRemaining() > 0) { |
- *dest_buf->data() = *src_buf_->data(); |
- src_buf_->DidConsume(1); |
- dest_buf->DidConsume(1); |
- } |
- return dest_buf->BytesConsumed(); |
-} |
- |
-class TestProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler { |
- public: |
- TestProtocolHandler(const std::string& body); |
- ~TestProtocolHandler() override {} |
- |
- net::URLRequestJob* MaybeCreateJob( |
- net::URLRequest* request, |
- net::NetworkDelegate* network_delegate) const override; |
- |
- private: |
- std::string body_; |
- |
- DISALLOW_COPY_AND_ASSIGN(TestProtocolHandler); |
-}; |
- |
-TestProtocolHandler::TestProtocolHandler(const std::string& body) |
- : body_(body) {} |
- |
-net::URLRequestJob* TestProtocolHandler::MaybeCreateJob( |
- net::URLRequest* request, |
- net::NetworkDelegate* network_delegate) const { |
- return new TestURLRequestJob(request, network_delegate, body_); |
-} |
- |
-} // namespace |
IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, CreateAndDestroyWebContents) { |
HeadlessWebContents* web_contents = |
@@ -237,137 +159,4 @@ IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, HttpsProtocolHandler) { |
EXPECT_EQ(kResponseBody, inner_html); |
} |
-namespace { |
-const char kMainPageCookie[] = "mood=quizzical"; |
-const char kIsolatedPageCookie[] = "mood=quixotic"; |
-} // namespace |
- |
-// This test creates two tabs pointing to the same security origin in two |
-// different browser contexts and checks that they are isolated by creating two |
-// cookies with the same name in both tabs. The steps are: |
-// |
-// 1. Wait for tab #1 to become ready for DevTools. |
-// 2. Create tab #2 and wait for it to become ready for DevTools. |
-// 3. Navigate tab #1 to the test page and wait for it to finish loading. |
-// 4. Navigate tab #2 to the test page and wait for it to finish loading. |
-// 5. Set a cookie in tab #1. |
-// 6. Set the same cookie in tab #2 to a different value. |
-// 7. Read the cookie in tab #1 and check that it has the first value. |
-// 8. Read the cookie in tab #2 and check that it has the second value. |
-// |
-// If the tabs aren't properly isolated, step 7 will fail. |
-class HeadlessBrowserContextIsolationTest |
- : public HeadlessAsyncDevTooledBrowserTest { |
- public: |
- HeadlessBrowserContextIsolationTest() |
- : web_contents2_(nullptr), |
- devtools_client2_(HeadlessDevToolsClient::Create()) { |
- EXPECT_TRUE(embedded_test_server()->Start()); |
- } |
- |
- // HeadlessWebContentsObserver implementation: |
- void DevToolsTargetReady() override { |
- if (!web_contents2_) { |
- browser_context_ = browser()->CreateBrowserContextBuilder().Build(); |
- web_contents2_ = browser() |
- ->CreateWebContentsBuilder() |
- .SetBrowserContext(browser_context_.get()) |
- .Build(); |
- web_contents2_->AddObserver(this); |
- return; |
- } |
- |
- web_contents2_->GetDevToolsTarget()->AttachClient(devtools_client2_.get()); |
- HeadlessAsyncDevTooledBrowserTest::DevToolsTargetReady(); |
- } |
- |
- void RunDevTooledTest() override { |
- load_observer_.reset(new LoadObserver( |
- devtools_client_.get(), |
- base::Bind(&HeadlessBrowserContextIsolationTest::OnFirstLoadComplete, |
- base::Unretained(this)))); |
- devtools_client_->GetPage()->Navigate( |
- embedded_test_server()->GetURL("/hello.html").spec()); |
- } |
- |
- void OnFirstLoadComplete() { |
- EXPECT_TRUE(load_observer_->navigation_succeeded()); |
- load_observer_.reset(new LoadObserver( |
- devtools_client2_.get(), |
- base::Bind(&HeadlessBrowserContextIsolationTest::OnSecondLoadComplete, |
- base::Unretained(this)))); |
- devtools_client2_->GetPage()->Navigate( |
- embedded_test_server()->GetURL("/hello.html").spec()); |
- } |
- |
- void OnSecondLoadComplete() { |
- EXPECT_TRUE(load_observer_->navigation_succeeded()); |
- load_observer_.reset(); |
- |
- devtools_client_->GetRuntime()->Evaluate( |
- base::StringPrintf("document.cookie = '%s'", kMainPageCookie), |
- base::Bind(&HeadlessBrowserContextIsolationTest::OnFirstSetCookieResult, |
- base::Unretained(this))); |
- } |
- |
- void OnFirstSetCookieResult(std::unique_ptr<runtime::EvaluateResult> result) { |
- std::string cookie; |
- EXPECT_TRUE(result->GetResult()->GetValue()->GetAsString(&cookie)); |
- EXPECT_EQ(kMainPageCookie, cookie); |
- |
- devtools_client2_->GetRuntime()->Evaluate( |
- base::StringPrintf("document.cookie = '%s'", kIsolatedPageCookie), |
- base::Bind( |
- &HeadlessBrowserContextIsolationTest::OnSecondSetCookieResult, |
- base::Unretained(this))); |
- } |
- |
- void OnSecondSetCookieResult( |
- std::unique_ptr<runtime::EvaluateResult> result) { |
- std::string cookie; |
- EXPECT_TRUE(result->GetResult()->GetValue()->GetAsString(&cookie)); |
- EXPECT_EQ(kIsolatedPageCookie, cookie); |
- |
- devtools_client_->GetRuntime()->Evaluate( |
- "document.cookie", |
- base::Bind(&HeadlessBrowserContextIsolationTest::OnFirstGetCookieResult, |
- base::Unretained(this))); |
- } |
- |
- void OnFirstGetCookieResult(std::unique_ptr<runtime::EvaluateResult> result) { |
- std::string cookie; |
- EXPECT_TRUE(result->GetResult()->GetValue()->GetAsString(&cookie)); |
- EXPECT_EQ(kMainPageCookie, cookie); |
- |
- devtools_client2_->GetRuntime()->Evaluate( |
- "document.cookie", |
- base::Bind( |
- &HeadlessBrowserContextIsolationTest::OnSecondGetCookieResult, |
- base::Unretained(this))); |
- } |
- |
- void OnSecondGetCookieResult( |
- std::unique_ptr<runtime::EvaluateResult> result) { |
- std::string cookie; |
- EXPECT_TRUE(result->GetResult()->GetValue()->GetAsString(&cookie)); |
- EXPECT_EQ(kIsolatedPageCookie, cookie); |
- FinishTest(); |
- } |
- |
- void FinishTest() { |
- web_contents2_->RemoveObserver(this); |
- web_contents2_->Close(); |
- browser_context_.reset(); |
- FinishAsynchronousTest(); |
- } |
- |
- private: |
- std::unique_ptr<HeadlessBrowserContext> browser_context_; |
- HeadlessWebContents* web_contents2_; |
- std::unique_ptr<HeadlessDevToolsClient> devtools_client2_; |
- std::unique_ptr<LoadObserver> load_observer_; |
-}; |
- |
-HEADLESS_ASYNC_DEVTOOLED_TEST_F(HeadlessBrowserContextIsolationTest); |
- |
} // namespace headless |