OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "chrome/test/chromedriver/session_commands.h" | 5 #include "chrome/test/chromedriver/session_commands.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/logging.h" // For CHECK macros. | 10 #include "base/logging.h" // For CHECK macros. |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/test/chromedriver/basic_types.h" |
14 #include "chrome/test/chromedriver/chrome.h" | 15 #include "chrome/test/chromedriver/chrome.h" |
15 #include "chrome/test/chromedriver/session.h" | 16 #include "chrome/test/chromedriver/session.h" |
16 #include "chrome/test/chromedriver/session_map.h" | 17 #include "chrome/test/chromedriver/session_map.h" |
17 #include "chrome/test/chromedriver/status.h" | 18 #include "chrome/test/chromedriver/status.h" |
18 #include "chrome/test/chromedriver/web_view.h" | 19 #include "chrome/test/chromedriver/web_view.h" |
19 | 20 |
| 21 namespace { |
| 22 |
| 23 const char kWindowHandlePrefix[] = "CDwindow-"; |
| 24 |
| 25 } // namespace |
| 26 |
20 Status ExecuteSessionCommand( | 27 Status ExecuteSessionCommand( |
21 SessionMap* session_map, | 28 SessionMap* session_map, |
22 const SessionCommand& command, | 29 const SessionCommand& command, |
23 const base::DictionaryValue& params, | 30 const base::DictionaryValue& params, |
24 const std::string& session_id, | 31 const std::string& session_id, |
25 scoped_ptr<base::Value>* out_value, | 32 scoped_ptr<base::Value>* out_value, |
26 std::string* out_session_id) { | 33 std::string* out_session_id) { |
27 *out_session_id = session_id; | 34 *out_session_id = session_id; |
28 scoped_refptr<SessionAccessor> session_accessor; | 35 scoped_refptr<SessionAccessor> session_accessor; |
29 if (!session_map->Get(session_id, &session_accessor)) | 36 if (!session_map->Get(session_id, &session_accessor)) |
(...skipping 14 matching lines...) Expand all Loading... |
44 CHECK(session_map->Remove(session->id)); | 51 CHECK(session_map->Remove(session->id)); |
45 return session->chrome->Quit(); | 52 return session->chrome->Quit(); |
46 } | 53 } |
47 | 54 |
48 Status ExecuteGetCurrentWindowHandle( | 55 Status ExecuteGetCurrentWindowHandle( |
49 Session* session, | 56 Session* session, |
50 const base::DictionaryValue& params, | 57 const base::DictionaryValue& params, |
51 scoped_ptr<base::Value>* value) { | 58 scoped_ptr<base::Value>* value) { |
52 if (session->window.empty()) | 59 if (session->window.empty()) |
53 return Status(kNoSuchWindow); | 60 return Status(kNoSuchWindow); |
54 value->reset(new StringValue(session->window)); | 61 value->reset(new StringValue(kWindowHandlePrefix + session->window)); |
55 return Status(kOk); | 62 return Status(kOk); |
56 } | 63 } |
57 | 64 |
58 Status ExecuteGetWindowHandles( | 65 Status ExecuteGetWindowHandles( |
59 Session* session, | 66 Session* session, |
60 const base::DictionaryValue& params, | 67 const base::DictionaryValue& params, |
61 scoped_ptr<base::Value>* value) { | 68 scoped_ptr<base::Value>* value) { |
62 std::list<WebView*> web_views; | 69 std::list<WebView*> web_views; |
63 Status status = session->chrome->GetWebViews(&web_views); | 70 Status status = session->chrome->GetWebViews(&web_views); |
64 if (status.IsError()) | 71 if (status.IsError()) |
65 return status; | 72 return status; |
66 base::ListValue window_ids; | 73 base::ListValue window_ids; |
67 for (std::list<WebView*>::const_iterator it = web_views.begin(); | 74 for (std::list<WebView*>::const_iterator it = web_views.begin(); |
68 it != web_views.end(); ++it) { | 75 it != web_views.end(); ++it) { |
69 window_ids.AppendString((*it)->GetId()); | 76 window_ids.AppendString(kWindowHandlePrefix + (*it)->GetId()); |
70 } | 77 } |
71 value->reset(window_ids.DeepCopy()); | 78 value->reset(window_ids.DeepCopy()); |
72 return Status(kOk); | 79 return Status(kOk); |
73 } | 80 } |
74 | 81 |
| 82 Status ExecuteSwitchToWindow( |
| 83 Session* session, |
| 84 const base::DictionaryValue& params, |
| 85 scoped_ptr<base::Value>* value) { |
| 86 std::string name; |
| 87 if (!params.GetString("name", &name) || name.empty()) |
| 88 return Status(kUnknownError, "'name' must be a nonempty string"); |
| 89 |
| 90 std::list<WebView*> web_views; |
| 91 Status status = session->chrome->GetWebViews(&web_views); |
| 92 if (status.IsError()) |
| 93 return status; |
| 94 |
| 95 WebView* web_view = NULL; |
| 96 if (name.find(kWindowHandlePrefix) == 0u) { |
| 97 std::string handle = name.substr(std::string(kWindowHandlePrefix).length()); |
| 98 // Check if any window handle matches |handle|. |
| 99 for (std::list<WebView*>::const_iterator it = web_views.begin(); |
| 100 it != web_views.end(); ++it) { |
| 101 if ((*it)->GetId() == handle) { |
| 102 web_view = *it; |
| 103 break; |
| 104 } |
| 105 } |
| 106 } else { |
| 107 // Check if any of the tab window names match |name|. |
| 108 const char* kGetWindowNameScript = "function() { return window.name; }"; |
| 109 base::ListValue args; |
| 110 for (std::list<WebView*>::const_iterator it = web_views.begin(); |
| 111 it != web_views.end(); ++it) { |
| 112 scoped_ptr<base::Value> result; |
| 113 status = (*it)->CallFunction("", kGetWindowNameScript, args, &result); |
| 114 if (status.IsError()) |
| 115 return status; |
| 116 std::string window_name; |
| 117 if (!result->GetAsString(&window_name)) |
| 118 return Status(kUnknownError, "failed to get window name"); |
| 119 if (window_name == name) { |
| 120 web_view = *it; |
| 121 break; |
| 122 } |
| 123 } |
| 124 } |
| 125 |
| 126 if (!web_view) |
| 127 return Status(kNoSuchWindow); |
| 128 session->window = web_view->GetId(); |
| 129 session->frame = ""; |
| 130 session->mouse_position = WebPoint(0, 0); |
| 131 return Status(kOk); |
| 132 } |
| 133 |
75 Status ExecuteSetTimeout( | 134 Status ExecuteSetTimeout( |
76 Session* session, | 135 Session* session, |
77 const base::DictionaryValue& params, | 136 const base::DictionaryValue& params, |
78 scoped_ptr<base::Value>* value) { | 137 scoped_ptr<base::Value>* value) { |
79 int ms; | 138 int ms; |
80 if (!params.GetInteger("ms", &ms) || ms < 0) | 139 if (!params.GetInteger("ms", &ms) || ms < 0) |
81 return Status(kUnknownError, "'ms' must be a non-negative integer"); | 140 return Status(kUnknownError, "'ms' must be a non-negative integer"); |
82 std::string type; | 141 std::string type; |
83 if (!params.GetString("type", &type)) | 142 if (!params.GetString("type", &type)) |
84 return Status(kUnknownError, "'type' must be a string"); | 143 return Status(kUnknownError, "'type' must be a string"); |
85 if (type == "implicit") | 144 if (type == "implicit") |
86 session->implicit_wait = ms; | 145 session->implicit_wait = ms; |
87 else if (type == "script") | 146 else if (type == "script") |
88 session->script_timeout = ms; | 147 session->script_timeout = ms; |
89 else if (type == "page load") | 148 else if (type == "page load") |
90 session->page_load_timeout = ms; | 149 session->page_load_timeout = ms; |
91 else | 150 else |
92 return Status(kUnknownError, "unknown type of timeout:" + type); | 151 return Status(kUnknownError, "unknown type of timeout:" + type); |
93 return Status(kOk); | 152 return Status(kOk); |
94 } | 153 } |
OLD | NEW |