OLD | NEW |
| (Empty) |
1 /* | |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-present 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.EventCallback; | |
22 import org.adblockplus.libadblockplus.JsValue; | |
23 | |
24 import org.junit.Test; | |
25 | |
26 import java.util.List; | |
27 | |
28 public class JsEngineTest extends BaseJsEngineTest | |
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 } | |
38 | |
39 @Test | |
40 public void testRuntimeExceptionIsThrown() | |
41 { | |
42 try | |
43 { | |
44 jsEngine.evaluate("doesnotexist()"); | |
45 fail(); | |
46 } | |
47 catch (AdblockPlusException e) | |
48 { | |
49 // ignored | |
50 } | |
51 } | |
52 | |
53 @Test | |
54 public void testCompileTimeExceptionIsThrown() | |
55 { | |
56 try | |
57 { | |
58 jsEngine.evaluate("'foo'bar'"); | |
59 fail(); | |
60 } | |
61 catch (AdblockPlusException e) | |
62 { | |
63 // ignored | |
64 } | |
65 } | |
66 | |
67 @Test | |
68 public void testValueCreation() | |
69 { | |
70 JsValue value; | |
71 | |
72 final String STRING_VALUE = "foo"; | |
73 value = jsEngine.newValue(STRING_VALUE); | |
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; | |
97 } | |
98 }; | |
99 | |
100 @Test | |
101 public void testEventCallbacks() | |
102 { | |
103 callbackCalled = false; | |
104 | |
105 // Trigger event without a callback | |
106 callbackCalled = false; | |
107 jsEngine.evaluate("_triggerEvent('foobar')"); | |
108 assertFalse(callbackCalled); | |
109 | |
110 // Set callback | |
111 final String EVENT_NAME = "foobar"; | |
112 jsEngine.setEventCallback(EVENT_NAME, callback); | |
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()); | |
121 | |
122 // Trigger a different event | |
123 callbackCalled = false; | |
124 jsEngine.evaluate("_triggerEvent('barfoo')"); | |
125 assertFalse(callbackCalled); | |
126 | |
127 // Remove callback | |
128 jsEngine.removeEventCallback(EVENT_NAME); | |
129 callbackCalled = false; | |
130 jsEngine.evaluate("_triggerEvent('foobar')"); | |
131 assertFalse(callbackCalled); | |
132 } | |
133 } | |
OLD | NEW |