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" |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 void WaitForNotification(const std::string& notification) { | 173 void WaitForNotification(const std::string& notification) { |
174 waiting_for_notification_ = notification; | 174 waiting_for_notification_ = notification; |
175 RunMessageLoop(); | 175 RunMessageLoop(); |
176 } | 176 } |
177 | 177 |
178 std::unique_ptr<base::DictionaryValue> result_; | 178 std::unique_ptr<base::DictionaryValue> result_; |
179 scoped_refptr<DevToolsAgentHost> agent_host_; | 179 scoped_refptr<DevToolsAgentHost> agent_host_; |
180 int last_sent_id_; | 180 int last_sent_id_; |
181 std::vector<int> result_ids_; | 181 std::vector<int> result_ids_; |
182 std::vector<std::string> notifications_; | 182 std::vector<std::string> notifications_; |
| 183 std::unique_ptr<base::DictionaryValue> requested_notification_params_; |
183 | 184 |
184 private: | 185 private: |
185 void DispatchProtocolMessage(DevToolsAgentHost* agent_host, | 186 void DispatchProtocolMessage(DevToolsAgentHost* agent_host, |
186 const std::string& message) override { | 187 const std::string& message) override { |
187 std::unique_ptr<base::DictionaryValue> root( | 188 std::unique_ptr<base::DictionaryValue> root( |
188 static_cast<base::DictionaryValue*>( | 189 static_cast<base::DictionaryValue*>( |
189 base::JSONReader::Read(message).release())); | 190 base::JSONReader::Read(message).release())); |
190 int id; | 191 int id; |
191 if (root->GetInteger("id", &id)) { | 192 if (root->GetInteger("id", &id)) { |
192 result_ids_.push_back(id); | 193 result_ids_.push_back(id); |
193 base::DictionaryValue* result; | 194 base::DictionaryValue* result; |
194 ASSERT_TRUE(root->GetDictionary("result", &result)); | 195 ASSERT_TRUE(root->GetDictionary("result", &result)); |
195 result_.reset(result->DeepCopy()); | 196 result_.reset(result->DeepCopy()); |
196 in_dispatch_ = false; | 197 in_dispatch_ = false; |
197 if (id && id == waiting_for_command_result_id_) { | 198 if (id && id == waiting_for_command_result_id_) { |
198 waiting_for_command_result_id_ = 0; | 199 waiting_for_command_result_id_ = 0; |
199 base::MessageLoop::current()->QuitNow(); | 200 base::MessageLoop::current()->QuitNow(); |
200 } | 201 } |
201 } else { | 202 } else { |
202 std::string notification; | 203 std::string notification; |
203 EXPECT_TRUE(root->GetString("method", ¬ification)); | 204 EXPECT_TRUE(root->GetString("method", ¬ification)); |
204 notifications_.push_back(notification); | 205 notifications_.push_back(notification); |
205 if (waiting_for_notification_ == notification) { | 206 if (waiting_for_notification_ == notification) { |
| 207 base::DictionaryValue* params; |
| 208 if (root->GetDictionary("params", ¶ms)) { |
| 209 requested_notification_params_ = params->CreateDeepCopy(); |
| 210 } else { |
| 211 requested_notification_params_.reset(); |
| 212 } |
206 waiting_for_notification_ = std::string(); | 213 waiting_for_notification_ = std::string(); |
207 base::MessageLoop::current()->QuitNow(); | 214 base::MessageLoop::current()->QuitNow(); |
208 } | 215 } |
209 } | 216 } |
210 } | 217 } |
211 | 218 |
212 void AgentHostClosed(DevToolsAgentHost* agent_host, bool replaced) override { | 219 void AgentHostClosed(DevToolsAgentHost* agent_host, bool replaced) override { |
213 EXPECT_TRUE(false); | 220 EXPECT_TRUE(false); |
214 } | 221 } |
215 | 222 |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 IN_PROC_BROWSER_TEST_F(DevToolsProtocolTest, BrowserNewPage) { | 597 IN_PROC_BROWSER_TEST_F(DevToolsProtocolTest, BrowserNewPage) { |
591 NavigateToURLBlockUntilNavigationsComplete(shell(), GURL("about:blank"), 1); | 598 NavigateToURLBlockUntilNavigationsComplete(shell(), GURL("about:blank"), 1); |
592 Attach(); | 599 Attach(); |
593 EXPECT_EQ(1u, shell()->windows().size()); | 600 EXPECT_EQ(1u, shell()->windows().size()); |
594 std::unique_ptr<base::DictionaryValue> params(new base::DictionaryValue()); | 601 std::unique_ptr<base::DictionaryValue> params(new base::DictionaryValue()); |
595 params->SetString("initialUrl", "about:blank"); | 602 params->SetString("initialUrl", "about:blank"); |
596 SendCommand("Browser.newPage", std::move(params), true); | 603 SendCommand("Browser.newPage", std::move(params), true); |
597 EXPECT_EQ(2u, shell()->windows().size()); | 604 EXPECT_EQ(2u, shell()->windows().size()); |
598 } | 605 } |
599 | 606 |
| 607 IN_PROC_BROWSER_TEST_F(DevToolsProtocolTest, NavigationThrottle) { |
| 608 NavigateToURLBlockUntilNavigationsComplete(shell(), GURL("about:blank"), 1); |
| 609 ASSERT_TRUE(embedded_test_server()->Start()); |
| 610 Attach(); |
| 611 SendCommand("Page.enable", nullptr); |
| 612 |
| 613 std::unique_ptr<base::DictionaryValue> params(new base::DictionaryValue()); |
| 614 params->SetBoolean("enabled", true); |
| 615 SendCommand("Page.setNavigationThrottleEnabled", std::move(params), true); |
| 616 |
| 617 GURL test_url = embedded_test_server()->GetURL("/devtools/meta_tag.html"); |
| 618 shell()->LoadURL(test_url); |
| 619 |
| 620 // Wait for and allow navigation to /devtools/meta_tag.html |
| 621 WaitForNotification("Page.navigationRequested"); |
| 622 ASSERT_TRUE(requested_notification_params_.get()); |
| 623 std::string url; |
| 624 ASSERT_TRUE(requested_notification_params_->GetString("url", &url)); |
| 625 EXPECT_TRUE(base::EndsWith(url, "/devtools/meta_tag.html", |
| 626 base::CompareCase::SENSITIVE)); |
| 627 int navigation_id; |
| 628 ASSERT_TRUE(requested_notification_params_->GetInteger("navigationId", |
| 629 &navigation_id)); |
| 630 |
| 631 params.reset(new base::DictionaryValue()); |
| 632 params->SetString("response", "Proceed"); |
| 633 params->SetInteger("navigationId", navigation_id); |
| 634 SendCommand("Page.processNavigation", std::move(params), true); |
| 635 |
| 636 // Wait for and cancel navigation to /devtools/navigation.html |
| 637 WaitForNotification("Page.navigationRequested"); |
| 638 |
| 639 ASSERT_TRUE(requested_notification_params_.get()); |
| 640 ASSERT_TRUE(requested_notification_params_->GetString("url", &url)); |
| 641 EXPECT_TRUE(base::EndsWith(url, "/devtools/navigation.html", |
| 642 base::CompareCase::SENSITIVE)); |
| 643 ASSERT_TRUE(requested_notification_params_->GetInteger("navigationId", |
| 644 &navigation_id)); |
| 645 |
| 646 params.reset(new base::DictionaryValue()); |
| 647 params->SetString("response", "Cancel"); |
| 648 params->SetInteger("navigationId", navigation_id); |
| 649 std::string navigating_frame_id; |
| 650 ASSERT_TRUE(requested_notification_params_->GetString("frameId", |
| 651 &navigating_frame_id)); |
| 652 SendCommand("Page.processNavigation", std::move(params), true); |
| 653 |
| 654 // Check the cancellation occurred. |
| 655 WaitForNotification("Page.frameClearedScheduledNavigation"); |
| 656 ASSERT_TRUE(requested_notification_params_.get()); |
| 657 std::string frame_id; |
| 658 ASSERT_TRUE(requested_notification_params_->GetString("frameId", &frame_id)); |
| 659 EXPECT_EQ(frame_id, navigating_frame_id); |
| 660 } |
| 661 |
600 } // namespace content | 662 } // namespace content |
OLD | NEW |