| 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   GURL test_url = GetTestUrl("devtools", "input_ack_tests.html"); | 
 |   474   NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 1); | 
 |   475   Attach(); | 
 |   476  | 
 |   477   DOMMessageQueue dom_message_queue; | 
 |   478   scoped_refptr<InputMsgWatcher> filter = new InputMsgWatcher( | 
 |   479       RenderWidgetHostImpl::From( | 
 |   480           shell()->web_contents()->GetRenderViewHost()->GetWidget()), | 
 |   481       blink::WebInputEvent::RawKeyDown); | 
 |   482  | 
 |   483   // Call Input.dispatchKeyEvent, but don't wait for a response. | 
 |   484   SendKeyEvent("rawKeyDown", 0, 13, 13, "Enter", false); | 
 |   485   // Make sure we've entered the page's keydown event listener. | 
 |   486   std::string message; | 
 |   487   ASSERT_TRUE(dom_message_queue.WaitForMessage(&message)); | 
 |   488   EXPECT_EQ("\"KeyboardEvent listener\"", message); | 
 |   489   // Check that an ack was not received before the response. | 
 |   490   ASSERT_FALSE(filter->HasReceivedAck()); | 
 |   491   // Check that an ack is received after the response. | 
 |   492   WaitForResponse(); | 
 |   493   ASSERT_EQ(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, filter->WaitForAck()); | 
 |   494 } | 
 |   495  | 
 |   496 IN_PROC_BROWSER_TEST_F(SyntheticMouseEventTest, MouseEventAck) { | 
 |   497   GURL test_url = GetTestUrl("devtools", "input_ack_tests.html"); | 
 |   498   NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 1); | 
 |   499   Attach(); | 
 |   500  | 
 |   501   DOMMessageQueue dom_message_queue; | 
 |   502   scoped_refptr<InputMsgWatcher> filter = new InputMsgWatcher( | 
 |   503       RenderWidgetHostImpl::From( | 
 |   504           shell()->web_contents()->GetRenderViewHost()->GetWidget()), | 
 |   505       blink::WebInputEvent::MouseMove); | 
 |   506  | 
 |   507   // Call Input.dispatchMouseEvent, but don't wait for a response. | 
 |   508   SendMouseEvent("mouseMoved", 15, 15, false); | 
 |   509   // Make sure we've entered the page's keydown event listener. | 
 |   510   std::string message; | 
 |   511   ASSERT_TRUE(dom_message_queue.WaitForMessage(&message)); | 
 |   512   EXPECT_EQ("\"MouseEvent listener\"", message); | 
 |   513   // Check that an ack was not received before the response. | 
 |   514   ASSERT_FALSE(filter->HasReceivedAck()); | 
 |   515   // Check that an ack is received after the response. | 
 |   516   WaitForResponse(); | 
 |   517   ASSERT_EQ(INPUT_EVENT_ACK_STATE_CONSUMED, filter->WaitForAck()); | 
 |   518 } | 
 |   519  | 
|   457 namespace { |   520 namespace { | 
|   458 bool DecodePNG(std::string base64_data, SkBitmap* bitmap) { |   521 bool DecodePNG(std::string base64_data, SkBitmap* bitmap) { | 
|   459   std::string png_data; |   522   std::string png_data; | 
|   460   if (!base::Base64Decode(base64_data, &png_data)) |   523   if (!base::Base64Decode(base64_data, &png_data)) | 
|   461     return false; |   524     return false; | 
|   462   return gfx::PNGCodec::Decode( |   525   return gfx::PNGCodec::Decode( | 
|   463       reinterpret_cast<unsigned const char*>(png_data.data()), png_data.size(), |   526       reinterpret_cast<unsigned const char*>(png_data.data()), png_data.size(), | 
|   464       bitmap); |   527       bitmap); | 
|   465 } |   528 } | 
|   466  |   529  | 
| (...skipping 928 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  1395   command_params.reset(new base::DictionaryValue()); |  1458   command_params.reset(new base::DictionaryValue()); | 
|  1396   command_params->SetBoolean("autoAttach", false); |  1459   command_params->SetBoolean("autoAttach", false); | 
|  1397   command_params->SetBoolean("waitForDebuggerOnStart", false); |  1460   command_params->SetBoolean("waitForDebuggerOnStart", false); | 
|  1398   SendCommand("Target.setAutoAttach", std::move(command_params), false); |  1461   SendCommand("Target.setAutoAttach", std::move(command_params), false); | 
|  1399   params = WaitForNotification("Target.detachedFromTarget", true); |  1462   params = WaitForNotification("Target.detachedFromTarget", true); | 
|  1400   EXPECT_TRUE(params->GetString("targetId", &temp)); |  1463   EXPECT_TRUE(params->GetString("targetId", &temp)); | 
|  1401   EXPECT_EQ(target_id, temp); |  1464   EXPECT_EQ(target_id, temp); | 
|  1402 } |  1465 } | 
|  1403  |  1466  | 
|  1404 }  // namespace content |  1467 }  // namespace content | 
| OLD | NEW |