| 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/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 command.SetInteger(kIdParam, ++last_sent_id_); | 179 command.SetInteger(kIdParam, ++last_sent_id_); |
| 180 command.SetString(kMethodParam, method); | 180 command.SetString(kMethodParam, method); |
| 181 if (params) | 181 if (params) |
| 182 command.Set(kParamsParam, params.release()); | 182 command.Set(kParamsParam, params.release()); |
| 183 | 183 |
| 184 std::string json_command; | 184 std::string json_command; |
| 185 base::JSONWriter::Write(command, &json_command); | 185 base::JSONWriter::Write(command, &json_command); |
| 186 agent_host_->DispatchProtocolMessage(this, json_command); | 186 agent_host_->DispatchProtocolMessage(this, json_command); |
| 187 // Some messages are dispatched synchronously. | 187 // Some messages are dispatched synchronously. |
| 188 // Only run loop if we are not finished yet. | 188 // Only run loop if we are not finished yet. |
| 189 if (in_dispatch_ && wait) { | 189 if (in_dispatch_ && wait) |
| 190 waiting_for_command_result_id_ = last_sent_id_; | 190 WaitForResponse(); |
| 191 base::RunLoop().Run(); | |
| 192 } | |
| 193 in_dispatch_ = false; | 191 in_dispatch_ = false; |
| 194 } | 192 } |
| 195 | 193 |
| 194 void WaitForResponse() { |
| 195 waiting_for_command_result_id_ = last_sent_id_; |
| 196 base::RunLoop().Run(); |
| 197 } |
| 198 |
| 196 bool HasValue(const std::string& path) { | 199 bool HasValue(const std::string& path) { |
| 197 base::Value* value = 0; | 200 base::Value* value = 0; |
| 198 return result_->Get(path, &value); | 201 return result_->Get(path, &value); |
| 199 } | 202 } |
| 200 | 203 |
| 201 bool HasListItem(const std::string& path_to_list, | 204 bool HasListItem(const std::string& path_to_list, |
| 202 const std::string& name, | 205 const std::string& name, |
| 203 const std::string& value) { | 206 const std::string& value) { |
| 204 base::ListValue* list; | 207 base::ListValue* list; |
| 205 if (!result_->GetList(path_to_list, &list)) | 208 if (!result_->GetList(path_to_list, &list)) |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 // InterstitialPageDelegate: | 405 // InterstitialPageDelegate: |
| 403 std::string GetHTMLContents() override { return "<p>Interstitial</p>"; } | 406 std::string GetHTMLContents() override { return "<p>Interstitial</p>"; } |
| 404 }; | 407 }; |
| 405 | 408 |
| 406 class SyntheticKeyEventTest : public DevToolsProtocolTest { | 409 class SyntheticKeyEventTest : public DevToolsProtocolTest { |
| 407 protected: | 410 protected: |
| 408 void SendKeyEvent(const std::string& type, | 411 void SendKeyEvent(const std::string& type, |
| 409 int modifier, | 412 int modifier, |
| 410 int windowsKeyCode, | 413 int windowsKeyCode, |
| 411 int nativeKeyCode, | 414 int nativeKeyCode, |
| 412 const std::string& key) { | 415 const std::string& key, |
| 416 bool wait) { |
| 413 std::unique_ptr<base::DictionaryValue> params(new base::DictionaryValue()); | 417 std::unique_ptr<base::DictionaryValue> params(new base::DictionaryValue()); |
| 414 params->SetString("type", type); | 418 params->SetString("type", type); |
| 415 params->SetInteger("modifiers", modifier); | 419 params->SetInteger("modifiers", modifier); |
| 416 params->SetInteger("windowsVirtualKeyCode", windowsKeyCode); | 420 params->SetInteger("windowsVirtualKeyCode", windowsKeyCode); |
| 417 params->SetInteger("nativeVirtualKeyCode", nativeKeyCode); | 421 params->SetInteger("nativeVirtualKeyCode", nativeKeyCode); |
| 418 params->SetString("key", key); | 422 params->SetString("key", key); |
| 419 SendCommand("Input.dispatchKeyEvent", std::move(params)); | 423 SendCommand("Input.dispatchKeyEvent", std::move(params), wait); |
| 420 } | 424 } |
| 421 }; | 425 }; |
| 422 | 426 |
| 427 class SyntheticMouseEventTest : public DevToolsProtocolTest { |
| 428 protected: |
| 429 void SendMouseEvent(const std::string& type, int x, int y, bool wait) { |
| 430 std::unique_ptr<base::DictionaryValue> params(new base::DictionaryValue()); |
| 431 params->SetString("type", type); |
| 432 params->SetInteger("x", x); |
| 433 params->SetInteger("y", y); |
| 434 SendCommand("Input.dispatchMouseEvent", std::move(params), wait); |
| 435 } |
| 436 }; |
| 437 |
| 423 IN_PROC_BROWSER_TEST_F(SyntheticKeyEventTest, KeyEventSynthesizeKey) { | 438 IN_PROC_BROWSER_TEST_F(SyntheticKeyEventTest, KeyEventSynthesizeKey) { |
| 424 NavigateToURLBlockUntilNavigationsComplete(shell(), GURL("about:blank"), 1); | 439 NavigateToURLBlockUntilNavigationsComplete(shell(), GURL("about:blank"), 1); |
| 425 Attach(); | 440 Attach(); |
| 426 ASSERT_TRUE(content::ExecuteScript( | 441 ASSERT_TRUE(content::ExecuteScript( |
| 427 shell()->web_contents()->GetRenderViewHost(), | 442 shell()->web_contents()->GetRenderViewHost(), |
| 428 "function handleKeyEvent(event) {" | 443 "function handleKeyEvent(event) {" |
| 429 "domAutomationController.setAutomationId(0);" | 444 "domAutomationController.setAutomationId(0);" |
| 430 "domAutomationController.send(event.key);" | 445 "domAutomationController.send(event.key);" |
| 431 "}" | 446 "}" |
| 432 "document.body.addEventListener('keydown', handleKeyEvent);" | 447 "document.body.addEventListener('keydown', handleKeyEvent);" |
| 433 "document.body.addEventListener('keyup', handleKeyEvent);")); | 448 "document.body.addEventListener('keyup', handleKeyEvent);")); |
| 434 | 449 |
| 435 DOMMessageQueue dom_message_queue; | 450 DOMMessageQueue dom_message_queue; |
| 436 | 451 |
| 437 // Send enter (keycode 13). | 452 // Send enter (keycode 13). |
| 438 SendKeyEvent("rawKeyDown", 0, 13, 13, "Enter"); | 453 SendKeyEvent("rawKeyDown", 0, 13, 13, "Enter", true); |
| 439 SendKeyEvent("keyUp", 0, 13, 13, "Enter"); | 454 SendKeyEvent("keyUp", 0, 13, 13, "Enter", true); |
| 440 | 455 |
| 441 std::string key; | 456 std::string key; |
| 442 ASSERT_TRUE(dom_message_queue.WaitForMessage(&key)); | 457 ASSERT_TRUE(dom_message_queue.WaitForMessage(&key)); |
| 443 EXPECT_EQ("\"Enter\"", key); | 458 EXPECT_EQ("\"Enter\"", key); |
| 444 ASSERT_TRUE(dom_message_queue.WaitForMessage(&key)); | 459 ASSERT_TRUE(dom_message_queue.WaitForMessage(&key)); |
| 445 EXPECT_EQ("\"Enter\"", key); | 460 EXPECT_EQ("\"Enter\"", key); |
| 446 | 461 |
| 447 // Send escape (keycode 27). | 462 // Send escape (keycode 27). |
| 448 SendKeyEvent("rawKeyDown", 0, 27, 27, "Escape"); | 463 SendKeyEvent("rawKeyDown", 0, 27, 27, "Escape", true); |
| 449 SendKeyEvent("keyUp", 0, 27, 27, "Escape"); | 464 SendKeyEvent("keyUp", 0, 27, 27, "Escape", true); |
| 450 | 465 |
| 451 ASSERT_TRUE(dom_message_queue.WaitForMessage(&key)); | 466 ASSERT_TRUE(dom_message_queue.WaitForMessage(&key)); |
| 452 EXPECT_EQ("\"Escape\"", key); | 467 EXPECT_EQ("\"Escape\"", key); |
| 453 ASSERT_TRUE(dom_message_queue.WaitForMessage(&key)); | 468 ASSERT_TRUE(dom_message_queue.WaitForMessage(&key)); |
| 454 EXPECT_EQ("\"Escape\"", key); | 469 EXPECT_EQ("\"Escape\"", key); |
| 455 } | 470 } |
| 456 | 471 |
| 472 IN_PROC_BROWSER_TEST_F(SyntheticKeyEventTest, KeyboardEventAck) { |
| 473 NavigateToURLBlockUntilNavigationsComplete(shell(), GURL("about:blank"), 1); |
| 474 Attach(); |
| 475 ASSERT_TRUE(content::ExecuteScript( |
| 476 shell()->web_contents()->GetRenderViewHost(), |
| 477 "document.body.addEventListener('keydown', () => console.log('x'));")); |
| 478 |
| 479 scoped_refptr<InputMsgWatcher> filter = new InputMsgWatcher( |
| 480 RenderWidgetHostImpl::From( |
| 481 shell()->web_contents()->GetRenderViewHost()->GetWidget()), |
| 482 blink::WebInputEvent::MouseMove); |
| 483 |
| 484 SendCommand("Runtime.enable", nullptr); |
| 485 SendKeyEvent("rawKeyDown", 0, 13, 13, "Enter", false); |
| 486 |
| 487 // We expect that the console log message event arrives *before* the input |
| 488 // event ack, and the subsequent command response for Input.dispatchKeyEvent. |
| 489 WaitForNotification("Runtime.consoleAPICalled"); |
| 490 EXPECT_THAT(console_messages_, ElementsAre("x")); |
| 491 EXPECT_FALSE(filter->HasReceivedAck()); |
| 492 EXPECT_EQ(1u, result_ids_.size()); |
| 493 |
| 494 WaitForResponse(); |
| 495 EXPECT_EQ(2u, result_ids_.size()); |
| 496 } |
| 497 |
| 498 IN_PROC_BROWSER_TEST_F(SyntheticMouseEventTest, MouseEventAck) { |
| 499 NavigateToURLBlockUntilNavigationsComplete(shell(), GURL("about:blank"), 1); |
| 500 Attach(); |
| 501 ASSERT_TRUE(content::ExecuteScript( |
| 502 shell()->web_contents()->GetRenderViewHost(), |
| 503 "document.body.addEventListener('mousemove', () => console.log('x'));")); |
| 504 |
| 505 scoped_refptr<InputMsgWatcher> filter = new InputMsgWatcher( |
| 506 RenderWidgetHostImpl::From( |
| 507 shell()->web_contents()->GetRenderViewHost()->GetWidget()), |
| 508 blink::WebInputEvent::MouseMove); |
| 509 |
| 510 SendCommand("Runtime.enable", nullptr); |
| 511 SendMouseEvent("mouseMoved", 15, 15, false); |
| 512 |
| 513 // We expect that the console log message event arrives *before* the input |
| 514 // event ack, and the subsequent command response for |
| 515 // Input.dispatchMouseEvent. |
| 516 WaitForNotification("Runtime.consoleAPICalled"); |
| 517 EXPECT_THAT(console_messages_, ElementsAre("x")); |
| 518 EXPECT_FALSE(filter->HasReceivedAck()); |
| 519 EXPECT_EQ(1u, result_ids_.size()); |
| 520 |
| 521 WaitForResponse(); |
| 522 EXPECT_EQ(2u, result_ids_.size()); |
| 523 } |
| 524 |
| 457 namespace { | 525 namespace { |
| 458 bool DecodePNG(std::string base64_data, SkBitmap* bitmap) { | 526 bool DecodePNG(std::string base64_data, SkBitmap* bitmap) { |
| 459 std::string png_data; | 527 std::string png_data; |
| 460 if (!base::Base64Decode(base64_data, &png_data)) | 528 if (!base::Base64Decode(base64_data, &png_data)) |
| 461 return false; | 529 return false; |
| 462 return gfx::PNGCodec::Decode( | 530 return gfx::PNGCodec::Decode( |
| 463 reinterpret_cast<unsigned const char*>(png_data.data()), png_data.size(), | 531 reinterpret_cast<unsigned const char*>(png_data.data()), png_data.size(), |
| 464 bitmap); | 532 bitmap); |
| 465 } | 533 } |
| 466 | 534 |
| (...skipping 1042 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1509 EXPECT_EQ("polyglottal", value); | 1577 EXPECT_EQ("polyglottal", value); |
| 1510 found++; | 1578 found++; |
| 1511 } else { | 1579 } else { |
| 1512 FAIL(); | 1580 FAIL(); |
| 1513 } | 1581 } |
| 1514 } | 1582 } |
| 1515 EXPECT_EQ(2u, found); | 1583 EXPECT_EQ(2u, found); |
| 1516 } | 1584 } |
| 1517 | 1585 |
| 1518 } // namespace content | 1586 } // namespace content |
| OLD | NEW |