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/test/webdriver/webdriver_basic_types.h" 6 7 #include <cmath> 8 9 #include "base/values.h" 10 11 using base::Value; 12 13 namespace webdriver { 14 15 Point::Point() { } 16 17 Point::Point(double x, double y) : x_(x), y_(y) { } 18 19 Point::~Point() { } 20 21 void Point::Offset(double x, double y) { 22 x_ += x; 23 y_ += y; 24 } 25 26 double Point::x() const { 27 return x_; 28 } 29 30 double Point::y() const { 31 return y_; 32 } 33 34 int Point::rounded_x() const { 35 int truncated = static_cast<int>(x_); 36 if (std::abs(x_ - truncated) < 0.5) 37 return truncated; 38 return truncated + 1; 39 } 40 41 int Point::rounded_y() const { 42 int truncated = static_cast<int>(y_); 43 if (std::abs(y_ - truncated) < 0.5) 44 return truncated; 45 return truncated + 1; 46 } 47 48 Size::Size() { } 49 50 Size::Size(double width, double height) : width_(width), height_(height) { } 51 52 Size::~Size() { } 53 54 double Size::width() const { 55 return width_; 56 } 57 58 double Size::height() const { 59 return height_; 60 } 61 62 Rect::Rect() { } 63 64 Rect::Rect(double x, double y, double width, double height) 65 : origin_(x, y), size_(width, height) { } 66 67 Rect::Rect(const Point& origin, const Size& size) 68 : origin_(origin), size_(size) { } 69 70 Rect::~Rect() { } 71 72 const Point& Rect::origin() const { 73 return origin_; 74 } 75 76 const Size& Rect::size() const { 77 return size_; 78 } 79 80 double Rect::x() const { 81 return origin_.x(); 82 } 83 84 double Rect::y() const { 85 return origin_.y(); 86 } 87 88 double Rect::width() const { 89 return size_.width(); 90 } 91 92 double Rect::height() const { 93 return size_.height(); 94 } 95 96 } // namespace webdriver 97 98 Value* ValueConversionTraits<webdriver::Point>::CreateValueFrom( 99 const webdriver::Point& t) { 100 DictionaryValue* value = new DictionaryValue(); 101 value->SetDouble("x", t.x()); 102 value->SetDouble("y", t.y()); 103 return value; 104 } 105 106 bool ValueConversionTraits<webdriver::Point>::SetFromValue( 107 const Value* value, webdriver::Point* t) { 108 if (!value->IsType(Value::TYPE_DICTIONARY)) 109 return false; 110 111 const DictionaryValue* dict_value = 112 static_cast<const DictionaryValue*>(value); 113 double x, y; 114 if (!dict_value->GetDouble("x", &x) || 115 !dict_value->GetDouble("y", &y)) 116 return false; 117 *t = webdriver::Point(x, y); 118 return true; 119 } 120 121 bool ValueConversionTraits<webdriver::Point>::CanConvert(const Value* value) { 122 webdriver::Point t; 123 return SetFromValue(value, &t); 124 } 125 126 Value* ValueConversionTraits<webdriver::Size>::CreateValueFrom( 127 const webdriver::Size& t) { 128 DictionaryValue* value = new DictionaryValue(); 129 value->SetDouble("width", t.width()); 130 value->SetDouble("height", t.height()); 131 return value; 132 } 133 134 bool ValueConversionTraits<webdriver::Size>::SetFromValue( 135 const Value* value, webdriver::Size* t) { 136 if (!value->IsType(Value::TYPE_DICTIONARY)) 137 return false; 138 139 const DictionaryValue* dict_value = 140 static_cast<const DictionaryValue*>(value); 141 double width, height; 142 if (!dict_value->GetDouble("width", &width) || 143 !dict_value->GetDouble("height", &height)) 144 return false; 145 *t = webdriver::Size(width, height); 146 return true; 147 } 148 149 bool ValueConversionTraits<webdriver::Size>::CanConvert(const Value* value) { 150 webdriver::Size t; 151 return SetFromValue(value, &t); 152 } 153 154 Value* ValueConversionTraits<webdriver::Rect>::CreateValueFrom( 155 const webdriver::Rect& t) { 156 DictionaryValue* value = new DictionaryValue(); 157 value->SetDouble("left", t.x()); 158 value->SetDouble("top", t.y()); 159 value->SetDouble("width", t.width()); 160 value->SetDouble("height", t.height()); 161 return value; 162 } 163 164 bool ValueConversionTraits<webdriver::Rect>::SetFromValue( 165 const Value* value, webdriver::Rect* t) { 166 if (!value->IsType(Value::TYPE_DICTIONARY)) 167 return false; 168 169 const DictionaryValue* dict_value = 170 static_cast<const DictionaryValue*>(value); 171 double x, y, width, height; 172 if (!dict_value->GetDouble("left", &x) || 173 !dict_value->GetDouble("top", &y) || 174 !dict_value->GetDouble("width", &width) || 175 !dict_value->GetDouble("height", &height)) 176 return false; 177 *t = webdriver::Rect(x, y, width, height); 178 return true; 179 } 180 181 bool ValueConversionTraits<webdriver::Rect>::CanConvert(const Value* value) { 182 webdriver::Rect t; 183 return SetFromValue(value, &t); 184 } 185