Chromium Code Reviews| Index: ppapi/proxy/raw_var_data_unittest.cc |
| diff --git a/ppapi/proxy/raw_var_data_unittest.cc b/ppapi/proxy/raw_var_data_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..158fb7cd0c33bd8c3318662d9ba4c27e1c4b6b73 |
| --- /dev/null |
| +++ b/ppapi/proxy/raw_var_data_unittest.cc |
| @@ -0,0 +1,278 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ppapi/proxy/raw_var_data.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/values.h" |
| +#include "ppapi/c/pp_bool.h" |
| +#include "ppapi/c/pp_var.h" |
| +#include "ppapi/shared_impl/array_var.h" |
| +#include "ppapi/shared_impl/dictionary_var.h" |
| +#include "ppapi/shared_impl/ppapi_globals.h" |
| +#include "ppapi/shared_impl/proxy_lock.h" |
| +#include "ppapi/shared_impl/scoped_pp_var.h" |
| +#include "ppapi/shared_impl/test_globals.h" |
| +#include "ppapi/shared_impl/var.h" |
| +#include "ppapi/shared_impl/var_tracker.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace ppapi { |
| +namespace proxy { |
| + |
| +namespace { |
| + |
| +class RawVarDataTest : public testing::Test { |
| + public: |
| + RawVarDataTest() {} |
| + ~RawVarDataTest() {} |
| + |
| + // testing::Test implementation. |
| + virtual void SetUp() { |
| + ProxyLock::Acquire(); |
| + } |
| + virtual void TearDown() { |
| + ASSERT_TRUE(PpapiGlobals::Get()->GetVarTracker()->GetLiveVars().empty()); |
| + ProxyLock::Release(); |
| + } |
| + |
| + private: |
| + TestGlobals globals_; |
| +}; |
| + |
| +// Compares two vars for equality. When two vars are found to be equal, an entry |
| +// is inserted into |visited_map| with (expected id, actual id). This avoids |
| +// 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
|
| +// TODO(raymes): Add logging in this function to make debugging easier. |
| +bool Equals(const PP_Var& expected, |
| + const PP_Var& actual, |
| + base::hash_map<int64_t, int64_t>* visited_map) { |
| + if (expected.type != actual.type) |
| + return false; |
| + if (VarTracker::IsVarTypeRefcounted(expected.type)) { |
| + base::hash_map<int64_t, int64_t>::iterator it = |
| + visited_map->find(expected.value.as_id); |
| + if (it != visited_map->end()) |
| + return it->second == actual.value.as_id; |
| + else |
| + (*visited_map)[expected.value.as_id] = actual.value.as_id; |
| + } |
| + switch (expected.type) { |
| + case PP_VARTYPE_UNDEFINED: |
| + return true; |
| + case PP_VARTYPE_NULL: |
| + return true; |
| + case PP_VARTYPE_BOOL: |
| + return expected.value.as_bool == actual.value.as_bool; |
| + case PP_VARTYPE_INT32: |
| + return expected.value.as_int == actual.value.as_int; |
| + case PP_VARTYPE_DOUBLE: |
| + return expected.value.as_double == actual.value.as_double; |
| + case PP_VARTYPE_OBJECT: |
| + return expected.value.as_id == actual.value.as_id; |
| + case PP_VARTYPE_STRING: { |
| + StringVar* expected_var = StringVar::FromPPVar(expected); |
| + StringVar* actual_var = StringVar::FromPPVar(actual); |
| + DCHECK(expected_var && actual_var); |
| + return expected_var->value() == expected_var->value(); |
| + } |
| + case PP_VARTYPE_ARRAY_BUFFER: { |
| + ArrayBufferVar* expected_var = ArrayBufferVar::FromPPVar(expected); |
| + ArrayBufferVar* actual_var = ArrayBufferVar::FromPPVar(actual); |
| + DCHECK(expected_var && actual_var); |
| + if (expected_var->ByteLength() != actual_var->ByteLength()) |
| + return false; |
| + return memcmp(expected_var->Map(), actual_var->Map(), |
| + expected_var->ByteLength()) == 0; |
| + } |
| + case PP_VARTYPE_ARRAY: { |
| + ArrayVar* expected_var = ArrayVar::FromPPVar(expected); |
| + ArrayVar* actual_var = ArrayVar::FromPPVar(actual); |
| + DCHECK(expected_var && actual_var); |
| + if (expected_var->elements().size() != actual_var->elements().size()) |
| + return false; |
| + for (size_t i = 0; i < expected_var->elements().size(); ++i) { |
| + if (!Equals(expected_var->elements()[i].get(), |
| + actual_var->elements()[i].get(), |
| + visited_map)) { |
| + return false; |
| + } |
| + } |
| + return true; |
| + } |
| + case PP_VARTYPE_DICTIONARY: { |
| + DictionaryVar* expected_var = DictionaryVar::FromPPVar(expected); |
| + DictionaryVar* actual_var = DictionaryVar::FromPPVar(actual); |
| + DCHECK(expected_var && actual_var); |
| + if (expected_var->key_value_map().size() != |
| + actual_var->key_value_map().size()) |
| + return false; |
| + DictionaryVar::KeyValueMap::const_iterator expected_iter = |
| + expected_var->key_value_map().begin(); |
| + DictionaryVar::KeyValueMap::const_iterator actual_iter = |
| + actual_var->key_value_map().begin(); |
| + for ( ; expected_iter != expected_var->key_value_map().end(); |
| + ++expected_iter, ++actual_iter) { |
| + if (expected_iter->first != actual_iter->first) |
| + return false; |
| + if (!Equals(expected_iter->second.get(), |
| + actual_iter->second.get(), |
| + visited_map)) { |
| + return false; |
| + } |
| + } |
| + return true; |
| + } |
| + } |
| + NOTREACHED(); |
| + return false; |
| +} |
| + |
| +bool Equals(const PP_Var& expected, |
| + const PP_Var& actual) { |
| + base::hash_map<int64_t, int64_t> visited_map; |
| + return Equals(expected, actual, &visited_map); |
| +} |
| + |
| +PP_Var WriteAndRead(const PP_Var& var) { |
| + PP_Instance dummy_instance = 1234; |
| + scoped_ptr<RawVarDataGraph> expected_data(RawVarDataGraph::Create( |
| + var, dummy_instance)); |
| + IPC::Message m; |
| + expected_data->Write(&m); |
| + PickleIterator iter(m); |
| + scoped_ptr<RawVarDataGraph> actual_data(RawVarDataGraph::Read(&m, &iter)); |
| + return actual_data->CreatePPVar(dummy_instance); |
| +} |
| + |
| +// Assumes a ref for var. |
| +bool WriteReadAndCompare(const PP_Var& var) { |
| + ScopedPPVar expected(ScopedPPVar::PassRef(), var); |
| + ScopedPPVar actual(ScopedPPVar::PassRef(), WriteAndRead(expected.get())); |
| + return Equals(expected.get(), actual.get()); |
| +} |
| + |
| +} // namespace |
| + |
| +TEST_F(RawVarDataTest, SimpleTest) { |
| + EXPECT_TRUE(WriteReadAndCompare(PP_MakeUndefined())); |
| + EXPECT_TRUE(WriteReadAndCompare(PP_MakeNull())); |
| + EXPECT_TRUE(WriteReadAndCompare(PP_MakeInt32(100))); |
| + EXPECT_TRUE(WriteReadAndCompare(PP_MakeBool(PP_TRUE))); |
| + EXPECT_TRUE(WriteReadAndCompare(PP_MakeDouble(53.75))); |
| + PP_Var object; |
| + object.type = PP_VARTYPE_OBJECT; |
| + object.value.as_id = 10; |
| + EXPECT_TRUE(WriteReadAndCompare(object)); |
| +} |
| + |
| +TEST_F(RawVarDataTest, StringTest) { |
| + EXPECT_TRUE(WriteReadAndCompare(StringVar::StringToPPVar(""))); |
| + EXPECT_TRUE(WriteReadAndCompare(StringVar::StringToPPVar("hello world!"))); |
| +} |
| + |
| +TEST_F(RawVarDataTest, ArrayBufferTest) { |
| + std::string data = "hello world!"; |
| + PP_Var var = PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar( |
| + data.size(), data.data()); |
| + EXPECT_TRUE(WriteReadAndCompare(var)); |
| + var = PpapiGlobals::Get()->GetVarTracker()->MakeArrayBufferPPVar( |
| + 0, static_cast<void*>(NULL)); |
| + EXPECT_TRUE(WriteReadAndCompare(var)); |
| + // TODO(raymes): add tests for shmem type array buffers. |
| +} |
| + |
| +TEST_F(RawVarDataTest, DictionaryArrayTest) { |
| + // Empty array. |
| + scoped_refptr<ArrayVar> array(new ArrayVar); |
| + ScopedPPVar release_array(ScopedPPVar::PassRef(), array->GetPPVar()); |
| + EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); |
| + |
| + size_t index = 0; |
| + |
| + // Array with primitives. |
| + array->Set(index++, PP_MakeUndefined()); |
| + array->Set(index++, PP_MakeNull()); |
| + array->Set(index++, PP_MakeInt32(100)); |
| + array->Set(index++, PP_MakeBool(PP_FALSE)); |
| + array->Set(index++, PP_MakeDouble(0.123)); |
| + EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); |
| + |
| + // Array with 2 references to the same string. |
| + ScopedPPVar release_string( |
| + ScopedPPVar::PassRef(), StringVar::StringToPPVar("abc")); |
| + array->Set(index++, release_string.get()); |
| + array->Set(index++, release_string.get()); |
| + EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); |
| + |
|
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
|
| + // Array with nested array that references the same string. |
| + scoped_refptr<ArrayVar> array2(new ArrayVar); |
| + ScopedPPVar release_array2(ScopedPPVar::PassRef(), array2->GetPPVar()); |
| + array2->Set(0, release_string.get()); |
| + array->Set(index++, release_array2.get()); |
| + EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); |
| + |
| + // Empty dictionary. |
| + scoped_refptr<DictionaryVar> dictionary(new DictionaryVar); |
| + ScopedPPVar release_dictionary(ScopedPPVar::PassRef(), |
| + dictionary->GetPPVar()); |
| + EXPECT_TRUE(WriteReadAndCompare(dictionary->GetPPVar())); |
| + |
| + // Dictionary with primitives. |
| + dictionary->SetWithStringKey("1", PP_MakeUndefined()); |
| + dictionary->SetWithStringKey("2", PP_MakeNull()); |
| + dictionary->SetWithStringKey("3", PP_MakeInt32(-100)); |
| + dictionary->SetWithStringKey("4", PP_MakeBool(PP_TRUE)); |
| + dictionary->SetWithStringKey("5", PP_MakeDouble(-103.52)); |
| + EXPECT_TRUE(WriteReadAndCompare(dictionary->GetPPVar())); |
| + |
| + // Dictionary with 2 references to the same string. |
| + dictionary->SetWithStringKey("6", release_string.get()); |
| + dictionary->SetWithStringKey("7", release_string.get()); |
| + EXPECT_TRUE(WriteReadAndCompare(dictionary->GetPPVar())); |
| + |
| + // Dictionary with nested dictionary that references the same string. |
| + scoped_refptr<DictionaryVar> dictionary2(new DictionaryVar); |
| + ScopedPPVar release_dictionary2(ScopedPPVar::PassRef(), |
| + dictionary2->GetPPVar()); |
| + dictionary2->SetWithStringKey("abc", release_string.get()); |
| + dictionary->SetWithStringKey("8", release_dictionary2.get()); |
| + EXPECT_TRUE(WriteReadAndCompare(dictionary->GetPPVar())); |
| + |
| + // Array with dictionary. |
| + array->Set(index++, release_dictionary.get()); |
| + EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); |
| + |
| + // Array with dictionary with array. |
| + array2->Set(0, PP_MakeInt32(100)); |
| + dictionary->SetWithStringKey("9", release_array2.get()); |
| + EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); |
| + |
| + // Array <-> dictionary cycle. |
| + dictionary->SetWithStringKey("10", release_array.get()); |
| + ScopedPPVar result = ScopedPPVar(ScopedPPVar::PassRef(), |
| + WriteAndRead(release_dictionary.get())); |
| + EXPECT_TRUE(Equals(release_dictionary.get(), result.get())); |
| + // Break the cycle. |
| + // TODO(raymes): We need some better machinery for releasing vars with |
| + // cycles. Remove the code below once we have that. |
| + dictionary->DeleteWithStringKey("10"); |
| + DictionaryVar* result_dictionary = DictionaryVar::FromPPVar(result.get()); |
| + result_dictionary->DeleteWithStringKey("10"); |
| + |
| + // Array with self references. |
| + array->Set(index, release_array.get()); |
| + result = ScopedPPVar(ScopedPPVar::PassRef(), |
| + WriteAndRead(release_array.get())); |
| + EXPECT_TRUE(Equals(release_array.get(), result.get())); |
| + // Break the self reference. |
| + array->Set(index, PP_MakeUndefined()); |
| + ArrayVar* result_array = ArrayVar::FromPPVar(result.get()); |
| + result_array->Set(index, PP_MakeUndefined()); |
| +} |
| + |
| +} // namespace proxy |
| +} // namespace ppapi |