OLD | NEW |
(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.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 BaseJsTest { |
| 29 |
| 30 @Test |
| 31 public void testEvaluate() { |
| 32 jsEngine.evaluate("function hello() { return 'Hello'; }"); |
| 33 JsValue result = jsEngine.evaluate("hello()"); |
| 34 assertTrue(result.isString()); |
| 35 assertEquals("Hello", result.asString()); |
| 36 } |
| 37 |
| 38 @Test |
| 39 public void testRuntimeExceptionIsThrown() { |
| 40 try { |
| 41 jsEngine.evaluate("doesnotexist()"); |
| 42 fail(); |
| 43 } catch (AdblockPlusException e) { |
| 44 // ignored |
| 45 } |
| 46 } |
| 47 |
| 48 @Test |
| 49 public void testCompileTimeExceptionIsThrown() { |
| 50 try { |
| 51 jsEngine.evaluate("'foo'bar'"); |
| 52 fail(); |
| 53 } catch (AdblockPlusException e) { |
| 54 // ignored |
| 55 } |
| 56 } |
| 57 |
| 58 @Test |
| 59 public void testValueCreation() { |
| 60 JsValue value; |
| 61 |
| 62 final String STRING_VALUE = "foo"; |
| 63 value = jsEngine.newValue(STRING_VALUE); |
| 64 assertTrue(value.isString()); |
| 65 assertEquals(STRING_VALUE, value.asString()); |
| 66 |
| 67 final long LONG_VALUE = 12345678901234l; |
| 68 value = jsEngine.newValue(LONG_VALUE); |
| 69 assertTrue(value.isNumber()); |
| 70 assertEquals(LONG_VALUE, value.asLong()); |
| 71 |
| 72 final boolean BOOLEAN_VALUE = true; |
| 73 value = jsEngine.newValue(BOOLEAN_VALUE); |
| 74 assertTrue(value.isBoolean()); |
| 75 assertEquals(BOOLEAN_VALUE, value.asBoolean()); |
| 76 } |
| 77 |
| 78 private boolean callbackCalled; |
| 79 private List<JsValue> callbackParams; |
| 80 private EventCallback callback = new EventCallback() { |
| 81 @Override |
| 82 public void eventCallback(List<JsValue> params) { |
| 83 callbackCalled = true; |
| 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 } |
OLD | NEW |