OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 #include "BaseJsTest.h" | 18 #include "BaseJsTest.h" |
19 | 19 |
20 namespace | 20 namespace |
21 { | 21 { |
22 class JsValueTest : public BaseJsTest | 22 class JsValueTest : public BaseJsTest |
23 { | 23 { |
24 }; | 24 }; |
25 } | 25 } |
26 | 26 |
27 TEST_F(JsValueTest, UndefinedValue) | 27 TEST_F(JsValueTest, UndefinedValue) |
28 { | 28 { |
29 auto value = platform->GetJsEngine()->Evaluate("undefined"); | 29 auto value = GetJsEngine().Evaluate("undefined"); |
30 ASSERT_TRUE(value.IsUndefined()); | 30 ASSERT_TRUE(value.IsUndefined()); |
31 ASSERT_FALSE(value.IsNull()); | 31 ASSERT_FALSE(value.IsNull()); |
32 ASSERT_FALSE(value.IsString()); | 32 ASSERT_FALSE(value.IsString()); |
33 ASSERT_FALSE(value.IsBool()); | 33 ASSERT_FALSE(value.IsBool()); |
34 ASSERT_FALSE(value.IsNumber()); | 34 ASSERT_FALSE(value.IsNumber()); |
35 ASSERT_FALSE(value.IsObject()); | 35 ASSERT_FALSE(value.IsObject()); |
36 ASSERT_FALSE(value.IsArray()); | 36 ASSERT_FALSE(value.IsArray()); |
37 ASSERT_FALSE(value.IsFunction()); | 37 ASSERT_FALSE(value.IsFunction()); |
38 ASSERT_EQ("undefined", value.AsString()); | 38 ASSERT_EQ("undefined", value.AsString()); |
39 ASSERT_FALSE(value.AsBool()); | 39 ASSERT_FALSE(value.AsBool()); |
40 ASSERT_ANY_THROW(value.AsList()); | 40 ASSERT_ANY_THROW(value.AsList()); |
41 ASSERT_ANY_THROW(value.GetProperty("foo")); | 41 ASSERT_ANY_THROW(value.GetProperty("foo")); |
42 ASSERT_ANY_THROW(value.SetProperty("foo", false)); | 42 ASSERT_ANY_THROW(value.SetProperty("foo", false)); |
43 ASSERT_ANY_THROW(value.GetClass()); | 43 ASSERT_ANY_THROW(value.GetClass()); |
44 ASSERT_ANY_THROW(value.GetOwnPropertyNames()); | 44 ASSERT_ANY_THROW(value.GetOwnPropertyNames()); |
45 ASSERT_ANY_THROW(value.Call()); | 45 ASSERT_ANY_THROW(value.Call()); |
46 } | 46 } |
47 | 47 |
48 TEST_F(JsValueTest, NullValue) | 48 TEST_F(JsValueTest, NullValue) |
49 { | 49 { |
50 auto value = platform->GetJsEngine()->Evaluate("null"); | 50 auto value = GetJsEngine().Evaluate("null"); |
51 ASSERT_FALSE(value.IsUndefined()); | 51 ASSERT_FALSE(value.IsUndefined()); |
52 ASSERT_TRUE(value.IsNull()); | 52 ASSERT_TRUE(value.IsNull()); |
53 ASSERT_FALSE(value.IsString()); | 53 ASSERT_FALSE(value.IsString()); |
54 ASSERT_FALSE(value.IsBool()); | 54 ASSERT_FALSE(value.IsBool()); |
55 ASSERT_FALSE(value.IsNumber()); | 55 ASSERT_FALSE(value.IsNumber()); |
56 ASSERT_FALSE(value.IsObject()); | 56 ASSERT_FALSE(value.IsObject()); |
57 ASSERT_FALSE(value.IsArray()); | 57 ASSERT_FALSE(value.IsArray()); |
58 ASSERT_FALSE(value.IsFunction()); | 58 ASSERT_FALSE(value.IsFunction()); |
59 ASSERT_EQ("null", value.AsString()); | 59 ASSERT_EQ("null", value.AsString()); |
60 ASSERT_FALSE(value.AsBool()); | 60 ASSERT_FALSE(value.AsBool()); |
61 ASSERT_ANY_THROW(value.AsList()); | 61 ASSERT_ANY_THROW(value.AsList()); |
62 ASSERT_ANY_THROW(value.GetProperty("foo")); | 62 ASSERT_ANY_THROW(value.GetProperty("foo")); |
63 ASSERT_ANY_THROW(value.SetProperty("foo", false)); | 63 ASSERT_ANY_THROW(value.SetProperty("foo", false)); |
64 ASSERT_ANY_THROW(value.GetClass()); | 64 ASSERT_ANY_THROW(value.GetClass()); |
65 ASSERT_ANY_THROW(value.GetOwnPropertyNames()); | 65 ASSERT_ANY_THROW(value.GetOwnPropertyNames()); |
66 ASSERT_ANY_THROW(value.Call()); | 66 ASSERT_ANY_THROW(value.Call()); |
67 } | 67 } |
68 | 68 |
69 TEST_F(JsValueTest, StringValue) | 69 TEST_F(JsValueTest, StringValue) |
70 { | 70 { |
71 auto value = platform->GetJsEngine()->Evaluate("'123'"); | 71 auto value = GetJsEngine().Evaluate("'123'"); |
72 ASSERT_FALSE(value.IsUndefined()); | 72 ASSERT_FALSE(value.IsUndefined()); |
73 ASSERT_FALSE(value.IsNull()); | 73 ASSERT_FALSE(value.IsNull()); |
74 ASSERT_TRUE(value.IsString()); | 74 ASSERT_TRUE(value.IsString()); |
75 ASSERT_FALSE(value.IsBool()); | 75 ASSERT_FALSE(value.IsBool()); |
76 ASSERT_FALSE(value.IsNumber()); | 76 ASSERT_FALSE(value.IsNumber()); |
77 ASSERT_FALSE(value.IsObject()); | 77 ASSERT_FALSE(value.IsObject()); |
78 ASSERT_FALSE(value.IsArray()); | 78 ASSERT_FALSE(value.IsArray()); |
79 ASSERT_FALSE(value.IsFunction()); | 79 ASSERT_FALSE(value.IsFunction()); |
80 ASSERT_EQ("123", value.AsString()); | 80 ASSERT_EQ("123", value.AsString()); |
81 ASSERT_EQ(123, value.AsInt()); | 81 ASSERT_EQ(123, value.AsInt()); |
82 ASSERT_TRUE(value.AsBool()); | 82 ASSERT_TRUE(value.AsBool()); |
83 ASSERT_ANY_THROW(value.AsList()); | 83 ASSERT_ANY_THROW(value.AsList()); |
84 ASSERT_ANY_THROW(value.GetProperty("foo")); | 84 ASSERT_ANY_THROW(value.GetProperty("foo")); |
85 ASSERT_ANY_THROW(value.SetProperty("foo", false)); | 85 ASSERT_ANY_THROW(value.SetProperty("foo", false)); |
86 ASSERT_ANY_THROW(value.GetClass()); | 86 ASSERT_ANY_THROW(value.GetClass()); |
87 ASSERT_ANY_THROW(value.GetOwnPropertyNames()); | 87 ASSERT_ANY_THROW(value.GetOwnPropertyNames()); |
88 ASSERT_ANY_THROW(value.Call()); | 88 ASSERT_ANY_THROW(value.Call()); |
89 } | 89 } |
90 | 90 |
91 TEST_F(JsValueTest, IntValue) | 91 TEST_F(JsValueTest, IntValue) |
92 { | 92 { |
93 auto value = platform->GetJsEngine()->Evaluate("12345678901234"); | 93 auto value = GetJsEngine().Evaluate("12345678901234"); |
94 ASSERT_FALSE(value.IsUndefined()); | 94 ASSERT_FALSE(value.IsUndefined()); |
95 ASSERT_FALSE(value.IsNull()); | 95 ASSERT_FALSE(value.IsNull()); |
96 ASSERT_FALSE(value.IsString()); | 96 ASSERT_FALSE(value.IsString()); |
97 ASSERT_FALSE(value.IsBool()); | 97 ASSERT_FALSE(value.IsBool()); |
98 ASSERT_TRUE(value.IsNumber()); | 98 ASSERT_TRUE(value.IsNumber()); |
99 ASSERT_FALSE(value.IsObject()); | 99 ASSERT_FALSE(value.IsObject()); |
100 ASSERT_FALSE(value.IsArray()); | 100 ASSERT_FALSE(value.IsArray()); |
101 ASSERT_FALSE(value.IsFunction()); | 101 ASSERT_FALSE(value.IsFunction()); |
102 ASSERT_EQ("12345678901234", value.AsString()); | 102 ASSERT_EQ("12345678901234", value.AsString()); |
103 ASSERT_EQ(12345678901234, value.AsInt()); | 103 ASSERT_EQ(12345678901234, value.AsInt()); |
104 ASSERT_TRUE(value.AsBool()); | 104 ASSERT_TRUE(value.AsBool()); |
105 ASSERT_ANY_THROW(value.AsList()); | 105 ASSERT_ANY_THROW(value.AsList()); |
106 ASSERT_ANY_THROW(value.GetProperty("foo")); | 106 ASSERT_ANY_THROW(value.GetProperty("foo")); |
107 ASSERT_ANY_THROW(value.SetProperty("foo", false)); | 107 ASSERT_ANY_THROW(value.SetProperty("foo", false)); |
108 ASSERT_ANY_THROW(value.GetClass()); | 108 ASSERT_ANY_THROW(value.GetClass()); |
109 ASSERT_ANY_THROW(value.GetOwnPropertyNames()); | 109 ASSERT_ANY_THROW(value.GetOwnPropertyNames()); |
110 ASSERT_ANY_THROW(value.Call()); | 110 ASSERT_ANY_THROW(value.Call()); |
111 } | 111 } |
112 | 112 |
113 TEST_F(JsValueTest, BoolValue) | 113 TEST_F(JsValueTest, BoolValue) |
114 { | 114 { |
115 auto value = platform->GetJsEngine()->Evaluate("true"); | 115 auto value = GetJsEngine().Evaluate("true"); |
116 ASSERT_FALSE(value.IsUndefined()); | 116 ASSERT_FALSE(value.IsUndefined()); |
117 ASSERT_FALSE(value.IsNull()); | 117 ASSERT_FALSE(value.IsNull()); |
118 ASSERT_FALSE(value.IsString()); | 118 ASSERT_FALSE(value.IsString()); |
119 ASSERT_TRUE(value.IsBool()); | 119 ASSERT_TRUE(value.IsBool()); |
120 ASSERT_FALSE(value.IsNumber()); | 120 ASSERT_FALSE(value.IsNumber()); |
121 ASSERT_FALSE(value.IsObject()); | 121 ASSERT_FALSE(value.IsObject()); |
122 ASSERT_FALSE(value.IsArray()); | 122 ASSERT_FALSE(value.IsArray()); |
123 ASSERT_FALSE(value.IsFunction()); | 123 ASSERT_FALSE(value.IsFunction()); |
124 ASSERT_EQ("true", value.AsString()); | 124 ASSERT_EQ("true", value.AsString()); |
125 ASSERT_TRUE(value.AsBool()); | 125 ASSERT_TRUE(value.AsBool()); |
126 ASSERT_ANY_THROW(value.AsList()); | 126 ASSERT_ANY_THROW(value.AsList()); |
127 ASSERT_ANY_THROW(value.GetProperty("foo")); | 127 ASSERT_ANY_THROW(value.GetProperty("foo")); |
128 ASSERT_ANY_THROW(value.SetProperty("foo", false)); | 128 ASSERT_ANY_THROW(value.SetProperty("foo", false)); |
129 ASSERT_ANY_THROW(value.GetClass()); | 129 ASSERT_ANY_THROW(value.GetClass()); |
130 ASSERT_ANY_THROW(value.GetOwnPropertyNames()); | 130 ASSERT_ANY_THROW(value.GetOwnPropertyNames()); |
131 ASSERT_ANY_THROW(value.Call()); | 131 ASSERT_ANY_THROW(value.Call()); |
132 } | 132 } |
133 | 133 |
134 TEST_F(JsValueTest, ObjectValue) | 134 TEST_F(JsValueTest, ObjectValue) |
135 { | 135 { |
136 const std::string source("\ | 136 const std::string source("\ |
137 function Foo() {\ | 137 function Foo() {\ |
138 this.x = 2;\ | 138 this.x = 2;\ |
139 this.toString = function() {return 'foo';};\ | 139 this.toString = function() {return 'foo';};\ |
140 this.valueOf = function() {return 123;};\ | 140 this.valueOf = function() {return 123;};\ |
141 };\ | 141 };\ |
142 new Foo()"); | 142 new Foo()"); |
143 auto value = platform->GetJsEngine()->Evaluate(source); | 143 auto value = GetJsEngine().Evaluate(source); |
144 ASSERT_FALSE(value.IsUndefined()); | 144 ASSERT_FALSE(value.IsUndefined()); |
145 ASSERT_FALSE(value.IsNull()); | 145 ASSERT_FALSE(value.IsNull()); |
146 ASSERT_FALSE(value.IsString()); | 146 ASSERT_FALSE(value.IsString()); |
147 ASSERT_FALSE(value.IsBool()); | 147 ASSERT_FALSE(value.IsBool()); |
148 ASSERT_FALSE(value.IsNumber()); | 148 ASSERT_FALSE(value.IsNumber()); |
149 ASSERT_TRUE(value.IsObject()); | 149 ASSERT_TRUE(value.IsObject()); |
150 ASSERT_FALSE(value.IsArray()); | 150 ASSERT_FALSE(value.IsArray()); |
151 ASSERT_FALSE(value.IsFunction()); | 151 ASSERT_FALSE(value.IsFunction()); |
152 ASSERT_EQ("foo", value.AsString()); | 152 ASSERT_EQ("foo", value.AsString()); |
153 ASSERT_EQ(123, value.AsInt()); | 153 ASSERT_EQ(123, value.AsInt()); |
154 ASSERT_TRUE(value.AsBool()); | 154 ASSERT_TRUE(value.AsBool()); |
155 ASSERT_ANY_THROW(value.AsList()); | 155 ASSERT_ANY_THROW(value.AsList()); |
156 ASSERT_EQ(2, value.GetProperty("x").AsInt()); | 156 ASSERT_EQ(2, value.GetProperty("x").AsInt()); |
157 value.SetProperty("x", 12); | 157 value.SetProperty("x", 12); |
158 ASSERT_EQ(12, value.GetProperty("x").AsInt()); | 158 ASSERT_EQ(12, value.GetProperty("x").AsInt()); |
159 value.SetProperty("x", platform->GetJsEngine()->NewValue(15)); | 159 value.SetProperty("x", GetJsEngine().NewValue(15)); |
160 ASSERT_EQ(15, value.GetProperty("x").AsInt()); | 160 ASSERT_EQ(15, value.GetProperty("x").AsInt()); |
161 ASSERT_EQ("Foo", value.GetClass()); | 161 ASSERT_EQ("Foo", value.GetClass()); |
162 ASSERT_EQ(3u, value.GetOwnPropertyNames().size()); | 162 ASSERT_EQ(3u, value.GetOwnPropertyNames().size()); |
163 ASSERT_ANY_THROW(value.Call()); | 163 ASSERT_ANY_THROW(value.Call()); |
164 } | 164 } |
165 | 165 |
166 TEST_F(JsValueTest, ArrayValue) | 166 TEST_F(JsValueTest, ArrayValue) |
167 { | 167 { |
168 auto value = platform->GetJsEngine()->Evaluate("[5,8,12]"); | 168 auto value = GetJsEngine().Evaluate("[5,8,12]"); |
169 ASSERT_FALSE(value.IsUndefined()); | 169 ASSERT_FALSE(value.IsUndefined()); |
170 ASSERT_FALSE(value.IsNull()); | 170 ASSERT_FALSE(value.IsNull()); |
171 ASSERT_FALSE(value.IsString()); | 171 ASSERT_FALSE(value.IsString()); |
172 ASSERT_FALSE(value.IsBool()); | 172 ASSERT_FALSE(value.IsBool()); |
173 ASSERT_FALSE(value.IsNumber()); | 173 ASSERT_FALSE(value.IsNumber()); |
174 ASSERT_TRUE(value.IsObject()); | 174 ASSERT_TRUE(value.IsObject()); |
175 ASSERT_TRUE(value.IsArray()); | 175 ASSERT_TRUE(value.IsArray()); |
176 ASSERT_FALSE(value.IsFunction()); | 176 ASSERT_FALSE(value.IsFunction()); |
177 ASSERT_EQ("5,8,12", value.AsString()); | 177 ASSERT_EQ("5,8,12", value.AsString()); |
178 ASSERT_TRUE(value.AsBool()); | 178 ASSERT_TRUE(value.AsBool()); |
179 ASSERT_EQ(3u, value.AsList().size()); | 179 ASSERT_EQ(3u, value.AsList().size()); |
180 ASSERT_EQ(8, value.AsList()[1].AsInt()); | 180 ASSERT_EQ(8, value.AsList()[1].AsInt()); |
181 ASSERT_EQ(3, value.GetProperty("length").AsInt()); | 181 ASSERT_EQ(3, value.GetProperty("length").AsInt()); |
182 ASSERT_EQ("Array", value.GetClass()); | 182 ASSERT_EQ("Array", value.GetClass()); |
183 ASSERT_ANY_THROW(value.Call()); | 183 ASSERT_ANY_THROW(value.Call()); |
184 } | 184 } |
185 | 185 |
186 TEST_F(JsValueTest, FunctionValue) | 186 TEST_F(JsValueTest, FunctionValue) |
187 { | 187 { |
188 auto value = platform->GetJsEngine()->Evaluate("(function(foo, bar) {return th
is.x + '/' + foo + '/' + bar;})"); | 188 auto value = GetJsEngine().Evaluate("(function(foo, bar) {return this.x + '/'
+ foo + '/' + bar;})"); |
189 ASSERT_FALSE(value.IsUndefined()); | 189 ASSERT_FALSE(value.IsUndefined()); |
190 ASSERT_FALSE(value.IsNull()); | 190 ASSERT_FALSE(value.IsNull()); |
191 ASSERT_FALSE(value.IsString()); | 191 ASSERT_FALSE(value.IsString()); |
192 ASSERT_FALSE(value.IsBool()); | 192 ASSERT_FALSE(value.IsBool()); |
193 ASSERT_FALSE(value.IsNumber()); | 193 ASSERT_FALSE(value.IsNumber()); |
194 ASSERT_TRUE(value.IsObject()); | 194 ASSERT_TRUE(value.IsObject()); |
195 ASSERT_FALSE(value.IsArray()); | 195 ASSERT_FALSE(value.IsArray()); |
196 ASSERT_TRUE(value.IsFunction()); | 196 ASSERT_TRUE(value.IsFunction()); |
197 ASSERT_TRUE(value.AsBool()); | 197 ASSERT_TRUE(value.AsBool()); |
198 ASSERT_ANY_THROW(value.AsList()); | 198 ASSERT_ANY_THROW(value.AsList()); |
199 ASSERT_EQ(2, value.GetProperty("length").AsInt()); | 199 ASSERT_EQ(2, value.GetProperty("length").AsInt()); |
200 | 200 |
201 auto thisPtr = platform->GetJsEngine()->Evaluate("({x:2})"); | 201 auto thisPtr = GetJsEngine().Evaluate("({x:2})"); |
202 AdblockPlus::JsValueList params; | 202 AdblockPlus::JsValueList params; |
203 params.push_back(platform->GetJsEngine()->NewValue(5)); | 203 params.push_back(GetJsEngine().NewValue(5)); |
204 params.push_back(platform->GetJsEngine()->NewValue("xyz")); | 204 params.push_back(GetJsEngine().NewValue("xyz")); |
205 ASSERT_EQ("2/5/xyz", value.Call(params, thisPtr).AsString()); | 205 ASSERT_EQ("2/5/xyz", value.Call(params, thisPtr).AsString()); |
206 } | 206 } |
207 | 207 |
208 TEST_F(JsValueTest, JsValueCallSingleArg) | 208 TEST_F(JsValueTest, JsValueCallSingleArg) |
209 { | 209 { |
210 auto func = platform->GetJsEngine()->Evaluate("(function(arg) {return arg * 2;
})"); | 210 auto func = GetJsEngine().Evaluate("(function(arg) {return arg * 2;})"); |
211 EXPECT_EQ(10, func.Call(platform->GetJsEngine()->NewValue(5)).AsInt()); | 211 EXPECT_EQ(10, func.Call(GetJsEngine().NewValue(5)).AsInt()); |
212 } | 212 } |
213 | 213 |
214 TEST_F(JsValueTest, ThrowingCoversion) | 214 TEST_F(JsValueTest, ThrowingCoversion) |
215 { | 215 { |
216 const std::string source("\ | 216 const std::string source("\ |
217 function Foo() {\ | 217 function Foo() {\ |
218 this.toString = function() {throw 'test1';};\ | 218 this.toString = function() {throw 'test1';};\ |
219 this.valueOf = function() {throw 'test2';};\ | 219 this.valueOf = function() {throw 'test2';};\ |
220 };\ | 220 };\ |
221 new Foo()"); | 221 new Foo()"); |
222 auto value = platform->GetJsEngine()->Evaluate(source); | 222 auto value = GetJsEngine().Evaluate(source); |
223 ASSERT_EQ("", value.AsString()); | 223 ASSERT_EQ("", value.AsString()); |
224 ASSERT_EQ(0, value.AsInt()); | 224 ASSERT_EQ(0, value.AsInt()); |
225 } | 225 } |
OLD | NEW |