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

Side by Side Diff: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/JsTest.java

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

Powered by Google App Engine
This is Rietveld