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

Powered by Google App Engine
This is Rietveld