| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/test/webdriver/webdriver_util.h" | 5 #include "chrome/test/webdriver/webdriver_util.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 14 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
| 15 #include "base/string_split.h" |
| 15 #include "base/third_party/icu/icu_utf.h" | 16 #include "base/third_party/icu/icu_utf.h" |
| 16 #include "chrome/common/automation_id.h" | 17 #include "chrome/common/automation_id.h" |
| 17 #include "chrome/test/automation/automation_json_requests.h" | 18 #include "chrome/test/automation/automation_json_requests.h" |
| 18 | 19 |
| 19 using base::DictionaryValue; | 20 using base::DictionaryValue; |
| 20 using base::ListValue; | 21 using base::ListValue; |
| 21 using base::Value; | 22 using base::Value; |
| 22 | 23 |
| 23 namespace webdriver { | 24 namespace webdriver { |
| 24 | 25 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 case Value::TYPE_BINARY: | 115 case Value::TYPE_BINARY: |
| 115 return "binary"; | 116 return "binary"; |
| 116 case Value::TYPE_DICTIONARY: | 117 case Value::TYPE_DICTIONARY: |
| 117 return "dictionary"; | 118 return "dictionary"; |
| 118 case Value::TYPE_LIST: | 119 case Value::TYPE_LIST: |
| 119 return "list"; | 120 return "list"; |
| 120 } | 121 } |
| 121 return "unknown"; | 122 return "unknown"; |
| 122 } | 123 } |
| 123 | 124 |
| 125 std::string AutomationIdToString(const AutomationId& id) { |
| 126 return base::StringPrintf("%d-%s", id.type(), id.id().c_str()); |
| 127 } |
| 128 |
| 124 bool StringToAutomationId(const std::string& string_id, AutomationId* id) { | 129 bool StringToAutomationId(const std::string& string_id, AutomationId* id) { |
| 125 scoped_ptr<Value> value(base::JSONReader::Read(string_id, false)); | 130 std::vector<std::string> split_id; |
| 126 std::string error_msg; | 131 base::SplitString(string_id, '-', &split_id); |
| 127 return value.get() && AutomationId::FromValue(value.get(), id, &error_msg); | 132 if (split_id.size() != 2) |
| 133 return false; |
| 134 int type; |
| 135 if (!base::StringToInt(split_id[0], &type)) |
| 136 return false; |
| 137 *id = AutomationId(static_cast<AutomationId::Type>(type), split_id[1]); |
| 138 return true; |
| 128 } | 139 } |
| 129 | 140 |
| 130 std::string WebViewIdToString(const WebViewId& view_id) { | 141 std::string WebViewIdToString(const WebViewId& view_id) { |
| 131 DictionaryValue* dict = view_id.GetId().ToValue(); | 142 return base::StringPrintf( |
| 132 if (view_id.old_style()) | 143 "%s%s", |
| 133 dict->SetBoolean("old_style", true); | 144 view_id.old_style() ? "t" : "f", |
| 134 return JsonStringify(dict); | 145 AutomationIdToString(view_id.GetId()).c_str()); |
| 135 } | 146 } |
| 136 | 147 |
| 137 bool StringToWebViewId(const std::string& string_id, WebViewId* view_id) { | 148 bool StringToWebViewId(const std::string& string_id, WebViewId* view_id) { |
| 138 scoped_ptr<Value> value(base::JSONReader::Read(string_id, false)); | 149 if (string_id.empty() || (string_id[0] != 'f' && string_id[0] != 't')) |
| 150 return false; |
| 151 bool old_style = string_id[0] == 't'; |
| 139 AutomationId id; | 152 AutomationId id; |
| 140 std::string error_msg; | 153 if (!StringToAutomationId(string_id.substr(1), &id)) |
| 141 DictionaryValue* dict; | |
| 142 if (!value.get() || !AutomationId::FromValue(value.get(), &id, &error_msg) || | |
| 143 !value->GetAsDictionary(&dict)) | |
| 144 return false; | 154 return false; |
| 145 | 155 |
| 146 bool old_style = false; | |
| 147 dict->GetBoolean("old_style", &old_style); | |
| 148 | |
| 149 if (old_style) { | 156 if (old_style) { |
| 150 int tab_id; | 157 int tab_id; |
| 151 if (!base::StringToInt(id.id(), &tab_id)) | 158 if (!base::StringToInt(id.id(), &tab_id)) |
| 152 return false; | 159 return false; |
| 153 *view_id = WebViewId::ForOldStyleTab(tab_id); | 160 *view_id = WebViewId::ForOldStyleTab(tab_id); |
| 154 } else { | 161 } else { |
| 155 *view_id = WebViewId::ForView(id); | 162 *view_id = WebViewId::ForView(id); |
| 156 } | 163 } |
| 157 return true; | 164 return true; |
| 158 } | 165 } |
| (...skipping 23 matching lines...) Expand all Loading... |
| 182 | 189 |
| 183 bool ValueConversionTraits<webdriver::SkipParsing>::SetFromValue( | 190 bool ValueConversionTraits<webdriver::SkipParsing>::SetFromValue( |
| 184 const Value* value, const webdriver::SkipParsing* t) { | 191 const Value* value, const webdriver::SkipParsing* t) { |
| 185 return true; | 192 return true; |
| 186 } | 193 } |
| 187 | 194 |
| 188 bool ValueConversionTraits<webdriver::SkipParsing>::CanConvert( | 195 bool ValueConversionTraits<webdriver::SkipParsing>::CanConvert( |
| 189 const Value* value) { | 196 const Value* value) { |
| 190 return true; | 197 return true; |
| 191 } | 198 } |
| OLD | NEW |