| OLD | NEW | 
|    1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |    1 // Copyright (c) 2011 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 "chrome/test/webdriver/session_manager.h" |    5 #include "chrome/test/webdriver/session_manager.h" | 
|    6  |    6  | 
|    7 #include <sstream> |    7 #include <sstream> | 
|    8 #include <vector> |    8 #include <vector> | 
|    9  |    9  | 
|   10 #include "base/command_line.h" |   10 #include "base/command_line.h" | 
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   52 FrameId& FrameId::operator=(const FrameId& other) { |   52 FrameId& FrameId::operator=(const FrameId& other) { | 
|   53   window_id = other.window_id; |   53   window_id = other.window_id; | 
|   54   frame_path = other.frame_path; |   54   frame_path = other.frame_path; | 
|   55   return *this; |   55   return *this; | 
|   56 } |   56 } | 
|   57  |   57  | 
|   58 Session::Session() |   58 Session::Session() | 
|   59     : id_(GenerateRandomID()), |   59     : id_(GenerateRandomID()), | 
|   60       thread_(id_.c_str()), |   60       thread_(id_.c_str()), | 
|   61       implicit_wait_(0), |   61       implicit_wait_(0), | 
|   62       current_target_(FrameId(0, FramePath())) { |   62       current_target_(FrameId(0, FramePath())), | 
 |   63       use_native_events_(false) { | 
|   63   SessionManager::GetInstance()->Add(this); |   64   SessionManager::GetInstance()->Add(this); | 
|   64 } |   65 } | 
|   65  |   66  | 
|   66 Session::~Session() { |   67 Session::~Session() { | 
|   67   SessionManager::GetInstance()->Remove(id_); |   68   SessionManager::GetInstance()->Remove(id_); | 
|   68 } |   69 } | 
|   69  |   70  | 
|   70 bool Session::Init(const FilePath& browser_dir) { |   71 bool Session::Init(const FilePath& browser_dir) { | 
|   71   bool success = false; |   72   bool success = false; | 
|   72   if (thread_.Start()) { |   73   if (thread_.Start()) { | 
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  166   return static_cast<ErrorCode>(status); |  167   return static_cast<ErrorCode>(status); | 
|  167 } |  168 } | 
|  168  |  169  | 
|  169 ErrorCode Session::ExecuteScript(const std::string& script, |  170 ErrorCode Session::ExecuteScript(const std::string& script, | 
|  170                                  const ListValue* const args, |  171                                  const ListValue* const args, | 
|  171                                  Value** value) { |  172                                  Value** value) { | 
|  172   return ExecuteScript(current_target_, script, args, value); |  173   return ExecuteScript(current_target_, script, args, value); | 
|  173 } |  174 } | 
|  174  |  175  | 
|  175 ErrorCode Session::SendKeys(const WebElementId& element, const string16& keys) { |  176 ErrorCode Session::SendKeys(const WebElementId& element, const string16& keys) { | 
 |  177   // This method will first check if the element we want to send the keys to is | 
 |  178   // already focused, if not it will try to focus on it first. | 
|  176   ListValue args; |  179   ListValue args; | 
|  177   args.Append(element.ToValue()); |  180   args.Append(element.ToValue()); | 
|  178   // TODO(jleyba): Update this to use the correct atom. |  181   // TODO(jleyba): Update this to use the correct atom. | 
|  179   std::string script = "document.activeElement.blur();arguments[0].focus();"; |  182   std::string script = "if(document.activeElement!=arguments[0]){" | 
 |  183                        "  if(document.activeElement)" | 
 |  184                        "    document.activeElement.blur();" | 
 |  185                        "  arguments[0].focus();" | 
 |  186                        "}"; | 
|  180   Value* unscoped_result = NULL; |  187   Value* unscoped_result = NULL; | 
|  181   ErrorCode code = ExecuteScript(script, &args, &unscoped_result); |  188   ErrorCode code = ExecuteScript(script, &args, &unscoped_result); | 
|  182   scoped_ptr<Value> result(unscoped_result); |  | 
|  183   if (code != kSuccess) { |  189   if (code != kSuccess) { | 
|  184     LOG(ERROR) << "Failed to focus element before sending keys"; |  190     LOG(ERROR) << "Failed to get or set focus element before sending keys"; | 
|  185     return code; |  191     return code; | 
|  186   } |  192   } | 
|  187  |  | 
|  188   bool success = false; |  193   bool success = false; | 
|  189   RunSessionTask(NewRunnableMethod( |  194   RunSessionTask(NewRunnableMethod( | 
|  190       this, |  195       this, | 
|  191       &Session::SendKeysOnSessionThread, |  196       &Session::SendKeysOnSessionThread, | 
|  192       keys, |  197       keys, | 
|  193       &success)); |  198       &success)); | 
|  194   if (!success) |  199   if (!success) | 
|  195     return kUnknownError; |  200     return kUnknownError; | 
|  196   return kSuccess; |  201   return kSuccess; | 
|  197 } |  202 } | 
| (...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  707   automation_.reset(); |  712   automation_.reset(); | 
|  708 } |  713 } | 
|  709  |  714  | 
|  710 void Session::SendKeysOnSessionThread(const string16& keys, |  715 void Session::SendKeysOnSessionThread(const string16& keys, | 
|  711                                       bool* success) { |  716                                       bool* success) { | 
|  712   *success = true; |  717   *success = true; | 
|  713   std::vector<WebKeyEvent> key_events; |  718   std::vector<WebKeyEvent> key_events; | 
|  714   ConvertKeysToWebKeyEvents(keys, &key_events); |  719   ConvertKeysToWebKeyEvents(keys, &key_events); | 
|  715   for (size_t i = 0; i < key_events.size(); ++i) { |  720   for (size_t i = 0; i < key_events.size(); ++i) { | 
|  716     bool key_success = false; |  721     bool key_success = false; | 
|  717     automation_->SendWebKeyEvent( |  722     if (use_native_events_) { | 
|  718         current_target_.window_id, key_events[i], &key_success); |  723       // The automation provider will generate up/down events for us, we | 
 |  724       // only need to call it once as compared to the WebKeyEvent method. | 
 |  725       // Hence we filter events by their types, keeping only rawkeydown. | 
 |  726       if (key_events[i].type != automation::kRawKeyDownType) | 
 |  727         continue; | 
 |  728       automation_->SendNativeKeyEvent( | 
 |  729           current_target_.window_id, | 
 |  730           key_events[i].key_code, | 
 |  731           key_events[i].modifiers, | 
 |  732           &key_success); | 
 |  733     } else { | 
 |  734       automation_->SendWebKeyEvent( | 
 |  735           current_target_.window_id, key_events[i], &key_success); | 
 |  736     } | 
|  719     if (!key_success) { |  737     if (!key_success) { | 
|  720       LOG(ERROR) << "Failed to send key event. Event details:\n" |  738       LOG(ERROR) << "Failed to send key event. Event details:\n" | 
|  721                  << "Type: " << key_events[i].type << "\n" |  739                  << "Type: " << key_events[i].type << "\n" | 
|  722                  << "KeyCode: " << key_events[i].key_code << "\n" |  740                  << "KeyCode: " << key_events[i].key_code << "\n" | 
|  723                  << "UnmodifiedText: " << key_events[i].unmodified_text << "\n" |  741                  << "UnmodifiedText: " << key_events[i].unmodified_text << "\n" | 
|  724                  << "ModifiedText: " << key_events[i].modified_text << "\n" |  742                  << "ModifiedText: " << key_events[i].modified_text << "\n" | 
|  725                  << "Modifiers: " << key_events[i].modifiers << "\n"; |  743                  << "Modifiers: " << key_events[i].modifiers << "\n"; | 
|  726       *success = false; |  744       *success = false; | 
|  727     } |  745     } | 
|  728   } |  746   } | 
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  862   if (!loc_dict->GetInteger("x", &x) || |  880   if (!loc_dict->GetInteger("x", &x) || | 
|  863       !loc_dict->GetInteger("y", &y)) { |  881       !loc_dict->GetInteger("y", &y)) { | 
|  864     LOG(ERROR) << "Location atom returned bad coordinate dictionary"; |  882     LOG(ERROR) << "Location atom returned bad coordinate dictionary"; | 
|  865     code = kUnknownError; |  883     code = kUnknownError; | 
|  866   } |  884   } | 
|  867   *location = gfx::Point(x, y); |  885   *location = gfx::Point(x, y); | 
|  868   return kSuccess; |  886   return kSuccess; | 
|  869 } |  887 } | 
|  870  |  888  | 
|  871 }  // namespace webdriver |  889 }  // namespace webdriver | 
| OLD | NEW |