Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / test / chromedriver / chrome / automation_extension.cc
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
12 AutomationExtension::AutomationExtension(scoped_ptr<WebView> web_view)
13     : web_view_(web_view.Pass()) {}
14
15 AutomationExtension::~AutomationExtension() {}
16
17 Status 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
33 Status AutomationExtension::GetWindowPosition(int* x, int* y) {
34   int temp_width, temp_height;
35   return GetWindowInfo(x, y, &temp_width, &temp_height);
36 }
37
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);
44 }
45
46 Status AutomationExtension::GetWindowSize(int* width, int* height) {
47   int temp_x, temp_y;
48   return GetWindowInfo(&temp_x, &temp_y, width, height);
49 }
50
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);
57 }
58
59 Status AutomationExtension::MaximizeWindow() {
60   base::DictionaryValue update_info;
61   update_info.SetString("state", "maximized");
62   return UpdateWindow(update_info);
63 }
64
65 Status 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   base::DictionaryValue* dict;
80   int temp_x = 0;
81   int temp_y = 0;
82   int temp_width = 0;
83   int temp_height = 0;
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");
90   }
91   *x = temp_x;
92   *y = temp_y;
93   *width = temp_width;
94   *height = temp_height;
95   return Status(kOk);
96 }
97
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(),
104                                       "updateWindow",
105                                       args,
106                                       base::TimeDelta::FromSeconds(10),
107                                       &result);
108 }
109
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(),
115                                       "launchApp",
116                                       args,
117                                       base::TimeDelta::FromSeconds(10),
118                                       &result);
119 }