| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/proxy/raw_var_data.h" | 5 #include "ppapi/proxy/raw_var_data.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 } | 37 } |
| 38 virtual void TearDown() { | 38 virtual void TearDown() { |
| 39 ASSERT_TRUE(PpapiGlobals::Get()->GetVarTracker()->GetLiveVars().empty()); | 39 ASSERT_TRUE(PpapiGlobals::Get()->GetVarTracker()->GetLiveVars().empty()); |
| 40 ProxyLock::Release(); | 40 ProxyLock::Release(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 private: | 43 private: |
| 44 TestGlobals globals_; | 44 TestGlobals globals_; |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 PP_Var WriteAndRead(const PP_Var& var) { | 47 bool WriteAndRead(const PP_Var& var, PP_Var* result, bool allow_cycles) { |
| 48 PP_Instance dummy_instance = 1234; | 48 PP_Instance dummy_instance = 1234; |
| 49 scoped_ptr<RawVarDataGraph> expected_data(RawVarDataGraph::Create( | 49 scoped_ptr<RawVarDataGraph> expected_data(RawVarDataGraph::Create( |
| 50 var, dummy_instance)); | 50 var, dummy_instance, allow_cycles)); |
| 51 if (!expected_data) |
| 52 return false; |
| 51 IPC::Message m; | 53 IPC::Message m; |
| 52 expected_data->Write(&m); | 54 expected_data->Write(&m); |
| 53 PickleIterator iter(m); | 55 PickleIterator iter(m); |
| 54 scoped_ptr<RawVarDataGraph> actual_data(RawVarDataGraph::Read(&m, &iter)); | 56 scoped_ptr<RawVarDataGraph> actual_data(RawVarDataGraph::Read(&m, &iter)); |
| 55 return actual_data->CreatePPVar(dummy_instance); | 57 *result = actual_data->CreatePPVar(dummy_instance); |
| 58 return true; |
| 56 } | 59 } |
| 57 | 60 |
| 58 // Assumes a ref for var. | 61 // Assumes a ref for var. |
| 59 bool WriteReadAndCompare(const PP_Var& var) { | 62 bool WriteReadAndCompare(const PP_Var& var) { |
| 60 ScopedPPVar expected(ScopedPPVar::PassRef(), var); | 63 ScopedPPVar expected(ScopedPPVar::PassRef(), var); |
| 61 ScopedPPVar actual(ScopedPPVar::PassRef(), WriteAndRead(expected.get())); | 64 PP_Var result; |
| 65 bool success = WriteAndRead(expected.get(), &result, false); |
| 66 if (!success) |
| 67 return false; |
| 68 ScopedPPVar actual(ScopedPPVar::PassRef(), result); |
| 62 return TestEqual(expected.get(), actual.get()); | 69 return TestEqual(expected.get(), actual.get()); |
| 63 } | 70 } |
| 64 | 71 |
| 65 } // namespace | 72 } // namespace |
| 66 | 73 |
| 67 TEST_F(RawVarDataTest, SimpleTest) { | 74 TEST_F(RawVarDataTest, SimpleTest) { |
| 68 EXPECT_TRUE(WriteReadAndCompare(PP_MakeUndefined())); | 75 EXPECT_TRUE(WriteReadAndCompare(PP_MakeUndefined())); |
| 69 EXPECT_TRUE(WriteReadAndCompare(PP_MakeNull())); | 76 EXPECT_TRUE(WriteReadAndCompare(PP_MakeNull())); |
| 70 EXPECT_TRUE(WriteReadAndCompare(PP_MakeInt32(100))); | 77 EXPECT_TRUE(WriteReadAndCompare(PP_MakeInt32(100))); |
| 71 EXPECT_TRUE(WriteReadAndCompare(PP_MakeBool(PP_TRUE))); | 78 EXPECT_TRUE(WriteReadAndCompare(PP_MakeBool(PP_TRUE))); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 array->Set(index++, release_dictionary.get()); | 160 array->Set(index++, release_dictionary.get()); |
| 154 EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); | 161 EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); |
| 155 | 162 |
| 156 // Array with dictionary with array. | 163 // Array with dictionary with array. |
| 157 array2->Set(0, PP_MakeInt32(100)); | 164 array2->Set(0, PP_MakeInt32(100)); |
| 158 dictionary->SetWithStringKey("9", release_array2.get()); | 165 dictionary->SetWithStringKey("9", release_array2.get()); |
| 159 EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); | 166 EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); |
| 160 | 167 |
| 161 // Array <-> dictionary cycle. | 168 // Array <-> dictionary cycle. |
| 162 dictionary->SetWithStringKey("10", release_array.get()); | 169 dictionary->SetWithStringKey("10", release_array.get()); |
| 163 ScopedPPVar result = ScopedPPVar(ScopedPPVar::PassRef(), | 170 PP_Var result; |
| 164 WriteAndRead(release_dictionary.get())); | 171 ASSERT_FALSE(WriteAndRead(release_dictionary.get(), &result, false)); |
| 165 EXPECT_TRUE(TestEqual(release_dictionary.get(), result.get())); | 172 ASSERT_TRUE(WriteAndRead(release_dictionary.get(), &result, true)); |
| 173 ScopedPPVar release_result = ScopedPPVar(ScopedPPVar::PassRef(), result); |
| 174 EXPECT_TRUE(TestEqual(release_dictionary.get(), result)); |
| 166 // Break the cycle. | 175 // Break the cycle. |
| 167 // TODO(raymes): We need some better machinery for releasing vars with | 176 // TODO(raymes): We need some better machinery for releasing vars with |
| 168 // cycles. Remove the code below once we have that. | 177 // cycles. Remove the code below once we have that. |
| 169 dictionary->DeleteWithStringKey("10"); | 178 dictionary->DeleteWithStringKey("10"); |
| 170 DictionaryVar* result_dictionary = DictionaryVar::FromPPVar(result.get()); | 179 DictionaryVar* result_dictionary = DictionaryVar::FromPPVar(result); |
| 171 result_dictionary->DeleteWithStringKey("10"); | 180 result_dictionary->DeleteWithStringKey("10"); |
| 172 | 181 |
| 173 // Array with self references. | 182 // Array with self references. |
| 174 array->Set(index, release_array.get()); | 183 array->Set(index, release_array.get()); |
| 175 result = ScopedPPVar(ScopedPPVar::PassRef(), | 184 ASSERT_FALSE(WriteAndRead(release_array.get(), &result, false)); |
| 176 WriteAndRead(release_array.get())); | 185 ASSERT_TRUE(WriteAndRead(release_array.get(), &result, true)); |
| 177 EXPECT_TRUE(TestEqual(release_array.get(), result.get())); | 186 release_result = ScopedPPVar(ScopedPPVar::PassRef(), result); |
| 187 EXPECT_TRUE(TestEqual(release_array.get(), result)); |
| 178 // Break the self reference. | 188 // Break the self reference. |
| 179 array->Set(index, PP_MakeUndefined()); | 189 array->Set(index, PP_MakeUndefined()); |
| 180 ArrayVar* result_array = ArrayVar::FromPPVar(result.get()); | 190 ArrayVar* result_array = ArrayVar::FromPPVar(result); |
| 181 result_array->Set(index, PP_MakeUndefined()); | 191 result_array->Set(index, PP_MakeUndefined()); |
| 182 } | 192 } |
| 183 | 193 |
| 184 } // namespace proxy | 194 } // namespace proxy |
| 185 } // namespace ppapi | 195 } // namespace ppapi |
| OLD | NEW |