Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "ppapi/shared_impl/var_value_conversions.h" | 5 #include "ppapi/shared_impl/var_value_conversions.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 #include <cstring> | 8 #include <cstring> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "ppapi/c/pp_bool.h" | 14 #include "ppapi/c/pp_bool.h" |
| 15 #include "ppapi/c/pp_var.h" | 15 #include "ppapi/c/pp_var.h" |
| 16 #include "ppapi/shared_impl/array_var.h" | |
| 16 #include "ppapi/shared_impl/dictionary_var.h" | 17 #include "ppapi/shared_impl/dictionary_var.h" |
| 17 #include "ppapi/shared_impl/proxy_lock.h" | 18 #include "ppapi/shared_impl/proxy_lock.h" |
| 18 #include "ppapi/shared_impl/scoped_pp_var.h" | 19 #include "ppapi/shared_impl/scoped_pp_var.h" |
| 19 #include "ppapi/shared_impl/test_globals.h" | 20 #include "ppapi/shared_impl/test_globals.h" |
| 20 #include "ppapi/shared_impl/var.h" | 21 #include "ppapi/shared_impl/var.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 23 |
| 23 namespace ppapi { | 24 namespace ppapi { |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 78 !value_iter.IsAtEnd(); value_iter.Advance()) { | 79 !value_iter.IsAtEnd(); value_iter.Advance()) { |
| 79 DictionaryVar::KeyValueMap::const_iterator var_iter = | 80 DictionaryVar::KeyValueMap::const_iterator var_iter = |
| 80 dict_var->key_value_map().find(value_iter.key()); | 81 dict_var->key_value_map().find(value_iter.key()); |
| 81 if (var_iter == dict_var->key_value_map().end() || | 82 if (var_iter == dict_var->key_value_map().end() || |
| 82 !Equals(value_iter.value(), var_iter->second.get())) | 83 !Equals(value_iter.value(), var_iter->second.get())) |
| 83 return false; | 84 return false; |
| 84 } | 85 } |
| 85 return true; | 86 return true; |
| 86 } | 87 } |
| 87 case base::Value::TYPE_LIST: { | 88 case base::Value::TYPE_LIST: { |
| 88 // TODO(yzshen): add support once array var is supported. | 89 const base::ListValue& list_value = |
| 89 return false; | 90 static_cast<const base::ListValue&>(value); |
| 91 ArrayVar* array_var = ArrayVar::FromPPVar(var); | |
| 92 if (!array_var || list_value.GetSize() != array_var->elements().size()) | |
| 93 return false; | |
| 94 | |
| 95 base::ListValue::const_iterator value_iter = list_value.begin(); | |
| 96 ArrayVar::ElementVector::const_iterator var_iter = | |
| 97 array_var->elements().begin(); | |
| 98 for (; value_iter != list_value.end() && | |
| 99 var_iter != array_var->elements().end(); | |
|
raymes
2013/03/05 18:19:16
indentation here looks wrong
yzshen1
2013/03/14 22:36:48
It is 4 spaces further than "value_iter != list_va
| |
| 100 ++value_iter, ++var_iter) { | |
| 101 if (!Equals(**value_iter, var_iter->get())) | |
| 102 return false; | |
| 103 } | |
| 104 return true; | |
| 90 } | 105 } |
| 91 } | 106 } |
| 92 NOTREACHED(); | 107 NOTREACHED(); |
| 93 return false; | 108 return false; |
| 94 } | 109 } |
| 95 | 110 |
| 96 class VarValueConversionsTest : public testing::Test { | 111 class VarValueConversionsTest : public testing::Test { |
| 97 public: | 112 public: |
| 98 VarValueConversionsTest() { | 113 VarValueConversionsTest() { |
| 99 } | 114 } |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 121 { | 136 { |
| 122 // Undefined var is not allowed even if it is stored in a dictionary var. | 137 // Undefined var is not allowed even if it is stored in a dictionary var. |
| 123 scoped_refptr<DictionaryVar> dict_var(new DictionaryVar()); | 138 scoped_refptr<DictionaryVar> dict_var(new DictionaryVar()); |
| 124 ASSERT_TRUE(dict_var->SetWithStringKey("key_1", PP_MakeUndefined())); | 139 ASSERT_TRUE(dict_var->SetWithStringKey("key_1", PP_MakeUndefined())); |
| 125 ScopedPPVar var(ScopedPPVar::PassRef(), dict_var->GetPPVar()); | 140 ScopedPPVar var(ScopedPPVar::PassRef(), dict_var->GetPPVar()); |
| 126 ASSERT_EQ(NULL, CreateValueFromVar(var.get())); | 141 ASSERT_EQ(NULL, CreateValueFromVar(var.get())); |
| 127 } | 142 } |
| 128 | 143 |
| 129 { | 144 { |
| 130 // Var holding a ref to itself is not a valid input. | 145 // Var holding a ref to itself is not a valid input. |
| 131 scoped_refptr<DictionaryVar> dict_var_1(new DictionaryVar()); | 146 scoped_refptr<DictionaryVar> dict_var(new DictionaryVar()); |
| 132 ScopedPPVar var_1(ScopedPPVar::PassRef(), dict_var_1->GetPPVar()); | 147 ScopedPPVar var_1(ScopedPPVar::PassRef(), dict_var->GetPPVar()); |
| 133 scoped_refptr<DictionaryVar> dict_var_2(new DictionaryVar()); | 148 scoped_refptr<ArrayVar> array_var(new ArrayVar()); |
| 134 ScopedPPVar var_2(ScopedPPVar::PassRef(), dict_var_2->GetPPVar()); | 149 ScopedPPVar var_2(ScopedPPVar::PassRef(), array_var->GetPPVar()); |
| 135 | 150 |
| 136 ASSERT_TRUE(dict_var_1->SetWithStringKey("key_1", var_2.get())); | 151 ASSERT_TRUE(dict_var->SetWithStringKey("key_1", var_2.get())); |
| 137 scoped_ptr<base::Value> value(CreateValueFromVar(var_1.get())); | 152 scoped_ptr<base::Value> value(CreateValueFromVar(var_1.get())); |
| 138 ASSERT_TRUE(value.get() != NULL); | 153 ASSERT_TRUE(value.get()); |
| 139 | 154 |
| 140 ASSERT_TRUE(dict_var_2->SetWithStringKey("key_2", var_1.get())); | 155 ASSERT_TRUE(array_var->Set(0, var_1.get())); |
| 141 value.reset(CreateValueFromVar(var_1.get())); | 156 value.reset(CreateValueFromVar(var_1.get())); |
| 142 ASSERT_EQ(NULL, value.get()); | 157 ASSERT_EQ(NULL, value.get()); |
| 143 | 158 |
| 144 // Make sure |var_1| doesn't indirectly hold a ref to itself, otherwise it | 159 // Make sure |var_1| doesn't indirectly hold a ref to itself, otherwise it |
| 145 // is leaked. | 160 // is leaked. |
| 146 dict_var_1->DeleteWithStringKey("key_1"); | 161 dict_var->DeleteWithStringKey("key_1"); |
| 147 } | 162 } |
| 148 | 163 |
| 149 { | 164 { |
| 150 // Test valid inputs. | 165 // Test valid inputs. |
| 151 scoped_refptr<DictionaryVar> dict_var_1(new DictionaryVar()); | 166 scoped_refptr<DictionaryVar> dict_var_1(new DictionaryVar()); |
| 152 ScopedPPVar dict_pp_var_1(ScopedPPVar::PassRef(), dict_var_1->GetPPVar()); | 167 ScopedPPVar dict_pp_var_1(ScopedPPVar::PassRef(), dict_var_1->GetPPVar()); |
| 153 scoped_refptr<DictionaryVar> dict_var_2(new DictionaryVar()); | 168 scoped_refptr<DictionaryVar> dict_var_2(new DictionaryVar()); |
| 154 ScopedPPVar dict_pp_var_2(ScopedPPVar::PassRef(), dict_var_2->GetPPVar()); | 169 ScopedPPVar dict_pp_var_2(ScopedPPVar::PassRef(), dict_var_2->GetPPVar()); |
| 170 scoped_refptr<ArrayVar> array_var(new ArrayVar()); | |
| 171 ScopedPPVar array_pp_var(ScopedPPVar::PassRef(), array_var->GetPPVar()); | |
| 155 scoped_refptr<StringVar> string_var(new StringVar("string_value")); | 172 scoped_refptr<StringVar> string_var(new StringVar("string_value")); |
| 156 ScopedPPVar string_pp_var(ScopedPPVar::PassRef(), string_var->GetPPVar()); | 173 ScopedPPVar string_pp_var(ScopedPPVar::PassRef(), string_var->GetPPVar()); |
| 157 | 174 |
| 158 ASSERT_TRUE(dict_var_1->SetWithStringKey("null_key", PP_MakeNull())); | 175 ASSERT_TRUE(dict_var_1->SetWithStringKey("null_key", PP_MakeNull())); |
| 159 ASSERT_TRUE(dict_var_1->SetWithStringKey("string_key", | 176 ASSERT_TRUE(dict_var_1->SetWithStringKey("string_key", |
| 160 string_pp_var.get())); | 177 string_pp_var.get())); |
| 161 ASSERT_TRUE(dict_var_1->SetWithStringKey("dict_key", dict_pp_var_2.get())); | 178 ASSERT_TRUE(dict_var_1->SetWithStringKey("dict_key", dict_pp_var_2.get())); |
| 162 | 179 |
| 163 ASSERT_TRUE(dict_var_2->SetWithStringKey("double_key", PP_MakeDouble(1))); | 180 ASSERT_TRUE(dict_var_2->SetWithStringKey("double_key", PP_MakeDouble(1))); |
| 164 ASSERT_TRUE(dict_var_2->SetWithStringKey("int_key", PP_MakeInt32(2))); | 181 ASSERT_TRUE(dict_var_2->SetWithStringKey("array_key", array_pp_var.get())); |
| 165 ASSERT_TRUE(dict_var_2->SetWithStringKey("bool_key", PP_MakeBool(PP_TRUE))); | 182 |
| 183 ASSERT_TRUE(array_var->Set(0, PP_MakeInt32(2))); | |
| 184 ASSERT_TRUE(array_var->Set(1, PP_MakeBool(PP_TRUE))); | |
|
raymes
2013/03/05 18:19:16
Seems like we could have some more simple conversi
yzshen1
2013/03/14 22:36:48
Done.
| |
| 166 | 185 |
| 167 scoped_ptr<base::Value> value(CreateValueFromVar(dict_pp_var_1.get())); | 186 scoped_ptr<base::Value> value(CreateValueFromVar(dict_pp_var_1.get())); |
| 168 ASSERT_TRUE(value.get() != NULL); | 187 ASSERT_TRUE(value.get()); |
| 169 ASSERT_TRUE(Equals(*value, dict_pp_var_1.get())); | 188 ASSERT_TRUE(Equals(*value, dict_pp_var_1.get())); |
| 170 } | 189 } |
| 171 | 190 |
| 172 { | 191 { |
| 173 // Test that dictionary keys containing '.' are handled correctly. | 192 // Test that dictionary keys containing '.' are handled correctly. |
| 174 scoped_refptr<DictionaryVar> dict_var(new DictionaryVar()); | 193 scoped_refptr<DictionaryVar> dict_var(new DictionaryVar()); |
| 175 ScopedPPVar dict_pp_var(ScopedPPVar::PassRef(), dict_var->GetPPVar()); | 194 ScopedPPVar dict_pp_var(ScopedPPVar::PassRef(), dict_var->GetPPVar()); |
| 176 | 195 |
| 177 ASSERT_TRUE(dict_var->SetWithStringKey("double.key", PP_MakeDouble(1))); | 196 ASSERT_TRUE(dict_var->SetWithStringKey("double.key", PP_MakeDouble(1))); |
| 178 ASSERT_TRUE(dict_var->SetWithStringKey("int.key..name", PP_MakeInt32(2))); | 197 ASSERT_TRUE(dict_var->SetWithStringKey("int.key..name", PP_MakeInt32(2))); |
| 179 | 198 |
| 180 scoped_ptr<base::Value> value(CreateValueFromVar(dict_pp_var.get())); | 199 scoped_ptr<base::Value> value(CreateValueFromVar(dict_pp_var.get())); |
| 181 ASSERT_TRUE(value.get() != NULL); | 200 ASSERT_TRUE(value.get()); |
| 182 ASSERT_TRUE(Equals(*value, dict_pp_var.get())); | 201 ASSERT_TRUE(Equals(*value, dict_pp_var.get())); |
| 183 } | 202 } |
| 184 } | 203 } |
| 185 | 204 |
| 186 TEST_F(VarValueConversionsTest, CreateVarFromValue) { | 205 TEST_F(VarValueConversionsTest, CreateVarFromValue) { |
| 187 base::DictionaryValue dict_value; | 206 base::DictionaryValue dict_value; |
| 188 dict_value.Set("null_key", base::Value::CreateNullValue()); | 207 dict_value.Set("null_key", base::Value::CreateNullValue()); |
| 189 dict_value.SetString("string_key", "string_value"); | 208 dict_value.SetString("string_key", "string_value"); |
| 190 dict_value.SetDouble("dict_key.double_key", 1); | 209 dict_value.SetDouble("dict_key.double_key", 1); |
| 191 dict_value.SetInteger("dict_key.int_key", 2); | 210 |
| 192 dict_value.SetBoolean("dict_key.bool_key", true); | 211 scoped_ptr<base::ListValue> list_value(new base::ListValue()); |
| 212 list_value->AppendInteger(2); | |
| 213 list_value->AppendBoolean(true); | |
| 214 | |
| 215 dict_value.Set("dict_key.array_key", list_value.release()); | |
| 193 | 216 |
| 194 ScopedPPVar var(ScopedPPVar::PassRef(), CreateVarFromValue(dict_value)); | 217 ScopedPPVar var(ScopedPPVar::PassRef(), CreateVarFromValue(dict_value)); |
| 195 ASSERT_TRUE(Equals(dict_value, var.get())); | 218 ASSERT_TRUE(Equals(dict_value, var.get())); |
| 196 } | 219 } |
| 197 | 220 |
| 198 } // namespace ppapi | 221 } // namespace ppapi |
| OLD | NEW |