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" |
11 #include "ppapi/c/pp_bool.h" | 11 #include "ppapi/c/pp_bool.h" |
12 #include "ppapi/c/pp_var.h" | 12 #include "ppapi/c/pp_var.h" |
13 #include "ppapi/shared_impl/array_var.h" | 13 #include "ppapi/shared_impl/array_var.h" |
14 #include "ppapi/shared_impl/dictionary_var.h" | 14 #include "ppapi/shared_impl/dictionary_var.h" |
15 #include "ppapi/shared_impl/ppapi_globals.h" | 15 #include "ppapi/shared_impl/ppapi_globals.h" |
16 #include "ppapi/shared_impl/proxy_lock.h" | 16 #include "ppapi/shared_impl/proxy_lock.h" |
17 #include "ppapi/shared_impl/scoped_pp_var.h" | 17 #include "ppapi/shared_impl/scoped_pp_var.h" |
18 #include "ppapi/shared_impl/test_globals.h" | 18 #include "ppapi/shared_impl/test_globals.h" |
19 #include "ppapi/shared_impl/unittest_utils.h" | 19 #include "ppapi/shared_impl/unittest_utils.h" |
20 #include "ppapi/shared_impl/var.h" | 20 #include "ppapi/shared_impl/var.h" |
21 #include "ppapi/shared_impl/var_tracker.h" | 21 #include "ppapi/shared_impl/var_tracker.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
24 namespace ppapi { | 24 namespace ppapi { |
25 namespace proxy { | 25 namespace proxy { |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
| 29 void DefaultHandleWriter(IPC::Message* m, const SerializedHandle& handle) { |
| 30 IPC::ParamTraits<SerializedHandle>::Write(m, handle); |
| 31 } |
| 32 |
29 class RawVarDataTest : public testing::Test { | 33 class RawVarDataTest : public testing::Test { |
30 public: | 34 public: |
31 RawVarDataTest() {} | 35 RawVarDataTest() {} |
32 ~RawVarDataTest() {} | 36 ~RawVarDataTest() {} |
33 | 37 |
34 // testing::Test implementation. | 38 // testing::Test implementation. |
35 virtual void SetUp() { | 39 virtual void SetUp() { |
36 ProxyLock::Acquire(); | 40 ProxyLock::Acquire(); |
37 } | 41 } |
38 virtual void TearDown() { | 42 virtual void TearDown() { |
39 ASSERT_TRUE(PpapiGlobals::Get()->GetVarTracker()->GetLiveVars().empty()); | 43 ASSERT_TRUE(PpapiGlobals::Get()->GetVarTracker()->GetLiveVars().empty()); |
40 ProxyLock::Release(); | 44 ProxyLock::Release(); |
41 } | 45 } |
42 | 46 |
43 private: | 47 private: |
44 TestGlobals globals_; | 48 TestGlobals globals_; |
45 }; | 49 }; |
46 | 50 |
47 PP_Var WriteAndRead(const PP_Var& var) { | 51 bool WriteAndRead(const PP_Var& var, PP_Var* result) { |
48 PP_Instance dummy_instance = 1234; | 52 PP_Instance dummy_instance = 1234; |
49 scoped_ptr<RawVarDataGraph> expected_data(RawVarDataGraph::Create( | 53 scoped_ptr<RawVarDataGraph> expected_data(RawVarDataGraph::Create( |
50 var, dummy_instance)); | 54 var, dummy_instance)); |
| 55 if (!expected_data) |
| 56 return false; |
51 IPC::Message m; | 57 IPC::Message m; |
52 expected_data->Write(&m); | 58 expected_data->Write(&m, base::Bind(&DefaultHandleWriter)); |
53 PickleIterator iter(m); | 59 PickleIterator iter(m); |
54 scoped_ptr<RawVarDataGraph> actual_data(RawVarDataGraph::Read(&m, &iter)); | 60 scoped_ptr<RawVarDataGraph> actual_data(RawVarDataGraph::Read(&m, &iter)); |
55 return actual_data->CreatePPVar(dummy_instance); | 61 *result = actual_data->CreatePPVar(dummy_instance); |
| 62 return true; |
56 } | 63 } |
57 | 64 |
58 // Assumes a ref for var. | 65 // Assumes a ref for var. |
59 bool WriteReadAndCompare(const PP_Var& var) { | 66 bool WriteReadAndCompare(const PP_Var& var) { |
60 ScopedPPVar expected(ScopedPPVar::PassRef(), var); | 67 ScopedPPVar expected(ScopedPPVar::PassRef(), var); |
61 ScopedPPVar actual(ScopedPPVar::PassRef(), WriteAndRead(expected.get())); | 68 PP_Var result; |
| 69 bool success = WriteAndRead(expected.get(), &result); |
| 70 if (!success) |
| 71 return false; |
| 72 ScopedPPVar actual(ScopedPPVar::PassRef(), result); |
62 return TestEqual(expected.get(), actual.get()); | 73 return TestEqual(expected.get(), actual.get()); |
63 } | 74 } |
64 | 75 |
65 } // namespace | 76 } // namespace |
66 | 77 |
67 TEST_F(RawVarDataTest, SimpleTest) { | 78 TEST_F(RawVarDataTest, SimpleTest) { |
68 EXPECT_TRUE(WriteReadAndCompare(PP_MakeUndefined())); | 79 EXPECT_TRUE(WriteReadAndCompare(PP_MakeUndefined())); |
69 EXPECT_TRUE(WriteReadAndCompare(PP_MakeNull())); | 80 EXPECT_TRUE(WriteReadAndCompare(PP_MakeNull())); |
70 EXPECT_TRUE(WriteReadAndCompare(PP_MakeInt32(100))); | 81 EXPECT_TRUE(WriteReadAndCompare(PP_MakeInt32(100))); |
71 EXPECT_TRUE(WriteReadAndCompare(PP_MakeBool(PP_TRUE))); | 82 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()); | 164 array->Set(index++, release_dictionary.get()); |
154 EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); | 165 EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); |
155 | 166 |
156 // Array with dictionary with array. | 167 // Array with dictionary with array. |
157 array2->Set(0, PP_MakeInt32(100)); | 168 array2->Set(0, PP_MakeInt32(100)); |
158 dictionary->SetWithStringKey("9", release_array2.get()); | 169 dictionary->SetWithStringKey("9", release_array2.get()); |
159 EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); | 170 EXPECT_TRUE(WriteReadAndCompare(array->GetPPVar())); |
160 | 171 |
161 // Array <-> dictionary cycle. | 172 // Array <-> dictionary cycle. |
162 dictionary->SetWithStringKey("10", release_array.get()); | 173 dictionary->SetWithStringKey("10", release_array.get()); |
163 ScopedPPVar result = ScopedPPVar(ScopedPPVar::PassRef(), | 174 PP_Var result; |
164 WriteAndRead(release_dictionary.get())); | 175 ASSERT_FALSE(WriteAndRead(release_dictionary.get(), &result)); |
165 EXPECT_TRUE(TestEqual(release_dictionary.get(), result.get())); | |
166 // Break the cycle. | 176 // Break the cycle. |
167 // TODO(raymes): We need some better machinery for releasing vars with | 177 // TODO(raymes): We need some better machinery for releasing vars with |
168 // cycles. Remove the code below once we have that. | 178 // cycles. Remove the code below once we have that. |
169 dictionary->DeleteWithStringKey("10"); | 179 dictionary->DeleteWithStringKey("10"); |
170 DictionaryVar* result_dictionary = DictionaryVar::FromPPVar(result.get()); | |
171 result_dictionary->DeleteWithStringKey("10"); | |
172 | 180 |
173 // Array with self references. | 181 // Array with self references. |
174 array->Set(index, release_array.get()); | 182 array->Set(index, release_array.get()); |
175 result = ScopedPPVar(ScopedPPVar::PassRef(), | 183 ASSERT_FALSE(WriteAndRead(release_array.get(), &result)); |
176 WriteAndRead(release_array.get())); | |
177 EXPECT_TRUE(TestEqual(release_array.get(), result.get())); | |
178 // Break the self reference. | 184 // Break the self reference. |
179 array->Set(index, PP_MakeUndefined()); | 185 array->Set(index, PP_MakeUndefined()); |
180 ArrayVar* result_array = ArrayVar::FromPPVar(result.get()); | |
181 result_array->Set(index, PP_MakeUndefined()); | |
182 } | 186 } |
183 | 187 |
184 } // namespace proxy | 188 } // namespace proxy |
185 } // namespace ppapi | 189 } // namespace ppapi |
OLD | NEW |