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/chrome/chrome_desktop_impl.h" | 5 #include "chrome/test/chromedriver/chrome/chrome_desktop_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ptr_util.h" |
12 #include "base/posix/eintr_wrapper.h" | 13 #include "base/posix/eintr_wrapper.h" |
13 #include "base/process/kill.h" | 14 #include "base/process/kill.h" |
14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
15 #include "base/sys_info.h" | 16 #include "base/sys_info.h" |
16 #include "base/threading/platform_thread.h" | 17 #include "base/threading/platform_thread.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "build/build_config.h" | 19 #include "build/build_config.h" |
19 #include "chrome/test/chromedriver/chrome/automation_extension.h" | 20 #include "chrome/test/chromedriver/chrome/automation_extension.h" |
20 #include "chrome/test/chromedriver/chrome/devtools_client.h" | 21 #include "chrome/test/chromedriver/chrome/devtools_client.h" |
21 #include "chrome/test/chromedriver/chrome/devtools_event_listener.h" | 22 #include "chrome/test/chromedriver/chrome/devtools_event_listener.h" |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 } | 227 } |
227 | 228 |
228 int ChromeDesktopImpl::GetNetworkConnection() const { | 229 int ChromeDesktopImpl::GetNetworkConnection() const { |
229 return network_connection_; | 230 return network_connection_; |
230 } | 231 } |
231 | 232 |
232 void ChromeDesktopImpl::SetNetworkConnection( | 233 void ChromeDesktopImpl::SetNetworkConnection( |
233 int network_connection) { | 234 int network_connection) { |
234 network_connection_ = network_connection; | 235 network_connection_ = network_connection; |
235 } | 236 } |
| 237 |
| 238 Status ChromeDesktopImpl::GetWindowPosition(const std::string& target_id, |
| 239 int* x, |
| 240 int* y) { |
| 241 Window window; |
| 242 Status status = GetWindow(target_id, &window); |
| 243 if (status.IsError()) |
| 244 return status; |
| 245 |
| 246 *x = window.left; |
| 247 *y = window.top; |
| 248 return Status(kOk); |
| 249 } |
| 250 |
| 251 Status ChromeDesktopImpl::GetWindowSize(const std::string& target_id, |
| 252 int* width, |
| 253 int* height) { |
| 254 Window window; |
| 255 Status status = GetWindow(target_id, &window); |
| 256 if (status.IsError()) |
| 257 return status; |
| 258 |
| 259 *width = window.width; |
| 260 *height = window.height; |
| 261 return Status(kOk); |
| 262 } |
| 263 |
| 264 Status ChromeDesktopImpl::SetWindowPosition(const std::string& target_id, |
| 265 int x, |
| 266 int y) { |
| 267 auto bounds = base::MakeUnique<base::DictionaryValue>(); |
| 268 bounds->SetInteger("left", x); |
| 269 bounds->SetInteger("top", y); |
| 270 return SetWindowBounds(target_id, std::move(bounds)); |
| 271 } |
| 272 |
| 273 Status ChromeDesktopImpl::SetWindowSize(const std::string& target_id, |
| 274 int width, |
| 275 int height) { |
| 276 auto bounds = base::MakeUnique<base::DictionaryValue>(); |
| 277 bounds->SetInteger("width", width); |
| 278 bounds->SetInteger("height", height); |
| 279 return SetWindowBounds(target_id, std::move(bounds)); |
| 280 } |
| 281 |
| 282 Status ChromeDesktopImpl::MaximizeWindow(const std::string& target_id) { |
| 283 Window window; |
| 284 Status status = GetWindow(target_id, &window); |
| 285 if (status.IsError()) |
| 286 return status; |
| 287 |
| 288 if (window.state == "maximized") |
| 289 return Status(kOk); |
| 290 |
| 291 if (window.state != "normal") { |
| 292 // always restore window to normal first, since chrome ui doesn't allow |
| 293 // maximizing a minimized or fullscreen window. |
| 294 status = SetWindowState(window.id, "normal"); |
| 295 if (status.IsError()) |
| 296 return status; |
| 297 } |
| 298 |
| 299 return SetWindowState(window.id, "maximized"); |
| 300 } |
| 301 |
| 302 Status ChromeDesktopImpl::ParseWindow( |
| 303 std::unique_ptr<base::DictionaryValue> params, |
| 304 Window* window) { |
| 305 if (!params->GetInteger("windowId", &window->id)) |
| 306 return Status(kUnknownError, "no window id in response"); |
| 307 |
| 308 const base::Value* value = nullptr; |
| 309 const base::DictionaryValue* bounds_dict = nullptr; |
| 310 if (!params->Get("bounds", &value) || !value->GetAsDictionary(&bounds_dict)) |
| 311 return Status(kUnknownError, "no window bounds in response"); |
| 312 |
| 313 if (!bounds_dict->GetString("windowState", &window->state)) |
| 314 return Status(kUnknownError, "no window state in window bounds"); |
| 315 |
| 316 if (!bounds_dict->GetInteger("left", &window->left)) |
| 317 return Status(kUnknownError, "no left offset in window bounds"); |
| 318 if (!bounds_dict->GetInteger("top", &window->top)) |
| 319 return Status(kUnknownError, "no top offset in window bounds"); |
| 320 if (!bounds_dict->GetInteger("width", &window->width)) |
| 321 return Status(kUnknownError, "no width in window bounds"); |
| 322 if (!bounds_dict->GetInteger("height", &window->height)) |
| 323 return Status(kUnknownError, "no height in window bounds"); |
| 324 |
| 325 return Status(kOk); |
| 326 } |
| 327 |
| 328 Status ChromeDesktopImpl::GetWindow(const std::string& target_id, |
| 329 Window* window) { |
| 330 Status status = devtools_websocket_client_->ConnectIfNecessary(); |
| 331 if (status.IsError()) |
| 332 return status; |
| 333 |
| 334 base::DictionaryValue params; |
| 335 params.SetString("targetId", target_id); |
| 336 std::unique_ptr<base::DictionaryValue> result; |
| 337 status = devtools_websocket_client_->SendCommandAndGetResult( |
| 338 "Browser.getWindowForTarget", params, &result); |
| 339 if (status.IsError()) |
| 340 return status; |
| 341 |
| 342 return ParseWindow(std::move(result), window); |
| 343 } |
| 344 |
| 345 Status ChromeDesktopImpl::SetWindowState(int window_id, |
| 346 const std::string& window_state) { |
| 347 Status status = devtools_websocket_client_->ConnectIfNecessary(); |
| 348 if (status.IsError()) |
| 349 return status; |
| 350 |
| 351 base::DictionaryValue params; |
| 352 params.SetInteger("windowId", window_id); |
| 353 auto bounds_object = base::MakeUnique<base::DictionaryValue>(); |
| 354 bounds_object->SetString("windowState", window_state); |
| 355 params.Set("bounds", std::move(bounds_object)); |
| 356 status = devtools_websocket_client_->SendCommand("Browser.setWindowBounds", |
| 357 params); |
| 358 if (status.IsError()) |
| 359 return status; |
| 360 |
| 361 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); |
| 362 return Status(kOk); |
| 363 } |
| 364 |
| 365 Status ChromeDesktopImpl::SetWindowBounds( |
| 366 const std::string& target_id, |
| 367 std::unique_ptr<base::DictionaryValue> bounds) { |
| 368 Window window; |
| 369 Status status = GetWindow(target_id, &window); |
| 370 if (status.IsError()) |
| 371 return status; |
| 372 |
| 373 if (window.state != "normal") { |
| 374 status = SetWindowState(window.id, "normal"); |
| 375 if (status.IsError()) |
| 376 return status; |
| 377 } |
| 378 |
| 379 base::DictionaryValue params; |
| 380 params.SetInteger("windowId", window.id); |
| 381 params.Set("bounds", std::move(bounds)); |
| 382 status = devtools_websocket_client_->SendCommand("Browser.setWindowBounds", |
| 383 params); |
| 384 if (status.IsError()) |
| 385 return status; |
| 386 |
| 387 return Status(kOk); |
| 388 } |
OLD | NEW |