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::GetWindowPosition(int* x, int* y) {
18  int temp_width, temp_height;
19  return GetWindowInfo(x, y, &temp_width, &temp_height);
20}
21
22Status AutomationExtension::SetWindowPosition(int x, int y) {
23  base::DictionaryValue update_info;
24  update_info.SetInteger("left", x);
25  update_info.SetInteger("top", y);
26  update_info.SetString("state", "normal");
27  return UpdateWindow(update_info);
28}
29
30Status AutomationExtension::GetWindowSize(int* width, int* height) {
31  int temp_x, temp_y;
32  return GetWindowInfo(&temp_x, &temp_y, width, height);
33}
34
35Status AutomationExtension::SetWindowSize(int width, int height) {
36  base::DictionaryValue update_info;
37  update_info.SetInteger("width", width);
38  update_info.SetInteger("height", height);
39  update_info.SetString("state", "normal");
40  return UpdateWindow(update_info);
41}
42
43Status AutomationExtension::MaximizeWindow() {
44  base::DictionaryValue update_info;
45  update_info.SetString("state", "maximized");
46  return UpdateWindow(update_info);
47}
48
49Status AutomationExtension::GetWindowInfo(int* x,
50                                          int* y,
51                                          int* width,
52                                          int* height) {
53  base::ListValue args;
54  scoped_ptr<base::Value> result;
55  Status status = web_view_->CallAsyncFunction(std::string(),
56                                               "getWindowInfo",
57                                               args,
58                                               base::TimeDelta::FromSeconds(10),
59                                               &result);
60  if (status.IsError())
61    return status;
62
63  int temp_x, temp_y, temp_width, temp_height;
64  base::DictionaryValue* dict;
65  if (!result->GetAsDictionary(&dict) ||
66      !dict->GetInteger("left", &temp_x) ||
67      !dict->GetInteger("top", &temp_y) ||
68      !dict->GetInteger("width", &temp_width) ||
69      !dict->GetInteger("height", &temp_height)) {
70    return Status(kUnknownError, "received invalid window info");
71  }
72  *x = temp_x;
73  *y = temp_y;
74  *width = temp_width;
75  *height = temp_height;
76  return Status(kOk);
77}
78
79Status AutomationExtension::UpdateWindow(
80    const base::DictionaryValue& update_info) {
81  base::ListValue args;
82  args.Append(update_info.DeepCopy());
83  scoped_ptr<base::Value> result;
84  return web_view_->CallAsyncFunction(std::string(),
85                                      "updateWindow",
86                                      args,
87                                      base::TimeDelta::FromSeconds(10),
88                                      &result);
89}
90