1 // Copyright (c) 2012 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/webdriver/commands/window_commands.h" 6 7 #include "base/values.h" 8 #include "chrome/test/webdriver/commands/response.h" 9 #include "chrome/test/webdriver/webdriver_error.h" 10 #include "chrome/test/webdriver/webdriver_session.h" 11 #include "chrome/test/webdriver/webdriver_util.h" 12 13 using base::Value; 14 15 namespace webdriver { 16 17 namespace { 18 19 bool GetWindowId(const std::string& window_id_string, 20 const WebViewId& current_id, 21 WebViewId* window_id, 22 Response* const response) { 23 if (window_id_string == "current") { 24 *window_id = current_id; 25 } else if (!StringToWebViewId(window_id_string, window_id)) { 26 response->SetError( 27 new Error(kBadRequest, "Invalid window ID")); 28 return false; 29 } 30 return true; 31 } 32 33 } 34 35 WindowSizeCommand::WindowSizeCommand( 36 const std::vector<std::string>& path_segments, 37 const base::DictionaryValue* parameters) 38 : WebDriverCommand(path_segments, parameters) { 39 } 40 41 WindowSizeCommand::~WindowSizeCommand() { 42 } 43 44 bool WindowSizeCommand::DoesGet() { 45 return true; 46 } 47 48 bool WindowSizeCommand::DoesPost() { 49 return true; 50 } 51 52 void WindowSizeCommand::ExecuteGet(Response* const response) { 53 // Path segment: "/session/$sessionId/window/$windowHandle/size" 54 WebViewId window_id; 55 WebViewId current_id = session_->current_target().view_id; 56 if (!GetWindowId(GetPathVariable(4), current_id, &window_id, response)) 57 return; 58 59 Rect bounds; 60 Error* error = session_->GetWindowBounds(window_id, &bounds); 61 if (error) { 62 response->SetError(error); 63 return; 64 } 65 base::DictionaryValue* size = new base::DictionaryValue(); 66 size->SetInteger("width", bounds.width()); 67 size->SetInteger("height", bounds.height()); 68 response->SetValue(size); 69 } 70 71 void WindowSizeCommand::ExecutePost(Response* const response) { 72 // Path segment: "/session/$sessionId/window/$windowHandle/size" 73 WebViewId window_id; 74 WebViewId current_id = session_->current_target().view_id; 75 if (!GetWindowId(GetPathVariable(4), current_id, &window_id, response)) 76 return; 77 78 int width, height; 79 if (!GetIntegerParameter("width", &width) || 80 !GetIntegerParameter("height", &height)) { 81 response->SetError(new Error( 82 kBadRequest, 83 "Missing or invalid 'width' or 'height' parameters")); 84 return; 85 } 86 Rect bounds; 87 Error* error = session_->GetWindowBounds(window_id, &bounds); 88 if (!error) { 89 bounds = Rect(bounds.origin(), Size(width, height)); 90 error = session_->SetWindowBounds(window_id, bounds); 91 } 92 if (error) { 93 response->SetError(error); 94 return; 95 } 96 } 97 98 WindowPositionCommand::WindowPositionCommand( 99 const std::vector<std::string>& path_segments, 100 const base::DictionaryValue* parameters) 101 : WebDriverCommand(path_segments, parameters) { 102 } 103 104 WindowPositionCommand::~WindowPositionCommand() { 105 } 106 107 bool WindowPositionCommand::DoesGet() { 108 return true; 109 } 110 111 bool WindowPositionCommand::DoesPost() { 112 return true; 113 } 114 115 void WindowPositionCommand::ExecuteGet(Response* const response) { 116 // Path segment: "/session/$sessionId/window/$windowHandle/position" 117 WebViewId window_id; 118 WebViewId current_id = session_->current_target().view_id; 119 if (!GetWindowId(GetPathVariable(4), current_id, &window_id, response)) 120 return; 121 122 Rect bounds; 123 Error* error = session_->GetWindowBounds(window_id, &bounds); 124 if (error) { 125 response->SetError(error); 126 return; 127 } 128 base::DictionaryValue* size = new base::DictionaryValue(); 129 size->SetInteger("x", bounds.x()); 130 size->SetInteger("y", bounds.y()); 131 response->SetValue(size); 132 } 133 134 void WindowPositionCommand::ExecutePost(Response* const response) { 135 // Path segment: "/session/$sessionId/window/$windowHandle/position" 136 WebViewId window_id; 137 WebViewId current_id = session_->current_target().view_id; 138 if (!GetWindowId(GetPathVariable(4), current_id, &window_id, response)) 139 return; 140 141 int x, y; 142 if (!GetIntegerParameter("x", &x) || 143 !GetIntegerParameter("y", &y)) { 144 response->SetError(new Error( 145 kBadRequest, 146 "Missing or invalid 'x' or 'y' parameters")); 147 return; 148 } 149 Rect bounds; 150 Error* error = session_->GetWindowBounds(window_id, &bounds); 151 if (!error) { 152 bounds = Rect(Point(x, y), bounds.size()); 153 error = session_->SetWindowBounds(window_id, bounds); 154 } 155 if (error) { 156 response->SetError(error); 157 return; 158 } 159 } 160 161 WindowMaximizeCommand::WindowMaximizeCommand( 162 const std::vector<std::string>& path_segments, 163 const base::DictionaryValue* parameters) 164 : WebDriverCommand(path_segments, parameters) { 165 } 166 167 WindowMaximizeCommand::~WindowMaximizeCommand() { 168 } 169 170 bool WindowMaximizeCommand::DoesPost() { 171 return true; 172 } 173 174 void WindowMaximizeCommand::ExecutePost(Response* const response) { 175 // Path segment: "/session/$sessionId/window/$windowHandle/maximize" 176 WebViewId window_id; 177 WebViewId current_id = session_->current_target().view_id; 178 if (!GetWindowId(GetPathVariable(4), current_id, &window_id, response)) 179 return; 180 181 Error* error = session_->MaximizeWindow(window_id); 182 if (error) { 183 response->SetError(error); 184 return; 185 } 186 } 187 188 } // namespace webdriver 189