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

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

Issue 29344967: Issue 4031 - Implement tests for libadblockplus-android (Closed)
Left Patch Set: Created May 27, 2016, 1:38 p.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.EventCallback; 21 import org.adblockplus.libadblockplus.EventCallback;
22 import org.adblockplus.libadblockplus.JsValue; 22 import org.adblockplus.libadblockplus.JsValue;
23 23
24 import org.junit.Test; 24 import org.junit.Test;
25 25
26 import java.util.List; 26 import java.util.List;
27 27
28 public class JsEngineTest extends BaseJsTest { 28 public class JsEngineTest extends BaseJsTest
29 {
30 @Test
31 public void testEvaluate()
32 {
33 jsEngine.evaluate("function hello() { return 'Hello'; }");
34 JsValue result = jsEngine.evaluate("hello()");
35 assertTrue(result.isString());
36 assertEquals("Hello", result.asString());
37 }
29 38
30 @Test 39 @Test
31 public void testEvaluate() { 40 public void testRuntimeExceptionIsThrown()
32 jsEngine.evaluate("function hello() { return 'Hello'; }"); 41 {
33 JsValue result = jsEngine.evaluate("hello()"); 42 try
34 assertTrue(result.isString()); 43 {
35 assertEquals("Hello", result.asString()); 44 jsEngine.evaluate("doesnotexist()");
45 fail();
36 } 46 }
47 catch (AdblockPlusException e)
48 {
49 // ignored
50 }
51 }
37 52
38 @Test 53 @Test
39 public void testRuntimeExceptionIsThrown() { 54 public void testCompileTimeExceptionIsThrown()
40 try { 55 {
41 jsEngine.evaluate("doesnotexist()"); 56 try
42 fail(); 57 {
43 } catch (AdblockPlusException e) { 58 jsEngine.evaluate("'foo'bar'");
44 // ignored 59 fail();
45 }
46 } 60 }
61 catch (AdblockPlusException e)
62 {
63 // ignored
64 }
65 }
47 66
48 @Test 67 @Test
49 public void testCompileTimeExceptionIsThrown() { 68 public void testValueCreation()
50 try { 69 {
51 jsEngine.evaluate("'foo'bar'"); 70 JsValue value;
52 fail(); 71
53 } catch (AdblockPlusException e) { 72 final String STRING_VALUE = "foo";
54 // ignored 73 value = jsEngine.newValue(STRING_VALUE);
55 } 74 assertTrue(value.isString());
75 assertEquals(STRING_VALUE, value.asString());
76
77 final long LONG_VALUE = 12345678901234l;
78 value = jsEngine.newValue(LONG_VALUE);
79 assertTrue(value.isNumber());
80 assertEquals(LONG_VALUE, value.asLong());
81
82 final boolean BOOLEAN_VALUE = true;
83 value = jsEngine.newValue(BOOLEAN_VALUE);
84 assertTrue(value.isBoolean());
85 assertEquals(BOOLEAN_VALUE, value.asBoolean());
86 }
87
88 private boolean callbackCalled;
89 private List<JsValue> callbackParams;
90 private EventCallback callback = new EventCallback()
91 {
92 @Override
93 public void eventCallback(List<JsValue> params)
94 {
95 callbackCalled = true;
96 callbackParams = params;
56 } 97 }
98 };
57 99
58 @Test 100 @Test
59 public void testValueCreation() { 101 public void testEventCallbacks()
60 JsValue value; 102 {
103 callbackCalled = false;
61 104
62 final String STRING_VALUE = "foo"; 105 // Trigger event without a callback
63 value = jsEngine.newValue(STRING_VALUE); 106 callbackCalled = false;
64 assertTrue(value.isString()); 107 jsEngine.evaluate("_triggerEvent('foobar')");
65 assertEquals(STRING_VALUE, value.asString()); 108 assertFalse(callbackCalled);
66 109
67 final long LONG_VALUE = 12345678901234l; 110 // Set callback
68 value = jsEngine.newValue(LONG_VALUE); 111 final String EVENT_NAME = "foobar";
69 assertTrue(value.isNumber()); 112 jsEngine.setEventCallback(EVENT_NAME, callback);
70 assertEquals(LONG_VALUE, value.asLong()); 113 callbackCalled = false;
114 jsEngine.evaluate("_triggerEvent('foobar', 1, 'x', true)");
115 assertTrue(callbackCalled);
116 assertNotNull(callbackParams);
117 assertEquals(3, callbackParams.size());
118 assertEquals(1, callbackParams.get(0).asLong());
119 assertEquals("x", callbackParams.get(1).asString());
120 assertTrue(callbackParams.get(2).asBoolean());
71 121
72 final boolean BOOLEAN_VALUE = true; 122 // Trigger a different event
73 value = jsEngine.newValue(BOOLEAN_VALUE); 123 callbackCalled = false;
74 assertTrue(value.isBoolean()); 124 jsEngine.evaluate("_triggerEvent('barfoo')");
75 assertEquals(BOOLEAN_VALUE, value.asBoolean()); 125 assertFalse(callbackCalled);
76 }
77 126
78 private boolean callbackCalled; 127 // Remove callback
79 private List<JsValue> callbackParams; 128 jsEngine.removeEventCallback(EVENT_NAME);
80 private EventCallback callback = new EventCallback() { 129 callbackCalled = false;
81 @Override 130 jsEngine.evaluate("_triggerEvent('foobar')");
82 public void eventCallback(List<JsValue> params) { 131 assertFalse(callbackCalled);
83 callbackCalled = true; 132 }
84 callbackParams = params;
85 }
86 };
87
88 @Test
89 public void testEventCallbacks() {
90 callbackCalled = false;
91
92 // Trigger event without a callback
93 callbackCalled = false;
94 jsEngine.evaluate("_triggerEvent('foobar')");
95 assertFalse(callbackCalled);
96
97 // Set callback
98 final String EVENT_NAME = "foobar";
99 jsEngine.setEventCallback(EVENT_NAME, callback);
100 callbackCalled = false;
101 jsEngine.evaluate("_triggerEvent('foobar', 1, 'x', true)");
102 assertTrue(callbackCalled);
103 assertNotNull(callbackParams);
104 assertEquals(3, callbackParams.size());
105 assertEquals(1, callbackParams.get(0).asLong());
106 assertEquals("x", callbackParams.get(1).asString());
107 assertTrue(callbackParams.get(2).asBoolean());
108
109 // Trigger a different event
110 callbackCalled = false;
111 jsEngine.evaluate("_triggerEvent('barfoo')");
112 assertFalse(callbackCalled);
113
114 // Remove callback
115 jsEngine.removeEventCallback(EVENT_NAME);
116 callbackCalled = false;
117 jsEngine.evaluate("_triggerEvent('foobar')");
118 assertFalse(callbackCalled);
119 }
120
121 @Test
122 public void testCallbackGetSet() {
123 /*
124 no getters:
125 .getLogSystem()
126 .getFileSystem()
127 .getWebRequest()
128
129 TODO: remove test/add getters?
130 */
131 }
132
133 @Test
134 public void testGlobalProperty() {
135 /*
136 no setter:
137 .setGlobalProperty()
138
139 TODO: remove test/add setter?
140 */
141 }
142
143 } 133 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld