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

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

Issue 29819555: Fix libadblockplus-android tests
Patch Set: Added missing files, removed a whitespace Created Aug. 16, 2018, 12:27 p.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-present eyeo GmbH 3 * Copyright (C) 2006-present 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.JsEngine; 20 import android.os.SystemClock;
21
22 import org.adblockplus.libadblockplus.BaseFilterEngineTest;
23 import org.adblockplus.libadblockplus.HeaderEntry;
21 import org.adblockplus.libadblockplus.WebRequest; 24 import org.adblockplus.libadblockplus.WebRequest;
22 import org.adblockplus.libadblockplus.android.AndroidWebRequest; 25 import org.adblockplus.libadblockplus.android.AndroidWebRequest;
23 import org.adblockplus.libadblockplus.JsValue; 26 import org.adblockplus.libadblockplus.JsValue;
24 import org.adblockplus.libadblockplus.ServerResponse; 27 import org.adblockplus.libadblockplus.ServerResponse;
25 28
26 import org.junit.Test; 29 import org.junit.Test;
27 30
28 import java.net.MalformedURLException; 31 import java.net.MalformedURLException;
29 import java.util.List; 32 import java.util.List;
30 33
31 // It inherits the fixture instantiating FilterEngine which is not explicitly 34 // It inherits the fixture instantiating FilterEngine which is not explicitly
32 // used in the test bodies in order to provide with JS XMLHttpRequest class 35 // 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 36 // which is defined in compat.js, but the latter is properly loaded by
34 // FilterEngine. 37 // FilterEngine.
35 public class AndroidWebRequestTest extends BaseFilterEngineTest 38 public class AndroidWebRequestTest extends BaseFilterEngineTest
36 { 39 {
40 private static class AndroidWebRequestWrapper implements WebRequest
41 {
42 private AndroidWebRequest androidWebRequest = new AndroidWebRequest(true, tr ue);
43 private volatile int requestCount;
44
45 public int getRequestCount()
46 {
47 return requestCount;
48 }
49
50 @Override
51 public ServerResponse httpGET(String url, List<HeaderEntry> headers)
52 {
53 ServerResponse response = androidWebRequest.httpGET(url, headers);
54 requestCount++;
55 return response;
56 }
57 }
58
59 private AndroidWebRequestWrapper webRequestWrapper = new AndroidWebRequestWrap per();
60
37 @Override 61 @Override
38 protected WebRequest createWebRequest() 62 protected void setUp() throws Exception
39 { 63 {
40 return new AndroidWebRequest(true, true); 64 setWebRequest(webRequestWrapper);
65 super.setUp();
41 } 66 }
42 67
43 @Test 68 @Test
44 public void testRealWebRequest() 69 public void testRealWebRequest()
45 { 70 {
46 JsEngine jsEngine = platform.getJsEngine();
47 // This URL should redirect to easylist-downloads.adblockplus.org and we 71 // This URL should redirect to easylist-downloads.adblockplus.org and we
48 // should get the actual filter list back. 72 // should get the actual filter list back.
49 jsEngine.evaluate( 73 jsEngine.evaluate(
50 "let foo; _webRequest.GET('https://easylist-downloads.adblockplus.org/easy list.txt', {}, " + 74 "let foo; _webRequest.GET('https://easylist-downloads.adblockplus.org/easy list.txt', {}, " +
51 "function(result) {foo = result;} )"); 75 "function(result) {foo = result;} )");
52 do 76 do
53 { 77 {
54 try 78 SystemClock.sleep(50);
55 {
56 Thread.sleep(200);
57 }
58 catch (InterruptedException e)
59 {
60 throw new RuntimeException(e);
61 }
62 } while (jsEngine.evaluate("foo").isUndefined()); 79 } while (jsEngine.evaluate("foo").isUndefined());
63 80
64 String response = jsEngine.evaluate("foo.responseText").asString(); 81 String response = jsEngine.evaluate("foo.responseText").asString();
65 assertNotNull(response); 82 assertNotNull(response);
66 assertEquals( 83 assertEquals(
67 ServerResponse.NsStatus.OK.getStatusCode(), 84 ServerResponse.NsStatus.OK.getStatusCode(),
68 jsEngine.evaluate("foo.status").asLong()); 85 jsEngine.evaluate("foo.status").asLong());
69 assertEquals(200l, jsEngine.evaluate("foo.responseStatus").asLong()); 86 assertEquals(200l, jsEngine.evaluate("foo.responseStatus").asLong());
70 assertEquals( 87 assertEquals(
71 "[Adblock Plus ", 88 "[Adblock Plus ",
72 jsEngine.evaluate("foo.responseText.substr(0, 14)").asString()); 89 jsEngine.evaluate("foo.responseText.substr(0, 14)").asString());
73 JsValue jsHeaders = jsEngine.evaluate("foo.responseHeaders"); 90 JsValue jsHeaders = jsEngine.evaluate("foo.responseHeaders");
74 assertNotNull(jsHeaders); 91 assertNotNull(jsHeaders);
75 assertFalse(jsHeaders.isUndefined()); 92 assertFalse(jsHeaders.isUndefined());
76 assertFalse(jsHeaders.isNull()); 93 assertFalse(jsHeaders.isNull());
77 assertTrue(jsHeaders.isObject()); 94 assertTrue(jsHeaders.isObject());
78 assertEquals( 95 assertEquals(
79 "text/plain", 96 "text/plain",
80 jsEngine.evaluate("foo.responseHeaders['content-type'].substr(0, 10)").asS tring()); 97 jsEngine.evaluate("foo.responseHeaders['content-type'].substr(0, 10)").asS tring());
81 assertTrue(jsEngine.evaluate("foo.responseHeaders['location']").isUndefined( )); 98 assertTrue(jsEngine.evaluate("foo.responseHeaders['location']").isUndefined( ));
82 } 99 }
83 100
84 @Test 101 @Test
85 public void testXMLHttpRequest() 102 public void testXMLHttpRequest()
86 { 103 {
87 JsEngine jsEngine = platform.getJsEngine();
88 jsEngine.evaluate( 104 jsEngine.evaluate(
89 "var result;\n" + 105 "var result;\n" +
90 "var request = new XMLHttpRequest();\n" + 106 "var request = new XMLHttpRequest();\n" +
91 "request.open('GET', 'https://easylist-downloads.adblockplus.org/easylist. txt');\n" + 107 "request.open('GET', 'https://easylist-downloads.adblockplus.org/easylist. txt');\n" +
92 "request.setRequestHeader('X', 'Y');\n" + 108 "request.setRequestHeader('X', 'Y');\n" +
93 "request.setRequestHeader('X2', 'Y2');\n" + 109 "request.setRequestHeader('X2', 'Y2');\n" +
94 "request.overrideMimeType('text/plain');\n" + 110 "request.overrideMimeType('text/plain');\n" +
95 "request.addEventListener('load',function() {result=request.responseText;} , false);\n" + 111 "request.addEventListener('load',function() {result=request.responseText;} , false);\n" +
96 "request.addEventListener('error',function() {result='error';}, false);\n" + 112 "request.addEventListener('error',function() {result='error';}, false);\n" +
97 "request.send(null);"); 113 "request.send(null);");
98 114
99 do 115 do
100 { 116 {
101 try 117 SystemClock.sleep(50);
102 {
103 Thread.sleep(200);
104 }
105 catch (InterruptedException e)
106 {
107 throw new RuntimeException(e);
108 }
109 } while (jsEngine.evaluate("result").isUndefined()); 118 } while (jsEngine.evaluate("result").isUndefined());
110 119
111 assertEquals( 120 assertEquals(
112 ServerResponse.NsStatus.OK.getStatusCode(), 121 ServerResponse.NsStatus.OK.getStatusCode(),
113 jsEngine.evaluate("request.channel.status").asLong()); 122 jsEngine.evaluate("request.channel.status").asLong());
114 123
115 assertEquals(200l, jsEngine.evaluate("request.status").asLong()); 124 assertEquals(200l, jsEngine.evaluate("request.status").asLong());
116 assertEquals("[Adblock Plus ", jsEngine.evaluate("result.substr(0, 14)").asS tring()); 125 assertEquals("[Adblock Plus ", jsEngine.evaluate("result.substr(0, 14)").asS tring());
117 assertEquals( 126 assertEquals(
118 "text/plain", 127 "text/plain",
119 jsEngine.evaluate("request.getResponseHeader('Content-Type').substr(0, 10) ").asString()); 128 jsEngine.evaluate("request.getResponseHeader('Content-Type').substr(0, 10) ").asString());
120 assertTrue(jsEngine.evaluate("request.getResponseHeader('Location')").isNull ()); 129 assertTrue(jsEngine.evaluate("request.getResponseHeader('Location')").isNull ());
121 } 130 }
122 131
123 @Test 132 @Test
124 public void testGetElemhideElements() throws MalformedURLException, Interrupte dException 133 public void testGetElemhideElements() throws MalformedURLException
125 { 134 {
126 Thread.sleep(20 * 1000); // wait for the subscription to be downloaded 135 // Wait for Easylist & AA
136 while (webRequestWrapper.getRequestCount() < 2)
137 {
138 SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS / UPDATE_SUBSCRIPTION S_WAIT_CHUNKS);
139 }
140 // A grace period, just in case
141 SystemClock.sleep(UPDATE_SUBSCRIPTIONS_WAIT_DELAY_MS / UPDATE_SUBSCRIPTIONS_ WAIT_CHUNKS);
127 142
128 final String url = "www.mobile01.com/somepage.html"; 143 final String url = "www.mobile01.com/somepage.html";
129 144
130 boolean isDocumentWhitelisted = filterEngine.isDocumentWhitelisted(url, null ); 145 boolean isDocumentWhitelisted = filterEngine.isDocumentWhitelisted(url, null );
131 assertFalse(isDocumentWhitelisted); 146 assertFalse(isDocumentWhitelisted);
132 147
133 boolean isElemhideWhitelisted = filterEngine.isElemhideWhitelisted(url, null ); 148 boolean isElemhideWhitelisted = filterEngine.isElemhideWhitelisted(url, null );
134 assertFalse(isElemhideWhitelisted); 149 assertFalse(isElemhideWhitelisted);
135 150
136 List<String> selectors = filterEngine.getElementHidingSelectors(url); 151 List<String> selectors = filterEngine.getElementHidingSelectors(url);
137 assertNotNull(selectors); 152 assertNotNull(selectors);
138 assertTrue(selectors.size() > 0); 153 assertTrue(selectors.size() > 0);
139 } 154 }
140 } 155 }
OLDNEW

Powered by Google App Engine
This is Rietveld