1// Copyright (c) 2011 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/browser/automation/testing_automation_provider.h"
6
7#include <windows.h>
8
9#include "base/string_util.h"
10#include "base/utf_string_conversions.h"
11#include "chrome/browser/automation/automation_browser_tracker.h"
12#include "chrome/browser/automation/automation_window_tracker.h"
13#include "chrome/browser/ui/browser_window.h"
14
15void TestingAutomationProvider::ActivateWindow(int handle) {
16  if (window_tracker_->ContainsHandle(handle)) {
17    ::SetActiveWindow(window_tracker_->GetResource(handle));
18  }
19}
20
21void TestingAutomationProvider::IsWindowMaximized(int handle,
22                                                  bool* is_maximized,
23                                                  bool* success) {
24  *success = false;
25
26  HWND hwnd = window_tracker_->GetResource(handle);
27  if (hwnd) {
28    *success = true;
29    WINDOWPLACEMENT window_placement;
30    GetWindowPlacement(hwnd, &window_placement);
31    *is_maximized = (window_placement.showCmd == SW_MAXIMIZE);
32  }
33}
34
35void TestingAutomationProvider::TerminateSession(int handle, bool* success) {
36  *success = false;
37
38  if (browser_tracker_->ContainsHandle(handle)) {
39    Browser* browser = browser_tracker_->GetResource(handle);
40    HWND window = browser->window()->GetNativeHandle();
41    *success = (::PostMessageW(window, WM_ENDSESSION, 0, 0) == TRUE);
42  }
43}
44
45void TestingAutomationProvider::GetWindowBounds(int handle,
46                                                gfx::Rect* bounds,
47                                                bool* success) {
48  *success = false;
49  HWND hwnd = window_tracker_->GetResource(handle);
50  if (hwnd) {
51    *success = true;
52    WINDOWPLACEMENT window_placement;
53    GetWindowPlacement(hwnd, &window_placement);
54    *bounds = window_placement.rcNormalPosition;
55  }
56}
57
58void TestingAutomationProvider::SetWindowBounds(int handle,
59                                                const gfx::Rect& bounds,
60                                                bool* success) {
61  *success = false;
62  if (window_tracker_->ContainsHandle(handle)) {
63    HWND hwnd = window_tracker_->GetResource(handle);
64    if (::MoveWindow(hwnd, bounds.x(), bounds.y(), bounds.width(),
65                     bounds.height(), true)) {
66      *success = true;
67    }
68  }
69}
70
71void TestingAutomationProvider::SetWindowVisible(int handle,
72                                                 bool visible,
73                                                 bool* result) {
74  if (window_tracker_->ContainsHandle(handle)) {
75    HWND hwnd = window_tracker_->GetResource(handle);
76    ::ShowWindow(hwnd, visible ? SW_SHOW : SW_HIDE);
77    *result = true;
78  } else {
79    *result = false;
80  }
81}
82
83void TestingAutomationProvider::GetWindowTitle(int handle, string16* text) {
84  gfx::NativeWindow window = window_tracker_->GetResource(handle);
85  std::wstring result;
86  int length = ::GetWindowTextLength(window) + 1;
87  ::GetWindowText(window, WriteInto(&result, length), length);
88  text->assign(WideToUTF16(result));
89}
90
91