Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/JsTest.java

Issue 29344967: Issue 4031 - Implement tests for libadblockplus-android (Closed)
Left Patch Set: Updated tests, also moved to libadblockplus-android repo Created June 21, 2016, 10:51 a.m.
Right Patch Set: fix typo ".. file[s]" Created Sept. 15, 2016, 10:44 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 package org.adblockplus.libadblockplus.tests; 18 package org.adblockplus.libadblockplus.tests;
19 19
20 import org.adblockplus.libadblockplus.AdblockPlusException; 20 import org.adblockplus.libadblockplus.AdblockPlusException;
21 import org.adblockplus.libadblockplus.JsValue; 21 import org.adblockplus.libadblockplus.JsValue;
22 22
23 import org.junit.Test; 23 import org.junit.Test;
24 24
25 import java.util.ArrayList; 25 import java.util.ArrayList;
26 import java.util.List; 26 import java.util.List;
27 27
28 public class JsTest extends BaseJsTest { 28 public class JsTest extends BaseJsTest
29 29 {
30 @Test 30 @Test
31 public void testUndefinedValue() { 31 public void testUndefinedValue()
32 final JsValue value = jsEngine.evaluate("undefined"); 32 {
33 assertTrue(value.isUndefined()); 33 final JsValue value = jsEngine.evaluate("undefined");
34 assertFalse(value.isNull()); 34 assertTrue(value.isUndefined());
35 assertFalse(value.isString()); 35 assertFalse(value.isNull());
36 assertFalse(value.isBoolean()); 36 assertFalse(value.isString());
37 assertFalse(value.isNumber()); 37 assertFalse(value.isBoolean());
38 assertFalse(value.isObject()); 38 assertFalse(value.isNumber());
39 assertFalse(value.isArray()); 39 assertFalse(value.isObject());
40 assertEquals("undefined", value.asString()); 40 assertFalse(value.isArray());
41 try { 41 assertEquals("undefined", value.asString());
42 value.asList(); 42 try
43 fail(AdblockPlusException.class.getSimpleName() + " is expected to b e thrown"); 43 {
44 } catch (AdblockPlusException e) { 44 value.asList();
45 // ignored 45 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
46 } 46 }
47 try { 47 catch (AdblockPlusException e)
48 value.getProperty("foo"); 48 {
49 fail(AdblockPlusException.class.getSimpleName() + " is expected to b e thrown"); 49 // ignored
50 } catch (AdblockPlusException e) { 50 }
51 // ignored 51 try
52 } 52 {
53 } 53 value.getProperty("foo");
54 54 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
55 @Test 55 }
56 public void testNullValue() { 56 catch (AdblockPlusException e)
57 final JsValue value = jsEngine.evaluate("null"); 57 {
58 assertFalse(value.isUndefined()); 58 // ignored
59 assertTrue(value.isNull()); 59 }
60 assertFalse(value.isString()); 60 }
61 assertFalse(value.isBoolean()); 61
62 assertFalse(value.isNumber()); 62 @Test
63 assertFalse(value.isObject()); 63 public void testNullValue()
64 assertFalse(value.isArray()); 64 {
65 assertFalse(value.isFunction()); 65 final JsValue value = jsEngine.evaluate("null");
66 assertEquals("null", value.asString()); 66 assertFalse(value.isUndefined());
67 assertFalse(value.asBoolean()); 67 assertTrue(value.isNull());
68 try { 68 assertFalse(value.isString());
69 value.asList(); 69 assertFalse(value.isBoolean());
70 fail(AdblockPlusException.class.getSimpleName() + " is expected to b e thrown"); 70 assertFalse(value.isNumber());
71 } catch (AdblockPlusException e) { 71 assertFalse(value.isObject());
72 // ignored 72 assertFalse(value.isArray());
73 } 73 assertFalse(value.isFunction());
74 try { 74 assertEquals("null", value.asString());
75 value.getProperty("foo"); 75 assertFalse(value.asBoolean());
76 fail(AdblockPlusException.class.getSimpleName() + " is expected to b e thrown"); 76 try
77 } catch (AdblockPlusException e) { 77 {
78 // ignored 78 value.asList();
79 } 79 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
80 } 80 }
81 81 catch (AdblockPlusException e)
82 @Test 82 {
83 public void testStringValue() { 83 // ignored
84 JsValue value = jsEngine.evaluate("'123'"); 84 }
85 assertFalse(value.isUndefined()); 85 try
86 assertFalse(value.isNull()); 86 {
87 assertTrue(value.isString()); 87 value.getProperty("foo");
88 assertFalse(value.isBoolean()); 88 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
89 assertFalse(value.isNumber()); 89 }
90 assertFalse(value.isObject()); 90 catch (AdblockPlusException e)
91 assertFalse(value.isArray()); 91 {
92 assertFalse(value.isFunction()); 92 // ignored
93 assertEquals("123", value.asString()); 93 }
94 assertEquals(123l, value.asLong()); 94 }
95 assertTrue(value.asBoolean()); 95
96 try { 96 @Test
97 value.asList(); 97 public void testStringValue()
98 fail(AdblockPlusException.class.getSimpleName() + " is expected to b e thrown"); 98 {
99 } catch (AdblockPlusException e) { 99 JsValue value = jsEngine.evaluate("'123'");
100 // ignored 100 assertFalse(value.isUndefined());
101 } 101 assertFalse(value.isNull());
102 try { 102 assertTrue(value.isString());
103 value.getProperty("foo"); 103 assertFalse(value.isBoolean());
104 fail(AdblockPlusException.class.getSimpleName() + " is expected to b e thrown"); 104 assertFalse(value.isNumber());
105 } catch (AdblockPlusException e) { 105 assertFalse(value.isObject());
106 // ignored 106 assertFalse(value.isArray());
107 } 107 assertFalse(value.isFunction());
108 } 108 assertEquals("123", value.asString());
109 109 assertEquals(123l, value.asLong());
110 @Test 110 assertTrue(value.asBoolean());
111 public void testLongValue() { 111 try
112 JsValue value = jsEngine.evaluate("12345678901234"); 112 {
113 assertFalse(value.isUndefined()); 113 value.asList();
114 assertFalse(value.isNull()); 114 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
115 assertFalse(value.isString()); 115 }
116 assertFalse(value.isBoolean()); 116 catch (AdblockPlusException e)
117 assertTrue(value.isNumber()); 117 {
118 assertFalse(value.isObject()); 118 // ignored
119 assertFalse(value.isArray()); 119 }
120 assertFalse(value.isFunction()); 120 try
121 assertEquals("12345678901234", value.asString()); 121 {
122 assertEquals(12345678901234l, value.asLong()); 122 value.getProperty("foo");
123 assertTrue(value.asBoolean()); 123 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
124 try { 124 }
125 value.asList(); 125 catch (AdblockPlusException e)
126 fail(AdblockPlusException.class.getSimpleName() + " is expected to b e thrown"); 126 {
127 } catch (AdblockPlusException e) { 127 // ignored
128 // ignored 128 }
129 } 129 }
130 try { 130
131 value.getProperty("foo"); 131 @Test
132 fail(AdblockPlusException.class.getSimpleName() + " is expected to b e thrown"); 132 public void testLongValue()
133 } catch (AdblockPlusException e) { 133 {
134 // ignored 134 JsValue value = jsEngine.evaluate("12345678901234");
135 } 135 assertFalse(value.isUndefined());
136 } 136 assertFalse(value.isNull());
137 137 assertFalse(value.isString());
138 @Test 138 assertFalse(value.isBoolean());
139 public void testBoolValue() { 139 assertTrue(value.isNumber());
140 JsValue value = jsEngine.evaluate("true"); 140 assertFalse(value.isObject());
141 assertFalse(value.isUndefined()); 141 assertFalse(value.isArray());
142 assertFalse(value.isNull()); 142 assertFalse(value.isFunction());
143 assertFalse(value.isString()); 143 assertEquals("12345678901234", value.asString());
144 assertTrue(value.isBoolean()); 144 assertEquals(12345678901234l, value.asLong());
145 assertFalse(value.isNumber()); 145 assertTrue(value.asBoolean());
146 assertFalse(value.isObject()); 146 try
147 assertFalse(value.isArray()); 147 {
148 assertFalse(value.isFunction()); 148 value.asList();
149 assertEquals("true", value.asString()); 149 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
150 assertTrue(value.asBoolean()); 150 }
151 try { 151 catch (AdblockPlusException e)
152 value.asList(); 152 {
153 fail(AdblockPlusException.class.getSimpleName() + " is expected to b e thrown"); 153 // ignored
154 } catch (AdblockPlusException e) { 154 }
155 // ignored 155 try
156 } 156 {
157 try { 157 value.getProperty("foo");
158 value.getProperty("foo"); 158 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
159 fail(AdblockPlusException.class.getSimpleName() + " is expected to b e thrown"); 159 }
160 } catch (AdblockPlusException e) { 160 catch (AdblockPlusException e)
161 // ignored 161 {
162 } 162 // ignored
163 } 163 }
164 164 }
165 @Test 165
166 public void testObjectValue() { 166 @Test
167 final String source = 167 public void testBoolValue()
168 "function Foo() {\n" + 168 {
169 " this.x = 2;\n" + 169 JsValue value = jsEngine.evaluate("true");
170 " this.toString = function() {return 'foo';};\n" + 170 assertFalse(value.isUndefined());
171 " this.valueOf = function() {return 123;};\n" + 171 assertFalse(value.isNull());
172 "};\n" + 172 assertFalse(value.isString());
173 "new Foo()"; 173 assertTrue(value.isBoolean());
174 JsValue value = jsEngine.evaluate(source); 174 assertFalse(value.isNumber());
175 assertFalse(value.isUndefined()); 175 assertFalse(value.isObject());
176 assertFalse(value.isNull()); 176 assertFalse(value.isArray());
177 assertFalse(value.isString()); 177 assertFalse(value.isFunction());
178 assertFalse(value.isBoolean()); 178 assertEquals("true", value.asString());
179 assertFalse(value.isNumber()); 179 assertTrue(value.asBoolean());
180 assertTrue(value.isObject()); 180 try
181 assertFalse(value.isArray()); 181 {
182 assertFalse(value.isFunction()); 182 value.asList();
183 assertEquals("foo", value.asString()); 183 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
184 assertEquals(123l, value.asLong()); 184 }
185 assertTrue(value.asBoolean()); 185 catch (AdblockPlusException e)
186 try { 186 {
187 value.asList(); 187 // ignored
188 fail(AdblockPlusException.class.getSimpleName() + " is expected to b e thrown"); 188 }
189 } catch (AdblockPlusException e) { 189 try
190 // ignored 190 {
191 } 191 value.getProperty("foo");
192 assertEquals(2l, value.getProperty("x").asLong()); 192 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
193 } 193 }
194 194 catch (AdblockPlusException e)
195 @Test 195 {
196 public void testArrayValue() { 196 // ignored
197 JsValue value = jsEngine.evaluate("[5,8,12]"); 197 }
198 assertFalse(value.isUndefined()); 198 }
199 assertFalse(value.isNull()); 199
200 assertFalse(value.isString()); 200 @Test
201 assertFalse(value.isBoolean()); 201 public void testObjectValue()
202 assertFalse(value.isNumber()); 202 {
203 assertTrue(value.isObject()); 203 final String source =
204 assertTrue(value.isArray()); 204 "function Foo() {\n" +
205 assertEquals("5,8,12", value.asString()); 205 " this.x = 2;\n" +
206 assertTrue(value.asBoolean()); 206 " this.toString = function() {return 'foo';};\n" +
207 assertEquals(3l, value.asList().size()); 207 " this.valueOf = function() {return 123;};\n" +
208 assertEquals(5l, value.asList().get(0).asLong()); 208 "};\n" +
209 assertEquals(8l, value.asList().get(1).asLong()); 209 "new Foo()";
210 assertEquals(12l, value.asList().get(2).asLong()); 210 JsValue value = jsEngine.evaluate(source);
211 assertEquals(3l, value.getProperty("length").asLong()); 211 assertFalse(value.isUndefined());
212 } 212 assertFalse(value.isNull());
213 213 assertFalse(value.isString());
214 @Test 214 assertFalse(value.isBoolean());
215 public void testFunctionValue() { 215 assertFalse(value.isNumber());
216 JsValue value = jsEngine.evaluate( 216 assertTrue(value.isObject());
217 "(function(foo, bar) {return this.x + '/' + foo + '/' + bar;})"); 217 assertFalse(value.isArray());
218 assertFalse(value.isUndefined()); 218 assertFalse(value.isFunction());
219 assertFalse(value.isNull()); 219 assertEquals("foo", value.asString());
220 assertFalse(value.isString()); 220 assertEquals(123l, value.asLong());
221 assertFalse(value.isBoolean()); 221 assertTrue(value.asBoolean());
222 assertFalse(value.isNumber()); 222 try
223 assertTrue(value.isObject()); 223 {
224 assertFalse(value.isArray()); 224 value.asList();
225 assertTrue(value.isFunction()); 225 fail(AdblockPlusException.class.getSimpleName() + " is expected to be thro wn");
226 assertTrue(value.asBoolean()); 226 }
227 try { 227 catch (AdblockPlusException e)
228 value.asList(); 228 {
229 fail(AdblockPlusException.class.getSimpleName() + " is expected to b e thrown"); 229 // ignored
230 } catch (AdblockPlusException e) { 230 }
231 // ignored 231 assertEquals(2l, value.getProperty("x").asLong());
232 } 232 }
233 assertEquals(2l, value.getProperty("length").asLong()); 233
234 234 @Test
235 JsValue thisValue = jsEngine.evaluate("({x:2})"); 235 public void testArrayValue()
236 List<JsValue> params = new ArrayList<JsValue>(); 236 {
237 params.add(jsEngine.newValue(5l)); 237 JsValue value = jsEngine.evaluate("[5,8,12]");
238 params.add(jsEngine.newValue("xyz")); 238 assertFalse(value.isUndefined());
239 239 assertFalse(value.isNull());
240 // TODO : finish when method jsValue.call() is created 240 assertFalse(value.isString());
241 } 241 assertFalse(value.isBoolean());
242 242 assertFalse(value.isNumber());
243 @Test 243 assertTrue(value.isObject());
244 public void testThrowingCoversion() { 244 assertTrue(value.isArray());
245 final String source = 245 assertEquals("5,8,12", value.asString());
246 "function Foo() {\n" + 246 assertTrue(value.asBoolean());
247 " this.toString = function() {throw 'test1';};\n" + 247 assertEquals(3l, value.asList().size());
248 " this.valueOf = function() {throw 'test2';};\n" + 248 assertEquals(5l, value.asList().get(0).asLong());
249 "};\n" + 249 assertEquals(8l, value.asList().get(1).asLong());
250 "new Foo()"; 250 assertEquals(12l, value.asList().get(2).asLong());
251 251 assertEquals(3l, value.getProperty("length").asLong());
252 JsValue value = jsEngine.evaluate(source); 252 }
253 assertEquals("", value.asString()); 253
254 assertEquals(0l, value.asLong()); 254 @Test
255 } 255 public void testThrowingCoversion()
256 {
257 final String source =
258 "function Foo() {\n" +
259 " this.toString = function() {throw 'test1';};\n" +
260 " this.valueOf = function() {throw 'test2';};\n" +
261 "};\n" +
262 "new Foo()";
263
264 JsValue value = jsEngine.evaluate(source);
265 assertEquals("", value.asString());
266 assertEquals(0l, value.asLong());
267 }
256 } 268 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld