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

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

Issue 29348972: Issue 4239 - Change indentation to reflect our coding style (Closed)
Patch Set: Created Aug. 2, 2016, 10:43 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
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 { 29 {
30 @Test 30 @Test
31 public void testEvaluate() 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
32 { 43 {
33 jsEngine.evaluate("function hello() { return 'Hello'; }"); 44 jsEngine.evaluate("doesnotexist()");
34 JsValue result = jsEngine.evaluate("hello()"); 45 fail();
35 assertTrue(result.isString()); 46 } catch (AdblockPlusException e)
36 assertEquals("Hello", result.asString()); 47 {
48 // ignored
37 } 49 }
50 }
38 51
39 @Test 52 @Test
40 public void testRuntimeExceptionIsThrown() 53 public void testCompileTimeExceptionIsThrown()
54 {
55 try
41 { 56 {
42 try 57 jsEngine.evaluate("'foo'bar'");
43 { 58 fail();
44 jsEngine.evaluate("doesnotexist()"); 59 } catch (AdblockPlusException e)
45 fail(); 60 {
46 } catch (AdblockPlusException e) 61 // ignored
47 {
48 // ignored
49 }
50 } 62 }
63 }
51 64
52 @Test 65 @Test
53 public void testCompileTimeExceptionIsThrown() 66 public void testValueCreation()
67 {
68 JsValue value;
69
70 final String STRING_VALUE = "foo";
71 value = jsEngine.newValue(STRING_VALUE);
72 assertTrue(value.isString());
73 assertEquals(STRING_VALUE, value.asString());
74
75 final long LONG_VALUE = 12345678901234l;
76 value = jsEngine.newValue(LONG_VALUE);
77 assertTrue(value.isNumber());
78 assertEquals(LONG_VALUE, value.asLong());
79
80 final boolean BOOLEAN_VALUE = true;
81 value = jsEngine.newValue(BOOLEAN_VALUE);
82 assertTrue(value.isBoolean());
83 assertEquals(BOOLEAN_VALUE, value.asBoolean());
84 }
85
86 private boolean callbackCalled;
87 private List<JsValue> callbackParams;
88 private EventCallback callback = new EventCallback()
89 {
90 @Override
91 public void eventCallback(List<JsValue> params)
54 { 92 {
55 try 93 callbackCalled = true;
56 { 94 callbackParams = params;
57 jsEngine.evaluate("'foo'bar'");
58 fail();
59 } catch (AdblockPlusException e)
60 {
61 // ignored
62 }
63 } 95 }
96 };
64 97
65 @Test 98 @Test
66 public void testValueCreation() 99 public void testEventCallbacks()
67 { 100 {
68 JsValue value; 101 callbackCalled = false;
69 102
70 final String STRING_VALUE = "foo"; 103 // Trigger event without a callback
71 value = jsEngine.newValue(STRING_VALUE); 104 callbackCalled = false;
72 assertTrue(value.isString()); 105 jsEngine.evaluate("_triggerEvent('foobar')");
73 assertEquals(STRING_VALUE, value.asString()); 106 assertFalse(callbackCalled);
74 107
75 final long LONG_VALUE = 12345678901234l; 108 // Set callback
76 value = jsEngine.newValue(LONG_VALUE); 109 final String EVENT_NAME = "foobar";
77 assertTrue(value.isNumber()); 110 jsEngine.setEventCallback(EVENT_NAME, callback);
78 assertEquals(LONG_VALUE, value.asLong()); 111 callbackCalled = false;
112 jsEngine.evaluate("_triggerEvent('foobar', 1, 'x', true)");
113 assertTrue(callbackCalled);
114 assertNotNull(callbackParams);
115 assertEquals(3, callbackParams.size());
116 assertEquals(1, callbackParams.get(0).asLong());
117 assertEquals("x", callbackParams.get(1).asString());
118 assertTrue(callbackParams.get(2).asBoolean());
79 119
80 final boolean BOOLEAN_VALUE = true; 120 // Trigger a different event
81 value = jsEngine.newValue(BOOLEAN_VALUE); 121 callbackCalled = false;
82 assertTrue(value.isBoolean()); 122 jsEngine.evaluate("_triggerEvent('barfoo')");
83 assertEquals(BOOLEAN_VALUE, value.asBoolean()); 123 assertFalse(callbackCalled);
84 }
85 124
86 private boolean callbackCalled; 125 // Remove callback
87 private List<JsValue> callbackParams; 126 jsEngine.removeEventCallback(EVENT_NAME);
88 private EventCallback callback = new EventCallback() 127 callbackCalled = false;
89 { 128 jsEngine.evaluate("_triggerEvent('foobar')");
90 @Override 129 assertFalse(callbackCalled);
91 public void eventCallback(List<JsValue> params) 130 }
92 {
93 callbackCalled = true;
94 callbackParams = params;
95 }
96 };
97
98 @Test
99 public void testEventCallbacks()
100 {
101 callbackCalled = false;
102
103 // Trigger event without a callback
104 callbackCalled = false;
105 jsEngine.evaluate("_triggerEvent('foobar')");
106 assertFalse(callbackCalled);
107
108 // Set callback
109 final String EVENT_NAME = "foobar";
110 jsEngine.setEventCallback(EVENT_NAME, callback);
111 callbackCalled = false;
112 jsEngine.evaluate("_triggerEvent('foobar', 1, 'x', true)");
113 assertTrue(callbackCalled);
114 assertNotNull(callbackParams);
115 assertEquals(3, callbackParams.size());
116 assertEquals(1, callbackParams.get(0).asLong());
117 assertEquals("x", callbackParams.get(1).asString());
118 assertTrue(callbackParams.get(2).asBoolean());
119
120 // Trigger a different event
121 callbackCalled = false;
122 jsEngine.evaluate("_triggerEvent('barfoo')");
123 assertFalse(callbackCalled);
124
125 // Remove callback
126 jsEngine.removeEventCallback(EVENT_NAME);
127 callbackCalled = false;
128 jsEngine.evaluate("_triggerEvent('foobar')");
129 assertFalse(callbackCalled);
130 }
131 } 131 }
OLDNEW

Powered by Google App Engine
This is Rietveld