Index: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/UpdateCheckTest.java |
diff --git a/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/UpdateCheckTest.java b/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/UpdateCheckTest.java |
index 2ffe0639fc6651be71df6d03ac0ce349c7c7bc29..3f77cb47bbd1d84ed7668f803ef961b9835c7280 100644 |
--- a/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/UpdateCheckTest.java |
+++ b/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/UpdateCheckTest.java |
@@ -30,236 +30,243 @@ import org.adblockplus.libadblockplus.UpdateCheckDoneCallback; |
import org.junit.Test; |
+import java.io.IOException; |
import java.util.List; |
public class UpdateCheckTest extends BaseJsTest |
{ |
- protected String previousRequestUrl; |
+ 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
|
- public class TestWebRequest extends LazyWebRequest |
- { |
- public ServerResponse response = new ServerResponse(); |
- |
- @Override |
- public ServerResponse httpGET(String url, List<HeaderEntry> headers) |
- { |
- if (url.indexOf("easylist") >= 0) |
- return super.httpGET(url, headers); |
- |
- previousRequestUrl = url; |
- return response; |
- } |
- } |
- |
- protected AppInfo appInfo; |
- protected TestWebRequest webRequest; |
- protected JsEngine jsEngine; |
- protected FilterEngine filterEngine; |
- |
- protected boolean eventCallbackCalled; |
- protected List<JsValue> eventCallbackParams; |
- protected boolean updateCallbackCalled; |
- protected String updateError; |
- |
- private EventCallback eventCallback = new EventCallback() |
- { |
- @Override |
- public void eventCallback(List<JsValue> params) |
- { |
- eventCallbackCalled = true; |
- eventCallbackParams = params; |
- } |
- }; |
- |
- private UpdateCheckDoneCallback updateCallback = new UpdateCheckDoneCallback() |
- { |
- @Override |
- public void updateCheckDoneCallback(String error) |
- { |
- updateCallbackCalled = true; |
- updateError = error; |
- } |
- }; |
- |
- public void reset() |
- { |
- jsEngine = new JsEngine(appInfo); |
- jsEngine.setLogSystem(new LazyLogSystem()); |
- jsEngine.setDefaultFileSystem(getContext().getFilesDir().getAbsolutePath()); |
- jsEngine.setWebRequest(webRequest); |
- jsEngine.setEventCallback("updateAvailable", eventCallback); |
- |
- filterEngine = new FilterEngine(jsEngine); |
- } |
+ public class TestWebRequest extends LazyWebRequest |
+ { |
+ public ServerResponse response = new ServerResponse(); |
@Override |
- protected void setUp() throws Exception |
+ public ServerResponse httpGET(String url, List<HeaderEntry> headers) |
{ |
- super.setUp(); |
+ if (url.indexOf("easylist") >= 0) |
+ { |
+ return super.httpGET(url, headers); |
+ } |
- appInfo = AppInfo.builder().build(); |
- webRequest = new TestWebRequest(); |
- eventCallbackCalled = false; |
- updateCallbackCalled = false; |
- reset(); |
+ previousRequestUrl = url; |
+ return response; |
} |
+ } |
- public void forceUpdateCheck() |
- { |
- filterEngine.forceUpdateCheck(updateCallback); |
- } |
- |
- @Test |
- public void testRequestFailure() throws InterruptedException |
- { |
- webRequest.response.setStatus(ServerResponse.NsStatus.ERROR_FAILURE); |
- |
- appInfo = AppInfo |
- .builder() |
- .setName("1") |
- .setVersion("3") |
- .setApplication("4") |
- .setApplicationVersion("2") |
- .setDevelopmentBuild(false) |
- .build(); |
- |
- reset(); |
- forceUpdateCheck(); |
- |
- Thread.sleep(100); |
- |
- assertFalse(eventCallbackCalled); |
- assertTrue(updateCallbackCalled); |
- assertNotNull(updateError); |
- |
- String expectedUrl = filterEngine.getPref("update_url_release").asString(); |
- String platform = "libadblockplus"; |
- String platformVersion = "1.0"; |
- |
- expectedUrl = expectedUrl |
- .replaceAll("%NAME%", appInfo.name) |
- .replaceAll("%TYPE%", "1"); // manual update |
- |
- expectedUrl += |
- "&addonName=" + appInfo.name + |
- "&addonVersion=" + appInfo.version + |
- "&application=" + appInfo.application + |
- "&applicationVersion=" + appInfo.applicationVersion + |
- "&platform=" + platform + |
- "&platformVersion=" + platformVersion + |
- "&lastVersion=0&downloadCount=0"; |
- |
- assertEquals(expectedUrl, previousRequestUrl); |
- } |
- |
- @Test |
- public void testApplicationUpdateAvailable() throws InterruptedException |
- { |
- webRequest.response.setStatus(ServerResponse.NsStatus.OK); |
- webRequest.response.setResponseStatus(200); |
- webRequest.response.setResponse( |
- "{\"1/4\": {\"version\":\"3.1\",\"url\":\"https://foo.bar/\"}}"); |
- |
- appInfo = AppInfo |
- .builder() |
- .setName("1") |
- .setVersion("3") |
- .setApplication("4") |
- .setApplicationVersion("2") |
- .setDevelopmentBuild(true) |
- .build(); |
- |
- reset(); |
- forceUpdateCheck(); |
- |
- Thread.sleep(1000); |
- |
- assertTrue(eventCallbackCalled); |
- assertNotNull(eventCallbackParams); |
- assertEquals(1l, eventCallbackParams.size()); |
- assertEquals("https://foo.bar/", eventCallbackParams.get(0).asString()); |
- assertTrue(updateCallbackCalled); |
- assertEquals("", updateError); |
- } |
+ protected AppInfo appInfo; |
+ protected TestWebRequest webRequest; |
+ protected JsEngine jsEngine; |
+ protected FilterEngine filterEngine; |
- @Test |
- public void testWrongApplication() throws InterruptedException |
- { |
- webRequest.response.setStatus(ServerResponse.NsStatus.OK); |
- webRequest.response.setResponseStatus(200); |
- webRequest.response.setResponse( |
- "{\"1/3\": {\"version\":\"3.1\",\"url\":\"https://foo.bar/\"}}"); |
- |
- appInfo = AppInfo |
- .builder() |
- .setName("1") |
- .setVersion("3") |
- .setApplication("4") |
- .setApplicationVersion("2") |
- .setDevelopmentBuild(true) |
- .build(); |
- |
- reset(); |
- forceUpdateCheck(); |
- |
- Thread.sleep(1000); |
- |
- assertFalse(eventCallbackCalled); |
- assertTrue(updateCallbackCalled); |
- assertEquals("", updateError); |
- } |
+ protected boolean eventCallbackCalled; |
+ protected List<JsValue> eventCallbackParams; |
+ protected boolean updateCallbackCalled; |
+ protected String updateError; |
- @Test |
- public void testWrongVersion() throws InterruptedException |
+ private EventCallback eventCallback = new EventCallback() |
+ { |
+ @Override |
+ public void eventCallback(List<JsValue> params) |
{ |
- webRequest.response.setStatus(ServerResponse.NsStatus.OK); |
- webRequest.response.setResponseStatus(200); |
- webRequest.response.setResponse( |
- "{\"1\": {\"version\":\"3\",\"url\":\"https://foo.bar/\"}}"); |
- |
- appInfo = AppInfo |
- .builder() |
- .setName("1") |
- .setVersion("3") |
- .setApplication("4") |
- .setApplicationVersion("2") |
- .setDevelopmentBuild(true) |
- .build(); |
- |
- reset(); |
- forceUpdateCheck(); |
- |
- Thread.sleep(1000); |
- |
- assertFalse(eventCallbackCalled); |
- assertTrue(updateCallbackCalled); |
- assertEquals("", updateError); |
+ eventCallbackCalled = true; |
+ eventCallbackParams = params; |
} |
+ }; |
- @Test |
- public void testWrongURL() throws InterruptedException |
+ private UpdateCheckDoneCallback updateCallback = new UpdateCheckDoneCallback() |
+ { |
+ @Override |
+ public void updateCheckDoneCallback(String error) |
{ |
- webRequest.response.setStatus(ServerResponse.NsStatus.OK); |
- webRequest.response.setResponseStatus(200); |
- webRequest.response.setResponse( |
- "{\"1\": {\"version\":\"3.1\",\"url\":\"http://insecure/\"}}"); |
- |
- appInfo = AppInfo |
- .builder() |
- .setName("1") |
- .setVersion("3") |
- .setApplication("4") |
- .setApplicationVersion("2") |
- .setDevelopmentBuild(true) |
- .build(); |
- |
- reset(); |
- forceUpdateCheck(); |
- |
- Thread.sleep(1000); |
- |
- assertFalse(eventCallbackCalled); |
- assertTrue(updateCallbackCalled); |
- assertTrue(updateError.length() > 0); |
+ updateCallbackCalled = true; |
+ updateError = error; |
} |
+ }; |
+ |
+ public void reset() throws IOException |
+ { |
+ jsEngine = new JsEngine(appInfo); |
+ jsEngine.setLogSystem(new LazyLogSystem()); |
+ |
+ cleanupFileSystem(); |
+ tmpFileSystemPath = buildTmpFileSystemPath(); |
+ jsEngine.setDefaultFileSystem(tmpFileSystemPath.getAbsolutePath()); |
+ |
+ jsEngine.setWebRequest(webRequest); |
+ jsEngine.setEventCallback("updateAvailable", eventCallback); |
+ |
+ filterEngine = new FilterEngine(jsEngine); |
+ } |
+ |
+ @Override |
+ protected void setUp() throws Exception |
+ { |
+ super.setUp(); |
+ |
+ appInfo = AppInfo.builder().build(); |
+ webRequest = new TestWebRequest(); |
+ eventCallbackCalled = false; |
+ updateCallbackCalled = false; |
+ reset(); |
+ } |
+ |
+ public void forceUpdateCheck() |
+ { |
+ filterEngine.forceUpdateCheck(updateCallback); |
+ } |
+ |
+ @Test |
+ public void testRequestFailure() throws InterruptedException, IOException |
+ { |
+ webRequest.response.setStatus(ServerResponse.NsStatus.ERROR_FAILURE); |
+ |
+ appInfo = AppInfo |
+ .builder() |
+ .setName("1") |
+ .setVersion("3") |
+ .setApplication("4") |
+ .setApplicationVersion("2") |
+ .setDevelopmentBuild(false) |
+ .build(); |
+ |
+ reset(); |
+ forceUpdateCheck(); |
+ |
+ Thread.sleep(100); |
+ |
+ assertFalse(eventCallbackCalled); |
+ assertTrue(updateCallbackCalled); |
+ assertNotNull(updateError); |
+ |
+ String expectedUrl = filterEngine.getPref("update_url_release").asString(); |
+ String platform = "libadblockplus"; |
+ String platformVersion = "1.0"; |
+ |
+ expectedUrl = expectedUrl |
+ .replaceAll("%NAME%", appInfo.name) |
+ .replaceAll("%TYPE%", "1"); // manual update |
+ |
+ expectedUrl += |
+ "&addonName=" + appInfo.name + |
+ "&addonVersion=" + appInfo.version + |
+ "&application=" + appInfo.application + |
+ "&applicationVersion=" + appInfo.applicationVersion + |
+ "&platform=" + platform + |
+ "&platformVersion=" + platformVersion + |
+ "&lastVersion=0&downloadCount=0"; |
+ |
+ assertEquals(expectedUrl, previousRequestUrl); |
+ } |
+ |
+ @Test |
+ public void testApplicationUpdateAvailable() throws InterruptedException, IOException |
+ { |
+ webRequest.response.setStatus(ServerResponse.NsStatus.OK); |
+ webRequest.response.setResponseStatus(200); |
+ webRequest.response.setResponse( |
+ "{\"1/4\": {\"version\":\"3.1\",\"url\":\"https://foo.bar/\"}}"); |
+ |
+ appInfo = AppInfo |
+ .builder() |
+ .setName("1") |
+ .setVersion("3") |
+ .setApplication("4") |
+ .setApplicationVersion("2") |
+ .setDevelopmentBuild(true) |
+ .build(); |
+ |
+ reset(); |
+ forceUpdateCheck(); |
+ |
+ Thread.sleep(1000); |
+ |
+ assertTrue(eventCallbackCalled); |
+ assertNotNull(eventCallbackParams); |
+ assertEquals(1l, eventCallbackParams.size()); |
+ assertEquals("https://foo.bar/", eventCallbackParams.get(0).asString()); |
+ assertTrue(updateCallbackCalled); |
+ assertEquals("", updateError); |
+ } |
+ |
+ @Test |
+ public void testWrongApplication() throws InterruptedException, IOException |
+ { |
+ webRequest.response.setStatus(ServerResponse.NsStatus.OK); |
+ webRequest.response.setResponseStatus(200); |
+ webRequest.response.setResponse( |
+ "{\"1/3\": {\"version\":\"3.1\",\"url\":\"https://foo.bar/\"}}"); |
+ |
+ appInfo = AppInfo |
+ .builder() |
+ .setName("1") |
+ .setVersion("3") |
+ .setApplication("4") |
+ .setApplicationVersion("2") |
+ .setDevelopmentBuild(true) |
+ .build(); |
+ |
+ reset(); |
+ forceUpdateCheck(); |
+ |
+ Thread.sleep(1000); |
+ |
+ assertFalse(eventCallbackCalled); |
+ assertTrue(updateCallbackCalled); |
+ assertEquals("", updateError); |
+ } |
+ |
+ @Test |
+ public void testWrongVersion() throws InterruptedException, IOException |
+ { |
+ webRequest.response.setStatus(ServerResponse.NsStatus.OK); |
+ webRequest.response.setResponseStatus(200); |
+ webRequest.response.setResponse( |
+ "{\"1\": {\"version\":\"3\",\"url\":\"https://foo.bar/\"}}"); |
+ |
+ appInfo = AppInfo |
+ .builder() |
+ .setName("1") |
+ .setVersion("3") |
+ .setApplication("4") |
+ .setApplicationVersion("2") |
+ .setDevelopmentBuild(true) |
+ .build(); |
+ |
+ reset(); |
+ forceUpdateCheck(); |
+ |
+ Thread.sleep(1000); |
+ |
+ assertFalse(eventCallbackCalled); |
+ assertTrue(updateCallbackCalled); |
+ assertEquals("", updateError); |
+ } |
+ |
+ @Test |
+ public void testWrongURL() throws InterruptedException, IOException |
+ { |
+ webRequest.response.setStatus(ServerResponse.NsStatus.OK); |
+ webRequest.response.setResponseStatus(200); |
+ webRequest.response.setResponse( |
+ "{\"1\": {\"version\":\"3.1\",\"url\":\"http://insecure/\"}}"); |
+ |
+ appInfo = AppInfo |
+ .builder() |
+ .setName("1") |
+ .setVersion("3") |
+ .setApplication("4") |
+ .setApplicationVersion("2") |
+ .setDevelopmentBuild(true) |
+ .build(); |
+ |
+ reset(); |
+ forceUpdateCheck(); |
+ |
+ Thread.sleep(1000); |
+ |
+ assertFalse(eventCallbackCalled); |
+ assertTrue(updateCallbackCalled); |
+ assertTrue(updateError.length() > 0); |
+ } |
} |