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

Side by Side Diff: headless/lib/headless_browser_browsertest.cc

Issue 2840993003: [headless] Expose DevToolsTarget via HeadlessBrowser + add tracing test. (Closed)
Patch Set: rebase Created 3 years, 7 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 unified diff | Download patch
« no previous file with comments | « headless/lib/browser/headless_browser_impl.cc ('k') | headless/public/headless_browser.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <memory> 5 #include <memory>
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/files/file_enumerator.h" 8 #include "base/files/file_enumerator.h"
9 #include "base/files/file_util.h" 9 #include "base/files/file_util.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "base/threading/thread_restrictions.h" 13 #include "base/threading/thread_restrictions.h"
14 #include "content/public/browser/permission_manager.h" 14 #include "content/public/browser/permission_manager.h"
15 #include "content/public/browser/permission_type.h" 15 #include "content/public/browser/permission_type.h"
16 #include "content/public/common/url_constants.h" 16 #include "content/public/common/url_constants.h"
17 #include "content/public/test/browser_test.h" 17 #include "content/public/test/browser_test.h"
18 #include "headless/lib/browser/headless_browser_context_impl.h" 18 #include "headless/lib/browser/headless_browser_context_impl.h"
19 #include "headless/lib/browser/headless_web_contents_impl.h" 19 #include "headless/lib/browser/headless_web_contents_impl.h"
20 #include "headless/lib/headless_macros.h" 20 #include "headless/lib/headless_macros.h"
21 #include "headless/public/devtools/domains/inspector.h" 21 #include "headless/public/devtools/domains/inspector.h"
22 #include "headless/public/devtools/domains/network.h" 22 #include "headless/public/devtools/domains/network.h"
23 #include "headless/public/devtools/domains/page.h" 23 #include "headless/public/devtools/domains/page.h"
24 #include "headless/public/devtools/domains/tracing.h"
24 #include "headless/public/headless_browser.h" 25 #include "headless/public/headless_browser.h"
25 #include "headless/public/headless_devtools_client.h" 26 #include "headless/public/headless_devtools_client.h"
26 #include "headless/public/headless_devtools_target.h" 27 #include "headless/public/headless_devtools_target.h"
27 #include "headless/public/headless_web_contents.h" 28 #include "headless/public/headless_web_contents.h"
28 #include "headless/test/headless_browser_test.h" 29 #include "headless/test/headless_browser_test.h"
29 #include "headless/test/test_protocol_handler.h" 30 #include "headless/test/test_protocol_handler.h"
30 #include "headless/test/test_url_request_job.h" 31 #include "headless/test/test_url_request_job.h"
31 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" 32 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
32 #include "net/cookies/cookie_store.h" 33 #include "net/cookies/cookie_store.h"
33 #include "net/test/spawned_test_server/spawned_test_server.h" 34 #include "net/test/spawned_test_server/spawned_test_server.h"
(...skipping 788 matching lines...) Expand 10 before | Expand all | Expand 10 after
822 .Build(); 823 .Build();
823 EXPECT_TRUE(WaitForLoad(web_contents)); 824 EXPECT_TRUE(WaitForLoad(web_contents));
824 browser()->Shutdown(); 825 browser()->Shutdown();
825 826
826 base::ThreadRestrictions::SetIOAllowed(true); 827 base::ThreadRestrictions::SetIOAllowed(true);
827 std::string net_log_data; 828 std::string net_log_data;
828 EXPECT_TRUE(base::ReadFileToString(net_log_, &net_log_data)); 829 EXPECT_TRUE(base::ReadFileToString(net_log_, &net_log_data));
829 EXPECT_GE(net_log_data.find("hello.html"), 0u); 830 EXPECT_GE(net_log_data.find("hello.html"), 0u);
830 } 831 }
831 832
833 namespace {
834
835 class TraceHelper : public headless::tracing::ExperimentalObserver {
836 public:
837 TraceHelper(HeadlessBrowserTest* browser_test, HeadlessDevToolsTarget* target)
838 : browser_test_(browser_test),
839 target_(target),
840 client_(HeadlessDevToolsClient::Create()),
841 tracing_data_(base::MakeUnique<base::ListValue>()) {
842 EXPECT_FALSE(target_->IsAttached());
843 target_->AttachClient(client_.get());
844 EXPECT_TRUE(target_->IsAttached());
845
846 client_->GetTracing()->GetExperimental()->AddObserver(this);
847
848 client_->GetTracing()->GetExperimental()->Start(
849 tracing::StartParams::Builder().Build(),
850 base::Bind(&TraceHelper::OnTracingStarted, base::Unretained(this)));
851 }
852
853 ~TraceHelper() override {
854 target_->DetachClient(client_.get());
855 EXPECT_FALSE(target_->IsAttached());
856 }
857
858 std::unique_ptr<base::ListValue> TakeTracingData() {
859 return std::move(tracing_data_);
860 }
861
862 private:
863 void OnTracingStarted(std::unique_ptr<headless::tracing::StartResult>) {
864 // We don't need the callback from End, but the OnTracingComplete event.
865 client_->GetTracing()->GetExperimental()->End(
866 headless::tracing::EndParams::Builder().Build());
867 }
868
869 // headless::tracing::ExperimentalObserver implementation:
870 void OnDataCollected(
871 const headless::tracing::DataCollectedParams& params) override {
872 for (const auto& value : *params.GetValue()) {
873 tracing_data_->Append(value->CreateDeepCopy());
874 }
875 }
876
877 void OnTracingComplete(
878 const headless::tracing::TracingCompleteParams& params) override {
879 browser_test_->FinishAsynchronousTest();
880 }
881
882 HeadlessBrowserTest* browser_test_; // Not owned.
883 HeadlessDevToolsTarget* target_; // Not owned.
884 std::unique_ptr<HeadlessDevToolsClient> client_;
885
886 std::unique_ptr<base::ListValue> tracing_data_;
887
888 DISALLOW_COPY_AND_ASSIGN(TraceHelper);
889 };
890
891 } // namespace
892
893 IN_PROC_BROWSER_TEST_F(HeadlessBrowserTest, TraceUsingBrowserDevToolsTarget) {
894 HeadlessDevToolsTarget* target = browser()->GetDevToolsTarget();
895 EXPECT_NE(nullptr, target);
896
897 TraceHelper helper(this, target);
898 RunAsynchronousTest();
899
900 std::unique_ptr<base::ListValue> tracing_data = helper.TakeTracingData();
901 EXPECT_TRUE(tracing_data);
902 EXPECT_LT(0u, tracing_data->GetSize());
903 }
904
832 } // namespace headless 905 } // namespace headless
OLDNEW
« no previous file with comments | « headless/lib/browser/headless_browser_impl.cc ('k') | headless/public/headless_browser.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698