OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <stddef.h> | 5 #include <stddef.h> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/base64.h" | 8 #include "base/base64.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/strings/utf_string_conversions.h" |
14 #include "base/values.h" | 15 #include "base/values.h" |
15 #include "build/build_config.h" | 16 #include "build/build_config.h" |
16 #include "content/public/browser/devtools_agent_host.h" | 17 #include "content/public/browser/devtools_agent_host.h" |
17 #include "content/public/browser/javascript_dialog_manager.h" | 18 #include "content/public/browser/javascript_dialog_manager.h" |
18 #include "content/public/browser/navigation_handle.h" | 19 #include "content/public/browser/navigation_handle.h" |
19 #include "content/public/browser/render_frame_host.h" | 20 #include "content/public/browser/render_frame_host.h" |
20 #include "content/public/browser/render_view_host.h" | 21 #include "content/public/browser/render_view_host.h" |
21 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
22 #include "content/public/common/url_constants.h" | 23 #include "content/public/common/url_constants.h" |
23 #include "content/public/test/browser_test_utils.h" | 24 #include "content/public/test/browser_test_utils.h" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 | 97 |
97 private: | 98 private: |
98 DialogClosedCallback callback_; | 99 DialogClosedCallback callback_; |
99 bool handle_; | 100 bool handle_; |
100 DISALLOW_COPY_AND_ASSIGN(TestJavaScriptDialogManager); | 101 DISALLOW_COPY_AND_ASSIGN(TestJavaScriptDialogManager); |
101 }; | 102 }; |
102 | 103 |
103 } | 104 } |
104 | 105 |
105 class DevToolsProtocolTest : public ContentBrowserTest, | 106 class DevToolsProtocolTest : public ContentBrowserTest, |
106 public DevToolsAgentHostClient { | 107 public DevToolsAgentHostClient, |
| 108 public WebContentsDelegate { |
107 public: | 109 public: |
108 DevToolsProtocolTest() | 110 DevToolsProtocolTest() |
109 : last_sent_id_(0), | 111 : last_sent_id_(0), |
110 waiting_for_command_result_id_(0), | 112 waiting_for_command_result_id_(0), |
111 in_dispatch_(false) { | 113 in_dispatch_(false) { |
112 } | 114 } |
113 | 115 |
114 protected: | 116 protected: |
| 117 // WebContentsDelegate method: |
| 118 bool AddMessageToConsole(WebContents* source, |
| 119 int32_t level, |
| 120 const base::string16& message, |
| 121 int32_t line_no, |
| 122 const base::string16& source_id) override { |
| 123 console_messages_.push_back(base::UTF16ToUTF8(message)); |
| 124 return true; |
| 125 } |
| 126 |
115 void SendCommand(const std::string& method, | 127 void SendCommand(const std::string& method, |
116 std::unique_ptr<base::DictionaryValue> params) { | 128 std::unique_ptr<base::DictionaryValue> params) { |
117 SendCommand(method, std::move(params), true); | 129 SendCommand(method, std::move(params), true); |
118 } | 130 } |
119 | 131 |
120 void SendCommand(const std::string& method, | 132 void SendCommand(const std::string& method, |
121 std::unique_ptr<base::DictionaryValue> params, | 133 std::unique_ptr<base::DictionaryValue> params, |
122 bool wait) { | 134 bool wait) { |
123 in_dispatch_ = true; | 135 in_dispatch_ = true; |
124 base::DictionaryValue command; | 136 base::DictionaryValue command; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 return false; | 172 return false; |
161 if (id == value) | 173 if (id == value) |
162 return true; | 174 return true; |
163 } | 175 } |
164 return false; | 176 return false; |
165 } | 177 } |
166 | 178 |
167 void Attach() { | 179 void Attach() { |
168 agent_host_ = DevToolsAgentHost::GetOrCreateFor(shell()->web_contents()); | 180 agent_host_ = DevToolsAgentHost::GetOrCreateFor(shell()->web_contents()); |
169 agent_host_->AttachClient(this); | 181 agent_host_->AttachClient(this); |
| 182 shell()->web_contents()->SetDelegate(this); |
170 } | 183 } |
171 | 184 |
172 void TearDownOnMainThread() override { | 185 void TearDownOnMainThread() override { |
173 if (agent_host_) { | 186 if (agent_host_) { |
174 agent_host_->DetachClient(this); | 187 agent_host_->DetachClient(this); |
175 agent_host_ = nullptr; | 188 agent_host_ = nullptr; |
176 } | 189 } |
177 } | 190 } |
178 | 191 |
179 void WaitForNotification(const std::string& notification) { | 192 void WaitForNotification(const std::string& notification) { |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 urls.push_back(RemovePort(render_frame_host->GetLastCommittedURL())); | 263 urls.push_back(RemovePort(render_frame_host->GetLastCommittedURL())); |
251 } | 264 } |
252 return urls; | 265 return urls; |
253 } | 266 } |
254 | 267 |
255 std::unique_ptr<base::DictionaryValue> result_; | 268 std::unique_ptr<base::DictionaryValue> result_; |
256 scoped_refptr<DevToolsAgentHost> agent_host_; | 269 scoped_refptr<DevToolsAgentHost> agent_host_; |
257 int last_sent_id_; | 270 int last_sent_id_; |
258 std::vector<int> result_ids_; | 271 std::vector<int> result_ids_; |
259 std::vector<std::string> notifications_; | 272 std::vector<std::string> notifications_; |
| 273 std::vector<std::string> console_messages_; |
260 std::unique_ptr<base::DictionaryValue> requested_notification_params_; | 274 std::unique_ptr<base::DictionaryValue> requested_notification_params_; |
261 | 275 |
262 private: | 276 private: |
263 void DispatchProtocolMessage(DevToolsAgentHost* agent_host, | 277 void DispatchProtocolMessage(DevToolsAgentHost* agent_host, |
264 const std::string& message) override { | 278 const std::string& message) override { |
265 std::unique_ptr<base::DictionaryValue> root( | 279 std::unique_ptr<base::DictionaryValue> root( |
266 static_cast<base::DictionaryValue*>( | 280 static_cast<base::DictionaryValue*>( |
267 base::JSONReader::Read(message).release())); | 281 base::JSONReader::Read(message).release())); |
268 int id; | 282 int id; |
269 if (root->GetInteger("id", &id)) { | 283 if (root->GetInteger("id", &id)) { |
(...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
825 | 839 |
826 // Make sure each frame has the expected url. | 840 // Make sure each frame has the expected url. |
827 EXPECT_THAT( | 841 EXPECT_THAT( |
828 GetAllFrameUrls(), | 842 GetAllFrameUrls(), |
829 ElementsAre("http://127.0.0.1/devtools/control_navigations/" | 843 ElementsAre("http://127.0.0.1/devtools/control_navigations/" |
830 "iframe_navigation.html", | 844 "iframe_navigation.html", |
831 "http://a.com/devtools/navigation.html", | 845 "http://a.com/devtools/navigation.html", |
832 "http://b.com/devtools/control_navigations/meta_tag.html")); | 846 "http://b.com/devtools/control_navigations/meta_tag.html")); |
833 } | 847 } |
834 | 848 |
| 849 IN_PROC_BROWSER_TEST_F(DevToolsProtocolTest, VirtualTimeTest) { |
| 850 NavigateToURLBlockUntilNavigationsComplete(shell(), GURL("about:blank"), 1); |
| 851 Attach(); |
| 852 |
| 853 std::unique_ptr<base::DictionaryValue> params(new base::DictionaryValue()); |
| 854 params->SetString("policy", "pause"); |
| 855 SendCommand("Emulation.setVirtualTimePolicy", std::move(params), true); |
| 856 |
| 857 params.reset(new base::DictionaryValue()); |
| 858 params->SetString("expression", |
| 859 "setTimeout(function(){console.log('before')}, 1000);" |
| 860 "setTimeout(function(){console.log('after')}, 1001);"); |
| 861 SendCommand("Runtime.evaluate", std::move(params), true); |
| 862 |
| 863 // Let virtual time advance for one second. |
| 864 params.reset(new base::DictionaryValue()); |
| 865 params->SetString("policy", "advance"); |
| 866 params->SetInteger("budget", 1000); |
| 867 SendCommand("Emulation.setVirtualTimePolicy", std::move(params), true); |
| 868 |
| 869 WaitForNotification("Emulation.virtualTimeBudgetExpired"); |
| 870 |
| 871 params.reset(new base::DictionaryValue()); |
| 872 params->SetString("expression", "console.log('done')"); |
| 873 SendCommand("Runtime.evaluate", std::move(params), true); |
| 874 |
| 875 // The second timer shold not fire. |
| 876 EXPECT_THAT(console_messages_, ElementsAre("before", "done")); |
| 877 |
| 878 // Let virtual time advance for another second, which should make the second |
| 879 // timer fire. |
| 880 params.reset(new base::DictionaryValue()); |
| 881 params->SetString("policy", "advance"); |
| 882 params->SetInteger("budget", 1000); |
| 883 SendCommand("Emulation.setVirtualTimePolicy", std::move(params), true); |
| 884 |
| 885 WaitForNotification("Emulation.virtualTimeBudgetExpired"); |
| 886 |
| 887 EXPECT_THAT(console_messages_, ElementsAre("before", "done", "after")); |
| 888 } |
| 889 |
835 } // namespace content | 890 } // namespace content |
OLD | NEW |