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 3// found in the LICENSE file. 4 5#include "chrome/test/chromedriver/chrome/automation_extension.h" 6 7#include "base/time/time.h" 8#include "base/values.h" 9#include "chrome/test/chromedriver/chrome/status.h" 10#include "chrome/test/chromedriver/chrome/web_view.h" 11 12AutomationExtension::AutomationExtension(scoped_ptr<WebView> web_view) 13 : web_view_(web_view.Pass()) {} 14 15AutomationExtension::~AutomationExtension() {} 16 17Status AutomationExtension::CaptureScreenshot(std::string* screenshot) { 18 base::ListValue args; 19 scoped_ptr<base::Value> result; 20 Status status = web_view_->CallAsyncFunction( 21 std::string(), 22 "captureScreenshot", 23 args, 24 base::TimeDelta::FromSeconds(10), 25 &result); 26 if (status.IsError()) 27 return Status(status.code(), "cannot take screenshot", status); 28 if (!result->GetAsString(screenshot)) 29 return Status(kUnknownError, "screenshot is not a string"); 30 return Status(kOk); 31} 32 33Status AutomationExtension::GetWindowPosition(int* x, int* y) { 34 int temp_width, temp_height; 35 return GetWindowInfo(x, y, &temp_width, &temp_height); 36} 37 38Status AutomationExtension::SetWindowPosition(int x, int y) { 39 base::DictionaryValue update_info; 40 update_info.SetInteger("left", x); 41 update_info.SetInteger("top", y); 42 update_info.SetString("state", "normal"); 43 return UpdateWindow(update_info); 44} 45 46Status AutomationExtension::GetWindowSize(int* width, int* height) { 47 int temp_x, temp_y; 48 return GetWindowInfo(&temp_x, &temp_y, width, height); 49} 50 51Status AutomationExtension::SetWindowSize(int width, int height) { 52 base::DictionaryValue update_info; 53 update_info.SetInteger("width", width); 54 update_info.SetInteger("height", height); 55 update_info.SetString("state", "normal"); 56 return UpdateWindow(update_info); 57} 58 59Status AutomationExtension::MaximizeWindow() { 60 base::DictionaryValue update_info; 61 update_info.SetString("state", "maximized"); 62 return UpdateWindow(update_info); 63} 64 65Status AutomationExtension::GetWindowInfo(int* x, 66 int* y, 67 int* width, 68 int* height) { 69 base::ListValue args; 70 scoped_ptr<base::Value> result; 71 Status status = web_view_->CallAsyncFunction(std::string(), 72 "getWindowInfo", 73 args, 74 base::TimeDelta::FromSeconds(10), 75 &result); 76 if (status.IsError()) 77 return status; 78 79 int temp_x, temp_y, temp_width, temp_height; 80 base::DictionaryValue* dict; 81 if (!result->GetAsDictionary(&dict) || 82 !dict->GetInteger("left", &temp_x) || 83 !dict->GetInteger("top", &temp_y) || 84 !dict->GetInteger("width", &temp_width) || 85 !dict->GetInteger("height", &temp_height)) { 86 return Status(kUnknownError, "received invalid window info"); 87 } 88 *x = temp_x; 89 *y = temp_y; 90 *width = temp_width; 91 *height = temp_height; 92 return Status(kOk); 93} 94 95Status AutomationExtension::UpdateWindow( 96 const base::DictionaryValue& update_info) { 97 base::ListValue args; 98 args.Append(update_info.DeepCopy()); 99 scoped_ptr<base::Value> result; 100 return web_view_->CallAsyncFunction(std::string(), 101 "updateWindow", 102 args, 103 base::TimeDelta::FromSeconds(10), 104 &result); 105} 106