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

Delta Between Two Patch Sets: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/UpdateCheckTest.java

Issue 29347192: Issue 4181 - Fix FilterEngineTest tests (Closed)
Left Patch Set: Created July 1, 2016, 1:41 p.m.
Right Patch Set: marked method as static Created Dec. 2, 2016, 6:14 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
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
(...skipping 12 matching lines...) Expand all
23 import org.adblockplus.libadblockplus.HeaderEntry; 23 import org.adblockplus.libadblockplus.HeaderEntry;
24 import org.adblockplus.libadblockplus.JsEngine; 24 import org.adblockplus.libadblockplus.JsEngine;
25 import org.adblockplus.libadblockplus.JsValue; 25 import org.adblockplus.libadblockplus.JsValue;
26 import org.adblockplus.libadblockplus.LazyLogSystem; 26 import org.adblockplus.libadblockplus.LazyLogSystem;
27 import org.adblockplus.libadblockplus.LazyWebRequest; 27 import org.adblockplus.libadblockplus.LazyWebRequest;
28 import org.adblockplus.libadblockplus.ServerResponse; 28 import org.adblockplus.libadblockplus.ServerResponse;
29 import org.adblockplus.libadblockplus.UpdateCheckDoneCallback; 29 import org.adblockplus.libadblockplus.UpdateCheckDoneCallback;
30 30
31 import org.junit.Test; 31 import org.junit.Test;
32 32
33 import java.io.File;
34 import java.io.IOException; 33 import java.io.IOException;
35 import java.util.List; 34 import java.util.List;
36 35
37 public class UpdateCheckTest extends BaseJsTest 36 public class UpdateCheckTest extends BaseJsTest
38 { 37 {
39 protected String previousRequestUrl; 38 protected String previousRequestUrl;
Felix Dahlke 2017/05/09 08:06:45 Looks like more indentation changes. As before, co
anton 2017/05/10 11:00:14 Lot's of changes were done since that times, this
40 39
41 public class TestWebRequest extends LazyWebRequest 40 public class TestWebRequest extends LazyWebRequest
41 {
42 public ServerResponse response = new ServerResponse();
43
44 @Override
45 public ServerResponse httpGET(String url, List<HeaderEntry> headers)
42 { 46 {
43 public ServerResponse response = new ServerResponse(); 47 if (url.indexOf("easylist") >= 0)
44 48 {
45 @Override 49 return super.httpGET(url, headers);
46 public ServerResponse httpGET(String url, List<HeaderEntry> headers) 50 }
47 { 51
48 if (url.indexOf("easylist") >= 0) 52 previousRequestUrl = url;
49 return super.httpGET(url, headers); 53 return response;
50
51 previousRequestUrl = url;
52 return response;
53 }
54 } 54 }
55 55 }
56 protected AppInfo appInfo; 56
57 protected TestWebRequest webRequest; 57 protected AppInfo appInfo;
58 protected JsEngine jsEngine; 58 protected TestWebRequest webRequest;
59 protected FilterEngine filterEngine; 59 protected JsEngine jsEngine;
60 60 protected FilterEngine filterEngine;
61 protected boolean eventCallbackCalled; 61
62 protected List<JsValue> eventCallbackParams; 62 protected boolean eventCallbackCalled;
63 protected boolean updateCallbackCalled; 63 protected List<JsValue> eventCallbackParams;
64 protected String updateError; 64 protected boolean updateCallbackCalled;
65 65 protected String updateError;
66 private EventCallback eventCallback = new EventCallback() 66
67 private EventCallback eventCallback = new EventCallback()
68 {
69 @Override
70 public void eventCallback(List<JsValue> params)
67 { 71 {
68 @Override 72 eventCallbackCalled = true;
69 public void eventCallback(List<JsValue> params) 73 eventCallbackParams = params;
70 { 74 }
71 eventCallbackCalled = true; 75 };
72 eventCallbackParams = params; 76
73 } 77 private UpdateCheckDoneCallback updateCallback = new UpdateCheckDoneCallback()
74 }; 78 {
75 79 @Override
76 private UpdateCheckDoneCallback updateCallback = new UpdateCheckDoneCallback () 80 public void updateCheckDoneCallback(String error)
77 { 81 {
78 @Override 82 updateCallbackCalled = true;
79 public void updateCheckDoneCallback(String error) 83 updateError = error;
80 {
81 updateCallbackCalled = true;
82 updateError = error;
83 }
84 };
85
86 public void reset() throws IOException
87 {
88 jsEngine = new JsEngine(appInfo);
89 jsEngine.setLogSystem(new LazyLogSystem());
90
91 cleanupFileSystem();
92 tmpFileSystemPath = buildTmpFileSystemPath();
93 jsEngine.setDefaultFileSystem(tmpFileSystemPath.getAbsolutePath());
94
95 jsEngine.setWebRequest(webRequest);
96 jsEngine.setEventCallback("updateAvailable", eventCallback);
97
98 filterEngine = new FilterEngine(jsEngine);
99 } 84 }
100 85 };
101 @Override 86
102 protected void setUp() throws Exception 87 public void reset() throws IOException
103 { 88 {
104 super.setUp(); 89 jsEngine = new JsEngine(appInfo);
105 90 jsEngine.setLogSystem(new LazyLogSystem());
106 appInfo = AppInfo.builder().build(); 91
107 webRequest = new TestWebRequest(); 92 cleanupFileSystem();
108 eventCallbackCalled = false; 93 tmpFileSystemPath = buildTmpFileSystemPath();
109 updateCallbackCalled = false; 94 jsEngine.setDefaultFileSystem(tmpFileSystemPath.getAbsolutePath());
110 reset(); 95
111 } 96 jsEngine.setWebRequest(webRequest);
112 97 jsEngine.setEventCallback("updateAvailable", eventCallback);
113 public void forceUpdateCheck() 98
114 { 99 filterEngine = new FilterEngine(jsEngine);
115 filterEngine.forceUpdateCheck(updateCallback); 100 }
116 } 101
117 102 @Override
118 @Test 103 protected void setUp() throws Exception
119 public void testRequestFailure() throws InterruptedException, IOException 104 {
120 { 105 super.setUp();
121 webRequest.response.setStatus(ServerResponse.NsStatus.ERROR_FAILURE); 106
122 107 appInfo = AppInfo.builder().build();
123 appInfo = AppInfo 108 webRequest = new TestWebRequest();
124 .builder() 109 eventCallbackCalled = false;
125 .setName("1") 110 updateCallbackCalled = false;
126 .setVersion("3") 111 reset();
127 .setApplication("4") 112 }
128 .setApplicationVersion("2") 113
129 .setDevelopmentBuild(false) 114 public void forceUpdateCheck()
130 .build(); 115 {
131 116 filterEngine.forceUpdateCheck(updateCallback);
132 reset(); 117 }
133 forceUpdateCheck(); 118
134 119 @Test
135 Thread.sleep(100); 120 public void testRequestFailure() throws InterruptedException, IOException
136 121 {
137 assertFalse(eventCallbackCalled); 122 webRequest.response.setStatus(ServerResponse.NsStatus.ERROR_FAILURE);
138 assertTrue(updateCallbackCalled); 123
139 assertNotNull(updateError); 124 appInfo = AppInfo
140 125 .builder()
141 String expectedUrl = filterEngine.getPref("update_url_release").asString (); 126 .setName("1")
142 String platform = "libadblockplus"; 127 .setVersion("3")
143 String platformVersion = "1.0"; 128 .setApplication("4")
144 129 .setApplicationVersion("2")
145 expectedUrl = expectedUrl 130 .setDevelopmentBuild(false)
146 .replaceAll("%NAME%", appInfo.name) 131 .build();
147 .replaceAll("%TYPE%", "1"); // manual update 132
148 133 reset();
149 expectedUrl += 134 forceUpdateCheck();
150 "&addonName=" + appInfo.name + 135
151 "&addonVersion=" + appInfo.version + 136 Thread.sleep(100);
152 "&application=" + appInfo.application + 137
153 "&applicationVersion=" + appInfo.applicationVersion + 138 assertFalse(eventCallbackCalled);
154 "&platform=" + platform + 139 assertTrue(updateCallbackCalled);
155 "&platformVersion=" + platformVersion + 140 assertNotNull(updateError);
156 "&lastVersion=0&downloadCount=0"; 141
157 142 String expectedUrl = filterEngine.getPref("update_url_release").asString();
158 assertEquals(expectedUrl, previousRequestUrl); 143 String platform = "libadblockplus";
159 } 144 String platformVersion = "1.0";
160 145
161 @Test 146 expectedUrl = expectedUrl
162 public void testApplicationUpdateAvailable() throws InterruptedException, IO Exception 147 .replaceAll("%NAME%", appInfo.name)
163 { 148 .replaceAll("%TYPE%", "1"); // manual update
164 webRequest.response.setStatus(ServerResponse.NsStatus.OK); 149
165 webRequest.response.setResponseStatus(200); 150 expectedUrl +=
166 webRequest.response.setResponse( 151 "&addonName=" + appInfo.name +
167 "{\"1/4\": {\"version\":\"3.1\",\"url\":\"https://foo.bar/\"}}"); 152 "&addonVersion=" + appInfo.version +
168 153 "&application=" + appInfo.application +
169 appInfo = AppInfo 154 "&applicationVersion=" + appInfo.applicationVersion +
170 .builder() 155 "&platform=" + platform +
171 .setName("1") 156 "&platformVersion=" + platformVersion +
172 .setVersion("3") 157 "&lastVersion=0&downloadCount=0";
173 .setApplication("4") 158
174 .setApplicationVersion("2") 159 assertEquals(expectedUrl, previousRequestUrl);
175 .setDevelopmentBuild(true) 160 }
176 .build(); 161
177 162 @Test
178 reset(); 163 public void testApplicationUpdateAvailable() throws InterruptedException, IOEx ception
179 forceUpdateCheck(); 164 {
180 165 webRequest.response.setStatus(ServerResponse.NsStatus.OK);
181 Thread.sleep(1000); 166 webRequest.response.setResponseStatus(200);
182 167 webRequest.response.setResponse(
183 assertTrue(eventCallbackCalled); 168 "{\"1/4\": {\"version\":\"3.1\",\"url\":\"https://foo.bar/\"}}");
184 assertNotNull(eventCallbackParams); 169
185 assertEquals(1l, eventCallbackParams.size()); 170 appInfo = AppInfo
186 assertEquals("https://foo.bar/", eventCallbackParams.get(0).asString()); 171 .builder()
187 assertTrue(updateCallbackCalled); 172 .setName("1")
188 assertEquals("", updateError); 173 .setVersion("3")
189 } 174 .setApplication("4")
190 175 .setApplicationVersion("2")
191 @Test 176 .setDevelopmentBuild(true)
192 public void testWrongApplication() throws InterruptedException, IOException 177 .build();
193 { 178
194 webRequest.response.setStatus(ServerResponse.NsStatus.OK); 179 reset();
195 webRequest.response.setResponseStatus(200); 180 forceUpdateCheck();
196 webRequest.response.setResponse( 181
197 "{\"1/3\": {\"version\":\"3.1\",\"url\":\"https://foo.bar/\"}}"); 182 Thread.sleep(1000);
198 183
199 appInfo = AppInfo 184 assertTrue(eventCallbackCalled);
200 .builder() 185 assertNotNull(eventCallbackParams);
201 .setName("1") 186 assertEquals(1l, eventCallbackParams.size());
202 .setVersion("3") 187 assertEquals("https://foo.bar/", eventCallbackParams.get(0).asString());
203 .setApplication("4") 188 assertTrue(updateCallbackCalled);
204 .setApplicationVersion("2") 189 assertEquals("", updateError);
205 .setDevelopmentBuild(true) 190 }
206 .build(); 191
207 192 @Test
208 reset(); 193 public void testWrongApplication() throws InterruptedException, IOException
209 forceUpdateCheck(); 194 {
210 195 webRequest.response.setStatus(ServerResponse.NsStatus.OK);
211 Thread.sleep(1000); 196 webRequest.response.setResponseStatus(200);
212 197 webRequest.response.setResponse(
213 assertFalse(eventCallbackCalled); 198 "{\"1/3\": {\"version\":\"3.1\",\"url\":\"https://foo.bar/\"}}");
214 assertTrue(updateCallbackCalled); 199
215 assertEquals("", updateError); 200 appInfo = AppInfo
216 } 201 .builder()
217 202 .setName("1")
218 @Test 203 .setVersion("3")
219 public void testWrongVersion() throws InterruptedException, IOException 204 .setApplication("4")
220 { 205 .setApplicationVersion("2")
221 webRequest.response.setStatus(ServerResponse.NsStatus.OK); 206 .setDevelopmentBuild(true)
222 webRequest.response.setResponseStatus(200); 207 .build();
223 webRequest.response.setResponse( 208
224 "{\"1\": {\"version\":\"3\",\"url\":\"https://foo.bar/\"}}"); 209 reset();
225 210 forceUpdateCheck();
226 appInfo = AppInfo 211
227 .builder() 212 Thread.sleep(1000);
228 .setName("1") 213
229 .setVersion("3") 214 assertFalse(eventCallbackCalled);
230 .setApplication("4") 215 assertTrue(updateCallbackCalled);
231 .setApplicationVersion("2") 216 assertEquals("", updateError);
232 .setDevelopmentBuild(true) 217 }
233 .build(); 218
234 219 @Test
235 reset(); 220 public void testWrongVersion() throws InterruptedException, IOException
236 forceUpdateCheck(); 221 {
237 222 webRequest.response.setStatus(ServerResponse.NsStatus.OK);
238 Thread.sleep(1000); 223 webRequest.response.setResponseStatus(200);
239 224 webRequest.response.setResponse(
240 assertFalse(eventCallbackCalled); 225 "{\"1\": {\"version\":\"3\",\"url\":\"https://foo.bar/\"}}");
241 assertTrue(updateCallbackCalled); 226
242 assertEquals("", updateError); 227 appInfo = AppInfo
243 } 228 .builder()
244 229 .setName("1")
245 @Test 230 .setVersion("3")
246 public void testWrongURL() throws InterruptedException, IOException 231 .setApplication("4")
247 { 232 .setApplicationVersion("2")
248 webRequest.response.setStatus(ServerResponse.NsStatus.OK); 233 .setDevelopmentBuild(true)
249 webRequest.response.setResponseStatus(200); 234 .build();
250 webRequest.response.setResponse( 235
251 "{\"1\": {\"version\":\"3.1\",\"url\":\"http://insecure/\"}}"); 236 reset();
252 237 forceUpdateCheck();
253 appInfo = AppInfo 238
254 .builder() 239 Thread.sleep(1000);
255 .setName("1") 240
256 .setVersion("3") 241 assertFalse(eventCallbackCalled);
257 .setApplication("4") 242 assertTrue(updateCallbackCalled);
258 .setApplicationVersion("2") 243 assertEquals("", updateError);
259 .setDevelopmentBuild(true) 244 }
260 .build(); 245
261 246 @Test
262 reset(); 247 public void testWrongURL() throws InterruptedException, IOException
263 forceUpdateCheck(); 248 {
264 249 webRequest.response.setStatus(ServerResponse.NsStatus.OK);
265 Thread.sleep(1000); 250 webRequest.response.setResponseStatus(200);
266 251 webRequest.response.setResponse(
267 assertFalse(eventCallbackCalled); 252 "{\"1\": {\"version\":\"3.1\",\"url\":\"http://insecure/\"}}");
268 assertTrue(updateCallbackCalled); 253
269 assertTrue(updateError.length() > 0); 254 appInfo = AppInfo
270 } 255 .builder()
256 .setName("1")
257 .setVersion("3")
258 .setApplication("4")
259 .setApplicationVersion("2")
260 .setDevelopmentBuild(true)
261 .build();
262
263 reset();
264 forceUpdateCheck();
265
266 Thread.sleep(1000);
267
268 assertFalse(eventCallbackCalled);
269 assertTrue(updateCallbackCalled);
270 assertTrue(updateError.length() > 0);
271 }
271 } 272 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld