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

Unified Diff: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/JsEngineTest.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.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/JsEngineTest.java
diff --git a/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/JsEngineTest.java b/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/JsEngineTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..3438ed5e4d6df995c75a6a4415913223af719b7d
--- /dev/null
+++ b/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/JsEngineTest.java
@@ -0,0 +1,120 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2016 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package org.adblockplus.libadblockplus.tests;
+
+import org.adblockplus.libadblockplus.AdblockPlusException;
+import org.adblockplus.libadblockplus.EventCallback;
+import org.adblockplus.libadblockplus.JsValue;
+
+import org.junit.Test;
+
+import java.util.List;
+
+public class JsEngineTest extends BaseJsTest {
+
+ @Test
+ public void testEvaluate() {
+ jsEngine.evaluate("function hello() { return 'Hello'; }");
+ JsValue result = jsEngine.evaluate("hello()");
+ assertTrue(result.isString());
+ assertEquals("Hello", result.asString());
+ }
+
+ @Test
+ public void testRuntimeExceptionIsThrown() {
+ try {
+ jsEngine.evaluate("doesnotexist()");
+ fail();
+ } catch (AdblockPlusException e) {
+ // ignored
+ }
+ }
+
+ @Test
+ public void testCompileTimeExceptionIsThrown() {
+ try {
+ jsEngine.evaluate("'foo'bar'");
+ fail();
+ } catch (AdblockPlusException e) {
+ // ignored
+ }
+ }
+
+ @Test
+ public void testValueCreation() {
+ JsValue value;
+
+ final String STRING_VALUE = "foo";
+ value = jsEngine.newValue(STRING_VALUE);
+ assertTrue(value.isString());
+ assertEquals(STRING_VALUE, value.asString());
+
+ final long LONG_VALUE = 12345678901234l;
+ value = jsEngine.newValue(LONG_VALUE);
+ assertTrue(value.isNumber());
+ assertEquals(LONG_VALUE, value.asLong());
+
+ final boolean BOOLEAN_VALUE = true;
+ value = jsEngine.newValue(BOOLEAN_VALUE);
+ assertTrue(value.isBoolean());
+ assertEquals(BOOLEAN_VALUE, value.asBoolean());
+ }
+
+ private boolean callbackCalled;
+ private List<JsValue> callbackParams;
+ private EventCallback callback = new EventCallback() {
+ @Override
+ public void eventCallback(List<JsValue> params) {
+ callbackCalled = true;
+ callbackParams = params;
+ }
+ };
+
+ @Test
+ public void testEventCallbacks() {
+ callbackCalled = false;
+
+ // Trigger event without a callback
+ callbackCalled = false;
+ jsEngine.evaluate("_triggerEvent('foobar')");
+ assertFalse(callbackCalled);
+
+ // Set callback
+ final String EVENT_NAME = "foobar";
+ jsEngine.setEventCallback(EVENT_NAME, callback);
+ callbackCalled = false;
+ jsEngine.evaluate("_triggerEvent('foobar', 1, 'x', true)");
+ assertTrue(callbackCalled);
+ assertNotNull(callbackParams);
+ assertEquals(3, callbackParams.size());
+ assertEquals(1, callbackParams.get(0).asLong());
+ assertEquals("x", callbackParams.get(1).asString());
+ assertTrue(callbackParams.get(2).asBoolean());
+
+ // Trigger a different event
+ callbackCalled = false;
+ jsEngine.evaluate("_triggerEvent('barfoo')");
+ assertFalse(callbackCalled);
+
+ // Remove callback
+ jsEngine.removeEventCallback(EVENT_NAME);
+ callbackCalled = false;
+ jsEngine.evaluate("_triggerEvent('foobar')");
+ assertFalse(callbackCalled);
+ }
+}

Powered by Google App Engine
This is Rietveld