OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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/tests/test_var.h" | |
6 | |
7 #include <limits> | |
8 | |
9 #include "ppapi/c/pp_var.h" | |
10 #include "ppapi/c/ppb_var.h" | |
11 #include "ppapi/cpp/module.h" | |
12 #include "ppapi/cpp/var.h" | |
13 #include "ppapi/tests/test_utils.h" | |
14 #include "ppapi/tests/testing_instance.h" | |
15 | |
16 REGISTER_TEST_CASE(Var); | |
17 | |
18 namespace { | |
19 pp::Var adoptVar(PP_Var var) { | |
20 return pp::Var(pp::Var::PassRef(), var); | |
21 } | |
22 } // namespace | |
23 | |
24 bool TestVar::Init() { | |
25 var_interface_ = reinterpret_cast<PPB_Var const*>( | |
26 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); | |
27 return var_interface_ && InitTestingInterface(); | |
28 } | |
29 | |
30 void TestVar::RunTest() { | |
31 RUN_TEST(ConvertType); | |
32 RUN_TEST(DefineProperty); | |
33 } | |
34 | |
35 std::string TestVar::TestConvertType() { | |
36 pp::Var result; | |
37 PP_Var exception = PP_MakeUndefined(); | |
38 double NaN = std::numeric_limits<double>::quiet_NaN(); | |
39 | |
40 // Int to string | |
41 result = adoptVar(var_interface_->ConvertType(instance_->pp_instance(), | |
42 pp::Var(100).pp_var(), | |
43 PP_VARTYPE_STRING, | |
44 &exception)); | |
45 ASSERT_EQ(pp::Var("100"), result); | |
46 ASSERT_TRUE(adoptVar(exception).is_undefined()); | |
47 | |
48 // Int to double | |
49 result = adoptVar(var_interface_->ConvertType(instance_->pp_instance(), | |
50 pp::Var(100).pp_var(), | |
51 PP_VARTYPE_DOUBLE, | |
52 &exception)); | |
53 ASSERT_EQ(pp::Var(100.0), result); | |
54 ASSERT_TRUE(adoptVar(exception).is_undefined()); | |
55 | |
56 // Double to int | |
57 result = adoptVar(var_interface_->ConvertType(instance_->pp_instance(), | |
58 pp::Var(100.0).pp_var(), | |
59 PP_VARTYPE_INT32, | |
60 &exception)); | |
61 ASSERT_EQ(pp::Var(100), result); | |
62 ASSERT_TRUE(adoptVar(exception).is_undefined()); | |
63 | |
64 // Double(NaN) to int | |
65 result = adoptVar(var_interface_->ConvertType(instance_->pp_instance(), | |
66 pp::Var(NaN).pp_var(), | |
67 PP_VARTYPE_INT32, | |
68 &exception)); | |
69 ASSERT_EQ(pp::Var(0), result); | |
70 ASSERT_TRUE(adoptVar(exception).is_undefined()); | |
71 | |
72 // Double to string | |
73 result = adoptVar(var_interface_->ConvertType(instance_->pp_instance(), | |
74 pp::Var(100.0).pp_var(), | |
75 PP_VARTYPE_STRING, | |
76 &exception)); | |
77 ASSERT_EQ(pp::Var("100"), result); | |
78 ASSERT_TRUE(adoptVar(exception).is_undefined()); | |
79 | |
80 // Double(NaN) to string | |
81 result = adoptVar(var_interface_->ConvertType(instance_->pp_instance(), | |
82 pp::Var(NaN).pp_var(), | |
83 PP_VARTYPE_STRING, | |
84 &exception)); | |
85 ASSERT_EQ(pp::Var("NaN"), result); | |
86 ASSERT_TRUE(adoptVar(exception).is_undefined()); | |
87 | |
88 // String to int, valid string | |
89 result = adoptVar(var_interface_->ConvertType(instance_->pp_instance(), | |
90 pp::Var("100").pp_var(), | |
91 PP_VARTYPE_INT32, | |
92 &exception)); | |
93 ASSERT_EQ(pp::Var(100), result); | |
94 ASSERT_TRUE(adoptVar(exception).is_undefined()); | |
95 | |
96 // String to int, invalid string | |
97 result = adoptVar(var_interface_->ConvertType(instance_->pp_instance(), | |
98 pp::Var("jockey").pp_var(), | |
99 PP_VARTYPE_INT32, | |
100 &exception)); | |
101 ASSERT_EQ(pp::Var(0), result); | |
102 ASSERT_TRUE(adoptVar(exception).is_undefined()); | |
103 | |
104 PASS(); | |
105 } | |
106 | |
107 std::string TestVar::TestDefineProperty() { | |
108 pp::Var exception; | |
109 pp::Var property; | |
110 pp::Var value; | |
111 PP_Var unmanaged_exception = PP_MakeUndefined(); | |
112 | |
113 // Create an empty object. | |
114 pp::Var test_obj = instance_->ExecuteScript("({})", &exception); | |
115 ASSERT_TRUE(exception.is_undefined()); | |
116 ASSERT_TRUE(test_obj.is_object()); | |
117 | |
118 // Define a simple property. | |
119 property = "x"; | |
120 value = 1001; | |
121 var_interface_->DefineProperty(test_obj.pp_var(), | |
122 PP_MakeSimpleProperty(property.pp_var(), | |
123 value.pp_var()), | |
124 &unmanaged_exception); | |
125 ASSERT_TRUE(adoptVar(unmanaged_exception).is_undefined()); | |
126 | |
127 ASSERT_EQ(value, test_obj.GetProperty(property, &exception)); | |
128 ASSERT_TRUE(exception.is_undefined()); | |
129 | |
130 // Define a property with a getter that always returns 123 and setter that | |
131 // sets another property to the given value. | |
132 property = "y"; | |
133 pp::Var getter = instance_->ExecuteScript( | |
134 "(function(){return 'okey';})", &exception); | |
135 ASSERT_TRUE(getter.is_object()); | |
136 ASSERT_TRUE(exception.is_undefined()); | |
137 pp::Var setter = instance_->ExecuteScript( | |
138 "(function(x){this['another']=x;})", &exception); | |
139 ASSERT_TRUE(setter.is_object()); | |
140 ASSERT_TRUE(exception.is_undefined()); | |
141 | |
142 struct PP_ObjectProperty property_attributes = { | |
143 property.pp_var(), | |
144 PP_MakeUndefined(), | |
145 getter.pp_var(), | |
146 setter.pp_var(), | |
147 PP_OBJECTPROPERTY_MODIFIER_NONE | |
148 }; | |
149 var_interface_->DefineProperty(test_obj.pp_var(), property_attributes, | |
150 &unmanaged_exception); | |
151 ASSERT_TRUE(adoptVar(unmanaged_exception).is_undefined()); | |
152 | |
153 value = test_obj.GetProperty(property, &exception); | |
154 ASSERT_EQ(pp::Var("okey"), value); | |
155 ASSERT_TRUE(exception.is_undefined()); | |
156 | |
157 value = test_obj.GetProperty("another", &exception); | |
158 ASSERT_TRUE(value.is_undefined()); | |
159 ASSERT_TRUE(exception.is_undefined()); | |
160 | |
161 test_obj.SetProperty("another", "dokey", &exception); | |
162 ASSERT_TRUE(exception.is_undefined()); | |
163 | |
164 ASSERT_EQ(pp::Var("dokey"), test_obj.GetProperty("another", &exception)); | |
165 ASSERT_TRUE(exception.is_undefined()); | |
166 | |
167 PASS(); | |
168 } | |
OLD | NEW |