| 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 "base/base64.h" | 5 #include "base/base64.h" |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "content/public/browser/devtools_agent_host.h" | 10 #include "content/public/browser/devtools_agent_host.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 const char kIdParam[] = "id"; | 24 const char kIdParam[] = "id"; |
| 25 const char kMethodParam[] = "method"; | 25 const char kMethodParam[] = "method"; |
| 26 const char kParamsParam[] = "params"; | 26 const char kParamsParam[] = "params"; |
| 27 | 27 |
| 28 } | 28 } |
| 29 | 29 |
| 30 class DevToolsProtocolTest : public ContentBrowserTest, | 30 class DevToolsProtocolTest : public ContentBrowserTest, |
| 31 public DevToolsAgentHostClient { | 31 public DevToolsAgentHostClient { |
| 32 public: |
| 33 DevToolsProtocolTest() : has_dispatched_command(false) {} |
| 34 |
| 32 protected: | 35 protected: |
| 33 void SendCommand(const std::string& method, | 36 void SendCommand(const std::string& method, |
| 34 scoped_ptr<base::DictionaryValue> params) { | 37 scoped_ptr<base::DictionaryValue> params) { |
| 35 base::DictionaryValue command; | 38 base::DictionaryValue command; |
| 39 has_dispatched_command = false; |
| 36 command.SetInteger(kIdParam, 1); | 40 command.SetInteger(kIdParam, 1); |
| 37 command.SetString(kMethodParam, method); | 41 command.SetString(kMethodParam, method); |
| 38 if (params) | 42 if (params) |
| 39 command.Set(kParamsParam, params.release()); | 43 command.Set(kParamsParam, params.release()); |
| 40 | 44 |
| 41 std::string json_command; | 45 std::string json_command; |
| 42 base::JSONWriter::Write(command, &json_command); | 46 base::JSONWriter::Write(command, &json_command); |
| 43 agent_host_->DispatchProtocolMessage(json_command); | 47 agent_host_->DispatchProtocolMessage(json_command); |
| 44 base::MessageLoop::current()->Run(); | 48 // Some messages are dispatched synchronously. |
| 49 // Only run loop if we are not finished yet. |
| 50 if (!has_dispatched_command) |
| 51 base::MessageLoop::current()->Run(); |
| 45 } | 52 } |
| 46 | 53 |
| 47 bool HasValue(const std::string& path) { | 54 bool HasValue(const std::string& path) { |
| 48 base::Value* value = 0; | 55 base::Value* value = 0; |
| 49 return result_->Get(path, &value); | 56 return result_->Get(path, &value); |
| 50 } | 57 } |
| 51 | 58 |
| 52 bool HasListItem(const std::string& path_to_list, | 59 bool HasListItem(const std::string& path_to_list, |
| 53 const std::string& name, | 60 const std::string& name, |
| 54 const std::string& value) { | 61 const std::string& value) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 83 scoped_refptr<DevToolsAgentHost> agent_host_; | 90 scoped_refptr<DevToolsAgentHost> agent_host_; |
| 84 | 91 |
| 85 private: | 92 private: |
| 86 void DispatchProtocolMessage(DevToolsAgentHost* agent_host, | 93 void DispatchProtocolMessage(DevToolsAgentHost* agent_host, |
| 87 const std::string& message) override { | 94 const std::string& message) override { |
| 88 scoped_ptr<base::DictionaryValue> root( | 95 scoped_ptr<base::DictionaryValue> root( |
| 89 static_cast<base::DictionaryValue*>(base::JSONReader::Read(message))); | 96 static_cast<base::DictionaryValue*>(base::JSONReader::Read(message))); |
| 90 base::DictionaryValue* result; | 97 base::DictionaryValue* result; |
| 91 EXPECT_TRUE(root->GetDictionary("result", &result)); | 98 EXPECT_TRUE(root->GetDictionary("result", &result)); |
| 92 result_.reset(result->DeepCopy()); | 99 result_.reset(result->DeepCopy()); |
| 93 base::MessageLoop::current()->QuitNow(); | 100 if (base::MessageLoop::current()->is_running()) |
| 101 base::MessageLoop::current()->QuitNow(); |
| 102 has_dispatched_command = true; |
| 94 } | 103 } |
| 95 | 104 |
| 96 void AgentHostClosed(DevToolsAgentHost* agent_host, bool replaced) override { | 105 void AgentHostClosed(DevToolsAgentHost* agent_host, bool replaced) override { |
| 97 EXPECT_TRUE(false); | 106 EXPECT_TRUE(false); |
| 98 } | 107 } |
| 108 |
| 109 bool has_dispatched_command; |
| 99 }; | 110 }; |
| 100 | 111 |
| 112 class SyntheticKeyEventTest : public DevToolsProtocolTest { |
| 113 protected: |
| 114 void SendKeyEvent(const std::string& type, |
| 115 int modifier, |
| 116 int windowsKeyCode, |
| 117 int nativeKeyCode) { |
| 118 scoped_ptr<base::DictionaryValue> params(new base::DictionaryValue()); |
| 119 params->SetString("type", type); |
| 120 params->SetInteger("modifiers", modifier); |
| 121 params->SetInteger("windowsVirtualKeyCode", windowsKeyCode); |
| 122 params->SetInteger("nativeVirtualKeyCode", nativeKeyCode); |
| 123 SendCommand("Input.dispatchKeyEvent", params.Pass()); |
| 124 } |
| 125 }; |
| 126 |
| 127 IN_PROC_BROWSER_TEST_F(SyntheticKeyEventTest, KeyEventSynthesizeKeyIdentifier) { |
| 128 NavigateToURLBlockUntilNavigationsComplete(shell(), GURL("about:blank"), 1); |
| 129 ASSERT_TRUE(content::ExecuteScript( |
| 130 shell()->web_contents()->GetRenderViewHost(), |
| 131 "function handleKeyEvent(event) {" |
| 132 "domAutomationController.setAutomationId(0);" |
| 133 "domAutomationController.send(event.keyIdentifier);" |
| 134 "}" |
| 135 "document.body.addEventListener('keydown', handleKeyEvent);" |
| 136 "document.body.addEventListener('keyup', handleKeyEvent);")); |
| 137 |
| 138 DOMMessageQueue dom_message_queue; |
| 139 |
| 140 // Send enter (keycode 13). |
| 141 SendKeyEvent("rawKeyDown", 0, 13, 13); |
| 142 SendKeyEvent("keyUp", 0, 13, 13); |
| 143 |
| 144 std::string key_identifier; |
| 145 ASSERT_TRUE(dom_message_queue.WaitForMessage(&key_identifier)); |
| 146 EXPECT_EQ("\"Enter\"", key_identifier); |
| 147 ASSERT_TRUE(dom_message_queue.WaitForMessage(&key_identifier)); |
| 148 EXPECT_EQ("\"Enter\"", key_identifier); |
| 149 |
| 150 // Send escape (keycode 27). |
| 151 SendKeyEvent("rawKeyDown", 0, 27, 27); |
| 152 SendKeyEvent("keyUp", 0, 27, 27); |
| 153 |
| 154 ASSERT_TRUE(dom_message_queue.WaitForMessage(&key_identifier)); |
| 155 EXPECT_EQ("\"U+001B\"", key_identifier); |
| 156 ASSERT_TRUE(dom_message_queue.WaitForMessage(&key_identifier)); |
| 157 EXPECT_EQ("\"U+001B\"", key_identifier); |
| 158 } |
| 159 |
| 101 class CaptureScreenshotTest : public DevToolsProtocolTest { | 160 class CaptureScreenshotTest : public DevToolsProtocolTest { |
| 102 private: | 161 private: |
| 103 #if !defined(OS_ANDROID) | 162 #if !defined(OS_ANDROID) |
| 104 void SetUpCommandLine(base::CommandLine* command_line) override { | 163 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 105 command_line->AppendSwitch(switches::kEnablePixelOutputInTests); | 164 command_line->AppendSwitch(switches::kEnablePixelOutputInTests); |
| 106 } | 165 } |
| 107 #endif | 166 #endif |
| 108 }; | 167 }; |
| 109 | 168 |
| 110 // Does not link on Android | 169 // Does not link on Android |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 // new value of |document.body.scrollTop| will depend on the screen dimensions | 314 // new value of |document.body.scrollTop| will depend on the screen dimensions |
| 256 // of the device that we're testing on, but in any case it should be greater | 315 // of the device that we're testing on, but in any case it should be greater |
| 257 // than 0. | 316 // than 0. |
| 258 ASSERT_TRUE(content::ExecuteScriptAndExtractInt( | 317 ASSERT_TRUE(content::ExecuteScriptAndExtractInt( |
| 259 shell()->web_contents(), | 318 shell()->web_contents(), |
| 260 "domAutomationController.send(document.body.scrollTop)", &scroll_top)); | 319 "domAutomationController.send(document.body.scrollTop)", &scroll_top)); |
| 261 ASSERT_GT(scroll_top, 0); | 320 ASSERT_GT(scroll_top, 0); |
| 262 } | 321 } |
| 263 | 322 |
| 264 } // namespace content | 323 } // namespace content |
| OLD | NEW |