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.
5 #include "chrome/test/chromedriver/chrome/automation_extension.h"
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"
12 AutomationExtension::AutomationExtension(scoped_ptr<WebView> web_view)
13 : web_view_(web_view.Pass()) {}
15 AutomationExtension::~AutomationExtension() {}
17 Status AutomationExtension::CaptureScreenshot(std::string* screenshot) {
19 scoped_ptr<base::Value> result;
20 Status status = web_view_->CallAsyncFunction(
24 base::TimeDelta::FromSeconds(10),
27 return Status(status.code(), "cannot take screenshot", status);
28 if (!result->GetAsString(screenshot))
29 return Status(kUnknownError, "screenshot is not a string");
33 Status AutomationExtension::GetWindowPosition(int* x, int* y) {
34 int temp_width, temp_height;
35 return GetWindowInfo(x, y, &temp_width, &temp_height);
38 Status 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);
46 Status AutomationExtension::GetWindowSize(int* width, int* height) {
48 return GetWindowInfo(&temp_x, &temp_y, width, height);
51 Status 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);
59 Status AutomationExtension::MaximizeWindow() {
60 base::DictionaryValue update_info;
61 update_info.SetString("state", "maximized");
62 return UpdateWindow(update_info);
65 Status AutomationExtension::GetWindowInfo(int* x,
70 scoped_ptr<base::Value> result;
71 Status status = web_view_->CallAsyncFunction(std::string(),
74 base::TimeDelta::FromSeconds(10),
79 base::DictionaryValue* dict;
84 if (!result->GetAsDictionary(&dict) ||
85 !dict->GetInteger("left", &temp_x) ||
86 !dict->GetInteger("top", &temp_y) ||
87 !dict->GetInteger("width", &temp_width) ||
88 !dict->GetInteger("height", &temp_height)) {
89 return Status(kUnknownError, "received invalid window info");
94 *height = temp_height;
98 Status AutomationExtension::UpdateWindow(
99 const base::DictionaryValue& update_info) {
100 base::ListValue args;
101 args.Append(update_info.DeepCopy());
102 scoped_ptr<base::Value> result;
103 return web_view_->CallAsyncFunction(std::string(),
106 base::TimeDelta::FromSeconds(10),
110 Status AutomationExtension::LaunchApp(std::string id) {
111 base::ListValue args;
112 args.AppendString(id);
113 scoped_ptr<base::Value> result;
114 return web_view_->CallAsyncFunction(std::string(),
117 base::TimeDelta::FromSeconds(10),