Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "ppapi/proxy/raw_var_data.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/memory/ref_counted.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/values.h" | |
| 11 #include "ppapi/c/pp_bool.h" | |
| 12 #include "ppapi/c/pp_var.h" | |
| 13 #include "ppapi/shared_impl/array_var.h" | |
| 14 #include "ppapi/shared_impl/dictionary_var.h" | |
| 15 #include "ppapi/shared_impl/ppapi_globals.h" | |
| 16 #include "ppapi/shared_impl/proxy_lock.h" | |
| 17 #include "ppapi/shared_impl/scoped_pp_var.h" | |
| 18 #include "ppapi/shared_impl/test_globals.h" | |
| 19 #include "ppapi/shared_impl/var.h" | |
| 20 #include "ppapi/shared_impl/var_tracker.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 | |
| 23 namespace ppapi { | |
| 24 namespace proxy { | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 class RawVarDataTest : public testing::Test { | |
| 29 public: | |
| 30 RawVarDataTest() {} | |
| 31 ~RawVarDataTest() {} | |
| 32 | |
| 33 // testing::Test implementation. | |
| 34 virtual void SetUp() { | |
| 35 ProxyLock::Acquire(); | |
| 36 } | |
| 37 virtual void TearDown() { | |
| 38 ASSERT_TRUE(PpapiGlobals::Get()->GetVarTracker()->GetLiveVars().empty()); | |
| 39 ProxyLock::Release(); | |
| 40 } | |
| 41 | |
| 42 private: | |
| 43 TestGlobals globals_; | |
| 44 }; | |
| 45 | |
| 46 // Compares two vars for equality. When two vars are found to be equal, an entry | |
| 47 // is inserted into |visited_map| with (expected id, actual id). This avoids | |
| 48 // following reference cycles. | |
|
dmichael (off chromium)
2013/04/16 18:41:20
It also ensures that id X in expected always maps
raymes
2013/04/16 20:34:19
Yes exactly :) This was tricky to say concisely th
| |
| 49 // TODO(raymes): Add logging in this function to make debugging easier. | |
| 50 bool Equals(const PP_Var& expected, | |
| 51 const PP_Var& actual, | |
| 52 base::hash_map<int64_t, int64_t>* visited_map) { | |
| 53 if (expected.type != actual.type) | |
| 54 return false; | |
| 55 if (VarTracker::IsVarTypeRefcounted(expected.type)) { | |
| 56 base::hash_map<int64_t, int64_t>::iterator it = | |
| 57 visited_map->find(expected.value.as_id); | |
| 58 if (it != visited_map->end()) | |
| 59 return it->second == actual.value.as_id; | |
| 60 else | |
| 61 (*visited_map)[expected.value.as_id] = actual.value.as_id; | |
| 62 } | |
| 63 switch (expected.type) { | |
| 64 case PP_VARTYPE_UNDEFINED: | |
| 65 return true; | |
| 66 case PP_VARTYPE_NULL: | |
| 67 return true; | |
| 68 case PP_VARTYPE_BOOL: | |
| 69 return expected.value.as_bool == actual.value.as_bool; | |
| 70 case PP_VARTYPE_INT32: | |
| 71 return expected.value.as_int == actual.value.as_int; | |
| 72 case PP_VARTYPE_DOUBLE: | |
| 73 return expected.value.as_double == actual.value.as_double; | |
| 74 case PP_VARTYPE_OBJECT: | |
| 75 return expected.value.as_id == actual.value.as_id; | |
| 76 case PP_VARTYPE_STRING: { | |
| 77 StringVar* expected_var = StringVar::FromPPVar(expected); | |
| 78 StringVar* actual_var = StringVar::FromPPVar(actual); | |
| 79 DCHECK(expected_var && actual_var); | |
| 80 return expected_var->value() == expected_var->value(); | |
| 81 } | |
| 82 case PP_VARTYPE_ARRAY_BUFFER: { | |
| 83 ArrayBufferVar* expected_var = ArrayBufferVar::FromPPVar(expected); | |
| 84 ArrayBufferVar* actual_var = ArrayBufferVar::FromPPVar(actual); | |
| 85 DCHECK(expected_var && actual_var); | |
| 86 if (expected_var->ByteLength() != actual_var->ByteLength()) | |
| 87 return false; | |
| 88 return memcmp(expected_var->Map(), actual_var->Map(), | |
| 89 expected_var->ByteLength()) == 0; | |
| 90 } | |
| 91 case PP_VARTYPE_ARRAY: { | |
| 92 ArrayVar* expected_var = ArrayVar::FromPPVar(expected); | |
| 93 ArrayVar* actual_var = ArrayVar::FromPPVar(actual); | |
| 94 DCHECK(expected_var && actual_var); | |
| 95 if (expected_var->elements().size() != actual_var->elements().size()) | |
| 96 return false; | |
| 97 for (size_t i = 0; i < expected_var->elements().size(); ++i) { | |
| 98 if (!Equals(expected_var->elements()[i].get(), | |
| 99 actual_var->elements()[i].get(), | |
| 100 visited_map)) { | |
| 101 return false; | |
| 102 } | |
| 103 } | |
| 104 return true; | |
| 105 } | |
| 106 case PP_VARTYPE_DICTIONARY: { | |
| 107 DictionaryVar* expected_var = DictionaryVar::FromPPVar(expected); | |
| 108 DictionaryVar* actual_var = DictionaryVar::FromPPVar(actual); | |
| 109 DCHECK(expected_var && actual_var); | |
| 110 if (expected_var->key_value_map().size() != | |
| 111 actual_var->key_value_map().size()) | |
| 112 return false; | |
| 113 DictionaryVar::KeyValueMap::const_iterator expected_iter = | |
| 114 expected_var->key_value_map().begin(); | |
| 115 DictionaryVar::KeyValueMap::const_iterator actual_iter = | |
| 116 actual_var->key_value_map().begin(); | |
| 117 for ( ; expected_iter != expected_var->key_value_map().end(); | |
| 118 ++expected_iter, ++actual_iter) { | |
| 119 if (expected_iter->first != actual_iter->first) | |
| 120 return false; | |
| 121 if (!Equals(expected_iter->second.get(), | |
| 122 actual_iter->second.get(), | |
| 123 visited_map)) { | |
| 124 return false; | |
| 125 } | |
| 126 } | |
| 127 return true; | |
| 128 } | |
| 129 } | |
| 130 NOTREACHED(); | |
| 131 return false; | |
| 132 } | |
| 133 | |
| 134 bool Equals(const PP_Var& expected, | |
| 135 const PP_Var& actual) { | |
| 136 base::hash_map<int64_t, int64_t> visited_map; | |
| 137 return Equals(expected, actual, &visited_map); | |
| 138 } | |
| 139 | |
| 140 PP_Var WriteAndRead(const PP_Var& var) { | |
| 141 PP_Instance dummy_instance = 1234; | |
| 142 scoped_ptr<RawVarDataGraph> expected_data(RawVarDataGraph::Create( | |
| 143 var, dummy_instance)); | |
| 144 IPC::Message m; | |
| 145 expected_data->Write(&m); | |
| 146 PickleIterator iter(m); | |
| 147 scoped_ptr<RawVarDataGraph> actual_data(RawVarDataGraph::Read(&m, &iter)); | |
| 148 return actual_data->CreatePPVar(dummy_instance); | |
| 149 } | |
| 150 | |
| 151 // Assumes a ref for var. | |
| 152 bool WriteReadAndCompare(const PP_Var& var) { | |
| 153 ScopedPPVar expected(ScopedPPVar::PassRef(), var); | |
| 154 ScopedPPVar actual(ScopedPPVar::PassRef(), WriteAndRead(expected.get())); | |
| 155 return Equals(expected.get(), actual.get()); | |
| 156 } | |
| 157 | |
| 158 } // namespace | |
| 159 | |
| 160 TEST_F(RawVarDataTest, SimpleTest) { | |
| 161 EXPECT_TRUE(WriteReadAndCompare(PP_MakeUndefined())); | |
| 162 EXPECT_TRUE(WriteReadAndCompare(PP_MakeNull())); | |
| 163 EXPECT_TRUE(WriteReadAndCompare(PP_MakeInt32(100))); | |
| 164 EXPECT_TRUE(WriteReadAndCompare(PP_MakeBool(PP_TRUE))); | |
| 165 EXPECT_TRUE(WriteReadAndCompare(PP_MakeDouble(53.75))); | |
| 166 PP_Var object; | |
| 167 object.type = PP_VARTYPE_OBJECT; | |
| 168 object.value.as_id = 10; | |
| 169 EXPECT_TRUE(WriteReadAndCompare(object)); | |
| 170 } | |
| 171 | |
| 172 TEST_F(RawVarDataTest, StringTest) { | |
| 173 EXPECT_TRUE(WriteReadAndCompare(StringVar::StringToPPVar(""))); | |
| 174 EXPECT_TRUE(WriteReadAndCompare(StringVar::StringToPPVar("hello world!"))); | |
| 175 } | |
| 176 | |
| 177 TEST_F(RawVarDataTest, ArrayBufferTest) { | |
| 178 std::string data = "hello world!"; | |
| 179 PP_Var var = PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar( | |
| 180 data.size(), data.data()); | |
| 181 EXPECT_TRUE(WriteReadAndCompare(var)); | |
| 182 var = PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar( | |
| 183 0, static_cast<void*>(NULL)); | |
| 184 EXPECT_TRUE(WriteReadAndCompare(var)); | |
| 185 // TODO(raymes): add tests for shmem type array buffers. | |
| 186 } | |
| 187 | |
| 188 TEST_F(RawVarDataTest, DictionaryArrayTest) { | |
| 189 // Empty array. | |
| 190 scoped_refptr<ArrayVar> array(new ArrayVar); | |
| 191 ScopedPPVar release_array(ScopedPPVar::PassRef(), array->GetPPVar()); | |
| 192 EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); | |
| 193 | |
| 194 size_t index = 0; | |
| 195 | |
| 196 // Array with primitives. | |
| 197 array->Set(index++, PP_MakeUndefined()); | |
| 198 array->Set(index++, PP_MakeNull()); | |
| 199 array->Set(index++, PP_MakeInt32(100)); | |
| 200 array->Set(index++, PP_MakeBool(PP_FALSE)); | |
| 201 array->Set(index++, PP_MakeDouble(0.123)); | |
| 202 EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); | |
| 203 | |
| 204 // Array with 2 references to the same string. | |
| 205 ScopedPPVar release_string( | |
| 206 ScopedPPVar::PassRef(), StringVar::StringToPPVar("abc")); | |
| 207 array->Set(index++, release_string.get()); | |
| 208 array->Set(index++, release_string.get()); | |
| 209 EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); | |
| 210 | |
|
dmichael (off chromium)
2013/04/16 18:41:20
Do you want to somehow clear the array out between
raymes
2013/04/16 20:34:19
Yeah the state builds up cumulatively. I'd prefer
| |
| 211 // Array with nested array that references the same string. | |
| 212 scoped_refptr<ArrayVar> array2(new ArrayVar); | |
| 213 ScopedPPVar release_array2(ScopedPPVar::PassRef(), array2->GetPPVar()); | |
| 214 array2->Set(0, release_string.get()); | |
| 215 array->Set(index++, release_array2.get()); | |
| 216 EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); | |
| 217 | |
| 218 // Empty dictionary. | |
| 219 scoped_refptr<DictionaryVar> dictionary(new DictionaryVar); | |
| 220 ScopedPPVar release_dictionary(ScopedPPVar::PassRef(), | |
| 221 dictionary->GetPPVar()); | |
| 222 EXPECT_TRUE(WriteReadAndCompare(dictionary->GetPPVar())); | |
| 223 | |
| 224 // Dictionary with primitives. | |
| 225 dictionary->SetWithStringKey("1", PP_MakeUndefined()); | |
| 226 dictionary->SetWithStringKey("2", PP_MakeNull()); | |
| 227 dictionary->SetWithStringKey("3", PP_MakeInt32(-100)); | |
| 228 dictionary->SetWithStringKey("4", PP_MakeBool(PP_TRUE)); | |
| 229 dictionary->SetWithStringKey("5", PP_MakeDouble(-103.52)); | |
| 230 EXPECT_TRUE(WriteReadAndCompare(dictionary->GetPPVar())); | |
| 231 | |
| 232 // Dictionary with 2 references to the same string. | |
| 233 dictionary->SetWithStringKey("6", release_string.get()); | |
| 234 dictionary->SetWithStringKey("7", release_string.get()); | |
| 235 EXPECT_TRUE(WriteReadAndCompare(dictionary->GetPPVar())); | |
| 236 | |
| 237 // Dictionary with nested dictionary that references the same string. | |
| 238 scoped_refptr<DictionaryVar> dictionary2(new DictionaryVar); | |
| 239 ScopedPPVar release_dictionary2(ScopedPPVar::PassRef(), | |
| 240 dictionary2->GetPPVar()); | |
| 241 dictionary2->SetWithStringKey("abc", release_string.get()); | |
| 242 dictionary->SetWithStringKey("8", release_dictionary2.get()); | |
| 243 EXPECT_TRUE(WriteReadAndCompare(dictionary->GetPPVar())); | |
| 244 | |
| 245 // Array with dictionary. | |
| 246 array->Set(index++, release_dictionary.get()); | |
| 247 EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); | |
| 248 | |
| 249 // Array with dictionary with array. | |
| 250 array2->Set(0, PP_MakeInt32(100)); | |
| 251 dictionary->SetWithStringKey("9", release_array2.get()); | |
| 252 EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); | |
| 253 | |
| 254 // Array <-> dictionary cycle. | |
| 255 dictionary->SetWithStringKey("10", release_array.get()); | |
| 256 ScopedPPVar result = ScopedPPVar(ScopedPPVar::PassRef(), | |
| 257 WriteAndRead(release_dictionary.get())); | |
| 258 EXPECT_TRUE(Equals(release_dictionary.get(), result.get())); | |
| 259 // Break the cycle. | |
| 260 // TODO(raymes): We need some better machinery for releasing vars with | |
| 261 // cycles. Remove the code below once we have that. | |
| 262 dictionary->DeleteWithStringKey("10"); | |
| 263 DictionaryVar* result_dictionary = DictionaryVar::FromPPVar(result.get()); | |
| 264 result_dictionary->DeleteWithStringKey("10"); | |
| 265 | |
| 266 // Array with self references. | |
| 267 array->Set(index, release_array.get()); | |
| 268 result = ScopedPPVar(ScopedPPVar::PassRef(), | |
| 269 WriteAndRead(release_array.get())); | |
| 270 EXPECT_TRUE(Equals(release_array.get(), result.get())); | |
| 271 // Break the self reference. | |
| 272 array->Set(index, PP_MakeUndefined()); | |
| 273 ArrayVar* result_array = ArrayVar::FromPPVar(result.get()); | |
| 274 result_array->Set(index, PP_MakeUndefined()); | |
| 275 } | |
| 276 | |
| 277 } // namespace proxy | |
| 278 } // namespace ppapi | |
| OLD | NEW |