| 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.JsEngine; | |
| 21 import org.adblockplus.libadblockplus.WebRequest; | |
| 22 import org.adblockplus.libadblockplus.android.AndroidWebRequest; | |
| 23 import org.adblockplus.libadblockplus.JsValue; | |
| 24 import org.adblockplus.libadblockplus.ServerResponse; | |
| 25 | |
| 26 import org.junit.Test; | |
| 27 | |
| 28 import java.net.MalformedURLException; | |
| 29 import java.util.List; | |
| 30 | |
| 31 // It inherits the fixture instantiating FilterEngine which is not explicitly | |
| 32 // used in the test bodies in order to provide with JS XMLHttpRequest class | |
| 33 // which is defined in compat.js, but the latter is properly loaded by | |
| 34 // FilterEngine. | |
| 35 public class AndroidWebRequestTest extends BaseFilterEngineTest | |
| 36 { | |
| 37 @Override | |
| 38 protected WebRequest createWebRequest() | |
| 39 { | |
| 40 return new AndroidWebRequest(true, true); | |
| 41 } | |
| 42 | |
| 43 @Test | |
| 44 public void testRealWebRequest() | |
| 45 { | |
| 46 JsEngine jsEngine = platform.getJsEngine(); | |
| 47 // This URL should redirect to easylist-downloads.adblockplus.org and we | |
| 48 // should get the actual filter list back. | |
| 49 jsEngine.evaluate( | |
| 50 "let foo; _webRequest.GET('https://easylist-downloads.adblockplus.org/easy
list.txt', {}, " + | |
| 51 "function(result) {foo = result;} )"); | |
| 52 do | |
| 53 { | |
| 54 try | |
| 55 { | |
| 56 Thread.sleep(200); | |
| 57 } | |
| 58 catch (InterruptedException e) | |
| 59 { | |
| 60 throw new RuntimeException(e); | |
| 61 } | |
| 62 } while (jsEngine.evaluate("foo").isUndefined()); | |
| 63 | |
| 64 String response = jsEngine.evaluate("foo.responseText").asString(); | |
| 65 assertNotNull(response); | |
| 66 assertEquals( | |
| 67 ServerResponse.NsStatus.OK.getStatusCode(), | |
| 68 jsEngine.evaluate("foo.status").asLong()); | |
| 69 assertEquals(200l, jsEngine.evaluate("foo.responseStatus").asLong()); | |
| 70 assertEquals( | |
| 71 "[Adblock Plus ", | |
| 72 jsEngine.evaluate("foo.responseText.substr(0, 14)").asString()); | |
| 73 JsValue jsHeaders = jsEngine.evaluate("foo.responseHeaders"); | |
| 74 assertNotNull(jsHeaders); | |
| 75 assertFalse(jsHeaders.isUndefined()); | |
| 76 assertFalse(jsHeaders.isNull()); | |
| 77 assertTrue(jsHeaders.isObject()); | |
| 78 assertEquals( | |
| 79 "text/plain", | |
| 80 jsEngine.evaluate("foo.responseHeaders['content-type'].substr(0, 10)").asS
tring()); | |
| 81 assertTrue(jsEngine.evaluate("foo.responseHeaders['location']").isUndefined(
)); | |
| 82 } | |
| 83 | |
| 84 @Test | |
| 85 public void testXMLHttpRequest() | |
| 86 { | |
| 87 JsEngine jsEngine = platform.getJsEngine(); | |
| 88 jsEngine.evaluate( | |
| 89 "var result;\n" + | |
| 90 "var request = new XMLHttpRequest();\n" + | |
| 91 "request.open('GET', 'https://easylist-downloads.adblockplus.org/easylist.
txt');\n" + | |
| 92 "request.setRequestHeader('X', 'Y');\n" + | |
| 93 "request.setRequestHeader('X2', 'Y2');\n" + | |
| 94 "request.overrideMimeType('text/plain');\n" + | |
| 95 "request.addEventListener('load',function() {result=request.responseText;}
, false);\n" + | |
| 96 "request.addEventListener('error',function() {result='error';}, false);\n"
+ | |
| 97 "request.send(null);"); | |
| 98 | |
| 99 do | |
| 100 { | |
| 101 try | |
| 102 { | |
| 103 Thread.sleep(200); | |
| 104 } | |
| 105 catch (InterruptedException e) | |
| 106 { | |
| 107 throw new RuntimeException(e); | |
| 108 } | |
| 109 } while (jsEngine.evaluate("result").isUndefined()); | |
| 110 | |
| 111 assertEquals( | |
| 112 ServerResponse.NsStatus.OK.getStatusCode(), | |
| 113 jsEngine.evaluate("request.channel.status").asLong()); | |
| 114 | |
| 115 assertEquals(200l, jsEngine.evaluate("request.status").asLong()); | |
| 116 assertEquals("[Adblock Plus ", jsEngine.evaluate("result.substr(0, 14)").asS
tring()); | |
| 117 assertEquals( | |
| 118 "text/plain", | |
| 119 jsEngine.evaluate("request.getResponseHeader('Content-Type').substr(0, 10)
").asString()); | |
| 120 assertTrue(jsEngine.evaluate("request.getResponseHeader('Location')").isNull
()); | |
| 121 } | |
| 122 | |
| 123 @Test | |
| 124 public void testGetElemhideElements() throws MalformedURLException, Interrupte
dException | |
| 125 { | |
| 126 Thread.sleep(20 * 1000); // wait for the subscription to be downloaded | |
| 127 | |
| 128 final String url = "www.mobile01.com/somepage.html"; | |
| 129 | |
| 130 boolean isDocumentWhitelisted = filterEngine.isDocumentWhitelisted(url, null
); | |
| 131 assertFalse(isDocumentWhitelisted); | |
| 132 | |
| 133 boolean isElemhideWhitelisted = filterEngine.isElemhideWhitelisted(url, null
); | |
| 134 assertFalse(isElemhideWhitelisted); | |
| 135 | |
| 136 List<String> selectors = filterEngine.getElementHidingSelectors(url); | |
| 137 assertNotNull(selectors); | |
| 138 assertTrue(selectors.size() > 0); | |
| 139 } | |
| 140 } | |
| OLD | NEW |