Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(884)

Side by Side Diff: chrome/test/chromedriver/chrome/chrome_desktop_impl.cc

Issue 2826393002: use devtools command to do window management (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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(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(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(std::string window, int x, int y) {
265 auto bounds = base::MakeUnique<base::DictionaryValue>();
266 bounds->SetInteger("left", x);
267 bounds->SetInteger("top", y);
268 return SetWindowBounds(window, std::move(bounds));
269 }
270
271 Status ChromeDesktopImpl::SetWindowSize(std::string window,
272 int width,
273 int height) {
274 auto bounds = base::MakeUnique<base::DictionaryValue>();
275 bounds->SetInteger("width", width);
276 bounds->SetInteger("height", height);
277 return SetWindowBounds(window, std::move(bounds));
278 }
279
280 Status ChromeDesktopImpl::MaximizeWindow(std::string target_id) {
281 Window window;
282 Status status = GetWindow(target_id, &window);
283 if (status.IsError())
284 return status;
285
286 if (window.state == "maximized")
287 return Status(kOk);
288
289 if (window.state != "normal") {
290 // always restore window to normal first.
stgao 2017/04/21 05:06:31 Maybe some more detail on why.
jzfeng 2017/04/21 07:25:54 Done.
291 status = SetWindowState(window.id, "normal");
292 if (status.IsError())
293 return status;
294 }
295
296 return SetWindowState(window.id, "maximized");
297 }
298
299 Status ChromeDesktopImpl::ParseWindow(
300 std::unique_ptr<base::DictionaryValue> params,
301 Window* window) {
302 if (!params->GetInteger("windowId", &window->id))
303 return Status(kUnknownError, "no window id in response");
304
305 const base::Value* value = nullptr;
306 const base::DictionaryValue* bounds_dict = nullptr;
307 if (!params->Get("bounds", &value) || !value->GetAsDictionary(&bounds_dict))
308 return Status(kUnknownError, "no window bounds in response");
309
310 if (!bounds_dict->GetString("windowState", &window->state))
311 return Status(kUnknownError, "no window state in window bounds");
312
313 if (!bounds_dict->GetInteger("left", &window->left))
314 return Status(kUnknownError, "no left offset in window bounds");
315 if (!bounds_dict->GetInteger("top", &window->top))
316 return Status(kUnknownError, "no top offset in window bounds");
317 if (!bounds_dict->GetInteger("width", &window->width))
318 return Status(kUnknownError, "no width in window bounds");
319 if (!bounds_dict->GetInteger("height", &window->height))
320 return Status(kUnknownError, "no height in window bounds");
321
322 return Status(kOk);
323 }
324
325 Status ChromeDesktopImpl::GetWindow(std::string target_id, Window* window) {
326 Status status = devtools_websocket_client_->ConnectIfNecessary();
327 if (status.IsError())
328 return status;
329
330 base::DictionaryValue params;
331 params.SetString("targetId", target_id);
332 std::unique_ptr<base::DictionaryValue> result;
333 status = devtools_websocket_client_->SendCommandAndGetResult(
334 "Browser.getWindowForTarget", params, &result);
335 if (status.IsError())
336 return status;
337
338 return ParseWindow(std::move(result), window);
339 }
340
341 Status ChromeDesktopImpl::SetWindowState(int window_id,
342 std::string window_state) {
343 Status status = devtools_websocket_client_->ConnectIfNecessary();
344 if (status.IsError())
345 return status;
346
347 base::DictionaryValue params;
348 params.SetInteger("windowId", window_id);
349 auto bounds_object = base::MakeUnique<base::DictionaryValue>();
350 bounds_object->SetString("windowState", window_state);
351 params.Set("bounds", std::move(bounds_object));
352 status = devtools_websocket_client_->SendCommand("Browser.setWindowBounds",
353 params);
354 if (status.IsError())
355 return status;
356
357 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
358 return Status(kOk);
359 }
360
361 Status ChromeDesktopImpl::SetWindowBounds(
362 std::string target_id,
363 std::unique_ptr<base::DictionaryValue> bounds) {
364 Window window;
365 Status status = GetWindow(target_id, &window);
366 if (status.IsError())
367 return status;
368
369 if (window.state != "normal") {
370 status = SetWindowState(window.id, "normal");
371 if (status.IsError())
372 return status;
373 }
374
375 base::DictionaryValue params;
376 params.SetInteger("windowId", window.id);
377 params.Set("bounds", std::move(bounds));
378 status = devtools_websocket_client_->SendCommand("Browser.setWindowBounds",
379 params);
380 if (status.IsError())
381 return status;
382
383 return Status(kOk);
384 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698