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

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

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

Powered by Google App Engine
This is Rietveld