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/ui/javascript_test_util.h" 6 7 #include "base/json/json_string_value_serializer.h" 8 #include "base/logging.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/utf_string_conversions.h" 12 #include "base/values.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 JsonDictionaryToMap(const std::string & json,std::map<std::string,std::string> * results)15bool JsonDictionaryToMap(const std::string& json, 16 std::map<std::string, std::string>* results) { 17 DCHECK(results != NULL); 18 JSONStringValueSerializer deserializer(json); 19 scoped_ptr<Value> root(deserializer.Deserialize(NULL, NULL)); 20 21 // Note that we don't use ASSERT_TRUE here (and in some other places) as it 22 // doesn't work inside a function with a return type other than void. 23 EXPECT_TRUE(root.get()); 24 if (!root.get()) 25 return false; 26 27 EXPECT_TRUE(root->IsType(Value::TYPE_DICTIONARY)); 28 if (!root->IsType(Value::TYPE_DICTIONARY)) 29 return false; 30 31 DictionaryValue* dict = static_cast<DictionaryValue*>(root.get()); 32 33 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { 34 double double_result; 35 std::string result; 36 if (it.value().GetAsString(&result)) { 37 } else if (it.value().GetAsDouble(&double_result)) { 38 result = base::DoubleToString(double_result); 39 } else { 40 NOTREACHED() << "Value type not supported!"; 41 return false; 42 } 43 results->insert(std::make_pair(it.key(), result)); 44 } 45 46 return true; 47 } 48